感觉没什么问题呀,但是为什么 TLE 好多,只得了64pts。请大家帮我康康
#include<bits/stdc++.h>
using namespace std;
inline int read()
{
int x=0,f=1;char ch=getchar();
while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
return x*f;
}
const int N = 2e6 + 100;
int n, m;
int a[N], wh[N];
struct Query { int x, y, pre, id; } Q[N];
struct Change { int pos, val; } C[N];
int Cnum, Qnum;
int col[N], ans, base, out[N];
inline bool cmp(const Query &a, const Query &b)
{
if (a.x != b.x) return wh[a.x] < wh[b.x];
if (a.y != b.y) return wh[a.y] < wh[b.y];
return a.pre < b.pre;
}
inline void add(int p) { if ( ++ col[p] == 1) ans ++ ; }
inline void del(int p) { if ( -- col[p] == 0) ans -- ; }
inline void swap(int &a, int &b) { a ^= b, b ^= a, a ^= b; }
inline void update(int now, int i)
{
if (C[now].pos >= Q[i].x and C[now].pos <= Q[i].y)
{
if ( -- col[a[C[now].pos]] == 0) ans -- ;
if ( ++ col[C[now].val] == 1) ans ++ ;
}
swap(C[now].val, a[C[now].pos]);
}
signed main()
{
n = read(), m = read();
base = pow(double(n), 2.0 / 3.0);
for (int i = 1; i <= n; i ++ )
a[i] = read(), wh[i] = (i - 1) / base + 1;
while (m --)
{
char ch; cin >> ch;
if (ch == 'Q')
{
Q[ ++ Qnum].x = read();
Q[Qnum].y = read();
Q[Qnum].pre = Cnum;
Q[Qnum].id = Qnum;
}
else
{
C[ ++ Cnum].pos = read();
C[Cnum].val = read();
}
}
sort(Q + 1, Q + Qnum + 1, cmp);
int l = 1, r = 0, now = 0;
for (int i = 1; i <= Qnum; i ++ )
{
while (l < Q[i].x) del(a[l ++ ]);
while (l > Q[i].x) add(a[ -- l]);
while (r < Q[i].y) add(a[ ++ r]);
while (r > Q[i].y) del(a[r -- ]);
while (now < Q[i].pre) update( ++ now, i);
while (now > Q[i].pre) update(now -- , i);
out[Q[i].id] = ans;
}
for (int i = 1; i <= Qnum; i ++ ) cout << out[i] << endl;
return 0;
}