poj 2763

Housewife Wind










Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 5471   Accepted: 1371

Description

After their royal wedding, Jiajia and Wind hid
away in XX Village, to enjoy their ordinary happy life. People in XX Village
lived in beautiful huts. There are some pairs of huts connected by bidirectional
roads. We say that huts in the same pair directly connected. XX Village is so
special that we can reach any other huts starting from an arbitrary hut. If each
road cannot be walked along twice, then the route between every pair is
unique. 

Since Jiajia earned enough money, Wind became a housewife.
Their children loved to go to other kids, then make a simple call to Wind:
‘Mummy, take me home!‘ 

At different times, the time needed to walk
along a road may be different. For example, Wind takes 5 minutes on a road
normally, but may take 10 minutes if there is a lovely little dog to play with,
or take 3 minutes if there is some unknown strange smell surrounding the
road. 

Wind loves her children, so she would like to tell her
children the exact time she will spend on the roads. Can you help
her?

Input

The first line contains three integers n, q, s.
There are n huts in XX Village, q messages to process, and Wind is currently in
hut s. n < 100001 , q < 100001. 

The following n-1 lines each
contains three integers a, b and w. That means there is a road directly
connecting hut a and b, time required is w. 1<=w<= 10000. 

The
following q lines each is one of the following two types: 

Message
A: 0 u 
A kid in hut u calls Wind. She should go to hut u from her
current position. 
Message B: 1 i w 
The time required for i-th
road is changed to w. Note that the time change will not happen when Wind is on
her way. The changed can only happen when Wind is staying somewhere, waiting to
take the next kid.

Output

For each message A, print an integer X, the time
required to take the next child.

Sample Input

3 3 1
1 2 1
2 3 2
0 2
1 2 3
0 3

Sample Output

1
3

Source

POJ
Monthly--2006.02.26
,zgl & twb

lca 加树状数组

  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 }

时间: 2024-08-10 15:03:59

poj 2763的相关文章

POJ 2763 Housewife Wind (树链剖分)

题目地址:POJ 2763 还是树链剖分模板题...不多说.. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h> #include <map> #include <set> #include <stdio.h>

poj 2763 树链剖分(单点更新,区间求值)

http://poj.org/problem?id=2763 Description After their royal wedding, Jiajia and Wind hid away in XX Village, to enjoy their ordinary happy life. People in XX Village lived in beautiful huts. There are some pairs of huts connected by bidirectional ro

POJ 2763 Housewife Wind (树链剖分+线段树)

题目链接:POJ 2763 Housewife Wind 题意:抽象出来就是 一棵已知节点之间的边权,两个操作,1·修改边权,2·询问两个节点之间的边权和. AC代码: #include <string.h> #include <stdio.h> #include <algorithm> using namespace std; #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 const int

POJ 2763 Housewife Wind(DFS序+LCA+树状数组)

Housewife Wind Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 11419   Accepted: 3140 Description After their royal wedding, Jiajia and Wind hid away in XX Village, to enjoy their ordinary happy life. People in XX Village lived in beauti

POJ 2763 Housewife Wind LCA转RMQ+时间戳+线段树成段更新

题目来源:POJ 2763 Housewife Wind 题意:给你一棵树 2种操作0 x 求当前点到x的最短路 然后当前的位置为x; 1 i x 将第i条边的权值置为x 思路:树上两点u, v距离为d[u]+d[v]-2*d[LCA(u,v)] 现在d数组是变化的 对应每一条边的变化 他修改的是一个区间 用时间戳处理每个点管辖的区域 然后用线段树修改 线段树的叶子节点村的是根到每一个点的距离 求最近公共祖先没差别 只是堕落用线段树维护d数组 各种错误 4个小时 伤不起 #include <cs

poj 2763 Housewife Wind(树链剖分+单点查询+区间修改)

题目链接:http://poj.org/problem?id=2763 题意:给一个数,边之间有权值,然后两种操作,第一种:求任意两点的权值和,第二,修改树上两点的权值. 题解:简单的树链剖分. #include <iostream> #include <cstdio> #include <cstring> using namespace std; const int M = 1e5 + 10; struct Edge { int v , next; }edge[M &

POJ 2763 Housewife Wind(树链剖分+树状数组)

[题目链接] http://poj.org/problem?id=2763 [题目大意] 在一棵树上,给出一些边的边长,有修改边的边长的操作, 询问每次从当前点到目标点的最短距离 [题解] 树链剖分之后,相当于树状数组的单点更新和区间查询, 注意边权转点权之后链操作不覆盖deep最浅的点,这里容易出错 [代码] #include <cstdio> #include <cstring> #include <algorithm> using namespace std; c

POJ 2763 (树链剖分+边修改+边查询)

题目链接:http://poj.org/problem?id=2763 题目大意:某人初始在s点.有q次移动,每次移动沿着树上一条链,每经过一条边有一定花费,这个花费可以任意修改.问每次移动的花费. 解题思路: 树链剖分基础题.每次Q之后改变一下s. 线段树记录的是边权.方法是对于一条边(u,v),边权值加在dep比较大的那一端. 链查询(边)和 链查询(点)在轻链时略有不同. 注意本题使用vector邻接表存图是会TLE的,应该使用链式前向星.树链剖分中使用链式前向星是基本要求. #inclu

poj 2763 Housewife Wind : 树链剖分维护边 O(nlogn)建树 O((logn)&#178;)修改与查询

1 /** 2 problem: http://poj.org/problem?id=2763 3 **/ 4 #include<stdio.h> 5 #include<stdlib.h> 6 #include<string.h> 7 #include<vector> 8 using namespace std; 9 10 const int MAXN = 100005; 11 12 template <typename T> 13 class

POJ 2763 Housewife Wind(树链剖分)(线段树单点修改)

Housewife Wind Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 10378   Accepted: 2886 Description After their royal wedding, Jiajia and Wind hid away in XX Village, to enjoy their ordinary happy life. People in XX Village lived in beauti