首页 技术 正文
技术 2022年11月11日
0 收藏 539 点赞 2,950 浏览 1520 个字

https://www.lydsy.com/JudgeOnline/problem.php?id=2120

标题里是两种不同的解法。

带修改的莫队和普通莫队比多了个修改操作,影响不大,但是注意一下细节不要出现zz错误。

这道题修改的数量比较少可以写莫队,但是如果修改数量多或者是特别极限的数据大概是不行的吧。

 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
const int maxn=;
using namespace std;
int n,m;
char ch[]={};
int clo[maxn]={},las[maxn]={};
struct node{
int pos,clo,pre;
}t[maxn];int tr=;
struct nod{
int l,r,id,pre;
}q[maxn];int tq=;
int b[maxn]={},ans[maxn]={},ans1=;
int vis[maxn*]={};
bool mcmp(nod aa,nod bb){
return (b[aa.l]==b[bb.l])?((aa.r==bb.r)?aa.id<bb.id:aa.r<bb.r):(b[aa.l]<b[bb.l]);
}
void doit(int x,int v){
if(v){
if(!vis[clo[x]])ans1++;
vis[clo[x]]++;
}
else{
if(vis[clo[x]]==)ans1--;
vis[clo[x]]--;
}
}
void change(int x,int y,int v){
//cout<<x<<y<<clo[x]<<v<<endl;
if(v){
doit(x,);
clo[x]=y;
doit(x,);
}clo[x]=y;
}
void work(){
int l=,r=,now=;
for(int i=;i<=tq;i++){
while(now<q[i].pre){now++;change(t[now].pos,t[now].clo,l<=t[now].pos&&t[now].pos<=r);}
while(now>q[i].pre){change(t[now].pos,t[now].pre,l<=t[now].pos&&t[now].pos<=r);now--;}
while(r<q[i].r)doit(++r,);
while(l>q[i].l)doit(--l,);
while(r>q[i].r){doit(r,);--r;}
while(l<q[i].l){doit(l,);++l;}
ans[q[i].id]=ans1;
}
for(int i=;i<=tq;i++){
printf("%d\n",ans[i]);
}
}
int main(){
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){scanf("%d",&clo[i]);las[i]=clo[i];}
int x,y;
for(int i=;i<=m;i++){
scanf("%s",ch);scanf("%d%d",&x,&y);
if(ch[]=='Q'){ q[++tq].l=x; q[tq].r=y; q[tq].id=tq; q[tq].pre=tr; }
else{ t[++tr].pos=x; t[tr].clo=y;t[tr].pre=las[x];las[x]=y;}
}int siz=(int)sqrt((double)n);
for(int i=;i<=n;i++)b[i]=(i-)/siz+;
sort(q+,q++tq,mcmp);
work();
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,490
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,291