[分块] BZOJ 4908 [BeiJing2017]开车

我们在车的地方加一 在加油站减一 这样答案就是一个类似绝对值的区间和的东西
这个东西不好搞 分块
对于整块加减 我们在块上再维护一个表示0的指针
对于小块 暴力基数排序重构
复杂度 O(nn)

#include
#include
#include
#include
#define pb push_back
using namespace std;
typedef long long ll;

inline char nc(){
  static char buf[100000],*p1=buf,*p2=buf;
  return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline void read(int &x){
  char c=nc(),b=1;
  for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
  for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}

const int N=150005;

int n;
int a[N],b[N];
int Q,x[N],y[N];

int sx[N],icnt;
inline int Bin(int x){
  return lower_bound(sx+1,sx+icnt+1,x)-sx;
}

const int BB=505;

int B,lp[BB],rp[BB];
int pos[N],cnt;

int v[N],c[N];

int idx[N];
int pnt[BB],lc[BB][BB]; ll svc[BB][BB],sv[BB][BB];
int p[BB],q[BB];
ll ans[BB];

#define Size(x) (rp[x]-lp[x]+1)

inline ll Sum(int x){
  return sv[x][p[x]]*q[x]-svc[x][p[x]]+(svc[x][pnt[x]]-svc[x][p[x]])-(sv[x][pnt[x]]-sv[x][p[x]])*q[x];
}

ll Ans=0;

int _c[512];
int tmp[N],t2[N];

inline void rsort(int l,int r){
  for (int i=l;i<=r;i++) c[i]+=50000;

  for (int i=0;i<512;i++) _c[i]=0;
  for (int i=l;i<=r;i++) _c[c[i]&511]++;
  for (int i=1;i<512;i++) _c[i]+=_c[i-1];
  for (int i=r;i>=l;i--) tmp[_c[c[i]&511]--]=i;

  for (int i=0;i<512;i++) _c[i]=0;
  for (int i=1;i<=r-l+1;i++) _c[c[tmp[i]]>>9]++;
  for (int i=1;i<512;i++) _c[i]+=_c[i-1];
  for (int i=r-l+1;i;i--) t2[_c[c[tmp[i]]>>9]--]=tmp[i];

  for (int i=l;i<=r;i++) idx[i]=t2[i-l+1],c[i]-=50000;
}

inline void Build(int x){
  rsort(lp[x],rp[x]);
  Ans-=ans[x]; pnt[x]=0;
  for (int j=lp[x];j<=rp[x];j++)
    if (j==lp[x] || c[idx[j]]!=c[idx[j-1]]){
      pnt[x]++,lc[x][pnt[x]]=c[idx[j]];
      sv[x][pnt[x]]=sv[x][pnt[x]-1]+v[idx[j]];
      svc[x][pnt[x]]=svc[x][pnt[x]-1]+(ll)v[idx[j]]*c[idx[j]];
    }
    else{
      sv[x][pnt[x]]+=v[idx[j]];
      svc[x][pnt[x]]+=(ll)v[idx[j]]*c[idx[j]];
    }
  q[x]=0;
  p[x]=upper_bound(lc[x]+1,lc[x]+pnt[x]+1,0)-lc[x]-1;
  Ans+=(ans[x]=Sum(x));
}

inline void Reb(int x,int l,int r,int t){
  if (l>r) return;
  for (int i=lp[x];i<=rp[x];i++) c[i]-=q[x];
  for (int i=l;i<=r;i++) c[i]+=t;
  Build(x);
}

inline void Add(int l,int r,int t){
  int lb=pos[l],rb=pos[r];
  if (lb==rb){
    Reb(lb,l,r,t);
    return;
  }
  Reb(lb,l,rp[lb],t);
  Reb(rb,lp[rb],r,t);
  for (int i=lb+1;iq[i]-=t;
    while (p[i]+1<=pnt[i] && lc[i][p[i]+1]<=q[i]) p[i]++;
    while (p[i]>0 && lc[i][p[i]]>q[i]) p[i]--; 
    Ans+=(ans[i]=Sum(i));
  }
}

int main(){
  freopen("t.in","r",stdin);
  freopen("t.out","w",stdout);
  read(n);
  for (int i=1;i<=n;i++) read(a[i]),sx[++icnt]=a[i];
  for (int i=1;i<=n;i++) read(b[i]),sx[++icnt]=b[i];
  read(Q); for (int i=1;i<=Q;i++) read(x[i]),read(y[i]),sx[++icnt]=y[i];
  sort(sx+1,sx+icnt+1); icnt=unique(sx+1,sx+icnt+1)-sx-1;
  for (int i=1;i1]-sx[i];
  B=sqrt(icnt);
  for (int i=1;i<=icnt;i++) pos[i]=(i-1)/B+1; cnt=pos[icnt];
  for (int i=1;i<=cnt;i++) lp[i]=(i-1)*B+1,rp[i]=i*B; rp[cnt]=icnt;
  for (int i=1;i<=n;i++) a[i]=Bin(a[i]),b[i]=Bin(b[i]),c[a[i]]++,c[b[i]]--;
  for (int i=1;i<=icnt;i++) c[i]+=c[i-1];
  for (int i=1;i<=cnt;i++)
    Build(i);
  printf("%lld\n",Ans);
  for (int i=1;i<=Q;i++){
    y[i]=Bin(y[i]);
    if (y[i]x[i]])
      Add(y[i],a[x[i]]-1,1),a[x[i]]=y[i];
    else if (a[x[i]]<y[i])
      Add(a[x[i]],y[i]-1,-1),a[x[i]]=y[i];
    printf("%lld\n",Ans);
  }
  return 0;
}

你可能感兴趣的:(分块)