//雪花飄飄北風嘯嘯
//天地一片蒼茫
#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[parent(l)]=node;
p[parent(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 |
620 KB |
Output is correct |
2 |
Correct |
2 ms |
620 KB |
Output is correct |
3 |
Correct |
2 ms |
620 KB |
Output is correct |
4 |
Correct |
2 ms |
620 KB |
Output is correct |
5 |
Correct |
2 ms |
620 KB |
Output is correct |
6 |
Correct |
2 ms |
620 KB |
Output is correct |
7 |
Correct |
2 ms |
620 KB |
Output is correct |
8 |
Correct |
2 ms |
620 KB |
Output is correct |
9 |
Correct |
2 ms |
620 KB |
Output is correct |
10 |
Correct |
2 ms |
620 KB |
Output is correct |
11 |
Correct |
2 ms |
620 KB |
Output is correct |
12 |
Correct |
2 ms |
620 KB |
Output is correct |
13 |
Correct |
2 ms |
620 KB |
Output is correct |
14 |
Correct |
2 ms |
620 KB |
Output is correct |
15 |
Correct |
2 ms |
640 KB |
Output is correct |
16 |
Correct |
2 ms |
620 KB |
Output is correct |
17 |
Correct |
2 ms |
620 KB |
Output is correct |
18 |
Correct |
2 ms |
620 KB |
Output is correct |
19 |
Correct |
2 ms |
620 KB |
Output is correct |
20 |
Correct |
2 ms |
620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
620 KB |
Output is correct |
2 |
Correct |
2 ms |
620 KB |
Output is correct |
3 |
Correct |
2 ms |
620 KB |
Output is correct |
4 |
Correct |
2 ms |
620 KB |
Output is correct |
5 |
Correct |
2 ms |
620 KB |
Output is correct |
6 |
Correct |
2 ms |
620 KB |
Output is correct |
7 |
Correct |
2 ms |
620 KB |
Output is correct |
8 |
Correct |
2 ms |
620 KB |
Output is correct |
9 |
Correct |
2 ms |
620 KB |
Output is correct |
10 |
Correct |
2 ms |
620 KB |
Output is correct |
11 |
Correct |
2 ms |
620 KB |
Output is correct |
12 |
Correct |
2 ms |
620 KB |
Output is correct |
13 |
Correct |
2 ms |
620 KB |
Output is correct |
14 |
Correct |
2 ms |
620 KB |
Output is correct |
15 |
Correct |
2 ms |
640 KB |
Output is correct |
16 |
Correct |
2 ms |
620 KB |
Output is correct |
17 |
Correct |
2 ms |
620 KB |
Output is correct |
18 |
Correct |
2 ms |
620 KB |
Output is correct |
19 |
Correct |
2 ms |
620 KB |
Output is correct |
20 |
Correct |
2 ms |
620 KB |
Output is correct |
21 |
Correct |
164 ms |
15824 KB |
Output is correct |
22 |
Correct |
149 ms |
15952 KB |
Output is correct |
23 |
Correct |
156 ms |
15952 KB |
Output is correct |
24 |
Correct |
109 ms |
15696 KB |
Output is correct |
25 |
Correct |
111 ms |
15696 KB |
Output is correct |
26 |
Correct |
115 ms |
15696 KB |
Output is correct |
27 |
Correct |
119 ms |
15824 KB |
Output is correct |
28 |
Correct |
118 ms |
15824 KB |
Output is correct |
29 |
Correct |
118 ms |
15824 KB |
Output is correct |
30 |
Correct |
129 ms |
15824 KB |
Output is correct |
31 |
Correct |
124 ms |
15824 KB |
Output is correct |
32 |
Correct |
125 ms |
15952 KB |
Output is correct |
33 |
Correct |
140 ms |
15824 KB |
Output is correct |
34 |
Correct |
153 ms |
15648 KB |
Output is correct |
35 |
Correct |
133 ms |
15696 KB |
Output is correct |
36 |
Correct |
155 ms |
15824 KB |
Output is correct |
37 |
Correct |
149 ms |
15824 KB |
Output is correct |
38 |
Correct |
164 ms |
15952 KB |
Output is correct |
39 |
Correct |
112 ms |
15836 KB |
Output is correct |
40 |
Correct |
124 ms |
15696 KB |
Output is correct |
41 |
Correct |
105 ms |
15696 KB |
Output is correct |
42 |
Correct |
120 ms |
16080 KB |
Output is correct |
43 |
Correct |
121 ms |
15876 KB |
Output is correct |
44 |
Correct |
123 ms |
15824 KB |
Output is correct |
45 |
Correct |
123 ms |
15964 KB |
Output is correct |
46 |
Correct |
134 ms |
15824 KB |
Output is correct |
47 |
Correct |
124 ms |
15824 KB |
Output is correct |
48 |
Correct |
131 ms |
15696 KB |
Output is correct |
49 |
Correct |
130 ms |
15696 KB |
Output is correct |
50 |
Correct |
134 ms |
15696 KB |
Output is correct |