bzoj 2120 数颜色 带修改莫队

题目链接

Description

墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问。

墨墨会向你发布如下指令:
1、 Q L R代表询问你从第L支画笔到第R支画笔中共有几种不同颜色的画笔。
2、 R P Col 把第P支画笔替换为颜色Col。

为了满足墨墨的要求,你知道你需要干什么了吗?

思路

参考:http://www.cnblogs.com/candy99/p/6194035.html

额外维护一个当前修改操作执行的时间(tim),以及每个修改操作之前之后的效果,以便之后不断进行修改还原

对于每个(query),也增加时间一维,按照(l,r,tim)排序。

移动时,首先移动(tim),再移动(l)(r).

注意:移动时间时,仅当当前时间修改的位置(x)在当前的([l,r])范围内,才需要考虑这个修改操作的影响。

Code

#include <bits/stdc++.h>
#define F(i, a, b) for (int i = (a); i < (b); ++i)
#define F2(i, a, b) for (int i = (a); i <= (b); ++i)
#define dF(i, a, b) for (int i = (a); i > (b); --i)
#define dF2(i, a, b) for (int i = (a); i >= (b); --i)
#define maxn 10010
#define maxm 1000010
using namespace std;
typedef long long LL;
int n,m,l,r,temp, cnt[maxm], a[maxn], t[maxn], bl[maxn], blo,ans[maxn];
struct qnode {
    int l,r,tim,id;
    bool operator < (const qnode& nd) const {
        return bl[l]==bl[nd.l] ? (bl[r]==bl[nd.r] ? tim<nd.tim : bl[r]<bl[nd.r]) : bl[l]<bl[nd.l];
    }
}q[maxn];
struct rnode { int x, ya, yb; }c[maxn];
inline void add(int x) { if (!cnt[x]) ++temp; ++cnt[x]; }
inline void del(int x) { --cnt[x]; if (!cnt[x]) --temp; }
inline void cha1(rnode& nd) { if (l<=nd.x&&nd.x<=r) del(nd.yb), add(nd.ya); a[nd.x] = nd.ya; }
inline void cha2(rnode& nd) { if (l<=nd.x&&nd.x<=r) del(nd.ya), add(nd.yb); a[nd.x] = nd.yb; }
int main() {
    scanf("%d%d",&n,&m); blo = sqrt(n);
    F2(i, 1, n) scanf("%d", &a[i]), t[i]=a[i], bl[i]=(i-1)/blo+1;
    int tim=0,cnt1=0,cnt2=0;
    F(i, 0, m) {
        char ch; int x, y;
        scanf("
%c%d%d", &ch,&x,&y);
        if (ch=='R') ++tim, c[++cnt2] = {x,y,t[x]}, t[x]=y;
        else q[cnt1] = {x,y,tim,cnt1}, cnt1++;
    }
    sort(q,q+cnt1);
    r=0, l=1; tim=0;
    F(i, 0, cnt1) {
        while (tim<q[i].tim) cha1(c[++tim]);
        while (tim>q[i].tim) cha2(c[tim--]);
        while (r<q[i].r) add(a[++r]);
        while (r>q[i].r) del(a[r--]);
        while (l<q[i].l) del(a[l++]);
        while (l>q[i].l) add(a[--l]);
        ans[q[i].id] = temp;
    }
    F(i, 0, cnt1) printf("%d
", ans[i]);
    return 0;
}

原文地址:https://www.cnblogs.com/kkkkahlua/p/8481296.html