BZOJ 2120: 数颜色

2120: 数颜色

Time Limit: 6 Sec  Memory Limit: 259 MB
Submit: 8079  Solved: 3321
[Submit][Status][Discuss]

Description

墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问。墨墨会像你发布如下指令: 1、 Q L R代表询问你从第L支画笔到第R支画笔中共有几种不同颜色的画笔。 2、 R P Col 把第P支画笔替换为颜色Col。为了满足墨墨的要求,你知道你需要干什么了吗?

Input

第1行两个整数N,M,分别代表初始画笔的数量以及墨墨会做的事情的个数。第2行N个整数,分别代表初始画笔排中第i支画笔的颜色。第3行到第2+M行,每行分别代表墨墨会做的一件事情,格式见题干部分。

Output

对于每一个Query的询问,你需要在对应的行中给出一个数字,代表第L支画笔到第R支画笔中共有几种不同颜色的画笔。

Sample Input

6 5
1 2 3 4 5 5
Q 1 4
Q 2 6
R 1 2
Q 1 4
Q 2 6

Sample Output

4
4
3
4

HINT

 

对于100%的数据,N≤10000,M≤10000,修改操作不多于1000次,所有的输入数据中出现的所有整数均大于等于1且不超过10^6。

总结:待修改莫队板子

#include <bits/stdc++.h>

using namespace std;
const int N = 1e5 + 7;
typedef long long ll;

struct Node{int l, r, tim, ID;} q[N];
struct DATA{int pos, Nw, Od;} c[N];
int t1, t2, sum[N * 10], blo, bl[N];
int n, m, l = 1, r, T, Ans, ans[N], s[N], now[N];
bool cmp(Node a, Node b) {
	return bl[a.l] == bl[b.l] ?(bl[a.r] == bl[b.r] ?a.tim < b.tim :a.r < b.r) :a.l < b.l;
}

void revise(int x, int d) {
	sum[x] += d;
	(d > 0) ?Ans += sum[x] == 1 :Ans -= sum[x] == 0;
}
void change(int x, int d) {
	if(l <= x && x <= r) {
		revise(d, 1); revise(s[x], -1);
	} s[x] = d;
}
int main() {
	char ch[2]; 
	scanf("%d%d", &n, &m); blo = sqrt(n);
	for (int i = 1; i <= n; ++i) scanf("%d", &s[i]), now[i] = s[i], bl[i] = i / blo  + 1;
	for (int i = 1; i <= m; ++i) {
		int x, y; scanf("%s%d%d", ch, &x, &y);
		if(ch[0] == 'Q') q[++t1] = (Node) {x, y, t2, t1};
		else c[++t2] = (DATA) {x, y, now[x]}, now[x] = y;
	}
	sort(q + 1, q + t1 + 1, cmp);
	for (int i = 1; i <= t1; ++i) {
		while(T < q[i].tim) change(c[T + 1].pos, c[T + 1].Nw), ++T;
		while(T > q[i].tim) change(c[T].pos, c[T].Od), --T;
		while(l < q[i].l) revise(s[l++], -1);
		while(l > q[i].l) revise(s[--l], 1);
		while(r < q[i].r) revise(s[++r], 1);
		while(r > q[i].r) revise(s[r--], -1);
		ans[q[i].ID] = Ans;
	}
	for (int i = 1; i <= t1; ++i) printf("%d\n", ans[i]);
	return 0;
}

  

猜你喜欢

转载自www.cnblogs.com/oi-forever/p/9125567.html
今日推荐