Submission #234956

# Submission time Handle Problem Language Result Execution time Memory
234956 2020-05-26T12:40:03 Z oolimry Construction of Highway (JOI18_construction) C++14
16 / 100
582 ms 23368 KB
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
using namespace std;
typedef pair<long long, long long> ii;

int n;
int cnt = 0;
int arr[100005];
int low[100005];
int high[100005];
int p[100005][20];
vector<int> adj[100005];
vector<ii> edges;

///segment tree
ii tree[200005]; 
int timer = 1;
void update(int i, int v){
	for(i += n;i > 0;i >>= 1) tree[i] = ii(timer, v);
	timer++;
}

ii query(int u){
	int l = low[u], r = high[u];
	ii res = ii(0,0);
	for(l += n, r += n+1;l < r;l >>= 1, r >>= 1){
		if(l&1) res = max(res, tree[l++]);
		if(r&1) res = max(res, tree[--r]);
	}
	return res;
}
///segment tree

///segment tree 2
int tree2[100005];

void update2(int i, int v){
	for(i += n;i > 0;i >>= 1) tree2[i] += v;
}

long long query2(int l, int r){
	long long res = 0;
	for(l += n, r += n;l < r;l >>= 1, r >>= 1){
		if(l&1) res += tree2[l++];
		if(r&1) res += tree2[--r];
	}
	return res;
}

///segment tree 2

void dfs(int u, int P){
	low[u] = cnt; cnt++;
	high[u] = low[u];
	for(int v : adj[u]){
		if(v == P) continue;
		dfs(v, u);
		p[v][0] = u;
		high[u] = max(high[u], high[v]);
	}
	update(low[u], arr[u]);
}

