【题目链接】 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;
}

最新文章

  1. JavaWeb三大组件——过滤器的运行机制理解
  2. java代码
  3. Foreach 与 Foreach-Object 的区别
  4. 样条曲线的Fortran程序
  5. 支付宝&amp;腾讯的OpenID之路
  6. 学会使用git
  7. poj 1849 Two
  8. 理解 B*tree index内部结构
  9. 【故障】MySQL主从同步故障-Slave_SQL_Running: No
  10. mysql学习笔记02 CRUD操作
  11. Mac实用技巧之:访达/Finder
  12. Python3学习笔记(urllib模块的使用)
  13. form表单提交到Servlet后,弹出对话框,然后在跳转页面
  14. Mac 10.12下安装python3环境
  15. 自制操作系统Antz(4)——进入保护模式 (下) 实现内核并从硬盘载入
  16. 【转载】SeleniumIDE入门
  17. 详解iOS多图下载的缓存机制
  18. MySQL中NULL与空字符串
  19. 美团点评2017校招笔试真题-算法工程师B
  20. java核心技术-多线程之线程基础

热门文章

  1. Idea 怎么远程debug
  2. Spring 对象的声明与注入
  3. React module methods with passing props to child or invoking callback to parent.
  4. 【洛谷 P4289】[HAOI2008]移动玩具(搜索)
  5. [bzoj4602][Sdoi2016]齿轮——dfs
  6. bzoj 2749 杂题
  7. bzoj 2330 SCOI2011糖果 查分约束系统
  8. Git常用命令总结【转】
  9. Launcher3自定义壁纸旋转后拉伸无法恢复
  10. python 多进程锁Lock和共享内存