#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define debug(x) cout << #x << "= " << x << ", "
#define ll long long
#define fast ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define SZ(x) (int) x.size()
#define wall cout << endl;
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define int long long
const int maxn = 1e5 + 10;
int n , w , q , st[maxn] , fn[maxn] , dis[maxn] , par[maxn];
vector <pair<int , int>> adj[maxn];
vector <int> trav;
void dfs(int v , int p = -1)
{
st[v] = SZ(trav);
trav.pb(dis[v]);
for(auto u : adj[v]) if(u.F != p)
{
par[u.F] = v;
dis[u.F] = dis[v] + u.S;
dfs(u.F , v);
trav.pb(dis[v]);
}
fn[v] = SZ(trav);
}
struct nod{
int mx , mxmn , mnmx , ans , mn , lzy;
nod()
{
lzy = 0;
}
} tree[(maxn << 2)];
nod Merge(nod a , nod b)
{
nod res;
res.mx = max(a.mx , b.mx);
res.mn = min(a.mn , b.mn);
res.mxmn = max({a.mxmn , b.mxmn , a.mx - (b.mn << 1)});
res.mnmx = max({a.mnmx , b.mnmx , b.mx - (a.mn << 1)});
res.ans = max({a.ans , b.ans , a.mxmn + b.mx , a.mx + b.mnmx});
return res;
}
void Build(int node = 1 , int nl = 0 , int nr = SZ(trav))
{
if(nr - nl == 1)
{
tree[node].mx = tree[node].mn = trav[nl];
tree[node].mxmn = tree[node].mnmx = -trav[nl];
tree[node].ans = 0;
return;
}
int mid = (nl + nr) >> 1 , lc = node << 1 , rc = lc | 1;
Build(lc , nl , mid); Build(rc , mid , nr);
tree[node] = Merge(tree[lc] , tree[rc]);
}
void add(int node , int val)
{
tree[node].lzy += val;
tree[node].mx += val;
tree[node].mn += val;
tree[node].mxmn -= val;
tree[node].mnmx -= val;
}
void Shift(int node)
{
int lc = node << 1 , rc = lc | 1;
add(lc , tree[node].lzy); add(rc , tree[node].lzy);
tree[node].lzy = 0;
}
void Add(int l , int r , int val , int node = 1 , int nl = 0 , int nr = SZ(trav))
{
if(r <= nl || nr <= l) return;
if(l <= nl && nr <= r)
{
add(node , val);
return;
}
if(tree[node].lzy != 0) Shift(node);
int mid = (nl + nr) >> 1 , lc = node << 1 , rc = lc | 1;
Add(l , r , val , lc , nl , mid); Add(l , r , val , rc , mid , nr);
tree[node] = Merge(tree[lc] , tree[rc]);
}
int32_t main()
{
fast;
cin >> n >> q >> w;
vector <pair<pair<int , int> , int>> edges;
for(int i = 0 ; i < n -1 ; i++)
{
int v , u , c; cin >> v >> u >> c;
adj[v].pb({u , c}); adj[u].pb({v , c});
edges.pb({{u , v} , c});
}
dfs(1);
Build();
int last = 0;
while(q--)
{
int id , val; cin >> id >> val;
id = (id + last) % (n - 1);
val = (val + last) % w;
auto u = edges[id];
int v = u.F.F;
if(par[v] != u.F.S) v = u.F.S;
int tmp = (val - u.S);
Add(st[v] , fn[v] , tmp);
edges[id].S = val;
last = tree[1].ans;
cout << last << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
21460 KB |
Output is correct |
2 |
Correct |
9 ms |
21472 KB |
Output is correct |
3 |
Correct |
9 ms |
21460 KB |
Output is correct |
4 |
Correct |
9 ms |
21476 KB |
Output is correct |
5 |
Correct |
9 ms |
21384 KB |
Output is correct |
6 |
Correct |
10 ms |
21480 KB |
Output is correct |
7 |
Correct |
9 ms |
21472 KB |
Output is correct |
8 |
Correct |
10 ms |
21480 KB |
Output is correct |
9 |
Correct |
9 ms |
21428 KB |
Output is correct |
10 |
Correct |
9 ms |
21460 KB |
Output is correct |
11 |
Correct |
9 ms |
21476 KB |
Output is correct |
12 |
Correct |
9 ms |
21472 KB |
Output is correct |
13 |
Correct |
9 ms |
21460 KB |
Output is correct |
14 |
Correct |
11 ms |
21472 KB |
Output is correct |
15 |
Correct |
10 ms |
21448 KB |
Output is correct |
16 |
Correct |
10 ms |
21460 KB |
Output is correct |
17 |
Correct |
10 ms |
21416 KB |
Output is correct |
18 |
Correct |
10 ms |
21476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
21460 KB |
Output is correct |
2 |
Correct |
9 ms |
21472 KB |
Output is correct |
3 |
Correct |
9 ms |
21460 KB |
Output is correct |
4 |
Correct |
9 ms |
21476 KB |
Output is correct |
5 |
Correct |
9 ms |
21384 KB |
Output is correct |
6 |
Correct |
10 ms |
21480 KB |
Output is correct |
7 |
Correct |
9 ms |
21472 KB |
Output is correct |
8 |
Correct |
10 ms |
21480 KB |
Output is correct |
9 |
Correct |
9 ms |
21428 KB |
Output is correct |
10 |
Correct |
9 ms |
21460 KB |
Output is correct |
11 |
Correct |
9 ms |
21476 KB |
Output is correct |
12 |
Correct |
9 ms |
21472 KB |
Output is correct |
13 |
Correct |
9 ms |
21460 KB |
Output is correct |
14 |
Correct |
11 ms |
21472 KB |
Output is correct |
15 |
Correct |
10 ms |
21448 KB |
Output is correct |
16 |
Correct |
10 ms |
21460 KB |
Output is correct |
17 |
Correct |
10 ms |
21416 KB |
Output is correct |
18 |
Correct |
10 ms |
21476 KB |
Output is correct |
19 |
Correct |
15 ms |
21604 KB |
Output is correct |
20 |
Correct |
14 ms |
21636 KB |
Output is correct |
21 |
Correct |
13 ms |
21612 KB |
Output is correct |
22 |
Correct |
14 ms |
21716 KB |
Output is correct |
23 |
Correct |
15 ms |
22356 KB |
Output is correct |
24 |
Correct |
16 ms |
22356 KB |
Output is correct |
25 |
Correct |
16 ms |
22364 KB |
Output is correct |
26 |
Correct |
16 ms |
22740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
21376 KB |
Output is correct |
2 |
Correct |
9 ms |
21432 KB |
Output is correct |
3 |
Correct |
10 ms |
21396 KB |
Output is correct |
4 |
Correct |
17 ms |
21536 KB |
Output is correct |
5 |
Correct |
42 ms |
21828 KB |
Output is correct |
6 |
Correct |
9 ms |
21420 KB |
Output is correct |
7 |
Correct |
10 ms |
21548 KB |
Output is correct |
8 |
Correct |
9 ms |
21460 KB |
Output is correct |
9 |
Correct |
10 ms |
21588 KB |
Output is correct |
10 |
Correct |
19 ms |
21744 KB |
Output is correct |
11 |
Correct |
65 ms |
22820 KB |
Output is correct |
12 |
Correct |
12 ms |
22228 KB |
Output is correct |
13 |
Correct |
11 ms |
22228 KB |
Output is correct |
14 |
Correct |
13 ms |
22284 KB |
Output is correct |
15 |
Correct |
22 ms |
22612 KB |
Output is correct |
16 |
Correct |
76 ms |
23784 KB |
Output is correct |
17 |
Runtime error |
74 ms |
72420 KB |
Execution killed with signal 11 |
18 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
21588 KB |
Output is correct |
2 |
Correct |
16 ms |
21744 KB |
Output is correct |
3 |
Correct |
47 ms |
22320 KB |
Output is correct |
4 |
Correct |
78 ms |
22932 KB |
Output is correct |
5 |
Correct |
15 ms |
23084 KB |
Output is correct |
6 |
Correct |
24 ms |
23384 KB |
Output is correct |
7 |
Correct |
57 ms |
23888 KB |
Output is correct |
8 |
Correct |
102 ms |
24760 KB |
Output is correct |
9 |
Correct |
35 ms |
29524 KB |
Output is correct |
10 |
Correct |
43 ms |
29696 KB |
Output is correct |
11 |
Correct |
90 ms |
30356 KB |
Output is correct |
12 |
Correct |
152 ms |
31176 KB |
Output is correct |
13 |
Runtime error |
87 ms |
74492 KB |
Execution killed with signal 11 |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
116 ms |
74272 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
21460 KB |
Output is correct |
2 |
Correct |
9 ms |
21472 KB |
Output is correct |
3 |
Correct |
9 ms |
21460 KB |
Output is correct |
4 |
Correct |
9 ms |
21476 KB |
Output is correct |
5 |
Correct |
9 ms |
21384 KB |
Output is correct |
6 |
Correct |
10 ms |
21480 KB |
Output is correct |
7 |
Correct |
9 ms |
21472 KB |
Output is correct |
8 |
Correct |
10 ms |
21480 KB |
Output is correct |
9 |
Correct |
9 ms |
21428 KB |
Output is correct |
10 |
Correct |
9 ms |
21460 KB |
Output is correct |
11 |
Correct |
9 ms |
21476 KB |
Output is correct |
12 |
Correct |
9 ms |
21472 KB |
Output is correct |
13 |
Correct |
9 ms |
21460 KB |
Output is correct |
14 |
Correct |
11 ms |
21472 KB |
Output is correct |
15 |
Correct |
10 ms |
21448 KB |
Output is correct |
16 |
Correct |
10 ms |
21460 KB |
Output is correct |
17 |
Correct |
10 ms |
21416 KB |
Output is correct |
18 |
Correct |
10 ms |
21476 KB |
Output is correct |
19 |
Correct |
15 ms |
21604 KB |
Output is correct |
20 |
Correct |
14 ms |
21636 KB |
Output is correct |
21 |
Correct |
13 ms |
21612 KB |
Output is correct |
22 |
Correct |
14 ms |
21716 KB |
Output is correct |
23 |
Correct |
15 ms |
22356 KB |
Output is correct |
24 |
Correct |
16 ms |
22356 KB |
Output is correct |
25 |
Correct |
16 ms |
22364 KB |
Output is correct |
26 |
Correct |
16 ms |
22740 KB |
Output is correct |
27 |
Correct |
9 ms |
21376 KB |
Output is correct |
28 |
Correct |
9 ms |
21432 KB |
Output is correct |
29 |
Correct |
10 ms |
21396 KB |
Output is correct |
30 |
Correct |
17 ms |
21536 KB |
Output is correct |
31 |
Correct |
42 ms |
21828 KB |
Output is correct |
32 |
Correct |
9 ms |
21420 KB |
Output is correct |
33 |
Correct |
10 ms |
21548 KB |
Output is correct |
34 |
Correct |
9 ms |
21460 KB |
Output is correct |
35 |
Correct |
10 ms |
21588 KB |
Output is correct |
36 |
Correct |
19 ms |
21744 KB |
Output is correct |
37 |
Correct |
65 ms |
22820 KB |
Output is correct |
38 |
Correct |
12 ms |
22228 KB |
Output is correct |
39 |
Correct |
11 ms |
22228 KB |
Output is correct |
40 |
Correct |
13 ms |
22284 KB |
Output is correct |
41 |
Correct |
22 ms |
22612 KB |
Output is correct |
42 |
Correct |
76 ms |
23784 KB |
Output is correct |
43 |
Runtime error |
74 ms |
72420 KB |
Execution killed with signal 11 |
44 |
Halted |
0 ms |
0 KB |
- |