社区讨论

样例输出11 11 交上去听取WA声一片 求条

P2073送花参与者 2已保存回复 2

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
2 条
当前快照
1 份
快照标识符
@m243k4gh
此快照首次捕获于
2024/10/11 10:16
去年
此快照最后确认于
2025/11/04 17:28
4 个月前
查看原帖
C
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6+5;
const int Inf = 1e9+5;
struct node{
	int ch[2],fa,val,w,siz;
};
node tree[N];
int root,tot,cnt;
int ans = 0,money = 0;
int get(int x){
	return x==tree[tree[x].fa].ch[1];
}
void pushup(int x){
	if(x==0) {
		return;	
	}
	tree[x].siz = tree[tree[x].ch[0]].siz+tree[tree[x].ch[1]].siz+1;
}
void rotate(int x){
	int y = tree[x].fa;
	int z = tree[y].fa;
	int chk = get(x);
	tree[y].ch[chk] = tree[x].ch[chk^1];
	if(tree[x].ch[chk^1]){
		tree[tree[x].ch[chk^1]].fa = y;
	}
	tree[x].ch[chk^1] = y;
	tree[y].fa = x;
	if(z){
		tree[z].ch[y==tree[z].ch[1]] = x;
	}
	tree[x].fa = z;
}
void splay(int x,int k){
	while(tree[x].fa!=k){
		int y = tree[x].fa;
		int z = tree[y].fa;
		if(z!=k){
			if(get(x)==get(y)){
				rotate(y);
			}
			else{
				rotate(x);
			}
		}
		rotate(x);
	}
	if(k==0) root = x;
}
void insert(int x,int k){
	int cur = root;
	int fa = 0;
	while(cur){
		if(x==tree[cur].val){
			cnt--;
			return;
		}
		fa = cur;
		cur = tree[cur].ch[x>tree[cur].val];
	}
	cur = ++tot;
	tree[cur].val = x;
	tree[cur].w = k;
	tree[cur].fa = fa;
	tree[fa].ch[x>tree[fa].val] = cur;
	pushup(cur);
	pushup(fa);
	splay(cur,0);
	return;
}
void find(int x){
	int cur = root;
	while(cur){
		if(x==tree[cur].val){
			splay(cur,0);
			return;
		}
		cur = tree[cur].ch[x<tree[cur].val];
	}
}
void del(int x){
	find(x);
	int L = tree[root].ch[0];
	int R = tree[root].ch[1];
	while(tree[L].ch[1]) L = tree[L].ch[1];
	while(tree[R].ch[0]) R = tree[R].ch[0];
	tree[R].ch[0]=0;
	pushup(R);
	pushup(L);
}
int kth(int x){
	int cur = root;
	while(cur){
		if(x<=tree[tree[cur].ch[0]].siz){
			cur = tree[cur].ch[0];
		}
		else{
			x-=tree[tree[cur].ch[0]].siz;
			if(x==1){
				splay(cur,0);
				return cur;
			} 
			else{
				x--;
				cur = tree[cur].ch[1];
			}
		}
	}
	return -1;
} 
void sum(int x){
	if(tree[x].w!=-Inf){
		ans+=tree[x].w;
		money += tree[x].val;
	}
	if(tree[x].ch[0]) sum(tree[x].ch[0]);
	if(tree[x].ch[1]) sum(tree[x].ch[1]);
}
int main(){
	insert(Inf,-Inf);
	insert(-Inf,-Inf);
	int op;
	while(cin>>op){
		if(op==-1){
			sum(root);
			cout<<ans<<" "<<money<<endl;
			return 0;
		}
		if(op==1){
			int x,y;
			cin>>x>>y;
			insert(y,x);
			cnt++;
		}
		if(op==2&&cnt>0){
			del(tree[kth(cnt+1)].val); 
			cnt--;
		}
		if(op==3&&cnt>0){
			del(tree[kth(2)].val);
			cnt--;
		}
	}
	return 0;
}

回复

2 条回复,欢迎继续交流。

正在加载回复...