답안 #305619

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
305619 2020-09-23T17:25:19 Z youngyojun Construction of Highway (JOI18_construction) C++17
100 / 100
696 ms 26616 KB
#include <bits/stdc++.h>
#define pb push_back
#define eb emplace_back
#define sz(V) ((int)(V).size())
#define allv(V) ((V).begin()),((V).end())
#define befv(V) ((V)[(sz(V)-2)])
#define sorv(V) sort(allv(V))
#define revv(V) reverse(allv(V))
#define univ(V) (V).erase(unique(allv(V)),(V).end())
#define clv(V) (V).clear()
#define upmin(a,b) (a)=min((a),(b))
#define upmax(a,b) (a)=max((a),(b))
#define rb(x) ((x)&(-(x)))
#define INF (0x3f3f3f3f)
#define INFLL (0x3f3f3f3f3f3f3f3fll)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
 
const int MAXN = 100055;
 
struct BIT {
	int d[MAXN];
	void upd(int x, int r) {
		for(x += 2; x < MAXN; x += rb(x))
			d[x] += r;
	}
	int get(int x) {
		int r = 0; for(x += 2; x; x -= rb(x))
			r += d[x];
		return r;
	}
} bit;
 
struct HLD {
	set<pii> V;
	int rprt, n;
 
	void init(int _n) {
		n = _n;
	}
 
	void get(vector<pii> &PQ, int i) {
		vector<pii> T;
 
		for(auto it = V.lower_bound(pii(i, -INF));; it--) {
			T.eb(min(i, it -> first), it -> second);
			if(V.begin() == it) break;
		}
		T.eb(-1, -1);
 
		for(int i = 1, n = sz(T), l, x; i < n; i++) {
			l = T[i-1].first - T[i].first;
			x = T[i-1].second;
			if(!l) continue;
 
			if(PQ.empty() || PQ.back().first != x) PQ.eb(x, l);
			else PQ.back().second += l;
		}
	}
	void upd(int i, int c) {
		for(; !V.empty();) {
			auto it = V.begin();
			if(i < it -> first) break;
			V.erase(it);
		}
		V.insert(pii(i, c));
	}
} hld[MAXN];
 
vector<int> G[MAXN];
 
int HI[MAXN], HJ[MAXN], HSZ[MAXN], Hn;
 
int prt[MAXN], dep[MAXN], cnt[MAXN], depo[MAXN];
 
int A[MAXN], B[MAXN], C[MAXN];
 
int N;
 
void f(vector<pii> &V, int i) {
	for(; i; i = hld[HI[i]].rprt)
		hld[HI[i]].get(V, HJ[i]);
}
void g(int i, int c) {
	for(; i; i = hld[HI[i]].rprt)
		hld[HI[i]].upd(HJ[i], c);
}
 
ll getRev(vector<pii> &V) {
	{
		vector<int> VX;
		for(auto &v : V) VX.pb(v.first);
		sorv(VX); univ(VX);
		for(auto &v : V)
			v.first = (int)(lower_bound(allv(VX), v.first) - VX.begin()) + 1;
	}
 
	ll ret = 0;
	for(auto &v : V) {
		ret += ll(bit.get(v.first-1)) * v.second;
		bit.upd(v.first, v.second);
	}
	for(auto &v : V) bit.upd(v.first, -v.second);
	return ret;
}
 
void dfs1(int i) {
	cnt[i] = 1;
	for(int v : G[i]) {
		dep[v] = dep[i] + 1;
		prt[v] = i;
		dfs1(v);
		cnt[i] += cnt[v];
	}
}
void dfs2(int i) {
	HJ[i] = HSZ[HI[i]]++;
	int hi = -1, hc = -1;
	for(int v : G[i]) {
		if(cnt[v] <= hc) continue;
		hi = v; hc = cnt[v];
	}
	if(hi < 0) return;
	HI[hi] = HI[i];
	dfs2(hi);
}
 
