hdu 5877 Weak Pair (Treap)

时间:2020-12-08 19:54:04

链接:http://acm.hdu.edu.cn/showproblem.php?pid=5877

题面;

Weak Pair

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 5706    Accepted Submission(s): 1617

Problem Description
You are given a rootedhdu 5877 Weak Pair (Treap)

tree of Nhdu 5877 Weak Pair (Treap)

nodes, labeled from 1 to Nhdu 5877 Weak Pair (Treap)

. To the ihdu 5877 Weak Pair (Treap)

th node a non-negative value ahdu 5877 Weak Pair (Treap)ihdu 5877 Weak Pair (Treap)hdu 5877 Weak Pair (Treap)

is assigned.An orderedhdu 5877 Weak Pair (Treap)

pair of nodes (u,v)hdu 5877 Weak Pair (Treap)

is said to be weakhdu 5877 Weak Pair (Treap)

if
  (1) uhdu 5877 Weak Pair (Treap)

is an ancestor of vhdu 5877 Weak Pair (Treap)

(Note: In this problem a node uhdu 5877 Weak Pair (Treap)

is not considered an ancestor of itself);
  (2) ahdu 5877 Weak Pair (Treap)uhdu 5877 Weak Pair (Treap)×ahdu 5877 Weak Pair (Treap)vhdu 5877 Weak Pair (Treap)≤khdu 5877 Weak Pair (Treap)

.

Can you find the number of weak pairs in the tree?

 
Input
There are multiple cases in the data set.
  The first line of input contains an integer Thdu 5877 Weak Pair (Treap)

denoting number of test cases.
  For each case, the first line contains two space-separated integers, Nhdu 5877 Weak Pair (Treap)

and khdu 5877 Weak Pair (Treap)

, respectively.
  The second line contains Nhdu 5877 Weak Pair (Treap)

space-separated integers, denoting ahdu 5877 Weak Pair (Treap)1hdu 5877 Weak Pair (Treap)hdu 5877 Weak Pair (Treap)

to ahdu 5877 Weak Pair (Treap)Nhdu 5877 Weak Pair (Treap)hdu 5877 Weak Pair (Treap)

.
  Each of the subsequent lines contains two space-separated integers defining an edge connecting nodes uhdu 5877 Weak Pair (Treap)

and vhdu 5877 Weak Pair (Treap)

, where node uhdu 5877 Weak Pair (Treap)

is the parent of node vhdu 5877 Weak Pair (Treap)

.

Constrains:
  
  1≤N≤10hdu 5877 Weak Pair (Treap)5hdu 5877 Weak Pair (Treap)hdu 5877 Weak Pair (Treap)

0≤ahdu 5877 Weak Pair (Treap)ihdu 5877 Weak Pair (Treap)≤10hdu 5877 Weak Pair (Treap)9hdu 5877 Weak Pair (Treap)hdu 5877 Weak Pair (Treap)

0≤k≤10hdu 5877 Weak Pair (Treap)18hdu 5877 Weak Pair (Treap)hdu 5877 Weak Pair (Treap)

 
Output
For each test case, print a single integer on a single line denoting the number of weak pairs in the tree.
 
Sample Input
1
2 3
1 2
1 2
 
Sample Output
1
 
Source
 
 
 
思路: treap板子题,注意并没有规定1为根,根要自己找下,还有a[i]可能为0,此时需要特判下
初始化没清空左右儿子,一直RE,真实自闭
实现代码;
#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ll long long
#define ls t[x].ch[0]
#define rs t[x].ch[1]
const ll M = 2e5 +;
const ll inf = 1e18+;
ll rt,sz,ans,a[M],n,k;
struct node{
ll ch[],cnt,siz,val,rd;
}t[M];
vector<ll>g[M];
void up(ll x){
t[x].siz = t[ls].siz + t[rs].siz+t[x].cnt;
} void rotate(ll &x,ll d){
ll son = t[x].ch[d];
t[x].ch[d] = t[son].ch[d^];
t[son].ch[d^] = x; up(x); up(x=son);
} void ins(ll &x,ll val){
if(!x){
x = ++sz;
t[x].cnt = t[x].siz = ;
t[x].val = val,t[x].rd = rand();
return ;
}
t[x].siz ++;
if(t[x].val == val){
t[x].cnt++; return ;
}
ll d = t[x].val < val; ins(t[x].ch[d],val);
if(t[x].rd > t[t[x].ch[d]].rd) rotate(x,d);
} void del(ll &x,ll val){
if(!x) return ;
if(t[x].val == val){
if(t[x].cnt > ){
t[x].cnt--,t[x].siz--;return ;
}
bool d = t[ls].rd > t[rs].rd;
if(ls == ||rs == ) x = ls+rs;
else rotate(x,d),del(x,val);
}
else t[x].siz--,del(t[x].ch[t[x].val<val],val);
} ll rk(ll x,ll val){
if(!x) return ;
if(t[x].val == val) return t[ls].siz+t[x].cnt;
if(t[x].val > val) return rk(ls,val);
return rk(rs,val)+t[ls].siz+t[x].cnt;
} void dfs(ll u,ll f){
ll num = inf;
if(a[u]!=) num = k/a[u];
ans += rk(rt,num);
ins(rt,a[u]);
for(ll i = ;i < g[u].size();i ++){
ll v = g[u][i];
if(v == f) continue;
dfs(v,u);
}
del(rt,a[u]);
} ll d[M]; void init(){
for(ll i = ;i < M;i ++){
t[i].val = ;d[i] = ;t[i].cnt=,t[i].siz = ;
t[i].ch[] = ; t[i].ch[] = ;
}
} int main()
{
ios::sync_with_stdio();
cin.tie(); cout.tie();
ll t,x,y;
cin>>t;
while(t--){
rt = ,ans = ,sz = ;
init();
cin>>n>>k;
for(ll i = ;i <= n;i ++) cin>>a[i];
for(ll i = ;i < n;i ++){
cin>>x>>y;
g[x].push_back(y);
g[y].push_back(x);
d[y]++;
}
for(ll i = ;i <= n;i ++)
if(d[i]==) {
dfs(i,); break;
}
cout<<ans<<endl;
for(ll i = ;i <= n ;i ++) g[i].clear();
}
}