답안 #579070

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
579070 2022-06-18T11:12:09 Z talant117408 Dynamic Diameter (CEOI19_diameter) C++17
31 / 100
5000 ms 242380 KB
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;

#define long                unsigned long 
#define pb                  push_back
#define mp                  make_pair
#define all(v)              (v).begin(),(v).end()
#define rall(v)             (v).rbegin(),(v).rend()
#define lb                  lower_bound
#define ub                  upper_bound
#define sz(v)               int((v).size())
#define do_not_disturb      ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl                '\n'

const int N = 1e5+7;
set <pair <int, ll>> graph[N];
vector <pair <pii, ll>> edges;
vector <vector <ll>> tree(N), lz(N);
set <pair <int, pii>> eulers[N];
multiset <ll, greater <ll>> to_leaves[N], ans;
int subtree[N], level[N], parent[N][20], timer, par[N];
int n, q;
ll w;

void push(int i, int v, int l, int r) {
    if (lz[i][v] != 0) {
        tree[i][v] += lz[i][v];
        if (l != r) {
            lz[i][v*2] += lz[i][v];
            lz[i][v*2+1] += lz[i][v];
        }
        lz[i][v] = 0;
    }
}

void update(int i, int v, int l, int r, int ql, int qr, ll val) {
    push(i, v, l, r);
    if (ql > r || l > qr) return;
    if (ql <= l && r <= qr) {
        lz[i][v] += val;
        push(i, v, l, r);
        return;
    }
    int mid = (l+r) >> 1;
    update(i, v*2, l, mid, ql, qr, val);
    update(i, v*2+1, mid+1, r, ql, qr, val);
    tree[i][v] = max(tree[i][v*2], tree[i][v*2+1]);
}

ll get(int i, int v, int l, int r, int ql, int qr) {
    push(i, v, l, r);
    if (ql > r || l > qr) return -1e18;
    if (ql <= l && r <= qr) return tree[i][v];
    int mid = (l+r) >> 1;
    return max(get(i, v*2, l, mid, ql, qr), get(i, v*2+1, mid+1, r, ql, qr));
}

void find_subtrees(int v, int p) {
    subtree[v] = 1;
    for (auto to : graph[v]) {
        if (to.first == p || level[to.first]) continue;
        find_subtrees(to.first, v);
        subtree[v] += subtree[to.first];
    }
}

int find_centroid(int v, int p, int saizu) {
    for (auto to : graph[v]) {
        if (to.first == p || level[to.first]) continue;
        if (subtree[to.first]*2 > saizu) return find_centroid(to.first, v, saizu);
    }
    return v;
}

void cnd_dfs(int v, int p, ll dist, int &origin, int &saizu, int &lev, int &origin2) {
    pii range;
    range.first = ++timer;
    update(origin, 1, 0, saizu-1, timer, timer, dist);
    parent[v][lev] = origin2;
    
    for (auto to : graph[v]) {
        if (to.first == p || level[to.first]) continue;
        cnd_dfs(to.first, v, dist+to.second, origin, saizu, lev, origin2);
    }
    range.second = timer;
    eulers[origin].insert(mp(v, range));
}

void centroid_decomposition(int v, int p = 0, int lev = 1) {
    find_subtrees(v, v);
    auto centroid = find_centroid(v, v, subtree[v]);
    level[centroid] = lev;
    par[centroid] = p;
    
    tree[centroid].resize(subtree[v]*4);
    lz[centroid].resize(subtree[v]*4);
    timer = 0;
    for (auto to : graph[centroid]) {
        if (level[to.first]) continue;
        int prev = timer+1;
        cnd_dfs(to.first, centroid, to.second, centroid, subtree[v], lev, to.first);
        to_leaves[centroid].insert(get(centroid, 1, 0, subtree[v]-1, prev, timer));
    }
    eulers[centroid].insert(mp(centroid, mp(0, timer)));
    ll res = 0, cnt = 0;
    for (auto to : to_leaves[centroid]) {
        res += to;
        if (++cnt > 1) break;
    }
    ans.insert(res);
    
    for (auto to : graph[centroid]) {
        if (level[to.first]) continue;
        centroid_decomposition(to.first, centroid, lev+1);
    }
}