int main(){
	ios_base::sync_with_stdio(false); cin.tie(0);
	
	cin >> n;
	vector<int> discrodile;
	for(int i = 1;i <= n;i++){
		cin >> arr[i];
		discrodile.push_back(arr[i]);
	}
	
	sort(discrodile.begin(), discrodile.end());
	discrodile.erase(unique(all(discrodile)), discrodile.end());
	
	for(int i = 1;i <= n;i++){
		arr[i] = lower_bound(all(discrodile), arr[i]) - discrodile.begin();
	}
	
	for(int i = 1;i < n;i++){
		int u, v; cin >> u >> v;
		adj[u].push_back(v);
		adj[v].push_back(u);
		edges.push_back(ii(u,v));
	}
	
	dfs(1,0);
	
	for(int d = 1;d <= 19;d++){
		for(int i = 1;i <= n;i++){
			p[i][d] = p[p[i][d-1]][d-1];
			//cout << p[i][d] << " ";
		}
		//cout << "\n";
	}
	
	
	
	for(ii e : edges){
		int u = e.first, v = e.second; ///extend tree from u to v;
		//cout << "\n" << u << " " << v << ":\n";
		
		vector<ii> parts;
		while(true){
			ii root = query(u);
			
			
			int sz = 1;
			for(int d = 19;d >= 0;d--){
				int nu = p[u][d];
				if(nu == 0) continue;
				if(query(nu) != root) continue;
				u = nu;
				sz += (1 << d);
			}
			
			//cout << root.first << " " << root.second << " " << sz << "\n";
			parts.push_back(ii(root.second, sz));
			if(u == 1) break;
			u = p[u][0];
		}
		
		long long ans = 0;
		for(ii x : parts){
			ans += x.second * query2(0, x.first);
			update2(x.first, x.second);
		}
		for(ii x : parts) update2(x.first, -x.second); ///undo
		
		cout << ans << "\n";
		
		update(low[v], arr[v]);
	}
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 2688 KB Output is correct
2 Correct 6 ms 2688 KB Output is correct
3 Correct 6 ms 2688 KB Output is correct
4 Correct 7 ms 2944 KB Output is correct
5 Correct 7 ms 2816 KB Output is correct
6 Correct 7 ms 2816 KB Output is correct
7 Correct 7 ms 2816 KB Output is correct
8 Correct 6 ms 2688 KB Output is correct
9 Correct 6 ms 2816 KB Output is correct
10 Correct 6 ms 2816 KB Output is correct
11 Correct 6 ms 2816 KB Output is correct
12 Correct 6 ms 2816 KB Output is correct
13 Correct 6 ms 2816 KB Output is correct
14 Correct 7 ms 2816 KB Output is correct
15 Correct 7 ms 2816 KB Output is correct
16 Correct 7 ms 2816 KB Output is correct
17 Correct 7 ms 2816 KB Output is correct
18 Correct 7 ms 2816 KB Output is correct
19 Correct 7 ms 2816 KB Output is correct
20 Correct 7 ms 2816 KB Output is correct
21 Correct 7 ms 2816 KB Output is correct
22 Correct 6 ms 2816 KB Output is correct
23 Correct 6 ms 2816 KB Output is correct
24 Correct 7 ms 2816 KB Output is correct
25 Correct 7 ms 2816 KB Output is correct
26 Correct 6 ms 2816 KB Output is correct
27 Correct 7 ms 2816 KB Output is correct
28 Correct 7 ms 2816 KB Output is correct
29 Correct 7 ms 2816 KB Output is correct
30 Correct 7 ms 2816 KB Output is correct
31 Correct 7 ms 2816 KB Output is correct
32 Correct 7 ms 2816 KB Output is correct
33 Correct 7 ms 2816 KB Output is correct
34 Correct 7 ms 2816 KB Output is correct
35 Correct 7 ms 2816 KB Output is correct
36 Correct 7 ms 2816 KB Output is correct
37 Correct 7 ms 2816 KB Output is correct
38 Correct 6 ms 2816 KB Output is correct
39 Correct 7 ms 2816 KB Output is correct
40 Correct 7 ms 2816 KB Output is correct
41 Correct 7 ms 2816 KB Output is correct
42 Correct 7 ms 2816 KB Output is correct
43 Correct 7 ms 2816 KB Output is correct
44 Correct 7 ms 2816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 2688 KB Output is correct
2 Correct 6 ms 2688 KB Output is correct
3 Correct 6 ms 2688 KB Output is correct
4 Correct 7 ms 2944 KB Output is correct
5 Correct 7 ms 2816 KB Output is correct
6 Correct 7 ms 2816 KB Output is correct
7 Correct 7 ms 2816 KB Output is correct
8 Correct 6 ms 2688 KB Output is correct
9 Correct 6 ms 2816 KB Output is correct
10 Correct 6 ms 2816 KB Output is correct
11 Correct 6 ms 2816 KB Output is correct
12 Correct 6 ms 2816 KB Output is correct
13 Correct 6 ms 2816 KB Output is correct
14 Correct 7 ms 2816 KB Output is correct
15 Correct 7 ms 2816 KB Output is correct
16 Correct 7 ms 2816 KB Output is correct
17 Correct 7 ms 2816 KB Output is correct
18 Correct 7 ms 2816 KB Output is correct
19 Correct 7 ms 2816 KB Output is correct
20 Correct 7 ms 2816 KB Output is correct
21 Correct 7 ms 2816 KB Output is correct
22 Correct 6 ms 2816 KB Output is correct
23 Correct 6 ms 2816 KB Output is correct
24 Correct 7 ms 2816 KB Output is correct
25 Correct 7 ms 2816 KB Output is correct
26 Correct 6 ms 2816 KB Output is correct
27 Correct 7 ms 2816 KB Output is correct
28 Correct 7 ms 2816 KB Output is correct
29 Correct 7 ms 2816 KB Output is correct
30 Correct 7 ms 2816 KB Output is correct
31 Correct 7 ms 2816 KB Output is correct
32 Correct 7 ms 2816 KB Output is correct
33 Correct 7 ms 2816 KB Output is correct
34 Correct 7 ms 2816 KB Output is correct
35 Correct 7 ms 2816 KB Output is correct
36 Correct 7 ms 2816 KB Output is correct
37 Correct 7 ms 2816 KB Output is correct
38 Correct 6 ms 2816 KB Output is correct
39 Correct 7 ms 2816 KB Output is correct
40 Correct 7 ms 2816 KB Output is correct
41 Correct 7 ms 2816 KB Output is correct
42 Correct 7 ms 2816 KB Output is correct
43 Correct 7 ms 2816 KB Output is correct
44 Correct 7 ms 2816 KB Output is correct
45 Correct 8 ms 2944 KB Output is correct
46 Correct 16 ms 3584 KB Output is correct
47 Correct 17 ms 3584 KB Output is correct
48 Correct 16 ms 3584 KB Output is correct
49 Correct 13 ms 3840 KB Output is correct
50 Correct 12 ms 3840 KB Output is correct
51 Correct 12 ms 3840 KB Output is correct
52 Correct 13 ms 3712 KB Output is correct
53 Correct 14 ms 3712 KB Output is correct
54 Correct 14 ms 3712 KB Output is correct
55 Correct 14 ms 3764 KB Output is correct
56 Correct 14 ms 3712 KB Output is correct
57 Correct 24 ms 3584 KB Output is correct
58 Correct 24 ms 3584 KB Output is correct
59 Correct 25 ms 3584 KB Output is correct
60 Correct 25 ms 3580 KB Output is correct
61 Correct 15 ms 3712 KB Output is correct
62 Correct 15 ms 3712 KB Output is correct
63 Correct 15 ms 3712 KB Output is correct
64 Correct 15 ms 3584 KB Output is correct
65 Correct 16 ms 3456 KB Output is correct
66 Correct 16 ms 3456 KB Output is correct
67 Correct 16 ms 3584 KB Output is correct
68 Correct 11 ms 3712 KB Output is correct
69 Correct 14 ms 3712 KB Output is correct
70 Correct 13 ms 3712 KB Output is correct
71 Correct 13 ms 3628 KB Output is correct
72 Correct 24 ms 3584 KB Output is correct
73 Correct 22 ms 3456 KB Output is correct
74 Correct 14 ms 3584 KB Output is correct
75 Correct 13 ms 3584 KB Output is correct
76 Correct 14 ms 3584 KB Output is correct
77 Correct 13 ms 3584 KB Output is correct
78 Correct 13 ms 3584 KB Output is correct
79 Correct 17 ms 3584 KB Output is correct
80 Correct 13 ms 3584 KB Output is correct
81 Correct 15 ms 3584 KB Output is correct
82 Correct 15 ms 3584 KB Output is correct
83 Correct 15 ms 3584 KB Output is correct
84 Correct 14 ms 3584 KB Output is correct
85 Correct 14 ms 3584 KB Output is correct
86 Correct 14 ms 3584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 2688 KB Output is correct
2 Correct 6 ms 2688 KB Output is correct
3 Correct 6 ms 2688 KB Output is correct
4 Correct 7 ms 2944 KB Output is correct
5 Correct 7 ms 2816 KB Output is correct
6 Correct 7 ms 2816 KB Output is correct
7 Correct 7 ms 2816 KB Output is correct
8 Correct 6 ms 2688 KB Output is correct
9 Correct 6 ms 2816 KB Output is correct
10 Correct 6 ms 2816 KB Output is correct
11 Correct 6 ms 2816 KB Output is correct
12 Correct 6 ms 2816 KB Output is correct
13 Correct 6 ms 2816 KB Output is correct
14 Correct 7 ms 2816 KB Output is correct
15 Correct 7 ms 2816 KB Output is correct
16 Correct 7 ms 2816 KB Output is correct
17 Correct 7 ms 2816 KB Output is correct
18 Correct 7 ms 2816 KB Output is correct
19 Correct 7 ms 2816 KB Output is correct
20 Correct 7 ms 2816 KB Output is correct
21 Correct 7 ms 2816 KB Output is correct
22 Correct 6 ms 2816 KB Output is correct
23 Correct 6 ms 2816 KB Output is correct
24 Correct 7 ms 2816 KB Output is correct
25 Correct 7 ms 2816 KB Output is correct
26 Correct 6 ms 2816 KB Output is correct
27 Correct 7 ms 2816 KB Output is correct
28 Correct 7 ms 2816 KB Output is correct
29 Correct 7 ms 2816 KB Output is correct
30 Correct 7 ms 2816 KB Output is correct
31 Correct 7 ms 2816 KB Output is correct
32 Correct 7 ms 2816 KB Output is correct
33 Correct 7 ms 2816 KB Output is correct
34 Correct 7 ms 2816 KB Output is correct
35 Correct 7 ms 2816 KB Output is correct
36 Correct 7 ms 2816 KB Output is correct
37 Correct 7 ms 2816 KB Output is correct
38 Correct 6 ms 2816 KB Output is correct
39 Correct 7 ms 2816 KB Output is correct
40 Correct 7 ms 2816 KB Output is correct
41 Correct 7 ms 2816 KB Output is correct
42 Correct 7 ms 2816 KB Output is correct
43 Correct 7 ms 2816 KB Output is correct
44 Correct 7 ms 2816 KB Output is correct
45 Correct 8 ms 2944 KB Output is correct
46 Correct 16 ms 3584 KB Output is correct
47 Correct 17 ms 3584 KB Output is correct
48 Correct 16 ms 3584 KB Output is correct
49 Correct 13 ms 3840 KB Output is correct
50 Correct 12 ms 3840 KB Output is correct
51 Correct 12 ms 3840 KB Output is correct
52 Correct 13 ms 3712 KB Output is correct
53 Correct 14 ms 3712 KB Output is correct
54 Correct 14 ms 3712 KB Output is correct
55 Correct 14 ms 3764 KB Output is correct
56 Correct 14 ms 3712 KB Output is correct
57 Correct 24 ms 3584 KB Output is correct
58 Correct 24 ms 3584 KB Output is correct
59 Correct 25 ms 3584 KB Output is correct
60 Correct 25 ms 3580 KB Output is correct
61 Correct 15 ms 3712 KB Output is correct
62 Correct 15 ms 3712 KB Output is correct
63 Correct 15 ms 3712 KB Output is correct
64 Correct 15 ms 3584 KB Output is correct
65 Correct 16 ms 3456 KB Output is correct
66 Correct 16 ms 3456 KB Output is correct
67 Correct 16 ms 3584 KB Output is correct
68 Correct 11 ms 3712 KB Output is correct
69 Correct 14 ms 3712 KB Output is correct
70 Correct 13 ms 3712 KB Output is correct
71 Correct 13 ms 3628 KB Output is correct
72 Correct 24 ms 3584 KB Output is correct
73 Correct 22 ms 3456 KB Output is correct
74 Correct 14 ms 3584 KB Output is correct
75 Correct 13 ms 3584 KB Output is correct
76 Correct 14 ms 3584 KB Output is correct
77 Correct 13 ms 3584 KB Output is correct
78 Correct 13 ms 3584 KB Output is correct
79 Correct 17 ms 3584 KB Output is correct
80 Correct 13 ms 3584 KB Output is correct
81 Correct 15 ms 3584 KB Output is correct
82 Correct 15 ms 3584 KB Output is correct
83 Correct 15 ms 3584 KB Output is correct
84 Correct 14 ms 3584 KB Output is correct
85 Correct 14 ms 3584 KB Output is correct
86 Correct 14 ms 3584 KB Output is correct
87 Correct 39 ms 4864 KB Output is correct
88 Correct 119 ms 8944 KB Output is correct
89 Incorrect 582 ms 23368 KB Output isn't correct
90 Halted 0 ms 0 KB -