Submission #417651

# Submission time Handle Problem Language Result Execution time Memory
417651 2021-06-04T05:56:43 Z maomao90 Worst Reporter 4 (JOI21_worst_reporter4) C++17
100 / 100
563 ms 120584 KB
#include <bits/stdc++.h> 
using namespace std;

template <class T>
inline bool mnto(T& a, T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T& a, T b) {return a < b ? a = b, 1: 0;}
#define REP(i, s, e) for (int i = s; i < e; i++)
#define RREP(i, s, e) for (int i = s; i >= e; i--)
typedef long long ll;
typedef long double ld;
#define MP make_pair
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
#define MT make_tuple
typedef tuple<int, int, int> iii;
#define ALL(_a) _a.begin(), _a.end()
#define pb emplace_back
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;

#define INF 1000000005
#define LINF 1000000000000000005
#define MOD 1000000007
#define MAXN 200005

int n;
int h[MAXN], c[MAXN];
vi adj[MAXN], radj[MAXN], nadj[MAXN];
int rep[MAXN];
bool instk[MAXN];
map<int, ll> incre[MAXN];
ll ans;
int in[MAXN];

vi topo, stk;
bool visited[MAXN];
void dfs(int u, bool r, vi& stk) {
	vi ad = r ? radj[u] : adj[u];
	for (int v : ad) {
		if (visited[v]) continue;
		visited[v] = 1;
		dfs(v, r, stk);
	}
	stk.pb(u);
}

void dp(int u) {
	for (int v : nadj[u]) {
		dp(v);
		if (incre[v].size() > incre[u].size()) swap(incre[v], incre[u]);
		for (auto i : incre[v]) {
			incre[u][i.FI] += i.SE;
		}
	}
	auto lower = incre[u].lower_bound(h[u]);
	if (lower != incre[u].begin()) {
		lower = prev(lower);
		int remc = c[u];
		while (1) {
			//printf("%d %lld\n", lower -> FI, lower -> SE);
			ll take = min((ll) remc, lower -> SE);
			lower -> SE -= take;
			remc -= take;
			if (lower -> SE == 0) {
				auto tmp = lower;
				bool hv = 0;
				if (tmp != incre[u].begin()) {
					tmp = prev(tmp);
					hv = 1;
				}
				incre[u].erase(lower);
				if (!hv) {
				   	break;
				} else {
					lower = tmp;
				}
			} else {
				break;
			}
		}
	}
	incre[u][h[u]] += c[u];
	//printf("%d:\n", u);
	//for (auto i : incre[u]) {
		//printf(" %d %lld\n", i.FI, i.SE);
	//}
}

int main() {
	scanf("%d", &n);
	REP (i, 1, n + 1) {
		int a; scanf("%d%d%d", &a, &h[i], &c[i]);
		ans += c[i];
		if (a != i) {
		   	adj[a].pb(i);
			radj[i].pb(a);
		}
	}
	REP (i, 1, n + 1) {
		if (!visited[i]) {
			visited[i] = 1;
			dfs(i, 0, topo);
		}
	}
	memset(visited, 0, sizeof visited);
	reverse(ALL(topo));
	for (int i : topo) {
		if (!visited[i]) {
			visited[i] = 1;
			stk.clear();
			dfs(i, 1, stk);
			for (int u : stk) {
				instk[u] = 1;
			}
			sort(ALL(stk), [&] (int l, int r) {
					return h[l] > h[r];
					});
			REP (i, 1, stk.size()) {
				nadj[stk[i - 1]].pb(stk[i]);
			}
			for (int u : stk) {
				rep[u] = stk.back();
				for (int v : radj[u]) {
					if (instk[v]) continue;
					nadj[rep[v]].pb(rep[u]);
				}
			}
			for (int u : stk) {
				instk[u] = 0;
			}
		}
	}
	REP (i, 1, n + 1) {
		for (int v : nadj[i]) {
			in[v]++;
		}
	}
	//REP (i, 1, n + 1) {
		//printf("%d:", i);
		//for (int v : nadj[i]) {
			//printf(" %d", v);
		//}
		//printf("\n");
	//}
	REP (i, 1, n + 1) {
		if (in[i] != 0) continue;
		dp(i);
		for (auto i : incre[i]) {
			ans -= i.SE;
		}
	}
	printf("%lld\n", ans);
	return 0;
}

/*
4
1 3 4
1 1 2
2 0 3
3 2 1
*/

Compilation message

