首先大体同 Leasier。
但是,注意到,砖块只会升高!
于是只需要写一个九条可怜线段树就可以避开中间的二分了。
赛场上 lingying 是这么写的,可惜并没有调出来。我赛后试了试,比前后两个 std::set 好写多了。
#include<stdio.h>
const int N=100001;
int a[N];
struct JRYSegT
{
struct node
{
int m,mc,sm;
long long s;
const node operator+(const node&a)const
{
if(m<a.m)
if(sm<a.m)
return node{m,mc,sm,s+a.s};
else
return node{m,mc,a.m,s+a.s};
else if(a.m<m)
if(a.sm<m)
return node{a.m,a.mc,a.sm,s+a.s};
else
return node{a.m,a.mc,m,s+a.s};
else if(sm<a.sm)
return node{a.m,mc+a.mc,sm,s+a.s};
else
return node{m,mc+a.mc,a.sm,s+a.s};
}
}e[N<<2|1];
void pushdown(int no)
{
if(e[no].m>e[no<<1].m)
e[no<<1].s+=1ll*(e[no].m-e[no<<1].m)*e[no<<1].mc,e[no<<1].m=e[no].m;
if(e[no].m>e[no<<1|1].m)
e[no<<1|1].s+=1ll*(e[no].m-e[no<<1|1].m)*e[no<<1|1].mc,e[no<<1|1].m=e[no].m;
return;
}
void build(int a[],int l=0,int r=N,int no=1)
{
if(l+1<r)
build(a,l,l+r>>1,no<<1),build(a,l+r>>1,r,no<<1|1),e[no]=e[no<<1]+e[no<<1|1];
else
e[no]=node{a[l],1,2000000000,a[l]};
return;
}
void max(int x,int y,int z,int l=0,int r=N,int no=1)
{
if(x>=r||y<=l||e[no].m>=z)
return;
if(x<=l&&y>=r&&e[no].sm>z)
{
e[no].s+=1ll*(z-e[no].m)*e[no].mc,e[no].m=z;
return;
}
pushdown(no),max(x,y,z,l,l+r>>1,no<<1),max(x,y,z,l+r>>1,r,no<<1|1),e[no]=e[no<<1]+e[no<<1|1];
return;
}
}st;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,q,ma=0;
scanf("%d%d",&n,&q);
long long sa=0;
for(int i=0;i<n;i++)
{
scanf("%d",a+i),sa+=a[i];
if(a[ma]<a[i])
ma=i;
}
for(int i=n;i<N;i++)
a[i]=0;
st.build(a);
for(int i=0;i<ma;i++)
st.max(i,ma,a[i]);
for(int i=ma+1;i<n;i++)
st.max(ma+1,i+1,a[i]);
while(q--)
{
char s[3];
scanf("%s",s);
if(s[0]=='P')
printf("%lld\n",st.e[1].s-sa);
else
{
int x,v;
scanf("%d%d",&x,&v),a[--x]+=v,sa+=v,st.max(x,x+1,a[x]);
if(a[ma]<a[x])
{
if(ma<x)
st.max(ma,x,a[ma]);
else
st.max(x,ma,a[ma]);
ma=x;
}
else
if(ma<x)
st.max(ma,x,a[x]);
else
st.max(x,ma,a[x]);
}
}
}
return 0;
}