题目大意:给你一串数字,多次询问区间内数字的种类数

题解:莫队

卡点:洛谷数据加强,开了个$O(2)$

C++ Code:

#include <cstdio>
#include <algorithm>
#define bsz 710
#define maxn 500010
#define N 1000010
int ans[maxn];
int n, m;
int s[maxn], cnt[N];
struct Query {
int l, r, pos;
inline bool operator < (const Query &rhs) const {return l / bsz == rhs.l / bsz ? (l / bsz & 1 ? r < rhs.r : r > rhs.r) : l < rhs.l;}
} q[maxn];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", s + i);
scanf("%d", &m);
for (int i = 1; i <= m; i++) {
scanf("%d%d", &q[i].l, &q[i].r);
q[i].pos = i;
}
std::sort(q + 1, q + m + 1);
int l = 1, r = 1, res = 1; cnt[s[1]]++;
for (int i = 1; i <= m; i++) {
while (l > q[i].l) cnt[s[--l]]++ ? 0 : res++;
while (r < q[i].r) cnt[s[++r]]++ ? 0 : res++;
while (l < q[i].l) --cnt[s[l++]] ? 0 : res--;
while (r > q[i].r) --cnt[s[r--]] ? 0 : res--;
ans[q[i].pos] = res;
}
for (int i = 1; i <= m; i++) printf("%d\n", ans[i]);
return 0;
}

最新文章

  1. jquery 如何去除select 控件重复的option
  2. 硬件抽象层:HAL
  3. 书单.md
  4. 【知识积累】爬虫之网页乱码解决方法(gb2312 -&gt; utf-8)
  5. WinPhone学习笔记(二)——页面外观剖析
  6. Android手势锁实现
  7. SnapKit代码约束
  8. linux命令篇
  9. PHP面向对象基础知识总结
  10. Webstorm10.0.4注册码
  11. date日期比较和格式化方法
  12. [BZOJ1050] [HAOI2006] 旅行comf (Kruskal, LCT)
  13. 如何开发使用自定义文件的OEM应用程序
  14. vue老司机
  15. spring中整合memcached,以及创建memcache的put和get方法
  16. Kotlin入门(16)容器的遍历方式
  17. Flexbox 布局的最简单表单 (转)
  18. LTP(LinuxTest Project)测试工具
  19. VmWare15 许可证
  20. 2019.01.23 hdu3377 Plan(轮廓线dp)

热门文章

  1. Vue 前端md5加密
  2. cordova创建工程添加插件
  3. SQLSERVER存储过程基本语法使用
  4. 好用的 Html、CSS、JavaScript 开源项目
  5. Django项目发布到Apache2.4配置mod_wsgi,解决遭遇的各种坑。
  6. 裸机——SD卡
  7. idea 项目jar包出错
  8. Python logging 模块简介
  9. Android 自定义WebView 实现可以加载缓存数据
  10. 利用split方法计算字符串中出现字母最多的次数