题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1878

不带修改的莫队,用一个桶记录一下当前区间中每种颜色的数量就可以做到$O(1)$更新了。

 #include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int inline readint(){
int Num;char ch;
while((ch=getchar())<''||ch>'');Num=ch-'';
while((ch=getchar())>=''&&ch<='') Num=Num*+ch-'';
return Num;
}
void outint(int x){
if(x>=) outint(x/);
putchar(x%+'');
}
int N,M,blk;
int a[];
struct Query{
int l,r,p,id;
bool operator < (const Query &_)const{
return p!=_.p?p<_.p:r<_.r;
}
}q[];
int Ans[],Tans=;
int c[];
void Upd(int x,int d){
if(d==){
if(!c[a[x]]) Tans++;
c[a[x]]++;
}
else{
if(c[a[x]]==) Tans--;
c[a[x]]--;
}
}
int main(){
N=readint();
blk=ceil(sqrt(N));
for(int i=;i<=N;i++) a[i]=readint();
M=readint();
for(int i=;i<=M;i++){
q[i].l=readint();
q[i].r=readint();
q[i].p=(q[i].l-)/blk+;
q[i].id=i;
}
sort(q+,q++M);
int l=,r=;
for(int i=;i<=M;i++){
for(;r<q[i].r;r++) Upd(r+,);
for(;r>q[i].r;r--) Upd(r,-);
for(;l>q[i].l;l--) Upd(l-,);
for(;l<q[i].l;l++) Upd(l,-);
Ans[q[i].id]=Tans;
}
for(int i=;i<=M;i++){
outint(Ans[i]);
putchar('\n');
}
return ;
}

最新文章

  1. 4.Android 打包时出现的Android Export aborted because fatal error were founds [closed]
  2. 在公有云AZURE上部署私有云AZUREPACK以及WEBSITE CLOUD(五)
  3. PHP list,explode的使用
  4. Python自动化之线程进阶篇
  5. Python 学习笔记01
  6. luigi学习3-使用luigid
  7. mysql下的SELECT INTO语句
  8. varchar 分享影响记忆 试
  9. WCF/WPF公司内部订餐程序开发
  10. 做推送,怎么能不了解推送的 4 种消息形式呢?( Android 篇)
  11. 数据挖掘概念与技术15--为快速高维OLAP预计算壳片段
  12. 【设计模式】抽象工厂模式 Abstract Factory Pattern
  13. Myeclipse的使用技巧
  14. 白话skynet第二篇:skynet的通信调试pack和sprotol
  15. Jlink使用技巧之烧写SPI Flash存储芯片
  16. SQL DATE_FORMAT() 函数
  17. ADO.Net 数据库修改
  18. noip2017d1t1
  19. CountDownLatch使用场景及分析
  20. 在html的JavaScript部分计算,保留小数点后面的位数

热门文章

  1. MIUI应用权限设置
  2. canvas鼠标点击划线
  3. 使用ADO.NET对SQL Server数据库进行訪问
  4. 通过fsharp 使用Enterprise Library Unity 3 - 三种拦截模式的探索
  5. Android中onInterceptTouchEvent、dispatchTouchEvent及onTouchEvent的调用顺序及内部原理
  6. 四:网络--NSURLConnection基本使用
  7. 并不对劲的Loj6031:「雅礼集训 2017 Day1」字符串
  8. 377D
  9. .NET获取汉字首字母
  10. python之计数统计