답안 #470548

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
470548 2021-09-04T08:16:19 Z Namnamseo 별자리 3 (JOI20_constellation3) C++17
100 / 100
594 ms 47944 KB
#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
using ll=long long;
using pp=pair<int,int>;
const int maxn = int(2e5) + 10;

int n;
int a[maxn];

pair<pp,int> gnum[maxn];

namespace {
	const int M = 262144;
	pp t[M<<1];

	void build() {
		for (int i=1; i<=n; ++i) t[M+i] = {a[i], i};
		for (int i=M-1; 1<=i; --i) t[i] = max(t[i*2], t[i*2+1]);
	}

	int maxi(int l, int r) {
		pp mx{};
		for (l+=M, r+=M; l<=r; l/=2, r/=2) {
			if (l%2 == 1) mx = max(mx, t[l++]);
			if (r%2 == 0) mx = max(mx, t[r--]);
		}
		return mx.second;
	}

	int wrap_v(int x, int y) {
		int l = x, r = x;
		static int vs[40], vn;
		auto Enum = [&](int l, int r) {
			static int vl[20], vr[20];
			int vln = 0, vrn = 0;
			for (l+=M, r+=M; l<=r; l/=2, r/=2) {
				if (l%2 == 1) vl[vln++] = l++;
				if (r%2 == 0) vr[vrn++] = r--;
			}
			vn = 0;
			copy(vl, vl+vln, vs); vn += vln;
			for (int i=vrn-1; 0<=i; --i) vs[vn++] = vr[i];
		};
		Enum(1, l-1);
		int lok = vn; while (lok && t[vs[lok-1]].first < y) --lok;
		if (lok-1 >= 0) {
			for (l = vs[lok-1]; l<M;) {
				if (t[l*2+1].first < y) l *= 2;
				else l = l*2+1;
			}
			l = l+1 - M;
		} else l = 1;
		Enum(r+1, n);
		lok = -1; while (lok+1<vn && t[vs[lok+1]].first < y) ++lok;
		if (lok+1 < vn) {
			for (r = vs[lok+1]; r<M;) {
				if (t[r*2].first < y) r = r*2+1;
				else r *= 2;
			}
			r = r-1 - M;
		} else r = n;
		return lower_bound(gnum, gnum+n, pair<pp, int>{{l, r}, -1})->second;
	}
}

vector<int> te[maxn];
int root;
vector<pp> paths[maxn];
int gdfs(int l, int r) {
	static int gn;
	int me = ++gn; gnum[gn-1] = {{l, r}, me};
	if (l == r) return me;
	int i = maxi(l, r);
	if (l < i) te[me].push_back(gdfs(l, i-1));
	if (i < r) te[me].push_back(gdfs(i+1, r));
	return me;
}

int tin[maxn], tout[maxn], nt;
void tdfs1(int x) {
	tin[x] = ++nt; for (int y : te[x]) tdfs1(y); tout[x] = nt;
}

ll dp[maxn];
struct It {
	const static int M = 262144;
	ll t[M<<1];
	void upd(int l, int r, ll v) {
		for (l+=M, r+=M; l<=r; l/=2, r/=2) {
			if (l%2==1) t[l++] += v;
			if (r%2==0) t[r--] += v;
		}
	}
	ll get(int p) { ll ret = 0;
		for (p+=M; p; p/=2) ret += t[p];
		return ret;
	}
} giveup;

void tdfs2(int x) {
	ll cds = 0;
	for (int y:te[x]) tdfs2(y), cds += dp[y];
	ll mdf = 0;
	for (auto &tmp:paths[x]) {
		int to, c; tie(to, c) = tmp;
		mdf = max(mdf, c + giveup.get(tin[to]));
	}
	dp[x] = cds + mdf;
	giveup.upd(tin[x], tout[x], -mdf);
}

