测试点20TLE,操作完后卡住了。求调
查看原帖
测试点20TLE,操作完后卡住了。求调
989792
lrx___楼主2024/7/31 02:12

本人下载了数据,并在每次操作后输出操作的编号。本人证明 500000 次操作都执行完了,然后就卡住不动了。

对于码风请谅解。因为本人想使用指针且不想在初始时开够数组,于是使用了 std::shared_ptr 实现动态开点。

// #define ___coding

#ifdef ___coding
#define ___coding1 true
#else
#define ___coding1 false
#endif
#include<cstdlib>
// #include<cassert>
// #include<cctype>
// #include<climits>
// #include<cmath>
#include<cstdio>
// #include<cstring>
// #include<ctime>

// #include<algorithm>
// using std::__gcd;
// using std::lower_bound;
// using std::max;
// using std::min;
// using std::next_permutation;
// using std::reverse;
// using std::sort;
// using std::swap;
// using std::unique;
// using std::upper_bound;

// #include<bitset>
// using std::bitset;

// #include<deque>
// using std::deque;

// #include<fstream>
// using std::ifstream;
// using std::ofstream;

// #include<iomanip>
// using std::fixed;
// using std::setprecision;

// #include<iostream>
// using std::cin;
// using std::cout;
// using std::ios;

// #include<list>
// using std::list;

// #include<map>
// using std::map;

#include<memory>
using std::make_shared;
// using std::make_unique;
using std::shared_ptr;
// using std::unique_ptr;

// #include<queue>
// using std::priority_queue;
// using std::queue;

// #include<stack>
// using std::stack;

// #include<set>
// using std::multiset;
// using std::set;

// #include<sstream>
// using std::stringstream;

// #include<string>
// using std::getline;
// using std::string;
// using std::to_string;

// #include<utility>
// using std::make_pair;
// using std::pair;

// #include<unordered_map>
// using std::unordered_map;

// #include<unordered_set>
// using std::unordered_set;

