AT_dwango2015_finals_4 コインの取り合い Solution
MatrixGroup · · 题解
Analysis
Consider the case where
To solve the problem, one must find
One may use std::set to maintain the indices where the suffix minimum of
Code
#include <bits/stdc++.h>
#define mp make_pair
#define fi first
#define se second
typedef long long ll;
using namespace std;
set<pair<int,ll> > S;
int n,q,p;ll s;
ll a[200005];
ll b[200005];
ll ans;
inline int query(int L,int R)
{
return (R%2==0)-(L%2==0);
}
int main()
{
ios_base::sync_with_stdio(false);cin.tie(0);
cin>>n;
for(int i=1;i<=n;++i)
{
cin>>a[i];
if(i&1) ans+=a[i];
}
for(int i=n;i>=2;--i)
{
if(i==n) b[i]=a[i];
else b[i]=min(b[i+1],a[i]);
if(i==n||b[i]!=b[i+1])
{
S.insert(mp(i,b[i]));
}
if(i&1) ans-=b[i];
else ans+=b[i];
}
S.insert(mp(1,0));
cout<<ans<<'\n';cin>>q;
while(q--)
{
cin>>p>>s;
a[p]-=s;
if(p&1) ans-=s;
if(p>1)
{
set<pair<int,ll> >::iterator itr=S.lower_bound(mp(p,0));
if(a[p]<(itr->se))
{
ans-=query(prev(itr)->fi,p)*((itr->se)-a[p]);
if((itr->fi)==p)
{
S.erase(itr);
}
S.insert(mp(p,a[p]));
int cur=p;
while(1)
{
itr=S.lower_bound(mp(cur,0));
--itr;
if((itr->se)<=a[p]) break;
ans-=query(prev(itr)->fi,itr->fi)*((itr->se)-a[p]);
S.erase(itr);
cur=itr->fi;
}
}
}
cout<<ans<<'\n';
}
return 0;
}