void solve() {
    cin >> n >> q >> w;
    for (int i = 0; i < n-1; i++) {
        int a, b;
        ll c;
        cin >> a >> b >> c;
        if (a > b) swap(a, b);
        edges.pb(mp(mp(a, b), c));
        graph[a].insert(mp(b, c));
        graph[b].insert(mp(a, c));
    }
    centroid_decomposition(1);
    ll last = 0;
    while (q--) {
        ll d, e;
        cin >> d >> e;
        d = (d + last) % (n - 1);
        e = (e + last) % w; 
        auto a = edges[d].first.first, b = edges[d].first.second;
        auto &c = edges[d].second;
        if (level[a] > level[b]) swap(a, b);
        auto x = a;
        while (x) {
            {
                auto ita = (*eulers[x].lb(mp(a, mp(0, 0)))).second.first;
                auto itb = (*eulers[x].lb(mp(b, mp(0, 0)))).second.first;
                if (get(x, 1, 0, sz(eulers[x])-1, ita, ita) > get(x, 1, 0, sz(eulers[x])-1, itb, itb)) swap(a, b);
            }
            auto it = eulers[x].lb(mp(b, mp(0, 0)));
            auto range = (*it).second;
            auto pit = eulers[x].lb(mp(parent[b][level[x]], mp(0, 0)));
            auto prange = (*pit).second;
            ll res = 0, cnt = 0;
            for (auto to : to_leaves[x]) {
                res += to;
                if (++cnt > 1) break;
            }
            ans.erase(ans.find(res));
            to_leaves[x].erase(to_leaves[x].find(get(x, 1, 0, sz(eulers[x])-1, prange.first, prange.second)));
            update(x, 1, 0, sz(eulers[x])-1, range.first, range.second, e-c);
            to_leaves[x].insert(get(x, 1, 0, sz(eulers[x])-1, prange.first, prange.second));
            res = cnt = 0;
            for (auto to : to_leaves[x]) {
                res += to;
                if (++cnt > 1) break;
            }
            ans.insert(res);
            x = par[x];
        }
        cout << *ans.begin() << endl;
        c = e;
        last = *ans.begin();
    }
}

