CF1495F
题意简述
说:给定
题解
介绍一个题解区中没有的倍增做法。
尝试走的过程,首先注意到,钦定后,只需要算出来相邻
尝试观察最短路的形式,对于某
考虑
若将
直接处理,可以支持强制在线,复杂度
代码
#include<bits/stdc++.h>
namespace ifzw{
//~ #define int LL
#define ll long long
#define dd double
#define ull unsigned ll
#define LL __int128
#define siz(A) ((int)A.size())
using namespace std;
char gc(){static char buf[1<<16],*s,*t;if(s==t){t=(s=buf)+fread(buf,1,1<<16,stdin);if(s==t)return EOF;}return *s++;}
#define getchar gc
ll read()
{
char c;
ll w=1;
while((c=getchar())>'9'||c<'0')if(c=='-')w=-1;
ll ans=c-'0';
while((c=getchar())>='0'&&c<='9')ans=(ans<<1)+(ans<<3)+c-'0';
return ans*w;
}
void pc(char c,int op)
{
static char buf[1<<16],*s=buf,*t=buf+(1<<16);
(op||((*s++=c)&&(s==t)))&&(fwrite(buf,1,s-buf,stdout),s=buf);
}
void wt(int x)
{
if(x>9)wt(x/10);
pc('0'+x%10,0);
}
void wts(int x,char op)
{
if(x<0)pc('-',0),x=-x;
wt(x),pc(op,0);
}
char ST;
const int xx=2e5+5;
int n,q;
int p[xx],a[xx],b[xx];
int f[xx][21],tl[xx],stk[xx],top;
ll F[xx],G[xx];
ll ans;
ll get(int l,int r)
{
if(r==n+1)return F[l];
int x=l;
for(int i=20;i>=0;i--)if(f[x][i]<=r)x=f[x][i];
return F[l]-F[x]+G[r]-G[x];
}
char ED;
int main(){
cerr<<abs(&ST-&ED)/1024.0/1024<<"\n";
#ifdef AAA
system("ulimit -s 524288");
cerr<<" stack is on \n";
#endif
//~ freopen("a.in","r",stdin);
//~ freopen("a.out","w",stdout);
n=read(),q=read();
for(int i=1;i<=n;i++)p[i]=read();
for(int i=1;i<=n;i++)a[i]=read();
for(int i=1;i<=n;i++)b[i]=read();
p[n+1]=1e9;
stk[top=1]=n+1;
for(int i=n;i>=1;i--)
{
while(top&&p[stk[top]]<p[i])--top;
if(!top)f[i][0]=n+1,F[i]=0;
else
{
f[i][0]=stk[top],F[i]=b[i];
ll s=a[i],x=i+1;
while(x!=stk[top])s+=F[x],x=f[x][0];
F[i]=min(F[i],s);
}
stk[++top]=i;
}
f[n+1][0]=n+1;
for(int j=1;j<=20;j++)
for(int i=1;i<=n+1;i++)f[i][j]=f[f[i][j-1]][j-1];
p[0]=1e9;
stk[top=1]=0;
for(int i=1;i<=n;i++)
{
int ls=0;
while(top&&p[stk[top]]<p[i])ls=stk[top--];
if(!top)tl[i]=0,G[i]=0;
else
{
tl[i]=stk[top];
if(stk[top]==i-1)G[i]=a[i-1];
else G[i]=G[ls]+F[ls];
}
stk[++top]=i;
}
for(int i=1;i<=n;i++)G[i]+=G[tl[i]];
for(int i=n;i>=1;i--)F[i]+=F[f[i][0]];
set<int>s;
s.insert(1),s.insert(n+1);
ans=get(1,n+1);
//cerr<<get(2,3)<<" "<<F[2]<<" "<<F[3]<<" "<<G[2]<<" "<<G[3]<<"@ss\n";
while(q--)
{
int x=read();
if(x!=1)
{
if(s.count(x))
{
s.erase(x);
int R=*s.lower_bound(x),L=*--s.lower_bound(x);
ans-=get(L,x);
ans-=get(x,R);
ans+=get(L,R);
}
else
{
int R=*s.lower_bound(x),L=*--s.lower_bound(x);
//~ cerr<<x<<" "<<L<<" "<<R<<" "<<get(L,R)<<" "<<get(L,x)<<" "<<get(x,R)<<"@\n";
s.insert(x);
ans+=get(L,x);
ans+=get(x,R);
ans-=get(L,R);
}
}
cout<<ans<<"\n";
}
pc('1',1);
return 0;
}
}signed main(){return ifzw::main();}