标签:span cat tin find training 节点 data bsp exists
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2501 Accepted Submission(s): 763
主席树。中序遍历,按顺序将节点建树。
1 #include <algorithm> 2 #include <iostream> 3 #include <cstdlib> 4 #include <cstring> 5 #include <cstdio> 6 const int N = 100000 + 3 ; 7 using namespace std; 8 int t,n,m,q,w[N],li[N],tot,root[N],roots,cnt; 9 bool ru[N]; 10 struct id 11 { 12 int lson,rson; 13 } id_tree[N]; 14 struct seg 15 { 16 int l,r,sum[2];//0 -> left_sum ; 1 -> right_sum 17 }tree[N*30]; 18 19 void Init( ) 20 { 21 scanf("%d",&n); 22 for(int i = 1;i <= n; ++i) 23 { 24 scanf("%d",w+i); 25 li[i] = w[i]; 26 } 27 sort(li+1,li+1+n); tot = 1; 28 for(int i = 2; i <= n; ++i) if(li[i] != li[tot]) li[++tot] = li[i]; 29 scanf("%d",&m); int a,u,v; 30 memset(id_tree,0,sizeof(id_tree)); 31 memset(ru,0,sizeof(ru)); 32 for(int i = 1; i <= m; ++i) 33 { 34 scanf("%d%d%d",&a,&u,&v); 35 id_tree[a].lson = u , id_tree[a].rson = v; 36 ru[u] = true , ru[v] = true; 37 } 38 for( int i = 1; i <= n; ++i ) 39 { 40 if(ru[i] == false) 41 { 42 roots = i; 43 break; 44 } 45 } 46 } 47 48 int binary_search( int num ) 49 { 50 int l = 1 , r = tot,ret = tot+1; 51 while( l <= r ) 52 { 53 int mid = l + ((r-l)>>1); 54 if( li[mid] == num ) return mid; 55 if( li[mid] < num ) l = mid + 1 ; 56 else r = mid - 1 , ret = min(tot,mid); 57 } 58 return ret; 59 } 60 61 62 void updata(int l,int r,int &x,int y,int pos,int id) 63 { 64 tree[++cnt] = tree[y] ; x = cnt; ++tree[cnt].sum[id]; 65 if( l == r ) return; 66 int mid = l + ((r-l)>>1) ; 67 if( pos <= mid ) updata(l,mid,tree[x].l,tree[y].l,pos,id); 68 else updata(mid+1,r,tree[x].r,tree[y].r,pos,id); 69 } 70 71 void dfs(int u) 72 { 73 int l = id_tree[u].lson , r = id_tree[u].rson; 74 if(l == r && r == 0) return; 75 int add = binary_search( w[u] ); 76 updata(1,tot,root[l],root[u],add,0); 77 dfs( l ); 78 updata(1,tot,root[r],root[u],add,1); 79 dfs( r ); 80 } 81 82 83 int query(int l,int r,int num,int L,int R,int id) 84 { 85 if( L > R ) return 0; 86 if( l == L && r == R ) return tree[num].sum[id]; 87 int mid =l + ((r-l)>>1); 88 if( R <= mid ) return query(l,mid,tree[num].l,L,R,id); 89 else if( L > mid ) return query(mid+1,r,tree[num].r,L,R,id); 90 return query(l,mid,tree[num].l,L,mid,id) + query(mid+1,r,tree[num].r,mid+1,R,id); 91 } 92 93 94 void Solve( ) 95 { 96 memset(tree,0,sizeof(tree)); cnt = 0; 97 dfs( roots ) ; 98 scanf("%d",&q); 99 int v,X; 100 while(q--) 101 { 102 scanf("%d%d",&v,&X); 103 if( v == 1 ) 104 { 105 printf("0 0\n"); 106 continue; 107 } 108 int r = binary_search( X ) ; int l = r - 1; 109 if( li[r] == X ) 110 { 111 if(query(1,tot,root[v],r,r,0) + query(1,tot,root[v],r,r,1) > 0) 112 { 113 puts("0") ; 114 continue ; 115 } 116 } 117 int r_less = query(1,tot,root[v],1,l,1), r_more = query(1,tot,root[v],r,tot,1); 118 int l_less = query(1,tot,root[v],1,l,0), l_more = query(1,tot,root[v],r,tot,0); 119 int x = r_less, y = 3*(r_less + l_less) + r_more + l_more; 120 121 printf("%d %d\n",x,y); 122 } 123 } 124 125 int main() 126 { 127 scanf("%d",&t); 128 while(t--) 129 { 130 Init(); 131 Solve(); 132 } 133 fclose(stdin); 134 fclose(stdout); 135 return 0; 136 }
标签:span cat tin find training 节点 data bsp exists
原文地址:http://www.cnblogs.com/Ateisti/p/6367703.html