답안 #752158

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
752158 2023-06-02T11:26:53 Z Dan4Life Construction of Highway (JOI18_construction) C++17
100 / 100
1387 ms 106516 KB
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define all(a) begin(a),end(a)
const int N = (int)1e5+2;
using ll = long long;
using ar = array<int,2>;
vector<int> adj[N];
deque<ar> ch[N], edge;
int n, c[N], h[N], sz[N], p[N], dep[N], fen[N];

void upd(int x, int v){	for(; x<N; x+=x&-x) fen[x]+=v; }
int sum(int x){	int s=0; for(; x; x-=x&-x) s+=fen[x]; return s; }

int dfsSz(int s){
	for(auto &u : adj[s]){
		int x = adj[s][0];
		dep[u] = dep[s]+1, sz[s]+=dfsSz(u);
		if(sz[x]<sz[u]) swap(adj[s][0],u);
	}
	return (sz[s]+=1);
}

void Hld(int s, int head){
	h[s] = head;
	for(auto u : adj[s]) 
		Hld(u,u==adj[s][0]?head:u);
}

ll calc(int a){
	deque<ar> v, tv; v.clear(); ll ans = 0;
	while(a!=-1){
		int H = h[a], siz = dep[a]-dep[h[a]]+1; tv.clear();
		for(auto [x,cnt] : ch[H]){
			tv.push_front({x,min(cnt,siz)});
			siz-=min(cnt,siz); if(!siz) break;
		}
		a = p[H]; for(auto u : tv) v.pb(u); 
	}
	for(int i = 1; i <= n; i++) fen[i]=0;
	for(auto [x,y] : v) ans+=(ll)y*sum(x-1), upd(x,y);
	return ans;
}

