Submission #373764

# Submission time Handle Problem Language Result Execution time Memory
373764 2021-03-05T16:30:42 Z lohacho Construction of Highway (JOI18_construction) C++14
100 / 100
795 ms 22636 KB
#include <bits/stdc++.h>

using namespace std;

const int NS = (int)1e5 + 4;
int n, c[NS];
vector<int> way[NS];
int sz[NS], in[NS], incnt, up[NS], pr[NS];
pair<int, int> que[NS];
vector<pair<int, int>> block;

void dfs_sz(int x, int from){
	sz[x] = 1; pr[x] = from;
	for(auto&nxt:way[x]){
		if(nxt == from){
			continue;
		}
		dfs_sz(nxt, x);
		sz[x] += sz[nxt];
		if(way[x][0] == from || sz[way[x][0]] < sz[nxt]){
			swap(way[x][0], nxt);
		}
	}
}

void dfs_hld(int x, int from){
	in[x] = incnt++;
	for(auto&nxt:way[x]){
		if(nxt == from){
			continue;
		}
		if(way[x][0] == nxt){
			up[nxt] = up[x];
		}
		else{
			up[nxt] = nxt;
		}
		dfs_hld(nxt, x);
	}
}

struct Seg{
	int n;
	vector<int> tree, lazy;
	Seg(int N):n(N){
		tree.resize(n * 4, -1), lazy.resize(n * 4, -1);
	}
	void add(int x, int l, int r, int val){
		lazy[x] = val;
		tree[x] = val;
	}
	void update(int x, int l, int r){
		if(lazy[x] == -1) return;
		int mid = (l + r) >> 1;
		add(x * 2, l, mid, lazy[x]), add(x * 2 + 1, mid + 1, r, lazy[x]);
		lazy[x] = -1;
	}
	void push(int x, int l, int r, int pl, int pr, int val){
		if(pr < l || pl > r || pl > pr) return;
		if(l >= pl && r <= pr){
			add(x, l, r, val);
			return;
		}
		update(x, l, r);
		int mid = (l + r) >> 1;
		push(x * 2, l, mid, pl, pr, val), push(x * 2 + 1, mid + 1, r, pl, pr, val);
		if(tree[x * 2] == tree[x * 2 + 1] && tree[x * 2] != -1){
			tree[x] = tree[x * 2];
		}
		else{
			tree[x] = -1;
		}
	}
	void get(int x, int l, int r, int fl, int fr){
		if(fr < l || fl > r || fl > fr) return;
		if(tree[x] != -1){
			block.push_back({tree[x], min(r, fr) - max(l, fl) + 1});
			return;
		}
		update(x, l, r);
		int mid = (l + r) >> 1;
		get(x * 2 + 1, mid + 1, r, fl, fr);
		get(x * 2, l, mid, fl, fr);
	}
};

