题解:P12448 [COTS 2025] 观草 / Trava
题目:观草。
观观草草,草草观观,草观草观草。
观草观草,草观草观,观草观草观。
请注意,这并不是一首有名的诗歌。
Part 0
当求和本身不好计算的时候,我们应该考虑
于是研究
从
:::success[PICTURE] :::
省流
CODE
:::success[code]
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define For(i,L,R) for(int i=L;i<=R;i++)
#define lc u<<1
#define rc u<<1|1
const int N=5e5+10;
int n,m,a[N];
int stk[N],top;
int L[N],R[N];
int t[N<<2];
void pushup(int u){
t[u]=max(t[lc],t[rc]);
}
void build(int u,int L,int R){
if(L==R){
t[u]=a[L];
return;
}
int m=(L+R)>>1;
build(lc,L,m);
build(rc,m+1,R);
pushup(u);
}
void upd(int u,int L,int R,int p,int v){
if(L==R){t[u]+=v;return;}
int m=(L+R)>>1;
if(p<=m)upd(lc,L,m,p,v);
else upd(rc,m+1,R,p,v);
pushup(u);
}
int fl(int u,int L,int R,int l,int r,int v){
if(t[u]<v||l>r)return -1;
if(L==R)return L;
int m=(L+R)>>1,res=-1;
if(l<=m)res=fl(lc,L,m,l,r,v);
if(res==-1&&r>m)res=fl(rc,m+1,R,l,r,v);
return res;
}
int fr(int u,int L,int R,int l,int r,int v){
if(t[u]<v||l>r)return -1;
if(L==R)return L;
int m=(L+R)>>1,res=-1;
if(r>m)res=fr(rc,m+1,R,l,r,v);
if(res==-1&&l<=m)res=fr(lc,L,m,l,r,v);
return res;
}
ll k[N],d[N];
void add(int x,ll vk,ll vd){
for(;x<=n;x+=x&-x){
k[x]+=vk;
d[x]+=vd;
}
}
void work(int L,int R,ll vk,ll vd){
if(L>R)return;
add(L,vk,vd);
add(R+1,-vk,-vd);
}
pair<ll,ll>qry(int x){
ll sk=0,sd=0;
for(;x;x-=x&-x){sk+=k[x];sd+=d[x];}
return make_pair(sk,sd);
}
void upr(int u,int lp,int rp,ll x){
int d1=u-lp,d2=rp-u,t=rp-lp-1;
if(d1>d2)swap(d1,d2);
work(1,d1,x,0);
if(d1<d2)work(d1+1,d2,0,d1*x);
if(d2<t)work(d2+1,t,-x,(t+1)*x);
}
ll solve(int u){
pair<ll,ll>p=qry(u);
return p.first*u+p.second;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>n>>m;
For(i,1,n){
cin>>a[i];
while(top&&a[stk[top]]<=a[i]){
R[stk[top--]]=i;
}
L[i]=stk[top];
stk[++top]=i;
}
while(top){
R[stk[top--]]=n+1;
}
For(i,1,n){
upr(i,L[i],R[i],a[i]);
}
build(1,1,n);
while(m--){
char op;int k;
cin>>op>>k;
if(op=='?')cout<<solve(k)<<"\n";
else{
a[k]++;
upd(1,1,n,k,1);
int l=fr(1,1,n,1,k-1,a[k]);
if(l==-1)l=0;
int r=fl(1,1,n,k+1,n,a[k]);
if(r==-1)r=n+1;
upr(k,l,r,1);
}
}
return 0;
}
:::