#include<bits/stdc++.h>
using namespace std;
#define mp make_pair
#define INF 10000000
#define MOD 1000000007
#define MID ((l+r)/2)
#define HASHMOD 2305843009213693951
#define ll long long
#define ull unsigned long long
#define F first
#define S second
typedef pair<ll, ll> ii;
typedef pair<ii, ll> iii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef map<ll, ll> mii;
#define EPS 1e-6
#define FOR(i,n) for(int i=0;i<((int)(n));i++)
#define FORi(i,a,b) for(int i=((int)(a));i<((int)(b));i++)
#define FOA(v, a) for(auto v : a)
ll t, n;
vi a, b;
vector<vii> adj;
ii f;
ll cnt=0;
vector<int> dfso;
void dfs(int s, int p=-1, ll d=0){
dfso[s] = cnt++;
if(d>f.F) f = max(f, {d, s});
FOA(v, adj[s]){
if(v.F!=p) dfs(v.F, s, d+v.S);
}
}
vector<int> pr;
struct node{
ii l1={0,0}, l2={0,0};
int w=0;
int id=1;
ll ans=0;
vector<node*> c;
vector<node*> all_c;
void build(int p=-1){
pr.push_back(id);
FOA(v,adj[id]){
if(v.F!=p){
c.push_back(new node());
c[c.size()-1]->w = v.S;
c[c.size()-1]->id = v.F;
c[c.size()-1]->build(id);
}
}
FOA(v, c){
if(v->w + v->l1.F > l1.F) l2 = l1, l1 = {v->l1.F + v->w, v->id};
else if(v->w + v->l1.F > l2.F && v->id != l1.S) l2 = {v->l1.F + v->w, v->id};
ans = max(ans, v->ans);
//else if(v->w + v->l2 > l2) l2 = v->l2 + v->w;
}
ans = max(ans, l1.F + l2.F);
//cout<<id<<" ("<<l1.F<<" "<<l1.S<<") ("<<l2.F<<" "<<l2.S<<")"<<" "<<ans<<endl;
}
void update(int s, ll t){
int pos = c.size()-1;
FOR(i,c.size()-1){
if(dfso[c[i]->id] <= dfso[s] && dfso[s] < dfso[c[i+1]->id]) pos = i;
}
if(s==id){
//cout<<"CHAINGE "<<s<<endl;
w = t;
}
else{
c[pos]->update(s, t);
//node*v = c[pos];
l1 = {0,0}, l2 = {0,0};
ans = 0;
FOA(v, c){
if(v->w + v->l1.F > l1.F) l2 = l1, l1 = {v->l1.F + v->w, v->id};
else if(v->w + v->l1.F > l2.F && v->id != l1.S) l2 = {v->l1.F + v->w, v->id};
ans = max(ans, v->ans);
}
ans = max(ans, l1.F + l2.F);
//cout<<id<<" ("<<l1.F<<" "<<l1.S<<") ("<<l2.F<<" "<<l2.S<<")"<<" "<<ans<<endl;
}
}
};
int main(){
int q;
ll w;
cin>>n>>q>>w;
adj.assign(n+1, vii());
dfso.assign(n+1, 0);
vector<pair<ii,ii> > edg(n-1);
set<ii> ws;
vi wu(n);
FOR(i,n-1){
ll a, b, c;
cin>>a>>b>>c;
ws.insert({-c, i});
wu[i] = c;
edg[i] = {{a, adj[a].size()}, {b, adj[b].size()}};
adj[a].push_back({b,c});
adj[b].push_back({a,c});
}
node root;
root.build();
ll last=0;
dfs(1);
//FOR(i,n) cout<<dfso[i+1]<<" "; cout<<endl;
while(q--){
ll a, b;
cin>>a>>b;
//cout<<"UPDATE\n";
a= (a + last) % (n-1);
b = (b+last) % w;
adj[edg[a].F.F][edg[a].F.S].S = b;
adj[edg[a].S.F][edg[a].S.S].S = b;
f = {0,1};
//dfs(1);
//dfs(f.S);
//cout<<a<<" "<<b<<endl;
//cout<<"edg "<<edg[a].F.F<<" "<<edg[a].S.F<<endl;
int upn = max(ii(dfso[edg[a].F.F], edg[a].F.F),ii(dfso[edg[a].S.F], edg[a].S.F)).S;
//cout<<"pos "<<upn<<endl;
root.update(upn, b);
//last = f.F;
last = root.ans;
cout<<last<<endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
284 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
292 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
284 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
292 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
13 ms |
672 KB |
Output is correct |
20 |
Correct |
14 ms |
684 KB |
Output is correct |
21 |
Correct |
21 ms |
588 KB |
Output is correct |
22 |
Correct |
28 ms |
728 KB |
Output is correct |
23 |
Correct |
19 ms |
2008 KB |
Output is correct |
24 |
Correct |
28 ms |
2032 KB |
Output is correct |
25 |
Correct |
61 ms |
2072 KB |
Output is correct |
26 |
Correct |
177 ms |
2436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
288 KB |
Output is correct |
3 |
Correct |
4 ms |
204 KB |
Output is correct |
4 |
Correct |
35 ms |
488 KB |
Output is correct |
5 |
Correct |
173 ms |
1060 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Correct |
8 ms |
460 KB |
Output is correct |
10 |
Correct |
69 ms |
664 KB |
Output is correct |
11 |
Correct |
352 ms |
1472 KB |
Output is correct |
12 |
Correct |
6 ms |
1868 KB |
Output is correct |
13 |
Correct |
10 ms |
1868 KB |
Output is correct |
14 |
Correct |
47 ms |
1892 KB |
Output is correct |
15 |
Correct |
457 ms |
2220 KB |
Output is correct |
16 |
Correct |
2240 ms |
2812 KB |
Output is correct |
17 |
Correct |
158 ms |
30500 KB |
Output is correct |
18 |
Correct |
438 ms |
30496 KB |
Output is correct |
19 |
Correct |
3199 ms |
30500 KB |
Output is correct |
20 |
Execution timed out |
5064 ms |
30500 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
588 KB |
Output is correct |
2 |
Correct |
19 ms |
712 KB |
Output is correct |
3 |
Correct |
95 ms |
1436 KB |
Output is correct |
4 |
Correct |
181 ms |
1964 KB |
Output is correct |
5 |
Correct |
14 ms |
3532 KB |
Output is correct |
6 |
Correct |
35 ms |
3660 KB |
Output is correct |
7 |
Correct |
106 ms |
4248 KB |
Output is correct |
8 |
Correct |
202 ms |
5028 KB |
Output is correct |
9 |
Correct |
71 ms |
16664 KB |
Output is correct |
10 |
Correct |
92 ms |
16804 KB |
Output is correct |
11 |
Correct |
172 ms |
17532 KB |
Output is correct |
12 |
Correct |
296 ms |
18388 KB |
Output is correct |
13 |
Correct |
149 ms |
33080 KB |
Output is correct |
14 |
Correct |
173 ms |
33224 KB |
Output is correct |
15 |
Correct |
260 ms |
33984 KB |
Output is correct |
16 |
Correct |
375 ms |
34752 KB |
Output is correct |
17 |
Correct |
438 ms |
34676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3109 ms |
35244 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
284 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
292 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
13 ms |
672 KB |
Output is correct |
20 |
Correct |
14 ms |
684 KB |
Output is correct |
21 |
Correct |
21 ms |
588 KB |
Output is correct |
22 |
Correct |
28 ms |
728 KB |
Output is correct |
23 |
Correct |
19 ms |
2008 KB |
Output is correct |
24 |
Correct |
28 ms |
2032 KB |
Output is correct |
25 |
Correct |
61 ms |
2072 KB |
Output is correct |
26 |
Correct |
177 ms |
2436 KB |
Output is correct |
27 |
Correct |
0 ms |
204 KB |
Output is correct |
28 |
Correct |
1 ms |
288 KB |
Output is correct |
29 |
Correct |
4 ms |
204 KB |
Output is correct |
30 |
Correct |
35 ms |
488 KB |
Output is correct |
31 |
Correct |
173 ms |
1060 KB |
Output is correct |
32 |
Correct |
0 ms |
204 KB |
Output is correct |
33 |
Correct |
1 ms |
332 KB |
Output is correct |
34 |
Correct |
2 ms |
332 KB |
Output is correct |
35 |
Correct |
8 ms |
460 KB |
Output is correct |
36 |
Correct |
69 ms |
664 KB |
Output is correct |
37 |
Correct |
352 ms |
1472 KB |
Output is correct |
38 |
Correct |
6 ms |
1868 KB |
Output is correct |
39 |
Correct |
10 ms |
1868 KB |
Output is correct |
40 |
Correct |
47 ms |
1892 KB |
Output is correct |
41 |
Correct |
457 ms |
2220 KB |
Output is correct |
42 |
Correct |
2240 ms |
2812 KB |
Output is correct |
43 |
Correct |
158 ms |
30500 KB |
Output is correct |
44 |
Correct |
438 ms |
30496 KB |
Output is correct |
45 |
Correct |
3199 ms |
30500 KB |
Output is correct |
46 |
Execution timed out |
5064 ms |
30500 KB |
Time limit exceeded |
47 |
Halted |
0 ms |
0 KB |
- |