답안 #56326

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
56326 2018-07-11T05:11:07 Z 윤교준(#1600) Construction of Highway (JOI18_construction) C++11
100 / 100
967 ms 84040 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 9 ms 8312 KB Output is correct
2 Correct 8 ms 8312 KB Output is correct
3 Correct 10 ms 8380 KB Output is correct
4 Correct 9 ms 8588 KB Output is correct
5 Correct 11 ms 8588 KB Output is correct
6 Correct 9 ms 8588 KB Output is correct
7 Correct 11 ms 8588 KB Output is correct
8 Correct 9 ms 8636 KB Output is correct
9 Correct 12 ms 8636 KB Output is correct
10 Correct 12 ms 8636 KB Output is correct
11 Correct 11 ms 8636 KB Output is correct
12 Correct 8 ms 8640 KB Output is correct
13 Correct 10 ms 8652 KB Output is correct
14 Correct 10 ms 8652 KB Output is correct
15 Correct 10 ms 8652 KB Output is correct
16 Correct 19 ms 8652 KB Output is correct
17 Correct 11 ms 8704 KB Output is correct
18 Correct 11 ms 8704 KB Output is correct
19 Correct 11 ms 8704 KB Output is correct
20 Correct 11 ms 8704 KB Output is correct
21 Correct 11 ms 8704 KB Output is correct
22 Correct 10 ms 8816 KB Output is correct
23 Correct 11 ms 8816 KB Output is correct
24 Correct 11 ms 8816 KB Output is correct
25 Correct 13 ms 8816 KB Output is correct
26 Correct 12 ms 8816 KB Output is correct
27 Correct 10 ms 8816 KB Output is correct
28 Correct 19 ms 8816 KB Output is correct
29 Correct 11 ms 8816 KB Output is correct
30 Correct 12 ms 8816 KB Output is correct
31 Correct 10 ms 8816 KB Output is correct
32 Correct 10 ms 8816 KB Output is correct
33 Correct 10 ms 8816 KB Output is correct
34 Correct 11 ms 8816 KB Output is correct
35 Correct 11 ms 8816 KB Output is correct
36 Correct 12 ms 8816 KB Output is correct
37 Correct 52 ms 8816 KB Output is correct
38 Correct 11 ms 8816 KB Output is correct
39 Correct 10 ms 8816 KB Output is correct
40 Correct 16 ms 8816 KB Output is correct
41 Correct 11 ms 8816 KB Output is correct
42 Correct 10 ms 8816 KB Output is correct
43 Correct 10 ms 8816 KB Output is correct
44 Correct 10 ms 8816 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 8312 KB Output is correct
2 Correct 8 ms 8312 KB Output is correct
3 Correct 10 ms 8380 KB Output is correct
4 Correct 9 ms 8588 KB Output is correct
5 Correct 11 ms 8588 KB Output is correct
6 Correct 9 ms 8588 KB Output is correct
7 Correct 11 ms 8588 KB Output is correct
8 Correct 9 ms 8636 KB Output is correct
9 Correct 12 ms 8636 KB Output is correct
10 Correct 12 ms 8636 KB Output is correct
11 Correct 11 ms 8636 KB Output is correct
12 Correct 8 ms 8640 KB Output is correct
13 Correct 10 ms 8652 KB Output is correct
14 Correct 10 ms 8652 KB Output is correct
15 Correct 10 ms 8652 KB Output is correct
16 Correct 19 ms 8652 KB Output is correct
17 Correct 11 ms 8704 KB Output is correct
18 Correct 11 ms 8704 KB Output is correct
19 Correct 11 ms 8704 KB Output is correct
20 Correct 11 ms 8704 KB Output is correct
21 Correct 11 ms 8704 KB Output is correct
22 Correct 10 ms 8816 KB Output is correct
23 Correct 11 ms 8816 KB Output is correct
24 Correct 11 ms 8816 KB Output is correct
25 Correct 13 ms 8816 KB Output is correct
26 Correct 12 ms 8816 KB Output is correct
27 Correct 10 ms 8816 KB Output is correct
28 Correct 19 ms 8816 KB Output is correct
29 Correct 11 ms 8816 KB Output is correct
30 Correct 12 ms 8816 KB Output is correct
31 Correct 10 ms 8816 KB Output is correct
32 Correct 10 ms 8816 KB Output is correct
33 Correct 10 ms 8816 KB Output is correct
34 Correct 11 ms 8816 KB Output is correct
35 Correct 11 ms 8816 KB Output is correct
36 Correct 12 ms 8816 KB Output is correct
37 Correct 52 ms 8816 KB Output is correct
38 Correct 11 ms 8816 KB Output is correct
39 Correct 10 ms 8816 KB Output is correct
40 Correct 16 ms 8816 KB Output is correct
41 Correct 11 ms 8816 KB Output is correct
42 Correct 10 ms 8816 KB Output is correct
43 Correct 10 ms 8816 KB Output is correct
44 Correct 10 ms 8816 KB Output is correct
45 Correct 13 ms 8816 KB Output is correct
46 Correct 21 ms 9072 KB Output is correct
47 Correct 30 ms 9072 KB Output is correct
48 Correct 23 ms 9216 KB Output is correct
49 Correct 15 ms 9360 KB Output is correct
50 Correct 18 ms 9360 KB Output is correct
51 Correct 20 ms 9360 KB Output is correct
52 Correct 16 ms 9360 KB Output is correct
53 Correct 15 ms 9384 KB Output is correct
54 Correct 18 ms 9384 KB Output is correct
55 Correct 16 ms 9384 KB Output is correct
56 Correct 20 ms 9384 KB Output is correct
57 Correct 24 ms 9384 KB Output is correct
58 Correct 34 ms 9384 KB Output is correct
59 Correct 29 ms 9384 KB Output is correct
60 Correct 31 ms 9384 KB Output is correct
61 Correct 18 ms 9384 KB Output is correct
62 Correct 22 ms 9384 KB Output is correct
63 Correct 15 ms 9384 KB Output is correct
64 Correct 24 ms 9384 KB Output is correct
65 Correct 29 ms 9384 KB Output is correct
66 Correct 30 ms 9384 KB Output is correct
67 Correct 20 ms 9384 KB Output is correct
68 Correct 15 ms 9384 KB Output is correct
69 Correct 15 ms 9384 KB Output is correct
70 Correct 15 ms 9384 KB Output is correct
71 Correct 18 ms 9384 KB Output is correct
72 Correct 23 ms 9384 KB Output is correct
73 Correct 23 ms 9384 KB Output is correct
74 Correct 18 ms 9384 KB Output is correct
75 Correct 15 ms 9384 KB Output is correct
76 Correct 18 ms 9384 KB Output is correct
77 Correct 15 ms 9384 KB Output is correct
78 Correct 17 ms 9384 KB Output is correct
79 Correct 15 ms 9384 KB Output is correct
80 Correct 17 ms 9384 KB Output is correct
81 Correct 17 ms 9384 KB Output is correct
82 Correct 17 ms 9384 KB Output is correct
83 Correct 22 ms 9384 KB Output is correct
84 Correct 13 ms 9384 KB Output is correct
85 Correct 19 ms 9384 KB Output is correct
86 Correct 16 ms 9384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 8312 KB Output is correct
2 Correct 8 ms 8312 KB Output is correct
3 Correct 10 ms 8380 KB Output is correct
4 Correct 9 ms 8588 KB Output is correct
5 Correct 11 ms 8588 KB Output is correct
6 Correct 9 ms 8588 KB Output is correct
7 Correct 11 ms 8588 KB Output is correct
8 Correct 9 ms 8636 KB Output is correct
9 Correct 12 ms 8636 KB Output is correct
10 Correct 12 ms 8636 KB Output is correct
11 Correct 11 ms 8636 KB Output is correct
12 Correct 8 ms 8640 KB Output is correct
13 Correct 10 ms 8652 KB Output is correct
14 Correct 10 ms 8652 KB Output is correct
15 Correct 10 ms 8652 KB Output is correct
16 Correct 19 ms 8652 KB Output is correct
17 Correct 11 ms 8704 KB Output is correct
18 Correct 11 ms 8704 KB Output is correct
19 Correct 11 ms 8704 KB Output is correct
20 Correct 11 ms 8704 KB Output is correct
21 Correct 11 ms 8704 KB Output is correct
22 Correct 10 ms 8816 KB Output is correct
23 Correct 11 ms 8816 KB Output is correct
24 Correct 11 ms 8816 KB Output is correct
25 Correct 13 ms 8816 KB Output is correct
26 Correct 12 ms 8816 KB Output is correct
27 Correct 10 ms 8816 KB Output is correct
28 Correct 19 ms 8816 KB Output is correct
29 Correct 11 ms 8816 KB Output is correct
30 Correct 12 ms 8816 KB Output is correct
31 Correct 10 ms 8816 KB Output is correct
32 Correct 10 ms 8816 KB Output is correct
33 Correct 10 ms 8816 KB Output is correct
34 Correct 11 ms 8816 KB Output is correct
35 Correct 11 ms 8816 KB Output is correct
36 Correct 12 ms 8816 KB Output is correct
37 Correct 52 ms 8816 KB Output is correct
38 Correct 11 ms 8816 KB Output is correct
39 Correct 10 ms 8816 KB Output is correct
40 Correct 16 ms 8816 KB Output is correct
41 Correct 11 ms 8816 KB Output is correct
42 Correct 10 ms 8816 KB Output is correct
43 Correct 10 ms 8816 KB Output is correct
44 Correct 10 ms 8816 KB Output is correct
45 Correct 13 ms 8816 KB Output is correct
46 Correct 21 ms 9072 KB Output is correct
47 Correct 30 ms 9072 KB Output is correct
48 Correct 23 ms 9216 KB Output is correct
49 Correct 15 ms 9360 KB Output is correct
50 Correct 18 ms 9360 KB Output is correct
51 Correct 20 ms 9360 KB Output is correct
52 Correct 16 ms 9360 KB Output is correct
53 Correct 15 ms 9384 KB Output is correct
54 Correct 18 ms 9384 KB Output is correct
55 Correct 16 ms 9384 KB Output is correct
56 Correct 20 ms 9384 KB Output is correct
57 Correct 24 ms 9384 KB Output is correct
58 Correct 34 ms 9384 KB Output is correct
59 Correct 29 ms 9384 KB Output is correct
60 Correct 31 ms 9384 KB Output is correct
61 Correct 18 ms 9384 KB Output is correct
62 Correct 22 ms 9384 KB Output is correct
63 Correct 15 ms 9384 KB Output is correct
64 Correct 24 ms 9384 KB Output is correct
65 Correct 29 ms 9384 KB Output is correct
66 Correct 30 ms 9384 KB Output is correct
67 Correct 20 ms 9384 KB Output is correct
68 Correct 15 ms 9384 KB Output is correct
69 Correct 15 ms 9384 KB Output is correct
70 Correct 15 ms 9384 KB Output is correct
71 Correct 18 ms 9384 KB Output is correct
72 Correct 23 ms 9384 KB Output is correct
73 Correct 23 ms 9384 KB Output is correct
74 Correct 18 ms 9384 KB Output is correct
75 Correct 15 ms 9384 KB Output is correct
76 Correct 18 ms 9384 KB Output is correct
77 Correct 15 ms 9384 KB Output is correct
78 Correct 17 ms 9384 KB Output is correct
79 Correct 15 ms 9384 KB Output is correct
80 Correct 17 ms 9384 KB Output is correct
81 Correct 17 ms 9384 KB Output is correct
82 Correct 17 ms 9384 KB Output is correct
83 Correct 22 ms 9384 KB Output is correct
84 Correct 13 ms 9384 KB Output is correct
85 Correct 19 ms 9384 KB Output is correct
86 Correct 16 ms 9384 KB Output is correct
87 Correct 44 ms 9768 KB Output is correct
88 Correct 117 ms 11836 KB Output is correct
89 Correct 584 ms 19100 KB Output is correct
90 Correct 581 ms 19100 KB Output is correct
91 Correct 578 ms 19164 KB Output is correct
92 Correct 305 ms 25060 KB Output is correct
93 Correct 250 ms 27068 KB Output is correct
94 Correct 292 ms 29080 KB Output is correct
95 Correct 249 ms 29080 KB Output is correct
96 Correct 310 ms 30544 KB Output is correct
97 Correct 350 ms 32660 KB Output is correct
98 Correct 300 ms 34972 KB Output is correct
99 Correct 337 ms 35844 KB Output is correct
100 Correct 835 ms 35908 KB Output is correct
101 Correct 967 ms 37784 KB Output is correct
102 Correct 930 ms 39916 KB Output is correct
103 Correct 945 ms 42052 KB Output is correct
104 Correct 254 ms 46108 KB Output is correct
105 Correct 210 ms 48228 KB Output is correct
106 Correct 271 ms 50184 KB Output is correct
107 Correct 511 ms 50184 KB Output is correct
108 Correct 415 ms 50700 KB Output is correct
109 Correct 560 ms 52524 KB Output is correct
110 Correct 233 ms 59872 KB Output is correct
111 Correct 259 ms 59872 KB Output is correct
112 Correct 249 ms 60496 KB Output is correct
113 Correct 232 ms 61068 KB Output is correct
114 Correct 783 ms 61068 KB Output is correct
115 Correct 865 ms 62368 KB Output is correct
116 Correct 293 ms 65736 KB Output is correct
117 Correct 239 ms 66344 KB Output is correct
118 Correct 243 ms 67860 KB Output is correct
119 Correct 276 ms 69500 KB Output is correct
120 Correct 257 ms 71576 KB Output is correct
121 Correct 273 ms 72732 KB Output is correct
122 Correct 267 ms 74012 KB Output is correct
123 Correct 335 ms 76536 KB Output is correct
124 Correct 309 ms 77896 KB Output is correct
125 Correct 301 ms 79552 KB Output is correct
126 Correct 293 ms 81664 KB Output is correct
127 Correct 309 ms 82784 KB Output is correct
128 Correct 281 ms 84040 KB Output is correct