Submission #44217

# Submission time Handle Problem Language Result Execution time Memory
44217 2018-03-30T14:54:30 Z Filyan Fireworks (APIO16_fireworks) C++14
100 / 100
733 ms 168124 KB
#include <bits/stdc++.h> 
using namespace std;

#define sz(x) (int)x.size() 
#define pb push_back 
#define mp make_pair 
#define fi(a, b) for(int i=a; i<=b; i++) 
#define fj(a, b) for(int j=a; j<=b; j++) 
#define fo(a, b) for(int o=a; o<=b; o++) 
#define fdi(a, b) for(int i=a; i>=b; i--) 
#define fdj(a, b) for(int j=a; j>=b; j--) 
#define fdo(a, b) for(int o=a; o>=b; o--) 

#ifdef LOCAL
#define err(...) fprintf(stderr, __VA_ARGS__)
#else
#define err(...) while(false) {}
#endif

typedef long long ll; 
typedef pair<int, int> pii; 
typedef pair<ll, ll> pll; 
typedef vector<int> vi; 
typedef vector<pii> vpii; 
typedef vector<pll> vpll; 
typedef long double ld;

/////////////////////////////////

int const MAX = 300 * 1000 + 41;

int n, m;
vpii e[MAX];

struct State {
	multiset<ll> l, r;
	ll addl, addr, ans;

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

	void normalize() {
		while (sz(r) > 1) {
			auto it = r.end();
			it--;
			r.erase(it);		
		}
	}

	ll getleft() {
		auto it = l.end();
		--it;
		ll res = (*it) + addl;
		return res;		
	}

	ll extractleft() {
		ll res = getleft();
		auto it = l.end();
		--it;
		l.erase(it);
		return res;
	}

	ll getright() {
		auto it = r.begin();
		ll res = (*it) + addr;
		return res;		
	}

	ll extractright() {
		ll res = getright();
		auto it = r.begin();
		r.erase(it);
		return res;
	}

	void addedge(int x) {
		addr += x;
		ll y = extractleft();	
		l.insert(y - addl + x);
	}

	void pushl(ll x) {
		l.insert(x - addl);
	}	

	void pushr(ll x) {
		r.insert(x - addr);
	}

};

State st[MAX];

void dfs(int x) {
	if (x >= n + 1) {
		st[x].l.insert(0);
		st[x].r.insert(0);
		return;
	}	
	for (pii z : e[x]) {
		int y = z.first;
		dfs(y);
	}
	ll sum = 0;
	for (pii z : e[x]) {
		int y = z.first;
		int c = z.second;
		sum += st[y].ans;
		st[y].addedge(c);
		st[y].normalize();
	}
	for (pii z : e[x]) {
		int y = z.first;
		if (sz(st[y].l) > sz(st[x].l)) {	
			swap(st[x], st[y]);
		}
	}
	st[x].ans = sum;
	for (pii z : e[x]) {
		int y = z.first;
		while (sz(st[y].l)) {
			ll p = st[y].extractleft();
			if (p <= st[x].getright()) {
				st[x].pushl(p);
			} else {
				st[x].ans += p - st[x].getright();
				st[x].pushl(st[x].extractright());
				st[x].pushr(p);
			}
		}	
		while (sz(st[y].r)) {
			ll p = st[y].extractright();
			if (p >= st[x].getleft()) {
				st[x].pushr(p);
			} else {
				st[x].ans += st[x].getleft() - p;
				st[x].pushr(st[x].extractleft());
				st[x].pushl(p);
			}
		}	
	}
}

void solve() {
	dfs(1);
	printf("%lld\n", st[1].ans);
}

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

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

	
#ifdef LOCAL
	err("ELAPSED TIME: %.3Lf\n", (ld) clock() / CLOCKS_PER_SEC);
#endif	
	
	return 0;
}

Compilation message

fireworks.cpp: In function 'int main()':
fireworks.cpp:158:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &n, &m);
  ~~~~~^~~~~~~~~~~~~~~~~
fireworks.cpp:161:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &p, &c);
   ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 35 ms 42744 KB Output is correct
