#include <bits/stdc++.h>
#define gibon ios::sync_with_stdio(false); cin.tie(0);
#define bp __builtin_popcount
#define fir first
#define sec second
#define pii pair<int, int>
#define pll pair<ll, ll>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
typedef long long ll;
using namespace std;
int dx[4]={0, 1, 0, -1}, dy[4]={1, 0, -1 , 0};
const int mxN=202000;
const int mxM=104;
const int mxK=1000000;
const ll MOD=1000000007;
const ll INF=100000000000001;
typedef struct rng{
int l, r;
ll val;
}rng;
typedef struct cmp1{
bool operator()(rng a, rng b)
{
return a.val<b.val;
}
}cmp1;
int N;
ll A[mxN];
priority_queue <rng, vector<rng>, cmp1> pq;
int par[mxN];
rng uf[mxN];
ll ans;
int L, R;
int findpar(int a)
{
return par[a]==a ? a : (par[a]=findpar(par[a]));
}
int main()
{
gibon
cin >> N;
for(int i=1;i<=N;i++) cin >> A[i];
L=0, R=N+1;
for(int i=1;i<=N;i++)
{
par[i]=i;
uf[i].l=uf[i].r=i, uf[i].val=A[i];
pq.push(uf[i]);
}
for(int i=1;i<=(N+1)/2;i++)
{
while(true)
{
rng now=pq.top();
pq.pop();
int ncur=findpar(now.l);
if(uf[ncur].l!=now.l || uf[ncur].r!=now.r || uf[ncur].l<=L || uf[ncur].r>=R) continue;
ans+=now.val;
uf[ncur].val*=-1;
cout << ans << '\n';
if(i==(N+1)/2) break;
if(uf[ncur].l==L+1)
{
L=uf[findpar(uf[ncur].r+1)].r;
break;
}
if(uf[ncur].r==R-1)
{
R=uf[findpar(uf[ncur].l-1)].l;
break;
}
int lc=findpar(uf[ncur].l-1);
par[lc]=ncur;
uf[ncur].val+=uf[lc].val;
uf[ncur].l=uf[lc].l;
int rc=findpar(uf[ncur].r+1);
par[rc]=ncur;
uf[ncur].val+=uf[rc].val;
uf[ncur].r=uf[rc].r;
pq.push(uf[ncur]);
break;
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
460 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
460 KB |
Output is correct |
4 |
Correct |
2 ms |
460 KB |
Output is correct |
5 |
Correct |
2 ms |
460 KB |
Output is correct |
6 |
Correct |
2 ms |
460 KB |
Output is correct |
7 |
Correct |
2 ms |
460 KB |
Output is correct |
8 |
Correct |
2 ms |
472 KB |
Output is correct |
9 |
Correct |
2 ms |
460 KB |
Output is correct |
10 |
Correct |
2 ms |
460 KB |
Output is correct |
11 |
Correct |
2 ms |
460 KB |
Output is correct |
12 |
Correct |
2 ms |
460 KB |
Output is correct |
13 |
Correct |
2 ms |
460 KB |
Output is correct |
14 |
Correct |
2 ms |
460 KB |
Output is correct |
15 |
Correct |
2 ms |
460 KB |
Output is correct |
16 |
Correct |
2 ms |
460 KB |
Output is correct |
17 |
Correct |
2 ms |
460 KB |
Output is correct |
18 |
Correct |
2 ms |
460 KB |
Output is correct |
19 |
Correct |
2 ms |
472 KB |
Output is correct |
20 |
Correct |
2 ms |
476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
460 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
460 KB |
Output is correct |
4 |
Correct |
2 ms |
460 KB |
Output is correct |
5 |
Correct |
2 ms |
460 KB |
Output is correct |
6 |
Correct |
2 ms |
460 KB |
Output is correct |
7 |
Correct |
2 ms |
460 KB |
Output is correct |
8 |
Correct |
2 ms |
472 KB |
Output is correct |
9 |
Correct |
2 ms |
460 KB |
Output is correct |
10 |
Correct |
2 ms |
460 KB |
Output is correct |
11 |
Correct |
2 ms |
460 KB |
Output is correct |
12 |
Correct |
2 ms |
460 KB |
Output is correct |
13 |
Correct |
2 ms |
460 KB |
Output is correct |
14 |
Correct |
2 ms |
460 KB |
Output is correct |
15 |
Correct |
2 ms |
460 KB |
Output is correct |
16 |
Correct |
2 ms |
460 KB |
Output is correct |
17 |
Correct |
2 ms |
460 KB |
Output is correct |
18 |
Correct |
2 ms |
460 KB |
Output is correct |
19 |
Correct |
2 ms |
472 KB |
Output is correct |
20 |
Correct |
2 ms |
476 KB |
Output is correct |
21 |
Correct |
144 ms |
12404 KB |
Output is correct |
22 |
Correct |
139 ms |
12480 KB |
Output is correct |
23 |
Correct |
133 ms |
12484 KB |
Output is correct |
24 |
Correct |
97 ms |
12236 KB |
Output is correct |
25 |
Correct |
95 ms |
12304 KB |
Output is correct |
26 |
Correct |
94 ms |
12320 KB |
Output is correct |
27 |
Correct |
94 ms |
12480 KB |
Output is correct |
28 |
Correct |
102 ms |
12460 KB |
Output is correct |
29 |
Correct |
96 ms |
12516 KB |
Output is correct |
30 |
Correct |
97 ms |
12592 KB |
Output is correct |
31 |
Correct |
98 ms |
12496 KB |
Output is correct |
32 |
Correct |
123 ms |
12460 KB |
Output is correct |
33 |
Correct |
107 ms |
12204 KB |
Output is correct |
34 |
Correct |
114 ms |
12324 KB |
Output is correct |
35 |
Correct |
123 ms |
12308 KB |
Output is correct |
36 |
Correct |
136 ms |
12464 KB |
Output is correct |
37 |
Correct |
130 ms |
12464 KB |
Output is correct |
38 |
Correct |
165 ms |
12424 KB |
Output is correct |
39 |
Correct |
103 ms |
12336 KB |
Output is correct |
40 |
Correct |
93 ms |
12264 KB |
Output is correct |
41 |
Correct |
93 ms |
12304 KB |
Output is correct |
42 |
Correct |
94 ms |
12456 KB |
Output is correct |
43 |
Correct |
94 ms |
12516 KB |
Output is correct |
44 |
Correct |
100 ms |
12452 KB |
Output is correct |
45 |
Correct |
98 ms |
12404 KB |
Output is correct |
46 |
Correct |
98 ms |
12460 KB |
Output is correct |
47 |
Correct |
100 ms |
12608 KB |
Output is correct |
48 |
Correct |
122 ms |
12216 KB |
Output is correct |
49 |
Correct |
106 ms |
12208 KB |
Output is correct |
50 |
Correct |
123 ms |
12304 KB |
Output is correct |