Codeforces Round #466 (Div. 2)-F-Machine Learning(待修改的莫队+离散化)


题意:给你n个数,m个查询,查询有两种:

(1)区间[l,r]中每个数出现的次数构成一个数列,问你这个数列中没出现的最小数是多少

(2)将a[l]修改成r

题解:我们考虑莫队,因为每个数有1e9这么大,因此要先对每个数进行离散化更新数组,然后直接跑莫队即可。

对于每个第二种操作,我们更新它的影响即可。

#include<map>
#include<math.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
#define ll long long 
#define maxn 300005
map<int,int>mp;
int n,q,cnt,tot,siz,sum[maxn],ans[maxn];
int l,r,a[maxn],aa[maxn],g[maxn];
struct node
{
	int l,r,t;
}b[maxn],c[maxn];
bool comp(node a,node b)
{
	if(a.l/siz<b.l/siz || a.l/siz==b.l/siz && a.r/siz<b.r/siz || a.l/siz==b.l/siz && a.r/siz==b.r/siz && a.t<b.t)
		return 1;
	return 0;
}
void update1(int x,int val)
{
	g[sum[x]]--;
	sum[x]+=val;
	g[sum[x]]++;
}
void update2(int x,int y)
{
	if(l<=x && x<=r)
		update1(a[x],-1),update1(y,1);
	a[x]=y;
}
int main(void)
{
	int i,j,k=0;
	scanf("%d%d",&n,&q);
	siz=ceil(pow(n,2.0/3));
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
		if(!mp[a[i]]) mp[a[i]]=++cnt;
		a[i]=aa[i]=mp[a[i]];
	}
	for(i=1;i<=q;i++)
	{
		scanf("%d%d%d",&c[i].t,&c[i].l,&c[i].r);
		if(c[i].t==1) 
			b[++tot]=c[i],b[tot].t=i;
		else
		{
			if(!mp[c[i].r]) mp[c[i].r]=++cnt;
			c[i].r=mp[c[i].r];
		}
	}
	sort(b+1,b+tot+1,comp);l=1;
	for(i=1;i<=tot;i++)
	{
		while(l>b[i].l) update1(a[--l],1);
		while(r<b[i].r) update1(a[++r],1);
		while(l<b[i].l) update1(a[l++],-1);
		while(r>b[i].r) update1(a[r--],-1);
		if(b[i].t<k)
		{
			k=0;
			for(j=1;j<=n;j++)
				update2(j,aa[j]);
		}
	//	printf("%d %d\n",l,r);
		for(;k<b[i].t;k++)
			if(c[k].t==2)
				update2(c[k].l,c[k].r);
		//printf("%d %d\n",a[7],sum[1]);
		for(j=1;g[j];j++);ans[b[i].t]=j;
	}
	for(i=1;i<=q;i++)
		if(ans[i])
			printf("%d\n",ans[i]);
	return 0;
}
/*
10 4
1 2 3 1 1 2 2 2 9 9
1 1 1
1 2 8
2 7 1
1 2 8
*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值