保护警钟,我也来敲一敲
查看原帖
保护警钟,我也来敲一敲
759145
lhc0707楼主2023/8/18 21:38

如果你 WA 10pts,而你对数据的处理并没有问题,请检查你的输入输出,将读入单个字符改成读入字符串,并把Insert中的root=merge(merge(L,New(x)),R)改成root=merge(merge(L,cnt),R),同时将New()改为void形式。不要问我为什么,我也不知道,但我这样改之后就从 10pts 变为 AC

10 pts:

#include<ctime>
#include<cstdio>
#include<cstdlib>
#include<iostream>
using namespace std;
const int N=100005;
int n,limit,upd,rt,cnt,L,R,p,tot;
struct node{
	int ls,rs,siz;
	int pri,val;
}t[N];
int New(int x){
	t[++cnt].ls=t[cnt].rs=0,t[cnt].siz=1;
	t[cnt].pri=rand(); t[cnt].val=x; return cnt;
}
void Insert(int x){
	Split(rt,x,L,R);
	rt=Merge(Merge(L,New(x)),R);
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	srand(time(NULL));
	cin>>n>>limit;
	while(n--){
		char ch;int k;
		cin>>ch>>k;
		if(ch=='I'&&k>=limit){
			k-=upd;Insert(k);
		}
		else if(ch=='A')upd+=k;
		else if(ch=='S'){
			upd-=k;
			Split(rt,limit-upd-1,L,R);
			tot+=t[L].siz,rt=R;
		}
		else if(ch=='F'){
			if(k>t[rt].siz)cout<<-1<<endl;
			else cout<<t[GetNum(rt,t[rt].siz-k+1)].val+upd<<endl;
		}
	}
	cout<<tot<<endl;
	return 0;
}

AC代码:

#include<ctime>
#include<cstdio>
#include<cstdlib>
#include<iostream>
using namespace std;
const int N=100005;
int n,limit,upd,rt,cnt,L,R,p,tot;
struct node{
	int ls,rs,siz;
	int pri,val;
}t[N];
inline void Update(int u){
	t[u].siz=t[t[u].ls].siz+t[t[u].rs].siz+1;
}
void New(int x){
	++cnt,t[cnt].ls=t[cnt].rs=0,t[cnt].siz=1;
	t[cnt].pri=rand(); t[cnt].val=x; 
}
void Insert(int x){
	Split(rt,x,L,R); New(x);
	rt=Merge(Merge(L,cnt),R);
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	srand(time(NULL));
	cin>>n>>limit;
	while(n--){
		char ch[2];int k;
		cin>>ch>>k;
		if(ch[0]=='I'&&k>=limit){
			k-=upd;Insert(k);
		}
		if(ch[0]=='A')upd+=k;
		if(ch[0]=='S'){
			upd-=k;
			Split(rt,limit-upd-1,L,R);
			tot+=t[L].siz,rt=R;
		}
		if(ch[0]=='F'){
			if(k>t[rt].siz)cout<<-1<<endl;
			else cout<<t[GetNum(rt,t[rt].siz-k+1)].val+upd<<endl;
		}
	}
	cout<<tot<<endl;
	return 0;
}

不要问我为什么删掉一部分代码。。。

FHQ-Treap YYDS!

2023/8/18 21:38
加载中...