답안 #513186

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
513186 2022-01-17T03:29:06 Z 8e7 별자리 3 (JOI20_constellation3) C++17
100 / 100
571 ms 58664 KB
//Challenge: Accepted
#include <bits/stdc++.h>
using namespace std;
void debug(){cout << endl;}
template<class T, class ... U> void debug(T a, U ... b) {cout << a << " ", debug(b...);}
template<class T> void pary(T l, T r) {
	while (l != r) cout << *l << " ", l++;
	cout << endl;
}
#define int long long
#define ll long long
#define maxn 200005
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
int a[maxn], lef[maxn], rig[maxn], anc[18][maxn], lc[maxn], rc[maxn];
vector<pii> que[maxn];
ll dp[maxn];
struct BIT{
	ll bit[maxn];
	void modify(int ind, ll val) {
		if (ind <= 0) return;
		for (;ind < maxn;ind += ind & (-ind))bit[ind] += val; 
	}
	ll query(int ind) {
		ll ret = 0;
		for (;ind > 0;ind -= ind & (-ind)) ret += bit[ind];
		return ret;
	}
	void rmod(int l, int r, ll val) { //[l, r]
		//debug("mod", l, r, val);
		modify(r+1, -val);modify(l, val);
	}
} b;
void dfs(int n, int par) {
	if (lc[n]) dfs(lc[n], n);	
	if (rc[n]) dfs(rc[n], n);
	if (lc[n]) b.rmod(n, rig[n]-1, dp[lc[n]]);
	if (rc[n]) b.rmod(lef[n]+1, n, dp[rc[n]]);
	dp[n] = b.query(n);
	for (auto p:que[n]) {
		//debug(n, p.ff, p.ss);
		dp[n] = max(dp[n], p.ss + b.query(p.ff));
	}
	//debug(n, lc[n], rc[n]);
}
signed main() {
	io
	int n;
	cin >> n;
	for (int i = 1;i <= n;i++) cin >> a[i];
	stack<int> stk;
	for (int i = 1;i <= n;i++) {
		while (stk.size() && a[i] >= a[stk.top()]) {
			rig[stk.top()] = i;
			stk.pop();
		}
		lef[i] = (stk.size() ? stk.top() : 0);
		stk.push(i);
	}
	while (stk.size()) {
		rig[stk.top()] = n+1;
		stk.pop();
	}
	int root = 0;
	for (int i = 1;i <= n;i++) {
		int le = 0, ri = 0;
		if (lef[i] != 0) le = a[lef[i]];
		if (rig[i] != n+1) ri = a[rig[i]];
		if (!le && !ri) root = i, anc[0][i] = i;
		else if (!le) anc[0][i] = rig[i];
		else if (!ri) anc[0][i] = lef[i];
		else {
			if (le <= ri) anc[0][i] = lef[i];
			else anc[0][i] = rig[i];
		}
		//debug(i, anc[0][i]);
		if (i != root) {
			if (anc[0][i] < i) rc[anc[0][i]] = i;
			else lc[anc[0][i]] = i;
		}
	}
	for (int i = 1;i < 18;i++) {
		for (int j = 1;j <= n;j++) anc[i][j] = anc[i-1][anc[i-1][j]];
	}
	int m;
	cin >> m;
	ll tot = 0;
	for (int i = 0;i < m;i++) {
		int x, y, c;
		cin >> x >> y >> c;
		tot += c;
		int to = x;
		for (int j = 17;j >= 0;j--) {
			if (a[anc[j][to]] < y) to = anc[j][to];
		}
		que[to].push_back({x, c});
		//debug(x, y, c, to);
	}
	dfs(root, 0);
	cout << tot - dp[root] << "\n";	
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5196 KB Output is correct
2 Correct 3 ms 5280 KB Output is correct
3 Correct 3 ms 5280 KB Output is correct
4 Correct 3 ms 5280 KB Output is correct
5 Correct 3 ms 5196 KB Output is correct
6 Correct 4 ms 5248 KB Output is correct
7 Correct 3 ms 5196 KB Output is correct
8 Correct 4 ms 5324 KB Output is correct
9 Correct 3 ms 5288 KB Output is correct
10 Correct 3 ms 5196 KB Output is correct
11 Correct 3 ms 5196 KB Output is correct
12 Correct 4 ms 5196 KB Output is correct
13 Correct 3 ms 5196 KB Output is correct
14 Correct 3 ms 5196 KB Output is correct
15 Correct 3 ms 5196 KB Output is correct
16 Correct 3 ms 5196 KB Output is correct
17 Correct 3 ms 5196 KB Output is correct
18 Correct 3 ms 5284 KB Output is correct
19 Correct 3 ms 5196 KB Output is correct
20 Correct 3 ms 5196 KB Output is correct
21 Correct 3 ms 5280 KB Output is correct
22 Correct 4 ms 5284 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5196 KB Output is correct
2 Correct 3 ms 5280 KB Output is correct
3 Correct 3 ms 5280 KB Output is correct
4 Correct 3 ms 5280 KB Output is correct
5 Correct 3 ms 5196 KB Output is correct
6 Correct 4 ms 5248 KB Output is correct
7 Correct 3 ms 5196 KB Output is correct
8 Correct 4 ms 5324 KB Output is correct
9 Correct 3 ms 5288 KB Output is correct
10 Correct 3 ms 5196 KB Output is correct
11 Correct 3 ms 5196 KB Output is correct
12 Correct 4 ms 5196 KB Output is correct
13 Correct 3 ms 5196 KB Output is correct
14 Correct 3 ms 5196 KB Output is correct
15 Correct 3 ms 5196 KB Output is correct
16 Correct 3 ms 5196 KB Output is correct
17 Correct 3 ms 5196 KB Output is correct
18 Correct 3 ms 5284 KB Output is correct
19 Correct 3 ms 5196 KB Output is correct
20 Correct 3 ms 5196 KB Output is correct
21 Correct 3 ms 5280 KB Output is correct
22 Correct 4 ms 5284 KB Output is correct
23 Correct 4 ms 5580 KB Output is correct
24 Correct 5 ms 5604 KB Output is correct
25 Correct 4 ms 5544 KB Output is correct
26 Correct 4 ms 5580 KB Output is correct
27 Correct 4 ms 5672 KB Output is correct
28 Correct 4 ms 5580 KB Output is correct
29 Correct 4 ms 5676 KB Output is correct
30 Correct 7 ms 5580 KB Output is correct
31 Correct 4 ms 5580 KB Output is correct
32 Correct 4 ms 5708 KB Output is correct
33 Correct 4 ms 5708 KB Output is correct
34 Correct 5 ms 5580 KB Output is correct
35 Correct 6 ms 5580 KB Output is correct
36 Correct 4 ms 5708 KB Output is correct
37 Correct 4 ms 5708 KB Output is correct
38 Correct 4 ms 5724 KB Output is correct
39 Correct 4 ms 5672 KB Output is correct
40 Correct 4 ms 5708 KB Output is correct
41 Correct 4 ms 5580 KB Output is correct
42 Correct 4 ms 5580 KB Output is correct
43 Correct 4 ms 5708 KB Output is correct
44 Correct 4 ms 5672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 5196 KB Output is correct
2 Correct 3 ms 5280 KB Output is correct
3 Correct 3 ms 5280 KB Output is correct
4 Correct 3 ms 5280 KB Output is correct
5 Correct 3 ms 5196 KB Output is correct
6 Correct 4 ms 5248 KB Output is correct
7 Correct 3 ms 5196 KB Output is correct
8 Correct 4 ms 5324 KB Output is correct
9 Correct 3 ms 5288 KB Output is correct
10 Correct 3 ms 5196 KB Output is correct
11 Correct 3 ms 5196 KB Output is correct
12 Correct 4 ms 5196 KB Output is correct
13 Correct 3 ms 5196 KB Output is correct
14 Correct 3 ms 5196 KB Output is correct
15 Correct 3 ms 5196 KB Output is correct
16 Correct 3 ms 5196 KB Output is correct
17 Correct 3 ms 5196 KB Output is correct
18 Correct 3 ms 5284 KB Output is correct
19 Correct 3 ms 5196 KB Output is correct
20 Correct 3 ms 5196 KB Output is correct
21 Correct 3 ms 5280 KB Output is correct
22 Correct 4 ms 5284 KB Output is correct
23 Correct 4 ms 5580 KB Output is correct
24 Correct 5 ms 5604 KB Output is correct
25 Correct 4 ms 5544 KB Output is correct
26 Correct 4 ms 5580 KB Output is correct
27 Correct 4 ms 5672 KB Output is correct
28 Correct 4 ms 5580 KB Output is correct
29 Correct 4 ms 5676 KB Output is correct
30 Correct 7 ms 5580 KB Output is correct
31 Correct 4 ms 5580 KB Output is correct
32 Correct 4 ms 5708 KB Output is correct
33 Correct 4 ms 5708 KB Output is correct
34 Correct 5 ms 5580 KB Output is correct
35 Correct 6 ms 5580 KB Output is correct
36 Correct 4 ms 5708 KB Output is correct
37 Correct 4 ms 5708 KB Output is correct
38 Correct 4 ms 5724 KB Output is correct
39 Correct 4 ms 5672 KB Output is correct
40 Correct 4 ms 5708 KB Output is correct
41 Correct 4 ms 5580 KB Output is correct
42 Correct 4 ms 5580 KB Output is correct
43 Correct 4 ms 5708 KB Output is correct
44 Correct 4 ms 5672 KB Output is correct
45 Correct 563 ms 54528 KB Output is correct
46 Correct 528 ms 54008 KB Output is correct
47 Correct 571 ms 53568 KB Output is correct
48 Correct 556 ms 54336 KB Output is correct
49 Correct 546 ms 53060 KB Output is correct
50 Correct 543 ms 52980 KB Output is correct
51 Correct 555 ms 53068 KB Output is correct
52 Correct 556 ms 54276 KB Output is correct
53 Correct 569 ms 54080 KB Output is correct
54 Correct 549 ms 58664 KB Output is correct
55 Correct 562 ms 56504 KB Output is correct
56 Correct 552 ms 55320 KB Output is correct
57 Correct 560 ms 54684 KB Output is correct
58 Correct 397 ms 55400 KB Output is correct
59 Correct 372 ms 55344 KB Output is correct
60 Correct 124 ms 58052 KB Output is correct
61 Correct 485 ms 53552 KB Output is correct
62 Correct 570 ms 57728 KB Output is correct
63 Correct 491 ms 52784 KB Output is correct
64 Correct 466 ms 51928 KB Output is correct
65 Correct 536 ms 58060 KB Output is correct
66 Correct 456 ms 52308 KB Output is correct