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