2018.09.26 洛谷P2464 [SDOI2008]郁闷的小J(map+vector)

传送门
本来出题人出出来想考数据结构的。
但是我们拥有map+vector/set这样优秀的STL,因此直接用map离散化,vector存下标在里面二分找答案就行了。
代码:

#include<bits/stdc++.h>
#define N 100005
using namespace std;
inline int read(){
	int ans=0;
	char ch=getchar();
	while(!isdigit(ch))ch=getchar();
	while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(ch^48),ch=getchar();
	return ans; 
}
inline void write(int x){
	if(x>9)write(x/10);
	putchar((x%10)^48);
}
int n,m,x,tot=0,cnt=0,num[N];
map<int,int>mp;
vector<int>pos[N<<1];
int main(){
	n=read(),m=read();
	for(int i=1;i<=n;++i){
		num[i]=read();
		if(!mp[num[i]])mp[num[i]]=++tot;
		pos[mp[num[i]]].push_back(i);
	}
	while(m--){
		char op[2];
		scanf("%s",op);
		int a=read(),b=read();
		if(op[0]=='Q'){
			int c=read();
			vector<int>::iterator l=lower_bound(pos[mp[c]].begin(),pos[mp[c]].end(),a);
			vector<int>::iterator r=lower_bound(pos[mp[c]].begin(),pos[mp[c]].end(),b);
			if(l==pos[mp[c]].end()){puts("0");continue;}
			printf("%d\n",r-l+(*r==b));
		}
		else{
			if(num[a]==b)continue;
			if(!mp[b])mp[b]=++tot;
			pos[mp[num[a]]].erase(lower_bound(pos[mp[num[a]]].begin(),pos[mp[num[a]]].end(),a));
			pos[mp[b]].insert(lower_bound(pos[mp[b]].begin(),pos[mp[b]].end(),a),a); 
			num[a]=b;
		}
	}
	return 0;
}

转载于:https://www.cnblogs.com/ldxcaicai/p/9738202.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值