// #include<vector>
// using std::vector;
#define UP(i,a,b) for(i=a;i<=(b);++i)
#define DN(i,a,b) for(i=a;i>=(b);--i)
#define up(i,a,b) for(i=a;i<(b);++i)
#define dn(i,a,b) for(i=a;i>(b);--i)
#define pr make_pair
typedef unsigned char unc;
typedef unsigned short uns;
typedef unsigned uni;
typedef unsigned long unl;
typedef long long ll;
typedef unsigned long long ull;
const int BUF_SIZE(1<<20);
bool ___f;
char ___c,___a[45],*___p,___ibuf[BUF_SIZE+1],*___ib(___ibuf),*___ie(___ibuf),___obuf[BUF_SIZE+1],*___ob(___obuf),*___oe(___obuf+BUF_SIZE);
char& gtch(){if(___ib==___ie)___ie=(___ib=___ibuf)+fread(___ibuf,1,BUF_SIZE,stdin);if(___ib==___ie){return *___ibuf=-1;}return *___ib++;}
void ___flush(){fwrite(___obuf,1,___ob-___obuf,stdout);___ob=___obuf;}
void ptch(char c){if(___ob==___oe)___flush();*___ob=c;++___ob;}
typedef __int128 pll;
typedef unsigned __int128 upll;
void rd(pll &r){r=0;___f=false;for(___c=gtch();___c<'0'||___c>'9';___c=gtch()){___f=(___c=='-');}for(;___c>='0'&&___c<='9';___c=gtch()){r=(r<<3)+(r<<1)+(___c^'0');}if(___f){r=~(r-1);}}
void rd(upll &r){r=0;for(___c=gtch();___c<'0'||___c>'9';___c=gtch());for(;___c>='0'&&___c<='9';___c=gtch()){r=(r<<3)+(r<<1)+(___c^'0');}}
void wt(pll r){if(!r){ptch('0');return;}if(r<0){ptch('-');r=~(r-1);}___p=___a;while(r!=0){++___p;*___p=char(r%10)^'0';r/=10;}while(___p!=___a){ptch(*___p);--___p;}}
void wt(upll r){if(!r){ptch('0');return;}___p=___a;while(r!=0){++___p;*___p=char(r%10)^'0';r/=10;}while(___p!=___a){ptch(*___p);--___p;}}
void rd(bool &r){r=false;for(___c=gtch();___c<'0'||___c>'9';___c=gtch());for(;___c>='0'&&___c<='9';___c=gtch()){r|=bool(___c^'0');}}
void rd(char &c){for(c=gtch();c==' '||c=='\r'||c=='\n';c=gtch());}
void rd(short &r){r=0;___f=false;for(___c=gtch();___c<'0'||___c>'9';___c=gtch()){___f=(___c=='-');}for(;___c>='0'&&___c<='9';___c=gtch()){r=(r<<3)+(r<<1)+(short)(___c^'0');}if(___f){r=~(r-1);}}
void rd(int &r){r=0;___f=false;for(___c=gtch();___c<'0'||___c>'9';___c=gtch()){___f=(___c=='-');}for(;___c>='0'&&___c<='9';___c=gtch()){r=(r<<3)+(r<<1)+(___c^'0');}if(___f){r=~(r-1);}}
void rd(long &r){r=0;___f=false;for(___c=gtch();___c<'0'||___c>'9';___c=gtch()){___f=(___c=='-');}for(;___c>='0'&&___c<='9';___c=gtch()){r=(r<<3)+(r<<1)+(___c^'0');}if(___f){r=~(r-1);}}
void rd(ll &r){r=0;___f=false;for(___c=gtch();___c<'0'||___c>'9';___c=gtch()){___f=(___c=='-');}for(;___c>='0'&&___c<='9';___c=gtch()){r=(r<<3)+(r<<1)+(___c^'0');}if(___f){r=~(r-1);}}
void rd(unc &c){for(c=gtch();c==' '||c=='\r'||c=='\n';c=gtch());}
void rd(uns &r){r=0;for(___c=gtch();___c<'0'||___c>'9';___c=gtch());for(;___c>='0'&&___c<='9';___c=gtch()){r=(r<<3)+(r<<1)+(uns)(___c^'0');}}
void rd(uni &r){r=0;for(___c=gtch();___c<'0'||___c>'9';___c=gtch());for(;___c>='0'&&___c<='9';___c=gtch()){r=(r<<3)+(r<<1)+(___c^'0');}}
void rd(unl &r){r=0;for(___c=gtch();___c<'0'||___c>'9';___c=gtch());for(;___c>='0'&&___c<='9';___c=gtch()){r=(r<<3)+(r<<1)+(___c^'0');}}
void rd(ull &r){r=0;for(___c=gtch();___c<'0'||___c>'9';___c=gtch());for(;___c>='0'&&___c<='9';___c=gtch()){r=(r<<3)+(r<<1)+(___c^'0');}}
void rd(char *s){for(*s=gtch();*s==' '||*s=='\r'||*s=='\n';*s=gtch());for(;~*s&&*s!=' '&&*s!='\r'&&*s!='\n';++s,*s=gtch());*s=0;}
void rd(char *s,char n){for(*s=gtch();*s==' '||*s=='\r'||*s=='\n';*s=gtch());for(;~*s&&*s!=n;++s,*s=gtch());*s=0;}
void wt(bool a){ptch('0'+a);}
void wt(char c){ptch(c);}
void wt(short r){if(!r){ptch('0');return;}if(r<0){ptch('-');r=~(r-1);}___p=___a;while(r!=0){++___p;*___p=char(r%10)^'0';r/=10;}while(___p!=___a){ptch(*___p);--___p;}}
void wt(int r){if(!r){ptch('0');return;}if(r<0){ptch('-');r=~(r-1);}___p=___a;while(r!=0){++___p;*___p=char(r%10)^'0';r/=10;}while(___p!=___a){ptch(*___p);--___p;}}
void wt(long r){if(!r){ptch('0');return;}if(r<0){ptch('-');r=~(r-1);}___p=___a;while(r!=0){++___p;*___p=char(r%10)^'0';r/=10;}while(___p!=___a){ptch(*___p);--___p;}}
void wt(ll r){if(!r){ptch('0');return;}if(r<0){ptch('-');r=~(r-1);}___p=___a;while(r!=0){++___p;*___p=char(r%10)^'0';r/=10;}while(___p!=___a){ptch(*___p);--___p;}}
void wt(unc c){ptch(c);}
void wt(uns r){if(!r){ptch('0');return;}___p=___a;while(r!=0){++___p;*___p=char(r%10)^'0';r/=10;}while(___p!=___a){ptch(*___p);--___p;}}
void wt(uni r){if(!r){ptch('0');return;}___p=___a;while(r!=0){++___p;*___p=char(r%10)^'0';r/=10;}while(___p!=___a){ptch(*___p);--___p;}}
void wt(unl r){if(!r){ptch('0');return;}___p=___a;while(r!=0){++___p;*___p=char(r%10)^'0';r/=10;}while(___p!=___a){ptch(*___p);--___p;}}
void wt(ull r){if(!r){ptch('0');return;}___p=___a;while(r!=0){++___p;*___p=char(r%10)^'0';r/=10;}while(___p!=___a){ptch(*___p);--___p;}}
void wt(char *s){for(;*s;++s){ptch(*s);}}
void wt(const char *s){for(;*s;++s){ptch(*s);}}
template<typename T,typename ...T1>void rd(T &a,T1 &...b){rd(a);rd(b...);}
template<typename T,typename ...T1>void wt(T a,T1 ...b){wt(a);wt(b...);}
template<typename T>void RD(T* a,int n=1){while(n--){rd(*a);++a;}}
ll _llabs(ll a){return a<0?-a:a;}

