답안 #547243

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
547243 2022-04-10T03:32:27 Z pure_mem Magic Tree (CEOI19_magictree) C++14
100 / 100
123 ms 18860 KB
#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define X first
#define Y second
#define MP make_pair

using namespace std;

const int N = 1e5 + 2;
const int INF = 1e9;

typedef map<int, ll> DP;

void merge(DP& oth, DP& cur) {
	if(cur.size() < oth.size())
		cur.swap(oth);
	for(auto [pos, val]: oth) {
		cur[pos] += val;
	}	
	oth.clear();
}

void insert(DP& cur, int pos, ll val) {
	cur[pos] += val;
	DP::iterator it = cur.upper_bound(pos);
	ll sum = 0;

    while(it != cur.end()) {
    	if(it->second + sum > val) {
    		it->second += sum - val;
    		break;		      
    	}
    	else {
    		sum += it->second;
    		it = cur.erase(it);
    	}
    }
}

int n, m, k;
DP dp[N];
vector<int> g[N];
pair<int, int> fruit[N];

void dfs(int v) {
	for(int to: g[v]) {
		dfs(to);
		merge(dp[to], dp[v]);
	}
	insert(dp[v], fruit[v].X, fruit[v].Y);
}

int main () {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	cin >> n >> m >> k;
	for(int i = 2, v;i <= n;i++) {
		cin >> v;
		g[v].push_back(i);
	}
	for(int i = 1;i <= m;i++) {
		int v, d, w;
		cin >> v >> d >> w;
		fruit[v] = {d, w};
	}

	dfs(1);
	ll res = 0;
	for(auto [pos, val]: dp[1])
		res += val;
	cout << res;
}

Compilation message

