[洛谷P1972][SDOI2009]HH的项链
2024-10-19 16:01:51
题目大意:给你一串数字,多次询问区间内数字的种类数
题解:莫队
卡点:洛谷数据加强,开了个$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;
}
最新文章
- jquery 如何去除select 控件重复的option
- 硬件抽象层:HAL
- 书单.md
- 【知识积累】爬虫之网页乱码解决方法(gb2312 ->; utf-8)
- WinPhone学习笔记(二)——页面外观剖析
- Android手势锁实现
- SnapKit代码约束
- linux命令篇
- PHP面向对象基础知识总结
- Webstorm10.0.4注册码
- date日期比较和格式化方法
- [BZOJ1050] [HAOI2006] 旅行comf (Kruskal, LCT)
- 如何开发使用自定义文件的OEM应用程序
- vue老司机
- spring中整合memcached,以及创建memcache的put和get方法
- Kotlin入门(16)容器的遍历方式
- Flexbox 布局的最简单表单 (转)
- LTP(LinuxTest Project)测试工具
- VmWare15 许可证
- 2019.01.23 hdu3377 Plan(轮廓线dp)