玄学RE
  • 板块灌水区
  • 楼主一铭君一
  • 当前回复3
  • 已保存回复3
  • 发布时间2021/7/28 21:07
  • 上次更新2023/11/4 12:49:25
查看原帖
玄学RE
307143
一铭君一楼主2021/7/28 21:07

RT,我的程序跑了一半直接RE了(类似)。

具体情况就是卡死。我输出变量调试了一波,发现没有无限递归,也没有死循环,就是单纯卡住不动了。

ps:不是爆系统栈,我开了 2GB 的栈还是卡死。。。

最离谱的是我把程序 exe 关掉之后缓冲区里答案跑出来是对的。

想问问这是什么情况。

代码恶臭,建议不要看(P4008 文本编辑器)

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<iomanip>
#include<cstring>
#include<utility>
#include<cstdio>
#include<queue>
#include<stack>
#include<ctime>
#include<cmath>
#include<list>
#include<set>
#include<map>

namespace zhang_tao{
	#define inf 0x7f7f7f7f
  #define ll long long
  template <typename _T>
  inline _T const& read(_T &x){
    x=0;int fh=1;
    char ch=getchar();
    while(!isdigit(ch)){

      if(ch=='-')
        fh=-1;
      ch=getchar();
    }
    while(isdigit(ch)){
      x=x*10+ch-'0';
      ch=getchar();
    }
    return x*=fh;
  }
  void write(int x){
    if(x<0) putchar('-'),x=-x;
    if(x>9) write(x/10);
    putchar(x%10+48);
  }
  inline int _gcd(const int x,const int y){
		return y?_gcd(y,x%y):x;
	}
	inline int _lcm(const int x,const int y){
		return x*y/_gcd(x,y);
	}
	inline int max(int a,int b,int c=-inf){
		return std::max(std::max(a,b),c);
	}
	inline int min(int a,int b,int c=inf){
		return std::min(std::min(a,b),c);
	}
	// inline void swap(int &x,int &y){
	// 	x^=y^=x^=y;
	// }
} // namespace zhang_tao

#define inf 0x7f7f7f7f
using namespace zhang_tao;
const int maxn=3000005;

int n;
int cursor=1;

struct Splay{
  int ch,size,tag;
  Splay *fa,*son[2];
};

struct Splay *root,*null;

inline void swap(Splay* &a,Splay* &b){
  Splay *tmp=a;a=b;b=tmp;
}

inline void update(Splay *node){
  node->size=node->son[0]->size+node->son[1]->size+1;
}

inline bool get_which(Splay *node){
  return node->fa->son[1]==node;
}

inline void push_down(Splay *node){
  if(node->tag){
    node->son[1]->tag^=1;
    node->son[0]->tag^=1;
    swap(node->son[0],node->son[1]);
    node->tag=0;
  }
}

void rotate(Splay *node){
  Splay *fa=node->fa;
  Splay *grandfa=fa->fa;
  int which_son=get_which(node);
  push_down(fa),push_down(node);
  if(grandfa!=null)
    grandfa->son[get_which(fa)]=node;
  node->fa=grandfa;
  fa->son[which_son]=node->son[which_son^1];
  node->son[which_son^1]->fa=fa;
  node->son[which_son^1]=fa;
  fa->fa=node;
  update(fa),update(node);  
}

void splay(Splay *node,Splay *target){
  while(node->fa!=target){
    Splay *fa=node->fa;
    Splay *grandfa=fa->fa;
    if(grandfa!=target) 
      rotate( get_which(node) ^ get_which(fa) ? node :fa );
    rotate(node);
  }
  if(target==null)
    root=node;
}

Splay *get_kth(int k){
  Splay *node=root;
  while(1){
    push_down(node);
    if(node->son[0]->size>=k)
      node=node->son[0];
    else{
      k-=(node->son[0]->size+1);
      if(k<=0) return node;
      else node=node->son[1];
    }
  }
}

void insert(int value,int k){
  Splay *kth=get_kth(k);
  Splay *k1th=get_kth(k+1);
  splay(kth,null);splay(k1th,kth);
  Splay *node=new Splay;
  node->fa=k1th;
  k1th->son[0]=node;
  node->son[0]=node->son[1]=null;
  node->tag=0; 
  node->size=1;
  node->ch=value;
  splay(node,null);
}

void _delete(int k,int num){
  Splay *L=get_kth(k);
  Splay *R=get_kth(k+num+1);
  splay(L,null),splay(R,L);
  R->son[0]=null;
  splay(R,null);
}

void reverse(int k,int num){
  Splay *L=get_kth(k),*R=get_kth(k+num+1);
  splay(L,null),splay(R,L);
  R->son[0]->tag^=1;
}

//void Get(int k){
//  putchar(get_kth(k+1)->ch+32);
//  putchar('\n');
//}

void output(Splay *node){
	push_down(node);
	if(node->son[0]!=null)
		output(node->son[0]);
	if(node->ch>=32 && node->ch<=126)
		putchar(node->ch);
	if(node->son[1]!=null)
		output(node->son[1]); 
}//不出意外的话是这个函数炸了

void Get(int k){
	Splay *L=get_kth(cursor);
	Splay *R=get_kth(cursor+1+k);
	splay(L,null),splay(R,L);
	output(R->son[0]);
	putchar('\n');
} 

char str[maxn];

void Init(){
  null=new Splay;
  null->size=null->tag=null->ch=0;
  null->fa=null->son[0]=null->son[1]=NULL;
  Splay *p=new Splay;
  p->ch=-inf,p->tag=0,p->size=1;
  p->fa=p->son[1]=p->son[0]=null;
  Splay *q=new Splay;
  q->ch=inf,q->tag=0,q->size=1;
  q->son[0]=q->son[1]=null,q->fa=p;
  p->son[1]=q;update(p);
  root=p;
}

signed main(){
	freopen("P4008_1.in","r",stdin);
	freopen("my.out","w",stdout);
  Init();
//  insert((int)'a'-' ',cursor);
//  _delete(cursor,1);
//  Get(cursor);
  read(n);
  char opt[15];int x;
  while(n--){
    scanf("%s",opt);
    switch(opt[0]){
      case 'M' :{
      	read(x);
        cursor=x+1;
				break;
			}  
      case 'I' :{
      	read(x);
        for(int i=1;i<=x;++i){
        	str[i]=getchar();
        	str[i]=='\n' ? --i : 0;
				}
        int tmp=cursor;
        for(int i=1;i<=x;++i)
          insert(str[i],tmp++);
				break;
			}
      case 'D' :{
      	read(x);
        _delete(cursor,x);
				break;
			}
      case 'R' :{
      	read(x);
        reverse(cursor,x);
				break;
			}
      case 'G' :{
      	read(x);
      	Get(x);
				break;
			}
      case 'P' :{
      	cursor--;
				break;
			}
      case 'N' :{
      	cursor++;
				break;
			}
  	}
  }
  return 0;
}

2021/7/28 21:07
加载中...