BZOJ 1878 [SDOI2009]HH的项链(扫描线+树状数组)
2024-10-21 03:52:52
【题目链接】 http://www.lydsy.com/JudgeOnline/problem.php?id=1878
【题目大意】
给出一个数列,给出m个查询,每次查询一个区间中不相同的数字个数
【题解】
我们记录每一个位置上下一个相同相同元素的位置,当扫描线扫过当前点时
我们消除这个点的影响,并在其下个出现的位置进行更新,
这样就能求出固定左端点在不同右端点情况下不同区间内不同元素的数量
离线处理可得各区间解。
【代码】
#include <cstdio>
#include <algorithm>
using namespace std;
const int N=1000010;
int n,m,c[N],a[N],pre[N],nxt[N],ans[N];
struct data{int l,r,id;}p[N];
bool cmp(data a,data b){return a.l<b.l;}
void add(int x,int val){while(x<=n+1)c[x]+=val,x+=x&-x;}
int query(int x){int s=0;while(x)s+=c[x],x-=x&-x;return s;}
int main(){
while(~scanf("%d",&n)){
for(int i=1;i<=n;i++)scanf("%d",&a[i]),c[i]=0;
for(int i=1;i<N;i++)pre[i]=n+1;
for(int i=n;i;i--){
nxt[i]=pre[a[i]];
pre[a[i]]=i;
}
for(int i=1;i<N;i++)add(pre[i],1);
scanf("%d",&m);
for(int i=1;i<=m;i++)scanf("%d%d",&p[i].l,&p[i].r),p[i].id=i;
sort(p+1,p+m+1,cmp);
int t=1;
for(int i=1;i<=m;i++){
while(t<p[i].l){
add(t,-1);
add(nxt[t],1);
t++;
}ans[p[i].id]=query(p[i].r);
}
for(int i=1;i<=m;i++)printf("%d\n",ans[i]);
}return 0;
}
最新文章
- JavaWeb三大组件——过滤器的运行机制理解
- java代码
- Foreach 与 Foreach-Object 的区别
- 样条曲线的Fortran程序
- 支付宝&;腾讯的OpenID之路
- 学会使用git
- poj 1849 Two
- 理解 B*tree index内部结构
- 【故障】MySQL主从同步故障-Slave_SQL_Running: No
- mysql学习笔记02 CRUD操作
- Mac实用技巧之:访达/Finder
- Python3学习笔记(urllib模块的使用)
- form表单提交到Servlet后,弹出对话框,然后在跳转页面
- Mac 10.12下安装python3环境
- 自制操作系统Antz(4)——进入保护模式 (下) 实现内核并从硬盘载入
- 【转载】SeleniumIDE入门
- 详解iOS多图下载的缓存机制
- MySQL中NULL与空字符串
- 美团点评2017校招笔试真题-算法工程师B
- java核心技术-多线程之线程基础
热门文章
- Idea 怎么远程debug
- Spring 对象的声明与注入
- React module methods with passing props to child or invoking callback to parent.
- 【洛谷 P4289】[HAOI2008]移动玩具(搜索)
- [bzoj4602][Sdoi2016]齿轮——dfs
- bzoj 2749 杂题
- bzoj 2330 SCOI2011糖果 查分约束系统
- Git常用命令总结【转】
- Launcher3自定义壁纸旋转后拉伸无法恢复
- python 多进程锁Lock和共享内存