int main() {
    do_not_disturb
    
    int t = 1;
    //~ cin >> t;
    while (t--) {
        solve();
    }
    
    return 0;
}
/*

*/
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 19028 KB Output is correct
2 Correct 10 ms 19148 KB Output is correct
3 Correct 10 ms 19128 KB Output is correct
4 Correct 9 ms 19052 KB Output is correct
5 Correct 10 ms 19028 KB Output is correct
6 Correct 13 ms 19028 KB Output is correct
7 Correct 10 ms 19128 KB Output is correct
8 Correct 10 ms 19156 KB Output is correct
9 Correct 11 ms 19156 KB Output is correct
10 Correct 12 ms 19156 KB Output is correct
11 Correct 11 ms 19080 KB Output is correct
12 Correct 13 ms 19080 KB Output is correct
13 Correct 10 ms 19156 KB Output is correct
14 Correct 10 ms 19156 KB Output is correct
15 Correct 11 ms 19128 KB Output is correct
16 Correct 10 ms 19156 KB Output is correct
17 Correct 10 ms 19176 KB Output is correct
18 Correct 10 ms 19156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 19028 KB Output is correct
2 Correct 10 ms 19148 KB Output is correct
3 Correct 10 ms 19128 KB Output is correct
4 Correct 9 ms 19052 KB Output is correct
5 Correct 10 ms 19028 KB Output is correct
6 Correct 13 ms 19028 KB Output is correct
7 Correct 10 ms 19128 KB Output is correct
8 Correct 10 ms 19156 KB Output is correct
9 Correct 11 ms 19156 KB Output is correct
10 Correct 12 ms 19156 KB Output is correct
11 Correct 11 ms 19080 KB Output is correct
12 Correct 13 ms 19080 KB Output is correct
13 Correct 10 ms 19156 KB Output is correct
14 Correct 10 ms 19156 KB Output is correct
15 Correct 11 ms 19128 KB Output is correct
16 Correct 10 ms 19156 KB Output is correct
17 Correct 10 ms 19176 KB Output is correct
18 Correct 10 ms 19156 KB Output is correct
19 Correct 52 ms 20288 KB Output is correct
20 Correct 56 ms 20384 KB Output is correct
21 Correct 63 ms 20540 KB Output is correct
22 Correct 75 ms 20756 KB Output is correct
23 Correct 93 ms 25264 KB Output is correct
24 Correct 151 ms 26616 KB Output is correct
25 Correct 154 ms 27376 KB Output is correct
26 Correct 188 ms 28540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 19072 KB Output is correct
2 Correct 10 ms 19128 KB Output is correct
3 Correct 12 ms 19112 KB Output is correct
4 Correct 29 ms 19368 KB Output is correct
5 Correct 101 ms 20284 KB Output is correct
6 Correct 13 ms 19092 KB Output is correct
7 Correct 13 ms 19392 KB Output is correct
8 Correct 12 ms 19412 KB Output is correct
9 Correct 18 ms 19384 KB Output is correct
10 Correct 48 ms 19664 KB Output is correct
11 Correct 164 ms 20824 KB Output is correct
12 Correct 24 ms 22240 KB Output is correct
13 Correct 21 ms 22184 KB Output is correct
14 Correct 25 ms 22212 KB Output is correct
15 Correct 83 ms 22468 KB Output is correct
16 Correct 254 ms 23664 KB Output is correct
17 Correct 376 ms 80968 KB Output is correct
18 Correct 302 ms 80880 KB Output is correct
19 Correct 316 ms 81072 KB Output is correct
20 Correct 406 ms 81196 KB Output is correct
21 Correct 1208 ms 82400 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 20436 KB Output is correct
2 Correct 81 ms 20660 KB Output is correct
3 Correct 323 ms 21172 KB Output is correct
4 Correct 600 ms 21808 KB Output is correct
5 Correct 102 ms 37064 KB Output is correct
6 Correct 203 ms 37304 KB Output is correct
7 Correct 761 ms 37896 KB Output is correct
8 Correct 1491 ms 38584 KB Output is correct
9 Correct 416 ms 123852 KB Output is correct
10 Correct 644 ms 124248 KB Output is correct
11 Correct 1449 ms 124724 KB Output is correct
12 Correct 3137 ms 125556 KB Output is correct
13 Correct 966 ms 241104 KB Output is correct
14 Correct 1294 ms 241440 KB Output is correct
15 Correct 2602 ms 242004 KB Output is correct
16 Correct 4061 ms 242380 KB Output is correct
17 Execution timed out 5101 ms 242180 KB Time limit exceeded
18 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5078 ms 195264 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 19028 KB Output is correct
2 Correct 10 ms 19148 KB Output is correct
3 Correct 10 ms 19128 KB Output is correct
4 Correct 9 ms 19052 KB Output is correct
5 Correct 10 ms 19028 KB Output is correct
6 Correct 13 ms 19028 KB Output is correct
7 Correct 10 ms 19128 KB Output is correct
8 Correct 10 ms 19156 KB Output is correct
9 Correct 11 ms 19156 KB Output is correct
10 Correct 12 ms 19156 KB Output is correct
11 Correct 11 ms 19080 KB Output is correct
12 Correct 13 ms 19080 KB Output is correct
13 Correct 10 ms 19156 KB Output is correct
14 Correct 10 ms 19156 KB Output is correct
15 Correct 11 ms 19128 KB Output is correct
16 Correct 10 ms 19156 KB Output is correct
17 Correct 10 ms 19176 KB Output is correct
18 Correct 10 ms 19156 KB Output is correct
19 Correct 52 ms 20288 KB Output is correct
20 Correct 56 ms 20384 KB Output is correct
21 Correct 63 ms 20540 KB Output is correct
22 Correct 75 ms 20756 KB Output is correct
23 Correct 93 ms 25264 KB Output is correct
24 Correct 151 ms 26616 KB Output is correct
25 Correct 154 ms 27376 KB Output is correct
26 Correct 188 ms 28540 KB Output is correct
27 Correct 12 ms 19072 KB Output is correct
28 Correct 10 ms 19128 KB Output is correct
29 Correct 12 ms 19112 KB Output is correct
30 Correct 29 ms 19368 KB Output is correct
31 Correct 101 ms 20284 KB Output is correct
32 Correct 13 ms 19092 KB Output is correct
33 Correct 13 ms 19392 KB Output is correct
34 Correct 12 ms 19412 KB Output is correct
35 Correct 18 ms 19384 KB Output is correct
36 Correct 48 ms 19664 KB Output is correct
37 Correct 164 ms 20824 KB Output is correct
38 Correct 24 ms 22240 KB Output is correct
39 Correct 21 ms 22184 KB Output is correct
40 Correct 25 ms 22212 KB Output is correct
41 Correct 83 ms 22468 KB Output is correct
42 Correct 254 ms 23664 KB Output is correct
43 Correct 376 ms 80968 KB Output is correct
44 Correct 302 ms 80880 KB Output is correct
45 Correct 316 ms 81072 KB Output is correct
46 Correct 406 ms 81196 KB Output is correct
47 Correct 1208 ms 82400 KB Output is correct
48 Correct 21 ms 20436 KB Output is correct
49 Correct 81 ms 20660 KB Output is correct
50 Correct 323 ms 21172 KB Output is correct
51 Correct 600 ms 21808 KB Output is correct
52 Correct 102 ms 37064 KB Output is correct
53 Correct 203 ms 37304 KB Output is correct
54 Correct 761 ms 37896 KB Output is correct
55 Correct 1491 ms 38584 KB Output is correct
56 Correct 416 ms 123852 KB Output is correct
57 Correct 644 ms 124248 KB Output is correct
58 Correct 1449 ms 124724 KB Output is correct
59 Correct 3137 ms 125556 KB Output is correct
60 Correct 966 ms 241104 KB Output is correct
61 Correct 1294 ms 241440 KB Output is correct
62 Correct 2602 ms 242004 KB Output is correct
63 Correct 4061 ms 242380 KB Output is correct
64 Execution timed out 5101 ms 242180 KB Time limit exceeded
65 Halted 0 ms 0 KB -