Submission #44385

# Submission time Handle Problem Language Result Execution time Memory
44385 2018-04-01T11:11:12 Z Bruteforceman Construction of Highway (JOI18_construction) C++11
16 / 100
1000 ms 29980 KB
#include "bits/stdc++.h"
using namespace std;
typedef pair <int, int> pii;
int l[100010], r[100010];
int c[100010];
int n;
vector <int> g[100010];
int sub[100010];
int big[100010];
int head[100010];
int pos[100010];

void dfs(int x) {
	big[x] = -1;
	sub[x] = 1;
	for(auto i : g[x]) {
		dfs(i);
		sub[x] += sub[i];
		if(big[x] == -1 || sub[big[x]] < sub[i]) {
			big[x] = i;
		}
	}
}
int mx[100010 * 4];
int mn[100010 * 4];
int prop[100010 * 4];
int par[100010];
int dp[20][100010];
const int logn = 19;

void pushdown(int c) {
	if(prop[c] == -1) return ;
	int l = c << 1;
	int r = l + 1;
	prop[l] = prop[r] = prop[c];
	mx[l] = mx[r] = prop[c];
	mn[l] = mn[r] = prop[c];
	prop[c] = -1;
}

void update(int x, int y, int val, int c = 1, int b = 1, int e = n) {
	if(x <= b && e <= y) {
		mn[c] = mx[c] = val;
		prop[c] = val;
		return ;
	}
	if(y < b || e < x) return ;
	pushdown(c);

	int m = (b + e) >> 1;
	int l = c << 1;
	int r = l + 1;
	update(x, y, val, l, b, m);
	update(x, y, val, r, m+1, e);
	mn[c] = min(mn[l], mn[r]);
	mx[c] = max(mx[l], mx[r]);
}

int query(int x, int val, int c = 1, int b = 1, int e = n) {
	if(mx[c] == mn[c]) {
		return mx[c] == val ? x - b + 1 : 0;
	}
	if(b == e) return mx[c] == val;
	pushdown(c);
	int m = (b + e) >> 1;
	int l = c << 1;
	int r = l + 1;
	if(x <= m) return query(x, val, l, b, m);
	else {
		int len = query(x, val, r, m + 1, e);
		if(x - m == len) {
			return query(m, val, l, b, m) + len;
		} else {
			return len;
		}
	}
} 
int get(int x, int c = 1, int b = 1, int e = n) {
	if(b == e) return mx[c];
	pushdown(c);
	int m = (b + e) >> 1;
	int l = c << 1;
	int r = l + 1;
	if(x <= m) return get(x, l, b, m);
	else return get(x, r, m+1, e); 
}

void precalc() {
	memset(prop, -1, sizeof prop);
	int cur = 0;
	for(int i = 1; i <= n; i++) {
		if(par[i] == -1 || big[par[i]] != i) {
			for(int j = i; j != -1; j = big[j]) {
				pos[j] = ++cur;
				head[j] = i;
			}
		}
	}
	for(int i = 1; i <= n; i++) {
		update(pos[i], pos[i], c[i]);
	}
	dp[0][1] = 0;
	for(int i = 2; i <= n; i++) dp[0][i] = par[i];
	for(int i = 1; i <= logn; i++) {
		for(int j = 1; j <= n; j++) {
			dp[i][j] = dp[i - 1][dp[i - 1][j]];
		}
	}
}
void change(int x) {
	int cur = x;
	while(cur != -1) {
		update(pos[head[cur]], pos[cur], c[x]);
		cur = head[cur];
		cur = par[cur];
	}
}

int lift(int node, int up) {
	for(int i = logn; i >= 0; i--) {
		if((1 << i) <= up) {
			node = dp[i][node];
			up -= 1 << i;
		}
	}
	return node;
}

long long inverse(vector <pii> v) {
	long long ans = 0;
	for(int i = 0; i < v.size(); i++) {
		for(int j = i + 1; j < v.size(); j++) {
			if(v[i].first < v[j].first) ans += 1LL * v[i].second * v[j].second;
		}
	}
	return ans;
}
long long getCost(int x) {
	vector <pii> v;
	int cur = par[x];
	while(cur > 0) {
		int val = get(pos[cur]);
		int len = min(query(pos[cur], val), pos[cur] - pos[head[cur]] + 1);
		v.push_back(pii(val, len));
		cur = lift(cur, len);
	} 
	return inverse(v);
} 
int main(int argc, char const *argv[])
{
	scanf("%d", &n);
	for(int i = 1; i <= n; i++) {
		scanf("%d", &c[i]);
	}
	par[1] = -1;
	for(int i = 1; i < n; i++) {
		scanf("%d %d", &l[i], &r[i]);
		g[l[i]].push_back(r[i]);
		par[r[i]] = l[i];
	}
	dfs(1);
	precalc();
	for(int i = 1; i < n; i++) {
		printf("%lld\n", getCost(r[i]));
		change(r[i]);
	}
	return 0;	
}

