답안 #251635

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
251635 2020-07-22T05:43:07 Z errorgorn Candies (JOI18_candies) C++14
100 / 100
152 ms 14056 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<ll,ii>
#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;
ll arr[200005];
ll pref[200005];

priority_queue<iii> pq;
bool vis[200005];

ll get(int l,int r){
	if (r&1) return pref[r]-pref[l-1];
	else return pref[l-1]-pref[r];
}

ii range[200005];
int p[200005];

ii merge(ii i,ii j){
	return ii(min(i.fi,j.fi),max(i.se,j.se));
}

int parent(int i){
	if (p[i]==i) return i;
	else return p[i]=parent(p[i]);
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	
	cin>>n;
	rep(x,1,n+1){
		cin>>arr[x];
		pref[x]=pref[x-1]+(x&1?arr[x]:-arr[x]);
		pq.push(iii(arr[x],ii(x,x)));
		
		p[x]=x;
		range[x]=ii(x,x);
	}
	
	ll curr=0;
	
	rep(zzz,0,(n+1)/2){
		int l,r;
		do{
			l=pq.top().se.fi;
			r=pq.top().se.se;
			pq.pop();
		}while (vis[l-1] || vis[r+1]);
		
		vis[l]=true;
		vis[r]=true;
		
		curr+=get(l,r);
		int node;
		if (l==r) node=parent(l);
		else{
			node=parent(l+1);
			p[l]=node;
			p[r]=node;
			range[node]=ii(l,r);
		}
		
		if (vis[r+2]){
			int temp=parent(r+2);
			
			p[temp]=node;
			range[node]=merge(range[node],range[temp]);
		}
		if (l>1 && vis[l-2]){
			int temp=parent(l-2);
			
			p[temp]=node;
			range[node]=merge(range[node],range[temp]);
		}
		
		l=range[node].fi,r=range[node].se;
		if (l!=1 && r!=n){
			pq.push(iii(get(l-1,r+1),ii(l-1,r+1))); 
		}
		
		cout<<curr<<endl;
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 512 KB Output is correct
2 Correct 2 ms 512 KB Output is correct
3 Correct 1 ms 512 KB Output is correct
4 Correct 1 ms 512 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 1 ms 512 KB Output is correct
7 Correct 2 ms 512 KB Output is correct
8 Correct 2 ms 512 KB Output is correct
9 Correct 2 ms 512 KB Output is correct
10 Correct 2 ms 512 KB Output is correct
11 Correct 1 ms 512 KB Output is correct
12 Correct 1 ms 512 KB Output is correct
13 Correct 1 ms 512 KB Output is correct
14 Correct 2 ms 512 KB Output is correct
15 Correct 2 ms 512 KB Output is correct
16 Correct 1 ms 512 KB Output is correct
17 Correct 1 ms 512 KB Output is correct
18 Correct 1 ms 512 KB Output is correct
19 Correct 1 ms 512 KB Output is correct
20 Correct 1 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 512 KB Output is correct
2 Correct 2 ms 512 KB Output is correct
3 Correct 1 ms 512 KB Output is correct
4 Correct 1 ms 512 KB Output is correct
5 Correct 1 ms 512 KB Output is correct
6 Correct 1 ms 512 KB Output is correct
7 Correct 2 ms 512 KB Output is correct
8 Correct 2 ms 512 KB Output is correct
9 Correct 2 ms 512 KB Output is correct
10 Correct 2 ms 512 KB Output is correct
11 Correct 1 ms 512 KB Output is correct
12 Correct 1 ms 512 KB Output is correct
13 Correct 1 ms 512 KB Output is correct
14 Correct 2 ms 512 KB Output is correct
15 Correct 2 ms 512 KB Output is correct
16 Correct 1 ms 512 KB Output is correct
17 Correct 1 ms 512 KB Output is correct
18 Correct 1 ms 512 KB Output is correct
19 Correct 1 ms 512 KB Output is correct
20 Correct 1 ms 512 KB Output is correct
21 Correct 152 ms 13952 KB Output is correct
22 Correct 136 ms 13912 KB Output is correct
23 Correct 151 ms 13916 KB Output is correct
24 Correct 104 ms 13912 KB Output is correct
25 Correct 104 ms 13912 KB Output is correct
26 Correct 108 ms 13912 KB Output is correct
27 Correct 117 ms 13912 KB Output is correct
28 Correct 117 ms 13912 KB Output is correct
29 Correct 116 ms 13912 KB Output is correct
30 Correct 116 ms 14040 KB Output is correct
31 Correct 118 ms 13912 KB Output is correct
32 Correct 122 ms 13912 KB Output is correct
33 Correct 130 ms 13916 KB Output is correct
34 Correct 123 ms 13912 KB Output is correct
35 Correct 124 ms 14040 KB Output is correct
36 Correct 143 ms 13912 KB Output is correct
37 Correct 133 ms 13912 KB Output is correct
38 Correct 145 ms 13912 KB Output is correct
39 Correct 102 ms 13916 KB Output is correct
40 Correct 102 ms 13912 KB Output is correct
41 Correct 102 ms 13912 KB Output is correct
42 Correct 132 ms 13912 KB Output is correct
43 Correct 113 ms 13912 KB Output is correct
44 Correct 122 ms 13912 KB Output is correct
45 Correct 114 ms 14056 KB Output is correct
46 Correct 126 ms 13912 KB Output is correct
47 Correct 118 ms 13912 KB Output is correct
48 Correct 124 ms 13912 KB Output is correct
49 Correct 139 ms 13908 KB Output is correct
50 Correct 128 ms 13912 KB Output is correct