Submission #242216

# Submission time Handle Problem Language Result Execution time Memory
242216 2020-06-27T06:32:58 Z errorgorn Magic Tree (CEOI19_magictree) C++14
100 / 100
200 ms 37804 KB
//雪花飄飄北風嘯嘯
//天地一片蒼茫

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define ll long long
#define ii pair<ll,ll>
#define iii pair<ii,ll>
#define fi first
#define se second
#define endl '\n'
#define debug(x) cout << #x << " is " << x << endl;

#define rep(x,start,end) for(auto x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?x++:x--))
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()

ll MAX(ll a){return a;}
ll MIN(ll a){return a;}
template<typename... Args>
ll MAX(ll a,Args... args){return max(a,MAX(args...));}
template<typename... Args>
ll MIN(ll a,Args... args){return min(a,MIN(args...));}

#define indexed_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>

mt19937 rng(chrono::system_clock::now().time_since_epoch().count());


int n,mm,k;
vector<int> al[100005];
ii fruit[100005];

map<int,ll> m[100005];

void dfs(int i){
	for (auto &it:al[i]){
		dfs(it);
		
		//map merging :thinking:
		if (sz(m[i])<sz(m[it])) swap(m[i],m[it]);
		
		for (auto &item:m[it]){
			if (m[i].count(item.fi)) m[i][item.fi]+=item.se;
			else m[i][item.fi]=item.se;
		}
	}
	
	if (fruit[i]==ii(0,0)) return;
	
	map<int,ll>::iterator it;
	if (m[i].count(fruit[i].fi)){
		it=m[i].find(fruit[i].fi);
	}
	else{
		it=m[i].insert({fruit[i].fi,0}).fi;
	}
	
	it->se+=fruit[i].se;
	ll curr=fruit[i].se;
	
	while (next(it)!=m[i].end()){
		int temp=min(next(it)->se,curr);
		
		if (temp<curr){
			m[i].erase(next(it));
			curr-=temp;
		}
		else{
			next(it)->se-=temp;
			break;
		}
	}
	
	//cout<<"debug: "<<i<<endl;
	//for (auto &it:m[i]) cout<<it.fi<<"_"<<it.se<<" ";cout<<endl;
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	
	cin>>n>>mm>>k;
	
	rep(x,2,n+1){
		int p;
		cin>>p;
		al[p].push_back(x);
	}
	
	rep(x,0,mm){
		int a,b,c;
		cin>>a>>b>>c;
		fruit[a]={b,c};
	}
	
	dfs(1);
	
	ll ans=0;
	for (auto &it:m[1]) ans+=it.se;
	cout<<ans<<endl;
}
# Verdict Execution time Memory Grader output
1 Correct 9 ms 7424 KB Output is correct
2 Correct 10 ms 7424 KB Output is correct
3 Correct 10 ms 7424 KB Output is correct
4 Correct 9 ms 7424 KB Output is correct
5 Correct 10 ms 7424 KB Output is correct
6 Correct 9 ms 7552 KB Output is correct
7 Correct 10 ms 7424 KB Output is correct
8 Correct 9 ms 7296 KB Output is correct
9 Correct 9 ms 7424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 84 ms 18296 KB Output is correct
2 Correct 66 ms 18680 KB Output is correct
3 Correct 185 ms 37804 KB Output is correct
4 Correct 111 ms 20852 KB Output is correct
5 Correct 153 ms 22392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 7552 KB Output is correct
2 Correct 9 ms 7552 KB Output is correct
3 Correct 9 ms 7552 KB Output is correct
4 Correct 81 ms 24952 KB Output is correct
5 Correct 98 ms 28920 KB Output is correct
6 Correct 93 ms 24952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 95 ms 14456 KB Output is correct
2 Correct 120 ms 13560 KB Output is correct
3 Correct 93 ms 17144 KB Output is correct
4 Correct 55 ms 15092 KB Output is correct
5 Correct 67 ms 23160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 7424 KB Output is correct
2 Correct 10 ms 7424 KB Output is correct
3 Correct 10 ms 7424 KB Output is correct
4 Correct 9 ms 7424 KB Output is correct
5 Correct 10 ms 7424 KB Output is correct
6 Correct 9 ms 7552 KB Output is correct
7 Correct 10 ms 7424 KB Output is correct
8 Correct 9 ms 7296 KB Output is correct
9 Correct 9 ms 7424 KB Output is correct
10 Correct 106 ms 18552 KB Output is correct
11 Correct 103 ms 17272 KB Output is correct
12 Correct 89 ms 17016 KB Output is correct
13 Correct 52 ms 15096 KB Output is correct
14 Correct 66 ms 23160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 8320 KB Output is correct
2 Correct 46 ms 11128 KB Output is correct
3 Correct 44 ms 11104 KB Output is correct
4 Correct 50 ms 11256 KB Output is correct
5 Correct 17 ms 9596 KB Output is correct
6 Correct 43 ms 13048 KB Output is correct
7 Correct 40 ms 16376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 7424 KB Output is correct
2 Correct 10 ms 7424 KB Output is correct
3 Correct 10 ms 7424 KB Output is correct
4 Correct 9 ms 7424 KB Output is correct
5 Correct 10 ms 7424 KB Output is correct
6 Correct 9 ms 7552 KB Output is correct
7 Correct 10 ms 7424 KB Output is correct
8 Correct 9 ms 7296 KB Output is correct
9 Correct 9 ms 7424 KB Output is correct
10 Correct 9 ms 7552 KB Output is correct
11 Correct 9 ms 7552 KB Output is correct
12 Correct 9 ms 7552 KB Output is correct
13 Correct 81 ms 24952 KB Output is correct
14 Correct 98 ms 28920 KB Output is correct
15 Correct 93 ms 24952 KB Output is correct
16 Correct 106 ms 18552 KB Output is correct
17 Correct 103 ms 17272 KB Output is correct
18 Correct 89 ms 17016 KB Output is correct
19 Correct 52 ms 15096 KB Output is correct
20 Correct 66 ms 23160 KB Output is correct
21 Correct 42 ms 11640 KB Output is correct
22 Correct 131 ms 21240 KB Output is correct
23 Correct 124 ms 25464 KB Output is correct
24 Correct 200 ms 37112 KB Output is correct
25 Correct 114 ms 20080 KB Output is correct
26 Correct 137 ms 22648 KB Output is correct
27 Correct 106 ms 21116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 7424 KB Output is correct
2 Correct 10 ms 7424 KB Output is correct
3 Correct 10 ms 7424 KB Output is correct
4 Correct 9 ms 7424 KB Output is correct
5 Correct 10 ms 7424 KB Output is correct
6 Correct 9 ms 7552 KB Output is correct
7 Correct 10 ms 7424 KB Output is correct
8 Correct 9 ms 7296 KB Output is correct
9 Correct 9 ms 7424 KB Output is correct
10 Correct 84 ms 18296 KB Output is correct
11 Correct 66 ms 18680 KB Output is correct
12 Correct 185 ms 37804 KB Output is correct
13 Correct 111 ms 20852 KB Output is correct
14 Correct 153 ms 22392 KB Output is correct
15 Correct 9 ms 7552 KB Output is correct
16 Correct 9 ms 7552 KB Output is correct
17 Correct 9 ms 7552 KB Output is correct
18 Correct 81 ms 24952 KB Output is correct
19 Correct 98 ms 28920 KB Output is correct
20 Correct 93 ms 24952 KB Output is correct
21 Correct 95 ms 14456 KB Output is correct
22 Correct 120 ms 13560 KB Output is correct
23 Correct 93 ms 17144 KB Output is correct
24 Correct 55 ms 15092 KB Output is correct
25 Correct 67 ms 23160 KB Output is correct
26 Correct 106 ms 18552 KB Output is correct
27 Correct 103 ms 17272 KB Output is correct
28 Correct 89 ms 17016 KB Output is correct
29 Correct 52 ms 15096 KB Output is correct
30 Correct 66 ms 23160 KB Output is correct
31 Correct 17 ms 8320 KB Output is correct
32 Correct 46 ms 11128 KB Output is correct
33 Correct 44 ms 11104 KB Output is correct
34 Correct 50 ms 11256 KB Output is correct
35 Correct 17 ms 9596 KB Output is correct
36 Correct 43 ms 13048 KB Output is correct
37 Correct 40 ms 16376 KB Output is correct
38 Correct 42 ms 11640 KB Output is correct
39 Correct 131 ms 21240 KB Output is correct
40 Correct 124 ms 25464 KB Output is correct
41 Correct 200 ms 37112 KB Output is correct
42 Correct 114 ms 20080 KB Output is correct
43 Correct 137 ms 22648 KB Output is correct
44 Correct 106 ms 21116 KB Output is correct
45 Correct 37 ms 11384 KB Output is correct
46 Correct 109 ms 20600 KB Output is correct
47 Correct 116 ms 24184 KB Output is correct
48 Correct 192 ms 34168 KB Output is correct
49 Correct 109 ms 20848 KB Output is correct
50 Correct 169 ms 22648 KB Output is correct
51 Correct 124 ms 21240 KB Output is correct