题目链接
思路:带修改莫队在普通莫队的基础上增加了一个时间指针,对时间指针的操作和对 L , R 指针操作几乎相同,排序函数稍有变化。对于时间指针,新增一个 update 函数,具体实现见代码
Code:
#include<bits/stdc++.h>
#define debug(x) cout << "[" << #x <<": " << (x) <<"]"<< endl
#define pii pair<int,int>
#define clr(a,b) memset((a),b,sizeof(a))
#define rep(i,a,b) for(int i = a;i < b;i ++)
#define pb push_back
#define MP make_pair
#define LL long long
#define ull unsigned LL
#define ls i << 1
#define rs (i << 1) + 1
#define fi first
#define se second
#define CLR(a) while(!(a).empty()) a.pop()
using namespace std;
const int maxn = 1e4 + 10;
int ans[maxn],cnt[maxn * 100],Ans = 0;
int a[maxn],belong[maxn];
struct xx{
int l,r,id,time;
}Q[maxn];
struct change{
int pos,val;
}cge[maxn];
int n,m;
inline int read() {
int X = 0, w = 0;
char ch = 0;
while(!isdigit(ch)) {
w |= ch == '-';
ch = getchar();
}
while(isdigit(ch))
X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
return w ? -X : X;
}
bool cmp(xx A,xx B){
if(belong[A.l] != belong[B.l])
return belong[A.l] < belong[B.l];
if(belong[A.r] != belong[B.r])
return belong[A.r] < belong[B.r];
return A.time < B.time;
}
void add(int x){
if(!cnt[a[x]])
++ Ans;
++ cnt[a[x]];
}
void del(int x){
if(cnt[a[x]] == 1)
-- Ans;
-- cnt[a[x]];
}
void update(int i,int _time){
if(cge[_time].pos >= Q[i].l && cge[_time].pos <= Q[i].r){
del(cge[_time].pos);
}
swap(cge[_time].val,a[cge[_time].pos]);
if(cge[_time].pos >= Q[i].l && cge[_time].pos <= Q[i].r){
add(cge[_time].pos);
}
}
int main() {
//#ifndef ONLINE_JUDGE
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
//#endif
int n = read(),m = read();
int block = pow(n,2.0 / 3);
for(int i = 1;i <= n;++ i){
a[i] = read();
belong[i] = (i - 1) / block + 1;
}
int tmp = 0,pq = 0;
for(int i = 1;i <= m;++ i){
char op; scanf(" %c",&op);
if(op == 'Q'){
Q[++ pq].l = read(); Q[pq].r = read();
Q[pq].id = pq; Q[pq].time = tmp;
}
else {
cge[++ tmp].pos = read();
cge[tmp].val = read();
}
}
sort(Q + 1,Q + 1 + pq,cmp);
int L = 1,R = 0,_time = 0;
for(int i = 1;i <= pq;++ i){
while(L < Q[i].l) del(L ++);
while(L > Q[i].l) add(-- L);
while(R < Q[i].r) add(++ R);
while(R > Q[i].r) del(R --);
while(_time > Q[i].time) update(i,_time --);
while(_time < Q[i].time) update(i,++ _time);
ans[Q[i].id] = Ans;
}
for(int i = 1;i <= pq;++ i)
printf("%d\n",ans[i]);
return 0;
}