Submission #856516

# Submission time Handle Problem Language Result Execution time Memory
856516 2023-10-03T18:52:10 Z hocky Dynamic Diameter (CEOI19_diameter) C++17
24 / 100
207 ms 63828 KB
#include "bits/stdc++.h"

using namespace std;
const int LIM = 100000;
#define fi first
#define se second
typedef pair < int, int > PII;
typedef long long LL;

namespace DynamicDiameter {
    vector <pair<int, LL>> edge[LIM + 5];
    int event[2 * LIM + 5];
    int dfsTime = 0;
    LL weights[LIM + 5], depth[LIM + 5];
    PII order[LIM + 5];
    void eulerTour(int pos, int par = -1) {
        dfsTime++;
        order[pos].fi = dfsTime;
        event[dfsTime] = pos;
        for (auto &nx: edge[pos]) {
            if (nx.fi == par) continue;
            depth[nx.fi] = depth[pos] + nx.se;
            weights[nx.fi] = nx.se;
            eulerTour(nx.fi, pos);
            dfsTime++;
            event[dfsTime] = pos;
        }
        order[pos].se = dfsTime;
        return;
    }
    
    const LL INF = 1e14;
    // Segment Tree starts here
    struct Node{
		LL maxDepth = -INF, minDepth = INF;
		LL leftMax = -INF, rightMax = -INF;
		LL diameter = 0;
		LL lazy;
	};
	
	Node segt[400005];
	void pushDown(int rangeL = 1, int rangeR = dfsTime, int idx = 1){
		if(segt[idx].lazy == 0) return;
		auto &val = segt[idx].lazy;
		segt[idx].maxDepth += val;
		segt[idx].minDepth += val;
		segt[idx].leftMax -= val;
		segt[idx].rightMax -= val;
		if(rangeL != rangeR){
			segt[idx * 2].lazy += val;
			segt[idx * 2 + 1].lazy += val;
		}
		val = 0;
		return;
	}
	void merge(Node &head, Node &l, Node &r){
		head.maxDepth = max(l.maxDepth, r.maxDepth);
		head.minDepth = min(l.minDepth, r.minDepth);
		
		head.leftMax = max(l.leftMax, r.leftMax);
		head.leftMax = max(head.leftMax, l.maxDepth - 2 * r.minDepth);
		
		head.rightMax = max(l.rightMax, r.rightMax);
		head.rightMax = max(head.rightMax, r.maxDepth - 2 * l.minDepth);
		
		head.diameter = max(l.diameter, r.diameter);
		head.diameter = max(head.diameter, l.maxDepth + r.rightMax);
		head.diameter = max(head.diameter, r.maxDepth + l.leftMax);
		//~ cout << head.maxDepth << " " << head.minDepth << " " << head.leftMax << " " << head.rightMax << " " << head.diameter << endl;
	}
	void build(int rangeL = 1, int rangeR = dfsTime, int idx = 1){
		//~ cout << rangeL << " " << rangeR << " " << idx << endl;
		if(rangeL == rangeR){
			segt[idx].minDepth = segt[idx].maxDepth = depth[event[rangeL]];
			segt[idx].leftMax = segt[idx].rightMax = -depth[event[rangeL]];
			return;
		}
		int mid = (rangeL + rangeR) >> 1;
		build(rangeL, mid, idx * 2);
		build(mid + 1, rangeR, idx * 2 + 1);
		merge(segt[idx], segt[idx * 2], segt[idx * 2 + 1]);
	}
    void update(int queryL, int queryR, int val, int rangeL = 1, int rangeR = dfsTime, int idx = 1){
		pushDown(rangeL, rangeR, idx);
		if(rangeR < queryL) return;
		if(queryR < rangeL) return; 
		if(queryL <= rangeL && rangeR <= queryR){
			segt[idx].lazy = val;
			pushDown(rangeL, rangeR, idx);
			return;
		}
		int mid = (rangeL + rangeR) >> 1;
		update(queryL, queryR, val, rangeL, mid, idx * 2);
		update(queryL, queryR, val, mid + 1, rangeR, idx * 2 + 1);
		merge(segt[idx], segt[idx * 2], segt[idx * 2 + 1]);
		return;
	}
}

