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

来源:互联网 发布:full node bitcoin 编辑:程序博客网 时间:2024/05/15 12:10

[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

1 0
原创粉丝点击