Compilation message

construction.cpp: In function 'long long int inverse(std::vector<std::pair<int, int> >)':
construction.cpp:131:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i < v.size(); i++) {
                 ~~^~~~~~~~~~
construction.cpp:132:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int j = i + 1; j < v.size(); j++) {
                      ~~^~~~~~~~~~
construction.cpp: In function 'int main(int, const char**)':
construction.cpp:151:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
construction.cpp:153:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &c[i]);
   ~~~~~^~~~~~~~~~~~~
construction.cpp:157:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &l[i], &r[i]);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 4344 KB Output is correct
2 Correct 5 ms 4584 KB Output is correct
3 Correct 5 ms 4584 KB Output is correct
4 Correct 5 ms 4584 KB Output is correct
5 Correct 6 ms 4584 KB Output is correct
6 Correct 6 ms 4680 KB Output is correct
7 Correct 6 ms 4680 KB Output is correct
8 Correct 6 ms 4680 KB Output is correct
9 Correct 6 ms 4680 KB Output is correct
10 Correct 6 ms 4680 KB Output is correct
11 Correct 6 ms 4680 KB Output is correct
12 Correct 6 ms 4680 KB Output is correct
13 Correct 8 ms 4680 KB Output is correct
14 Correct 6 ms 4680 KB Output is correct
15 Correct 7 ms 4680 KB Output is correct
16 Correct 6 ms 4680 KB Output is correct
17 Correct 6 ms 4680 KB Output is correct
18 Correct 6 ms 4680 KB Output is correct
19 Correct 6 ms 4680 KB Output is correct
20 Correct 7 ms 4680 KB Output is correct
21 Correct 7 ms 4680 KB Output is correct
22 Correct 6 ms 4680 KB Output is correct
23 Correct 6 ms 4680 KB Output is correct
24 Correct 8 ms 4680 KB Output is correct
25 Correct 6 ms 4680 KB Output is correct
26 Correct 6 ms 4820 KB Output is correct
27 Correct 5 ms 4820 KB Output is correct
28 Correct 7 ms 4820 KB Output is correct
29 Correct 8 ms 4820 KB Output is correct
30 Correct 9 ms 4820 KB Output is correct
31 Correct 7 ms 4820 KB Output is correct
32 Correct 5 ms 4820 KB Output is correct
33 Correct 6 ms 4820 KB Output is correct
34 Correct 6 ms 4820 KB Output is correct
35 Correct 6 ms 4820 KB Output is correct
36 Correct 7 ms 4820 KB Output is correct
37 Correct 7 ms 4820 KB Output is correct
38 Correct 6 ms 4820 KB Output is correct
39 Correct 6 ms 4820 KB Output is correct
40 Correct 7 ms 4820 KB Output is correct
41 Correct 7 ms 4820 KB Output is correct
42 Correct 6 ms 4820 KB Output is correct
43 Correct 6 ms 4820 KB Output is correct
44 Correct 8 ms 4820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 4344 KB Output is correct
2 Correct 5 ms 4584 KB Output is correct
3 Correct 5 ms 4584 KB Output is correct
4 Correct 5 ms 4584 KB Output is correct
5 Correct 6 ms 4584 KB Output is correct
6 Correct 6 ms 4680 KB Output is correct
7 Correct 6 ms 4680 KB Output is correct
8 Correct 6 ms 4680 KB Output is correct
9 Correct 6 ms 4680 KB Output is correct
10 Correct 6 ms 4680 KB Output is correct
11 Correct 6 ms 4680 KB Output is correct
12 Correct 6 ms 4680 KB Output is correct
13 Correct 8 ms 4680 KB Output is correct
14 Correct 6 ms 4680 KB Output is correct
15 Correct 7 ms 4680 KB Output is correct
16 Correct 6 ms 4680 KB Output is correct
17 Correct 6 ms 4680 KB Output is correct
18 Correct 6 ms 4680 KB Output is correct
19 Correct 6 ms 4680 KB Output is correct
20 Correct 7 ms 4680 KB Output is correct
21 Correct 7 ms 4680 KB Output is correct
22 Correct 6 ms 4680 KB Output is correct
23 Correct 6 ms 4680 KB Output is correct
24 Correct 8 ms 4680 KB Output is correct
25 Correct 6 ms 4680 KB Output is correct
26 Correct 6 ms 4820 KB Output is correct
27 Correct 5 ms 4820 KB Output is correct
28 Correct 7 ms 4820 KB Output is correct
29 Correct 8 ms 4820 KB Output is correct
30 Correct 9 ms 4820 KB Output is correct
31 Correct 7 ms 4820 KB Output is correct
32 Correct 5 ms 4820 KB Output is correct
33 Correct 6 ms 4820 KB Output is correct
34 Correct 6 ms 4820 KB Output is correct
35 Correct 6 ms 4820 KB Output is correct
36 Correct 7 ms 4820 KB Output is correct
37 Correct 7 ms 4820 KB Output is correct
38 Correct 6 ms 4820 KB Output is correct
39 Correct 6 ms 4820 KB Output is correct
40 Correct 7 ms 4820 KB Output is correct
41 Correct 7 ms 4820 KB Output is correct
42 Correct 6 ms 4820 KB Output is correct
43 Correct 6 ms 4820 KB Output is correct
44 Correct 8 ms 4820 KB Output is correct
45 Correct 8 ms 4820 KB Output is correct
46 Correct 17 ms 5220 KB Output is correct
47 Correct 21 ms 5220 KB Output is correct
48 Correct 19 ms 5236 KB Output is correct
49 Correct 13 ms 5492 KB Output is correct
50 Correct 11 ms 5508 KB Output is correct
51 Correct 16 ms 5508 KB Output is correct
52 Correct 16 ms 5508 KB Output is correct
53 Correct 20 ms 5508 KB Output is correct
54 Correct 18 ms 5508 KB Output is correct
55 Correct 19 ms 5508 KB Output is correct
56 Correct 12 ms 5508 KB Output is correct
57 Correct 31 ms 5508 KB Output is correct
58 Correct 36 ms 5508 KB Output is correct
59 Correct 24 ms 5508 KB Output is correct
60 Correct 25 ms 5508 KB Output is correct
61 Correct 12 ms 5508 KB Output is correct
62 Correct 13 ms 5508 KB Output is correct
63 Correct 16 ms 5508 KB Output is correct
64 Correct 17 ms 5508 KB Output is correct
65 Correct 18 ms 5508 KB Output is correct
66 Correct 16 ms 5508 KB Output is correct
67 Correct 18 ms 5508 KB Output is correct
68 Correct 10 ms 5564 KB Output is correct
69 Correct 13 ms 5564 KB Output is correct
70 Correct 19 ms 5564 KB Output is correct
71 Correct 13 ms 5564 KB Output is correct
72 Correct 25 ms 5564 KB Output is correct
73 Correct 34 ms 5564 KB Output is correct
74 Correct 12 ms 5564 KB Output is correct
75 Correct 12 ms 5564 KB Output is correct
76 Correct 12 ms 5564 KB Output is correct
77 Correct 13 ms 5564 KB Output is correct
78 Correct 16 ms 5564 KB Output is correct
79 Correct 13 ms 5564 KB Output is correct
80 Correct 13 ms 5564 KB Output is correct
81 Correct 17 ms 5564 KB Output is correct
82 Correct 14 ms 5564 KB Output is correct
83 Correct 13 ms 5564 KB Output is correct
84 Correct 14 ms 5564 KB Output is correct
85 Correct 15 ms 5564 KB Output is correct
86 Correct 14 ms 5564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 4344 KB Output is correct
2 Correct 5 ms 4584 KB Output is correct
3 Correct 5 ms 4584 KB Output is correct
4 Correct 5 ms 4584 KB Output is correct
5 Correct 6 ms 4584 KB Output is correct
6 Correct 6 ms 4680 KB Output is correct
7 Correct 6 ms 4680 KB Output is correct
8 Correct 6 ms 4680 KB Output is correct
9 Correct 6 ms 4680 KB Output is correct
10 Correct 6 ms 4680 KB Output is correct
11 Correct 6 ms 4680 KB Output is correct
12 Correct 6 ms 4680 KB Output is correct
13 Correct 8 ms 4680 KB Output is correct
14 Correct 6 ms 4680 KB Output is correct
15 Correct 7 ms 4680 KB Output is correct
16 Correct 6 ms 4680 KB Output is correct
17 Correct 6 ms 4680 KB Output is correct
18 Correct 6 ms 4680 KB Output is correct
19 Correct 6 ms 4680 KB Output is correct
20 Correct 7 ms 4680 KB Output is correct
21 Correct 7 ms 4680 KB Output is correct
22 Correct 6 ms 4680 KB Output is correct
23 Correct 6 ms 4680 KB Output is correct
24 Correct 8 ms 4680 KB Output is correct
25 Correct 6 ms 4680 KB Output is correct
26 Correct 6 ms 4820 KB Output is correct
27 Correct 5 ms 4820 KB Output is correct
28 Correct 7 ms 4820 KB Output is correct
29 Correct 8 ms 4820 KB Output is correct
30 Correct 9 ms 4820 KB Output is correct
31 Correct 7 ms 4820 KB Output is correct
32 Correct 5 ms 4820 KB Output is correct
33 Correct 6 ms 4820 KB Output is correct
34 Correct 6 ms 4820 KB Output is correct
35 Correct 6 ms 4820 KB Output is correct
36 Correct 7 ms 4820 KB Output is correct
37 Correct 7 ms 4820 KB Output is correct
38 Correct 6 ms 4820 KB Output is correct
39 Correct 6 ms 4820 KB Output is correct
40 Correct 7 ms 4820 KB Output is correct
41 Correct 7 ms 4820 KB Output is correct
42 Correct 6 ms 4820 KB Output is correct
43 Correct 6 ms 4820 KB Output is correct
44 Correct 8 ms 4820 KB Output is correct
45 Correct 8 ms 4820 KB Output is correct
46 Correct 17 ms 5220 KB Output is correct
47 Correct 21 ms 5220 KB Output is correct
48 Correct 19 ms 5236 KB Output is correct
49 Correct 13 ms 5492 KB Output is correct
50 Correct 11 ms 5508 KB Output is correct
51 Correct 16 ms 5508 KB Output is correct
52 Correct 16 ms 5508 KB Output is correct
53 Correct 20 ms 5508 KB Output is correct
54 Correct 18 ms 5508 KB Output is correct
55 Correct 19 ms 5508 KB Output is correct
56 Correct 12 ms 5508 KB Output is correct
57 Correct 31 ms 5508 KB Output is correct
58 Correct 36 ms 5508 KB Output is correct
59 Correct 24 ms 5508 KB Output is correct
60 Correct 25 ms 5508 KB Output is correct
61 Correct 12 ms 5508 KB Output is correct
62 Correct 13 ms 5508 KB Output is correct
63 Correct 16 ms 5508 KB Output is correct
64 Correct 17 ms 5508 KB Output is correct
65 Correct 18 ms 5508 KB Output is correct
66 Correct 16 ms 5508 KB Output is correct
67 Correct 18 ms 5508 KB Output is correct
68 Correct 10 ms 5564 KB Output is correct
69 Correct 13 ms 5564 KB Output is correct
70 Correct 19 ms 5564 KB Output is correct
71 Correct 13 ms 5564 KB Output is correct
72 Correct 25 ms 5564 KB Output is correct
73 Correct 34 ms 5564 KB Output is correct
74 Correct 12 ms 5564 KB Output is correct
75 Correct 12 ms 5564 KB Output is correct
76 Correct 12 ms 5564 KB Output is correct
77 Correct 13 ms 5564 KB Output is correct
78 Correct 16 ms 5564 KB Output is correct
79 Correct 13 ms 5564 KB Output is correct
80 Correct 13 ms 5564 KB Output is correct
81 Correct 17 ms 5564 KB Output is correct
82 Correct 14 ms 5564 KB Output is correct
83 Correct 13 ms 5564 KB Output is correct
84 Correct 14 ms 5564 KB Output is correct
85 Correct 15 ms 5564 KB Output is correct
86 Correct 14 ms 5564 KB Output is correct
87 Correct 45 ms 6204 KB Output is correct
88 Correct 145 ms 9280 KB Output is correct
89 Correct 740 ms 19588 KB Output is correct
90 Correct 727 ms 19688 KB Output is correct
91 Correct 759 ms 19688 KB Output is correct
92 Correct 225 ms 25576 KB Output is correct
93 Correct 295 ms 27708 KB Output is correct
94 Correct 247 ms 29980 KB Output is correct
95 Execution timed out 1084 ms 29980 KB Time limit exceeded
96 Halted 0 ms 0 KB -