Submission #1368781

#TimeUsernameProblemLanguageResultExecution timeMemory
1368781otariusDynamic Diameter (CEOI19_diameter)C++20
100 / 100
2085 ms40692 KiB
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <cstring>
#include <queue>
#include <map>
#include <cmath>
#include <iomanip>
using namespace std;

void __print(int x) { cerr << x; }

void __print(long x) { cerr << x; }

void __print(long long x) { cerr << x; }

void __print(unsigned x) { cerr << x; }

void __print(unsigned long x) { cerr << x; }

void __print(unsigned long long x) { cerr << x; }

void __print(float x) { cerr << x; }

void __print(double x) { cerr << x; }

void __print(long double x) { cerr << x; }

void __print(char x) { cerr << '\'' << x << '\''; }

void __print(const char *x) { cerr << '\"' << x << '\"'; }

void __print(const string &x) { cerr << '\"' << x << '\"'; }

void __print(bool x) { cerr << (x ? "true" : "false"); }

template<typename T, typename V>
void __print(const pair<T, V> &x) {
    cerr << '{';
    __print(x.first);
    cerr << ',';
    __print(x.second);
    cerr << '}';
}

template<typename T>
void __print(const T &x) {
    int f = 0;
    cerr << '{';
    for (auto &i: x) cerr << (f++ ? "," : ""), __print(i);
    cerr << "}";
}

void _print() { cerr << "]\n"; }

template<typename T, typename... V>
void _print(T t, V... v) {
    __print(t);
    if (sizeof...(v)) cerr << ", ";
    _print(v...);
}

#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

#define ff first
#define sc second
#define pb push_back
#define ll long long
#define pll pair<ll, ll>
#define pii pair <int, int>
#define ull unsigned long long

#define int long long
// #define int unsigned long long

const ll mod = 1e9 + 7;
const ll weirdMod = 998244353;
const int mxN = 1e5 + 20;
const int lg = 20;

struct node {
    int x, y, vl;
} t[4 * mxN];
vector<int> adj[mxN];
int n, q, w, a[mxN], b[mxN], c[mxN], par[mxN][lg], in[mxN], out[mxN], rev[mxN], tim, fen[mxN], lazy[4 * mxN];
bool isanc(int x, int y) {
    return (in[x] <= in[y] && out[x] >= out[y]);
}
int lca(int x, int y) {
    if (isanc(x, y)) return x;
    if (isanc(y, x)) return y;
    for (int i = lg - 1; i >= 0; i--)
        if (!isanc(par[x][i], y)) x = par[x][i];
    return par[x][0];
}
void prop(int v, int tl, int tr) {
    if (tl != tr) {
        lazy[2 * v] += lazy[v];
        lazy[2 * v + 1] += lazy[v];
        lazy[v] = 0;
    }
}
void update(int v, int tl, int tr, int l, int r, int val) {
    if (lazy[v]) prop(v, tl, tr);
    if (r < tl || tr < l) return;
    if (l <= tl && tr <= r) {
        if (tl != tr) {
            lazy[2 * v] += val;
            lazy[2 * v + 1] += val;
        } else lazy[v] += val;
        return;
    } int tm = (tl + tr) / 2;
    update(2 * v, tl, tm, l, min(r, tm), val);
    update(2 * v + 1, tm + 1, tr, max(l, tm + 1), r, val);
}
ll get(int v, int tl, int tr, int pos) {
    if (lazy[v]) prop(v, tl, tr);
    if (tl == tr) return lazy[v];
    int tm = (tl + tr) / 2;
    if (pos <= tm) return get(2 * v, tl, tm, pos);
    return get(2 * v + 1, tm + 1, tr, pos);
}
int query(int x, int y) {
    return get(1, 1, n, in[x]) + get(1, 1, n, in[y]) - 2 * get(1, 1, n, in[lca(x, y)]);
}
void dfs(int v, int p) {
    in[v] = ++tim;
    par[v][0] = p;
    rev[in[v]] = v;
    for (int i = 1; i < lg; i++)
        par[v][i] = par[par[v][i - 1]][i - 1];
    for (int u : adj[v])
        if (u != p) dfs(u, v);
    out[v] = tim;
}
void merge(int v, int x, int y) {
    int mx, a, b;
    if (t[x].vl > t[y].vl) {
        mx = t[x].vl; a = t[x].x; b = t[x].y;
    } else {
        mx = t[y].vl; a = t[y].x; b = t[y].y;
    } for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            int dx = t[x].x, dy = t[y].x;
            if (i) dx = t[x].y;
            if (j) dy = t[y].y;
            int cr = query(dx, dy);
            if (cr > mx) {
                mx = cr; a = dx; b = dy;
            }
        }
    } t[v].vl = mx; t[v].x = a; t[v].y = b;
}
void build(int v, int tl, int tr) {
    if (tl == tr) {
        t[v].vl = 0;
        t[v].x = rev[tl];
        t[v].y = rev[tl];
        return;
    } int tm = (tl + tr) / 2;
    build(2 * v, tl, tm);
    build(2 * v + 1, tm + 1, tr);
    merge(v, 2 * v, 2 * v + 1);
}
void update(int v, int tl, int tr, int l, int r) {
    if (r < tl || tr < l) return;
    if (l <= tl && tr <= r) return;
    int tm = (tl + tr) / 2;
    update(2 * v, tl, tm, l, min(r, tm));
    update(2 * v + 1, tm + 1, tr, max(l, tm + 1), r);
    merge(v, 2 * v, 2 * v + 1);
}
void solve() {
    cin >> n >> q >> w;
    for (int i = 1; i < n; i++) {
        cin >> a[i] >> b[i] >> c[i];
        adj[a[i]].pb(b[i]); adj[b[i]].pb(a[i]);
    } dfs(1, 1);
    for (int i = 1; i < n; i++) {
        if (in[a[i]] < in[b[i]]) swap(a[i], b[i]);
        update(1, 1, n, in[a[i]], out[a[i]], c[i]);
    } build(1, 1, n);
    int last = 0, d, e;
    while (q--) {
        cin >> d >> e;
        d = (d + last) % (n - 1) + 1;
        e = (e + last) % w;
        update(1, 1, n, in[a[d]], out[a[d]], -c[d]);
        c[d] = e;
        update(1, 1, n, in[a[d]], out[a[d]], c[d]);
        update(1, 1, n, in[a[d]], out[a[d]]);
        cout << t[1].vl << '\n'; last = t[1].vl;
    }
}
int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    
    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
        cout << '\n';
    }
    return 0;
}
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...