离散化加带修改的莫队
转了两篇代码,区别。。。。对于现在的我来说,应该是离散化方式不同......应该....第一个用STL,边读入,边离散化,第二个先读入,在用unique和lower_bound,离散化.............
转自https://blog.youkuaiyun.com/dsaghjkye/article/details/82285145
#include <bits/stdc++.h>
const int maxn=2e5+10;
using namespace std;
int a[maxn],now[maxn],be[maxn],num[maxn],cnt[maxn],ans[maxn];//a 代表输入数据对应离散化后数字
//now 修改后代表的数字,每当修改时 会把now传入记录之前值和现在值
//be 分块值
//num 数字的个数 cnt 数字个数的 个数 ans 保存结果
int n,m,l,r,t;
int msz;
map<int,int>M;//离散化 记录对应值
struct Query
{
int l,r,t,id;
}q[maxn];
int cmp(Query n1,Query n2)//排序
{
if(be[n1.l]==be[n2.l])
{
if(be[n1.r]==be[n2.r])
return n1.t<n2.t;
return n1.r<n2.r;
}
return n1.l<n2.l;
}
struct nn
{
int pos,neww,oldd;//位置 ,现在的新值 ,之前的值
}c[maxn];
int compress(int x)
{
if(!M.count(x)) M[x]=++msz; //如果未出现过 ,赋予新值
return M[x];
}
void add(int val,int d)
{
if(num[val]>0) cnt[num[val]]--;
num[val]+=d;
if(num[val]>0) cnt[num[val]]++;
}
void go(int idx,int val)
{
if(l<=idx&&idx<=r) //要修改的值在当前l r 中 那么得先去掉之前的值在添新值,不在则无影响
{
add(a[idx],-1);
add(val,1);
}
a[idx]=val; //修改
}
int get() { //得到最小整数
for(int i = 1; ;++i) {
if(cnt[i] == 0) return i;
}
}
int main()
{
msz=0;
int x,qsz=0,csz=0,y,op;
scanf("%d%d", &n, &m);
int tt=pow(n, 2.0/3.0);//
for(int i=1; i<=n; i++)
{
scanf("%d", &x);
now[i]=a[i] =compress(x);//获得对应值
be[i]=(i-1)/tt+1;
}
for(int i=0;i<m;i++)
{
scanf("%d%d%d", &op, &x, &y);
if(op==1) q[++qsz]=(Query){x,y,csz,qsz};
else
{
y = compress(y);
c[++csz] = (nn){x,y,now[x]};
now[x] = y;//之前的值修改
}
}
sort(q+1,q+qsz+1,cmp);
l=1,r=0,t=0;
for(int i=1;i<=qsz;i++)
{
while(t<q[i].t) go(c[t+1].pos,c[t+1].neww),++t;
while(t>q[i].t) go(c[t].pos,c[t].oldd),--t;
while(l<q[i].l) add(a[l++],-1);
while(l>q[i].l) add(a[--l],1);
while(r<q[i].r) add(a[++r],1);
while(r>q[i].r) add(a[r--],-1);
ans[q[i].id]=get();
}
for(int i=1;i<=qsz;i++)
printf("%d\n",ans[i]);
}
转自:https://blog.youkuaiyun.com/blue_kid/article/details/79475167
#include<bits/stdc++.h>
using namespace std;
int block_len;
const int maxn=1e5+5;
struct node{
int l,r,bl,br,id,t;
node(){}
node(int l,int r,int id,int t):l(l),r(r),id(id),t(t){
bl=l/block_len;
br=r/block_len;
}
bool operator<(const node&x)const{
if(bl==x.bl){
if(br==x.br) return t<x.t;
else return br<x.br;
}
else return bl<x.bl;
}
}query[maxn];
struct node2{
int pos,Old,New;
}c[maxn];
int m,n,curT,Q,T,tot,l=1,r,mex;
int a[maxn],b[maxn<<1],now[maxn],cnt[maxn<<1],cnt2[maxn],ans[maxn];
inline void add(int val)
{
cnt2[cnt[val]]--;
cnt[val]++;
cnt2[cnt[val]]++;
}
inline void del(int val)
{
cnt2[cnt[val]]--;
cnt[val]--;
cnt2[cnt[val]]++;
}
inline void change(int pos,int x)
{
if(pos>=l&&pos<=r){
del(now[pos]);
add(x);
}
now[pos]=x;
}
int main()
{
scanf("%d%d", &n, &m);
block_len=pow(n, 0.666667);
for(int i=1;i<=n;i++){
scanf("%d", &a[i]);
now[i] = a[i];
b[tot++] = a[i];
}
for(int i=1;i<=m;i++){
int op;
scanf("%d",&op);
if(op==1){
int l, r;
scanf("%d%d", &l, &r);
query[++Q]=node(l, r , Q, T);
}
else{
int p, x;
scanf("%d%d", &p, &x);
b[tot++]=x;
c[++T] = {p,now[p],x};
now[p]=x;
}
}
sort(query+1,query+Q+1);
sort(b,b+tot);
tot=unique(b,b+tot)-b;
for(int i=1;i<=n;i++){
now[i]=lower_bound(b,b+tot,a[i])-b+1;
}
for(int i=1;i<=T;i++){
c[i].Old=lower_bound(b,b+tot,c[i].Old)-b+1;
c[i].New=lower_bound(b,b+tot,c[i].New)-b+1;
}
for(int i=1;i<=Q;i++){
node &qr=query[i];
while(r<qr.r) r++,add(now[r]);
while(l>qr.l) l--,add(now[l]);
while(r>qr.r) del(now[r]),r--;
while(l<qr.l) del(now[l]),l++;
while(curT<qr.t) curT++,change(c[curT].pos,c[curT].New);
while(curT>qr.t) change(c[curT].pos,c[curT].Old),curT--;
mex=1;
while(cnt2[mex]>0) mex++;
ans[qr.id]=mex;
}
for(int i=1;i<=Q;i++) printf("%d\n",ans[i]);
//system("pause");
return 0;
}