Submission #391307

# Submission time Handle Problem Language Result Execution time Memory
391307 2021-04-18T13:33:23 Z Mlxa Worst Reporter 4 (JOI21_worst_reporter4) C++14
100 / 100
1408 ms 124596 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
#define x first
#define y second
#define mp make_pair
#define mt make_tuple
#define all(x) x.begin(), x.end()

const int NODES = 10e6 + 10;
mt19937 rnd;

struct node {
	int x, w, dy, sy, ls, rs, sz;
} pool[NODES];
int ff = 1;

#define getter(f) int &f(int v) { assert(0 <= v && v < NODES); return pool[v].f; }
getter(x) getter(w) getter(dy) getter(sy) getter(ls) getter(rs) getter(sz)

int new_node(int nx, int ny) {
	assert(ff < NODES);
	int v = ff++;
	x(v) = nx;
	w(v) = (int)rnd();
	dy(v) = ny;
	sy(v) = ny;
	ls(v) = rs(v) = 0;
	sz(v) = 1;
	return v;
}

int pull(int v) {
	sy(v) = sy(ls(v)) + dy(v) + sy(rs(v));
	sz(v) = sz(ls(v)) + 1 + sz(rs(v));
	return v;
}

void to_vector(int t, vector<pair<int, int>> &vec) {
	if (!t) {
		return;
	}
	to_vector(ls(t), vec);
	vec.emplace_back(x(t), dy(t));
	to_vector(rs(t), vec);
}

void print(int t) {
	vector<pair<int, int>> v;
	to_vector(t, v);
	for (auto e : v) {
		cout << "(" << e.x << "; " << e.y << ") ";
	}
	cout << endl;
}


pair<int, int> splitsum(int t, int s) {
	if (!t) {
		return {0, 0};
	}
	// print(t);
	assert(0 <= s && s <= sy(t));
	int lsum = sy(ls(t)), msum = dy(t);
	if (lsum < s && lsum + msum > s) {
		int lt = new_node(x(t), s - lsum), rt = new_node(x(t), msum - dy(lt));
		ls(lt) = ls(t), rs(rt) = rs(t);
		return {pull(lt), pull(rt)};
	}
	int l, r;
	if (s <= lsum) {
		tie(l, ls(t)) = splitsum(ls(t), s);
		r = pull(t);
	} else {
		tie(rs(t), r) = splitsum(rs(t), s - lsum - msum);
		l = pull(t);
	}
	assert(sy(l) == s);
	return {l, r};
}

// (-inf, k], (k, +inf)
pair<int, int> split(int t, int k) {
	if (!t) {
		return {0, 0};
	}
	int l, r;
	if (k < x(t)) {
		tie(l, ls(t)) = split(ls(t), k);
		r = pull(t);
	} else {
		tie(rs(t), r) = split(rs(t), k);
		l = pull(t);
	}
	return {l, r};
}

int merge(int l, int r) {
	if (!l) {
		return r;
	}
	if (!r) {
		return l;
	}
	if (w(l) < w(r)) {
		rs(l) = merge(rs(l), r);
		return pull(l);
	} else {
		ls(r) = merge(l, ls(r));
		return pull(r);
	}
}

int get_value(int t, int x) {
	int l, r;
	tie(l, r) = split(t, x);
	int res = sy(l);
	t = merge(l, r);
	return res;
}

__attribute__((warn_unused_result))
int insert(int t, int nx, int ny) {
	int l, m, r;
	tie(l, m) = split(t, nx - 1);
	tie(m, r) = split(m, nx);
	// cout << "insert " << nx << " " << ny << endl;
	// print(l);
	// print(m);
	// print(r);
	// cout << "---" << endl;
	m = new_node(nx, sy(m) + ny);
	l = merge(l, m);
	// print(l);
	return merge(l, r);
}

__attribute__((warn_unused_result))
int uminpref(int t, int px) {
	int checksum = sy(t);

	int l, m, r;
	tie(l, m) = split(t, px - 1);
	tie(m, r) = split(m, px);
	if (sy(m) <= 0) {
		int p, q;
		tie(p, q) = splitsum(l, sy(l) + sy(m));
		t = merge(p, r);
		assert(sy(t) == checksum);
		return t;
	} else {
		return merge(merge(l, m), r);
	}
}

__attribute__((warn_unused_result))
int add(int tv, int tu) {
	if (sz(tv) < sz(tu)) {
		swap(tv, tu);
	}
	if (!tu) {
		return tv;
	}
	tv = insert(tv, x(tu), dy(tu));
	tv = add(tv, ls(tu));
	tv = add(tv, rs(tu));
	return tv;
}



const int N = 2e5 + 10;
int n, ia[N], ih[N], ic[N], dp[N];
vector<int> g[N];

