[BZOJ2453][维护队列][莫队]
裸的莫队吧。。
代码:
#include <cstdio>
#include <cmath>
#include <algorithm>
const int Maxn = 1010000;
using namespace std;
inline char get(void) {
static char buf[1000000], *p1 = buf, *p2 = buf;
if (p1 == p2) {
p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin);
if (p1 == p2) return EOF;
}
return *p1++;
}
inline void read(int &x) {
x = 0; static char c; bool minus = false;
for (; !(c >= '0' && c <= '9'); c = get()) if (c == '-') minus = true;
for (; c >= '0' && c <= '9'; x = x * 10 + c - '0', c = get()); if (minus) x = -x;
}
inline void read(char &x) {
x = get();
while (!(x >= 'A' && x <= 'Z')) x = get();
}
struct Modify {
int x, y, last;
} mo[Maxn];
int block[Maxn], a[Maxn], last[Maxn];
struct Ask {
int l, r, id, apart;
friend bool operator < (const Ask &a, const Ask &b) {
if (block[a.l] == block[b.l]) {
if (block[a.r] == block[b.r])
return a.apart < b.apart;
else return block[a.r] < block[b.r];
}
else return block[a.l] < block[b.l];
}
} ask[Maxn];
int n, m, B, tot1, tot2, Ans[Maxn], ans, L, R, head, cnt[Maxn];
inline void change(int x, int y) {
if (x >= L && x <= R) {
cnt[a[x]]--;
if (!cnt[a[x]]) ans--;
a[x] = y;
cnt[a[x]]++;
if (cnt[a[x]] == 1) ans++;
}
else a[x] = y;
}
inline void update(int pos, int type) {
int his = cnt[a[pos]];
cnt[a[pos]] += type;
if (!his && cnt[a[pos]] == 1) ans++;
if (his == 1 && !cnt[a[pos]]) ans--;
}
int main(void) {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
read(n), read(m); B = sqrt(n);
for (int i = 1; i <= n; i++) read(a[i]), last[i] = a[i];
for (int i = 1; i <= n; i++) block[i] = (i - 1) / B + 1;
for (int i = 1; i <= m; i++) {
char op; read(op);
if (op == 'R') {
read(mo[++tot1].x);
read(mo[tot1].y);
mo[tot1].last = last[mo[tot1].x];
last[mo[tot1].x] = mo[tot1].y;
}
else {
read(ask[++tot2].l);
read(ask[tot2].r);
ask[tot2].id = tot2;
ask[tot2].apart = tot1;
}
}
sort(ask + 1, ask + 1 + tot2);
L = 1, R = 0;
for (int i = 1; i <= tot2; i++) {
while (head > ask[i].apart) {
change(mo[head].x, mo[head].last);
head--;
}
while (head < ask[i].apart) {
++head;
change(mo[head].x, mo[head].y);
}
while (L < ask[i].l) update(L, -1), L++;
while (R > ask[i].r) update(R, -1), R--;
while (L > ask[i].l) L--, update(L, 1);
while (R < ask[i].r) R++, update(R, 1);
Ans[ask[i].id] = ans;
}
for (int i = 1; i <= tot2; i++) printf("%d\n", Ans[i]);
return 0;
}
完。
By g1n0st