/*

*/
const int N(5e5+5),INF(2147483647);
int n;
struct node{
	int sz,vl,pri;
	shared_ptr<node> ls,rs;
	node(){}
	node(const node &oth):sz(oth.sz),vl(oth.vl),pri(oth.pri),ls(oth.ls),rs(oth.rs){}
};
shared_ptr<node> root[N],nil;

void init_node(shared_ptr<node> u,int k){
	u->sz=1;u->vl=k;u->pri=rand();
	u->ls=u->rs=nil;
}
void push_u(shared_ptr<node> u){
	u->sz=u->ls->sz+u->rs->sz+1;
}
shared_ptr<node> clone(shared_ptr<node> u){
	return make_shared<node>(*u);
}
void split(shared_ptr<node> u,int k,shared_ptr<node> &l,shared_ptr<node> &r){
	if(u==nil){
		l=r=nil;return;
	}
	u=clone(u);
	if(k<u->vl){
		r=u;
		split(u->ls,k,l,u->ls);
	}else{
		l=u;
		split(u->rs,k,u->rs,r);
	}
	push_u(u);
}
void merge(shared_ptr<node> l,shared_ptr<node> r,shared_ptr<node> &u){
	if(l==nil){
		u=r;return;
	}else if(r==nil){
		u=l;return;
	}
	if(l->pri>r->pri){
		u=l;
		merge(l->rs,r,l->rs);
	}else{
		u=r;
		merge(l,r->ls,r->ls);
	}
	push_u(u);
}
void insert(int k,shared_ptr<node> &root){
	shared_ptr<node> u,v,w;
	split(root,k,u,v);
	w=make_shared<node>();
	init_node(w,k);
	merge(u,w,u);merge(u,v,root);
}
void erase(int k,shared_ptr<node> &root){
	shared_ptr<node> u,v,w,ls,rs;
	split(root,k,u,v);
	split(u,k-1,u,w);
	if(w!=nil){
		ls=w->ls;rs=w->rs;
		w.reset();
		merge(ls,rs,w);
	}
	merge(u,w,u);
	merge(u,v,root);
}
int less_than(int k,shared_ptr<node> u){
	int res(0);
	while(u!=nil){
		if(k<=u->vl){
			u=u->ls;
		}else{
			res+=u->ls->sz+1;
			u=u->rs;
		}
	}
	return res;
}
int kth(int k,shared_ptr<node> u){
	while(k!=u->ls->sz+1){
		if(k<u->ls->sz+1){
			u=u->ls;
		}else{
			k-=u->ls->sz+1;
			u=u->rs;
		}
	}
	return u->vl;
}
int pre(int k,shared_ptr<node> u){
	int r(less_than(k,u));
	if(r==0)return -INF;
	return kth(r,u);
}
int suf(int k,shared_ptr<node> u){
	int r(less_than(k+1,u));
	if(r==u->sz)return INF;
	return kth(r+1,u);
}
void init(){
	nil=make_shared<node>();
	init_node(nil,0);nil->sz=0;
	root[0]=nil;
}
void main1(){
	int i,o,k,v;
	init();
	rd(n);
	UP(i,1,n){
		rd(v,o,k);
		root[i]=root[v];
		switch(o){
			case 1:insert(k,root[i]);break;
			case 2:erase(k,root[i]);break;
			case 3:printf("%d\n",less_than(k,root[i])+1);break;
			case 4:printf("%d\n",kth(k,root[i]));break;
			case 5:printf("%d\n",pre(k,root[i]));break;
			case 6:printf("%d\n",suf(k,root[i]));break;
		}
	}
}
int main(){if(___coding1)freopen("in.txt","r",stdin);main1();___flush();if(___coding1){freopen("CON","r",stdin);system("pause");}return 0;}

2024/7/31 02:12
加载中...