题解:P11930 [PA 2025] 吃树叶 / Liście

· · 题解

绝顶搞笑题。

25s,n\leq 10^6,q\leq 2\times 10^6,显然可以往根号做法想。

题面写了一大堆没用的,但是总结起来就是在 1 到 a_i 的每棵树上摘 v_i 片叶子,修改操作等价于前缀加操作,而查询相当于查询区间内 \geq d 的 v_i 和。

考虑块长为 B,维护块内排序后的后缀和,修改暴力修改,查询直接预处理整块内 a_i 排序后第一个 \geq d 的位置,这个是预处理的时空复杂度都是 O\left(\dfrac{nV}{B}\right) 的。而修改和查询显然都是单次 O\left(B+\dfrac{n}{B}\right) 的。取 B=O(\sqrt n) 即可。建议将块长稍微开大一点,以免卡空间。

#include<iostream>
#include<algorithm>
using ll=long long;
const int sz=1e6+10;
const int sqsz=1010;
namespace IO{
  using ll=long long;
  const int MAXSIZE=1<<21;
  char buf[MAXSIZE],*pb,*pe;
#define gc() (pb==pe&&(pe=(pb=buf)+fread(buf,1,MAXSIZE,stdin),pb==pe)?EOF:*pb++)
  inline ll read(){
    ll x=0,f=1;
    char c=gc();
    while(!isdigit(c)){
      if(c=='-')f=-1;
      c=gc();
    }
    while(isdigit(c))x=(x<<3)+(x<<1)+(c^48),c=gc();
    return x*f;
  }
  char pbuf[MAXSIZE],*pp=pbuf;
  int sta[35];
  inline void push(const char &c) {
    if(pp-pbuf==MAXSIZE)fwrite(pbuf,1,MAXSIZE,stdout),pp=pbuf;
    *pp++=c;
  }
  inline void write(ll x){
    if(x<0)push('-'),x=-x;
    int top=0;
    do sta[top++]=x%10,x/=10;while(x!=0);
    while(top!=0)push(sta[--top]+'0');
  }
  inline std::string readline(){
    std::string s;
    char c;
    while((c=gc())!='\n'&&c!=EOF)s+=c;
    return s;
  }
  inline void write(std::string s){for(char &c:s)push(c);}
  inline void flush(){if(pp-pbuf!=0)fwrite(pbuf,1,pp-pbuf,stdout),pp=pbuf;}
}
using IO::read,IO::write,IO::flush;
int belong[sz],bl[sz],br[sz],l[sz],a[sz];
ll tag[sz],suf[sz],v[sz];
short pos[sqsz][sz];
inline void add(int x,ll val){
  for(int i=1;i<belong[x]+(x==br[belong[x]]);i++)tag[i]+=val;
  if(x==br[belong[x]])return;
  for(int i=bl[belong[x]];i<=x;i++)v[i]+=val;
  suf[br[belong[x]]]=v[a[br[belong[x]]]];
  for(int i=br[belong[x]]-1;i>=bl[belong[x]];i--)suf[i]=suf[i+1]+v[a[i]];
}
inline ll query(int x,int d){
  ll res=0;
  for(int i=1;i<belong[x]+(x==br[belong[x]]);i++)
    if(pos[i][d]!=0)res+=suf[pos[i][d]+bl[i]-1]+tag[i]*(br[i]-pos[i][d]-bl[i]+2);
  if(x!=br[belong[x]])for(int i=bl[belong[x]];i<=x;i++)if(l[i]>=d)res+=v[i]+tag[belong[x]];
  return res;
}
int main(){
  int n=read(),q=read()+read();
  for(int i=1;i<=n;i++)l[i]=read();
  int lim=2000,num=n/lim;
  for(int i=1;i<=num;i++)bl[i]=br[i-1]+1,br[i]=i*lim;
  if(br[num]!=n)num++,bl[num]=br[num-1]+1,br[num]=n;
  for(int i=1;i<=num;i++){
    for(int j=bl[i];j<=br[i];j++)belong[j]=i,a[j]=j;
    std::sort(a+bl[i],a+br[i]+1,[&](int x,int y){return l[x]<l[y];});
    int lst=1;
    for(int j=bl[i];j<=br[i];j++)while(lst<=l[a[j]])pos[i][lst++]=j-bl[i]+1;
  }
  while(q--){
    int op=read(),x=read(),y=read();
    if(op==1)add(x,y);
    else write(query(x,y)),write("\n");
  }
  flush();
  return 0;
}