Submission #119920

# Submission time Handle Problem Language Result Execution time Memory
119920 2019-06-22T15:44:48 Z arman_ferdous Construction of Highway (JOI18_construction) C++14
100 / 100
589 ms 95340 KB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
const int N = 1e5+100;
int n, c[N];
vector<int> g[N];
struct edge{
	int u, v;
}e[N];

int par[N], sub[N], lev[N];
int subdfs(int u, int f, int l) {
	par[u] = f;
	sub[u] = 1;
	lev[u] = l;
	for(int v : g[u]) if(v != f) 
		sub[u] += subdfs(v, u, l + 1);
	return sub[u];
}

int head[N], cid[N], zap;
deque< pair<int,ll> > val[N];
void hld(int u, int f, int dad, int id) {
	head[u] = dad;
	cid[u] = id;

	if(!val[id].empty() && val[id].back().first == c[u])
		val[id].back().second++;
	else val[id].push_back({c[u], 1});

	int mx = -1, sc;
	for(int v : g[u]) if(v != f && sub[v] > mx)
		mx = sub[v], sc = v;
	if(mx == -1) return;
	hld(sc, u, dad, id);

	for(int v : g[u]) if(v != f && v != sc)
		hld(v, u, v, ++zap);
}

int bit[N];
deque< pair<int,ll> > tmp, full;

void upd(int pos, int x) {
	while(pos < N) bit[pos] += x, pos += pos&-pos;
}
ll get(int pos, int sum = 0) {
	while(pos > 0) sum += bit[pos], pos -= pos&-pos;
	return sum;
}

ll calc(int u, int newc) {
	full.clear();
	while(u != -1) {
		tmp.clear();
		int take = lev[u] - lev[head[u]] + 1, id = cid[u];
		while(take) {
			if(take - val[id][0].second <= 0) {
				tmp.push_front({val[id][0].first, take});
				val[id][0].second -= take;
				if(val[id][0].second == 0) val[id].pop_front();
				break;
			}
			else {
				tmp.push_front(val[id][0]);
				take -= val[id][0].second;
				val[id].pop_front();
			}
		}
		if(!val[id].empty() && val[id][0].first == newc) 
			val[id][0].second += lev[u] - lev[head[u]] + 1;
		else val[id].push_front({newc, lev[u] - lev[head[u]] + 1});
		u = par[head[u]];
		while(!tmp.empty()) {
			full.push_front(tmp.front());
			tmp.pop_front();
		}
	}
	int sz = full.size(); ll ret = 0;
	for(int i = sz - 1; i >= 0; i--) {
		ret += get(full[i].first - 1) * full[i].second;
		upd(full[i].first, full[i].second);
	}
	for(int i = 0; i < sz; i++) 
		upd(full[i].first, -full[i].second);
	return ret;
}

vector<int> v;
map<int,int> mp;
int main() {
	scanf("%d", &n);
	for(int i = 1; i <= n; i++) {
		scanf("%d", &c[i]);
		v.push_back(c[i]);
	} sort(v.begin(), v.end());
	v.erase(unique(v.begin(),v.end()),v.end());
	int cur = 1;
	for(auto it : v) mp[it] = cur++;
	for(int i = 1; i <= n; i++) c[i] = mp[c[i]];

	for(int i = 1; i < n; i++) {
		scanf("%d %d", &e[i].u, &e[i].v);
		g[e[i].u].push_back(e[i].v);
		g[e[i].v].push_back(e[i].u);
	}
	subdfs(1, -1, 1);
	hld(1, -1, 1, ++zap);

	for(int i = 1; i < n; i++)
		printf("%lld\n", calc(e[i].u, c[e[i].v]));
	return 0;
}

Compilation message

construction.cpp: In function 'int main()':
construction.cpp:93:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
construction.cpp:95:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &c[i]);
   ~~~~~^~~~~~~~~~~~~
