[BZOJ2453][维护队列][莫队]

50 篇文章 0 订阅
3 篇文章 0 订阅

[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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值