[BZOJ 2120][数颜色][带修改的莫队]

3 篇文章 0 订阅

[BZOJ 2120][数颜色][带修改的莫队]

安利大法好,有关莫队算法可以关注这篇文章:
https://zhuanlan.zhihu.com/p/25017840

题目大意:

处理两种询问,第一种是统计区间 [L,R] 中有几种不同的颜色,第二种是将颜色A修改成B。

询问数和区间长度都<=10000,暴力完全可过……

思路:

和普通莫队同理,分块后用 (l,r) 去更新 (l+1,r)(l1,r),(l,r+1),(l,r1) 。但由于这题还带了修改,因此对于每个询问我们要记录一个 time 表示在这个询问之前有多少个修改操作。在每次更新区间时,要同步更新或者回退修改操作,和更新区间类似, time>time+1time>time1 的复杂度都是 O(1)

同时为了保证复杂度,采取的策略是对于每个询问按 (l/block,r/block,time) 排序(分别表示l所在的块、r所在的询问之前的修改次数),再顺序完成,根据复杂度证明可以保证复杂度上界为 O(n53)

代码:
#include <bits/stdc++.h>
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、付费专栏及课程。

余额充值