2120: 数颜色
Time Limit: 6 Sec Memory Limit: 259 MBSubmit: 6367 Solved: 2537
[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
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
4
3
4
HINT
对于100%的数据,N≤10000,M≤10000,修改操作不多于1000次,所有的输入数据中出现的所有整数均大于等于1且不超过10^6。
2016.3.2新加数据两组by Nano_Ape
Source
析:带修莫队,先离线所有的询问,然后根据三个不同的值进行分块,l,r,time,分别是左端点的所有的块的位置,右端点所在块的位置,最后是时间,也就是修改的时间,然后先固定时间,再求 l 和 r,就简单了。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(i,x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-4;
const int maxn = 1e4 + 10;
const int maxm = 1e6 + 10;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, -1, 0, 1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
}
const int SIZE = 100;
int cnt[maxm], a[maxn];
int pos[maxn];
struct Query{
int id, l, r, tim;
bool operator < (const Query &q) const{
if(pos[l] != pos[q.l]) return pos[l] < pos[q.l];
if(pos[r] != pos[q.r]) return pos[r] < pos[q.r];
return tim < q.tim;
}
};
struct Update{
int p, now, last;
};
Query q[maxn];
Update p[maxn];
int t[maxn], ans[maxn];
int res, l = 1, r;
inline void del(int x){ res -= --cnt[x] == 0; }
inline void add(int x){ res += ++cnt[x] == 1; }
inline void update(int pos, int now){
if(l <= pos && pos <= r) del(a[pos]), add(now);
a[pos] = now;
}
int main(){
scanf("%d %d", &n, &m);
int b = 0, j = 0;
for(int i = 1; i <= n; ++i){
scanf("%d", a+i);
t[i] = a[i];
pos[i] = b;
if(++j == SIZE) ++b, j = 0;
}
int time = 0;
char op[5];
int qcnt = 0;
for(int i = 0; i < m; ++i){
int l, r; scanf("%s %d %d", op, &l, &r);
if(op[0] == 'Q') q[++qcnt] = (Query){qcnt, l, r, time};
else p[++time] = (Update){l, r, t[l]}, t[l] = r;
}
sort(q + 1, q + qcnt + 1);
int cur = 0;
for(int i = 1; i <= qcnt; ++i){
while(cur < q[i].tim) ++cur, update(p[cur].p, p[cur].now);
while(cur > q[i].tim) update(p[cur].p, p[cur].last), --cur;
while(l < q[i].l) del(a[l++]);
while(l > q[i].l) add(a[--l]);
while(r < q[i].r) add(a[++r]);
while(r > q[i].r) del(a[r--]);
ans[q[i].id] = res;
}
for(int i = 1; i <= qcnt; ++i) printf("%d
", ans[i]);
return 0;
}