Submission #946674

# Submission time Handle Problem Language Result Execution time Memory
946674 2024-03-14T21:57:12 Z Vladth11 Dynamic Diameter (CEOI19_diameter) C++14
7 / 100
382 ms 322772 KB
#include <bits/stdc++.h>
#define debug(x) cerr << #x << " " << x << "\n"
#define debugs(x) cerr << #x << " " << x << " "
#pragma GCC optimize("Ofast")

#define int ll

using namespace std;
typedef long long ll;
typedef pair <ll, ll> pii;

const ll NMAX = 300002;
const ll INF = (1LL << 58);
const ll nrbits = 18;
const ll MOD = 998244353;

vector <pii> v[NMAX];
vector <int> children[NMAX];
int n;
pii timp[NMAX * nrbits];

struct edge {
    int a, b;
    ll c;
} edges[NMAX];

class AINT {
public:
    struct Node {
        ll mx, lazy;
    };
    Node* aint;
    int n;
    void init(int _n) {
        n = _n;
        aint = new Node[4 * (n + 1)];
        for(int i = 0; i < 4 * (n + 1); i++) {
            aint[i].mx = 0;
            aint[i].lazy = 0;
        }
    }
    Node combine(Node a, Node b) {
        Node sol = {0, 0};
        sol.mx = max(a.mx, b.mx);
        return sol;
    }
    void propaga(int node, int st, int dr) {
        if(st != dr) {
            aint[node * 2].lazy += aint[node].lazy;
            aint[node * 2 + 1].lazy += aint[node].lazy;
        }
        aint[node].mx += aint[node].lazy;
        aint[node].lazy = 0;
    }
    void baga(int node, int st, int dr, int a, int b, int val) {
        propaga(node, st, dr);
        if(a <= st && dr <= b) {
            aint[node].lazy += val;
            return;
        }
        int mid = (st + dr) / 2;
        if(a <= mid) {
            baga(node * 2, st, mid, a, b, val);
        }
        if(b > mid) {
            baga(node * 2 + 1, mid + 1, dr, a, b, val);
        }
        propaga(node * 2, st, mid);
        propaga(node * 2 + 1, mid + 1, dr);
        aint[node] = combine(aint[node * 2], aint[node * 2 + 1]);
    }
    ll sol(int node, int st, int dr, int a, int b){
        propaga(node, st, dr);
        if(a <= st && dr <= b){
            return aint[node].mx;
        }
        int mid = (st + dr) / 2;
        ll maxim = 0;
        if(a <= mid)
            maxim = max(maxim, sol(node * 2, st, mid, a, b));
        if(b > mid)
            maxim = max(maxim, sol(node * 2 + 1, mid + 1, dr, a, b));
        return maxim;
    }
    void update(int st, int dr, int val) {
        baga(1, 1, n, st, dr, val);
    }
    ll query(int st, int dr) {
        return sol(1, 1, n, st, dr);
    }
} st[NMAX];

int viz[NMAX];
int stamp;

struct event {
    int st, dr, unde, subarb;
};

vector <event> events[NMAX];

multiset <ll> candidates;
multiset <ll> indv[NMAX];

int sz[NMAX];
int total;

void getsz(int node, int p) {
    sz[node] = 1;
    for(auto x : v[node]) {
        if(x.first == p || viz[x.first]) continue;
        getsz(x.first, node);
        sz[node] += sz[x.first];
    }
    total = sz[node];
}

int findCentroid(int node, int p) {
    for(auto x : v[node]) {
        if(x.first == p || viz[x.first]) continue;
        if(sz[x.first] > total / 2)
            return findCentroid(x.first, node);
    }
    return node;
}

int unde;
int subarb;

void DFS(int node, int p) {
    stamp++;
    if(timp[subarb].first == 0) timp[subarb].first = stamp;
    timp[subarb].second = stamp;
    for(auto x : v[node]) {
        if(x.first == p || viz[x.first]) continue;
        event nou = {stamp + 1, 0, unde, subarb};
        DFS(x.first, node);
        nou.dr = stamp;
        events[x.second].push_back(nou);
    }
}