void add(int b, int v){
	while(b!=-1){
		int H = h[b], siz = dep[b]-dep[h[b]]+1; int tsz = siz;
		while(!ch[H].empty() and siz){
			int cnt = ch[H].front()[1];
			if(cnt<=siz) ch[H].pop_front(), siz-=cnt;
			else ch[H].front()[1]-=siz, siz=0;
		}
		ch[H].push_front({v,tsz}); b = p[H];
	}
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie();
	cin >> n; vector<int> v; int a, b; p[1]=-1;
	for(int i = 1; i <= n; i++) cin >> c[i], v.pb(c[i]);
	sort(all(v)), v.erase(unique(all(v)),end(v));
	for(int i = 1; i <= n; i++) c[i] = lower_bound(all(v),c[i])-begin(v)+1;
	for(int i = 1; i < n; i++) cin >> a >> b, adj[a].pb(b), p[b]=a, edge.pb({a,b});
	ch[1].pb({c[1],1}); dfsSz(1); Hld(1,1); 
	for(auto [a,b] : edge) cout << calc(a) << "\n", add(b,c[b]);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 42 ms 69972 KB Output is correct
2 Correct 41 ms 70020 KB Output is correct
3 Correct 48 ms 70036 KB Output is correct
4 Correct 47 ms 70116 KB Output is correct
5 Correct 46 ms 70152 KB Output is correct
6 Correct 49 ms 70184 KB Output is correct
7 Correct 50 ms 70192 KB Output is correct
8 Correct 42 ms 70092 KB Output is correct
9 Correct 43 ms 70016 KB Output is correct
10 Correct 43 ms 70096 KB Output is correct
11 Correct 50 ms 70140 KB Output is correct
12 Correct 49 ms 70156 KB Output is correct
13 Correct 49 ms 70100 KB Output is correct
14 Correct 48 ms 70140 KB Output is correct
15 Correct 50 ms 70092 KB Output is correct
16 Correct 51 ms 70176 KB Output is correct
17 Correct 53 ms 70188 KB Output is correct
18 Correct 49 ms 70100 KB Output is correct
19 Correct 46 ms 70144 KB Output is correct
20 Correct 47 ms 70184 KB Output is correct
21 Correct 49 ms 70120 KB Output is correct
22 Correct 54 ms 70092 KB Output is correct
23 Correct 50 ms 70148 KB Output is correct
24 Correct 52 ms 70176 KB Output is correct
25 Correct 49 ms 70252 KB Output is correct
26 Correct 43 ms 70096 KB Output is correct
27 Correct 47 ms 70100 KB Output is correct
28 Correct 47 ms 70188 KB Output is correct
29 Correct 48 ms 70136 KB Output is correct
30 Correct 48 ms 70096 KB Output is correct
31 Correct 47 ms 70100 KB Output is correct
32 Correct 48 ms 70212 KB Output is correct
33 Correct 50 ms 70168 KB Output is correct
34 Correct 45 ms 70188 KB Output is correct
35 Correct 47 ms 70184 KB Output is correct
36 Correct 45 ms 70188 KB Output is correct
37 Correct 48 ms 70100 KB Output is correct
38 Correct 47 ms 70168 KB Output is correct
39 Correct 47 ms 70188 KB Output is correct
40 Correct 47 ms 70168 KB Output is correct
41 Correct 50 ms 70084 KB Output is correct
42 Correct 48 ms 70068 KB Output is correct
43 Correct 48 ms 70184 KB Output is correct
44 Correct 47 ms 70092 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 42 ms 69972 KB Output is correct
2 Correct 41 ms 70020 KB Output is correct
3 Correct 48 ms 70036 KB Output is correct
4 Correct 47 ms 70116 KB Output is correct
5 Correct 46 ms 70152 KB Output is correct
6 Correct 49 ms 70184 KB Output is correct
7 Correct 50 ms 70192 KB Output is correct
8 Correct 42 ms 70092 KB Output is correct
9 Correct 43 ms 70016 KB Output is correct
10 Correct 43 ms 70096 KB Output is correct
11 Correct 50 ms 70140 KB Output is correct
12 Correct 49 ms 70156 KB Output is correct
13 Correct 49 ms 70100 KB Output is correct
14 Correct 48 ms 70140 KB Output is correct
15 Correct 50 ms 70092 KB Output is correct
16 Correct 51 ms 70176 KB Output is correct
17 Correct 53 ms 70188 KB Output is correct
18 Correct 49 ms 70100 KB Output is correct
19 Correct 46 ms 70144 KB Output is correct
20 Correct 47 ms 70184 KB Output is correct
21 Correct 49 ms 70120 KB Output is correct
22 Correct 54 ms 70092 KB Output is correct
23 Correct 50 ms 70148 KB Output is correct
24 Correct 52 ms 70176 KB Output is correct
25 Correct 49 ms 70252 KB Output is correct
26 Correct 43 ms 70096 KB Output is correct
27 Correct 47 ms 70100 KB Output is correct
28 Correct 47 ms 70188 KB Output is correct
29 Correct 48 ms 70136 KB Output is correct
30 Correct 48 ms 70096 KB Output is correct
31 Correct 47 ms 70100 KB Output is correct
32 Correct 48 ms 70212 KB Output is correct
33 Correct 50 ms 70168 KB Output is correct
34 Correct 45 ms 70188 KB Output is correct
35 Correct 47 ms 70184 KB Output is correct
36 Correct 45 ms 70188 KB Output is correct
37 Correct 48 ms 70100 KB Output is correct
38 Correct 47 ms 70168 KB Output is correct
39 Correct 47 ms 70188 KB Output is correct
40 Correct 47 ms 70168 KB Output is correct
41 Correct 50 ms 70084 KB Output is correct
42 Correct 48 ms 70068 KB Output is correct
43 Correct 48 ms 70184 KB Output is correct
44 Correct 47 ms 70092 KB Output is correct
45 Correct 50 ms 70228 KB Output is correct
46 Correct 54 ms 71244 KB Output is correct
47 Correct 54 ms 71172 KB Output is correct
48 Correct 57 ms 71216 KB Output is correct
49 Correct 48 ms 70484 KB Output is correct
50 Correct 45 ms 70596 KB Output is correct
51 Correct 48 ms 70496 KB Output is correct
52 Correct 53 ms 71444 KB Output is correct
53 Correct 53 ms 71460 KB Output is correct
54 Correct 57 ms 71372 KB Output is correct
55 Correct 55 ms 71460 KB Output is correct
56 Correct 59 ms 71320 KB Output is correct
57 Correct 54 ms 71240 KB Output is correct
58 Correct 56 ms 71116 KB Output is correct
59 Correct 57 ms 71192 KB Output is correct
60 Correct 56 ms 71152 KB Output is correct
61 Correct 52 ms 71396 KB Output is correct
62 Correct 55 ms 71392 KB Output is correct
63 Correct 58 ms 71408 KB Output is correct
64 Correct 54 ms 71224 KB Output is correct
65 Correct 54 ms 71244 KB Output is correct
66 Correct 54 ms 71172 KB Output is correct
67 Correct 55 ms 71236 KB Output is correct
68 Correct 49 ms 70500 KB Output is correct
69 Correct 53 ms 71400 KB Output is correct
70 Correct 53 ms 71452 KB Output is correct
71 Correct 52 ms 71316 KB Output is correct
72 Correct 55 ms 71228 KB Output is correct
73 Correct 55 ms 71192 KB Output is correct
74 Correct 53 ms 71392 KB Output is correct
75 Correct 59 ms 71280 KB Output is correct
76 Correct 57 ms 71312 KB Output is correct
77 Correct 51 ms 71244 KB Output is correct
78 Correct 52 ms 71344 KB Output is correct
79 Correct 53 ms 71296 KB Output is correct
80 Correct 51 ms 71176 KB Output is correct
81 Correct 59 ms 71336 KB Output is correct
82 Correct 62 ms 71244 KB Output is correct
83 Correct 55 ms 71320 KB Output is correct
84 Correct 52 ms 71272 KB Output is correct
85 Correct 53 ms 71224 KB Output is correct
86 Correct 53 ms 71356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 42 ms 69972 KB Output is correct
2 Correct 41 ms 70020 KB Output is correct
3 Correct 48 ms 70036 KB Output is correct
4 Correct 47 ms 70116 KB Output is correct
5 Correct 46 ms 70152 KB Output is correct
6 Correct 49 ms 70184 KB Output is correct
7 Correct 50 ms 70192 KB Output is correct
8 Correct 42 ms 70092 KB Output is correct
9 Correct 43 ms 70016 KB Output is correct
10 Correct 43 ms 70096 KB Output is correct
11 Correct 50 ms 70140 KB Output is correct
12 Correct 49 ms 70156 KB Output is correct
13 Correct 49 ms 70100 KB Output is correct
14 Correct 48 ms 70140 KB Output is correct
15 Correct 50 ms 70092 KB Output is correct
16 Correct 51 ms 70176 KB Output is correct
17 Correct 53 ms 70188 KB Output is correct
18 Correct 49 ms 70100 KB Output is correct
19 Correct 46 ms 70144 KB Output is correct
20 Correct 47 ms 70184 KB Output is correct
21 Correct 49 ms 70120 KB Output is correct
22 Correct 54 ms 70092 KB Output is correct
23 Correct 50 ms 70148 KB Output is correct
24 Correct 52 ms 70176 KB Output is correct
25 Correct 49 ms 70252 KB Output is correct
26 Correct 43 ms 70096 KB Output is correct
27 Correct 47 ms 70100 KB Output is correct
28 Correct 47 ms 70188 KB Output is correct
29 Correct 48 ms 70136 KB Output is correct
30 Correct 48 ms 70096 KB Output is correct
31 Correct 47 ms 70100 KB Output is correct
32 Correct 48 ms 70212 KB Output is correct
33 Correct 50 ms 70168 KB Output is correct
34 Correct 45 ms 70188 KB Output is correct
35 Correct 47 ms 70184 KB Output is correct
36 Correct 45 ms 70188 KB Output is correct
37 Correct 48 ms 70100 KB Output is correct
38 Correct 47 ms 70168 KB Output is correct
39 Correct 47 ms 70188 KB Output is correct
40 Correct 47 ms 70168 KB Output is correct
41 Correct 50 ms 70084 KB Output is correct
42 Correct 48 ms 70068 KB Output is correct
43 Correct 48 ms 70184 KB Output is correct
44 Correct 47 ms 70092 KB Output is correct
45 Correct 50 ms 70228 KB Output is correct
46 Correct 54 ms 71244 KB Output is correct
47 Correct 54 ms 71172 KB Output is correct
48 Correct 57 ms 71216 KB Output is correct
49 Correct 48 ms 70484 KB Output is correct
50 Correct 45 ms 70596 KB Output is correct
51 Correct 48 ms 70496 KB Output is correct
52 Correct 53 ms 71444 KB Output is correct
53 Correct 53 ms 71460 KB Output is correct
54 Correct 57 ms 71372 KB Output is correct
55 Correct 55 ms 71460 KB Output is correct
56 Correct 59 ms 71320 KB Output is correct
57 Correct 54 ms 71240 KB Output is correct
58 Correct 56 ms 71116 KB Output is correct
59 Correct 57 ms 71192 KB Output is correct
60 Correct 56 ms 71152 KB Output is correct
61 Correct 52 ms 71396 KB Output is correct
62 Correct 55 ms 71392 KB Output is correct
63 Correct 58 ms 71408 KB Output is correct
64 Correct 54 ms 71224 KB Output is correct
65 Correct 54 ms 71244 KB Output is correct
66 Correct 54 ms 71172 KB Output is correct
67 Correct 55 ms 71236 KB Output is correct
68 Correct 49 ms 70500 KB Output is correct
69 Correct 53 ms 71400 KB Output is correct
70 Correct 53 ms 71452 KB Output is correct
71 Correct 52 ms 71316 KB Output is correct
72 Correct 55 ms 71228 KB Output is correct
73 Correct 55 ms 71192 KB Output is correct
74 Correct 53 ms 71392 KB Output is correct
75 Correct 59 ms 71280 KB Output is correct
76 Correct 57 ms 71312 KB Output is correct
77 Correct 51 ms 71244 KB Output is correct
78 Correct 52 ms 71344 KB Output is correct
79 Correct 53 ms 71296 KB Output is correct
80 Correct 51 ms 71176 KB Output is correct
81 Correct 59 ms 71336 KB Output is correct
82 Correct 62 ms 71244 KB Output is correct
83 Correct 55 ms 71320 KB Output is correct
84 Correct 52 ms 71272 KB Output is correct
85 Correct 53 ms 71224 KB Output is correct
86 Correct 53 ms 71356 KB Output is correct
87 Correct 73 ms 73080 KB Output is correct
88 Correct 186 ms 79516 KB Output is correct
89 Correct 1375 ms 101392 KB Output is correct
90 Correct 1174 ms 101232 KB Output is correct
91 Correct 1387 ms 101160 KB Output is correct
92 Correct 1211 ms 84712 KB Output is correct
93 Correct 1076 ms 84672 KB Output is correct
94 Correct 1006 ms 84772 KB Output is correct
95 Correct 1030 ms 106392 KB Output is correct
96 Correct 1063 ms 106188 KB Output is correct
97 Correct 1038 ms 106264 KB Output is correct
98 Correct 1059 ms 106228 KB Output is correct
99 Correct 1136 ms 105244 KB Output is correct
100 Correct 1161 ms 93872 KB Output is correct
101 Correct 1159 ms 93700 KB Output is correct
102 Correct 1191 ms 93720 KB Output is correct
103 Correct 1195 ms 93704 KB Output is correct
104 Correct 1106 ms 105328 KB Output is correct
105 Correct 1043 ms 105100 KB Output is correct
106 Correct 1121 ms 105296 KB Output is correct
107 Correct 1166 ms 101332 KB Output is correct
108 Correct 1167 ms 101324 KB Output is correct
109 Correct 1143 ms 101376 KB Output is correct
110 Correct 987 ms 84752 KB Output is correct
111 Correct 1064 ms 106516 KB Output is correct
112 Correct 1020 ms 106328 KB Output is correct
113 Correct 1048 ms 105260 KB Output is correct
114 Correct 1135 ms 93880 KB Output is correct
115 Correct 1135 ms 93644 KB Output is correct
116 Correct 1127 ms 105188 KB Output is correct
117 Correct 1036 ms 103184 KB Output is correct
118 Correct 1076 ms 102624 KB Output is correct
119 Correct 1095 ms 102188 KB Output is correct
120 Correct 1053 ms 103040 KB Output is correct
121 Correct 1141 ms 102468 KB Output is correct
122 Correct 1050 ms 102100 KB Output is correct
123 Correct 1060 ms 103284 KB Output is correct
124 Correct 1083 ms 102688 KB Output is correct
125 Correct 1096 ms 102256 KB Output is correct
126 Correct 1223 ms 103268 KB Output is correct
127 Correct 1217 ms 102612 KB Output is correct
128 Correct 1268 ms 102204 KB Output is correct