F. Machine Learning 带修端点莫队
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
#include<cstdio>
#include<cstring>
#include<map>
#include<cmath>
#include<algorithm>
using namespace std;
const int N=2e5+;
map<int,int>M;
int vis[N],num[N],a[N],b[N],now[N],ans[N],unit,l,r,t;
struct Query{
int l,r,tim,id;
bool operator < (const Query &A)const{
return l/unit==A.l/unit?(r/unit==A.r/unit?tim<A.tim:r<A.r):l<A.l;
}
}Q[N];
struct Change{
int pos,x,y;
}C[N];
void modify(int x,int d){
--vis[num[x]];num[x]+=d;++vis[num[x]];
}
void going(int T,int d){
if(C[T].pos>=l&&C[T].pos<=r) modify(C[T].x,d),modify(C[T].y,-d);
if(d==) a[C[T].pos]=C[T].x;else a[C[T].pos]=C[T].y;
}
int calc(){
for(int i=;;++i) if(!vis[i]) return i;
}
int main(){
int n,q,tot,op,cc=,pp=;
scanf("%d%d",&n,&q);
for(int i=;i<=n;++i) scanf("%d",&a[i]),now[i]=b[i]=a[i];
tot=n,unit=(int)pow(n,0.6666666);
for(int i=;i<=q;++i) {
scanf("%d",&op);
if(op==){
++cc,scanf("%d%d",&C[cc].pos,&C[cc].x);
C[cc].y=now[C[cc].pos],b[++tot]=now[C[cc].pos]=C[cc].x;
}
else {
++pp,scanf("%d%d",&Q[pp].l,&Q[pp].r);
Q[pp].tim=cc,Q[pp].id=pp;
}
}
sort(b+,b+tot+);
tot=unique(b+,b+tot+)-b-;
for(int i=;i<=tot;++i) M[b[i]]=i;
for(int i=;i<=n;++i) a[i]=M[a[i]];
for(int i=;i<=cc;++i) C[i].x=M[C[i].x],C[i].y=M[C[i].y];
sort(Q+,Q+pp+);
for(int i=;i<=pp;++i) {
while(t<Q[i].tim) going(t+,),++t;
while(t>Q[i].tim) going(t,-),--t; while(l<Q[i].l) modify(a[l],-),++l;
while(l>Q[i].l) modify(a[l-],),--l;
while(r<Q[i].r) modify(a[r+],),++r;
while(r>Q[i].r) modify(a[r],-),--r;
ans[Q[i].id]=calc(); }
for(int i=;i<=pp;++i) printf("%d\n",ans[i]);
}
2
3
2
F. Machine Learning 带修端点莫队的更多相关文章
- 学习笔记——不带修序列莫队 (luogu2079)小B的询问
莫队是一种对于询问的离线算法 时间复杂度:O(\(n \sqrt n\)) 大致思想就是 首先将询问离线,然后对原序列分块,使得每一个\(l和r\)都在一个块里 然后按照左节点排序,若所在的块相等,就 ...
- codeforces 940F 带修改的莫队
F. Machine Learning time limit per test 4 seconds memory limit per test 512 megabytes input standard ...
- 【bzoj4129】Haruna’s Breakfast 带修改树上莫队+分块
题目描述 给出一棵树,点有点权.支持两种操作:修改一个点的点权,查询链上mex. 输入 第一行包括两个整数n,m,代表树上的结点数(标号为1~n)和操作数.第二行包括n个整数a1...an,代表每个结 ...
- P1903 [国家集训队]数颜色 / 维护队列 带修改的莫队
\(\color{#0066ff}{ 题目描述 }\) 墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会向你发布如下指令: 1. Q L R代表询问你从第L支 ...
- UOJ 58 (树上带修改的莫队)
UOJ 58 糖果公园 Problem : 给一棵n个点的树,每个点上有一种颜色,对于一条路径上的点,若 i 颜色第 j 次出现对该路径权值的贡献为 w[i] * c[j], 每次询问一条路径的权值, ...
- 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是 ...
- 【bzoj3052】[wc2013]糖果公园 带修改树上莫队
题目描述 给出一棵n个点的树,每个点有一个点权,点权范围为1~m.支持两种操作:(1)修改一个点的点权 (2)对于一条路径,求$\sum\limits_{i=1}^m\sum\limits_{j=1} ...
- UVA - 12345 带修改的莫队
题意显然:给出初始序列,单点修改,区间查询元素的种类. 由于时限过宽,暴力可过. 比较优秀的解法应该是莫队. 带修改的莫队题解可以看https://www.luogu.org/blog/user126 ...
随机推荐
- Solidity的Bytecode和Opcode简介
Solidity的Bytecode和Opcode简介 随着我们更深入地编写智能合约,我们将遇到诸如" PUSH1"," SSTORE"," CALLV ...
- poj_1323 Game Prediction 贪心
Game Prediction Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11814 Accepted: 5701 ...
- bdc抢夺域控
1.运行CMD2.在 ntdsutil :提示符下输入 ntdsutil3.在 ntdsutil :提示符下输入 roles4.在 fsmo maintenance:提示符下输入 connection ...
- Python爬虫,抓取淘宝商品评论内容!
作为一个资深吃货,网购各种零食是很频繁的,但是能否在浩瀚的商品库中找到合适的东西,就只能参考评论了!今天给大家分享用python做个抓取淘宝商品评论的小爬虫! 思路 我们就拿"德州扒鸡&qu ...
- C++类学习(2)
Ⅰ:类概念 一:类的构成 class 类名 { public: 公有数据成员和成员函数:类的接口 protected: 保护数据成员和成员函数: private: 私有数据成员和成员函数: }://注 ...
- ACM-ICPC 2019 山东省省赛总结
五题手快拿银,不然拿铜,甚至不拿,从结果上来看拿了铜牌对第一年的我们来说算好的,也不算太好. 从拿奖后的第一天,我想写这篇博客,但是我忍了下来,那时候被喜悦冲昏了头脑,当 冷静下来,我开始打算写这篇博 ...
- 题目分享Q
题意:给出一个N个点的树,找出一个点来,以这个点为根的树时,所有点的深度之和最大 分析:这可以说是换根法的裸题吧 首先考虑对一个给定的根如何计算,这应该是最简单的那种树形dp吧甚至可能都不算dp(好像 ...
- vue项目兼容ie
一.兼容ES6 Vue 的核心框架 vuejs 本身,以及官方核心插件(VueRouter.Vuex等)均可以在 ie9 上正常使用.但ie不兼容es6,所以需要安装插件将“Promise”等高级语法 ...
- 王颖奇 20171010129《面向对象程序设计(java)》第十二周学习总结
实验十二 图形程序设计 理论: 10.1 AWT与Swing简介 10.2 框架的创建10.3 图形程序设计10.4 显示图像 (具体学习总结在最后) 实验: 实验时间 2018-11-14 1.实 ...
- LeetCode--LinkedList--160. Intersection of Two Linked Lists(Easy)
160. Intersection of Two Linked Lists(Easy) 题目地址https://leetcode.com/problems/intersection-of-two-li ...