#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC target ("avx2")
#define pb push_back
#define rc (i<<1)|1
#define lc (i<<1)
#define el "\n"
#define f first
#define s second
typedef long long ll;
const int MM=1e5+5, MOD=1e9+7, LOG=19;
int N, Q, sz[MM], bk[MM], pa[MM], in[LOG][MM], ot[LOG][MM], cp[LOG][MM], tp[LOG][MM], ct;
vector<int> adj[MM]; ll av[MM], W;
gp_hash_table<int,int> mp[MM];
multiset<ll> ast;
struct node {
ll lz;
pair<ll,int> mx;
};
struct tree {
vector<node> seg;
int wt;
inline void prop(int i, int l, int r) {
if(l!=r) {
seg[lc].lz+=seg[i].lz;
seg[rc].lz+=seg[i].lz; }
seg[i].mx.f+=seg[i].lz;
seg[i].lz=0;
}
void build(int i, int sl, int sr) {
if(sl==sr) {
seg[i].mx=make_pair(0,sl);
return; }
int m=sl+sr>>1;
build(rc, m+1, sr);
build(lc, sl, m);
calc(i);
}
inline void calc(int i) {
seg[i].mx=max(seg[lc].mx, seg[rc].mx);
}
void upd(int i, int sl, int sr, int l, int r, ll v) {
prop(i, sl, sr);
if(l>sr || r<sl) return;
if(l<=sl&&sr<=r) {
seg[i].lz+=v;
prop(i, sl, sr); return; }
int m=sl+sr>>1;
upd(rc, m+1, sr, l, r, v);
upd(lc, sl, m, l, r, v);
calc(i);
}
pair<ll,int> rmq(int i, int sl, int sr, int l, int r) {
prop(i, sl, sr);
if(l>sr || r<sl) return {0,0};
if(l<=sl&&sr<=r) return seg[i].mx;
int m=sl+sr>>1;
return max(rmq(lc, sl, m, l, r), rmq(rc, m+1, sr, l, r));
}
void init(int n) {
wt=n;
seg.resize(4*n+4);
build(1, 1, n);
}
} st[MM];
int crt(int u, int th, int fa) {
for(int &v:adj[u]) if(v!=fa&&!bk[v]&&sz[v]>th) {
return crt(v, th, u); }
return u;
}
void dfs(int u, int fa) {
sz[u]=1;
for(int &v:adj[u]) if(v!=fa&&!bk[v]) {
dfs(v, u);
sz[u]+=sz[v];
}
}
void etr(int u, int fa, int lvl, int rt, int tv) {
in[lvl][u]=++ct; cp[lvl][u]=rt; tp[lvl][u]=tv; mp[rt][ct]=u;
for(int &v:adj[u]) if(v!=fa&&!bk[v]) {
etr(v, u, lvl, rt, tv); }
ot[lvl][u]=ct;
}
void rec(int rt, int nds, int fa=-1, int lvl=0) {
if(nds<=1) return;
dfs(rt, 0);
// cout<<"centroid for "<<rt<<" nds "<<nds<<" lvl "<<lvl<<" is ";
rt=crt(rt, nds/2, 0); pa[rt]=fa;
// cout<<rt<<el;
bk[rt]=true; ct=0;
// get ett
cp[lvl][rt]=tp[lvl][rt]=rt;
for(int &v:adj[rt]) if(!bk[v]) etr(v, rt, lvl, rt, v);
if(ct==0) return;
// construct tree
st[rt].init(nds);
int tally=0, lv=-1;
for(int &v:adj[rt]) if(!bk[v]) {
if(sz[v]<sz[rt]) {
tally+=sz[v];
rec(v, sz[v], rt, lvl+1);
} else lv=v;
}
if(~lv) rec(lv, nds-tally-1, rt, lvl+1);
}
void qry(int u, int v, ll val) {
// find smallest component
int cur=0;
while(cur<LOG-1&&cp[cur+1][u]==cp[cur+1][v]&&cp[cur+1][u]!=0) ++cur;
// cout<<"iterating on "<<u<<" starting with "<<cur<<" delta "<<val<<el;
// jump
int crt=cp[cur][u];
while(cur>=0) {
// update edge
// cout<<"crt "<<crt<<el;
int w=(in[cur][u]>in[cur][v]?u:v);
// cout<<"level "<<cur<<" w "<<w<<" cent "<<crt<<" updating "<<in[cur][w]<<" "<<ot[cur][w]<<el;
st[crt].upd(1, 1, st[crt].wt, in[cur][w], ot[cur][w], val);
// query max
auto x=st[crt].rmq(1, 1, st[crt].wt, 1, st[crt].wt);
x.s=mp[crt][x.s];
// cout<<"x value "<<x.f<<" "<<x.s<<" "<<tp[cur][x.s]<<" "<<in[cur][tp[cur][x.s]]<<" "<<ot[cur][tp[cur][x.s]]<<el;
auto y=max(st[crt].rmq(1, 1, st[crt].wt, 1, in[cur][tp[cur][x.s]]-1), st[crt].rmq(1, 1, st[crt].wt, ot[cur][tp[cur][x.s]]+1, st[crt].wt));
//cout<<"y value "<<y.f<<" "<<x.s<<el;
ll cnd=x.f+y.f;
if(cnd!=av[crt]) {
ast.erase(ast.find(av[crt]));
ast.insert(av[crt]=cnd);
}
crt=pa[crt]; cur--;
}
}
int32_t main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin>>N>>Q>>W;
vector<pair<ll,pair<int,int>>> edg;
for(int i=1; i<N; ++i) {
ll u, v, w; cin>>u>>v>>w;
adj[u].pb(v); adj[v].pb(u);
edg.pb({w,{u,v}});
}
// preprocess
rec(1, N);
for(int i=1; i<=N; ++i) ast.insert(0);
for(auto &e:edg) qry(e.s.f, e.s.s, e.f);
ll last=0, e, w;
for(int q=1; q<=Q; ++q) {
cin>>e>>w;
e=(e+last)%(N-1);
w=(w+last)%W;
qry(edg[e].s.f, edg[e].s.s, w-edg[e].f);
last=*ast.rbegin(); edg[e].f=w;
cout<<last<<el;
}
}
Compilation message
diameter.cpp: In member function 'void tree::build(int, int, int)':
diameter.cpp:43:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
43 | int m=sl+sr>>1;
| ~~^~~
diameter.cpp: In member function 'void tree::upd(int, int, int, int, int, ll)':
diameter.cpp:59:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
59 | int m=sl+sr>>1;
| ~~^~~
diameter.cpp: In member function 'std::pair<long long int, int> tree::rmq(int, int, int, int, int)':
diameter.cpp:69:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
69 | int m=sl+sr>>1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
26956 KB |
Output is correct |
2 |
Correct |
20 ms |
27028 KB |
Output is correct |
3 |
Correct |
20 ms |
26956 KB |
Output is correct |
4 |
Correct |
21 ms |
27032 KB |
Output is correct |
5 |
Correct |
20 ms |
26956 KB |
Output is correct |
6 |
Correct |
22 ms |
26956 KB |
Output is correct |
7 |
Correct |
22 ms |
27028 KB |
Output is correct |
8 |
Correct |
22 ms |
27048 KB |
Output is correct |
9 |
Correct |
23 ms |
27084 KB |
Output is correct |
10 |
Correct |
21 ms |
27016 KB |
Output is correct |
11 |
Correct |
22 ms |
27004 KB |
Output is correct |
12 |
Correct |
21 ms |
27056 KB |
Output is correct |
13 |
Correct |
23 ms |
27084 KB |
Output is correct |
14 |
Correct |
24 ms |
27072 KB |
Output is correct |
15 |
Correct |
22 ms |
27128 KB |
Output is correct |
16 |
Correct |
25 ms |
27084 KB |
Output is correct |
17 |
Correct |
25 ms |
27168 KB |
Output is correct |
18 |
Correct |
23 ms |
27112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
26956 KB |
Output is correct |
2 |
Correct |
20 ms |
27028 KB |
Output is correct |
3 |
Correct |
20 ms |
26956 KB |
Output is correct |
4 |
Correct |
21 ms |
27032 KB |
Output is correct |
5 |
Correct |
20 ms |
26956 KB |
Output is correct |
6 |
Correct |
22 ms |
26956 KB |
Output is correct |
7 |
Correct |
22 ms |
27028 KB |
Output is correct |
8 |
Correct |
22 ms |
27048 KB |
Output is correct |
9 |
Correct |
23 ms |
27084 KB |
Output is correct |
10 |
Correct |
21 ms |
27016 KB |
Output is correct |
11 |
Correct |
22 ms |
27004 KB |
Output is correct |
12 |
Correct |
21 ms |
27056 KB |
Output is correct |
13 |
Correct |
23 ms |
27084 KB |
Output is correct |
14 |
Correct |
24 ms |
27072 KB |
Output is correct |
15 |
Correct |
22 ms |
27128 KB |
Output is correct |
16 |
Correct |
25 ms |
27084 KB |
Output is correct |
17 |
Correct |
25 ms |
27168 KB |
Output is correct |
18 |
Correct |
23 ms |
27112 KB |
Output is correct |
19 |
Correct |
39 ms |
28032 KB |
Output is correct |
20 |
Correct |
43 ms |
28152 KB |
Output is correct |
21 |
Correct |
49 ms |
28284 KB |
Output is correct |
22 |
Correct |
52 ms |
28392 KB |
Output is correct |
23 |
Correct |
69 ms |
32468 KB |
Output is correct |
24 |
Correct |
90 ms |
34028 KB |
Output is correct |
25 |
Correct |
94 ms |
34964 KB |
Output is correct |
26 |
Correct |
111 ms |
36276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
26984 KB |
Output is correct |
2 |
Correct |
21 ms |
26988 KB |
Output is correct |
3 |
Correct |
22 ms |
26920 KB |
Output is correct |
4 |
Correct |
30 ms |
26956 KB |
Output is correct |
5 |
Correct |
63 ms |
27320 KB |
Output is correct |
6 |
Correct |
20 ms |
26940 KB |
Output is correct |
7 |
Correct |
26 ms |
27108 KB |
Output is correct |
8 |
Correct |
24 ms |
27084 KB |
Output is correct |
9 |
Correct |
28 ms |
27100 KB |
Output is correct |
10 |
Correct |
49 ms |
27084 KB |
Output is correct |
11 |
Correct |
118 ms |
27520 KB |
Output is correct |
12 |
Correct |
27 ms |
28220 KB |
Output is correct |
13 |
Correct |
28 ms |
28176 KB |
Output is correct |
14 |
Correct |
28 ms |
28220 KB |
Output is correct |
15 |
Correct |
54 ms |
28256 KB |
Output is correct |
16 |
Correct |
124 ms |
28724 KB |
Output is correct |
17 |
Correct |
145 ms |
51224 KB |
Output is correct |
18 |
Correct |
150 ms |
51116 KB |
Output is correct |
19 |
Correct |
153 ms |
51164 KB |
Output is correct |
20 |
Correct |
190 ms |
51372 KB |
Output is correct |
21 |
Correct |
277 ms |
51856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
28236 KB |
Output is correct |
2 |
Correct |
54 ms |
28236 KB |
Output is correct |
3 |
Correct |
171 ms |
28500 KB |
Output is correct |
4 |
Correct |
293 ms |
28760 KB |
Output is correct |
5 |
Correct |
103 ms |
43092 KB |
Output is correct |
6 |
Correct |
144 ms |
43200 KB |
Output is correct |
7 |
Correct |
357 ms |
43456 KB |
Output is correct |
8 |
Correct |
645 ms |
43712 KB |
Output is correct |
9 |
Correct |
472 ms |
122640 KB |
Output is correct |
10 |
Correct |
561 ms |
122632 KB |
Output is correct |
11 |
Correct |
947 ms |
122960 KB |
Output is correct |
12 |
Correct |
1370 ms |
123408 KB |
Output is correct |
13 |
Correct |
1016 ms |
231180 KB |
Output is correct |
14 |
Correct |
1113 ms |
231204 KB |
Output is correct |
15 |
Correct |
1615 ms |
231476 KB |
Output is correct |
16 |
Correct |
2287 ms |
231784 KB |
Output is correct |
17 |
Correct |
3082 ms |
231872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3754 ms |
194684 KB |
Output is correct |
2 |
Correct |
3807 ms |
200236 KB |
Output is correct |
3 |
Correct |
3751 ms |
197612 KB |
Output is correct |
4 |
Correct |
3853 ms |
201192 KB |
Output is correct |
5 |
Correct |
3706 ms |
190340 KB |
Output is correct |
6 |
Correct |
2618 ms |
140564 KB |
Output is correct |
7 |
Execution timed out |
5072 ms |
244064 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
26956 KB |
Output is correct |
2 |
Correct |
20 ms |
27028 KB |
Output is correct |
3 |
Correct |
20 ms |
26956 KB |
Output is correct |
4 |
Correct |
21 ms |
27032 KB |
Output is correct |
5 |
Correct |
20 ms |
26956 KB |
Output is correct |
6 |
Correct |
22 ms |
26956 KB |
Output is correct |
7 |
Correct |
22 ms |
27028 KB |
Output is correct |
8 |
Correct |
22 ms |
27048 KB |
Output is correct |
9 |
Correct |
23 ms |
27084 KB |
Output is correct |
10 |
Correct |
21 ms |
27016 KB |
Output is correct |
11 |
Correct |
22 ms |
27004 KB |
Output is correct |
12 |
Correct |
21 ms |
27056 KB |
Output is correct |
13 |
Correct |
23 ms |
27084 KB |
Output is correct |
14 |
Correct |
24 ms |
27072 KB |
Output is correct |
15 |
Correct |
22 ms |
27128 KB |
Output is correct |
16 |
Correct |
25 ms |
27084 KB |
Output is correct |
17 |
Correct |
25 ms |
27168 KB |
Output is correct |
18 |
Correct |
23 ms |
27112 KB |
Output is correct |
19 |
Correct |
39 ms |
28032 KB |
Output is correct |
20 |
Correct |
43 ms |
28152 KB |
Output is correct |
21 |
Correct |
49 ms |
28284 KB |
Output is correct |
22 |
Correct |
52 ms |
28392 KB |
Output is correct |
23 |
Correct |
69 ms |
32468 KB |
Output is correct |
24 |
Correct |
90 ms |
34028 KB |
Output is correct |
25 |
Correct |
94 ms |
34964 KB |
Output is correct |
26 |
Correct |
111 ms |
36276 KB |
Output is correct |
27 |
Correct |
21 ms |
26984 KB |
Output is correct |
28 |
Correct |
21 ms |
26988 KB |
Output is correct |
29 |
Correct |
22 ms |
26920 KB |
Output is correct |
30 |
Correct |
30 ms |
26956 KB |
Output is correct |
31 |
Correct |
63 ms |
27320 KB |
Output is correct |
32 |
Correct |
20 ms |
26940 KB |
Output is correct |
33 |
Correct |
26 ms |
27108 KB |
Output is correct |
34 |
Correct |
24 ms |
27084 KB |
Output is correct |
35 |
Correct |
28 ms |
27100 KB |
Output is correct |
36 |
Correct |
49 ms |
27084 KB |
Output is correct |
37 |
Correct |
118 ms |
27520 KB |
Output is correct |
38 |
Correct |
27 ms |
28220 KB |
Output is correct |
39 |
Correct |
28 ms |
28176 KB |
Output is correct |
40 |
Correct |
28 ms |
28220 KB |
Output is correct |
41 |
Correct |
54 ms |
28256 KB |
Output is correct |
42 |
Correct |
124 ms |
28724 KB |
Output is correct |
43 |
Correct |
145 ms |
51224 KB |
Output is correct |
44 |
Correct |
150 ms |
51116 KB |
Output is correct |
45 |
Correct |
153 ms |
51164 KB |
Output is correct |
46 |
Correct |
190 ms |
51372 KB |
Output is correct |
47 |
Correct |
277 ms |
51856 KB |
Output is correct |
48 |
Correct |
33 ms |
28236 KB |
Output is correct |
49 |
Correct |
54 ms |
28236 KB |
Output is correct |
50 |
Correct |
171 ms |
28500 KB |
Output is correct |
51 |
Correct |
293 ms |
28760 KB |
Output is correct |
52 |
Correct |
103 ms |
43092 KB |
Output is correct |
53 |
Correct |
144 ms |
43200 KB |
Output is correct |
54 |
Correct |
357 ms |
43456 KB |
Output is correct |
55 |
Correct |
645 ms |
43712 KB |
Output is correct |
56 |
Correct |
472 ms |
122640 KB |
Output is correct |
57 |
Correct |
561 ms |
122632 KB |
Output is correct |
58 |
Correct |
947 ms |
122960 KB |
Output is correct |
59 |
Correct |
1370 ms |
123408 KB |
Output is correct |
60 |
Correct |
1016 ms |
231180 KB |
Output is correct |
61 |
Correct |
1113 ms |
231204 KB |
Output is correct |
62 |
Correct |
1615 ms |
231476 KB |
Output is correct |
63 |
Correct |
2287 ms |
231784 KB |
Output is correct |
64 |
Correct |
3082 ms |
231872 KB |
Output is correct |
65 |
Correct |
3754 ms |
194684 KB |
Output is correct |
66 |
Correct |
3807 ms |
200236 KB |
Output is correct |
67 |
Correct |
3751 ms |
197612 KB |
Output is correct |
68 |
Correct |
3853 ms |
201192 KB |
Output is correct |
69 |
Correct |
3706 ms |
190340 KB |
Output is correct |
70 |
Correct |
2618 ms |
140564 KB |
Output is correct |
71 |
Execution timed out |
5072 ms |
244064 KB |
Time limit exceeded |
72 |
Halted |
0 ms |
0 KB |
- |