Submission #110152

# Submission time Handle Problem Language Result Execution time Memory
110152 2019-05-09T15:28:50 Z Saboon Construction of Highway (JOI18_construction) C++14
16 / 100
2000 ms 13828 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const int maxn = 1e5 + 10;

int n, c[maxn], pos[maxn], par[maxn], up[maxn], sz[maxn], st[maxn], Time = 1;
vector<int> t[maxn];
pair<int,int> edge[maxn];
int mxm[4 * maxn], mnm[4 * maxn], lzy[4 * maxn];

void propagate(int, int, int);

pair<int,int> get(int id, int L, int R, int l, int r, int x){
	if (r <= L or R <= l)
		return {-1, -1};
	if (mxm[id] == x and mnm[id] == x)
		return {-1, -1};
	if (L + 1 == R)
		return {L, mxm[id]};
	propagate(id, L, R);
	int mid = (L + R) >> 1;
	auto it = get(2 * id + 1, mid, R, l, r, x);
	if (it.first != -1)
		return it;
	return get(2 * id + 0, L, mid, l, r, x);
}

void change(int id, int L, int R, int l, int r, int x){
	if (L == l and R == r){
		mxm[id] = x;
		mnm[id] = x;
		lzy[id] = x;
		return;
	}
	propagate(id, L, R);
	int mid = (L + R) >> 1;
	if (l < mid)
		change(2 * id + 0, L, mid, l, min(mid, r), x);
	if (mid < r)
		change(2 * id + 1, mid, R, max(l, mid), r, x);
	mxm[id] = max(mxm[2 * id + 0], mxm[2 * id + 1]);
	mnm[id] = min(mnm[2 * id + 0], mnm[2 * id + 1]);
}

void propagate(int id, int L, int R){
	if (lzy[id] == 0)
		return;
	int mid = (L + R) >> 1;
	change(2 * id + 0, L, mid, L, mid, lzy[id]);
	change(2 * id + 1, mid, R, mid, R, lzy[id]);
	lzy[id] = 0;
}

void change(int v){
	int val = c[v];
	while (v != 0){
		int u = up[v];
		change(1, 1, n + 1, st[u], st[v] + 1, val);
		v = par[u];
	}
}

int fen[maxn];

int getfen(int idx){
	int ret = 0;
	for (; idx; idx -= idx & -idx)
		ret += fen[idx];
	return ret;
}

void add(int idx, int x){
	for (; idx <= n; idx += idx & -idx)
		fen[idx] += x;
}

ll get(int v){
	memset(fen, 0, sizeof fen);
	ll sum = 0;
	int cnt = 0;
	int tmp = c[v];
//	cout << "Let's go : " << v << endl;
	while (v != 0){
//		cout << "# " << v << endl;
		int u = up[v];
		auto it = get(1, 1, n + 1, st[u], st[v] + 1, tmp);
//		cout << it.first << " " << it.second << endl;
		if (it.first == -1){
			cnt += st[v] - st[u] + 1;
			v = par[u];
		}
		else{
			cnt += st[v] - it.first;
//			cout << cnt << " -> " << tmp << endl;
			sum += 1ll * cnt * getfen(tmp - 1);
			add(tmp, cnt);
			cnt = 0;
			v = pos[it.first];
			tmp = it.second;
		}
	}
//	cout << cnt << " -> " << tmp << endl;
	sum += 1ll * cnt * getfen(tmp - 1);
	return sum;
}

bool cmp(int fi, int se){
	return sz[fi] > sz[se];
}

void dfs(int v){
	pos[Time] = v;
	st[v] = Time ++;
	sort(t[v].begin(), t[v].end(), cmp);
	bool heavy = 1;
	for (auto u : t[v]){
		up[u] = (heavy == 1 ? up[v] : u);
		heavy = 0;
		dfs(u);
	}
}

int dfssz(int v){
	sz[v] = 1;
	for (auto u : t[v])
		sz[v] += dfssz(u);
	return sz[v];
}

