题意:给出一个带权树,每次修改树上一条边的权值,询问树的直径
题解:https://www.cnblogs.com/TinyWong/p/11260601.html线段树维护全DFS序
#include <bits/stdc++.h>
#define FOR(i,s,t) for(int i=(s);i<=(t);i++)
#define ROF(i,s,t) for(int i=(s);i>=(t);i--)
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define fi first
#define se second
#define endl '\n'
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxn = 2e5+ 6;
const ll mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
int readInt(){
int x=0;
bool sign=false;
char c=getchar();
while(!isdigit(c)){
sign=c=='-';
c=getchar();
}
while(isdigit(c)){
x=x*10+c-'0';
c=getchar();
}
return sign?-x:x;
}
ll readLong(){
ll x=0;
bool sign=false;
char c=getchar();
while(!isdigit(c)){
sign=c=='-';
c=getchar();
}
while(isdigit(c)){
x=x*10+c-'0';
c=getchar();
}
return sign?-x:x;
}
string readString(){
string s;
char c=getchar();
while(isspace(c)){
c=getchar();
}
while(!isspace(c)){
s+=c;
c=getchar();
}
return s;
}
int n;
struct Seg{
ll tag, val, M, LM, MR, LMR;
}tree[maxn << 2];
int X[maxn], Y[maxn], idx[maxn];
vector<pair<int, ll> > edge[maxn];
pair<int, int> e[maxn];
int pre[maxn];
ll weight[maxn], dis[maxn];
int times;
void dfs(int x){
idx[X[x] = ++ times] = x;
for (auto p : edge[x]){
int y = p.fi;
ll z = p.se;
if (y == pre[x]) continue;
dis[y] = dis[x] + z;
pre[y] = x;
weight[y] = z;
dfs(y);
idx[++times] = x;
}
Y[x] = times;
}
void add(int k, ll d){
tree[k].tag += d;
tree[k].val += d;
tree[k].M -= 2 * d;
tree[k].LM -= d;
tree[k].MR -= d;
}
void pushdown(int k){
if (tree[k].tag){
add(k<<1, tree[k].tag);
add(k<<1|1,tree[k].tag);
tree[k].tag = 0;
}
}
void update(int k){
tree[k].val = max(tree[k << 1].val, tree[k << 1 | 1].val);
tree[k].M = max(tree[k << 1].M, tree[k << 1 | 1].M);
tree[k].LM = max(max(tree[k<<1].LM, tree[k<<1|1].LM), tree[k<<1].val + tree[k<<1|1].M);
tree[k].MR = max(max(tree[k<<1].MR, tree[k<<1|1].MR), tree[k<<1].M + tree[k<<1|1].val);
tree[k].LMR = max(max(tree[k<<1].LMR, tree[k<<1|1].LMR), max(tree[k<<1].val + tree[k<<1|1].MR, tree[k<<1].LM +tree[k<<1|1].val));
}
void build(int k, int L, int R){
if (L == R){
int x = idx[L];
tree[k].tag = 0;
tree[k].val = dis[x];
tree[k].M = -2 * dis[x];
tree[k].LM = tree[k].MR = -dis[x];
tree[k].LMR = 0;
return ;
}
int mid = (L + R) >> 1;
build(k << 1, L, mid);
build(k << 1 | 1, mid + 1, R);
update(k);
}
void add(int p, int L, int R, int x, int y, ll d){
if (L == x && R == y){
add(p, d);
return;
}
pushdown(p);
int mid = (L + R) / 2;
if (y <= mid) add(p << 1, L, mid, x, y, d);
else if(x > mid) add(p << 1 | 1, mid + 1, R, x, y, d);
else{
add(p << 1, L, mid, x, mid, d);
add(p << 1 | 1, mid + 1, R, mid + 1, y, d);
}
update(p);
}
int main() {
n = readInt();
int q = readInt();
ll W = readLong();
FOR(i,1,n-1){
int x = readInt();
int y = readInt();
ll z = readLong();
edge[x].pb({y, z});
edge[y].pb({x, z});
e[i] = {x, y};
}
dfs(1);
build(1, 1, times);
/*FOR(i,1,times){
cout << idx[i] << " ";
}
cout << endl;
FOR(i,1,n){
cout << i << " " << X[i] << " " << Y[i] << endl;
}*/
ll ans = 0;
while (q--){
int k = (readInt() + ans) %(n-1) + 1;
ll w = (readLong() +ans) % W;
int x = pre[e[k].fi] == e[k].se ? e[k].fi : e[k].se;
//cout << x << " " << X[x] << " " << Y[x] << endl;
add(1,1, times, X[x], Y[x], w - weight[x]);
weight[x] = w;
printf("%lld\n", ans = tree[1].LMR);
}
return 0;
}
/*
4 3 2000
1 2 100
2 3 1000
2 4 1000
2 1030
1 1020
1 890
4 3 2000
1 2 100
2 3 1000
2 4 1000
1 2 3 2 4 2 1
2 1030
4 5 5
2030
1 1020
0 0 0
*/