treap求助
  • 板块P2073 送花
  • 楼主PurslaneTsing Wah
  • 当前回复7
  • 已保存回复7
  • 发布时间2021/7/24 18:59
  • 上次更新2023/11/4 13:25:44
查看原帖
treap求助
120947
PurslaneTsing Wah楼主2021/7/24 18:59

RT,样例随机WA(貌似是treap写挂了

#include<bits/stdc++.h>
#define INF INT_MAX
using namespace std;
inline bool id(const char ch) {
	return ch>='0'&&ch<='9';	
}
inline int read(void) {
	int s=0,f=0;
	char ch=getchar();
	while(!id(ch)) f=(ch=='-'),ch=getchar();
	while(id(ch)) s=(s<<1)+(s<<3)+(ch^48),ch=getchar();
	return f?-s:s;	
}
const int MAXN=100000+10;
struct tree {
	int l,r,key,val,cnt,sze,w;	
}tr[MAXN];
int n,root,idx,ans1,ans2;
inline void push_up(const int p) {
	tr[p].sze=tr[tr[p].l].sze+tr[tr[p].r].sze+1;
	return;	
}
inline int get_node(const int k,const int w) {
	tr[++idx]=tree{0,0,k,rand(),1,1,w};
	return idx;
}
inline void zig(int &p) {
	int q=tr[p].l;
	tr[p].l=tr[q].r,tr[q].r=p,p=q;
	push_up(tr[p].r),push_up(p);
	return;	
}
inline void zag(int &p) {
	int q=tr[p].r;
	tr[p].r=tr[q].l,tr[q].l=p,p=q;
	push_up(tr[p].l),push_up(p);
	return;
}
void remv_min(int &p) {
	if(tr[p].key==-INF||tr[p].key==INF) return;
	if(tr[tr[p].l].key==-INF||p==0) {p=0;return;}
	remv_min(tr[p].l);
	push_up(tr[p].l),push_up(p);
	return;	
}
void remv_max(int &p) {
	if(tr[p].key==-INF||tr[p].key==INF) return;
	if(tr[tr[p].r].key==INF||p==0) {p=0;return;}
	remv_max(tr[p].r);
	push_up(tr[p].r),push_up(p);
	return;	
}
void insert(int& p,const int k,const int w) {
	if(!p) {p=get_node(k,w);return;}
	if(tr[p].key==k) return;
	if(tr[p].key>k) {
		insert(tr[p].l,k,w);
		if(tr[tr[p].l].val>tr[p].val) zig(p);	
	}
	else {
		insert(tr[p].r,k,w);
		if(tr[tr[p].r].val>tr[p].val) zag(p);	
	}
	push_up(p);
	return;
}
inline void build(void) {
	get_node(-INF,0),get_node(INF,0);
	root=1,tr[root].r=2;
	push_up(root);
	if(tr[root].val<tr[2].val) zag(root);
	return;
}
void get_ans(int p) {
	if(p==0) return;
	if(tr[p].key==INF||tr[p].key==-INF) return ;
	ans1+=tr[p].key,ans2+=tr[p].w;
	get_ans(tr[p].l),get_ans(tr[p].r);
	return;	
}
int main() {
	srand(time(NULL));
	build(); 
	for(int i=1,op,w,c;;i++) {
		op=read();
		if(op==-1) break;
		else if(op==1) w=read(),c=read(),insert(root,c,w);
		else if(op==2) remv_max(root);
		else remv_min(root);
	}
	get_ans(root);
	cout<<ans2<<" "<<ans1;
	return 0;
}
2021/7/24 18:59
加载中...