# 洛谷P5069 [Ynoi2015]纵使日薄西山（树状数组，set）

#include<bits/stdc++.h>
#define LL long long
#define R register int
using namespace std;
const int SZ=1<<19,N=1e5+9;
char buf[SZ],*ie=buf+SZ,*ip=ie-1;
inline int in(){
G;while(*ip<'-')G;
R x=*ip&15;G;
while(*ip>'-'){x*=10;x+=*ip&15;G;}
return x;
}
int a[N],b[N];
bool vis[N];
inline bool Cmp(R&x,R y){
return a[x]>a[y]||(a[x]==a[y]&&x<y);
}
int main(){
R n=in();
for(R i=1;i<=n;++i)a[i]=in();
for(R q=in();q--;){
a[in()]=in();
for(R i=1;i<=n;++i)b[i]=i;
sort(b+1,b+n+1,Cmp);
memset(vis+1,0,n);
LL ans=0;
for(R i=1;i<=n;++i){
if(vis[b[i]])continue;
vis[b[i]-1]=vis[b[i]]=vis[b[i]+1]=1;
ans+=a[b[i]];
}
printf("%lld\n",ans);
}
return 0;
}


#include<bits/stdc++.h>
#define LL long long
#define I inline
#define R register int
using namespace std;
typedef set<int>::iterator IT;
const int SZ=1<<19,N=1e5+9;
char buf[SZ],*ie=buf+SZ,*ip=ie-1;
inline int in(){
G;while(*ip<'-')G;
R x=*ip&15;G;
while(*ip>'-'){x*=10;x+=*ip&15;G;}
return x;
}
int n,a[N];LL ans;set<int>s;
struct BIT{
LL a[N];
I void Upd(R i,R v){
for(;i<N;i+=i&-i)a[i]+=v;
}
I void Qry(R l,R r,LL v){
for(R i=r;i;i-=i&-i)v+=a[i];
for(R i=l;i;i-=i&-i)v-=a[i];
}
}b[2];
#define ODD(O) ((*l^*O(j=l))&1)
I void Calc(IT l,IT r,R op){
IT i,j;LL sum=*l&&a[*l+1]>a[*l]&&!ODD(--)&&!ODD(++)?a[*l]:0;
for(i=l++;i!=r;i=l++)
if(a[*l]>a[*i])b[*l&1].Qry(*i,*l,sum);
else b[*i&1].Qry(*i,*l-ODD(++),sum);
ans+=sum*op;
}
I void Chk(R x){
if((a[x]>a[x-1])!=(a[x+1]>a[x]))s.insert(x);
else s.erase(x);
}
I void Upd(R x){
R y=in();
IT i=s.lower_bound(x),l=i,r=i;
--l;if(l!=s.begin())--l;
++r;if(r==s.end()||(*i==x&&++r==s.end()))--r;
Calc(l,r,-1);
b[x&1].Upd(x,y-a[x]);a[x]=y;
Chk(x);if(x>1)Chk(x-1);if(x<n)Chk(x+1);
Calc(l,r,1);
}
int main(){
n=in();s.insert(0);s.insert(n+1);
for(R i=1;i<=n;++i)Upd(i);
for(R q=in();q;--q)Upd(in()),printf("%lld\n",ans);
return 0;
}

posted @ 2019-03-08 13:39  Flash_Hu  阅读(454)  评论(2编辑  收藏  举报