codeforces 940F 带修改的莫队
4 seconds
512 megabytes
standard input
standard output
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.
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.
For each query of the first type output a single integer — the Mex of {c0, c1, ..., c109}.
- 10 4
1 2 3 1 1 2 2 2 9 9
1 1 1
1 2 8
2 7 1
1 2 8
- 2
3
2
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.
大意:给出一个序列,两种操作。
1.询问一个区间内,把每种元素的个数组成一个集合,这个集合的mex值(不在集合中的最小值)。
2.单点修改。
题解:
莫队算法:
题目给出数值的大小过大,但是实际出现过的最多200000个,离散化,把出现过的数从小到大排列,和1——200000,一一对应。
这样题目中的每个数值都可以用1——200000中的数等价替换。
然后就是带修改的莫队。
其实就是多加了一维时间,三维和二维类比一下,就是先按照左端点分块,再按照右端点分块,块中时间单调。
块的大小需要时n^(2/3),如果还是sqrt(n)会TLE。
因为块的大小是n^(2/3)的时候复杂度最低,通过考虑每种移动方式的复杂度可以证明,详情请移步大神博客:
https://www.luogu.org/blog/user12668/solution-p1903
- /*
- Welcome Hacking
- Wish You High Rating
- */
- #include<iostream>
- #include<cstdio>
- #include<cstring>
- #include<ctime>
- #include<cstdlib>
- #include<algorithm>
- #include<cmath>
- #include<string>
- #include<map>
- using namespace std;
- int read(){
- int xx=,ff=;char ch=getchar();
- while(ch>''||ch<''){if(ch=='-')ff=-;ch=getchar();}
- while(ch>=''&&ch<=''){xx=xx*+ch-'';ch=getchar();}
- return xx*ff;
- }
- const int maxn=;
- int N,M,block,a[maxn],b[maxn],belong[maxn];
- struct query{
- int L,R,tim,id;
- bool friend operator<(const query&A,const query&B){
- if(belong[A.L]!=belong[B.L])
- return A.L<B.L;
- if(belong[A.R]!=belong[B.R])
- return A.R<B.R;
- return A.tim<B.tim;
- }
- }Q[maxn];
- struct change{
- int pos,x,y;
- }C[maxn];
- int tp1,tp2,tp;
- int pm[maxn*],arg[][maxn];
- map<int,int>mp;
- int rk[maxn*],tot;
- int cnt[maxn*],siz[maxn],ans[maxn];
- int x,y,z;
- inline void add(int i){
- siz[cnt[i]]--;
- siz[++cnt[i]]++;
- }
- inline void del(int i){
- siz[cnt[i]]--;
- siz[--cnt[i]]++;
- }
- inline void change_add(int i){
- if(C[i].pos>=x&&C[i].pos<=y){
- del(C[i].x);
- add(C[i].y);
- }
- a[C[i].pos]=C[i].y;
- }
- inline void change_del(int i){
- if(C[i].pos>=x&&C[i].pos<=y){
- del(C[i].y);
- add(C[i].x);
- }
- a[C[i].pos]=C[i].x;
- }
- int main(){
- //freopen("in.txt","r",stdin);
- N=read(),M=read();
- for(int i=;i<=N;i++)
- pm[++tp]=b[i]=read();
- for(int i=;i<=M;i++){
- for(int j=;j<=;j++)
- arg[j][i]=read();
- if(arg[][i]==)
- pm[++tp]=arg[][i];
- }
- sort(pm+,pm++tp);
- for(int i=;i<=tp;i++)
- if(!mp[pm[i]])
- mp[pm[i]]=++tot,rk[tot]=pm[i];
- for(int i=;i<=N;i++)
- a[i]=b[i]=mp[b[i]];
- for(int i=;i<=M;i++)
- if(arg[][i]==)
- arg[][i]=mp[arg[][i]];
- for(int i=;i<=M;i++)
- if(arg[][i]==)
- Q[++tp1].tim=tp2,Q[tp1].L=arg[][i],Q[tp1].R=arg[][i],Q[tp1].id=tp1;
- else
- C[++tp2].pos=arg[][i],C[tp2].x=b[C[tp2].pos],C[tp2].y=arg[][i],b[C[tp2].pos]=arg[][i];
- block=(int)pow(N+0.5,2.0/);//caution
- for(int i=;i<=N;i++)
- belong[i]=(i-)/block+;
- sort(Q+,Q++tp1);
- x=Q[].L,y=Q[].L-,z=;
- for(int i=;i<=tp1;i++){
- for(;x<Q[i].L;x++)
- del(a[x]);
- for(;x>Q[i].L;x--)
- add(a[x-]);
- for(;y<Q[i].R;y++)
- add(a[y+]);
- for(;y>Q[i].R;y--)
- del(a[y]);
- for(;z<Q[i].tim;z++)
- change_add(z+);
- for(;z>Q[i].tim;z--)
- change_del(z);
- for(int j=;;j++)
- if(!siz[j]){
- ans[Q[i].id]=j;
- break;
- }
- }
- for(int i=;i<=tp1;i++)
- printf("%d\n",ans[i]);
- return ;
- }
codeforces 940F 带修改的莫队的更多相关文章
- Machine Learning CodeForces - 940F (带修改的莫队)
You come home and fell some unpleasant smell. Where is it coming from? You are given an array a. You ...
- BZOJ 2120: 数颜色 带修改的莫队算法 树状数组套主席树
https://www.lydsy.com/JudgeOnline/problem.php?id=2120 标题里是两种不同的解法. 带修改的莫队和普通莫队比多了个修改操作,影响不大,但是注意一下细节 ...
- 【BZOJ】2120: 数颜色 带修改的莫队算法
[题意]给定n个数字,m次操作,每次询问区间不同数字的个数,或修改某个位置的数字.n,m<=10^4,ai<=10^6. [算法]带修改的莫队算法 [题解]对于询问(x,y,t),其中t是 ...
- 【bzoj4129】Haruna’s Breakfast 带修改树上莫队+分块
题目描述 给出一棵树,点有点权.支持两种操作:修改一个点的点权,查询链上mex. 输入 第一行包括两个整数n,m,代表树上的结点数(标号为1~n)和操作数.第二行包括n个整数a1...an,代表每个结 ...
- 【bzoj3052】[wc2013]糖果公园 带修改树上莫队
题目描述 给出一棵n个点的树,每个点有一个点权,点权范围为1~m.支持两种操作:(1)修改一个点的点权 (2)对于一条路径,求$\sum\limits_{i=1}^m\sum\limits_{j=1} ...
- P1903 [国家集训队]数颜色 / 维护队列 带修改的莫队
\(\color{#0066ff}{ 题目描述 }\) 墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会向你发布如下指令: 1. Q L R代表询问你从第L支 ...
- UOJ 58 (树上带修改的莫队)
UOJ 58 糖果公园 Problem : 给一棵n个点的树,每个点上有一种颜色,对于一条路径上的点,若 i 颜色第 j 次出现对该路径权值的贡献为 w[i] * c[j], 每次询问一条路径的权值, ...
- UVA - 12345 带修改的莫队
题意显然:给出初始序列,单点修改,区间查询元素的种类. 由于时限过宽,暴力可过. 比较优秀的解法应该是莫队. 带修改的莫队题解可以看https://www.luogu.org/blog/user126 ...
- Machine Learning CodeForces - 940F(带修改的莫队)
题解原文地址:https://www.cnblogs.com/lujiaju6555/p/8468709.html 给数组a,有两种操作,1 l r查询[l,r]中每个数出现次数的mex,注意是出现次 ...
随机推荐
- 201621123079《Java程序设计》第1周学习总结
第1周-Java基本概念 1.本周学习总结 第一次上课接触java,了解了java的由来和历史,还有JCP,JSP的概念,并学会如何建立一个java文件和运行过程.感觉java比之前学习的数据结构更高 ...
- MySQL 快速入门教程
转:MySQL快速 入门教程 目录 一.MySQL的相关概念介绍 二.Windows下MySQL的配置 配置步骤 MySQL服务的启动.停止与卸载 三.MySQL脚本的基本组成 四.MySQL中的数据 ...
- 关于Python构建微服务的思考(一)
一:什么是微服务? 微服务是一种架构风格,一个大型复杂软件应用由一个或多个微服务组成. 系统中的各个微服务可被独立部署,各个微服务之间是松耦合的. 每个微服务仅关注于完成一件任务并很好地完成该任务. ...
- IDEA常用插件记录
让我们来记录一下常用的IDEA插件:(从其他博客中取了许多图片,出处见图片水印) 1.JRebel for IntelliJ 热部署神器2.Free MyBatis plugin 实现dao层方法与x ...
- 关于SELECT 逻辑的执行顺序问题
不会有大多数人都和我一样的认为,是先进行的Where 剔除结果集,再进行Join的吧 SQL server 2014 逻辑执行标准: https://msdn.microsoft.com/en-us/ ...
- win10 默认锁屏路径
C:\Users\YourUsername\AppData\Local\Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\ ...
- 開啟活動監視器 (SQL Server Management Studio)
本主題描述如何開啟 [活動監視器] 來取得有關 SQL Server 處理序以及這些處理序如何影響目前 SQL Server 執行個體的資訊. 此外,本主題也描述如何設定 [活動監視器] 的重新整理間 ...
- 玛丽卡(codevs 1021)
题目描述 Description 麦克找了个新女朋友,玛丽卡对他非常恼火并伺机报复. 因为她和他们不住在同一个城市,因此她开始准备她的长途旅行. 在这个国家中每两个城市之间最多只有一条路相通,并且我们 ...
- 【ZJOI2017 Round1练习】D8T1 mushroom(点分治)
题意: 思路: num[a[u]]表示存在a[u]这个颜色且终点在u子树中的链长总和 ans[i]表示以当前的u为根,前面的子树对i的贡献之和 ..]of longint; size,f,ans,su ...
- Java电商项目-5.内容管理cms系统
目录 实现加载内容分类树功能 实现内容分类动态添加 删除内容分类节点 实现内容分类节点的分页显示 实现广告内容的添加 实现广告内容删除 实现广告内容编辑 到Github获取源码请点击此处 实现加载内容 ...