답안 #234957

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
234957 2020-05-26T12:41:17 Z oolimry Construction of Highway (JOI18_construction) C++14
100 / 100
1132 ms 29416 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[200005];

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];
		}
	}
	
	
	
	for(ii e : edges){
		int u = e.first, v = e.second; ///extend tree from u to v;
		
		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);
			}
			
			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]);
	}
}
# 결과 실행 시간 메모리 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 6 ms 2816 KB Output is correct
5 Correct 7 ms 2816 KB Output is correct
6 Correct 8 ms 2816 KB Output is correct
7 Correct 8 ms 2816 KB Output is correct
8 Correct 7 ms 2816 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 7 ms 2816 KB Output is correct
13 Correct 7 ms 2816 KB Output is correct
14 Correct 6 ms 2816 KB Output is correct
15 Correct 7 ms 2816 KB Output is correct
16 Correct 8 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 7 ms 2816 KB Output is correct
23 Correct 7 ms 2816 KB Output is correct
24 Correct 7 ms 2816 KB Output is correct
25 Correct 8 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 9 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 6 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 8 ms 2816 KB Output is correct
38 Correct 6 ms 2816 KB Output is correct
39 Correct 6 ms 2816 KB Output is correct
40 Correct 7 ms 2816 KB Output is correct
41 Correct 6 ms 2816 KB Output is correct
42 Correct 7 ms 2816 KB Output is correct
43 Correct 6 ms 2816 KB Output is correct
44 Correct 7 ms 2816 KB Output is correct
# 결과 실행 시간 메모리 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 6 ms 2816 KB Output is correct
5 Correct 7 ms 2816 KB Output is correct
6 Correct 8 ms 2816 KB Output is correct
7 Correct 8 ms 2816 KB Output is correct
8 Correct 7 ms 2816 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 7 ms 2816 KB Output is correct
13 Correct 7 ms 2816 KB Output is correct
14 Correct 6 ms 2816 KB Output is correct
15 Correct 7 ms 2816 KB Output is correct
16 Correct 8 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 7 ms 2816 KB Output is correct
23 Correct 7 ms 2816 KB Output is correct
24 Correct 7 ms 2816 KB Output is correct
25 Correct 8 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 9 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 6 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 8 ms 2816 KB Output is correct
38 Correct 6 ms 2816 KB Output is correct
39 Correct 6 ms 2816 KB Output is correct
40 Correct 7 ms 2816 KB Output is correct
41 Correct 6 ms 2816 KB Output is correct
42 Correct 7 ms 2816 KB Output is correct
43 Correct 6 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 3456 KB Output is correct
47 Correct 18 ms 3456 KB Output is correct
48 Correct 16 ms 3456 KB Output is correct
49 Correct 12 ms 3712 KB Output is correct
50 Correct 12 ms 3712 KB Output is correct
51 Correct 12 ms 3712 KB Output is correct
52 Correct 13 ms 3712 KB Output is correct
53 Correct 14 ms 3712 KB Output is correct
54 Correct 13 ms 3584 KB Output is correct
55 Correct 13 ms 3584 KB Output is correct
56 Correct 14 ms 3584 KB Output is correct
57 Correct 24 ms 3456 KB Output is correct
58 Correct 24 ms 3456 KB Output is correct
59 Correct 25 ms 3456 KB Output is correct
60 Correct 25 ms 3456 KB Output is correct
61 Correct 15 ms 3584 KB Output is correct
62 Correct 15 ms 3584 KB Output is correct
63 Correct 17 ms 3584 KB Output is correct
64 Correct 15 ms 3584 KB Output is correct
65 Correct 15 ms 3456 KB Output is correct
66 Correct 17 ms 3456 KB Output is correct
67 Correct 16 ms 3456 KB Output is correct
68 Correct 11 ms 3840 KB Output is correct
69 Correct 14 ms 3712 KB Output is correct
70 Correct 13 ms 3584 KB Output is correct
71 Correct 13 ms 3584 KB Output is correct
72 Correct 23 ms 3456 KB Output is correct
73 Correct 22 ms 3456 KB Output is correct
74 Correct 14 ms 3584 KB Output is correct
75 Correct 14 ms 3456 KB Output is correct
76 Correct 13 ms 3456 KB Output is correct
77 Correct 13 ms 3456 KB Output is correct
78 Correct 13 ms 3584 KB Output is correct
79 Correct 12 ms 3456 KB Output is correct
80 Correct 12 ms 3456 KB Output is correct
81 Correct 16 ms 3456 KB Output is correct
82 Correct 15 ms 3508 KB Output is correct
83 Correct 14 ms 3456 KB Output is correct
84 Correct 15 ms 3456 KB Output is correct
85 Correct 14 ms 3456 KB Output is correct
86 Correct 14 ms 3456 KB Output is correct
# 결과 실행 시간 메모리 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 6 ms 2816 KB Output is correct
5 Correct 7 ms 2816 KB Output is correct
6 Correct 8 ms 2816 KB Output is correct
7 Correct 8 ms 2816 KB Output is correct
8 Correct 7 ms 2816 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 7 ms 2816 KB Output is correct
13 Correct 7 ms 2816 KB Output is correct
14 Correct 6 ms 2816 KB Output is correct
15 Correct 7 ms 2816 KB Output is correct
16 Correct 8 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 7 ms 2816 KB Output is correct
23 Correct 7 ms 2816 KB Output is correct
24 Correct 7 ms 2816 KB Output is correct
25 Correct 8 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 9 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 6 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 8 ms 2816 KB Output is correct
38 Correct 6 ms 2816 KB Output is correct
39 Correct 6 ms 2816 KB Output is correct
40 Correct 7 ms 2816 KB Output is correct
41 Correct 6 ms 2816 KB Output is correct
42 Correct 7 ms 2816 KB Output is correct
43 Correct 6 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 3456 KB Output is correct
47 Correct 18 ms 3456 KB Output is correct
48 Correct 16 ms 3456 KB Output is correct
49 Correct 12 ms 3712 KB Output is correct
50 Correct 12 ms 3712 KB Output is correct
51 Correct 12 ms 3712 KB Output is correct
52 Correct 13 ms 3712 KB Output is correct
53 Correct 14 ms 3712 KB Output is correct
54 Correct 13 ms 3584 KB Output is correct
55 Correct 13 ms 3584 KB Output is correct
56 Correct 14 ms 3584 KB Output is correct
57 Correct 24 ms 3456 KB Output is correct
58 Correct 24 ms 3456 KB Output is correct
59 Correct 25 ms 3456 KB Output is correct
60 Correct 25 ms 3456 KB Output is correct
61 Correct 15 ms 3584 KB Output is correct
62 Correct 15 ms 3584 KB Output is correct
63 Correct 17 ms 3584 KB Output is correct
64 Correct 15 ms 3584 KB Output is correct
65 Correct 15 ms 3456 KB Output is correct
66 Correct 17 ms 3456 KB Output is correct
67 Correct 16 ms 3456 KB Output is correct
68 Correct 11 ms 3840 KB Output is correct
69 Correct 14 ms 3712 KB Output is correct
70 Correct 13 ms 3584 KB Output is correct
71 Correct 13 ms 3584 KB Output is correct
72 Correct 23 ms 3456 KB Output is correct
73 Correct 22 ms 3456 KB Output is correct
74 Correct 14 ms 3584 KB Output is correct
75 Correct 14 ms 3456 KB Output is correct
76 Correct 13 ms 3456 KB Output is correct
77 Correct 13 ms 3456 KB Output is correct
78 Correct 13 ms 3584 KB Output is correct
79 Correct 12 ms 3456 KB Output is correct
80 Correct 12 ms 3456 KB Output is correct
81 Correct 16 ms 3456 KB Output is correct
82 Correct 15 ms 3508 KB Output is correct
83 Correct 14 ms 3456 KB Output is correct
84 Correct 15 ms 3456 KB Output is correct
85 Correct 14 ms 3456 KB Output is correct
86 Correct 14 ms 3456 KB Output is correct
87 Correct 41 ms 4608 KB Output is correct
88 Correct 118 ms 8300 KB Output is correct
89 Correct 567 ms 21228 KB Output is correct
90 Correct 600 ms 23276 KB Output is correct
91 Correct 584 ms 23276 KB Output is correct
92 Correct 287 ms 29292 KB Output is correct
93 Correct 287 ms 29416 KB Output is correct
94 Correct 287 ms 29292 KB Output is correct
95 Correct 383 ms 27628 KB Output is correct
96 Correct 404 ms 28140 KB Output is correct
97 Correct 383 ms 28012 KB Output is correct
98 Correct 373 ms 28012 KB Output is correct
99 Correct 369 ms 26472 KB Output is correct
100 Correct 1018 ms 22888 KB Output is correct
101 Correct 1112 ms 23276 KB Output is correct
102 Correct 1102 ms 23276 KB Output is correct
103 Correct 1132 ms 23276 KB Output is correct
104 Correct 536 ms 26348 KB Output is correct
105 Correct 526 ms 26344 KB Output is correct
106 Correct 516 ms 26220 KB Output is correct
107 Correct 457 ms 21740 KB Output is correct
108 Correct 502 ms 21740 KB Output is correct
109 Correct 573 ms 22380 KB Output is correct
110 Correct 250 ms 28016 KB Output is correct
111 Correct 353 ms 27752 KB Output is correct
112 Correct 345 ms 26772 KB Output is correct
113 Correct 354 ms 24940 KB Output is correct
114 Correct 1014 ms 23020 KB Output is correct
115 Correct 991 ms 21868 KB Output is correct
116 Correct 500 ms 24940 KB Output is correct
117 Correct 368 ms 24424 KB Output is correct
118 Correct 354 ms 23916 KB Output is correct
119 Correct 332 ms 23660 KB Output is correct
120 Correct 322 ms 23272 KB Output is correct
121 Correct 315 ms 22892 KB Output is correct
122 Correct 307 ms 22636 KB Output is correct
123 Correct 540 ms 24456 KB Output is correct
124 Correct 500 ms 23912 KB Output is correct
125 Correct 511 ms 23740 KB Output is correct
126 Correct 476 ms 23532 KB Output is correct
127 Correct 455 ms 23024 KB Output is correct
128 Correct 440 ms 22764 KB Output is correct