C-猪猪养成计划1(set)

题目链接:https://ac.nowcoder.com/acm/contest/99785/C

题意:

给定一个数组,操作q次,分别为 标记 和 查询

思路:

将每一个数组值放入集合set中,消除掉已经遍历过的数组值,通过set二分来加速区间遍历

注意:

集合本身就存在二分函数lower_bound,通过.lower_bound()调用
set的erase操作分为两种,括号内为元素时返回消除的元素个数
括号内为迭代器时返回下一个有效迭代器

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
set<int>s;
int n,q;
const int maxn=1e5+5;
int ans[maxn];
int temp=1;
signed main()
{
	ios::sync_with_stdio(false),cin.tie(0);
	cin>>n>>q;
	for(int i=1;i<=n;i++)s.insert(i);
	int op;
	while(q--){
		cin>>op;
		if(op==1){
			int l,r;cin>>l>>r;
			auto it=s.lower_bound(l);
			while(it!=s.end()&&(*it)<=r)
			{
				ans[*it]=temp++;
				it=s.erase(it);
			}
		}else{
			int x;
			cin>>x;
			cout<<ans[x]<<endl;
		}
	}
	return 0;
}


posted @ 2025-01-18 12:04  Marinaco  阅读(27)  评论(0)    收藏  举报
//雪花飘落效果