标签:des style blog class code tar
Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 5471 | Accepted: 1371 |
Description
Input
Output
Sample Input
3 3 1 1 2 1 2 3 2 0 2 1 2 3 0 3
Sample Output
1 3
Source
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 6 using namespace std; 7 8 const int MAX_N = 100005; 9 const int edge = MAX_N * 2; 10 int N,Q,S; 11 int first[MAX_N],Next[edge],w[edge],V[edge]; 12 int id[MAX_N],vs[MAX_N * 2],dep[MAX_N * 2]; 13 int d[2 * MAX_N][30],qid[2 * MAX_N][30]; 14 int E[edge],c[MAX_N * 2]; 15 int vis[MAX_N * 2]; 16 int n; 17 18 int lowbit(int x) { 19 return x & (-x); 20 } 21 22 int sum(int x) { 23 int ret = 0; 24 while(x > 0) { 25 ret += c[x]; 26 x -= lowbit(x); 27 } 28 return ret; 29 } 30 31 void add(int x,int d) { 32 //printf("x = %d\n",x); 33 while(x <= n) { 34 c[x] += d; 35 x += lowbit(x); 36 } 37 } 38 39 void add_edge(int id,int u) { 40 int e = first[u]; 41 Next[id] = e; 42 first[u] = id; 43 } 44 45 int no(int x) { 46 if(x > N - 1) return x - N + 1; 47 else return x + N - 1; 48 } 49 50 void dfs(int u,int fa,int d,int &k,int m) { 51 id[u] = k; 52 vs[k] = u; 53 add(k,w[m]); 54 vis[m] = 1; 55 E[m] = k; 56 dep[k++] = d; 57 for(int e = first[u]; e != -1; e = Next[e]) { 58 if(V[e] != fa) { 59 dfs(V[e],u,d + 1,k,e); 60 vs[k] = u; 61 add(k,-w[e]); 62 vis[no(e)] = -1; 63 E[no(e)] = k; 64 dep[k++] = d; 65 } 66 } 67 } 68 69 70 void RMQ() { 71 for(int i = 1; i <= n; ++i) { 72 d[i][0] = dep[i]; 73 qid[i][0] = i; 74 } 75 for(int j = 1; (1 << j) <= n; ++j) { 76 for(int i = 1; i + (1 << j) - 1 <= n; ++i) { 77 if(d[i][j - 1] > d[i + (1 << (j - 1))][j - 1]) { 78 d[i][j] = d[i + (1 << (j - 1))][j - 1]; 79 qid[i][j] = qid[i + (1 << (j - 1))][j - 1]; 80 } else { 81 d[i][j] = d[i][j - 1]; 82 qid[i][j] = qid[i][j - 1]; 83 } 84 } 85 } 86 } 87 88 int query(int L,int R) { 89 int k = 0; 90 //printf("L = %d R = %d\n",L,R); 91 while( (1 << (k + 1)) < (R - L + 1) ) ++k; 92 //printf("k = %d\n",k); 93 //printf("d= %d %d\n",d[L][1 << k] , d[R - (1 << k) + 1][1 << k]); 94 return d[L][k] < d[R - (1 << k) + 1][k] ? 95 qid[L][k] : qid[R - (1 << k) + 1][k]; 96 97 } 98 99 int main() 100 { 101 // freopen("sw.in","r",stdin); 102 scanf("%d%d%d",&N,&Q,&S); 103 n = 2 * N - 1; 104 for(int i = 1; i <= N; ++i) first[i] = -1; 105 for(int i = 1; i <= N - 1; ++i) { 106 int u; 107 scanf("%d%d%d",&u,&V[i],&w[i]); 108 V[i + N - 1] = u; 109 w[i + N - 1] = w[i]; 110 add_edge(i,u); 111 add_edge(i + N - 1,V[i]); 112 } 113 114 int k = 1; 115 dfs(S,-1,0,k,0); 116 RMQ(); 117 //printf("k = %d\n",k); 118 119 int now = S; 120 for(int i = 1; i <= Q; ++i) { 121 int ch,v,id1; 122 scanf("%d",&ch); 123 if(ch == 0) { 124 scanf("%d",&v); 125 // printf("now = %d\n",now); 126 int p = vs[ query(min(id[now],id[v]),max(id[now],id[v])) ]; 127 printf("%d\n",sum(id[now] ) + sum( id[v] ) - 2 * sum( id[p] )); 128 now = v; 129 } else { 130 scanf("%d%d",&id1,&v); 131 int d = v - w[id1]; 132 w[id1] = v; 133 add(E[id1],vis[id1] * d); 134 add(E[id1 + N - 1],vis[id1 + N - 1] * d); 135 } 136 } 137 138 139 return 0; 140 }
标签:des style blog class code tar
原文地址:http://www.cnblogs.com/hyxsolitude/p/3710642.html