#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const int N=2e5+1;
int n, q, maxW;
vector<int> adj[N];
int edge[N][2];
int W[N];
int ti=-1;
int fst[N], lst[N];
vector<int> et;
ll path[N];
ll mx[4*N];
ll mn[4*N];
ll pref[4*N];
ll suf[4*N];
ll seg[4*N];
ll lazy[4*N];
void pull(int x){
mx[x]=max(mx[2*x+1], mx[2*x+2]);
mn[x]=min(mn[2*x+1], mn[2*x+2]);
pref[x]=max({pref[2*x+1], pref[2*x+2], mx[2*x+2]-2*mn[2*x+1]});
suf[x]=max({suf[2*x+1], suf[2*x+2], mx[2*x+1]-2*mn[2*x+2]});
// cout << "seg " << x << " = max " << seg[2*x+1] << ", " << seg[2*x+2] << ", " << pref[2*x+2] << "+" << mx[2*x+1] <<", " << mx[2*x+2] << "+" << suf[2*x+1] << "\n";
seg[x]=max({seg[2*x+1], seg[2*x+2], pref[2*x+2]+mx[2*x+1], mx[2*x+2]+suf[2*x+1]});
}
void push(int x, int lx, int rx){
if(rx-lx==1) return;
ll v=lazy[x];
for(int y:{2*x+1, 2*x+2}){
mx[y]+=v;
mn[y]+=v;
pref[y]-=v;
suf[y]-=v;
lazy[y]+=v;
}
lazy[x]=0;
}
void add(int l, int r, ll v, int x, int lx, int rx){
if(lx>=r || rx<=l) return;
push(x, lx, rx);
if(lx>=l && rx<=r){
lazy[x]=v;
mx[x]+=v;
mn[x]+=v;
pref[x]-=v;
suf[x]-=v;
// cout << x << " " << lx << " " << rx << " " << pref[x] << " " << suf[x] << "\n";
return;
}
int m=(lx+rx)/2;
add(l,r,v,2*x+1,lx,m);
add(l,r,v,2*x+2,m,rx);
pull(x);
// cout << x << " seg " << lx << " " << rx << " = " << seg[x] << "\n";
}
void add(int l, int r, ll v){ add(l,r,v,0,0,2*n-1); }
void dfs(int x, int par){
et.push_back(x);
for(int e:adj[x]){
int u=(edge[e][0]==x?edge[e][1]:edge[e][0]);
if(u==par) continue;
path[u]=path[x]+W[e];
dfs(u, x);
et.push_back(x);
}
}
int main(){
cin.tie(0)->sync_with_stdio(0);
cin >> n >> q >> maxW;
for(int i=0; i<n-1; i++){
int u,v; ll w; cin >> u >> v >> w;
edge[i][0]=u; edge[i][1]=v;
W[i]=w;
adj[u].push_back(i);
adj[v].push_back(i);
}
dfs(1, 0);
for(int i=1; i<=n; i++) fst[i]=1e9, lst[i]=-1;
for(int t=0; t<2*n-1; t++){
fst[et[t]]=min(fst[et[t]], t);
lst[et[t]]=max(lst[et[t]], t);
}
for(int i=0; i<n-1; i++){
if(path[edge[i][0]]<path[edge[i][1]]) swap(edge[i][0], edge[i][1]);
int v=edge[i][0];
// cout << "add " << fst[v] << " " << lst[v] << " " << path[v] << "\n";
add(fst[v], lst[v]+1, W[i]);
}
// for(int i=0; i<2*n-1; i++){
// add(i,i+1,i+1);
// }
// for(int i=0; i<2*n-1; i++){
// cout <<
// }
// cout << seg[0] << "\n";
ll ans=0;
while(q--){
ll i, w; cin >> i >> w;
i=(i+ans)%(n-1);
w=(w+ans)%maxW;
int u=edge[i][0];
add(fst[u], lst[u]+1, w-W[i]);
W[i]=w;
cout << (ans=seg[0]) << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5032 KB |
Output is correct |
2 |
Correct |
4 ms |
5036 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5032 KB |
Output is correct |
6 |
Correct |
3 ms |
5036 KB |
Output is correct |
7 |
Correct |
3 ms |
5032 KB |
Output is correct |
8 |
Correct |
3 ms |
5076 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
3 ms |
5040 KB |
Output is correct |
16 |
Correct |
4 ms |
5064 KB |
Output is correct |
17 |
Correct |
3 ms |
5076 KB |
Output is correct |
18 |
Correct |
3 ms |
5076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5032 KB |
Output is correct |
2 |
Correct |
4 ms |
5036 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5032 KB |
Output is correct |
6 |
Correct |
3 ms |
5036 KB |
Output is correct |
7 |
Correct |
3 ms |
5032 KB |
Output is correct |
8 |
Correct |
3 ms |
5076 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
3 ms |
5040 KB |
Output is correct |
16 |
Correct |
4 ms |
5064 KB |
Output is correct |
17 |
Correct |
3 ms |
5076 KB |
Output is correct |
18 |
Correct |
3 ms |
5076 KB |
Output is correct |
19 |
Correct |
8 ms |
5460 KB |
Output is correct |
20 |
Correct |
7 ms |
5416 KB |
Output is correct |
21 |
Correct |
6 ms |
5332 KB |
Output is correct |
22 |
Correct |
10 ms |
5472 KB |
Output is correct |
23 |
Correct |
12 ms |
6996 KB |
Output is correct |
24 |
Correct |
11 ms |
6972 KB |
Output is correct |
25 |
Correct |
12 ms |
7096 KB |
Output is correct |
26 |
Correct |
15 ms |
7380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5036 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
4 ms |
5044 KB |
Output is correct |
4 |
Correct |
10 ms |
5288 KB |
Output is correct |
5 |
Correct |
43 ms |
6176 KB |
Output is correct |
6 |
Correct |
4 ms |
5076 KB |
Output is correct |
7 |
Correct |
4 ms |
5204 KB |
Output is correct |
8 |
Correct |
3 ms |
5204 KB |
Output is correct |
9 |
Correct |
4 ms |
5176 KB |
Output is correct |
10 |
Correct |
12 ms |
5460 KB |
Output is correct |
11 |
Correct |
48 ms |
6460 KB |
Output is correct |
12 |
Correct |
9 ms |
6868 KB |
Output is correct |
13 |
Correct |
7 ms |
6964 KB |
Output is correct |
14 |
Correct |
12 ms |
6968 KB |
Output is correct |
15 |
Correct |
18 ms |
7248 KB |
Output is correct |
16 |
Correct |
72 ms |
8360 KB |
Output is correct |
17 |
Correct |
79 ms |
36000 KB |
Output is correct |
18 |
Correct |
81 ms |
35972 KB |
Output is correct |
19 |
Correct |
80 ms |
35964 KB |
Output is correct |
20 |
Correct |
105 ms |
36336 KB |
Output is correct |
21 |
Correct |
198 ms |
37572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5332 KB |
Output is correct |
2 |
Correct |
11 ms |
5460 KB |
Output is correct |
3 |
Correct |
37 ms |
6008 KB |
Output is correct |
4 |
Correct |
76 ms |
6728 KB |
Output is correct |
5 |
Correct |
13 ms |
8788 KB |
Output is correct |
6 |
Correct |
21 ms |
8888 KB |
Output is correct |
7 |
Correct |
54 ms |
9516 KB |
Output is correct |
8 |
Correct |
91 ms |
10292 KB |
Output is correct |
9 |
Correct |
49 ms |
20552 KB |
Output is correct |
10 |
Correct |
59 ms |
20692 KB |
Output is correct |
11 |
Correct |
160 ms |
21336 KB |
Output is correct |
12 |
Correct |
223 ms |
22152 KB |
Output is correct |
13 |
Correct |
99 ms |
36024 KB |
Output is correct |
14 |
Correct |
117 ms |
36124 KB |
Output is correct |
15 |
Correct |
184 ms |
36828 KB |
Output is correct |
16 |
Correct |
226 ms |
37656 KB |
Output is correct |
17 |
Correct |
219 ms |
37448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
19 ms |
16068 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5032 KB |
Output is correct |
2 |
Correct |
4 ms |
5036 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5032 KB |
Output is correct |
6 |
Correct |
3 ms |
5036 KB |
Output is correct |
7 |
Correct |
3 ms |
5032 KB |
Output is correct |
8 |
Correct |
3 ms |
5076 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
3 ms |
5040 KB |
Output is correct |
16 |
Correct |
4 ms |
5064 KB |
Output is correct |
17 |
Correct |
3 ms |
5076 KB |
Output is correct |
18 |
Correct |
3 ms |
5076 KB |
Output is correct |
19 |
Correct |
8 ms |
5460 KB |
Output is correct |
20 |
Correct |
7 ms |
5416 KB |
Output is correct |
21 |
Correct |
6 ms |
5332 KB |
Output is correct |
22 |
Correct |
10 ms |
5472 KB |
Output is correct |
23 |
Correct |
12 ms |
6996 KB |
Output is correct |
24 |
Correct |
11 ms |
6972 KB |
Output is correct |
25 |
Correct |
12 ms |
7096 KB |
Output is correct |
26 |
Correct |
15 ms |
7380 KB |
Output is correct |
27 |
Correct |
3 ms |
5036 KB |
Output is correct |
28 |
Correct |
3 ms |
5076 KB |
Output is correct |
29 |
Correct |
4 ms |
5044 KB |
Output is correct |
30 |
Correct |
10 ms |
5288 KB |
Output is correct |
31 |
Correct |
43 ms |
6176 KB |
Output is correct |
32 |
Correct |
4 ms |
5076 KB |
Output is correct |
33 |
Correct |
4 ms |
5204 KB |
Output is correct |
34 |
Correct |
3 ms |
5204 KB |
Output is correct |
35 |
Correct |
4 ms |
5176 KB |
Output is correct |
36 |
Correct |
12 ms |
5460 KB |
Output is correct |
37 |
Correct |
48 ms |
6460 KB |
Output is correct |
38 |
Correct |
9 ms |
6868 KB |
Output is correct |
39 |
Correct |
7 ms |
6964 KB |
Output is correct |
40 |
Correct |
12 ms |
6968 KB |
Output is correct |
41 |
Correct |
18 ms |
7248 KB |
Output is correct |
42 |
Correct |
72 ms |
8360 KB |
Output is correct |
43 |
Correct |
79 ms |
36000 KB |
Output is correct |
44 |
Correct |
81 ms |
35972 KB |
Output is correct |
45 |
Correct |
80 ms |
35964 KB |
Output is correct |
46 |
Correct |
105 ms |
36336 KB |
Output is correct |
47 |
Correct |
198 ms |
37572 KB |
Output is correct |
48 |
Correct |
4 ms |
5332 KB |
Output is correct |
49 |
Correct |
11 ms |
5460 KB |
Output is correct |
50 |
Correct |
37 ms |
6008 KB |
Output is correct |
51 |
Correct |
76 ms |
6728 KB |
Output is correct |
52 |
Correct |
13 ms |
8788 KB |
Output is correct |
53 |
Correct |
21 ms |
8888 KB |
Output is correct |
54 |
Correct |
54 ms |
9516 KB |
Output is correct |
55 |
Correct |
91 ms |
10292 KB |
Output is correct |
56 |
Correct |
49 ms |
20552 KB |
Output is correct |
57 |
Correct |
59 ms |
20692 KB |
Output is correct |
58 |
Correct |
160 ms |
21336 KB |
Output is correct |
59 |
Correct |
223 ms |
22152 KB |
Output is correct |
60 |
Correct |
99 ms |
36024 KB |
Output is correct |
61 |
Correct |
117 ms |
36124 KB |
Output is correct |
62 |
Correct |
184 ms |
36828 KB |
Output is correct |
63 |
Correct |
226 ms |
37656 KB |
Output is correct |
64 |
Correct |
219 ms |
37448 KB |
Output is correct |
65 |
Runtime error |
19 ms |
16068 KB |
Execution killed with signal 11 |
66 |
Halted |
0 ms |
0 KB |
- |