magictree.cpp: In function 'void merge(DP&, DP&)':
magictree.cpp:21:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   21 |  for(auto [pos, val]: oth) {
      |           ^
magictree.cpp: In function 'int main()':
magictree.cpp:75:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   75 |  for(auto [pos, val]: dp[1])
      |           ^
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7252 KB Output is correct
2 Correct 4 ms 7252 KB Output is correct
3 Correct 4 ms 7252 KB Output is correct
4 Correct 4 ms 7252 KB Output is correct
5 Correct 4 ms 7252 KB Output is correct
6 Correct 4 ms 7252 KB Output is correct
7 Correct 4 ms 7252 KB Output is correct
8 Correct 4 ms 7368 KB Output is correct
9 Correct 4 ms 7252 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 88 ms 11456 KB Output is correct
2 Correct 42 ms 13204 KB Output is correct
3 Correct 116 ms 13500 KB Output is correct
4 Correct 65 ms 12172 KB Output is correct
5 Correct 123 ms 12820 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 43 ms 14900 KB Output is correct
5 Correct 70 ms 18860 KB Output is correct
6 Correct 71 ms 14928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 68 ms 9652 KB Output is correct
2 Correct 76 ms 11864 KB Output is correct
3 Correct 51 ms 14176 KB Output is correct
4 Correct 38 ms 10324 KB Output is correct
5 Correct 46 ms 16944 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7252 KB Output is correct
2 Correct 4 ms 7252 KB Output is correct
3 Correct 4 ms 7252 KB Output is correct
4 Correct 4 ms 7252 KB Output is correct
5 Correct 4 ms 7252 KB Output is correct
6 Correct 4 ms 7252 KB Output is correct
7 Correct 4 ms 7252 KB Output is correct
8 Correct 4 ms 7368 KB Output is correct
9 Correct 4 ms 7252 KB Output is correct
10 Correct 79 ms 9756 KB Output is correct
11 Correct 70 ms 9752 KB Output is correct
12 Correct 47 ms 12096 KB Output is correct
13 Correct 29 ms 8660 KB Output is correct
14 Correct 40 ms 14912 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 7904 KB Output is correct
2 Correct 41 ms 9712 KB Output is correct
3 Correct 45 ms 9756 KB Output is correct
4 Correct 41 ms 9760 KB Output is correct
5 Correct 15 ms 8660 KB Output is correct
6 Correct 33 ms 10212 KB Output is correct
7 Correct 25 ms 12040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7252 KB Output is correct
2 Correct 4 ms 7252 KB Output is correct
3 Correct 4 ms 7252 KB Output is correct
4 Correct 4 ms 7252 KB Output is correct
5 Correct 4 ms 7252 KB Output is correct
6 Correct 4 ms 7252 KB Output is correct
7 Correct 4 ms 7252 KB Output is correct
8 Correct 4 ms 7368 KB Output is correct
9 Correct 4 ms 7252 KB Output is correct
10 Correct 5 ms 7380 KB Output is correct
11 Correct 4 ms 7380 KB Output is correct
12 Correct 4 ms 7380 KB Output is correct
13 Correct 43 ms 14900 KB Output is correct
14 Correct 70 ms 18860 KB Output is correct
15 Correct 71 ms 14928 KB Output is correct
16 Correct 79 ms 9756 KB Output is correct
17 Correct 70 ms 9752 KB Output is correct
18 Correct 47 ms 12096 KB Output is correct
19 Correct 29 ms 8660 KB Output is correct
20 Correct 40 ms 14912 KB Output is correct
21 Correct 17 ms 8128 KB Output is correct
22 Correct 66 ms 10144 KB Output is correct
23 Correct 88 ms 11944 KB Output is correct
24 Correct 109 ms 13064 KB Output is correct
25 Correct 57 ms 12316 KB Output is correct
26 Correct 80 ms 14100 KB Output is correct
27 Correct 65 ms 13576 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7252 KB Output is correct
2 Correct 4 ms 7252 KB Output is correct
3 Correct 4 ms 7252 KB Output is correct
4 Correct 4 ms 7252 KB Output is correct
5 Correct 4 ms 7252 KB Output is correct
6 Correct 4 ms 7252 KB Output is correct
7 Correct 4 ms 7252 KB Output is correct
8 Correct 4 ms 7368 KB Output is correct
9 Correct 4 ms 7252 KB Output is correct
10 Correct 88 ms 11456 KB Output is correct
11 Correct 42 ms 13204 KB Output is correct
12 Correct 116 ms 13500 KB Output is correct
13 Correct 65 ms 12172 KB Output is correct
14 Correct 123 ms 12820 KB Output is correct
15 Correct 5 ms 7380 KB Output is correct
16 Correct 4 ms 7380 KB Output is correct
17 Correct 4 ms 7380 KB Output is correct
18 Correct 43 ms 14900 KB Output is correct
19 Correct 70 ms 18860 KB Output is correct
20 Correct 71 ms 14928 KB Output is correct
21 Correct 68 ms 9652 KB Output is correct
22 Correct 76 ms 11864 KB Output is correct
23 Correct 51 ms 14176 KB Output is correct
24 Correct 38 ms 10324 KB Output is correct
25 Correct 46 ms 16944 KB Output is correct
26 Correct 79 ms 9756 KB Output is correct
27 Correct 70 ms 9752 KB Output is correct
28 Correct 47 ms 12096 KB Output is correct
29 Correct 29 ms 8660 KB Output is correct
30 Correct 40 ms 14912 KB Output is correct
31 Correct 9 ms 7904 KB Output is correct
32 Correct 41 ms 9712 KB Output is correct
33 Correct 45 ms 9756 KB Output is correct
34 Correct 41 ms 9760 KB Output is correct
35 Correct 15 ms 8660 KB Output is correct
36 Correct 33 ms 10212 KB Output is correct
37 Correct 25 ms 12040 KB Output is correct
38 Correct 17 ms 8128 KB Output is correct
39 Correct 66 ms 10144 KB Output is correct
40 Correct 88 ms 11944 KB Output is correct
41 Correct 109 ms 13064 KB Output is correct
42 Correct 57 ms 12316 KB Output is correct
43 Correct 80 ms 14100 KB Output is correct
44 Correct 65 ms 13576 KB Output is correct
45 Correct 29 ms 8532 KB Output is correct
46 Correct 99 ms 11620 KB Output is correct
47 Correct 88 ms 13564 KB Output is correct
48 Correct 113 ms 15784 KB Output is correct
49 Correct 83 ms 14276 KB Output is correct
50 Correct 98 ms 16888 KB Output is correct
51 Correct 84 ms 16248 KB Output is correct