bool used[N];

void dfs(int v) {
	used[v] = true;
	for (int u : g[v]) {
		if (used[u]) {
			continue;
		}
		dfs(u);
		dp[u] = uminpref(dp[u], ih[u]);
		// cout << "v = " << u + 1 << endl;
		// print(dp[u]);
		dp[v] = add(dp[v], dp[u]);
	}
	dp[v] = insert(dp[v], 0, ic[v]);
	dp[v] = insert(dp[v], ih[v], -ic[v]);
	dp[v] = insert(dp[v], ih[v] + 1, ic[v]);
}


signed main() {
#ifdef LC
	assert(freopen("input.txt", "r", stdin));
#endif
	ios::sync_with_stdio(0); cin.tie(0); 

	cin >> n;
	for (int i = 0; i < n; ++i) {
		cin >> ia[i] >> ih[i] >> ic[i];
		--ia[i];
		g[ia[i]].push_back(i);
	}
	int answer = 0;
	for (int i = 0; i < n; ++i) {
		if (used[i]) {
			continue;
		}
		int v = ia[i], u = ia[ia[i]];
		while (v != u) {
			v = ia[v];
			u = ia[ia[u]];
		}
		vector<int> from = {v};
		u = ia[u];
		while (u != v) {
			from.push_back(u);
			u = ia[u];
		}
		int t = 0;
		for (int x : from) {
			used[x] = true;
		}
		for (int x : from) {
			dfs(x);
			t = add(t, dp[x]);
		}

		// cout << "v = " << v + 1 << endl;
		// print(t);

		t = insert(t, 0, 0);
		vector<pair<int, int>> ve;
		to_vector(t, ve);
		int cur = (int)1e18, y = 0;
		for (auto e : ve) {
			y += e.y;
			cur = min(cur, y);
		}
		answer += cur;
	}
	cout << answer << "\n";
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 14 ms 7116 KB Output is correct
6 Correct 11 ms 6784 KB Output is correct
7 Correct 9 ms 6656 KB Output is correct
8 Correct 14 ms 7116 KB Output is correct
9 Correct 11 ms 6732 KB Output is correct
10 Correct 9 ms 6664 KB Output is correct
11 Correct 7 ms 6220 KB Output is correct
12 Correct 14 ms 6988 KB Output is correct
13 Correct 10 ms 6988 KB Output is correct
14 Correct 13 ms 6812 KB Output is correct
15 Correct 12 ms 6808 KB Output is correct
16 Correct 15 ms 7416 KB Output is correct
17 Correct 11 ms 7020 KB Output is correct
18 Correct 7 ms 6220 KB Output is correct
19 Correct 13 ms 6732 KB Output is correct
20 Correct 11 ms 6604 KB Output is correct
21 Correct 10 ms 6604 KB Output is correct
22 Correct 10 ms 6564 KB Output is correct
23 Correct 8 ms 6396 KB Output is correct
24 Correct 14 ms 6732 KB Output is correct
25 Correct 11 ms 6732 KB Output is correct
26 Correct 7 ms 6476 KB Output is correct
27 Correct 11 ms 6732 KB Output is correct
28 Correct 12 ms 6860 KB Output is correct
29 Correct 12 ms 6964 KB Output is correct
30 Correct 18 ms 7172 KB Output is correct
31 Correct 18 ms 7116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 7116 KB Output is correct
2 Correct 671 ms 101664 KB Output is correct
3 Correct 450 ms 84164 KB Output is correct
4 Correct 654 ms 99088 KB Output is correct
5 Correct 474 ms 84184 KB Output is correct
6 Correct 310 ms 66676 KB Output is correct
7 Correct 238 ms 53556 KB Output is correct
8 Correct 502 ms 85060 KB Output is correct
9 Correct 460 ms 85060 KB Output is correct
10 Correct 229 ms 84932 KB Output is correct
11 Correct 474 ms 78860 KB Output is correct
12 Correct 440 ms 78788 KB Output is correct
13 Correct 758 ms 124596 KB Output is correct
14 Correct 442 ms 97196 KB Output is correct
15 Correct 160 ms 52892 KB Output is correct
16 Correct 730 ms 72304 KB Output is correct
17 Correct 402 ms 70212 KB Output is correct
18 Correct 200 ms 70084 KB Output is correct
19 Correct 547 ms 61128 KB Output is correct
20 Correct 252 ms 56888 KB Output is correct
21 Correct 800 ms 73120 KB Output is correct
22 Correct 397 ms 71092 KB Output is correct
23 Correct 167 ms 63096 KB Output is correct
24 Correct 472 ms 75444 KB Output is correct
25 Correct 498 ms 82020 KB Output is correct
26 Correct 492 ms 84864 KB Output is correct
27 Correct 1384 ms 88424 KB Output is correct
28 Correct 1408 ms 88520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 14 ms 7116 KB Output is correct
6 Correct 11 ms 6784 KB Output is correct
7 Correct 9 ms 6656 KB Output is correct
8 Correct 14 ms 7116 KB Output is correct
9 Correct 11 ms 6732 KB Output is correct
10 Correct 9 ms 6664 KB Output is correct
11 Correct 7 ms 6220 KB Output is correct
12 Correct 14 ms 6988 KB Output is correct
13 Correct 10 ms 6988 KB Output is correct
14 Correct 13 ms 6812 KB Output is correct
15 Correct 12 ms 6808 KB Output is correct
16 Correct 15 ms 7416 KB Output is correct
17 Correct 11 ms 7020 KB Output is correct
18 Correct 7 ms 6220 KB Output is correct
19 Correct 13 ms 6732 KB Output is correct
20 Correct 11 ms 6604 KB Output is correct
21 Correct 10 ms 6604 KB Output is correct
22 Correct 10 ms 6564 KB Output is correct
23 Correct 8 ms 6396 KB Output is correct
24 Correct 14 ms 6732 KB Output is correct
25 Correct 11 ms 6732 KB Output is correct
26 Correct 7 ms 6476 KB Output is correct
27 Correct 11 ms 6732 KB Output is correct
28 Correct 12 ms 6860 KB Output is correct
29 Correct 12 ms 6964 KB Output is correct
30 Correct 18 ms 7172 KB Output is correct
31 Correct 18 ms 7116 KB Output is correct
32 Correct 13 ms 7116 KB Output is correct
33 Correct 671 ms 101664 KB Output is correct
34 Correct 450 ms 84164 KB Output is correct
35 Correct 654 ms 99088 KB Output is correct
36 Correct 474 ms 84184 KB Output is correct
37 Correct 310 ms 66676 KB Output is correct
38 Correct 238 ms 53556 KB Output is correct
39 Correct 502 ms 85060 KB Output is correct
40 Correct 460 ms 85060 KB Output is correct
41 Correct 229 ms 84932 KB Output is correct
42 Correct 474 ms 78860 KB Output is correct
43 Correct 440 ms 78788 KB Output is correct
44 Correct 758 ms 124596 KB Output is correct
45 Correct 442 ms 97196 KB Output is correct
46 Correct 160 ms 52892 KB Output is correct
47 Correct 730 ms 72304 KB Output is correct
48 Correct 402 ms 70212 KB Output is correct
49 Correct 200 ms 70084 KB Output is correct
50 Correct 547 ms 61128 KB Output is correct
51 Correct 252 ms 56888 KB Output is correct
52 Correct 800 ms 73120 KB Output is correct
53 Correct 397 ms 71092 KB Output is correct
54 Correct 167 ms 63096 KB Output is correct
55 Correct 472 ms 75444 KB Output is correct
56 Correct 498 ms 82020 KB Output is correct
57 Correct 492 ms 84864 KB Output is correct
58 Correct 1384 ms 88424 KB Output is correct
59 Correct 1408 ms 88520 KB Output is correct
60 Correct 4 ms 4940 KB Output is correct
61 Correct 3 ms 4940 KB Output is correct
62 Correct 3 ms 4940 KB Output is correct
63 Correct 3 ms 4940 KB Output is correct
64 Correct 615 ms 89936 KB Output is correct
65 Correct 467 ms 85820 KB Output is correct
66 Correct 602 ms 93620 KB Output is correct
67 Correct 486 ms 85936 KB Output is correct
68 Correct 319 ms 73196 KB Output is correct
69 Correct 262 ms 57876 KB Output is correct
70 Correct 1025 ms 94564 KB Output is correct
71 Correct 630 ms 88964 KB Output is correct
72 Correct 1132 ms 98700 KB Output is correct
73 Correct 589 ms 90312 KB Output is correct
74 Correct 908 ms 83664 KB Output is correct
75 Correct 515 ms 75432 KB Output is correct
76 Correct 322 ms 74496 KB Output is correct
77 Correct 844 ms 87772 KB Output is correct
78 Correct 438 ms 76148 KB Output is correct
79 Correct 877 ms 107568 KB Output is correct
80 Correct 540 ms 90988 KB Output is correct
81 Correct 315 ms 81196 KB Output is correct
82 Correct 508 ms 98772 KB Output is correct
83 Correct 174 ms 66628 KB Output is correct
84 Correct 802 ms 96148 KB Output is correct
85 Correct 804 ms 96000 KB Output is correct
86 Correct 777 ms 86652 KB Output is correct
87 Correct 772 ms 91892 KB Output is correct
88 Correct 803 ms 94440 KB Output is correct