BZOJ:1878: [SDOI2009]HH的项链
2024-10-08 14:41:49
题解:解法一:莫队
解法二:按区间左端点排序,让区间内最左边的贝壳对答案产生贡献,树状数组维护,转移对答案产生贡献的贝壳位置
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm> using namespace std;
const int maxn=1000009; int n,m;
int a[maxn];
int nex[maxn];
int ans[maxn]; int tong[maxn]; inline int lowbit(int x){
return x&(-x);
}
int c[maxn];
int add(int x,int val){
while(x<=1000001){
c[x]+=val;
x+=lowbit(x);
}
}
int query(int x){
int ret=0;
while(x){
ret+=c[x];
x-=lowbit(x);
}
return ret;
} struct Section{
int l,r,id;
}sec[maxn];
int cmp(const Section &t1,const Section &t2){
if(t1.l==t2.l)return t1.r<t2.r;
else return t1.l<t2.l;
} int main(){
scanf("%d",&n);
for(int i=1;i<=n;++i){
scanf("%d",&a[i]);
if(a[i]==0)a[i]=1000001;
}
for(int i=n;i>=1;--i){
if(!tong[a[i]]){
nex[i]=n+1;
}else{
nex[i]=tong[a[i]];
}
tong[a[i]]=i;
}
scanf("%d",&m);
for(int i=1;i<=m;++i){
scanf("%d%d",&sec[i].l,&sec[i].r);
sec[i].id=i;
}
sort(sec+1,sec+1+m,cmp); memset(tong,0,sizeof(tong));
for(int i=1;i<=n;++i){
if(!tong[a[i]])add(i,1);
tong[a[i]]=1;
} int head=1;
for(int i=1;i<=m;++i){
while(head<sec[i].l){
add(head,-1);
add(nex[head],1);
++head;
}
ans[sec[i].id]=query(sec[i].r)-query(sec[i].l-1);
}
for(int i=1;i<=m;++i)printf("%d\n",ans[i]);
return 0;
}
最新文章
- 随机函数的代码(srand、rand)
- The conversion of a varchar data type to a datetime data type resulted in an out-of-range value
- []with[[]]
- 阿里云oss上传图片
- 唉,还是Windows好
- outlook圆角table
- Python的类变量和对象变量声明解析
- 关于使用视图进行分页时出现当前记录集不支持书签的错误解决方法及原因(asp)
- Java和.NET的GZIP压缩功能对比
- linux命令之ls命令的简明讲解
- redis安装方法
- 基于FPGA的key button等开关消抖,按键消抖电路设计
- [译]Stairway to Integration Services Level 8 - SSIS 工作流管理高级
- .NET C#到Java没那么难,DB篇
- Docker最全教程之使用 Visual Studio Code玩转Docker(二十)
- css3实现旋转表
- Vue列表组件与弹窗组件示例
- Getting started - RN1
- springmvc+ajax文件上传
- 【Luogu1937】仓配置(贪心,线段树)