int cnt = 0;

void centroid(int node) {
    getsz(node, 0);
    int C = findCentroid(node, 0);
    st[C].init(sz[C]);
    viz[C] = 1;
    stamp = 1; /// n-are logica tbh
    unde = C;
    for(auto x : v[C]){
        if(viz[x.first]) continue;
        children[C].push_back(++cnt);
        subarb = cnt;
        event nou = {stamp + 1, 0, unde, subarb};
        DFS(x.first, C);
        nou.dr = stamp;
        events[x.second].push_back(nou);
    }
    for(auto x : v[C]) {
        if(viz[x.first]) continue;
        centroid(x.first);
    }
}

ll compute(int i){
    if(indv[i].size() == 0)
        return 0;
    if(indv[i].size() == 1)
        return *indv[i].begin();
    auto it =indv[i].end();
    return (*prev(it) + *prev(prev(it)));
}

signed main() {
#ifdef HOME
    ifstream cin(".in");
    ofstream cout(".out");
#endif // HOME
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int q, w, i;
    cin >> n >> q >> w;
    for(i = 1; i < n; i++) {
        int a, b;
        ll c;
        cin >> a >> b >> c;
        v[a].push_back({b, i});
        v[b].push_back({a, i});
        edges[i] = {a, b, c};
    }
    centroid(1);
    for(int a = 1; a < n; a++) {
        for(auto x : events[a]) {
            st[x.unde].update(x.st, x.dr, edges[a].c);
        }
    }
    for(i = 1; i <= n; i++){
        //debug(i);
        for(auto x : children[i]){
            //debug(st[i].query(timp[x].first, timp[x].second));
            indv[i].insert(st[i].query(timp[x].first, timp[x].second));
        }
        candidates.insert(compute(i));
    }
    ll last = 0;
    for(i = 1; i <= q; i++) {
        ll a, b;
        cin >> a >> b;
        a += last;
        a %= (n - 1);
        b += last;
        b %= w;
        a++;
        int toAdd = b - edges[a].c;
        edges[a].c = b;
        for(auto x : events[a]) {
            candidates.erase(candidates.find(compute(x.unde)));
            indv[x.unde].erase(indv[x.unde].find(st[x.unde].query(timp[x.subarb].first, timp[x.subarb].second)));
            st[x.unde].update(x.st, x.dr, toAdd);
            indv[x.unde].insert(st[x.unde].query(timp[x.subarb].first, timp[x.subarb].second));
            candidates.insert(compute(x.unde));
        }
        cout << *prev(candidates.end()) << '\n';
        last = *prev(candidates.end());
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Runtime error 43 ms 85072 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 43 ms 85072 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 9 ms 42076 KB Output is correct
2 Correct 9 ms 42076 KB Output is correct
3 Correct 11 ms 42076 KB Output is correct
4 Correct 18 ms 42076 KB Output is correct
5 Correct 63 ms 42332 KB Output is correct
6 Correct 9 ms 42024 KB Output is correct
7 Correct 10 ms 42332 KB Output is correct
8 Correct 10 ms 42332 KB Output is correct
9 Correct 11 ms 42288 KB Output is correct
10 Correct 22 ms 42332 KB Output is correct
11 Correct 88 ms 42580 KB Output is correct
12 Correct 13 ms 44240 KB Output is correct
13 Correct 13 ms 44072 KB Output is correct
14 Correct 18 ms 44124 KB Output is correct
15 Correct 32 ms 44452 KB Output is correct
16 Correct 111 ms 44616 KB Output is correct
17 Correct 119 ms 89720 KB Output is correct
18 Correct 120 ms 89788 KB Output is correct
19 Correct 122 ms 89792 KB Output is correct
20 Correct 148 ms 89788 KB Output is correct
21 Correct 294 ms 90304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 40 ms 86820 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 382 ms 322772 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 43 ms 85072 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -