#include<bits/stdc++.h>
#define fi first
#define se second
#define ll long long
using namespace std ;
const ll N = 2e5 ;
ll n, ans, num, a[N + 1], pref[N + 3] ;
set<pair<ll, ll>> s1, s2 ;
set<pair<ll, pair<ll, ll>>> s ;
ll get_sum(ll l, ll r)
{
if(!l || r > n)return -1e18 ;
ll num = 1 ;
if(l % 2 == 0)num = -1 ;
return (pref[r] - pref[l - 1]) * num ;
}
signed main()
{
ios_base::sync_with_stdio( 0 ) ;
cin.tie( 0 ) ;
cout.tie( 0 ) ;
cin >> n ;
for(ll i = 1 ; i <= n ; i++)
{
cin >> a[i] ;
s1.insert({i, i}) ;
s2.insert({-i, -i}) ;
pref[i] = pref[i - 1] ;
if(i % 2)pref[i] += a[i] ;
else pref[i] -= a[i] ;
s.insert({a[i], {i, i}}) ;
}
pref[n + 1] = pref[n] ;
for(ll i = 1 ; i <= (n + 1) / 2 ; i++)
{
ll num = (*s.rbegin()).fi, l = (*s.rbegin()).se.fi, r = (*s.rbegin()).se.se ;
pair<ll, ll> pz1 = {l, r + 1}, pz2 = {-r, -l - 1} ;
s.erase(*s.rbegin()) ;
s1.erase({l, r}) ;
s2.erase({-r, -l}) ;
if(s1.lower_bound({l, r}) != s1.end())
{
pz1 = *s1.lower_bound({l, r}) ;
s1.erase(pz1) ;
s2.erase({-pz1.se, -pz1.fi}) ;
s.erase({get_sum(pz1.fi, pz1.se), {pz1.fi, pz1.se}}) ;
}
if(s2.lower_bound({-r, -l}) != s2.end())
{
pz2 = *s2.lower_bound({-r, -l}) ;
s1.erase({-pz2.se, -pz2.fi}) ;
s2.erase(pz2) ;
s.erase({get_sum(-pz2.se, -pz2.fi), {-pz2.se, -pz2.fi}}) ;
}
l = -pz2.se ;
r = pz1.se ;
s1.insert({l, r}) ;
s2.insert({-r, -l}) ;
s.insert({get_sum(l, r), {l, r}}) ;
ans += num ;
cout << ans << '\n' ;
}
return 0 ;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
744 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
744 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |