【BZOJ 2120】 数颜色
Description
墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问。墨墨会像你发布如下指令: 1、 Q L R代表询问你从第L支画笔到第R支画笔中共有几种不同颜色的画笔。 2、 R P Col 把第P支画笔替换为颜色Col。为了满足墨墨的要求,你知道你需要干什么了吗?
Input
第1行两个整数N,M,分别代表初始画笔的数量以及墨墨会做的事情的个数。第2行N个整数,分别代表初始画笔排中第i支画笔的颜色。第3行到第2+M行,每行分别代表墨墨会做的一件事情,格式见题干部分。
Output
对于每一个Query的询问,你需要在对应的行中给出一个数字,代表第L支画笔到第R支画笔中共有几种不同颜色的画笔。
Sample Input
1 2 3 4 5 5
Q 1 4
Q 2 6
R 1 2
Q 1 4
Q 2 6
Sample Output
4
3
4
HINT
对于100%的数据,N≤10000,M≤10000,修改操作不多于1000次,所有的输入数据中出现的所有整数均大于等于1且不超过10^6。
#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=;
int bel[N],last[],size[N],pre[N],q[N],a[N],c[N];
int num,n,m,l,r,p,col,ans; void build(){
int block=sqrt(n);
for(int i=;i<=n;i++){
bel[i]=(i-)/block+;
if(bel[i]>num) num=bel[i];
size[bel[i]]++;
pre[i]=last[a[i]];
c[i]=pre[i];
last[a[i]]=i;
}
} void reset(int x){
for(int i=q[x-]+;i<=q[x];i++) pre[i]=c[i];
sort(pre+q[x-]+,pre+q[x]+);
} int find(int x,int y){
int tmp=lower_bound(pre+q[x-]+,pre+q[x]+,y)-pre-q[x-]-;
return tmp;
} int ask(int l,int r){
if(bel[l]==bel[r]){
for(int i=l;i<=r;i++) if(c[i]<l) ans++;
}
else{
for(int i=l;i<=q[bel[l]];i++) if(c[i]<l) ans++;
for(int i=q[bel[r]-]+;i<=r;i++)
if(c[i]<l) ans++;
}
for(int i=bel[l]+;i<bel[r];i++) ans+=find(i,l);
return ans;
} void change(int x,int y){
for(int i=;i<=n;i++) last[a[i]]=;
a[x]=y;
for(int i=;i<=n;i++){
int t=c[i];
c[i]=last[a[i]];
if(t!=c[i]) reset(bel[i]);
last[a[i]]=i;
}
} int main(){
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) scanf("%d",&a[i]);
build();
for(int i=;i<=num;i++) q[i]=q[i-]+size[i];
for(int i=;i<=num;i++)
reset(i);
char s[];
for(int i=;i<=m;i++){
scanf("%s",s);
if(s[]=='Q'){
ans=;
scanf("%d%d",&l,&r);
printf("%d\n",ask(l,r));
}
else{
scanf("%d%d",&p,&col);
change(p,col);
}
}
}
【BZOJ 2120】 数颜色的更多相关文章
- BZOJ 2120 数颜色(带修改的莫队)
2120: 数颜色 Time Limit: 6 Sec Memory Limit: 259 MB Submit: 3478 Solved: 1342 [Submit][Status][Discus ...
- BZOJ 2120: 数颜色
2120: 数颜色 Time Limit: 6 Sec Memory Limit: 259 MBSubmit: 3623 Solved: 1396[Submit][Status][Discuss] ...
- BZOJ 2120: 数颜色 分块
2120: 数颜色 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/problem.php? ...
- Bzoj 2120: 数颜色 && 2453: 维护队列 莫队,分块,bitset
2120: 数颜色 Time Limit: 6 Sec Memory Limit: 259 MBSubmit: 2645 Solved: 1039[Submit][Status][Discuss] ...
- BZOJ 2120 数颜色 (带修莫队)
2120: 数颜色 Time Limit: 6 Sec Memory Limit: 259 MBSubmit: 6367 Solved: 2537[Submit][Status][Discuss] ...
- BZOJ 2120 数颜色 【带修改莫队】
任意门:https://www.lydsy.com/JudgeOnline/problem.php?id=2120 2120: 数颜色 Time Limit: 6 Sec Memory Limit: ...
- bzoj 2120 数颜色 题解
转载请注明:http://blog.csdn.net/jiangshibiao/article/details/23990489 [原题] 2120: 数颜色 Time Limit: 6 Sec M ...
- bzoj 2120 数颜色 (带修莫队)
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=2120 题意:两种操作:Q 询问区间 l - r 内颜色的种类 ,R 单点修改 思路 ...
- BZOJ 2120 数颜色(带修改莫队)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2120 [题目大意] 给出一颜色序列,每次可以修改一个位置的颜色或者询问一个区间不同颜色 ...
- BZOJ 2120 数颜色&2453 维护队列 [带修改的莫队算法]【学习笔记】
2120: 数颜色 Time Limit: 6 Sec Memory Limit: 259 MBSubmit: 3665 Solved: 1422[Submit][Status][Discuss] ...
随机推荐
- Handler 取不到session 解决办法
Handler需要继承 Handler : IHttpHandler, IReadOnlySessionState, IRequiresSessionState
- 【linux】 静态库编译
文件如下: root@ubuntu:/home/test# ll total drwxr-xr-x root root Sep : ./ drwxr-xr-x root root Sep : ../ ...
- static local variable
Putting the keyword static in front of a local variable declaration creates a special type of variab ...
- Copying Fields to a new Record
This is a time saving tip for application designer. If you are creating a new record definition and ...
- SQL SERVER连接字符串学习
在使用connection string时遇到一些问题 字符串如下"Data Source= ******;Initial Catalog=******;Persist Security I ...
- java基础知识梳理
java基础知识梳理 1 基本数据类型
- centos6.7下网络设置
vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE="eth0"BOOTPROTO="static" # ...
- Thinkphp 下面执行crond
thinkphp开启cli支持 1.tp正好支持cli命令模式,手册的路径为13.7.4 如果是用的其他框架不支持cli,那么只能直接写程序了,其实就是写面向过程的最基础的php代码. 2.在入口文 ...
- 禁止 PC端打开网页 进行跳转
try {var urlhash = window.location.hash;if (!urlhash.match("fromapp")){if ((navigator.user ...
- android sdk国内目录http://mirrors.neusoft.edu.cn/android/repository/
http://mirrors.neusoft.edu.cn/android/repository/