20pts求助
查看原帖
20pts求助
149933
Zero_Legend楼主2022/2/11 21:54

rt,只过了7和10。2和9RE了,其他都是WA,都显示“ read 0, expected -.”。应该是操作4,5出了问题,但实在找不到错,请各位大佬帮忙看看!

#include<bits/stdc++.h>
using namespace std;
const int N=5e5+5;
const int MAXN=2147483647;
const int MINN=-2147483647;
int n,m,a[N];
int op,l,r,pos,k;

struct Segmentree{
  int l,r,root;
}t[20*N];

struct Splayyy{
  int son[2],val,cnt,siz,fa;
}st[20*N];
int tot;

int read(){
  int w=0;bool f=0;
  char ch=getchar();
  while(!isdigit(ch)){if(ch=='-') f=1;ch=getchar();}
  while(isdigit(ch)){w=(w<<3)+(w<<1)+ch-'0';ch=getchar();}
  w=f?-w:w;
  return w;
}

void Pushup(int x){st[x].siz=st[st[x].son[0]].siz+st[st[x].son[1]].siz+st[x].cnt;}

void Rotate(int x){
	int y=st[x].fa;int z=st[y].fa;
	int k=(st[y].son[1]==x);
	st[z].son[st[z].son[1]==y]=x;st[x].fa=z;
	st[y].son[k]=st[x].son[k^1];st[st[x].son[k^1]].fa=y;
	st[x].son[k^1]=y;st[y].fa=x;
	Pushup(y);Pushup(x);
}

void Splay(int p,int x,int goal){
	while(st[x].fa!=goal){
		int y=st[x].fa;int z=st[y].fa;
		if(z!=goal) (st[y].son[1]==x)^(st[z].son[1]==y)?Rotate(x):Rotate(y);
		Rotate(x);
	}
	if(!goal) t[p].root=x;
}

void Insert(int p,int x){
	int now=t[p].root,now_fa=0;
	while(now&&st[now].val!=x){
		now_fa=now;
		now=st[now].son[x>st[now].val];
	}
	if(now) st[now].cnt++;
	else{
		now=++tot;
		if(now_fa) st[now_fa].son[x>st[now_fa].val]=tot;
		st[tot].fa=now_fa;st[tot].son[0]=st[tot].son[1]=0;
		st[tot].cnt=st[tot].siz=1;st[now].val=x;
	}
	Splay(p,now,0);
}

void Find(int p,int x){
	int now=t[p].root;if(!now) return ;
	while(st[now].val!=x&&st[now].son[x>st[now].val]) now=st[now].son[x>st[now].val];
	Splay(p,now,0);
}

int Next(int p,int x,int tp){
	Find(p,x);
	int now=t[p].root;
	if(st[now].val<x&&!tp){Splay(p,now,0);return now;}
	if(st[now].val>x&&tp){Splay(p,now,0);return now;}
	now=st[now].son[tp];
	while(st[now].son[tp^1]) now=st[now].son[tp^1];
  Splay(p,now,0);
	return now;
}

void Delete(int p,int x){
	int las=Next(p,x,0);int nex=Next(p,x,1);
	Splay(p,las,0);Splay(p,nex,las);
	int pos=st[nex].son[0];
	if(st[pos].cnt>1){st[pos].cnt--;Splay(p,pos,0);}
	else st[nex].son[0]=0;
  Pushup(nex);Pushup(las);
}

/*------------------spaly-------------------*/
//wrong:p=121777
void Build(int p,int l,int r){
  t[p].l=l;t[p].r=r;t[p].root=++tot;
  Insert(p,MAXN);Insert(p,MINN);
  for(int i=l;i<=r;i++) Insert(p,a[i]);
  if(l==r) return ;
  int mid=(l+r)/2;
  Build(2*p,l,mid);Build(2*p+1,mid+1,r);
}

int Segquery(int p,int L,int R,int k){
  if(t[p].l>=L&&t[p].r<=R){
    Insert(p,k),Find(p,k);
    int g=st[st[t[p].root].son[0]].siz-1;;
    Delete(p,k);return g;
  }
  int mid=(t[p].l+t[p].r)/2;
  int flag=0;
  if(mid>=L) flag+=Segquery(2*p,L,R,k);
  if(mid<R) flag+=Segquery(2*p+1,L,R,k);
  return flag;
}

int Segkth(int p,int L,int R,int k){
  int l=0,r=100000000,mid;
   while (l<r){
       mid=(l+r)/2+1;
       Insert(p,mid);
       int gg=Segquery(p,L,R,mid)+1;
       Delete(p,mid);
       if(gg>k) r=mid-1;
       else l=mid;
   }
   return l;
}

void Segmodify(int p,int pos,int k){
  Delete(p,a[pos]);Insert(p,k);
  int mid=(t[p].l+t[p].r)/2;
  if(t[p].l==t[p].r) return ;
  if(mid>=pos) Segmodify(2*p,pos,k);
  else Segmodify(2*p+1,pos,k);
}

int Seglast(int p,int L,int R,int k){
  if(t[p].l>=L&&t[p].r<=R) return st[Next(p,k,0)].val;
  int maxn=MINN,mid=(t[p].l+t[p].r)/2;
  if(mid>=L) maxn=max(maxn,Seglast(2*p,L,R,k));
  if(mid<R) maxn=max(maxn,Seglast(2*p+1,L,R,k));
  return maxn;
}

int Segnext(int p,int L,int R,int k){
  if(t[p].l>=L&&t[p].r<=R) return st[Next(p,k,1)].val;
  int minn=MAXN,mid=(t[p].l+t[p].r)/2;
  if(mid>=L) minn=min(minn,Segnext(2*p,L,R,k));
  if(mid<R) minn=min(minn,Segnext(2*p+1,L,R,k));
  return minn;
}
/*------------------segmentree------------------*/

int main(){
  n=read();m=read();
  for(int i=1;i<=n;i++) a[i]=read();
  Build(1,1,n);
  for(int i=1;i<=m;i++){
    op=read();
    if(op==1){
      l=read();r=read();k=read();
      printf("%d\n",Segquery(1,l,r,k)+1);
    }
    if(op==2){
      l=read();r=read();k=read();
      printf("%d\n",Segkth(1,l,r,k));
    }
    if(op==3){
      pos=read();k=read();
      Segmodify(1,pos,k);a[pos]=k;
    }
    if(op==4){
      l=read();r=read();k=read();
      int ans=Seglast(1,l,r,k);
      printf("%d\n",ans);
    }
    if(op==5){
      l=read();r=read();k=read();
      int ans=Segnext(1,l,r,k);
      printf("%d\n",ans);
    }
  }
  return 0;
}

2022/2/11 21:54
加载中...