CEOI2019 / CodeForces 1192B. Dynamic Diameter

50 篇文章 0 订阅
6 篇文章 0 订阅

题意:给出一个带权树,每次修改树上一条边的权值,询问树的直径

题解: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
*/

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值