답안 #208185

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
208185 2020-03-10T07:29:54 Z E869120 Magic Tree (CEOI19_magictree) C++14
100 / 100
845 ms 255196 KB
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
#pragma warning (disable: 4996)

struct Node {
	long long val, l, r;
};

class DynamicBIT {
public:
	int size_ = 1;
	vector<Node> dat;

	void init(int sz) {
		while (size_ <= sz) size_ *= 2;
		dat.push_back(Node{ 0LL, -1LL, -1LL });
	}
	void add_(int l, int r, long long x, int a, int b, int u) {
		if (l <= a && b <= r) { dat[u].val += x; return; }
		if (b <= l || r <= a) return;

		if (dat[u].l == -1) { dat[u].l = dat.size(); dat.push_back(Node{ 0LL, -1LL, -1LL }); }
		if (dat[u].r == -1) { dat[u].r = dat.size(); dat.push_back(Node{ 0LL, -1LL, -1LL }); }
		add_(l, r, x, a, (a + b) >> 1, dat[u].l);
		add_(l, r, x, (a + b) >> 1, b, dat[u].r);
	}
	void add(int l, int r, long long x) {
		add_(l, r, x, 0, size_, 0);
	}
	long long sum(int pos) {
		int ranged = size_, cx = 0; long long s = 0;
		while (ranged >= 2 && cx >= 0) {
			ranged >>= 1; s += dat[cx].val;
			if (pos < ranged) cx = dat[cx].l;
			else { cx = dat[cx].r; pos -= ranged; }
		}
		if (cx != -1) s += dat[cx].val;
		return s;
	}
};

// 入力関連
long long N, P[1 << 18], idx[1 << 18];
long long M, A[1 << 18], B[1 << 18], C[1 << 18];
long long K;
vector<long long> Y;

// グラフ
long long dp[1 << 18];
vector<int> X[100009];

// マージテク
set<long long> Set[100009];
DynamicBIT Z[100009];
int num[100009];

int main() {
	// ステップ 1. 入力
	scanf("%lld%lld%lld", &N, &M, &K);
	for (int i = 2; i <= N; i++) scanf("%lld", &P[i]);
	for (int i = 1; i <= M; i++) scanf("%lld%lld%lld", &A[i], &B[i], &C[i]);

	// ステップ 2. グラフ構築
	for (int i = 1; i <= M; i++) idx[A[i]] = i;
	for (int i = 2; i <= N; i++) X[P[i]].push_back(i);
	for (int i = N; i >= 1; i--) {
		dp[i] = 1;
		for (int j : X[i]) dp[i] += dp[j];
	}

	// ステップ 3. 座標圧縮
	for (int i = 1; i <= M; i++) Y.push_back(B[i]);
	sort(Y.begin(), Y.end());
	Y.erase(unique(Y.begin(), Y.end()), Y.end());
	for (int i = 1; i <= M; i++) B[i] = (lower_bound(Y.begin(), Y.end(), B[i]) - Y.begin()) + 1;

	// ステップ 4. マージテク
	for (int i = N; i >= 1; i--) {
		if (X[i].size() == 0) {
			num[i] = i;
			Z[i].init(Y.size() + 2);
		}
		else {
			int heavy = -1, maxn = -1;
			for (int j : X[i]) {
				if (maxn < dp[j]) { maxn = dp[j]; heavy = j; num[i] = num[j]; }
			}
			for (int j : X[i]) {
				if (j == heavy) continue;
				auto itr = Set[num[j]].begin(); long long last = 0;
				while (itr != Set[num[j]].end()) {
					long long val = Z[num[j]].sum(*itr);
					Z[num[i]].add(*itr, Y.size() + 2, val - last);
					Set[num[i]].insert(*itr);
					last = val;
					itr++;
				}
			}
		}
		if (idx[i] != 0) {
			int id = idx[i], cx = B[id];
			long long goal = Z[num[i]].sum(B[id]) + C[id];
			while (cx <= Y.size()) {
				long long last = Z[num[i]].sum(cx);
				if (goal - last <= 0LL) break;

				int cl = cx, cr = Y.size() + 2, cm, maxn = cx - 1;
				for (int t = 0; t < 21; t++) {
					cm = (cl + cr) / 2;
					if (Z[num[i]].sum(cm) == last) { maxn = max(maxn, cm); cl = cm; }
					else { cr = cm; }
				}
				Z[num[i]].add(cx, maxn + 1, goal - last);
				cx = maxn + 1;
			}
			Set[num[i]].insert(B[id]);
		}
	}

	cout << Z[num[1]].sum(Y.size() + 1) << endl;
	return 0;
}