struct Fenwick{
	int n;
	vector<int> tree;
	Fenwick(int N):n(N){
		tree.resize(n);
	}
	void push(int x, int y){
		for(int i = x; i < n; i |= i + 1) tree[i] += y;
	}
	int get(int x){
		int rv = 0;
		for(int i = x; i >= 0; i = (i & (i + 1)) - 1){
			rv += tree[i];
		}
		return rv;
	}
};

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cin >> n;
	vector<int> srt;
	for(int i = 0; i < n; ++i){
		cin >> c[i];
		srt.push_back(c[i]);
	}
	sort(srt.begin(), srt.end());
	srt.erase(unique(srt.begin(), srt.end()), srt.end());
	for(int i = 0; i < n; ++i){
		c[i] = lower_bound(srt.begin(), srt.end(), c[i]) - srt.begin();
	}
	for(int i = 0; i < n - 1; ++i){
		int x, y; cin >> x >> y; --x, --y;
		que[i] = {x, y};
		way[x].push_back(y), way[y].push_back(x);
	}
	dfs_sz(0, -1);
	dfs_hld(0, -1);
	Seg seg(n + 4);
	seg.push(1, 0, n - 1, 0, 0, c[0]);
	Fenwick sum(n + 4);
	for(int i = 0; i < n - 1; ++i){
		int x = que[i].first;
		while(x != -1){
			seg.get(1, 0, n - 1, in[up[x]], in[x]);
			x = pr[up[x]];
		}
		vector<pair<int, int>> col;
		for(auto&i:block){
			if((int)col.size() && col.back().first == i.first){
				col.back().second += i.second;
			}
			else{
				col.push_back({i.first, i.second});
			}
		}
		block.clear();
		long long ans = 0;
		for(auto&i:col){
			ans += (long long)sum.get(i.first - 1) * i.second;
			sum.push(i.first, i.second);
		}
		for(auto&i:col){
			sum.push(i.first, -i.second);
		}
		cout << ans << '\n';
		x = que[i].second;
		while(x != -1){
			seg.push(1, 0, n - 1, in[up[x]], in[x], c[que[i].second]);
			x = pr[up[x]];
		}
	}
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2668 KB Output is correct
2 Correct 2 ms 2668 KB Output is correct
3 Correct 2 ms 2668 KB Output is correct
4 Correct 2 ms 2796 KB Output is correct
5 Correct 3 ms 2796 KB Output is correct
6 Correct 3 ms 2796 KB Output is correct
7 Correct 3 ms 2796 KB Output is correct
8 Correct 2 ms 2796 KB Output is correct
9 Correct 3 ms 2796 KB Output is correct
10 Correct 3 ms 2796 KB Output is correct
11 Correct 3 ms 2796 KB Output is correct
12 Correct 3 ms 2796 KB Output is correct
13 Correct 3 ms 2796 KB Output is correct
14 Correct 3 ms 2796 KB Output is correct
15 Correct 3 ms 2796 KB Output is correct
16 Correct 3 ms 2796 KB Output is correct
17 Correct 3 ms 2796 KB Output is correct
18 Correct 4 ms 2796 KB Output is correct
19 Correct 3 ms 2796 KB Output is correct
20 Correct 3 ms 2796 KB Output is correct
21 Correct 3 ms 2796 KB Output is correct
22 Correct 3 ms 2796 KB Output is correct
23 Correct 3 ms 2796 KB Output is correct
24 Correct 3 ms 2796 KB Output is correct
25 Correct 3 ms 2796 KB Output is correct
26 Correct 3 ms 2796 KB Output is correct
27 Correct 3 ms 2944 KB Output is correct
28 Correct 3 ms 2796 KB Output is correct
29 Correct 3 ms 2796 KB Output is correct
30 Correct 3 ms 2796 KB Output is correct
31 Correct 3 ms 2796 KB Output is correct
32 Correct 3 ms 2796 KB Output is correct
33 Correct 3 ms 2796 KB Output is correct
34 Correct 3 ms 2796 KB Output is correct
35 Correct 3 ms 2796 KB Output is correct
36 Correct 4 ms 2796 KB Output is correct
37 Correct 3 ms 2796 KB Output is correct
38 Correct 3 ms 2796 KB Output is correct
39 Correct 3 ms 2796 KB Output is correct
40 Correct 3 ms 2796 KB Output is correct
41 Correct 3 ms 2796 KB Output is correct
42 Correct 3 ms 2796 KB Output is correct
43 Correct 3 ms 2796 KB Output is correct
44 Correct 3 ms 2796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2668 KB Output is correct
2 Correct 2 ms 2668 KB Output is correct
3 Correct 2 ms 2668 KB Output is correct
4 Correct 2 ms 2796 KB Output is correct
5 Correct 3 ms 2796 KB Output is correct
6 Correct 3 ms 2796 KB Output is correct
7 Correct 3 ms 2796 KB Output is correct
8 Correct 2 ms 2796 KB Output is correct
9 Correct 3 ms 2796 KB Output is correct
10 Correct 3 ms 2796 KB Output is correct
11 Correct 3 ms 2796 KB Output is correct
12 Correct 3 ms 2796 KB Output is correct
13 Correct 3 ms 2796 KB Output is correct
14 Correct 3 ms 2796 KB Output is correct
15 Correct 3 ms 2796 KB Output is correct
16 Correct 3 ms 2796 KB Output is correct
17 Correct 3 ms 2796 KB Output is correct
18 Correct 4 ms 2796 KB Output is correct
19 Correct 3 ms 2796 KB Output is correct
20 Correct 3 ms 2796 KB Output is correct
21 Correct 3 ms 2796 KB Output is correct
22 Correct 3 ms 2796 KB Output is correct
23 Correct 3 ms 2796 KB Output is correct
24 Correct 3 ms 2796 KB Output is correct
25 Correct 3 ms 2796 KB Output is correct
26 Correct 3 ms 2796 KB Output is correct
27 Correct 3 ms 2944 KB Output is correct
28 Correct 3 ms 2796 KB Output is correct
29 Correct 3 ms 2796 KB Output is correct
30 Correct 3 ms 2796 KB Output is correct
31 Correct 3 ms 2796 KB Output is correct
32 Correct 3 ms 2796 KB Output is correct
33 Correct 3 ms 2796 KB Output is correct
34 Correct 3 ms 2796 KB Output is correct
35 Correct 3 ms 2796 KB Output is correct
36 Correct 4 ms 2796 KB Output is correct
37 Correct 3 ms 2796 KB Output is correct
38 Correct 3 ms 2796 KB Output is correct
39 Correct 3 ms 2796 KB Output is correct
40 Correct 3 ms 2796 KB Output is correct
41 Correct 3 ms 2796 KB Output is correct
42 Correct 3 ms 2796 KB Output is correct
43 Correct 3 ms 2796 KB Output is correct
44 Correct 3 ms 2796 KB Output is correct
45 Correct 5 ms 2796 KB Output is correct
46 Correct 11 ms 3180 KB Output is correct
47 Correct 12 ms 3308 KB Output is correct
48 Correct 12 ms 3180 KB Output is correct
49 Correct 7 ms 3436 KB Output is correct
50 Correct 7 ms 3436 KB Output is correct
51 Correct 7 ms 3436 KB Output is correct
52 Correct 7 ms 3308 KB Output is correct
53 Correct 7 ms 3436 KB Output is correct
54 Correct 7 ms 3436 KB Output is correct
55 Correct 7 ms 3436 KB Output is correct
56 Correct 8 ms 3308 KB Output is correct
57 Correct 14 ms 3180 KB Output is correct
58 Correct 15 ms 3180 KB Output is correct
59 Correct 15 ms 3200 KB Output is correct
60 Correct 15 ms 3180 KB Output is correct
61 Correct 8 ms 3308 KB Output is correct
62 Correct 8 ms 3308 KB Output is correct
63 Correct 8 ms 3436 KB Output is correct
64 Correct 10 ms 3180 KB Output is correct
65 Correct 11 ms 3180 KB Output is correct
66 Correct 11 ms 3180 KB Output is correct
67 Correct 12 ms 3180 KB Output is correct
68 Correct 6 ms 3436 KB Output is correct
69 Correct 7 ms 3308 KB Output is correct
70 Correct 6 ms 3308 KB Output is correct
71 Correct 7 ms 3308 KB Output is correct
72 Correct 14 ms 3180 KB Output is correct
73 Correct 14 ms 3180 KB Output is correct
74 Correct 7 ms 3308 KB Output is correct
75 Correct 10 ms 3308 KB Output is correct
76 Correct 10 ms 3180 KB Output is correct
77 Correct 9 ms 3180 KB Output is correct
78 Correct 8 ms 3180 KB Output is correct
79 Correct 8 ms 3180 KB Output is correct
80 Correct 8 ms 3180 KB Output is correct
81 Correct 9 ms 3308 KB Output is correct
82 Correct 9 ms 3180 KB Output is correct
83 Correct 10 ms 3180 KB Output is correct
84 Correct 8 ms 3180 KB Output is correct
85 Correct 8 ms 3180 KB Output is correct
86 Correct 9 ms 3180 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2668 KB Output is correct
2 Correct 2 ms 2668 KB Output is correct
3 Correct 2 ms 2668 KB Output is correct
4 Correct 2 ms 2796 KB Output is correct
5 Correct 3 ms 2796 KB Output is correct
6 Correct 3 ms 2796 KB Output is correct
7 Correct 3 ms 2796 KB Output is correct
8 Correct 2 ms 2796 KB Output is correct
9 Correct 3 ms 2796 KB Output is correct
10 Correct 3 ms 2796 KB Output is correct
11 Correct 3 ms 2796 KB Output is correct
12 Correct 3 ms 2796 KB Output is correct
13 Correct 3 ms 2796 KB Output is correct
14 Correct 3 ms 2796 KB Output is correct
15 Correct 3 ms 2796 KB Output is correct
16 Correct 3 ms 2796 KB Output is correct
17 Correct 3 ms 2796 KB Output is correct
18 Correct 4 ms 2796 KB Output is correct
19 Correct 3 ms 2796 KB Output is correct
20 Correct 3 ms 2796 KB Output is correct
21 Correct 3 ms 2796 KB Output is correct
22 Correct 3 ms 2796 KB Output is correct
23 Correct 3 ms 2796 KB Output is correct
24 Correct 3 ms 2796 KB Output is correct
25 Correct 3 ms 2796 KB Output is correct
26 Correct 3 ms 2796 KB Output is correct
27 Correct 3 ms 2944 KB Output is correct
28 Correct 3 ms 2796 KB Output is correct
29 Correct 3 ms 2796 KB Output is correct
30 Correct 3 ms 2796 KB Output is correct
31 Correct 3 ms 2796 KB Output is correct
32 Correct 3 ms 2796 KB Output is correct
33 Correct 3 ms 2796 KB Output is correct
34 Correct 3 ms 2796 KB Output is correct
35 Correct 3 ms 2796 KB Output is correct
36 Correct 4 ms 2796 KB Output is correct
37 Correct 3 ms 2796 KB Output is correct
38 Correct 3 ms 2796 KB Output is correct
39 Correct 3 ms 2796 KB Output is correct
40 Correct 3 ms 2796 KB Output is correct
41 Correct 3 ms 2796 KB Output is correct
42 Correct 3 ms 2796 KB Output is correct
43 Correct 3 ms 2796 KB Output is correct
44 Correct 3 ms 2796 KB Output is correct
45 Correct 5 ms 2796 KB Output is correct
46 Correct 11 ms 3180 KB Output is correct
47 Correct 12 ms 3308 KB Output is correct
48 Correct 12 ms 3180 KB Output is correct
49 Correct 7 ms 3436 KB Output is correct
50 Correct 7 ms 3436 KB Output is correct
51 Correct 7 ms 3436 KB Output is correct
52 Correct 7 ms 3308 KB Output is correct
53 Correct 7 ms 3436 KB Output is correct
54 Correct 7 ms 3436 KB Output is correct
55 Correct 7 ms 3436 KB Output is correct
56 Correct 8 ms 3308 KB Output is correct
57 Correct 14 ms 3180 KB Output is correct
58 Correct 15 ms 3180 KB Output is correct
59 Correct 15 ms 3200 KB Output is correct
60 Correct 15 ms 3180 KB Output is correct
61 Correct 8 ms 3308 KB Output is correct
62 Correct 8 ms 3308 KB Output is correct
63 Correct 8 ms 3436 KB Output is correct
64 Correct 10 ms 3180 KB Output is correct
65 Correct 11 ms 3180 KB Output is correct
66 Correct 11 ms 3180 KB Output is correct
67 Correct 12 ms 3180 KB Output is correct
68 Correct 6 ms 3436 KB Output is correct
69 Correct 7 ms 3308 KB Output is correct
70 Correct 6 ms 3308 KB Output is correct
71 Correct 7 ms 3308 KB Output is correct
72 Correct 14 ms 3180 KB Output is correct
73 Correct 14 ms 3180 KB Output is correct
74 Correct 7 ms 3308 KB Output is correct
75 Correct 10 ms 3308 KB Output is correct
76 Correct 10 ms 3180 KB Output is correct
77 Correct 9 ms 3180 KB Output is correct
78 Correct 8 ms 3180 KB Output is correct
79 Correct 8 ms 3180 KB Output is correct
80 Correct 8 ms 3180 KB Output is correct
81 Correct 9 ms 3308 KB Output is correct
82 Correct 9 ms 3180 KB Output is correct
83 Correct 10 ms 3180 KB Output is correct
84 Correct 8 ms 3180 KB Output is correct
85 Correct 8 ms 3180 KB Output is correct
86 Correct 9 ms 3180 KB Output is correct
87 Correct 31 ms 3948 KB Output is correct
88 Correct 111 ms 6508 KB Output is correct
89 Correct 496 ms 14956 KB Output is correct
90 Correct 467 ms 15084 KB Output is correct
91 Correct 465 ms 14956 KB Output is correct
92 Correct 166 ms 22636 KB Output is correct
93 Correct 159 ms 22636 KB Output is correct
94 Correct 161 ms 22636 KB Output is correct
95 Correct 186 ms 19696 KB Output is correct
96 Correct 179 ms 19952 KB Output is correct
97 Correct 173 ms 19824 KB Output is correct
98 Correct 184 ms 19824 KB Output is correct
99 Correct 193 ms 19052 KB Output is correct
100 Correct 672 ms 14700 KB Output is correct
101 Correct 729 ms 14956 KB Output is correct
102 Correct 795 ms 15084 KB Output is correct
103 Correct 773 ms 14956 KB Output is correct
104 Correct 208 ms 18796 KB Output is correct
105 Correct 218 ms 18924 KB Output is correct
106 Correct 252 ms 18796 KB Output is correct
107 Correct 417 ms 14188 KB Output is correct
108 Correct 448 ms 14368 KB Output is correct
109 Correct 525 ms 14632 KB Output is correct
110 Correct 158 ms 22044 KB Output is correct
111 Correct 193 ms 19440 KB Output is correct
112 Correct 187 ms 19312 KB Output is correct
113 Correct 176 ms 18284 KB Output is correct
114 Correct 707 ms 14828 KB Output is correct
115 Correct 699 ms 14444 KB Output is correct
116 Correct 196 ms 18284 KB Output is correct
117 Correct 310 ms 16620 KB Output is correct
118 Correct 228 ms 15932 KB Output is correct
119 Correct 257 ms 15596 KB Output is correct
120 Correct 255 ms 16364 KB Output is correct
121 Correct 217 ms 15596 KB Output is correct
122 Correct 237 ms 15212 KB Output is correct
123 Correct 284 ms 16620 KB Output is correct
124 Correct 271 ms 15980 KB Output is correct
125 Correct 303 ms 15736 KB Output is correct
126 Correct 285 ms 16364 KB Output is correct
127 Correct 264 ms 15724 KB Output is correct
128 Correct 270 ms 15296 KB Output is correct