RE 求助
查看原帖
RE 求助
363006
wangyibo201026楼主2022/6/9 20:57
#include<bits/stdc++.h>

#define endl '\n';
#define int long long

using namespace std;

const int N = 2e4 + 5;
const int M = 8e4 + 5;

int n, q, w;
int W[N], dep[N];

int id[N], cnt, in[N], out[N], fa[N];

// Graph | Start
int head[N], tot;

struct Graph{
  int to, w, next;
}edges[N * 2];

void add(int u, int v, int w){
  tot++;
  edges[tot].to = v;
  edges[tot].w = w;
  edges[tot].next = head[u];
  head[u] = tot;
}
// Graph | End

// Segment tree | Start
int maxd[M], mind[M], lm[M], mr[M], lmr[M], tag[M];

void pushup(int node){
  maxd[node] = max(maxd[node << 1], maxd[node << 1 | 1]);
  mind[node] = min(mind[node << 1], mind[node << 1 | 1]);
  lm[node] = max(max(lm[node << 1], lm[node << 1 | 1]), maxd[node << 1] - mind[node << 1 | 1] * 2);
  mr[node] = max(max(mr[node << 1], mr[node << 1 | 1]), maxd[node << 1 | 1] - mind[node << 1] * 2);
  lmr[node] = max(max(lmr[node << 1], lmr[node << 1 | 1]), max(lm[node << 1] + maxd[node << 1 | 1], maxd[node] + mr[node << 1 | 1]));
}

void addtag(int node, int val){
  maxd[node] += val;;
  mind[node] += val;
  lm[node] -= val;
  mr[node] -= val;
  tag[node] += val;
}

void pushdown(int node){
  if(!tag[node]){
    return ;
  }
  addtag(node << 1, tag[node]);
  addtag(node << 1 | 1, tag[node]);
  tag[node] = 0;
}

void build(int node, int lt, int rt){
  if(lt == rt){
    addtag(node, dep[id[lt]]);
    return ;
  }
  int mid = lt + rt >> 1;
  build(node << 1, lt, mid);
  build(node << 1 | 1, mid + 1, rt);
  pushup(node);
}

void update(int node, int lt, int rt, int x, int y, int val){
  if(y < lt || x > rt){
    return ;
  }
  if(x <= lt && rt <= y){
    addtag(node, val);
  }
  pushdown(node);
  int mid = lt + rt >> 1;
  update(node << 1, lt, mid, x, y, val);
  update(node << 1 | 1, mid + 1, rt, x, y, val);
  pushup(node);
}
// Segment tree | End

void dfs(int x, int f){
  id[++cnt] = x;
  in[x] = cnt;
  fa[x] = f;
  for(int i = head[x]; i; i = edges[i].next){
    if(edges[i].to != f){
      dep[edges[i].to] = dep[x] + edges[i].w;
      dfs(edges[i].to, x);
      id[++cnt] = x;
    }
  }
  out[x] = cnt;
}

void Solve(){
  cin >> n >> q >> w;
  for(int i = 1; i < n; i++){
    int u, v, w;
    cin >> u >> v >> w;
    add(u, v, w);
    add(v, u, w);
    W[i] = w;
  }
  dfs(1, 0);
  build(1, 1, cnt);
  int lastans = 0;
  for(int i = 1; i <= q; i++){
    int x, val;
    cin >> x >> val;
    x = (x + lastans) % (n - 1) + 1;
    val = (val + lastans) % w;
    update(1, 1, cnt, in[x], out[x], val - W[x]);
    W[x] = val;
    cout << (lastans = lmr[1]) << endl;
  }
}

signed main(){
  Solve();
  return 0;
}
2022/6/9 20:57
加载中...