set+lower_bound;
注意:
s.lower_bound(x)返回的是迭代器,想要输出迭代器指向的值和它的前一位指向的值应该是:- -t而不是t- -;
auto t=s.lower_bound(x);
cout<<*t-*(--t);
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define pb push_back
#define fi first
#define se second
#define mem(a,x) memset(a,x,sizeof(a));
#define db double
//======================
const int N=2e5+10;
int a[N];
int main()
{
set<int>s;
int n,q;
cin>>n>>q;
s.insert(0),s.insert(n);
for(int i=0;i<q;i++)
{
int a,b;cin>>a>>b;
if(a==1)
{
s.insert(b);
}
else
{
auto t=s.lower_bound(b);
int ans=*t-*(--t);
cout<<ans<<endl;
}
}
return 0;
}
这篇博客介绍了一个C++程序,该程序利用set数据结构处理区间查询。程序首先插入0和n到set中,然后对于每个查询,如果类型为1则插入一个数,否则找到给定数的下界并计算与前一个元素的差值作为答案。程序使用了lower_bound函数来高效地进行查询。

458

被折叠的 条评论
为什么被折叠?