2 Correct 34 ms 42744 KB Output is correct
3 Correct 34 ms 42920 KB Output is correct
4 Correct 34 ms 42920 KB Output is correct
5 Correct 34 ms 42924 KB Output is correct
6 Correct 34 ms 42924 KB Output is correct
7 Correct 34 ms 42924 KB Output is correct
8 Correct 35 ms 43112 KB Output is correct
9 Correct 34 ms 43112 KB Output is correct
10 Correct 35 ms 43124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 43124 KB Output is correct
2 Correct 35 ms 43132 KB Output is correct
3 Correct 33 ms 43132 KB Output is correct
4 Correct 33 ms 43132 KB Output is correct
5 Correct 35 ms 43132 KB Output is correct
6 Correct 34 ms 43140 KB Output is correct
7 Correct 34 ms 43140 KB Output is correct
8 Correct 35 ms 43140 KB Output is correct
9 Correct 34 ms 43148 KB Output is correct
10 Correct 34 ms 43196 KB Output is correct
11 Correct 35 ms 43204 KB Output is correct
12 Correct 36 ms 43208 KB Output is correct
13 Correct 35 ms 43212 KB Output is correct
14 Correct 35 ms 43216 KB Output is correct
15 Correct 35 ms 43220 KB Output is correct
16 Correct 34 ms 43224 KB Output is correct
17 Correct 34 ms 43228 KB Output is correct
18 Correct 34 ms 43228 KB Output is correct
19 Correct 36 ms 43272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 42744 KB Output is correct
2 Correct 34 ms 42744 KB Output is correct
3 Correct 34 ms 42920 KB Output is correct
4 Correct 34 ms 42920 KB Output is correct
5 Correct 34 ms 42924 KB Output is correct
6 Correct 34 ms 42924 KB Output is correct
7 Correct 34 ms 42924 KB Output is correct
8 Correct 35 ms 43112 KB Output is correct
9 Correct 34 ms 43112 KB Output is correct
10 Correct 35 ms 43124 KB Output is correct
11 Correct 34 ms 43124 KB Output is correct
12 Correct 35 ms 43132 KB Output is correct
13 Correct 33 ms 43132 KB Output is correct
14 Correct 33 ms 43132 KB Output is correct
15 Correct 35 ms 43132 KB Output is correct
16 Correct 34 ms 43140 KB Output is correct
17 Correct 34 ms 43140 KB Output is correct
18 Correct 35 ms 43140 KB Output is correct
19 Correct 34 ms 43148 KB Output is correct
20 Correct 34 ms 43196 KB Output is correct
21 Correct 35 ms 43204 KB Output is correct
22 Correct 36 ms 43208 KB Output is correct
23 Correct 35 ms 43212 KB Output is correct
24 Correct 35 ms 43216 KB Output is correct
25 Correct 35 ms 43220 KB Output is correct
26 Correct 34 ms 43224 KB Output is correct
27 Correct 34 ms 43228 KB Output is correct
28 Correct 34 ms 43228 KB Output is correct
29 Correct 36 ms 43272 KB Output is correct
30 Correct 35 ms 43496 KB Output is correct
31 Correct 36 ms 43496 KB Output is correct
32 Correct 37 ms 43524 KB Output is correct
33 Correct 37 ms 43680 KB Output is correct
34 Correct 37 ms 43680 KB Output is correct
35 Correct 39 ms 43828 KB Output is correct
36 Correct 38 ms 44000 KB Output is correct
37 Correct 42 ms 44000 KB Output is correct
38 Correct 40 ms 44008 KB Output is correct
39 Correct 39 ms 44112 KB Output is correct
40 Correct 39 ms 45400 KB Output is correct
41 Correct 38 ms 45540 KB Output is correct
42 Correct 38 ms 45540 KB Output is correct
43 Correct 39 ms 45540 KB Output is correct
44 Correct 39 ms 45540 KB Output is correct
45 Correct 40 ms 45540 KB Output is correct
46 Correct 40 ms 45540 KB Output is correct
47 Correct 40 ms 45540 KB Output is correct
48 Correct 41 ms 45540 KB Output is correct
49 Correct 41 ms 45540 KB Output is correct
50 Correct 40 ms 45540 KB Output is correct
51 Correct 39 ms 45540 KB Output is correct
52 Correct 40 ms 45540 KB Output is correct
53 Correct 39 ms 45540 KB Output is correct
54 Correct 40 ms 45540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 42744 KB Output is correct
2 Correct 34 ms 42744 KB Output is correct
3 Correct 34 ms 42920 KB Output is correct
4 Correct 34 ms 42920 KB Output is correct
5 Correct 34 ms 42924 KB Output is correct
6 Correct 34 ms 42924 KB Output is correct
7 Correct 34 ms 42924 KB Output is correct
8 Correct 35 ms 43112 KB Output is correct
9 Correct 34 ms 43112 KB Output is correct
10 Correct 35 ms 43124 KB Output is correct
11 Correct 34 ms 43124 KB Output is correct
12 Correct 35 ms 43132 KB Output is correct
13 Correct 33 ms 43132 KB Output is correct
14 Correct 33 ms 43132 KB Output is correct
15 Correct 35 ms 43132 KB Output is correct
16 Correct 34 ms 43140 KB Output is correct
17 Correct 34 ms 43140 KB Output is correct
18 Correct 35 ms 43140 KB Output is correct
19 Correct 34 ms 43148 KB Output is correct
20 Correct 34 ms 43196 KB Output is correct
21 Correct 35 ms 43204 KB Output is correct
22 Correct 36 ms 43208 KB Output is correct
23 Correct 35 ms 43212 KB Output is correct
24 Correct 35 ms 43216 KB Output is correct
25 Correct 35 ms 43220 KB Output is correct
26 Correct 34 ms 43224 KB Output is correct
27 Correct 34 ms 43228 KB Output is correct
28 Correct 34 ms 43228 KB Output is correct
29 Correct 36 ms 43272 KB Output is correct
30 Correct 35 ms 43496 KB Output is correct
31 Correct 36 ms 43496 KB Output is correct
32 Correct 37 ms 43524 KB Output is correct
33 Correct 37 ms 43680 KB Output is correct
34 Correct 37 ms 43680 KB Output is correct
35 Correct 39 ms 43828 KB Output is correct
36 Correct 38 ms 44000 KB Output is correct
37 Correct 42 ms 44000 KB Output is correct
38 Correct 40 ms 44008 KB Output is correct
39 Correct 39 ms 44112 KB Output is correct
40 Correct 39 ms 45400 KB Output is correct
41 Correct 38 ms 45540 KB Output is correct
42 Correct 38 ms 45540 KB Output is correct
43 Correct 39 ms 45540 KB Output is correct
44 Correct 39 ms 45540 KB Output is correct
45 Correct 40 ms 45540 KB Output is correct
46 Correct 40 ms 45540 KB Output is correct
47 Correct 40 ms 45540 KB Output is correct
48 Correct 41 ms 45540 KB Output is correct
49 Correct 41 ms 45540 KB Output is correct
50 Correct 40 ms 45540 KB Output is correct
51 Correct 39 ms 45540 KB Output is correct
52 Correct 40 ms 45540 KB Output is correct
53 Correct 39 ms 45540 KB Output is correct
54 Correct 40 ms 45540 KB Output is correct
55 Correct 62 ms 45660 KB Output is correct
56 Correct 92 ms 47816 KB Output is correct
57 Correct 178 ms 50608 KB Output is correct
58 Correct 184 ms 53232 KB Output is correct
59 Correct 235 ms 56792 KB Output is correct
60 Correct 283 ms 60880 KB Output is correct
61 Correct 324 ms 65048 KB Output is correct
62 Correct 368 ms 69380 KB Output is correct
63 Correct 446 ms 75096 KB Output is correct
64 Correct 456 ms 80224 KB Output is correct
65 Correct 238 ms 163196 KB Output is correct
66 Correct 233 ms 168124 KB Output is correct
67 Correct 258 ms 168124 KB Output is correct
68 Correct 361 ms 168124 KB Output is correct
69 Correct 440 ms 168124 KB Output is correct
70 Correct 427 ms 168124 KB Output is correct
71 Correct 567 ms 168124 KB Output is correct
72 Correct 580 ms 168124 KB Output is correct
73 Correct 616 ms 168124 KB Output is correct
74 Correct 600 ms 168124 KB Output is correct
75 Correct 620 ms 168124 KB Output is correct
76 Correct 625 ms 168124 KB Output is correct
77 Correct 612 ms 168124 KB Output is correct
78 Correct 690 ms 168124 KB Output is correct
79 Correct 733 ms 168124 KB Output is correct