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

相关文章

Python函数的基本用法、返回值特性、全局变量修改及异常处理技巧

《Python函数的基本用法、返回值特性、全局变量修改及异常处理技巧》本文将通过实际代码示例,深入讲解Python函数的基本用法、返回值特性、全局变量修改以及异常处理技巧,感兴趣的朋友跟随小编一起看看... 目录一、python函数定义与调用1.1 基本函数定义1.2 函数调用二、函数返回值详解2.1 有返

Nginx屏蔽服务器名称与版本信息方式(源码级修改)

《Nginx屏蔽服务器名称与版本信息方式(源码级修改)》本文详解如何通过源码修改Nginx1.25.4,移除Server响应头中的服务类型和版本信息,以增强安全性,需重新配置、编译、安装,升级时需重复... 目录一、背景与目的二、适用版本三、操作步骤修改源码文件四、后续操作提示五、注意事项六、总结一、背景与

SQL Server修改数据库名及物理数据文件名操作步骤

《SQLServer修改数据库名及物理数据文件名操作步骤》在SQLServer中重命名数据库是一个常见的操作,但需要确保用户具有足够的权限来执行此操作,:本文主要介绍SQLServer修改数据... 目录一、背景介绍二、操作步骤2.1 设置为单用户模式(断开连接)2.2 修改数据库名称2.3 查找逻辑文件名

Oracle修改端口号之后无法启动的解决方案

《Oracle修改端口号之后无法启动的解决方案》Oracle数据库更改端口后出现监听器无法启动的问题确实较为常见,但并非必然发生,这一问题通常源于​​配置错误或环境冲突​​,而非端口修改本身,以下是系... 目录一、问题根源分析​​​二、保姆级解决方案​​​​步骤1:修正监听器配置文件 (listener.

Linux中修改Apache HTTP Server(httpd)默认端口的完整指南

《Linux中修改ApacheHTTPServer(httpd)默认端口的完整指南》ApacheHTTPServer(简称httpd)是Linux系统中最常用的Web服务器之一,本文将详细介绍如何... 目录一、修改 httpd 默认端口的步骤1. 查找 httpd 配置文件路径2. 编辑配置文件3. 保存

Nginx 413修改上传文件大小限制的方法详解

《Nginx413修改上传文件大小限制的方法详解》在使用Nginx作为Web服务器时,有时会遇到客户端尝试上传大文件时返回​​413RequestEntityTooLarge​​... 目录1. 理解 ​​413 Request Entity Too Large​​ 错误2. 修改 Nginx 配置2.1

Python对PDF书签进行添加,修改提取和删除操作

《Python对PDF书签进行添加,修改提取和删除操作》PDF书签是PDF文件中的导航工具,通常包含一个标题和一个跳转位置,本教程将详细介绍如何使用Python对PDF文件中的书签进行操作... 目录简介使用工具python 向 PDF 添加书签添加书签添加嵌套书签Python 修改 PDF 书签Pytho

Docker镜像修改hosts及dockerfile修改hosts文件的实现方式

《Docker镜像修改hosts及dockerfile修改hosts文件的实现方式》:本文主要介绍Docker镜像修改hosts及dockerfile修改hosts文件的实现方式,具有很好的参考价... 目录docker镜像修改hosts及dockerfile修改hosts文件准备 dockerfile 文

Python实现无痛修改第三方库源码的方法详解

《Python实现无痛修改第三方库源码的方法详解》很多时候,我们下载的第三方库是不会有需求不满足的情况,但也有极少的情况,第三方库没有兼顾到需求,本文将介绍几个修改源码的操作,大家可以根据需求进行选择... 目录需求不符合模拟示例 1. 修改源文件2. 继承修改3. 猴子补丁4. 追踪局部变量需求不符合很

Linux修改pip和conda缓存路径的几种方法

《Linux修改pip和conda缓存路径的几种方法》在Python生态中,pip和conda是两种常见的软件包管理工具,它们在安装、更新和卸载软件包时都会使用缓存来提高效率,适当地修改它们的缓存路径... 目录一、pip 和 conda 的缓存机制1. pip 的缓存机制默认缓存路径2. conda 的缓