Submission #254485

# Submission time Handle Problem Language Result Execution time Memory
254485 2020-07-30T06:05:56 Z errorgorn Fireworks (APIO16_fireworks) C++14
0 / 100
11 ms 16768 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,m;
vector<int> al[300005];
ll d[300005];

ll w[300005];
priority_queue<ll> pq[300005];

void dfs(int i){
	if (i>n){
		pq[i].push(0);
		pq[i].push(0);
	}
	else{
		for (auto &it:al[i]){
			dfs(it);
			d[i]+=d[it];
			
			if (pq[i].empty()) swap(pq[i],pq[it]);
			else{
				if (sz(pq[i])<sz(pq[it])) swap(pq[i],pq[it]);
				
				int temp=min(pq[i].top(),pq[it].top());
				
				while (!pq[it].empty()){
					pq[i].push(pq[it].top());
					pq[it].pop();
				}
				
				pq[i].pop();
				d[i]+=pq[i].top()-temp;
			}
		}
	}
	
	int t1,t2;
	t1=pq[i].top(),pq[i].pop();
	t2=pq[i].top(),pq[i].pop();
	pq[i].push(t1+w[i]);
	pq[i].push(t2+w[i]);
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	
	cin>>n>>m;
	
	int a,b;
	rep(x,2,n+m+1){
		cin>>a>>b;
		al[a].push_back(x);
		w[x]=b;
	}
	
	dfs(1);
	cout<<d[1]<<endl;
}
# Verdict Execution time Memory Grader output
1 Correct 10 ms 16768 KB Output is correct
2 Incorrect 11 ms 16768 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 9 ms 16768 KB Output is correct
2 Correct 9 ms 16768 KB Output is correct
3 Incorrect 10 ms 16768 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 10 ms 16768 KB Output is correct
2 Incorrect 11 ms 16768 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 10 ms 16768 KB Output is correct
2 Incorrect 11 ms 16768 KB Output isn't correct
3 Halted 0 ms 0 KB -