Compilation message

magictree.cpp:6:0: warning: ignoring #pragma warning  [-Wunknown-pragmas]
 #pragma warning (disable: 4996)
 
magictree.cpp: In function 'int main()':
magictree.cpp:106:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    while (cx <= Y.size()) {
           ~~~^~~~~~~~~~~
magictree.cpp:62:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%lld%lld%lld", &N, &M, &K);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
magictree.cpp:63:36: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for (int i = 2; i <= N; i++) scanf("%lld", &P[i]);
                               ~~~~~^~~~~~~~~~~~~~~
magictree.cpp:64:36: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for (int i = 1; i <= M; i++) scanf("%lld%lld%lld", &A[i], &B[i], &C[i]);
                               ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 10616 KB Output is correct
2 Correct 11 ms 10600 KB Output is correct
3 Correct 10 ms 10616 KB Output is correct
4 Correct 12 ms 10488 KB Output is correct
5 Correct 11 ms 10488 KB Output is correct
6 Correct 10 ms 10488 KB Output is correct
7 Correct 11 ms 10488 KB Output is correct
8 Correct 10 ms 10488 KB Output is correct
9 Correct 10 ms 10492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 275 ms 99920 KB Output is correct
2 Correct 127 ms 51452 KB Output is correct
3 Correct 739 ms 255196 KB Output is correct
4 Correct 434 ms 165604 KB Output is correct
5 Correct 467 ms 165732 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 10616 KB Output is correct
2 Correct 11 ms 10616 KB Output is correct
3 Correct 13 ms 10616 KB Output is correct
4 Correct 265 ms 27496 KB Output is correct
5 Correct 197 ms 27368 KB Output is correct
6 Correct 430 ms 27460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 151 ms 32492 KB Output is correct
2 Correct 140 ms 31592 KB Output is correct
3 Correct 111 ms 25172 KB Output is correct
4 Correct 121 ms 41020 KB Output is correct
5 Correct 85 ms 21360 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 10616 KB Output is correct
2 Correct 11 ms 10600 KB Output is correct
3 Correct 10 ms 10616 KB Output is correct
4 Correct 12 ms 10488 KB Output is correct
5 Correct 11 ms 10488 KB Output is correct
6 Correct 10 ms 10488 KB Output is correct
7 Correct 11 ms 10488 KB Output is correct
8 Correct 10 ms 10488 KB Output is correct
9 Correct 10 ms 10492 KB Output is correct
10 Correct 253 ms 57148 KB Output is correct
11 Correct 206 ms 43624 KB Output is correct
12 Correct 129 ms 30436 KB Output is correct
13 Correct 185 ms 71148 KB Output is correct
14 Correct 99 ms 20468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 13688 KB Output is correct
2 Correct 52 ms 18808 KB Output is correct
3 Correct 50 ms 18296 KB Output is correct
4 Correct 52 ms 19448 KB Output is correct
5 Correct 34 ms 18160 KB Output is correct
6 Correct 46 ms 17528 KB Output is correct
7 Correct 41 ms 16888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 10616 KB Output is correct
2 Correct 11 ms 10600 KB Output is correct
3 Correct 10 ms 10616 KB Output is correct
4 Correct 12 ms 10488 KB Output is correct
5 Correct 11 ms 10488 KB Output is correct
6 Correct 10 ms 10488 KB Output is correct
7 Correct 11 ms 10488 KB Output is correct
8 Correct 10 ms 10488 KB Output is correct
9 Correct 10 ms 10492 KB Output is correct
10 Correct 11 ms 10616 KB Output is correct
11 Correct 11 ms 10616 KB Output is correct
12 Correct 13 ms 10616 KB Output is correct
13 Correct 265 ms 27496 KB Output is correct
14 Correct 197 ms 27368 KB Output is correct
15 Correct 430 ms 27460 KB Output is correct
16 Correct 253 ms 57148 KB Output is correct
17 Correct 206 ms 43624 KB Output is correct
18 Correct 129 ms 30436 KB Output is correct
19 Correct 185 ms 71148 KB Output is correct
20 Correct 99 ms 20468 KB Output is correct
21 Correct 109 ms 39028 KB Output is correct
22 Correct 334 ms 100008 KB Output is correct
23 Correct 456 ms 148564 KB Output is correct
24 Correct 799 ms 246616 KB Output is correct
25 Correct 415 ms 164776 KB Output is correct
26 Correct 566 ms 113488 KB Output is correct
27 Correct 531 ms 58408 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 10616 KB Output is correct
2 Correct 11 ms 10600 KB Output is correct
3 Correct 10 ms 10616 KB Output is correct
4 Correct 12 ms 10488 KB Output is correct
5 Correct 11 ms 10488 KB Output is correct
6 Correct 10 ms 10488 KB Output is correct
7 Correct 11 ms 10488 KB Output is correct
8 Correct 10 ms 10488 KB Output is correct
9 Correct 10 ms 10492 KB Output is correct
10 Correct 275 ms 99920 KB Output is correct
11 Correct 127 ms 51452 KB Output is correct
12 Correct 739 ms 255196 KB Output is correct
13 Correct 434 ms 165604 KB Output is correct
14 Correct 467 ms 165732 KB Output is correct
15 Correct 11 ms 10616 KB Output is correct
16 Correct 11 ms 10616 KB Output is correct
17 Correct 13 ms 10616 KB Output is correct
18 Correct 265 ms 27496 KB Output is correct
19 Correct 197 ms 27368 KB Output is correct
20 Correct 430 ms 27460 KB Output is correct
21 Correct 151 ms 32492 KB Output is correct
22 Correct 140 ms 31592 KB Output is correct
23 Correct 111 ms 25172 KB Output is correct
24 Correct 121 ms 41020 KB Output is correct
25 Correct 85 ms 21360 KB Output is correct
26 Correct 253 ms 57148 KB Output is correct
27 Correct 206 ms 43624 KB Output is correct
28 Correct 129 ms 30436 KB Output is correct
29 Correct 185 ms 71148 KB Output is correct
30 Correct 99 ms 20468 KB Output is correct
31 Correct 21 ms 13688 KB Output is correct
32 Correct 52 ms 18808 KB Output is correct
33 Correct 50 ms 18296 KB Output is correct
34 Correct 52 ms 19448 KB Output is correct
35 Correct 34 ms 18160 KB Output is correct
36 Correct 46 ms 17528 KB Output is correct
37 Correct 41 ms 16888 KB Output is correct
38 Correct 109 ms 39028 KB Output is correct
39 Correct 334 ms 100008 KB Output is correct
40 Correct 456 ms 148564 KB Output is correct
41 Correct 799 ms 246616 KB Output is correct
42 Correct 415 ms 164776 KB Output is correct
43 Correct 566 ms 113488 KB Output is correct
44 Correct 531 ms 58408 KB Output is correct
45 Correct 116 ms 39028 KB Output is correct
46 Correct 346 ms 100688 KB Output is correct
47 Correct 463 ms 149576 KB Output is correct
48 Correct 845 ms 249048 KB Output is correct
49 Correct 435 ms 165476 KB Output is correct
50 Correct 674 ms 114516 KB Output is correct
51 Correct 710 ms 59092 KB Output is correct