int main() { cin.tie(0)->sync_with_stdio(0);
	cin >> n; for (int i=1; i<=n; ++i) cin >> a[i];
	build();

	ll csum = 0;
	root = gdfs(1, n);
	sort(gnum, gnum+n);
	{ int m; cin >> m;
	for (;m--;) {
		int x, y, c; cin >> x >> y >> c; csum += c;
		int up = wrap_v(x, y), down = wrap_v(x, a[x]+1);
		paths[up].push_back({down, c});
	} }

	tdfs1(root);
	tdfs2(root);

	ll ans = csum - dp[root];
	cout << ans << '\n';

	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 11860 KB Output is correct
2 Correct 7 ms 11780 KB Output is correct
3 Correct 7 ms 11808 KB Output is correct
4 Correct 9 ms 11860 KB Output is correct
5 Correct 8 ms 11852 KB Output is correct
6 Correct 8 ms 11852 KB Output is correct
7 Correct 8 ms 11868 KB Output is correct
8 Correct 7 ms 11852 KB Output is correct
9 Correct 7 ms 11852 KB Output is correct
10 Correct 7 ms 11852 KB Output is correct
11 Correct 7 ms 11852 KB Output is correct
12 Correct 8 ms 11752 KB Output is correct
13 Correct 7 ms 11852 KB Output is correct
14 Correct 7 ms 11852 KB Output is correct
15 Correct 7 ms 11852 KB Output is correct
16 Correct 8 ms 11840 KB Output is correct
17 Correct 7 ms 11812 KB Output is correct
18 Correct 7 ms 11856 KB Output is correct
19 Correct 7 ms 11852 KB Output is correct
20 Correct 8 ms 11852 KB Output is correct
21 Correct 8 ms 11852 KB Output is correct
22 Correct 7 ms 11852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 11860 KB Output is correct
2 Correct 7 ms 11780 KB Output is correct
3 Correct 7 ms 11808 KB Output is correct
4 Correct 9 ms 11860 KB Output is correct
5 Correct 8 ms 11852 KB Output is correct
6 Correct 8 ms 11852 KB Output is correct
7 Correct 8 ms 11868 KB Output is correct
8 Correct 7 ms 11852 KB Output is correct
9 Correct 7 ms 11852 KB Output is correct
10 Correct 7 ms 11852 KB Output is correct
11 Correct 7 ms 11852 KB Output is correct
12 Correct 8 ms 11752 KB Output is correct
13 Correct 7 ms 11852 KB Output is correct
14 Correct 7 ms 11852 KB Output is correct
15 Correct 7 ms 11852 KB Output is correct
16 Correct 8 ms 11840 KB Output is correct
17 Correct 7 ms 11812 KB Output is correct
18 Correct 7 ms 11856 KB Output is correct
19 Correct 7 ms 11852 KB Output is correct
20 Correct 8 ms 11852 KB Output is correct
21 Correct 8 ms 11852 KB Output is correct
22 Correct 7 ms 11852 KB Output is correct
23 Correct 10 ms 12040 KB Output is correct
24 Correct 10 ms 11976 KB Output is correct
25 Correct 11 ms 11980 KB Output is correct
26 Correct 10 ms 11980 KB Output is correct
27 Correct 11 ms 12044 KB Output is correct
28 Correct 11 ms 12040 KB Output is correct
29 Correct 10 ms 11980 KB Output is correct
30 Correct 10 ms 11936 KB Output is correct
31 Correct 11 ms 11980 KB Output is correct
32 Correct 10 ms 12108 KB Output is correct
33 Correct 10 ms 12080 KB Output is correct
34 Correct 10 ms 12068 KB Output is correct
35 Correct 11 ms 11980 KB Output is correct
36 Correct 10 ms 12104 KB Output is correct
37 Correct 10 ms 12100 KB Output is correct
38 Correct 10 ms 12108 KB Output is correct
39 Correct 10 ms 11980 KB Output is correct
40 Correct 10 ms 12116 KB Output is correct
41 Correct 14 ms 12044 KB Output is correct
42 Correct 11 ms 11980 KB Output is correct
43 Correct 11 ms 12120 KB Output is correct
44 Correct 11 ms 12048 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 11860 KB Output is correct
2 Correct 7 ms 11780 KB Output is correct
3 Correct 7 ms 11808 KB Output is correct
4 Correct 9 ms 11860 KB Output is correct
5 Correct 8 ms 11852 KB Output is correct
6 Correct 8 ms 11852 KB Output is correct
7 Correct 8 ms 11868 KB Output is correct
8 Correct 7 ms 11852 KB Output is correct
9 Correct 7 ms 11852 KB Output is correct
10 Correct 7 ms 11852 KB Output is correct
11 Correct 7 ms 11852 KB Output is correct
12 Correct 8 ms 11752 KB Output is correct
13 Correct 7 ms 11852 KB Output is correct
14 Correct 7 ms 11852 KB Output is correct
15 Correct 7 ms 11852 KB Output is correct
16 Correct 8 ms 11840 KB Output is correct
17 Correct 7 ms 11812 KB Output is correct
18 Correct 7 ms 11856 KB Output is correct
19 Correct 7 ms 11852 KB Output is correct
20 Correct 8 ms 11852 KB Output is correct
21 Correct 8 ms 11852 KB Output is correct
22 Correct 7 ms 11852 KB Output is correct
23 Correct 10 ms 12040 KB Output is correct
24 Correct 10 ms 11976 KB Output is correct
25 Correct 11 ms 11980 KB Output is correct
26 Correct 10 ms 11980 KB Output is correct
27 Correct 11 ms 12044 KB Output is correct
28 Correct 11 ms 12040 KB Output is correct
29 Correct 10 ms 11980 KB Output is correct
30 Correct 10 ms 11936 KB Output is correct
31 Correct 11 ms 11980 KB Output is correct
32 Correct 10 ms 12108 KB Output is correct
33 Correct 10 ms 12080 KB Output is correct
34 Correct 10 ms 12068 KB Output is correct
35 Correct 11 ms 11980 KB Output is correct
36 Correct 10 ms 12104 KB Output is correct
37 Correct 10 ms 12100 KB Output is correct
38 Correct 10 ms 12108 KB Output is correct
39 Correct 10 ms 11980 KB Output is correct
40 Correct 10 ms 12116 KB Output is correct
41 Correct 14 ms 12044 KB Output is correct
42 Correct 11 ms 11980 KB Output is correct
43 Correct 11 ms 12120 KB Output is correct
44 Correct 11 ms 12048 KB Output is correct
45 Correct 443 ms 30476 KB Output is correct
46 Correct 450 ms 30232 KB Output is correct
47 Correct 442 ms 30120 KB Output is correct
48 Correct 434 ms 30516 KB Output is correct
49 Correct 427 ms 29904 KB Output is correct
50 Correct 440 ms 29924 KB Output is correct
51 Correct 433 ms 30052 KB Output is correct
52 Correct 444 ms 30644 KB Output is correct
53 Correct 436 ms 30404 KB Output is correct
54 Correct 582 ms 40644 KB Output is correct
55 Correct 543 ms 37188 KB Output is correct
56 Correct 555 ms 35388 KB Output is correct
57 Correct 554 ms 34080 KB Output is correct
58 Correct 488 ms 38084 KB Output is correct
59 Correct 515 ms 37956 KB Output is correct
60 Correct 286 ms 47944 KB Output is correct
61 Correct 437 ms 30236 KB Output is correct
62 Correct 594 ms 42532 KB Output is correct
63 Correct 483 ms 29892 KB Output is correct
64 Correct 436 ms 29564 KB Output is correct
65 Correct 573 ms 42828 KB Output is correct
66 Correct 480 ms 29552 KB Output is correct