[SPOJ3267]DQUERY-D-query——[莫队]

在这里插入图片描述
【题意分析】

裸莫队不解释,太模板了

直接上代码

Code:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <algorithm>
#define MAXN 1000100
using namespace std;

struct Node {
	int l, r, pos, id;
}ques[MAXN];

int a[MAXN], f[MAXN], ans[MAXN], n, q, block, cnt;

inline int read () {
	register int s = 0, w = 1;
	register char ch = getchar ();
	while (! isdigit (ch)) {if (ch == '-') w = -1; ch = getchar ();}
	while (isdigit (ch)) {s = (s << 3) + (s << 1) + (ch ^ 48); ch = getchar ();}
	return s * w;
}

inline bool cmp (Node a, Node b) {return (a.pos == b.pos) ? a.r < b.r : a.pos < b.pos;}
inline void add (int x) {f[a[x]]++; if (f[a[x]] == 1) cnt++;}
inline void del (int x) {f[a[x]]--; if (f[a[x]] == 0) cnt--;}

int main () {
	n = read (); block = floor (sqrt (n));
	for (register int i = 1; i <= n; i++) a[i] = read ();
	q = read ();
	for (register int i = 1; i <= q; i++) {
		ques[i].l = read (), ques[i].r = read ();
		ques[i].id = i, ques[i].pos = (ques[i].l - 1) / block + 1;
	}
	sort (ques + 1, ques + q + 1, cmp);
	int h = 1, t = 1; add (1);
	for (register int i = 1; i <= q; i++) {
		while (h < ques[i].l) del (h++);
		while (h > ques[i].l) add (--h);
		while (t < ques[i].r) add (++t);
		while (t > ques[i].r) del (t--);
		ans[ques[i].id] = cnt;
	}
	for (register int i = 1; i <= q; i++) printf ("%d\n", ans[i]);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值