답안 #39362

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
39362 2018-01-12T15:13:50 Z Filyan Fireworks (APIO16_fireworks) C++14
100 / 100
439 ms 114380 KB
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<functional>
using namespace std;

#define sz(x) (int)(x.size())
#define fi(a,b) for(int i=a;i<b;++i)
#define fj(a,b) for(int j=a;j<b;++j)
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
/////////////////////////

int const N = 3e5 + 41;

int n, m;
vector<pii> e[N];
ll ans;

struct Fun{
	priority_queue<ll> l;
	priority_queue<ll, vector<ll>, greater<ll> > r;
	ll addl, addr;
	ll ans;

	Fun(){
		addl = addr = ans = 0;
	};

	Fun(int c){
		addl = addr = ans = 0;
		l.push(c);
		r.push(c);
	}

	int getq(){
		return sz(l) + sz(r);
	}

	ll getleftmostmin(){
		ll x = (l.top());
		return x + addl;
	}

	ll getrightmostmin(){
		ll x = (r.top());
		return x + addr;
	}

	ll extractleftmostmin(){
		ll x = (l.top());
		l.pop();
		return x + addl;
	}

	ll extractrightmostmin(){
		ll x = (r.top());
		r.pop();
		return x + addr;
	}

	void pushrightpoint(ll x){
		if(x >= getleftmostmin()){
			r.push(x - addr);
		}else{
			ll lx = extractleftmostmin();
			ans += lx - x;
			l.push(x - addl);
			r.push(lx - addr);
		}
	}

	void pushright(priority_queue<ll, vector<ll>, greater<ll> > &rp, ll addright){
		while(!rp.empty()){
			ll x = rp.top();
			rp.pop();
			pushrightpoint(x + addright);
		}
	}

	void pushleftpoint(ll x){
		if(x <= getrightmostmin()){
			l.push(x - addl);
		}else{
			ll rx = extractrightmostmin();
			ans += x - rx;
			l.push(rx - addl);
			r.push(x - addr);
		}
	}

	void pushleft(priority_queue<ll> &lp, ll addleft){
		while(!lp.empty()){
			ll x = lp.top();
			lp.pop();
			pushleftpoint(x + addleft);
		}
	}

	void shift(ll s){
		addr += s;
		addl += s;
		ll lx = extractleftmostmin();
		addl -= s;
		l.push(lx - addl);
	}

	void relax(){
		ll x = r.top();
		while(!r.empty()) r.pop();
		r.push(x);
	}
};

Fun *f[N];

void dfs(int x, int p = -1, int c = 0){
	if(x >= n){
		f[x] = new Fun(c);
		return;
	}
	f[x] = new Fun();
	int id = x;
	for(pii e : ::e[x]){
		int y = e.first;
		if(y == p) continue;
		int c = e.second;
		dfs(y, x, c);
		if(f[x]->getq() < f[y]->getq()){
			f[x] = f[y];
			id = y;
		}
	}
	for(pii e : ::e[x]){
		int y = e.first;
		int c = e.second;
		if(y == id) continue;
		if(y == p) continue;
		f[x]->ans += f[y]->ans;
		f[x]->pushleft(f[y]->l, f[y]->addl);
		f[x]->pushright(f[y]->r, f[y]->addr);
	}

	if(p != -1){
		f[x]->shift(c);
		f[x]->relax();
	}
	//cerr << x << " " << f[x]->ans << endl;
}

void solve(){
	fi(0, n) f[i] = new Fun();
	dfs(0);
	ans = f[0]->ans;
}

int main(){
#ifdef _DEBUG
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
#endif

	scanf("%d %d",&n,&m);
	fi(1, n+m){
		int p, c;
		scanf("%d %d",&p,&c);
		--p;
		e[p].pb(mp(i, c));
		e[i].pb(mp(p, c));
	}

	solve();

	printf("%lld\n",ans);

	return 0;
}

Compilation message

fireworks.cpp: In function 'void dfs(int, int, int)':
fireworks.cpp:139:7: warning: unused variable 'c' [-Wunused-variable]
   int c = e.second;
       ^
fireworks.cpp: In function 'int main()':
fireworks.cpp:166:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d",&n,&m);
                      ^
