CodeForces - 940F Machine Learning —— 带修改莫队

2024-04-07 00:58

本文主要是介绍CodeForces - 940F Machine Learning —— 带修改莫队,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

You come home and fell some unpleasant smell. Where is it coming from?

You are given an array a. You have to answer the following queries:

You are given two integers l and r. Let ci be the number of occurrences of i in al: r, where al: r is the subarray of a from l-th element to r-th inclusive. Find the Mex of {c0, c1, …, c109}
You are given two integers p to x. Change ap to x.
The Mex of a multiset of numbers is the smallest non-negative integer not in the set.

Note that in this problem all elements of a are positive, which means that c0 = 0 and 0 is never the answer for the query of the second type.

Input
The first line of input contains two integers n and q (1 ≤ n, q ≤ 100 000) — the length of the array and the number of queries respectively.

The second line of input contains n integers — a1, a2, …, an (1 ≤ ai ≤ 109).

Each of the next q lines describes a single query.

The first type of query is described by three integers ti = 1, li, ri, where 1 ≤ li ≤ ri ≤ n — the bounds of the subarray.

The second type of query is described by three integers ti = 2, pi, xi, where 1 ≤ pi ≤ n is the index of the element, which must be changed and 1 ≤ xi ≤ 109 is the new value.

Output
For each query of the first type output a single integer — the Mex of {c0, c1, …, c109}.

Example
Input
10 4
1 2 3 1 1 2 2 2 9 9
1 1 1
1 2 8
2 7 1
1 2 8
Output
2
3
2
Note
The subarray of the first query consists of the single element — 1.

The subarray of the second query consists of four 2s, one 3 and two 1s.

The subarray of the fourth query consists of three 1s, three 2s and one 3.

题意:

给你n个数,有两个操作,1 l r表示在l到r这段区间,询问{c0,c1,c2,⋯,c109}的Mex,而ci表示数值i在l r中的出现次数,Mex代表未出现的最小的正整数。举个例子:1 2 3 4 3 2 2 ,最小为4,1 2 3 3 2 1最小是1.。。。2 p x 代表把a[p]改成x

题解:

带修莫队,cnt1[x]维护x出现的次数cnt2[x]维护出现x次的数有多少个。把所有2的操作记录下来,在记录tim代表这个1操作之前有多少2操作,如果在莫队的时候的t小了,那就增加2操作,反之减少

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int len;
struct node
{int l,r,t,bl,br,indx;node(){}node(int l,int r,int t,int indx):l(l),r(r),t(t),indx(indx){bl=l/len,br=r/len;}bool operator< (const node& a)const{if(bl==a.bl&&br==a.br)return t<a.t;if(bl==a.bl)return br<a.br;return bl<a.bl;}
}que[N];
struct query
{int pos,pre,val;// 位置,上一个数,当前的数query(){}query(int pos,int pre,int val):pos(pos),pre(pre),val(val){}
}c[N];
int cnt1[2*N],cnt2[2*N],a[N],b[N*2],now[N],cnt,ans[N];
void add(int pos)
{cnt2[cnt1[pos]]--;cnt1[pos]++;cnt2[cnt1[pos]]++;
}
void del(int pos)
{cnt2[cnt1[pos]]--;cnt1[pos]--;cnt2[cnt1[pos]]++;
}
int ll,rr;
void change(int pos,int x)
{if(pos>=ll&&pos<=rr){del(now[pos]);add(x);}now[pos]=x;
}
int main()
{int n,q;scanf("%d%d",&n,&q);len=pow(n,0.666667);for(int i=1;i<=n;i++){scanf("%d",&a[i]);now[i]=a[i];b[++cnt]=a[i];}/*for(int i=1;i<=n;i++)cout<<now[i]<<" ";cout<<endl;*/int op,qnum=0,tim=0;for(int i=1;i<=q;i++){scanf("%d%d%d",&op,&ll,&rr);if(op==1)que[++qnum]=node(ll,rr,tim,qnum);elseb[++cnt]=rr,c[++tim]=query(ll,now[ll],rr),now[ll]=rr;}/*for(int i=1;i<=n;i++)cout<<now[i]<<" ";cout<<endl;*/sort(b+1,b+1+cnt);sort(que+1,que+qnum+1);int all=unique(b+1,b+1+cnt)-b-1;for(int i=1;i<=n;i++)now[i]=lower_bound(b+1,b+1+all,a[i])-b;for(int i=1;i<=tim;i++){c[i].pre=lower_bound(b+1,b+1+all,c[i].pre)-b;c[i].val=lower_bound(b+1,b+1+all,c[i].val)-b;}ll=1,rr=0;int t=0;for(int i=1;i<=qnum;i++){while(ll>que[i].l)ll--,add(now[ll]);while(rr<que[i].r)rr++,add(now[rr]);while(ll<que[i].l)del(now[ll]),ll++;while(rr>que[i].r)del(now[rr]),rr--;while(t<que[i].t)t++,change(c[t].pos,c[t].val);while(t>que[i].t)change(c[t].pos,c[t].pre),t--;int mex=1;while(cnt2[mex])mex++;ans[que[i].indx]=mex;}for(int i=1;i<=qnum;i++)printf("%d\n",ans[i]);return 0;
}

