Submission #259944

#TimeUsernameProblemLanguageResultExecution timeMemory
259944dooweyDynamic Diameter (CEOI19_diameter)C++14
18 / 100
5125 ms995736 KiB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pii;

#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

const ll N = (ll)1e5 + 5;
ll ww[N];
vector<pii> T[N];

struct Edge{
    ll id;
    ll op_l;
    ll op_r;
    ll el;
    ll er;
};

vector<Edge> G[N];
const ll M = (ll)1e7;

struct Node{
    ll val;
    ll lazy;
};

multiset<ll> all_di;

struct Centroid{
    set<pii> best;
    vector<Node> Tree;
    ll CURN;
    void init(ll n){
        CURN = n;
        for(ll i = 0 ; i <= n * 4 + 12; i ++) {
            Tree.push_back({0,0});
        }
    }
    void push(ll node, ll cl, ll cr){
        Tree[node].val += Tree[node].lazy;
        if(cl != cr){
            Tree[node * 2].lazy += Tree[node].lazy;
            Tree[node * 2 + 1].lazy += Tree[node].lazy;
        }
        Tree[node].lazy = 0;
    }
    void upd(ll node, ll cl, ll cr, ll tl, ll tr, ll v){
        push(node, cl, cr);
        if(cr < tl)
            return;
        if(cl > tr)
            return;
        if(cl >= tl && cr <= tr){
            Tree[node].lazy += v;
            push(node, cl, cr);
            return;
        }
        ll mid = (cl + cr) / 2;
        upd(node * 2, cl, mid, tl, tr, v);
        upd(node * 2 + 1, mid + 1, cr, tl, tr, v);
        Tree[node].val = max(Tree[node * 2].val, Tree[node * 2 + 1].val);
    }
    ll get(ll node, ll cl, ll cr, ll tl, ll tr){
        push(node, cl, cr);
        if(cr < tl)
            return 0ll;
        if(cl > tr)
            return 0ll;
        if(cl >= tl && cr <= tr){
            return Tree[node].val;
        }
        ll mid = (cl + cr) / 2;
        return max(get(node * 2, cl, mid, tl, tr), get(node * 2 + 1, mid + 1, cr, tl, tr));
    }
    void check(ll mode){
        ll sum = 0;
        auto it = best.end();
        for(ll j = 0 ; j < 2; j ++ ){
            if(it != best.begin()){
                it -- ;
                sum += it->fi;
            }
        }
        if(mode == 1){
            all_di.insert(sum);
        }
        else{
            all_di.erase(sum);
        }
    }
};

Centroid shi[M];
ll idccc = 0;

bool ban[N];
ll subt[N];

void dfs1(ll u, ll par){
    subt[u]=1;
    for(auto x : T[u]){
        if(ban[x.fi] || x.fi == par) continue;
        dfs1(x.fi, u);
        subt[u] += subt[x.fi];
    }
}

ll cc;
ll tin[N];
ll tout[N];
ll las;
ll sz;

void dfs2(ll u, ll par){
    tin[u] = cc++;
    ll ni, nj;
    for(auto x : T[u]){
        if(ban[x.fi] || x.fi == par) continue;
        dfs2(x.fi, u);
    }
    tout[u] = cc - 1;
}

void dfs3(ll u, ll par, ll l1, ll r1){
    ll ni, nj;
    for(auto x : T[u]){
        if(ban[x.fi] || x.fi == par) continue;
        ni = l1, nj = r1;
        if(ni == -1)
            ni = tin[x.fi], nj = tout[x.fi];
        dfs3(x.fi, u, ni, nj);
        shi[las].upd(1, 0, sz - 1, tin[x.fi], tout[x.fi], +ww[x.se]);
        G[x.se].push_back({las, tin[x.fi], tout[x.fi], ni, nj});
        if(par == -1){
            shi[las].best.insert(mp(shi[las].get(1, 0, sz - 1, tin[x.fi], tout[x.fi]), tin[x.fi]));
        }
    }
}

void decomp(ll node){
    dfs1(node, -1);
    ll pr = -1;
    bool ok = true;
    sz = subt[node];
    if(sz == 1)
        return;
    while(ok){
        ok = false;
        for(auto x : T[node]){
            if(ban[x.fi] || x.fi == pr) continue;
            if(subt[x.fi] > sz/2){
                pr = node;
                node = x.fi;
                ok = true;
                break;
            }
        }
    }
    shi[idccc].init(sz);
    las=idccc;
    idccc ++ ;
    cc = 0;
    dfs2(node, -1);
    dfs3(node, -1, -1, -1);
    shi[las].check(1);
    ban[node]=true;
    for(auto p : T[node]){
        if(ban[p.fi]) continue;
        decomp(p.fi);
    }
}

int main(){
    fastIO;
    ll lim;
    ll n, q;
    cin >> n >> q >> lim;
    ll u, v;
    for(ll i = 0 ; i < n - 1; i ++ ){
        cin >> u >> v >> ww[i];
        T[u].push_back(mp(v, i));
        T[v].push_back(mp(u, i));
    }
    decomp(1);
    ll lasres = 0;
    ll id;
    ll new_w;
    for(ll tt = 0; tt < q; tt ++ ){
        cin >> id >> new_w;
        id += lasres;
        new_w += lasres;
        id %= (n - 1);
        new_w %= lim;
        lasres = 0;
        for(auto y : G[id]){
            shi[y.id].check(0);
            shi[y.id].best.erase(mp(shi[y.id].get(1, 0, shi[y.id].CURN - 1, y.el, y.er), y.el));
            shi[y.id].upd(1, 0, shi[y.id].CURN - 1, y.op_l, y.op_r, new_w - ww[id]);
            shi[y.id].best.insert(mp(shi[y.id].get(1, 0, shi[y.id].CURN - 1, y.el, y.er), y.el));
            shi[y.id].check(1);
        }
        ww[id] = new_w;
        auto gt = all_di.end();
        gt -- ;
        lasres = *gt;
        cout << lasres << "\n";
    }
    return 0;
}

Compilation message (stderr)

diameter.cpp: In function 'void dfs2(ll, ll)':
diameter.cpp:122:8: warning: unused variable 'ni' [-Wunused-variable]
     ll ni, nj;
        ^~
diameter.cpp:122:12: warning: unused variable 'nj' [-Wunused-variable]
     ll ni, nj;
            ^~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...