construction.cpp:104:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &e[i].u, &e[i].v);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 68 ms 70136 KB Output is correct
2 Correct 68 ms 70136 KB Output is correct
3 Correct 68 ms 70264 KB Output is correct
4 Correct 77 ms 70136 KB Output is correct
5 Correct 77 ms 70392 KB Output is correct
6 Correct 65 ms 70268 KB Output is correct
7 Correct 73 ms 70264 KB Output is correct
8 Correct 76 ms 70392 KB Output is correct
9 Correct 67 ms 70264 KB Output is correct
10 Correct 77 ms 70392 KB Output is correct
11 Correct 76 ms 70232 KB Output is correct
12 Correct 65 ms 70264 KB Output is correct
13 Correct 66 ms 70364 KB Output is correct
14 Correct 73 ms 70236 KB Output is correct
15 Correct 68 ms 70136 KB Output is correct
16 Correct 70 ms 70136 KB Output is correct
17 Correct 70 ms 70136 KB Output is correct
18 Correct 75 ms 70136 KB Output is correct
19 Correct 68 ms 70264 KB Output is correct
20 Correct 68 ms 70276 KB Output is correct
21 Correct 69 ms 70264 KB Output is correct
22 Correct 75 ms 70136 KB Output is correct
23 Correct 74 ms 70264 KB Output is correct
24 Correct 67 ms 70136 KB Output is correct
25 Correct 68 ms 70136 KB Output is correct
26 Correct 68 ms 70264 KB Output is correct
27 Correct 68 ms 70264 KB Output is correct
28 Correct 68 ms 70136 KB Output is correct
29 Correct 66 ms 70136 KB Output is correct
30 Correct 68 ms 70136 KB Output is correct
31 Correct 74 ms 70136 KB Output is correct
32 Correct 104 ms 70264 KB Output is correct
33 Correct 68 ms 70136 KB Output is correct
34 Correct 75 ms 70136 KB Output is correct
35 Correct 77 ms 70136 KB Output is correct
36 Correct 72 ms 70108 KB Output is correct
37 Correct 67 ms 70136 KB Output is correct
38 Correct 66 ms 70136 KB Output is correct
39 Correct 70 ms 70288 KB Output is correct
40 Correct 77 ms 70136 KB Output is correct
41 Correct 77 ms 70264 KB Output is correct
42 Correct 67 ms 70136 KB Output is correct
43 Correct 65 ms 70264 KB Output is correct
44 Correct 65 ms 70148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 68 ms 70136 KB Output is correct
2 Correct 68 ms 70136 KB Output is correct
3 Correct 68 ms 70264 KB Output is correct
4 Correct 77 ms 70136 KB Output is correct
5 Correct 77 ms 70392 KB Output is correct
6 Correct 65 ms 70268 KB Output is correct
7 Correct 73 ms 70264 KB Output is correct
8 Correct 76 ms 70392 KB Output is correct
9 Correct 67 ms 70264 KB Output is correct
10 Correct 77 ms 70392 KB Output is correct
11 Correct 76 ms 70232 KB Output is correct
12 Correct 65 ms 70264 KB Output is correct
13 Correct 66 ms 70364 KB Output is correct
14 Correct 73 ms 70236 KB Output is correct
15 Correct 68 ms 70136 KB Output is correct
16 Correct 70 ms 70136 KB Output is correct
17 Correct 70 ms 70136 KB Output is correct
18 Correct 75 ms 70136 KB Output is correct
19 Correct 68 ms 70264 KB Output is correct
20 Correct 68 ms 70276 KB Output is correct
21 Correct 69 ms 70264 KB Output is correct
22 Correct 75 ms 70136 KB Output is correct
23 Correct 74 ms 70264 KB Output is correct
24 Correct 67 ms 70136 KB Output is correct
25 Correct 68 ms 70136 KB Output is correct
26 Correct 68 ms 70264 KB Output is correct
27 Correct 68 ms 70264 KB Output is correct
28 Correct 68 ms 70136 KB Output is correct
29 Correct 66 ms 70136 KB Output is correct
30 Correct 68 ms 70136 KB Output is correct
31 Correct 74 ms 70136 KB Output is correct
32 Correct 104 ms 70264 KB Output is correct
33 Correct 68 ms 70136 KB Output is correct
34 Correct 75 ms 70136 KB Output is correct
35 Correct 77 ms 70136 KB Output is correct
36 Correct 72 ms 70108 KB Output is correct
37 Correct 67 ms 70136 KB Output is correct
38 Correct 66 ms 70136 KB Output is correct
39 Correct 70 ms 70288 KB Output is correct
40 Correct 77 ms 70136 KB Output is correct
41 Correct 77 ms 70264 KB Output is correct
42 Correct 67 ms 70136 KB Output is correct
43 Correct 65 ms 70264 KB Output is correct
44 Correct 65 ms 70148 KB Output is correct
45 Correct 68 ms 70408 KB Output is correct
46 Correct 79 ms 70724 KB Output is correct
47 Correct 71 ms 70648 KB Output is correct
48 Correct 78 ms 70620 KB Output is correct
49 Correct 81 ms 71072 KB Output is correct
50 Correct 78 ms 71160 KB Output is correct
51 Correct 76 ms 71160 KB Output is correct
52 Correct 73 ms 71032 KB Output is correct
53 Correct 74 ms 70896 KB Output is correct
54 Correct 70 ms 70904 KB Output is correct
55 Correct 80 ms 70968 KB Output is correct
56 Correct 73 ms 70904 KB Output is correct
57 Correct 77 ms 70648 KB Output is correct
58 Correct 66 ms 70648 KB Output is correct
59 Correct 75 ms 70648 KB Output is correct
60 Correct 73 ms 70652 KB Output is correct
61 Correct 71 ms 71000 KB Output is correct
62 Correct 73 ms 70904 KB Output is correct
63 Correct 70 ms 70904 KB Output is correct
64 Correct 83 ms 70520 KB Output is correct
65 Correct 84 ms 70648 KB Output is correct
66 Correct 71 ms 70520 KB Output is correct
67 Correct 71 ms 70520 KB Output is correct
68 Correct 82 ms 70904 KB Output is correct
69 Correct 71 ms 70876 KB Output is correct
70 Correct 68 ms 70648 KB Output is correct
71 Correct 78 ms 70648 KB Output is correct
72 Correct 73 ms 70648 KB Output is correct
73 Correct 72 ms 70520 KB Output is correct
74 Correct 80 ms 70648 KB Output is correct
75 Correct 80 ms 70776 KB Output is correct
76 Correct 81 ms 70776 KB Output is correct
77 Correct 72 ms 70776 KB Output is correct
78 Correct 70 ms 70648 KB Output is correct
79 Correct 68 ms 70520 KB Output is correct
80 Correct 72 ms 70500 KB Output is correct
81 Correct 81 ms 70768 KB Output is correct
82 Correct 71 ms 70776 KB Output is correct
83 Correct 73 ms 70776 KB Output is correct
84 Correct 75 ms 70724 KB Output is correct
85 Correct 71 ms 70648 KB Output is correct
86 Correct 98 ms 70520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 68 ms 70136 KB Output is correct
2 Correct 68 ms 70136 KB Output is correct
3 Correct 68 ms 70264 KB Output is correct
4 Correct 77 ms 70136 KB Output is correct
5 Correct 77 ms 70392 KB Output is correct
6 Correct 65 ms 70268 KB Output is correct
7 Correct 73 ms 70264 KB Output is correct
8 Correct 76 ms 70392 KB Output is correct
9 Correct 67 ms 70264 KB Output is correct
10 Correct 77 ms 70392 KB Output is correct
11 Correct 76 ms 70232 KB Output is correct
12 Correct 65 ms 70264 KB Output is correct
13 Correct 66 ms 70364 KB Output is correct
14 Correct 73 ms 70236 KB Output is correct
15 Correct 68 ms 70136 KB Output is correct
16 Correct 70 ms 70136 KB Output is correct
17 Correct 70 ms 70136 KB Output is correct
18 Correct 75 ms 70136 KB Output is correct
19 Correct 68 ms 70264 KB Output is correct
20 Correct 68 ms 70276 KB Output is correct
21 Correct 69 ms 70264 KB Output is correct
22 Correct 75 ms 70136 KB Output is correct
23 Correct 74 ms 70264 KB Output is correct
24 Correct 67 ms 70136 KB Output is correct
25 Correct 68 ms 70136 KB Output is correct
26 Correct 68 ms 70264 KB Output is correct
27 Correct 68 ms 70264 KB Output is correct
28 Correct 68 ms 70136 KB Output is correct
29 Correct 66 ms 70136 KB Output is correct
30 Correct 68 ms 70136 KB Output is correct
31 Correct 74 ms 70136 KB Output is correct
32 Correct 104 ms 70264 KB Output is correct
33 Correct 68 ms 70136 KB Output is correct
34 Correct 75 ms 70136 KB Output is correct
35 Correct 77 ms 70136 KB Output is correct
36 Correct 72 ms 70108 KB Output is correct
37 Correct 67 ms 70136 KB Output is correct
38 Correct 66 ms 70136 KB Output is correct
39 Correct 70 ms 70288 KB Output is correct
40 Correct 77 ms 70136 KB Output is correct
41 Correct 77 ms 70264 KB Output is correct
42 Correct 67 ms 70136 KB Output is correct
43 Correct 65 ms 70264 KB Output is correct
44 Correct 65 ms 70148 KB Output is correct
45 Correct 68 ms 70408 KB Output is correct
46 Correct 79 ms 70724 KB Output is correct
47 Correct 71 ms 70648 KB Output is correct
48 Correct 78 ms 70620 KB Output is correct
49 Correct 81 ms 71072 KB Output is correct
50 Correct 78 ms 71160 KB Output is correct
51 Correct 76 ms 71160 KB Output is correct
52 Correct 73 ms 71032 KB Output is correct
53 Correct 74 ms 70896 KB Output is correct
54 Correct 70 ms 70904 KB Output is correct
55 Correct 80 ms 70968 KB Output is correct
56 Correct 73 ms 70904 KB Output is correct
57 Correct 77 ms 70648 KB Output is correct
58 Correct 66 ms 70648 KB Output is correct
59 Correct 75 ms 70648 KB Output is correct
60 Correct 73 ms 70652 KB Output is correct
61 Correct 71 ms 71000 KB Output is correct
62 Correct 73 ms 70904 KB Output is correct
63 Correct 70 ms 70904 KB Output is correct
64 Correct 83 ms 70520 KB Output is correct
65 Correct 84 ms 70648 KB Output is correct
66 Correct 71 ms 70520 KB Output is correct
67 Correct 71 ms 70520 KB Output is correct
68 Correct 82 ms 70904 KB Output is correct
69 Correct 71 ms 70876 KB Output is correct
70 Correct 68 ms 70648 KB Output is correct
71 Correct 78 ms 70648 KB Output is correct
72 Correct 73 ms 70648 KB Output is correct
73 Correct 72 ms 70520 KB Output is correct
74 Correct 80 ms 70648 KB Output is correct
75 Correct 80 ms 70776 KB Output is correct
76 Correct 81 ms 70776 KB Output is correct
77 Correct 72 ms 70776 KB Output is correct
78 Correct 70 ms 70648 KB Output is correct
79 Correct 68 ms 70520 KB Output is correct
80 Correct 72 ms 70500 KB Output is correct
81 Correct 81 ms 70768 KB Output is correct
82 Correct 71 ms 70776 KB Output is correct
83 Correct 73 ms 70776 KB Output is correct
84 Correct 75 ms 70724 KB Output is correct
85 Correct 71 ms 70648 KB Output is correct
86 Correct 98 ms 70520 KB Output is correct
87 Correct 84 ms 71576 KB Output is correct
88 Correct 133 ms 74400 KB Output is correct
89 Correct 383 ms 84304 KB Output is correct
90 Correct 418 ms 84464 KB Output is correct
91 Correct 410 ms 84336 KB Output is correct
92 Correct 337 ms 95212 KB Output is correct
93 Correct 303 ms 95216 KB Output is correct
94 Correct 322 ms 95340 KB Output is correct
95 Correct 296 ms 89456 KB Output is correct
96 Correct 344 ms 89840 KB Output is correct
97 Correct 305 ms 89716 KB Output is correct
98 Correct 291 ms 89716 KB Output is correct
99 Correct 317 ms 89840 KB Output is correct
100 Correct 519 ms 83960 KB Output is correct
101 Correct 589 ms 84496 KB Output is correct
102 Correct 559 ms 84464 KB Output is correct
103 Correct 569 ms 84292 KB Output is correct
104 Correct 318 ms 89864 KB Output is correct
105 Correct 310 ms 89708 KB Output is correct
106 Correct 294 ms 89860 KB Output is correct
107 Correct 345 ms 79088 KB Output is correct
108 Correct 312 ms 78704 KB Output is correct
109 Correct 326 ms 80304 KB Output is correct
110 Correct 212 ms 89584 KB Output is correct
111 Correct 325 ms 89328 KB Output is correct
112 Correct 217 ms 84080 KB Output is correct
113 Correct 216 ms 84208 KB Output is correct
114 Correct 520 ms 83988 KB Output is correct
115 Correct 483 ms 78780 KB Output is correct
116 Correct 215 ms 84080 KB Output is correct
117 Correct 282 ms 86868 KB Output is correct
118 Correct 278 ms 86100 KB Output is correct
119 Correct 280 ms 85616 KB Output is correct
120 Correct 211 ms 81776 KB Output is correct
121 Correct 226 ms 80940 KB Output is correct
122 Correct 217 ms 80624 KB Output is correct
123 Correct 388 ms 87024 KB Output is correct
124 Correct 313 ms 86088 KB Output is correct
125 Correct 314 ms 85736 KB Output is correct
126 Correct 232 ms 81900 KB Output is correct
127 Correct 213 ms 81136 KB Output is correct
128 Correct 253 ms 80768 KB Output is correct