답안 #521611

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
521611 2022-02-02T14:38:54 Z eecs Worst Reporter 4 (JOI21_worst_reporter4) C++17
100 / 100
506 ms 164596 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int maxn = 200010;
int n, tot, a[maxn], h[maxn], c[maxn], vis[maxn], foo[maxn], rt[maxn];
ll all, ans;
vector<int> G[maxn], bel[maxn];
struct node { int l, r; ll mx, tag; } t[maxn * 100];
unordered_map<int, ll> mp;

#define mid (l + r >> 1)
inline void maintain(int k) {
	t[k].mx = max(t[t[k].l].mx, t[t[k].r].mx);
}

void pushdown(int k) {
	if (!t[k].tag) return;
	if (!t[k].l) t[k].l = ++tot;
	t[t[k].l].tag += t[k].tag, t[t[k].l].mx += t[k].tag;
	if (!t[k].r) t[k].r = ++tot;
	t[t[k].r].tag += t[k].tag, t[t[k].r].mx += t[k].tag;
	t[k].tag = 0;
}

int merge(int k1, int k2, int l, int r, ll mx1, ll mx2) {
	if (!k1 && !k2) return 0;
	if (!k2) { t[k1].tag += mx2, t[k1].mx += mx2; return k1; }
	if (!k1) { t[k2].tag += mx1, t[k2].mx += mx1; return k2; }
	if (l == r) {
		t[k1].mx = max({t[k1].mx + mx2, t[k2].mx + mx1, t[k1].mx + t[k2].mx});
		return k1;
	}
	if (!t[k1].l && !t[k1].r) {
		mx1 = max(mx1, t[k1].mx);
		t[k2].tag += mx1, t[k2].mx += mx1;
		return k2;
	}
	if (!t[k2].l && !t[k2].r) {
		mx2 = max(mx2, t[k2].mx);
		t[k1].tag += mx2, t[k1].mx += mx2;
		return k1;
	}
	pushdown(k1), pushdown(k2);
	t[k1].l = merge(t[k1].l, t[k2].l, l, mid, max(mx1, t[t[k1].r].mx) + t[k1].tag,
		max(mx2, t[t[k2].r].mx) + t[k2].tag);
	t[k1].r = merge(t[k1].r, t[k2].r, mid + 1, r, mx1, mx2);
	maintain(k1); return k1;
}

void modify(int &k, int l, int r, int p, ll v) {
	if (!k) k = ++tot;
	if (l == r) { t[k].mx = max(t[k].mx, v); return; }
	pushdown(k);
	if (mid >= p) modify(t[k].l, l, mid, p, v);
	else modify(t[k].r, mid + 1, r, p, v);
	maintain(k);
}

ll query(int k, int l, int r, int ql, int qr) {
	if (!k || l >= ql && r <= qr) return t[k].mx;
	pushdown(k);
	ll ans = 0;
	if (mid >= ql) ans = query(t[k].l, l, mid, ql, qr);
	if (mid < qr) ans = max(ans, query(t[k].r, mid + 1, r, ql, qr));
	return ans;
}

int find(int x) {
	return x == foo[x] ? x : foo[x] = find(foo[x]);
}

int main() {
	scanf("%d", &n);
	vector<int> V;
	iota(foo + 1, foo + n + 1, 1);
	for (int i = 1; i <= n; i++) {
		scanf("%d %d %d", &a[i], &h[i], &c[i]);
		all += c[i], V.push_back(h[i]);
		foo[find(i)] = find(a[i]);
	}
	sort(V.begin(), V.end());
	V.resize(unique(V.begin(), V.end()) - V.begin());
	for (int i = 1; i <= n; i++) {
		h[i] = lower_bound(V.begin(), V.end(), h[i]) - V.begin() + 1;
		bel[find(i)].push_back(i);
	}
	for (int i = 1; i <= n; i++) if (i == find(i)) {
		int tim = 0, tr = 0;
		V.clear(), mp.clear();
		int l, r;
		for (int j = i; ; j = a[j]) {
			if (vis[j]) { l = vis[j], r = tim; break; }
			vis[j] = ++tim;
		}
		for (int j : bel[i]) {
			if (vis[j] >= l && vis[j] <= r) V.push_back(j), mp[h[j]] += c[j];
			else G[a[j]].push_back(j);
		}
		function<void(int)> dfs = [&](int v) {
			for (int u : G[v]) {
				dfs(u), rt[v] = ::merge(rt[v], rt[u], 1, n, 0, 0);
			}
			modify(rt[v], 1, n, h[v], c[v] + query(rt[v], 1, n, h[v], n));
		};
		for (int j : V) for (int k : G[j]) {
			dfs(k), tr = ::merge(tr, rt[k], 1, n, 0, 0);
		}
		ll bar = t[tr].mx;
		for (auto p : mp) {
			bar = max(bar, p.second + query(tr, 1, n, p.first, n));
		}
		ans += bar;
	}
	printf("%lld\n", all - ans);
	return 0;
}