fireworks.cpp:169:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d",&p,&c);
                       ^
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 11396 KB Output is correct
2 Correct 3 ms 11396 KB Output is correct
3 Correct 0 ms 11396 KB Output is correct
4 Correct 0 ms 11396 KB Output is correct
5 Correct 0 ms 11396 KB Output is correct
6 Correct 0 ms 11396 KB Output is correct
7 Correct 0 ms 11396 KB Output is correct
8 Correct 3 ms 11396 KB Output is correct
9 Correct 3 ms 11396 KB Output is correct
10 Correct 0 ms 11396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 11396 KB Output is correct
2 Correct 0 ms 11396 KB Output is correct
3 Correct 0 ms 11396 KB Output is correct
4 Correct 0 ms 11396 KB Output is correct
5 Correct 3 ms 11396 KB Output is correct
6 Correct 0 ms 11396 KB Output is correct
7 Correct 0 ms 11396 KB Output is correct
8 Correct 0 ms 11396 KB Output is correct
9 Correct 0 ms 11396 KB Output is correct
10 Correct 0 ms 11396 KB Output is correct
11 Correct 0 ms 11528 KB Output is correct
12 Correct 0 ms 11528 KB Output is correct
13 Correct 3 ms 11528 KB Output is correct
14 Correct 0 ms 11528 KB Output is correct
15 Correct 0 ms 11528 KB Output is correct
16 Correct 0 ms 11528 KB Output is correct
17 Correct 0 ms 11528 KB Output is correct
18 Correct 3 ms 11528 KB Output is correct
19 Correct 0 ms 11528 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 11396 KB Output is correct
2 Correct 3 ms 11396 KB Output is correct
3 Correct 0 ms 11396 KB Output is correct
4 Correct 0 ms 11396 KB Output is correct
5 Correct 0 ms 11396 KB Output is correct
6 Correct 0 ms 11396 KB Output is correct
7 Correct 0 ms 11396 KB Output is correct
8 Correct 3 ms 11396 KB Output is correct
9 Correct 3 ms 11396 KB Output is correct
10 Correct 0 ms 11396 KB Output is correct
11 Correct 0 ms 11396 KB Output is correct
12 Correct 0 ms 11396 KB Output is correct
13 Correct 0 ms 11396 KB Output is correct
14 Correct 0 ms 11396 KB Output is correct
15 Correct 3 ms 11396 KB Output is correct
16 Correct 0 ms 11396 KB Output is correct
17 Correct 0 ms 11396 KB Output is correct
18 Correct 0 ms 11396 KB Output is correct
19 Correct 0 ms 11396 KB Output is correct
20 Correct 0 ms 11396 KB Output is correct
21 Correct 0 ms 11528 KB Output is correct
22 Correct 0 ms 11528 KB Output is correct
23 Correct 3 ms 11528 KB Output is correct
24 Correct 0 ms 11528 KB Output is correct
25 Correct 0 ms 11528 KB Output is correct
26 Correct 0 ms 11528 KB Output is correct
27 Correct 0 ms 11528 KB Output is correct
28 Correct 3 ms 11528 KB Output is correct
29 Correct 0 ms 11528 KB Output is correct
30 Correct 0 ms 11528 KB Output is correct
31 Correct 3 ms 11660 KB Output is correct
32 Correct 3 ms 11792 KB Output is correct
33 Correct 6 ms 11924 KB Output is correct
34 Correct 0 ms 12056 KB Output is correct
35 Correct 6 ms 12188 KB Output is correct
36 Correct 3 ms 12320 KB Output is correct
37 Correct 6 ms 12452 KB Output is correct
38 Correct 6 ms 12456 KB Output is correct
39 Correct 3 ms 12464 KB Output is correct
40 Correct 3 ms 12952 KB Output is correct
41 Correct 3 ms 12952 KB Output is correct
42 Correct 3 ms 12452 KB Output is correct
43 Correct 3 ms 12640 KB Output is correct
44 Correct 6 ms 12556 KB Output is correct
45 Correct 0 ms 12560 KB Output is correct
46 Correct 3 ms 12584 KB Output is correct
47 Correct 6 ms 12584 KB Output is correct
48 Correct 6 ms 12472 KB Output is correct
49 Correct 3 ms 12588 KB Output is correct
50 Correct 6 ms 12484 KB Output is correct
51 Correct 3 ms 12460 KB Output is correct
52 Correct 6 ms 12456 KB Output is correct
53 Correct 9 ms 12476 KB Output is correct
54 Correct 6 ms 12624 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 11396 KB Output is correct
2 Correct 3 ms 11396 KB Output is correct
3 Correct 0 ms 11396 KB Output is correct
4 Correct 0 ms 11396 KB Output is correct
5 Correct 0 ms 11396 KB Output is correct
6 Correct 0 ms 11396 KB Output is correct
7 Correct 0 ms 11396 KB Output is correct
8 Correct 3 ms 11396 KB Output is correct
9 Correct 3 ms 11396 KB Output is correct
10 Correct 0 ms 11396 KB Output is correct
11 Correct 0 ms 11396 KB Output is correct
12 Correct 0 ms 11396 KB Output is correct
13 Correct 0 ms 11396 KB Output is correct
14 Correct 0 ms 11396 KB Output is correct
15 Correct 3 ms 11396 KB Output is correct
16 Correct 0 ms 11396 KB Output is correct
17 Correct 0 ms 11396 KB Output is correct
18 Correct 0 ms 11396 KB Output is correct
19 Correct 0 ms 11396 KB Output is correct
20 Correct 0 ms 11396 KB Output is correct
21 Correct 0 ms 11528 KB Output is correct
22 Correct 0 ms 11528 KB Output is correct
23 Correct 3 ms 11528 KB Output is correct
24 Correct 0 ms 11528 KB Output is correct
25 Correct 0 ms 11528 KB Output is correct
26 Correct 0 ms 11528 KB Output is correct
27 Correct 0 ms 11528 KB Output is correct
28 Correct 3 ms 11528 KB Output is correct
29 Correct 0 ms 11528 KB Output is correct
30 Correct 0 ms 11528 KB Output is correct
31 Correct 3 ms 11660 KB Output is correct
32 Correct 3 ms 11792 KB Output is correct
33 Correct 6 ms 11924 KB Output is correct
34 Correct 0 ms 12056 KB Output is correct
35 Correct 6 ms 12188 KB Output is correct
36 Correct 3 ms 12320 KB Output is correct
37 Correct 6 ms 12452 KB Output is correct
38 Correct 6 ms 12456 KB Output is correct
39 Correct 3 ms 12464 KB Output is correct
40 Correct 3 ms 12952 KB Output is correct
41 Correct 3 ms 12952 KB Output is correct
42 Correct 3 ms 12452 KB Output is correct
43 Correct 3 ms 12640 KB Output is correct
44 Correct 6 ms 12556 KB Output is correct
45 Correct 0 ms 12560 KB Output is correct
46 Correct 3 ms 12584 KB Output is correct
47 Correct 6 ms 12584 KB Output is correct
48 Correct 6 ms 12472 KB Output is correct
49 Correct 3 ms 12588 KB Output is correct
50 Correct 6 ms 12484 KB Output is correct
51 Correct 3 ms 12460 KB Output is correct
52 Correct 6 ms 12456 KB Output is correct
53 Correct 9 ms 12476 KB Output is correct
54 Correct 6 ms 12624 KB Output is correct
55 Correct 6 ms 14304 KB Output is correct
56 Correct 83 ms 22664 KB Output is correct
57 Correct 106 ms 30508 KB Output is correct
58 Correct 166 ms 35840 KB Output is correct
59 Correct 193 ms 43944 KB Output is correct
60 Correct 229 ms 51792 KB Output is correct
61 Correct 256 ms 57836 KB Output is correct
62 Correct 309 ms 62636 KB Output is correct
63 Correct 396 ms 72464 KB Output is correct
64 Correct 439 ms 75456 KB Output is correct
65 Correct 179 ms 114380 KB Output is correct
66 Correct 199 ms 114372 KB Output is correct
67 Correct 239 ms 77000 KB Output is correct
68 Correct 259 ms 95328 KB Output is correct
69 Correct 333 ms 91912 KB Output is correct
70 Correct 363 ms 91916 KB Output is correct
71 Correct 296 ms 87380 KB Output is correct
72 Correct 289 ms 87112 KB Output is correct
73 Correct 239 ms 83228 KB Output is correct
74 Correct 266 ms 83392 KB Output is correct
75 Correct 273 ms 83876 KB Output is correct
76 Correct 259 ms 84040 KB Output is correct
77 Correct 256 ms 82620 KB Output is correct
78 Correct 309 ms 79392 KB Output is correct
79 Correct 203 ms 85484 KB Output is correct