这篇关于CodeForces - 940F Machine Learning —— 带修改莫队的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/881274

相关文章

修改若依框架Token的过期时间问题

《修改若依框架Token的过期时间问题》本文介绍了如何修改若依框架中Token的过期时间,通过修改`application.yml`文件中的配置来实现,默认单位为分钟,希望此经验对大家有所帮助,也欢迎... 目录修改若依框架Token的过期时间修改Token的过期时间关闭Token的过期时js间总结修改若依

MySQL修改密码的四种实现方式

《MySQL修改密码的四种实现方式》文章主要介绍了如何使用命令行工具修改MySQL密码,包括使用`setpassword`命令和`mysqladmin`命令,此外,还详细描述了忘记密码时的处理方法,包... 目录mysql修改密码四种方式一、set password命令二、使用mysqladmin三、修改u

使用Python在Excel中插入、修改、提取和删除超链接

《使用Python在Excel中插入、修改、提取和删除超链接》超链接是Excel中的常用功能,通过点击超链接可以快速跳转到外部网站、本地文件或工作表中的特定单元格,有效提升数据访问的效率和用户体验,这... 目录引言使用工具python在Excel中插入超链接Python修改Excel中的超链接Python

python修改字符串值的三种方法

《python修改字符串值的三种方法》本文主要介绍了python修改字符串值的三种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录第一种方法:第二种方法:第三种方法:在python中,字符串对象是不可变类型,所以我们没办法直接

Mysql8.0修改配置文件my.ini的坑及解决

《Mysql8.0修改配置文件my.ini的坑及解决》使用记事本直接编辑my.ini文件保存后,可能会导致MySQL无法启动,因为MySQL会以ANSI编码读取该文件,解决方法是使用Notepad++... 目录Myhttp://www.chinasem.cnsql8.0修改配置文件my.ini的坑出现的问题

Codeforces Round #240 (Div. 2) E分治算法探究1

Codeforces Round #240 (Div. 2) E  http://codeforces.com/contest/415/problem/E 2^n个数,每次操作将其分成2^q份,对于每一份内部的数进行翻转(逆序),每次操作完后输出操作后新序列的逆序对数。 图一:  划分子问题。 图二: 分而治之,=>  合并 。 图三: 回溯:

Codeforces Round #261 (Div. 2)小记

A  XX注意最后输出满足条件,我也不知道为什么写的这么长。 #define X first#define Y secondvector<pair<int , int> > a ;int can(pair<int , int> c){return -1000 <= c.X && c.X <= 1000&& -1000 <= c.Y && c.Y <= 1000 ;}int m

Codeforces Beta Round #47 C凸包 (最终写法)

题意慢慢看。 typedef long long LL ;int cmp(double x){if(fabs(x) < 1e-8) return 0 ;return x > 0 ? 1 : -1 ;}struct point{double x , y ;point(){}point(double _x , double _y):x(_x) , y(_y){}point op

Codeforces Round #113 (Div. 2) B 判断多边形是否在凸包内

题目点击打开链接 凸多边形A, 多边形B, 判断B是否严格在A内。  注意AB有重点 。  将A,B上的点合在一起求凸包,如果凸包上的点是B的某个点,则B肯定不在A内。 或者说B上的某点在凸包的边上则也说明B不严格在A里面。 这个处理有个巧妙的方法,只需在求凸包的时候, <=  改成< 也就是说凸包一条边上的所有点都重复点都记录在凸包里面了。 另外不能去重点。 int

Codeforces 482B 线段树

求是否存在这样的n个数; m次操作,每次操作就是三个数 l ,r,val          a[l] & a[l+1] &......&a[r] = val 就是区间l---r上的与的值为val 。 也就是意味着区间[L , R] 每个数要执行 | val 操作  最后判断  a[l] & a[l+1] &......&a[r] 是否= val import ja