1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
| #include<bits/stdc++.h> using namespace std;
#define int long long
void read(int &x) { x=0;int f=1;char ch=getchar(); for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-f; for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';x*=f; }
void print(int x) { if(x<0) putchar('-'),x=-x; if(!x) return ;print(x/10),putchar(x%10+48); } void write(int x) {if(!x) putchar('0');else print(x);putchar('\n');}
#define lf double #define ll long long
#define pii pair<int,int > #define vec vector<int >
#define pb push_back #define mp make_pair #define fr first #define sc second
#define FOR(i,l,r) for(int i=l,i##_r=r;i<=i##_r;i++)
const int maxn = 1e6+10; const int inf = 1e9; const lf eps = 1e-8; const int mod = 1e9+7;
int head[maxn],tot,n,q,val[maxn],id[maxn],rev[maxn],cnt,dep[maxn],w,in[maxn],out[maxn]; struct edge{int to,nxt,w,id;}e[maxn<<1];
void ins(int u,int v,int w,int i) {e[++tot]=(edge){v,head[u],w,i},head[u]=tot;}
void dfs(int x,int fa) { rev[in[x]=++cnt]=x; for(int v,i=head[x];i;i=e[i].nxt) { if((v=e[i].to)==fa) continue; val[v]=e[i].w,id[e[i].id]=v,dep[v]=dep[x]+val[v]; dfs(v,x);rev[++cnt]=x; } out[x]=cnt; }
#define ls p<<1 #define rs p<<1|1 #define mid ((l+r)>>1)
struct segment_tree { int mx[maxn],ab[maxn],bc[maxn],res[maxn],tag[maxn],mn[maxn];
void update(int p) { mx[p]=max(mx[ls],mx[rs]); mn[p]=min(mn[ls],mn[rs]); res[p]=max(res[ls],res[rs]); res[p]=max(res[p],max(ab[ls]+mx[rs],mx[ls]+bc[rs])); ab[p]=max(max(ab[ls],ab[rs]),mx[ls]-2*mn[rs]); bc[p]=max(max(bc[ls],bc[rs]),-2*mn[ls]+mx[rs]); }
void push(int p,int v) { tag[p]+=v,mx[p]+=v,mn[p]+=v,ab[p]-=v,bc[p]-=v; }
void pushdown(int p) { push(ls,tag[p]),push(rs,tag[p]),tag[p]=0; }
void modify(int p,int l,int r,int x,int y,int v) { if(x<=l&&r<=y) return push(p,v),void(); pushdown(p); if(x<=mid) modify(ls,l,mid,x,y,v); if(y>mid) modify(rs,mid+1,r,x,y,v); update(p); }
void build(int p,int l,int r) { if(l==r) { int a=dep[rev[l]]; mx[p]=mn[p]=a,ab[p]=bc[p]=-a; return ; }build(ls,l,mid),build(rs,mid+1,r),update(p); } }T;
signed main() { read(n),read(q),read(w); for(int i=1,x,y,z;i<n;i++) read(x),read(y),read(z),ins(x,y,z,i),ins(y,x,z,i); dfs(1,0); T.build(1,1,cnt); for(int la=0,i=1;i<=q;i++) { int a,b;read(a),read(b); a=(a+la)%(n-1)+1,b=(b+la)%w; a=id[a]; T.modify(1,1,cnt,in[a],out[a],b-val[a]),val[a]=b; write(la=T.res[1]); } return 0; }
|