Housewife Wind
Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 4682 | Accepted: 1186 |
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
- #include<cstdio>
- #include<algorithm>
- #include<cmath>
- using namespace std;
- struct node
- {
- int x,y,t;
- }origin[100010],tosort[100010];
- int fa[100010],s[100010],e[100010],dis[100010],l[100010],r[100010],in[100010];
- int n,total;
- int early[100010];
- int num;
- int a[200020];
- int m[200020][30];
- int village[100010];
- int ee[30];
- void process(int N)
- {
- ee[0] = 1;
- int i, j;
- for (i = 1 ; i <= N ; i++) ee[i] = ee[i-1] * 2;
- for (i = 0; i < N; i++) m[i][0] = i;
- for (j = 1; 1 << j <= N; j++)
- for (i = 0; i + (1 << j) - 1 < N; i++)
- if (a[m[i][j - 1]] < a[m[i + (1 << (j - 1))][j - 1]])
- m[i][j] = m[i][j - 1];
- else
- m[i][j] = m[i + (1 << (j - 1))][j - 1];
- }
- int RMQ(int i,int j)
- {
- float kk = log((float)(j - i + 1))/log(2.0);
- int k = (int) kk;
- if (a[m[i][k]] <= a[m[j-ee[k]+1][k]]) return m[i][k];
- else return m[j-ee[k]+1][k];
- }
- bool cmp(struct node a,struct node b)
- {
- return a.x < b.x;
- }
- void dfs(int k)
- {
- total++;
- village[total] = k;
- l[k] = total;
- a[num] = l[k];
- early[k] = num;
- num++;
- for (int i = s[k] ; i <= e[k] ; i++)
- {
- int next = tosort[i].y;
- if (fa[next] == -1)
- {
- fa[next] = k;
- dis[next] = dis[k] + tosort[i].t;
- dfs(next);
- a[num] = l[k];
- num++;
- }
- }
- r[k] = total;
- }
- int lowbit(int t)
- {
- return (t & (-t));
- }
- int sum(int t)
- {
- int ans = 0;
- while (t > 0)
- {
- ans += in[t];
- t -= lowbit(t);
- }
- return ans;
- }
- void update(int t,int v)
- {
- while (t <= n)
- {
- in[t] += v;
- t += lowbit(t);
- }
- }
- int dist(int st,int ed)
- {
- int mid;
- if (l[st] < l[ed]) mid = village[a[RMQ(early[l[st]],early[l[ed]])]];
- else mid = village[a[RMQ(early[l[ed]],early[l[st]])]];
- int dis1 = dis[st] + sum(l[st]);
- int dis2 = dis[mid] + sum(l[mid]);
- int dis3 = dis[ed] + sum(l[ed]);
- return dis1 - dis2 + dis3 - dis2;
- }
- int main()
- {
- int q,st;
- scanf("%d%d%d",&n,&q,&st);
- for (int i = 1 ; i < n ; i++)
- {
- scanf("%d%d%d",&origin[i].x,&origin[i].y,&origin[i].t);
- tosort[i-1].x = origin[i].x;
- tosort[i-1].y = origin[i].y;
- tosort[i-1].t = origin[i].t;
- }
- sort(tosort,tosort+n-1,cmp);
- for (int i = 1 ; i <= n ; i++)
- {
- fa[i] = -1;
- s[i] = n;
- e[i] = -1;
- }
- for (int i = 0 ; i < n-1 ; i++)
- {
- if (i < s[tosort[i].x]) s[tosort[i].x] = i;
- if (i > e[tosort[i].x]) e[tosort[i].x] = i;
- }
- fa[1] = 1;
- dis[1] = 0;
- total = 0;
- num = 0;
- for (int i = 1 ; i <= n ; i++)
- {
- in[i] = 0;
- early[i] = 3*n;
- }
- dfs(1);
- process(2*n-1);
- while (q--)
- {
- int op;
- scanf("%d",&op);
- if (op == 0)
- {
- int ed;
- scanf("%d",&ed);
- printf("%d\n",dist(st,ed));
- st = ed;
- }
- if (op == 1)
- {
- int num2,newt;
- scanf("%d%d",&num2,&newt);
- int ox = origin[num2].x;
- int oy = origin[num2].y;
- int dif = newt - origin[num2].t;
- origin[num2].t = newt;
- if (fa[oy] == ox)
- {
- update(l[oy],dif);
- update(r[oy]+1,-dif);
- }
- else
- {
- update(l[ox],dif);
- update(r[ox]+1,-dif);
- }
- }
- }
- return 0;
- }