bzoj3578(xor集合哈希+stl的利用)

用字符串读入字符,便于处理输入

迭代器的利用,注意erase后不能直接++,--了

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
int n,m,q;
map<ll,bool> mp;
set<int> s;
set<int>::iterator it;
ll key[200009],room[200009],num[200009];
int pos[200009];
int main()
{
	scanf("%d%d%d",&n,&m,&q);
	srand(2016+7+13);
	for (int i=1;i<=n;i++)
	{
		pos[i]=1;num[1]++;
		key[i]=((ll)rand()<<16) | rand();//随机一个值,注意rand()在int内最大约为3万
		room[1]=room[1]^key[i]; 
	}
	s.insert(1);
	int x,y,ans;
	char ch[3];
	for (int i=1;i<=q;i++)
	{
		scanf("%s%d%d",ch,&x,&y);//用字符串输入,可以避免多余空格和回车的影响 
		if (ch[0]=='C')
		{
			if (pos[x]==y) continue; 
			s.erase(pos[x]);
			s.erase(y);
			room[pos[x]]^=key[x];num[pos[x]]--;
			room[y]^=key[x];num[y]++;
			if (!mp[room[pos[x]]]) s.insert(pos[x]);
			if (!mp[room[y]]) s.insert(y); 
			pos[x]=y;
		}
		else if (ch[0]=='W') 
		{
			ans=0;
			it=s.lower_bound(x);
			while (*it<=y&&it!=s.end())
			{
				ans+=num[*it];
				mp[room[*it]]=true;
				s.erase(it);
				it=s.lower_bound(x);//被删除后就不能简单的++,--了 
			}
			printf("%d\n",ans);
		}
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值