1 //tree-shaped dp+ tree array HDU 5877 Weak Pair2 //idea: Each time a tree array is added k/a[i], each node ans+=sum (A[i]) represents a value that is increased by equal to a[i] each time3 //This problem needs to be discretized .4 5#include <bits/stdc++.h>6 using namespacestd;7 #defineLL Long Long8typedef pair<int,int>PII;9 Const DoubleINF =123456789012345.0;Ten ConstLL MOD =100000000LL; One Const intN = 2e5+Ten; A Const intMaxx =200010; - #defineCLC (A, B) memset (A,b,sizeof (a)) - Const DoubleEPS = 1e-7; the voidFre () {freopen ("In.txt","R", stdin);} - voidFreout () {freopen ("OUT.txt","W", stdout);} -InlineintRead () {intx=0, f=1;CharCh=getchar (); while(ch>'9'|| ch<'0') {if(ch=='-') f=-1; Ch=getchar ();} while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; Ch=getchar ();}returnx*F;} - +Map<ll,ll>Ma; - LL A[n]; + LL C[n],b[n]; ALLinch[N]; atVector<ll>G[n]; -ll Lowbit (ll x) {returnx& (-x);} -ll add (ll X,intt) { - while(x>0){ -c[x]+=T; -x-=lowbit (x); in } - } to ll Sum (ll x) { +LL sum=0; - while(x<Maxx) { thesum+=C[x]; *x+=lowbit (x); $ }Panax Notoginseng returnsum; - } the +LL ans=0; A LL n,k; the voidDFS (LL RT) { + for(LL i=0;i< (int) g[rt].size (); i++){ -LL v=G[rt][i]; $ans+=Sum (Ma[a[v]]); $ if(a[v]==0) Add (Maxx,1); - ElseAdd (Ma[k/a[v]],1); - Dfs (v); the if(a[v]==0) Add (maxx,-1); - ElseAdd (ma[k/a[v]],-1);Wuyi } the } - intMain () { Wu intT; -scanf"%d",&T); About while(t--){ $ ma.clear (); -Memset (c,0,sizeof(c)); -scanf"%i64d%i64d",&n,&k); - for(intI=1; i<=n;i++){ Ascanf"%i64d",&a[i]); +b[i*2-2]=A[i]; the if(a[i]!=0) b[i*2-1]=k/A[i]; - g[i].clear (); $ inch[i]=0; the } theSort (b,b+2*n); the intK=unique (b,b+2*n)-b; the intcxt=0; - for(intI=0; i<k;i++){ inma[b[i]]=++cxt; the } the for(LL i=0; i<n-1; i++){ About LL u,v; thescanf"%i64d%i64d",&u,&v); the G[u].push_back (v); the inch[v]++; + } - LL RT; the for(LL i=1; i<=n;i++){Bayi if(inch[i]==0){ thert=i; the Break; - } - } theans=0; the if(a[rt]==0) Add (Maxx,1); the ElseAdd (Ma[k/a[rt]],1); the DFS (RT); -printf"%i64d\n", ans); the } the return 0; the}
Tree-shaped dp+ tree array HDU 5877 Weak Pair