int main() {
	ios::sync_with_stdio(false);
 
	cin >> N;
	for(int i = 1; i <= N; i++) cin >> C[i];
	for(int i = 1; i < N; i++) cin >> A[i] >> B[i];
 
	for(int i = 1; i < N; i++) G[A[i]].pb(B[i]);
	dep[1] = 1; dfs1(1);
 
	iota(depo, depo+N+1, 0);
	sort(depo+1, depo+N+1, [&](int a, int b) { return dep[a] < dep[b]; });
	for(int oi = 1, i; oi <= N; oi++) {
		i = depo[oi];
		if(HI[i]) continue;
		Hn++; HI[i] = Hn;
		dfs2(i);
	}
 
	for(int i = 1; i <= Hn; i++) hld[i].init(HSZ[i]);
	for(int i = 1; i <= N; i++) {
		hld[HI[i]].V.insert(pii(HJ[i], C[i]));
		if(!HJ[i]) hld[HI[i]].rprt = prt[i];
	}
 
	for(int qi = 1, a, b; qi < N; qi++) {
		a = A[qi]; b = B[qi];
 
		vector<pii> V; f(V, a);
		printf("%lld\n", getRev(V));
		g(b, C[b]);
	}
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 8192 KB Output is correct
2 Correct 6 ms 8192 KB Output is correct
3 Correct 6 ms 8320 KB Output is correct
4 Correct 6 ms 8320 KB Output is correct
5 Correct 7 ms 8320 KB Output is correct
6 Correct 7 ms 8320 KB Output is correct
7 Correct 8 ms 8320 KB Output is correct
8 Correct 7 ms 8320 KB Output is correct
9 Correct 7 ms 8320 KB Output is correct
10 Correct 7 ms 8320 KB Output is correct
11 Correct 7 ms 8320 KB Output is correct
12 Correct 6 ms 8320 KB Output is correct
13 Correct 7 ms 8320 KB Output is correct
14 Correct 7 ms 8320 KB Output is correct
15 Correct 7 ms 8320 KB Output is correct
16 Correct 8 ms 8320 KB Output is correct
17 Correct 7 ms 8320 KB Output is correct
18 Correct 8 ms 8320 KB Output is correct
19 Correct 7 ms 8320 KB Output is correct
20 Correct 7 ms 8320 KB Output is correct
21 Correct 7 ms 8320 KB Output is correct
22 Correct 7 ms 8448 KB Output is correct
23 Correct 7 ms 8320 KB Output is correct
24 Correct 7 ms 8320 KB Output is correct
25 Correct 8 ms 8320 KB Output is correct
26 Correct 7 ms 8320 KB Output is correct
27 Correct 7 ms 8320 KB Output is correct
28 Correct 7 ms 8320 KB Output is correct
29 Correct 6 ms 8320 KB Output is correct
30 Correct 8 ms 8320 KB Output is correct
31 Correct 7 ms 8320 KB Output is correct
32 Correct 7 ms 8320 KB Output is correct
33 Correct 7 ms 8380 KB Output is correct
34 Correct 7 ms 8320 KB Output is correct
35 Correct 7 ms 8448 KB Output is correct
36 Correct 7 ms 8320 KB Output is correct
37 Correct 7 ms 8320 KB Output is correct
38 Correct 7 ms 8320 KB Output is correct
39 Correct 8 ms 8320 KB Output is correct
40 Correct 7 ms 8320 KB Output is correct
41 Correct 6 ms 8320 KB Output is correct
42 Correct 7 ms 8320 KB Output is correct
43 Correct 7 ms 8320 KB Output is correct
44 Correct 7 ms 8320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 8192 KB Output is correct
2 Correct 6 ms 8192 KB Output is correct
3 Correct 6 ms 8320 KB Output is correct
4 Correct 6 ms 8320 KB Output is correct
5 Correct 7 ms 8320 KB Output is correct
6 Correct 7 ms 8320 KB Output is correct
7 Correct 8 ms 8320 KB Output is correct
8 Correct 7 ms 8320 KB Output is correct
9 Correct 7 ms 8320 KB Output is correct
10 Correct 7 ms 8320 KB Output is correct
11 Correct 7 ms 8320 KB Output is correct
12 Correct 6 ms 8320 KB Output is correct
13 Correct 7 ms 8320 KB Output is correct
14 Correct 7 ms 8320 KB Output is correct
15 Correct 7 ms 8320 KB Output is correct
16 Correct 8 ms 8320 KB Output is correct
17 Correct 7 ms 8320 KB Output is correct
18 Correct 8 ms 8320 KB Output is correct
19 Correct 7 ms 8320 KB Output is correct
20 Correct 7 ms 8320 KB Output is correct
21 Correct 7 ms 8320 KB Output is correct
22 Correct 7 ms 8448 KB Output is correct
23 Correct 7 ms 8320 KB Output is correct
24 Correct 7 ms 8320 KB Output is correct
25 Correct 8 ms 8320 KB Output is correct
26 Correct 7 ms 8320 KB Output is correct
27 Correct 7 ms 8320 KB Output is correct
28 Correct 7 ms 8320 KB Output is correct
29 Correct 6 ms 8320 KB Output is correct
30 Correct 8 ms 8320 KB Output is correct
31 Correct 7 ms 8320 KB Output is correct
32 Correct 7 ms 8320 KB Output is correct
33 Correct 7 ms 8380 KB Output is correct
34 Correct 7 ms 8320 KB Output is correct
35 Correct 7 ms 8448 KB Output is correct
36 Correct 7 ms 8320 KB Output is correct
37 Correct 7 ms 8320 KB Output is correct
38 Correct 7 ms 8320 KB Output is correct
39 Correct 8 ms 8320 KB Output is correct
40 Correct 7 ms 8320 KB Output is correct
41 Correct 6 ms 8320 KB Output is correct
42 Correct 7 ms 8320 KB Output is correct
43 Correct 7 ms 8320 KB Output is correct
44 Correct 7 ms 8320 KB Output is correct
45 Correct 9 ms 8448 KB Output is correct
46 Correct 15 ms 8704 KB Output is correct
47 Correct 16 ms 8704 KB Output is correct
48 Correct 15 ms 8704 KB Output is correct
49 Correct 11 ms 8960 KB Output is correct
50 Correct 11 ms 8960 KB Output is correct
51 Correct 11 ms 8960 KB Output is correct
52 Correct 11 ms 8960 KB Output is correct
53 Correct 12 ms 8832 KB Output is correct
54 Correct 11 ms 8832 KB Output is correct
55 Correct 11 ms 8832 KB Output is correct
56 Correct 11 ms 8832 KB Output is correct
57 Correct 19 ms 8704 KB Output is correct
58 Correct 21 ms 8704 KB Output is correct
59 Correct 21 ms 8704 KB Output is correct
60 Correct 22 ms 8824 KB Output is correct
61 Correct 13 ms 8960 KB Output is correct
62 Correct 12 ms 8832 KB Output is correct
63 Correct 11 ms 8832 KB Output is correct
64 Correct 14 ms 8704 KB Output is correct
65 Correct 15 ms 8704 KB Output is correct
66 Correct 15 ms 8704 KB Output is correct
67 Correct 16 ms 8704 KB Output is correct
68 Correct 12 ms 8960 KB Output is correct
69 Correct 11 ms 8832 KB Output is correct
70 Correct 11 ms 8832 KB Output is correct
71 Correct 11 ms 8880 KB Output is correct
72 Correct 19 ms 8704 KB Output is correct
73 Correct 21 ms 8704 KB Output is correct
74 Correct 11 ms 8832 KB Output is correct
75 Correct 11 ms 8832 KB Output is correct
76 Correct 11 ms 8832 KB Output is correct
77 Correct 12 ms 8832 KB Output is correct
78 Correct 11 ms 8832 KB Output is correct
79 Correct 12 ms 8704 KB Output is correct
80 Correct 11 ms 8736 KB Output is correct
81 Correct 12 ms 8832 KB Output is correct
82 Correct 12 ms 8832 KB Output is correct
83 Correct 12 ms 8704 KB Output is correct
84 Correct 12 ms 8832 KB Output is correct
85 Correct 12 ms 8704 KB Output is correct
86 Correct 12 ms 8704 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 8192 KB Output is correct
2 Correct 6 ms 8192 KB Output is correct
3 Correct 6 ms 8320 KB Output is correct
4 Correct 6 ms 8320 KB Output is correct
5 Correct 7 ms 8320 KB Output is correct
6 Correct 7 ms 8320 KB Output is correct
7 Correct 8 ms 8320 KB Output is correct
8 Correct 7 ms 8320 KB Output is correct
9 Correct 7 ms 8320 KB Output is correct
10 Correct 7 ms 8320 KB Output is correct
11 Correct 7 ms 8320 KB Output is correct
12 Correct 6 ms 8320 KB Output is correct
13 Correct 7 ms 8320 KB Output is correct
14 Correct 7 ms 8320 KB Output is correct
15 Correct 7 ms 8320 KB Output is correct
16 Correct 8 ms 8320 KB Output is correct
17 Correct 7 ms 8320 KB Output is correct
18 Correct 8 ms 8320 KB Output is correct
19 Correct 7 ms 8320 KB Output is correct
20 Correct 7 ms 8320 KB Output is correct
21 Correct 7 ms 8320 KB Output is correct
22 Correct 7 ms 8448 KB Output is correct
23 Correct 7 ms 8320 KB Output is correct
24 Correct 7 ms 8320 KB Output is correct
25 Correct 8 ms 8320 KB Output is correct
26 Correct 7 ms 8320 KB Output is correct
27 Correct 7 ms 8320 KB Output is correct
28 Correct 7 ms 8320 KB Output is correct
29 Correct 6 ms 8320 KB Output is correct
30 Correct 8 ms 8320 KB Output is correct
31 Correct 7 ms 8320 KB Output is correct
32 Correct 7 ms 8320 KB Output is correct
33 Correct 7 ms 8380 KB Output is correct
34 Correct 7 ms 8320 KB Output is correct
35 Correct 7 ms 8448 KB Output is correct
36 Correct 7 ms 8320 KB Output is correct
37 Correct 7 ms 8320 KB Output is correct
38 Correct 7 ms 8320 KB Output is correct
39 Correct 8 ms 8320 KB Output is correct
40 Correct 7 ms 8320 KB Output is correct
41 Correct 6 ms 8320 KB Output is correct
42 Correct 7 ms 8320 KB Output is correct
43 Correct 7 ms 8320 KB Output is correct
44 Correct 7 ms 8320 KB Output is correct
45 Correct 9 ms 8448 KB Output is correct
46 Correct 15 ms 8704 KB Output is correct
47 Correct 16 ms 8704 KB Output is correct
48 Correct 15 ms 8704 KB Output is correct
49 Correct 11 ms 8960 KB Output is correct
50 Correct 11 ms 8960 KB Output is correct
51 Correct 11 ms 8960 KB Output is correct
52 Correct 11 ms 8960 KB Output is correct
53 Correct 12 ms 8832 KB Output is correct
54 Correct 11 ms 8832 KB Output is correct
55 Correct 11 ms 8832 KB Output is correct
56 Correct 11 ms 8832 KB Output is correct
57 Correct 19 ms 8704 KB Output is correct
58 Correct 21 ms 8704 KB Output is correct
59 Correct 21 ms 8704 KB Output is correct
60 Correct 22 ms 8824 KB Output is correct
61 Correct 13 ms 8960 KB Output is correct
62 Correct 12 ms 8832 KB Output is correct
63 Correct 11 ms 8832 KB Output is correct
64 Correct 14 ms 8704 KB Output is correct
65 Correct 15 ms 8704 KB Output is correct
66 Correct 15 ms 8704 KB Output is correct
67 Correct 16 ms 8704 KB Output is correct
68 Correct 12 ms 8960 KB Output is correct
69 Correct 11 ms 8832 KB Output is correct
70 Correct 11 ms 8832 KB Output is correct
71 Correct 11 ms 8880 KB Output is correct
72 Correct 19 ms 8704 KB Output is correct
73 Correct 21 ms 8704 KB Output is correct
74 Correct 11 ms 8832 KB Output is correct
75 Correct 11 ms 8832 KB Output is correct
76 Correct 11 ms 8832 KB Output is correct
77 Correct 12 ms 8832 KB Output is correct
78 Correct 11 ms 8832 KB Output is correct
79 Correct 12 ms 8704 KB Output is correct
80 Correct 11 ms 8736 KB Output is correct
81 Correct 12 ms 8832 KB Output is correct
82 Correct 12 ms 8832 KB Output is correct
83 Correct 12 ms 8704 KB Output is correct
84 Correct 12 ms 8832 KB Output is correct
85 Correct 12 ms 8704 KB Output is correct
86 Correct 12 ms 8704 KB Output is correct
87 Correct 33 ms 9472 KB Output is correct
88 Correct 101 ms 12024 KB Output is correct
89 Correct 404 ms 20628 KB Output is correct
90 Correct 405 ms 20604 KB Output is correct
91 Correct 398 ms 20600 KB Output is correct
92 Correct 175 ms 26616 KB Output is correct
93 Correct 170 ms 26616 KB Output is correct
94 Correct 169 ms 26616 KB Output is correct
95 Correct 189 ms 23628 KB Output is correct
96 Correct 194 ms 24048 KB Output is correct
97 Correct 196 ms 24048 KB Output is correct
98 Correct 205 ms 24048 KB Output is correct
99 Correct 185 ms 23032 KB Output is correct
100 Correct 601 ms 21112 KB Output is correct
101 Correct 666 ms 21112 KB Output is correct
102 Correct 666 ms 21256 KB Output is correct
103 Correct 684 ms 21112 KB Output is correct
104 Correct 201 ms 23164 KB Output is correct
105 Correct 191 ms 23032 KB Output is correct
106 Correct 193 ms 23032 KB Output is correct
107 Correct 351 ms 19960 KB Output is correct
108 Correct 400 ms 20088 KB Output is correct
109 Correct 404 ms 20216 KB Output is correct
110 Correct 170 ms 25976 KB Output is correct
111 Correct 185 ms 23664 KB Output is correct
112 Correct 188 ms 23560 KB Output is correct
113 Correct 168 ms 22392 KB Output is correct
114 Correct 625 ms 20728 KB Output is correct
115 Correct 696 ms 20596 KB Output is correct
116 Correct 189 ms 22392 KB Output is correct
117 Correct 177 ms 21624 KB Output is correct
118 Correct 185 ms 21112 KB Output is correct
119 Correct 182 ms 20856 KB Output is correct
120 Correct 183 ms 21112 KB Output is correct
121 Correct 186 ms 20856 KB Output is correct
122 Correct 186 ms 20728 KB Output is correct
123 Correct 201 ms 21624 KB Output is correct
124 Correct 213 ms 21240 KB Output is correct
125 Correct 208 ms 20984 KB Output is correct
126 Correct 201 ms 21240 KB Output is correct
127 Correct 204 ms 20856 KB Output is correct
128 Correct 204 ms 20728 KB Output is correct