답안 #336662

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
336662 2020-12-16T09:33:40 Z syy Candies (JOI18_candies) C++17
100 / 100
167 ms 14836 KB
/* Transition from k to k+1 will always be
 * ...0101000... to ...0101010..., or
 * ...0101010... to ...1010101...
 * Thus, store the possible states that it can go in a pq
 */
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define FOR(i, a, b) for(ll i = (ll)a; i <= (ll)b; i++)
#define DEC(i, a, b) for(ll i = (ll)a; i >= (ll)b; i--)
typedef pair<ll, ll> pi;
typedef pair<pi, ll> pii;
typedef pair<ll, pi> ipi;
typedef pair<pi, pi> pipi;
#define f first
#define s second
typedef vector<ll> vi;
typedef vector<pi> vpi;
typedef vector<pii> vpii;
#define pb push_back
#define pf push_front
#define all(v) v.begin(), v.end()
#define size(v) (ll) v.size()
#define disc(v) sort(all(v)); v.resize(unique(all(v)) - v.begin());
#define INF (ll) 1e9 + 100
#define LLINF (ll) 1e18
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define sandybridge __attribute__((optimize("Ofast"), target("arch=sandybridge")))
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());    //can be used by calling rng() or shuffle(A, A+n, rng)
inline ll rand(ll x, ll y) { ++y; return (rng() % (y-x)) + x; } //inclusivesss

ll n, arr[200005], ss[200005], p[200005], L[200005], R[200005], ans;
bool vis[200005];
priority_queue<ipi> pq;

inline ll getans(ll x, ll y) {
	return (y % 2 ? ss[y] - ss[x-1] : ss[x-1] - ss[y]);
}

ll fs(ll x) {
	if (p[x] == x) return x;
	return p[x] = fs(p[x]);
}

int main() {
	fastio; cin >> n;
	FOR(i, 1, n) {
		cin >> arr[i];
		ss[i] = ss[i-1] + (i % 2 ? arr[i] : -arr[i]);
		p[i] = L[i] = R[i] = i;
		pq.push(ipi(arr[i], pi(i, i)));
	}
	FOR(i, 1, (n+1)/2) {
		ll l, r;
		while (true) {
			l = pq.top().s.f;
			r = pq.top().s.s;
			pq.pop();
			if (!vis[l-1] and !vis[r+1]) break;
		}
		ans += getans(l, r);
		cout << ans << "\n";
		vis[l] = 1;
		vis[r] = 1;
		// because at every step you expand at most 1
		// you only need to set vis of l and r as
		// all the intermediate nodes must have been set before
		ll par;
		if (l == r) par = fs(l);
		else {
			par = fs(l+1);
			p[fs(l)] = par;
			p[fs(r)] = par;
			L[par] = l, R[par] = r;
		}
		if (vis[r+2]) {
			R[par] = R[fs(r+2)];
			p[fs(r+2)] = par;
		}
		if (l > 1 and vis[l-2]) {
			L[par] = L[fs(l-2)];
			p[fs(l-2)] = par;
		}
		if (L[par] > 1 and R[par] < n) pq.push(ipi(getans(L[par]-1, R[par]+1), pi(L[par]-1, R[par]+1)));
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 620 KB Output is correct
2 Correct 1 ms 620 KB Output is correct
3 Correct 2 ms 620 KB Output is correct
4 Correct 1 ms 620 KB Output is correct
5 Correct 1 ms 620 KB Output is correct
6 Correct 1 ms 620 KB Output is correct
7 Correct 1 ms 620 KB Output is correct
8 Correct 1 ms 620 KB Output is correct
9 Correct 1 ms 620 KB Output is correct
10 Correct 1 ms 620 KB Output is correct
11 Correct 1 ms 620 KB Output is correct
12 Correct 1 ms 620 KB Output is correct
13 Correct 1 ms 620 KB Output is correct
14 Correct 1 ms 620 KB Output is correct
15 Correct 2 ms 620 KB Output is correct
16 Correct 2 ms 620 KB Output is correct
17 Correct 2 ms 620 KB Output is correct
18 Correct 1 ms 620 KB Output is correct
19 Correct 1 ms 620 KB Output is correct
20 Correct 1 ms 620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 620 KB Output is correct
2 Correct 1 ms 620 KB Output is correct
3 Correct 2 ms 620 KB Output is correct
4 Correct 1 ms 620 KB Output is correct
5 Correct 1 ms 620 KB Output is correct
6 Correct 1 ms 620 KB Output is correct
7 Correct 1 ms 620 KB Output is correct
8 Correct 1 ms 620 KB Output is correct
9 Correct 1 ms 620 KB Output is correct
10 Correct 1 ms 620 KB Output is correct
11 Correct 1 ms 620 KB Output is correct
12 Correct 1 ms 620 KB Output is correct
13 Correct 1 ms 620 KB Output is correct
14 Correct 1 ms 620 KB Output is correct
15 Correct 2 ms 620 KB Output is correct
16 Correct 2 ms 620 KB Output is correct
17 Correct 2 ms 620 KB Output is correct
18 Correct 1 ms 620 KB Output is correct
19 Correct 1 ms 620 KB Output is correct
20 Correct 1 ms 620 KB Output is correct
21 Correct 166 ms 14672 KB Output is correct
22 Correct 164 ms 14672 KB Output is correct
23 Correct 166 ms 14800 KB Output is correct
24 Correct 111 ms 14672 KB Output is correct
25 Correct 111 ms 14712 KB Output is correct
26 Correct 112 ms 14672 KB Output is correct
27 Correct 121 ms 14800 KB Output is correct
28 Correct 120 ms 14672 KB Output is correct
29 Correct 123 ms 14800 KB Output is correct
30 Correct 124 ms 14672 KB Output is correct
31 Correct 123 ms 14672 KB Output is correct
32 Correct 123 ms 14672 KB Output is correct
33 Correct 140 ms 14800 KB Output is correct
34 Correct 136 ms 14672 KB Output is correct
35 Correct 139 ms 14672 KB Output is correct
36 Correct 165 ms 14800 KB Output is correct
37 Correct 167 ms 14672 KB Output is correct
38 Correct 163 ms 14672 KB Output is correct
39 Correct 112 ms 14672 KB Output is correct
40 Correct 110 ms 14672 KB Output is correct
41 Correct 113 ms 14800 KB Output is correct
42 Correct 125 ms 14672 KB Output is correct
43 Correct 124 ms 14672 KB Output is correct
44 Correct 120 ms 14672 KB Output is correct
45 Correct 124 ms 14672 KB Output is correct
46 Correct 125 ms 14672 KB Output is correct
47 Correct 135 ms 14676 KB Output is correct
48 Correct 138 ms 14672 KB Output is correct
49 Correct 146 ms 14836 KB Output is correct
50 Correct 139 ms 14672 KB Output is correct