Compilation message

worst_reporter2.cpp: In function 'int merge(int, int, int, int, ll, ll)':
worst_reporter2.cpp:12:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   12 | #define mid (l + r >> 1)
      |              ~~^~~
worst_reporter2.cpp:45:39: note: in expansion of macro 'mid'
   45 |  t[k1].l = merge(t[k1].l, t[k2].l, l, mid, max(mx1, t[t[k1].r].mx) + t[k1].tag,
      |                                       ^~~
worst_reporter2.cpp:12:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   12 | #define mid (l + r >> 1)
      |              ~~^~~
worst_reporter2.cpp:47:36: note: in expansion of macro 'mid'
   47 |  t[k1].r = merge(t[k1].r, t[k2].r, mid + 1, r, mx1, mx2);
      |                                    ^~~
worst_reporter2.cpp: In function 'void modify(int&, int, int, int, ll)':
worst_reporter2.cpp:12:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   12 | #define mid (l + r >> 1)
      |              ~~^~~
worst_reporter2.cpp:55:6: note: in expansion of macro 'mid'
   55 |  if (mid >= p) modify(t[k].l, l, mid, p, v);
      |      ^~~
worst_reporter2.cpp:12:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   12 | #define mid (l + r >> 1)
      |              ~~^~~
worst_reporter2.cpp:55:34: note: in expansion of macro 'mid'
   55 |  if (mid >= p) modify(t[k].l, l, mid, p, v);
      |                                  ^~~
worst_reporter2.cpp:12:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   12 | #define mid (l + r >> 1)
      |              ~~^~~
worst_reporter2.cpp:56:22: note: in expansion of macro 'mid'
   56 |  else modify(t[k].r, mid + 1, r, p, v);
      |                      ^~~
worst_reporter2.cpp: In function 'll query(int, int, int, int, int)':
worst_reporter2.cpp:61:20: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   61 |  if (!k || l >= ql && r <= qr) return t[k].mx;
      |            ~~~~~~~~^~~~~~~~~~
worst_reporter2.cpp:12:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   12 | #define mid (l + r >> 1)
      |              ~~^~~
worst_reporter2.cpp:64:6: note: in expansion of macro 'mid'
   64 |  if (mid >= ql) ans = query(t[k].l, l, mid, ql, qr);
      |      ^~~
worst_reporter2.cpp:12:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   12 | #define mid (l + r >> 1)
      |              ~~^~~
worst_reporter2.cpp:64:40: note: in expansion of macro 'mid'
   64 |  if (mid >= ql) ans = query(t[k].l, l, mid, ql, qr);
      |                                        ^~~
worst_reporter2.cpp:12:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   12 | #define mid (l + r >> 1)
      |              ~~^~~
worst_reporter2.cpp:65:6: note: in expansion of macro 'mid'
   65 |  if (mid < qr) ans = max(ans, query(t[k].r, mid + 1, r, ql, qr));
      |      ^~~
worst_reporter2.cpp:12:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   12 | #define mid (l + r >> 1)
      |              ~~^~~
worst_reporter2.cpp:65:45: note: in expansion of macro 'mid'
   65 |  if (mid < qr) ans = max(ans, query(t[k].r, mid + 1, r, ql, qr));
      |                                             ^~~
worst_reporter2.cpp: In function 'int main()':
worst_reporter2.cpp:74:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   74 |  scanf("%d", &n);
      |  ~~~~~^~~~~~~~~~
worst_reporter2.cpp:78:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   78 |   scanf("%d %d %d", &a[i], &h[i], &c[i]);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9704 KB Output is correct
2 Correct 5 ms 9676 KB Output is correct
3 Correct 6 ms 9716 KB Output is correct
4 Correct 5 ms 9620 KB Output is correct
5 Correct 12 ms 12276 KB Output is correct
6 Correct 11 ms 11248 KB Output is correct
7 Correct 10 ms 10872 KB Output is correct
8 Correct 13 ms 12284 KB Output is correct
9 Correct 10 ms 11340 KB Output is correct
10 Correct 12 ms 10868 KB Output is correct
11 Correct 9 ms 10796 KB Output is correct
12 Correct 10 ms 10700 KB Output is correct
13 Correct 9 ms 10480 KB Output is correct
14 Correct 10 ms 10668 KB Output is correct
15 Correct 9 ms 10272 KB Output is correct
16 Correct 12 ms 12272 KB Output is correct
17 Correct 10 ms 11376 KB Output is correct
18 Correct 9 ms 10828 KB Output is correct
19 Correct 10 ms 11212 KB Output is correct
20 Correct 13 ms 10996 KB Output is correct
21 Correct 9 ms 10956 KB Output is correct
22 Correct 11 ms 11636 KB Output is correct
23 Correct 12 ms 11624 KB Output is correct
24 Correct 11 ms 11116 KB Output is correct
25 Correct 10 ms 10996 KB Output is correct
26 Correct 10 ms 10740 KB Output is correct
27 Correct 10 ms 11304 KB Output is correct
28 Correct 11 ms 11088 KB Output is correct
29 Correct 9 ms 10828 KB Output is correct
30 Correct 9 ms 10576 KB Output is correct
31 Correct 10 ms 10504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9704 KB Output is correct
2 Correct 5 ms 9676 KB Output is correct
3 Correct 6 ms 9716 KB Output is correct
4 Correct 5 ms 9620 KB Output is correct
5 Correct 12 ms 12276 KB Output is correct
6 Correct 11 ms 11248 KB Output is correct
7 Correct 10 ms 10872 KB Output is correct
8 Correct 13 ms 12284 KB Output is correct
9 Correct 10 ms 11340 KB Output is correct
10 Correct 12 ms 10868 KB Output is correct
11 Correct 9 ms 10796 KB Output is correct
12 Correct 10 ms 10700 KB Output is correct
13 Correct 9 ms 10480 KB Output is correct
14 Correct 10 ms 10668 KB Output is correct
15 Correct 9 ms 10272 KB Output is correct
16 Correct 12 ms 12272 KB Output is correct
17 Correct 10 ms 11376 KB Output is correct
18 Correct 9 ms 10828 KB Output is correct
19 Correct 10 ms 11212 KB Output is correct
20 Correct 13 ms 10996 KB Output is correct
21 Correct 9 ms 10956 KB Output is correct
22 Correct 11 ms 11636 KB Output is correct
23 Correct 12 ms 11624 KB Output is correct
24 Correct 11 ms 11116 KB Output is correct
25 Correct 10 ms 10996 KB Output is correct
26 Correct 10 ms 10740 KB Output is correct
27 Correct 10 ms 11304 KB Output is correct
28 Correct 11 ms 11088 KB Output is correct
29 Correct 9 ms 10828 KB Output is correct
30 Correct 9 ms 10576 KB Output is correct
31 Correct 10 ms 10504 KB Output is correct
32 Correct 12 ms 12356 KB Output is correct
33 Correct 506 ms 162424 KB Output is correct
34 Correct 339 ms 109748 KB Output is correct
35 Correct 483 ms 162464 KB Output is correct
36 Correct 331 ms 109940 KB Output is correct
37 Correct 239 ms 70852 KB Output is correct
38 Correct 256 ms 67740 KB Output is correct
39 Correct 250 ms 51528 KB Output is correct
40 Correct 192 ms 42172 KB Output is correct
41 Correct 181 ms 42300 KB Output is correct
42 Correct 243 ms 47164 KB Output is correct
43 Correct 195 ms 34380 KB Output is correct
44 Correct 406 ms 164596 KB Output is correct
45 Correct 279 ms 111820 KB Output is correct
46 Correct 173 ms 67916 KB Output is correct
47 Correct 309 ms 82108 KB Output is correct
48 Correct 235 ms 75080 KB Output is correct
49 Correct 193 ms 75728 KB Output is correct
50 Correct 369 ms 112784 KB Output is correct
51 Correct 251 ms 108088 KB Output is correct
52 Correct 308 ms 81404 KB Output is correct
53 Correct 224 ms 76056 KB Output is correct
54 Correct 183 ms 51812 KB Output is correct
55 Correct 278 ms 81348 KB Output is correct
56 Correct 232 ms 65104 KB Output is correct
57 Correct 218 ms 59448 KB Output is correct
58 Correct 205 ms 43064 KB Output is correct
59 Correct 206 ms 43104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9704 KB Output is correct
2 Correct 5 ms 9676 KB Output is correct
3 Correct 6 ms 9716 KB Output is correct
4 Correct 5 ms 9620 KB Output is correct
5 Correct 12 ms 12276 KB Output is correct
6 Correct 11 ms 11248 KB Output is correct
7 Correct 10 ms 10872 KB Output is correct
8 Correct 13 ms 12284 KB Output is correct
9 Correct 10 ms 11340 KB Output is correct
10 Correct 12 ms 10868 KB Output is correct
11 Correct 9 ms 10796 KB Output is correct
12 Correct 10 ms 10700 KB Output is correct
13 Correct 9 ms 10480 KB Output is correct
14 Correct 10 ms 10668 KB Output is correct
15 Correct 9 ms 10272 KB Output is correct
16 Correct 12 ms 12272 KB Output is correct
17 Correct 10 ms 11376 KB Output is correct
18 Correct 9 ms 10828 KB Output is correct
19 Correct 10 ms 11212 KB Output is correct
20 Correct 13 ms 10996 KB Output is correct
21 Correct 9 ms 10956 KB Output is correct
22 Correct 11 ms 11636 KB Output is correct
23 Correct 12 ms 11624 KB Output is correct
24 Correct 11 ms 11116 KB Output is correct
25 Correct 10 ms 10996 KB Output is correct
26 Correct 10 ms 10740 KB Output is correct
27 Correct 10 ms 11304 KB Output is correct
28 Correct 11 ms 11088 KB Output is correct
29 Correct 9 ms 10828 KB Output is correct
30 Correct 9 ms 10576 KB Output is correct
31 Correct 10 ms 10504 KB Output is correct
32 Correct 12 ms 12356 KB Output is correct
33 Correct 506 ms 162424 KB Output is correct
34 Correct 339 ms 109748 KB Output is correct
35 Correct 483 ms 162464 KB Output is correct
36 Correct 331 ms 109940 KB Output is correct
37 Correct 239 ms 70852 KB Output is correct
38 Correct 256 ms 67740 KB Output is correct
39 Correct 250 ms 51528 KB Output is correct
40 Correct 192 ms 42172 KB Output is correct
41 Correct 181 ms 42300 KB Output is correct
42 Correct 243 ms 47164 KB Output is correct
43 Correct 195 ms 34380 KB Output is correct
44 Correct 406 ms 164596 KB Output is correct
45 Correct 279 ms 111820 KB Output is correct
46 Correct 173 ms 67916 KB Output is correct
47 Correct 309 ms 82108 KB Output is correct
48 Correct 235 ms 75080 KB Output is correct
49 Correct 193 ms 75728 KB Output is correct
50 Correct 369 ms 112784 KB Output is correct
51 Correct 251 ms 108088 KB Output is correct
52 Correct 308 ms 81404 KB Output is correct
53 Correct 224 ms 76056 KB Output is correct
54 Correct 183 ms 51812 KB Output is correct
55 Correct 278 ms 81348 KB Output is correct
56 Correct 232 ms 65104 KB Output is correct
57 Correct 218 ms 59448 KB Output is correct
58 Correct 205 ms 43064 KB Output is correct
59 Correct 206 ms 43104 KB Output is correct
60 Correct 5 ms 9676 KB Output is correct
61 Correct 5 ms 9636 KB Output is correct
62 Correct 5 ms 9680 KB Output is correct
63 Correct 5 ms 9932 KB Output is correct
64 Correct 487 ms 152172 KB Output is correct
65 Correct 338 ms 96708 KB Output is correct
66 Correct 458 ms 150996 KB Output is correct
67 Correct 344 ms 96412 KB Output is correct
68 Correct 247 ms 60332 KB Output is correct
69 Correct 211 ms 58136 KB Output is correct
70 Correct 155 ms 26064 KB Output is correct
71 Correct 115 ms 20728 KB Output is correct
72 Correct 158 ms 29740 KB Output is correct
73 Correct 115 ms 20408 KB Output is correct
74 Correct 371 ms 77456 KB Output is correct
75 Correct 232 ms 68148 KB Output is correct
76 Correct 194 ms 68796 KB Output is correct
77 Correct 297 ms 74756 KB Output is correct
78 Correct 195 ms 65896 KB Output is correct
79 Correct 365 ms 98156 KB Output is correct
80 Correct 227 ms 66364 KB Output is correct
81 Correct 173 ms 46392 KB Output is correct
82 Correct 175 ms 30012 KB Output is correct
83 Correct 127 ms 25696 KB Output is correct
84 Correct 322 ms 59012 KB Output is correct
85 Correct 325 ms 59256 KB Output is correct
86 Correct 297 ms 55160 KB Output is correct
87 Correct 331 ms 59140 KB Output is correct
88 Correct 318 ms 59068 KB Output is correct