worst_reporter2.cpp: In function 'int main()':
worst_reporter2.cpp:8:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    8 | #define REP(i, s, e) for (int i = s; i < e; i++)
......
  122 |    REP (i, 1, stk.size()) {
      |         ~~~~~~~~~~~~~~~~                
worst_reporter2.cpp:122:4: note: in expansion of macro 'REP'
  122 |    REP (i, 1, stk.size()) {
      |    ^~~
worst_reporter2.cpp:94:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   94 |  scanf("%d", &n);
      |  ~~~~~^~~~~~~~~~
worst_reporter2.cpp:96:15: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   96 |   int a; scanf("%d%d%d", &a, &h[i], &c[i]);
      |          ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 14 ms 24012 KB Output is correct
2 Correct 15 ms 23960 KB Output is correct
3 Correct 14 ms 23940 KB Output is correct
4 Correct 14 ms 23928 KB Output is correct
5 Correct 23 ms 25744 KB Output is correct
6 Correct 20 ms 25056 KB Output is correct
7 Correct 19 ms 24836 KB Output is correct
8 Correct 22 ms 25608 KB Output is correct
9 Correct 20 ms 25164 KB Output is correct
10 Correct 19 ms 24852 KB Output is correct
11 Correct 19 ms 24688 KB Output is correct
12 Correct 19 ms 25416 KB Output is correct
13 Correct 21 ms 25364 KB Output is correct
14 Correct 19 ms 25056 KB Output is correct
15 Correct 18 ms 25072 KB Output is correct
16 Correct 21 ms 25860 KB Output is correct
17 Correct 19 ms 25228 KB Output is correct
18 Correct 17 ms 24652 KB Output is correct
19 Correct 20 ms 25232 KB Output is correct
20 Correct 18 ms 25048 KB Output is correct
21 Correct 17 ms 24968 KB Output is correct
22 Correct 19 ms 25036 KB Output is correct
23 Correct 17 ms 24740 KB Output is correct
24 Correct 23 ms 25272 KB Output is correct
25 Correct 19 ms 25096 KB Output is correct
26 Correct 19 ms 25360 KB Output is correct
27 Correct 20 ms 25292 KB Output is correct
28 Correct 20 ms 25356 KB Output is correct
29 Correct 19 ms 25484 KB Output is correct
30 Correct 22 ms 25480 KB Output is correct
31 Correct 20 ms 25468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 24012 KB Output is correct
2 Correct 15 ms 23960 KB Output is correct
3 Correct 14 ms 23940 KB Output is correct
4 Correct 14 ms 23928 KB Output is correct
5 Correct 23 ms 25744 KB Output is correct
6 Correct 20 ms 25056 KB Output is correct
7 Correct 19 ms 24836 KB Output is correct
8 Correct 22 ms 25608 KB Output is correct
9 Correct 20 ms 25164 KB Output is correct
10 Correct 19 ms 24852 KB Output is correct
11 Correct 19 ms 24688 KB Output is correct
12 Correct 19 ms 25416 KB Output is correct
13 Correct 21 ms 25364 KB Output is correct
14 Correct 19 ms 25056 KB Output is correct
15 Correct 18 ms 25072 KB Output is correct
16 Correct 21 ms 25860 KB Output is correct
17 Correct 19 ms 25228 KB Output is correct
18 Correct 17 ms 24652 KB Output is correct
19 Correct 20 ms 25232 KB Output is correct
20 Correct 18 ms 25048 KB Output is correct
21 Correct 17 ms 24968 KB Output is correct
22 Correct 19 ms 25036 KB Output is correct
23 Correct 17 ms 24740 KB Output is correct
24 Correct 23 ms 25272 KB Output is correct
25 Correct 19 ms 25096 KB Output is correct
26 Correct 19 ms 25360 KB Output is correct
27 Correct 20 ms 25292 KB Output is correct
28 Correct 20 ms 25356 KB Output is correct
29 Correct 19 ms 25484 KB Output is correct
30 Correct 22 ms 25480 KB Output is correct
31 Correct 20 ms 25468 KB Output is correct
32 Correct 22 ms 25676 KB Output is correct
33 Correct 563 ms 107332 KB Output is correct
34 Correct 481 ms 77276 KB Output is correct
35 Correct 527 ms 105672 KB Output is correct
36 Correct 448 ms 77200 KB Output is correct
37 Correct 352 ms 52552 KB Output is correct
38 Correct 337 ms 47468 KB Output is correct
39 Correct 224 ms 75716 KB Output is correct
40 Correct 210 ms 75668 KB Output is correct
41 Correct 178 ms 75588 KB Output is correct
42 Correct 219 ms 61572 KB Output is correct
43 Correct 206 ms 61392 KB Output is correct
44 Correct 432 ms 120584 KB Output is correct
45 Correct 307 ms 84800 KB Output is correct
46 Correct 165 ms 47172 KB Output is correct
47 Correct 284 ms 68524 KB Output is correct
48 Correct 192 ms 61588 KB Output is correct
49 Correct 169 ms 61476 KB Output is correct
50 Correct 340 ms 61316 KB Output is correct
51 Correct 154 ms 48808 KB Output is correct
52 Correct 349 ms 69420 KB Output is correct
53 Correct 184 ms 62196 KB Output is correct
54 Correct 193 ms 75656 KB Output is correct
55 Correct 311 ms 73896 KB Output is correct
56 Correct 278 ms 78740 KB Output is correct
57 Correct 237 ms 82168 KB Output is correct
58 Correct 272 ms 80000 KB Output is correct
59 Correct 273 ms 80052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 24012 KB Output is correct
2 Correct 15 ms 23960 KB Output is correct
3 Correct 14 ms 23940 KB Output is correct
4 Correct 14 ms 23928 KB Output is correct
5 Correct 23 ms 25744 KB Output is correct
6 Correct 20 ms 25056 KB Output is correct
7 Correct 19 ms 24836 KB Output is correct
8 Correct 22 ms 25608 KB Output is correct
9 Correct 20 ms 25164 KB Output is correct
10 Correct 19 ms 24852 KB Output is correct
11 Correct 19 ms 24688 KB Output is correct
12 Correct 19 ms 25416 KB Output is correct
13 Correct 21 ms 25364 KB Output is correct
14 Correct 19 ms 25056 KB Output is correct
15 Correct 18 ms 25072 KB Output is correct
16 Correct 21 ms 25860 KB Output is correct
17 Correct 19 ms 25228 KB Output is correct
18 Correct 17 ms 24652 KB Output is correct
19 Correct 20 ms 25232 KB Output is correct
20 Correct 18 ms 25048 KB Output is correct
21 Correct 17 ms 24968 KB Output is correct
22 Correct 19 ms 25036 KB Output is correct
23 Correct 17 ms 24740 KB Output is correct
24 Correct 23 ms 25272 KB Output is correct
25 Correct 19 ms 25096 KB Output is correct
26 Correct 19 ms 25360 KB Output is correct
27 Correct 20 ms 25292 KB Output is correct
28 Correct 20 ms 25356 KB Output is correct
29 Correct 19 ms 25484 KB Output is correct
30 Correct 22 ms 25480 KB Output is correct
31 Correct 20 ms 25468 KB Output is correct
32 Correct 22 ms 25676 KB Output is correct
33 Correct 563 ms 107332 KB Output is correct
34 Correct 481 ms 77276 KB Output is correct
35 Correct 527 ms 105672 KB Output is correct
36 Correct 448 ms 77200 KB Output is correct
37 Correct 352 ms 52552 KB Output is correct
38 Correct 337 ms 47468 KB Output is correct
39 Correct 224 ms 75716 KB Output is correct
40 Correct 210 ms 75668 KB Output is correct
41 Correct 178 ms 75588 KB Output is correct
42 Correct 219 ms 61572 KB Output is correct
43 Correct 206 ms 61392 KB Output is correct
44 Correct 432 ms 120584 KB Output is correct
45 Correct 307 ms 84800 KB Output is correct
46 Correct 165 ms 47172 KB Output is correct
47 Correct 284 ms 68524 KB Output is correct
48 Correct 192 ms 61588 KB Output is correct
49 Correct 169 ms 61476 KB Output is correct
50 Correct 340 ms 61316 KB Output is correct
51 Correct 154 ms 48808 KB Output is correct
52 Correct 349 ms 69420 KB Output is correct
53 Correct 184 ms 62196 KB Output is correct
54 Correct 193 ms 75656 KB Output is correct
55 Correct 311 ms 73896 KB Output is correct
56 Correct 278 ms 78740 KB Output is correct
57 Correct 237 ms 82168 KB Output is correct
58 Correct 272 ms 80000 KB Output is correct
59 Correct 273 ms 80052 KB Output is correct
60 Correct 14 ms 23896 KB Output is correct
61 Correct 14 ms 23932 KB Output is correct
62 Correct 14 ms 24012 KB Output is correct
63 Correct 15 ms 24012 KB Output is correct
64 Correct 520 ms 82028 KB Output is correct
65 Correct 461 ms 65208 KB Output is correct
66 Correct 499 ms 81364 KB Output is correct
67 Correct 452 ms 65220 KB Output is correct
68 Correct 382 ms 51380 KB Output is correct
69 Correct 363 ms 47952 KB Output is correct
70 Correct 438 ms 68420 KB Output is correct
71 Correct 403 ms 62928 KB Output is correct
72 Correct 424 ms 77324 KB Output is correct
73 Correct 414 ms 77260 KB Output is correct
74 Correct 523 ms 70764 KB Output is correct
75 Correct 432 ms 63116 KB Output is correct
76 Correct 372 ms 63308 KB Output is correct
77 Correct 429 ms 70928 KB Output is correct
78 Correct 297 ms 63680 KB Output is correct
79 Correct 491 ms 88652 KB Output is correct
80 Correct 439 ms 75524 KB Output is correct
81 Correct 380 ms 65144 KB Output is correct
82 Correct 422 ms 77440 KB Output is correct
83 Correct 110 ms 40764 KB Output is correct
84 Correct 488 ms 75796 KB Output is correct
85 Correct 502 ms 75728 KB Output is correct
86 Correct 494 ms 75936 KB Output is correct
87 Correct 474 ms 75720 KB Output is correct
88 Correct 488 ms 75708 KB Output is correct