using namespace DynamicDiameter;

int main() {
    cin.tie(0) -> sync_with_stdio(0);
    cout.tie(0);
	int n, q, w; cin >> n >> q >> w;
	vector <PII> edges(n - 1);
    for (int i = 0; i < n - 1; i++) {
		int c;
        cin >> edges[i].fi >> edges[i].se >> c;
        edge[edges[i].fi].emplace_back(edges[i].se, c);
        edge[edges[i].se].emplace_back(edges[i].fi, c);
    }
    eulerTour(1);
    //~ for(int i = 1;i <= dfsTime;i++) cout << event[i] << " ";
    //~ cout << endl;
    //~ for(int i = 1;i <= n;i++) cout << i << " " << order[i].fi << " " << order[i].se << endl;
    build();
    int ans = 0;
    //~ cout << segt[1].diameter << endl;
    //~ return 0;
    while(q--){
		int d, e; cin >> d >> e;
		d = (d + ans) % (n - 1);
		e = (e + ans) % w;
		auto &edge = edges[d];
		if(order[edge.fi].fi > order[edge.se].fi) {
			swap(edge.fi, edge.se);
		}
		// Update the segment tree
		//~ cout << e - weights[edge.se] << endl;
		//~ cout << segt[1].diameter << endl;
		update(order[edge.se].fi, order[edge.se].se, e - weights[edge.se]);
		weights[edge.se] = e;
		pushDown();
		ans = segt[1].diameter;
		cout << ans << endl;
	}
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 24664 KB Output is correct
2 Correct 4 ms 24668 KB Output is correct
3 Correct 4 ms 24668 KB Output is correct
4 Correct 4 ms 24668 KB Output is correct
5 Correct 5 ms 24668 KB Output is correct
6 Correct 4 ms 24728 KB Output is correct
7 Correct 4 ms 24916 KB Output is correct
8 Correct 4 ms 24668 KB Output is correct
9 Correct 4 ms 24668 KB Output is correct
10 Correct 4 ms 24664 KB Output is correct
11 Correct 5 ms 24668 KB Output is correct
12 Correct 4 ms 24664 KB Output is correct
13 Correct 4 ms 24668 KB Output is correct
14 Correct 4 ms 24668 KB Output is correct
15 Correct 4 ms 24668 KB Output is correct
16 Correct 4 ms 24920 KB Output is correct
17 Correct 4 ms 24664 KB Output is correct
18 Correct 4 ms 24668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 24664 KB Output is correct
2 Correct 4 ms 24668 KB Output is correct
3 Correct 4 ms 24668 KB Output is correct
4 Correct 4 ms 24668 KB Output is correct
5 Correct 5 ms 24668 KB Output is correct
6 Correct 4 ms 24728 KB Output is correct
7 Correct 4 ms 24916 KB Output is correct
8 Correct 4 ms 24668 KB Output is correct
9 Correct 4 ms 24668 KB Output is correct
10 Correct 4 ms 24664 KB Output is correct
11 Correct 5 ms 24668 KB Output is correct
12 Correct 4 ms 24664 KB Output is correct
13 Correct 4 ms 24668 KB Output is correct
14 Correct 4 ms 24668 KB Output is correct
15 Correct 4 ms 24668 KB Output is correct
16 Correct 4 ms 24920 KB Output is correct
17 Correct 4 ms 24664 KB Output is correct
18 Correct 4 ms 24668 KB Output is correct
19 Correct 12 ms 24668 KB Output is correct
20 Correct 12 ms 24732 KB Output is correct
21 Correct 12 ms 24668 KB Output is correct
22 Correct 13 ms 24924 KB Output is correct
23 Correct 14 ms 25160 KB Output is correct
24 Correct 14 ms 25172 KB Output is correct
25 Correct 14 ms 25180 KB Output is correct
26 Correct 15 ms 25436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 24668 KB Output is correct
2 Correct 4 ms 24724 KB Output is correct
3 Correct 8 ms 24684 KB Output is correct
4 Correct 34 ms 25548 KB Output is correct
5 Correct 137 ms 25632 KB Output is correct
6 Correct 4 ms 24916 KB Output is correct
7 Correct 4 ms 24668 KB Output is correct
8 Correct 4 ms 24724 KB Output is correct
9 Correct 8 ms 24668 KB Output is correct
10 Correct 32 ms 24988 KB Output is correct
11 Correct 158 ms 25816 KB Output is correct
12 Correct 5 ms 24924 KB Output is correct
13 Correct 6 ms 24924 KB Output is correct
14 Correct 8 ms 24924 KB Output is correct
15 Correct 35 ms 25180 KB Output is correct
16 Correct 153 ms 26520 KB Output is correct
17 Runtime error 49 ms 61892 KB Execution killed with signal 11
18 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 24668 KB Output is correct
2 Correct 20 ms 24924 KB Output is correct
3 Correct 85 ms 25404 KB Output is correct
4 Correct 160 ms 26092 KB Output is correct
5 Correct 9 ms 25436 KB Output is correct
6 Correct 24 ms 25432 KB Output is correct
7 Correct 92 ms 26004 KB Output is correct
8 Correct 174 ms 27028 KB Output is correct
9 Correct 20 ms 28508 KB Output is correct
10 Correct 37 ms 28748 KB Output is correct
11 Correct 108 ms 29268 KB Output is correct
12 Correct 207 ms 30044 KB Output is correct
13 Runtime error 53 ms 63828 KB Execution killed with signal 11
14 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 29 ms 57892 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 24664 KB Output is correct
2 Correct 4 ms 24668 KB Output is correct
3 Correct 4 ms 24668 KB Output is correct
4 Correct 4 ms 24668 KB Output is correct
5 Correct 5 ms 24668 KB Output is correct
6 Correct 4 ms 24728 KB Output is correct
7 Correct 4 ms 24916 KB Output is correct
8 Correct 4 ms 24668 KB Output is correct
9 Correct 4 ms 24668 KB Output is correct
10 Correct 4 ms 24664 KB Output is correct
11 Correct 5 ms 24668 KB Output is correct
12 Correct 4 ms 24664 KB Output is correct
13 Correct 4 ms 24668 KB Output is correct
14 Correct 4 ms 24668 KB Output is correct
15 Correct 4 ms 24668 KB Output is correct
16 Correct 4 ms 24920 KB Output is correct
17 Correct 4 ms 24664 KB Output is correct
18 Correct 4 ms 24668 KB Output is correct
19 Correct 12 ms 24668 KB Output is correct
20 Correct 12 ms 24732 KB Output is correct
21 Correct 12 ms 24668 KB Output is correct
22 Correct 13 ms 24924 KB Output is correct
23 Correct 14 ms 25160 KB Output is correct
24 Correct 14 ms 25172 KB Output is correct
25 Correct 14 ms 25180 KB Output is correct
26 Correct 15 ms 25436 KB Output is correct
27 Correct 4 ms 24668 KB Output is correct
28 Correct 4 ms 24724 KB Output is correct
29 Correct 8 ms 24684 KB Output is correct
30 Correct 34 ms 25548 KB Output is correct
31 Correct 137 ms 25632 KB Output is correct
32 Correct 4 ms 24916 KB Output is correct
33 Correct 4 ms 24668 KB Output is correct
34 Correct 4 ms 24724 KB Output is correct
35 Correct 8 ms 24668 KB Output is correct
36 Correct 32 ms 24988 KB Output is correct
37 Correct 158 ms 25816 KB Output is correct
38 Correct 5 ms 24924 KB Output is correct
39 Correct 6 ms 24924 KB Output is correct
40 Correct 8 ms 24924 KB Output is correct
41 Correct 35 ms 25180 KB Output is correct
42 Correct 153 ms 26520 KB Output is correct
43 Runtime error 49 ms 61892 KB Execution killed with signal 11
44 Halted 0 ms 0 KB -