3685: 普通van Emde Boas树——set

本文介绍了一种利用C++标准库中的set数据结构来高效处理数据增删查操作的方法。通过对不同操作进行优化,实现了快速查找最大最小值、边界值判断及元素存在性的检查等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目大意:

都是中文,自己看吧。题目链接

题解:

看题目的第一眼就发现所有操作都可以用set。

但很明显会TLE,于是。。。。

加了个玄学读入优化,fread读入优化

然后就卡过了。。。。。

代码:

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<set>
#include<queue>
using std::set;
int n,m;
set<int>s;
inline char nc(){
    static char buf[1000000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
}
inline int read(){
    char ch=nc();int sum=0;
    while(!(ch>='0'&&ch<='9'))ch=nc();
    while(ch>='0'&&ch<='9')sum=sum*10+ch-48,ch=nc();
    return sum;
}
int main()
{
	n=read();m=read();
	int x,y;
	for(int i=1;i<=m;i++)
	{
		x=read();
		if(x==1)
		{
		  y=read();s.insert(y);
		}
		if(x==2)
		{
			y=read();s.erase(y);
		}
		if(x==3)
		{
			 printf("%d\n",s.size()?*s.begin():-1);
		}
		if(x==4)
		{
			printf("%d\n",s.size()?*--s.end():-1);
		}
		if(x==5)
		{
			y=read();
			set<int>::iterator yu=s.lower_bound(y);
			printf("%d\n",yu!=s.begin()?*--yu:-1);
		}
		if(x==6)
		{
		y=read();
			set<int>::iterator yu=s.upper_bound(y);
			printf("%d\n",yu!=s.end()?*yu:-1);
		}
		if(x==7)
		{
			y=read();
			printf("%d\n",s.count(y)?1:-1);
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值