int main(){
	ios_base::sync_with_stdio(false);
	cin >> n;
	vector<int> v;
	for (int i = 1; i <= n; i++){
		cin >> c[i];
		v.push_back(c[i]);
	}
	sort(v.begin(), v.end());
	v.resize(unique(v.begin(), v.end()) - v.begin());
	for (int i = 1; i <= n; i++)
		c[i] = lower_bound(v.begin(), v.end(), c[i]) - v.begin() + 1;
	for (int i = 1; i <= n - 1; i++){
		int v, u;
		cin >> v >> u;
		edge[i] = {v, u};
		par[u] = v;
		t[v].push_back(u);
	}
	dfssz(1);
	up[1] = 1;
	dfs(1);
	for (int v = 1; v <= n; v++)
		change(1, 1, n + 1, st[v], st[v] + 1, c[v]);
	for (int i = 1; i <= n - 1; i++){
		int v = edge[i].first, u = edge[i].second;
		cout << get(v) << '\n';
		change(u);
	}
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 3072 KB Output is correct
2 Correct 4 ms 3200 KB Output is correct
3 Correct 6 ms 3200 KB Output is correct
4 Correct 12 ms 3200 KB Output is correct
5 Correct 16 ms 3200 KB Output is correct
6 Correct 18 ms 3200 KB Output is correct
7 Correct 16 ms 3200 KB Output is correct
8 Correct 14 ms 3200 KB Output is correct
9 Correct 14 ms 3200 KB Output is correct
10 Correct 14 ms 3200 KB Output is correct
11 Correct 14 ms 3200 KB Output is correct
12 Correct 14 ms 3200 KB Output is correct
13 Correct 18 ms 3200 KB Output is correct
14 Correct 17 ms 3200 KB Output is correct
15 Correct 17 ms 3200 KB Output is correct
16 Correct 16 ms 3200 KB Output is correct
17 Correct 15 ms 3200 KB Output is correct
18 Correct 17 ms 3200 KB Output is correct
19 Correct 14 ms 3200 KB Output is correct
20 Correct 14 ms 3200 KB Output is correct
21 Correct 14 ms 3200 KB Output is correct
22 Correct 18 ms 3200 KB Output is correct
23 Correct 15 ms 3200 KB Output is correct
24 Correct 17 ms 3212 KB Output is correct
25 Correct 17 ms 3200 KB Output is correct
26 Correct 17 ms 3200 KB Output is correct
27 Correct 16 ms 3200 KB Output is correct
28 Correct 15 ms 3264 KB Output is correct
29 Correct 14 ms 3200 KB Output is correct
30 Correct 15 ms 3288 KB Output is correct
31 Correct 15 ms 3200 KB Output is correct
32 Correct 17 ms 3328 KB Output is correct
33 Correct 14 ms 3200 KB Output is correct
34 Correct 16 ms 3200 KB Output is correct
35 Correct 17 ms 3200 KB Output is correct
36 Correct 14 ms 3200 KB Output is correct
37 Correct 17 ms 3200 KB Output is correct
38 Correct 15 ms 3200 KB Output is correct
39 Correct 15 ms 3200 KB Output is correct
40 Correct 16 ms 3200 KB Output is correct
41 Correct 15 ms 3200 KB Output is correct
42 Correct 15 ms 3200 KB Output is correct
43 Correct 14 ms 3200 KB Output is correct
44 Correct 15 ms 3200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 3072 KB Output is correct
2 Correct 4 ms 3200 KB Output is correct
3 Correct 6 ms 3200 KB Output is correct
4 Correct 12 ms 3200 KB Output is correct
5 Correct 16 ms 3200 KB Output is correct
6 Correct 18 ms 3200 KB Output is correct
7 Correct 16 ms 3200 KB Output is correct
8 Correct 14 ms 3200 KB Output is correct
9 Correct 14 ms 3200 KB Output is correct
10 Correct 14 ms 3200 KB Output is correct
11 Correct 14 ms 3200 KB Output is correct
12 Correct 14 ms 3200 KB Output is correct
13 Correct 18 ms 3200 KB Output is correct
14 Correct 17 ms 3200 KB Output is correct
15 Correct 17 ms 3200 KB Output is correct
16 Correct 16 ms 3200 KB Output is correct
17 Correct 15 ms 3200 KB Output is correct
18 Correct 17 ms 3200 KB Output is correct
19 Correct 14 ms 3200 KB Output is correct
20 Correct 14 ms 3200 KB Output is correct
21 Correct 14 ms 3200 KB Output is correct
22 Correct 18 ms 3200 KB Output is correct
23 Correct 15 ms 3200 KB Output is correct
24 Correct 17 ms 3212 KB Output is correct
25 Correct 17 ms 3200 KB Output is correct
26 Correct 17 ms 3200 KB Output is correct
27 Correct 16 ms 3200 KB Output is correct
28 Correct 15 ms 3264 KB Output is correct
29 Correct 14 ms 3200 KB Output is correct
30 Correct 15 ms 3288 KB Output is correct
31 Correct 15 ms 3200 KB Output is correct
32 Correct 17 ms 3328 KB Output is correct
33 Correct 14 ms 3200 KB Output is correct
34 Correct 16 ms 3200 KB Output is correct
35 Correct 17 ms 3200 KB Output is correct
36 Correct 14 ms 3200 KB Output is correct
37 Correct 17 ms 3200 KB Output is correct
38 Correct 15 ms 3200 KB Output is correct
39 Correct 15 ms 3200 KB Output is correct
40 Correct 16 ms 3200 KB Output is correct
41 Correct 15 ms 3200 KB Output is correct
42 Correct 15 ms 3200 KB Output is correct
43 Correct 14 ms 3200 KB Output is correct
44 Correct 15 ms 3200 KB Output is correct
45 Correct 25 ms 3200 KB Output is correct
46 Correct 96 ms 3564 KB Output is correct
47 Correct 89 ms 3576 KB Output is correct
48 Correct 85 ms 3584 KB Output is correct
49 Correct 85 ms 3964 KB Output is correct
50 Correct 87 ms 3840 KB Output is correct
51 Correct 91 ms 3840 KB Output is correct
52 Correct 92 ms 3652 KB Output is correct
53 Correct 94 ms 3708 KB Output is correct
54 Correct 87 ms 3684 KB Output is correct
55 Correct 81 ms 3584 KB Output is correct
56 Correct 84 ms 3712 KB Output is correct
57 Correct 101 ms 3548 KB Output is correct
58 Correct 102 ms 3564 KB Output is correct
59 Correct 111 ms 3568 KB Output is correct
60 Correct 95 ms 3456 KB Output is correct
61 Correct 90 ms 3584 KB Output is correct
62 Correct 90 ms 3680 KB Output is correct
63 Correct 92 ms 3584 KB Output is correct
64 Correct 92 ms 3456 KB Output is correct
65 Correct 96 ms 3588 KB Output is correct
66 Correct 102 ms 3576 KB Output is correct
67 Correct 97 ms 3456 KB Output is correct
68 Correct 99 ms 3832 KB Output is correct
69 Correct 92 ms 3556 KB Output is correct
70 Correct 92 ms 3584 KB Output is correct
71 Correct 79 ms 3584 KB Output is correct
72 Correct 97 ms 3456 KB Output is correct
73 Correct 110 ms 3576 KB Output is correct
74 Correct 93 ms 3648 KB Output is correct
75 Correct 90 ms 3600 KB Output is correct
76 Correct 93 ms 3584 KB Output is correct
77 Correct 88 ms 3584 KB Output is correct
78 Correct 85 ms 3584 KB Output is correct
79 Correct 93 ms 3456 KB Output is correct
80 Correct 92 ms 3552 KB Output is correct
81 Correct 87 ms 3584 KB Output is correct
82 Correct 94 ms 3704 KB Output is correct
83 Correct 92 ms 3584 KB Output is correct
84 Correct 90 ms 3704 KB Output is correct
85 Correct 93 ms 3576 KB Output is correct
86 Correct 96 ms 3596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 3072 KB Output is correct
2 Correct 4 ms 3200 KB Output is correct
3 Correct 6 ms 3200 KB Output is correct
4 Correct 12 ms 3200 KB Output is correct
5 Correct 16 ms 3200 KB Output is correct
6 Correct 18 ms 3200 KB Output is correct
7 Correct 16 ms 3200 KB Output is correct
8 Correct 14 ms 3200 KB Output is correct
9 Correct 14 ms 3200 KB Output is correct
10 Correct 14 ms 3200 KB Output is correct
11 Correct 14 ms 3200 KB Output is correct
12 Correct 14 ms 3200 KB Output is correct
13 Correct 18 ms 3200 KB Output is correct
14 Correct 17 ms 3200 KB Output is correct
15 Correct 17 ms 3200 KB Output is correct
16 Correct 16 ms 3200 KB Output is correct
17 Correct 15 ms 3200 KB Output is correct
18 Correct 17 ms 3200 KB Output is correct
19 Correct 14 ms 3200 KB Output is correct
20 Correct 14 ms 3200 KB Output is correct
21 Correct 14 ms 3200 KB Output is correct
22 Correct 18 ms 3200 KB Output is correct
23 Correct 15 ms 3200 KB Output is correct
24 Correct 17 ms 3212 KB Output is correct
25 Correct 17 ms 3200 KB Output is correct
26 Correct 17 ms 3200 KB Output is correct
27 Correct 16 ms 3200 KB Output is correct
28 Correct 15 ms 3264 KB Output is correct
29 Correct 14 ms 3200 KB Output is correct
30 Correct 15 ms 3288 KB Output is correct
31 Correct 15 ms 3200 KB Output is correct
32 Correct 17 ms 3328 KB Output is correct
33 Correct 14 ms 3200 KB Output is correct
34 Correct 16 ms 3200 KB Output is correct
35 Correct 17 ms 3200 KB Output is correct
36 Correct 14 ms 3200 KB Output is correct
37 Correct 17 ms 3200 KB Output is correct
38 Correct 15 ms 3200 KB Output is correct
39 Correct 15 ms 3200 KB Output is correct
40 Correct 16 ms 3200 KB Output is correct
41 Correct 15 ms 3200 KB Output is correct
42 Correct 15 ms 3200 KB Output is correct
43 Correct 14 ms 3200 KB Output is correct
44 Correct 15 ms 3200 KB Output is correct
45 Correct 25 ms 3200 KB Output is correct
46 Correct 96 ms 3564 KB Output is correct
47 Correct 89 ms 3576 KB Output is correct
48 Correct 85 ms 3584 KB Output is correct
49 Correct 85 ms 3964 KB Output is correct
50 Correct 87 ms 3840 KB Output is correct
51 Correct 91 ms 3840 KB Output is correct
52 Correct 92 ms 3652 KB Output is correct
53 Correct 94 ms 3708 KB Output is correct
54 Correct 87 ms 3684 KB Output is correct
55 Correct 81 ms 3584 KB Output is correct
56 Correct 84 ms 3712 KB Output is correct
57 Correct 101 ms 3548 KB Output is correct
58 Correct 102 ms 3564 KB Output is correct
59 Correct 111 ms 3568 KB Output is correct
60 Correct 95 ms 3456 KB Output is correct
61 Correct 90 ms 3584 KB Output is correct
62 Correct 90 ms 3680 KB Output is correct
63 Correct 92 ms 3584 KB Output is correct
64 Correct 92 ms 3456 KB Output is correct
65 Correct 96 ms 3588 KB Output is correct
66 Correct 102 ms 3576 KB Output is correct
67 Correct 97 ms 3456 KB Output is correct
68 Correct 99 ms 3832 KB Output is correct
69 Correct 92 ms 3556 KB Output is correct
70 Correct 92 ms 3584 KB Output is correct
71 Correct 79 ms 3584 KB Output is correct
72 Correct 97 ms 3456 KB Output is correct
73 Correct 110 ms 3576 KB Output is correct
74 Correct 93 ms 3648 KB Output is correct
75 Correct 90 ms 3600 KB Output is correct
76 Correct 93 ms 3584 KB Output is correct
77 Correct 88 ms 3584 KB Output is correct
78 Correct 85 ms 3584 KB Output is correct
79 Correct 93 ms 3456 KB Output is correct
80 Correct 92 ms 3552 KB Output is correct
81 Correct 87 ms 3584 KB Output is correct
82 Correct 94 ms 3704 KB Output is correct
83 Correct 92 ms 3584 KB Output is correct
84 Correct 90 ms 3704 KB Output is correct
85 Correct 93 ms 3576 KB Output is correct
86 Correct 96 ms 3596 KB Output is correct
87 Correct 238 ms 4224 KB Output is correct
88 Correct 746 ms 6216 KB Output is correct
89 Execution timed out 2039 ms 13828 KB Time limit exceeded
90 Halted 0 ms 0 KB -