This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define gc getchar_unlocked()
#define pc(x) putchar_unlocked(x)
template<typename T> void scan(T &x){x = 0;bool _=0;T c=gc;_=c==45;c=_?gc:c;while(c<48||c>57)c=gc;for(;c<48||c>57;c=gc);for(;c>47&&c<58;c=gc)x=(x<<3)+(x<<1)+(c&15);x=_?-x:x;}
template<typename T> void printn(T n){bool _=0;_=n<0;n=_?-n:n;char snum[65];int i=0;do{snum[i++]=n%10+48;n/= 10;}while(n);--i;if (_)pc(45);while(i>=0)pc(snum[i--]);}
template<typename First, typename ... Ints> void scan(First &arg, Ints&... rest){scan(arg);scan(rest...);}
template<typename T> void print(T n){printn(n);pc(10);}
template<typename First, typename ... Ints> void print(First arg, Ints... rest){printn(arg);pc(32);print(rest...);}
typedef long long ll;
typedef long long T;
constexpr int MM = 1e5+2, LOG = 17;
int n, q, tot, sz[MM], a[MM], b[MM], dep[LOG][MM], in[LOG][MM], out[LOG][MM], t[LOG], centid[LOG][MM], parid[LOG][MM];
ll maxw, c[MM];
std::vector<std::pair<int, ll>> adj[MM];
bool vis[MM];
std::multiset<ll, std::greater<ll>> best, ch[MM];
//all best, ch set for each node as centroid
struct node{
T val, lp;
inline void apply(T v){
val += v;
lp += v;
}
};
struct segtree{
#define lc (rt<<1)
#define rc (rt<<1|1)
#define nm ((nl+nr)>>1)
node tree[MM*3];
const T DEF = 0;
//default value
inline void push_up(int rt){
tree[rt].val = std::max(tree[lc].val, tree[rc].val);
}
// node with lazy val means yet to push to children (but updated itself)
inline void push_down(int rt, int nl, int nr){
T &val = tree[rt].lp;
if(nl != nr){
tree[lc].apply(val);
tree[rc].apply(val);
}
val = DEF;
}
void update(int l, int r, T val, int nl = 1, int nr = n, int rt = 1){
if(r < nl || l > nr || l > r)
return;
if(l <= nl && r >= nr){
tree[rt].apply(val);
return;
}
push_down(rt, nl, nr);
update(l, r, val, nl, nm, lc);
update(l, r, val, nm+1, nr, rc);
push_up(rt);
}
T query(int l, int r, int nl = 1, int nr = n, int rt = 1){
if(r < nl || l > nr || l > r)
return DEF;
if(l <= nl && r >= nr)
return tree[rt].val;
push_down(rt, nl, nr);
return std::max(query(l, r, nl, nm, lc), query(l, r, nm+1, nr, rc));
}
#undef lc
#undef rc
#undef nm
} ST[LOG];
int getsz(int cur, int pre){
sz[cur] = 1;
for(auto e: adj[cur]){
int u = e.first;
if(u != pre && !vis[u])
sz[cur] += getsz(u, cur);
}
return sz[cur];
}
int findcent(int cur, int pre){
for(auto e: adj[cur]){
int u = e.first;
if(!vis[u] && u != pre && sz[u]*2 > tot)
return findcent(u, cur);
}
return cur;
}
void dfs1(int cur, int pre, int lvl, int cent, ll w){
in[lvl][cur] = ++t[lvl];
dep[lvl][cur] = dep[lvl][pre]+1;
centid[lvl][cur] = cent;
parid[lvl][cur] = pre == cent ? cur : parid[lvl][pre];
for(auto u: adj[cur]){
if(u.first == pre || vis[u.first])
continue;
dfs1(u.first, cur, lvl, cent, u.second);
}
out[lvl][cur] = t[lvl];
ST[lvl].update(in[lvl][cur], out[lvl][cur], w);
}
void go(int root, int lvl){
getsz(root, -1);
tot = sz[root];
if(tot == 1)
return;
int cent = findcent(root, -1);
vis[cent] = 1;
dfs1(cent, 0, lvl, cent, 0);
for(auto u: adj[cent]){
if(vis[u.first])
continue;
int st = in[lvl][u.first], ed = out[lvl][u.first];
ll v = ST[lvl].query(st, ed);
ch[cent].insert(v);
}
while(ch[cent].size() < 2)
ch[cent].insert(0);
auto ptr = ch[cent].begin();
best.insert(*ptr + *++ptr);
for(auto u: adj[cent]){
if(!vis[u.first])
go(u.first, lvl+1);
}
}
int main(){
scan(n, q, maxw);
for(int i = 0; i < n-1; i++){
scan(a[i], b[i], c[i]);
adj[a[i]].emplace_back(b[i], c[i]);
adj[b[i]].emplace_back(a[i], c[i]);
}
go(1, 0);
ll d, e, last = 0;
while(q--){
scan(d, e);
d = (d+last)%(n-1);
e = (e+last)%maxw;
int aa = a[d], bb = b[d];
ll dif = e-c[d];
c[d] = e;
for(int i = 0; i < LOG; i++){
int cent = centid[i][aa];
if(cent and cent == centid[i][bb]){
if(dep[i][aa] < dep[i][bb])
std::swap(aa, bb);
//aa is deeper, update it
auto ptr = ch[cent].begin();
best.erase(best.lower_bound(*ptr + *++ptr));
int par = parid[i][aa];
ch[cent].erase(ch[cent].lower_bound(ST[i].query(in[i][par], out[i][par])));
ST[i].update(in[i][aa], out[i][aa], dif);
ch[cent].insert(ST[i].query(in[i][par], out[i][par]));
ptr = ch[cent].begin();
best.insert(*ptr + *++ptr);
}
}
print(last = *best.begin());
}
}
/*
* ett each centroid
* when update edge, loop through LOG levels
* if for any lvl, there is a point where centid[lvl][a] == centid[lvl][b] != 0, then update the one with higher depth
* then for its centid, remove old ans from multiset and insert new one
*
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |