LOJ 3094 "BJOI2019" number deleted - tree line angle standard deviation

Topic: https://loj.ac/problem/3094

A weaker version is AGC017C.

Maintenance problems that can be used in the sequence tree line.

Correspondence between about:

  Range of the real value is [1-m, n + m]; consider setting offset fx, so that a [i] + fx is the true value. If the overall +1 on fx + 1.

  Because each value of the number to be recorded, so that a [i] preferably are non-negative. Then let the initial value of fx is -m, a [i] is the minimum value "true value of the minimum - fx", is a 1-m + m.

  Have a [] in the range [1, n + 2 * m]. Considering the number cnt, coverage is [1-n, n + 2 * m]. Therefore, so fx2 = n, a [] corresponds to the segment tree plus fx2 can subscript.

Note that if the value is outside the coverage brought [1, n] should not be considered. Because the cover is on the left side value, it is only necessary pipes> affect the value of n [. 1, n] of. Since the n-1 position of each move up, it is possible to maintain.

If> value of n is such that as a single point modification [. 1, n] changes in the value of the position, should be ignored, only modify "value is equal to the number of elements of the value" can.

Maintenance of the original "range in the number of zeros." This can not cope with reduced range. If you need to take into account "the number of sections 1", then the time interval in certain no 0, so (see explanations) we expect the number to a minimum of maintenance intervals.

#include<cstdio>
#include<cstring>
#include<algorithm>
#define ls Ls[cr]
#define rs Rs[cr]
using namespace std;
int rdn()
{
  int ret=0;bool fx=1;char ch=getchar();
  while(ch>'9'||ch<'0'){if(ch=='-')fx=0;ch=getchar();}
  while(ch>='0'&&ch<='9')ret=ret*10+ch-'0',ch=getchar();
  return fx?ret:-ret;
}
int Mn(int a,int b){return a<b?a:b;}
const int N=150005,N2=N*3,M=N*8;
int n,m,a[N],tp[N2],fx,fx2,lm;
int tot,Ls[M],Rs[M],tg[M];
struct Node{
  int mn,ct;
  Node(int m=0,int c=0):mn(m),ct(c) {}
  Node operator+ (const Node &b)const
  {
    int tmn=Mn(mn,b.mn),tct=0;
    if(mn==tmn)tct+=ct; if(b.mn==tmn)tct+=b.ct;
    return Node(tmn,tct);
  }
}vl[M];
void build(int l,int r,int cr)
{
  vl[cr].mn=0; vl[cr].ct=r-l+1;
  if(l==r)return; int mid=l+r>>1;
  ls=++tot; build(l,mid,ls);
  rs=++tot; build(mid+1,r,rs);
}
void pshd(int cr)
{
  if(!tg[cr])return; int w=tg[cr]; tg[cr]=0;
  tg[ls]+=w; tg[rs]+=w; vl[ls].mn+=w; vl[rs].mn+=w;
}
void mdfy(int l,int r,int cr,int L,int R,int k)
{
  if(l>=L&&r<=R){ tg[cr]+=k; vl[cr].mn+=k; return;}
  int mid=l+r>>1; pshd(cr);
  if(L<=mid)mdfy(l,mid,ls,L,R,k);
  if(mid<R)mdfy(mid+1,r,rs,L,R,k);
  vl[cr]=vl[ls]+vl[rs];
}
Node qry(int l,int r,int cr,int L,int R)
{
  if(l>=L&&r<=R)return vl[cr];
  int mid=l+r>>1; pshd(cr);
  if(R<=mid)return qry(l,mid,ls,L,R);
  if(mid<L)return qry(mid+1,r,rs,L,R);
  return qry(l,mid,ls,L,R)+qry(mid+1,r,rs,L,R);
}
int main()
{
  n=rdn();m=rdn(); fx=-m; fx2=n; lm=n+m-fx+fx2;
  for(int i=1;i<=n;i++)
    { a[i]=rdn()-fx; tp[a[i]]++;}
  tot=1; build(0,lm,1);
  for(int i=1;i<=n;i++)
    {
      int k=i-fx;
      if(tp[k]) mdfy(0,lm,1,k-tp[k]+1+fx2,k+fx2,1);
    }
  for(int i=1,x,y;i<=m;i++)
    {
      x=rdn(); y=rdn();
      if(x>0)
    {
      int d=a[x]-tp[a[x]]+1;
      if(a[x]<=n-fx) mdfy(0,lm,1,d+fx2,d+fx2,-1);
      tp[a[x]]--;
      a[x]=y-fx; tp[a[x]]++; d=a[x]-tp[a[x]]+1;
      if(a[x]<=n-fx) mdfy(0,lm,1,d+fx2,d+fx2,1);
    }
      else 
    {
      if(y==1)
        {
          int k=n-fx; fx++;
          if(tp[k]) mdfy(0,lm,1,k-tp[k]+1+fx2,k+fx2,-1);
        }
      else
        {
          fx--; int k=n-fx;
          if(tp[k]) mdfy(0,lm,1,k-tp[k]+1+fx2,k+fx2,1);
        }
    }
      Node tp=qry(0,lm,1,1-fx+fx2,n-fx+fx2);
      if(tp.mn>0)tp.ct=0; printf("%d\n",tp.ct);
    }
  return 0;
}

 

Guess you like

Origin www.cnblogs.com/Narh/p/10948167.html