Weak Pair
Time limit:4000/2000 MS (java/others) Memory limit:262144/262144 K (java/others)
Total submission (s): 439 Accepted Submission (s): 155
Problem Descriptionyou is given arooted Tree ofNNodes, labeled from 1 toN. to theITH Node A non-negative valueai is assigned. AnoRdEred Pair of nodes(u,v) is saidWeak If
(1)uis an ancestor ofv(Note:in This problem a nodeu is not considered an ancestor of itself);
(2) auxav≤k.
Can you find The number of weak pairs in the tree?
Inputthere is multiple cases in the data set.
The first line of input contains an integerTDenoting number of test cases.
For each case, the first line contains the space-separated integers,Nandk, respectively.
The second line containsNspace-separated integers, denotinga1 ToaN .
Each of the subsequent lines contains, space-separated integers defining an edge connecting nodesuandv, where nodeuis the parent of nodev.
Constrains:
1≤N≤5
0≤ai≤9
0≤k≤ Outputfor each test case, print a single integer on a denoting the number of weak pairs in the tree. Sample Input12 2 Sample OUTPUT1
/*hdu 5877 Segment Tree problem: give you a root tree of n nodes, each node has a value of a[i]. Ask how many point pairs (u,v) satisfy: U is the ancestor of V and A[u]*a[v] <= ksolve: First find the root node of this tree. Since the requirement U is the ancestor of V, so the father of V is equal to all points of the root node. So you can put the value of the crossing point into the line tree when the tree is traversing, and when I go to the value of node I to find out the total number of values in the segment tree [1,k/a[i]]. Then, when you pass the back, you delete the point. The data is very large, so we'll do a bit of discretization. hhh-2016-09-11 09:22:59*/#include <algorithm> #include <iostream># Include <cstdlib> #include <stdio.h> #include <cstring> #include <vector> #include <math.h > #include <queue> #include <set> #define Lson i<<1#define Rson i<<1|1#include <map># Define ll long longusing namespace Std;const int maxn = 200100;int a[maxn];struct node{int l,r; int Val;} TREE[MAXN <<2];void push_up (int i) {tree[i].val= tree[lson].val + tree[rson].val;} void build (int i,int l,int r) {tree[i].l = L,TREE[I].R = R; Tree[i].val = 0; if (L = = r) {return; } int mid = (tree[i].l + tree[i].r) >> 1; Build (Lson,l,mid); Build (Rson,mid+1,r); PUSH_UP (i);} void update (int i,int k,iNT VA) {if (tree[i].l = = TREE[I].R && tree[i].l = = k) {tree[i].val + = VA; Return } int mid = (tree[i].l + tree[i].r) >> 1; if (k <= mid) update (LSON,K,VA); else update (RSON,K,VA); PUSH_UP (i);} int query (int i,int l,int R) {if (L > R) return 0; if (tree[i].l >= l && tree[i].r <= R) {return tree[i].val; } int tans = 0; int mid = (tree[i].l + tree[i].r) >> 1; if (l <= mid) Tans + = query (LSON,L,R); if (R > Mid) Tans + = query (RSON,L,R); return tans;} struct node{int next; int to;} Edge[maxn];ll k;int ta,tot,n;int head[maxn];int deep[maxn];int t[maxn];void addedge (int from,int to) {edge[tot].to=to; Edge[tot].next=head[from]; head[from]=tot++;} ll ans = 0;void dfs (int u,int fa) {int o=lower_bound (t,t+ta,k/a[u])-t; Ans+=query (1,0,o); int Tk=lower_bound (T,t+ta,a[u])-t; Update (1,tk,1); for (int i=head[u]; ~i; i=edge[i].next) {int v=edge[i].to; DFS (V,U); } update (1,TK,-1);} int main () {int T; scanf ("%d", &t); while (t--) {scanf ("%d%i64d", &n,&k); int cnt=0; for (int i=1; i<=n; i++) {scanf ("%i64d", &a[i]); T[cnt++]=a[i]; } for (int i=1; i<=n; i++) t[cnt++]=k/a[i]; Sort (t,t+cnt); Ta=unique (t,t+cnt)-t; tot=0; memset (head,-1,sizeof (head)); memset (deep,0,sizeof (deep)); for (int i=0; i<n-1; i++) {int u,v; scanf ("%d%d", &u,&v); Addedge (U,V); deep[v]++; } ans=0; Build (1,0,ta); for (int i=1; i<=n; i++) if (deep[i]==0) DFS (I,-1); printf ("%i64d\n", ans); } return 0;}
HDU 5877 segment Tree (ACM/ICPC Asia regional Dalian Online)