BZOJ 2120 数颜色

带修莫队

因为莫队是强行离线,所以修改的话需要加一个时间戳,每次询问的时候暴力还原或者更新某个位置的数就行了,但是这样只能单点修改,区间修改好像是不资瓷的。

在给询问分块的时候,不能再让每块的长度是sqrt(n)了,最佳的长度经过神牛们证明是n^2/3,具体证明就看看网上的说明吧QAQ

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define full(a, b) memset(a, b, sizeof a)
using namespace std;
typedef long long ll;
inline int lowbit(int x){ return x & (-x); }
inline int read(){
    int X = 0, w = 0; char ch = 0;
    while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
    while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
    return w ? -X : X;
}
inline int gcd(int a, int b){ return b ? gcd(b, a % b) : a; }
inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
template<typename T>
inline T max(T x, T y, T z){ return max(max(x, y), z); }
template<typename T>
inline T min(T x, T y, T z){ return min(min(x, y), z); }
template<typename A, typename B, typename C>
inline A fpow(A x, B p, C lyd){
    A ans = 1;
    for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
    return ans;
}
const int N = 1000005;
int n, m, k, q, t, a[N], pos[N], ans, freq[N], res[N];
struct Query{
    int l, r, t, id;
    bool operator < (const Query &rhs) const {
        if(pos[l] != pos[rhs.l]) return l < rhs.l;
        if(pos[r] != pos[rhs.r]) return r < rhs.r;
        return t < rhs.t;
    }
}query[N];
struct Modify{
    int p, pre, suc;
}modify[N];

void add(int k){
    freq[a[k]] ++;
    if(freq[a[k]] == 1) ans ++;
}

void remove(int k){
    freq[a[k]] --;
    if(freq[a[k]] == 0) ans --;
}

int main(){

    n = read(), m = read();
    for(int i = 1; i <= n; i ++) a[i] = read();
    t = (int)pow(n, 2.0/3);
    //t = (int)sqrt(n);
    for(int i = 1; i <= m; i ++){
        char opt[3]; scanf("%s", opt);
        if(opt[0] == 'R'){
            k ++;
            modify[k].p = read(), modify[k].suc = read();
            modify[k].pre = a[modify[k].p];
            a[modify[k].p] = modify[k].suc;
        }
        else{
            q ++;
            query[q].l = read(), query[q].r = read();
            query[q].id = q, query[q].t = k;
            pos[query[q].l] = (query[q].l - 1) / t + 1;
            pos[query[q].r] = (query[q].r - 1) / t + 1;
        }
    }
    for(int i = k; i >= 1; i --) a[modify[i].p] = modify[i].pre;
    sort(query + 1, query + q + 1);
    int l = 1, r = 0, ti = 0;
    for(int i = 1; i <= q; i ++){
        int curL = query[i].l, curR = query[i].r, curT = query[i].t;
        while(l < curL) remove(l ++);
        while(r < curR) add(++ r);
        while(l > curL) add(-- l);
        while(r > curR) remove(r --);
        while(ti < curT){
            ti ++;
            int ps = modify[ti].p;
            if(l <= ps && ps <= r) remove(ps);
            a[ps] = modify[ti].suc;
            if(l <= ps && ps <= r) add(ps);
        }
        while(ti > curT){
            int ps = modify[ti].p;
            if(l <= ps && ps <= r) remove(ps);
            a[ps] = modify[ti].pre;
            if(l <= ps && ps <= r) add(ps);
            ti --;
        }
        res[query[i].id] = ans;
    }
    for(int i = 1; i <= q; i ++){
        printf("%d\n", res[i]);
    }
    return 0;
}
posted @ 2019-05-14 20:51  清楚少女ひなこ  阅读(162)  评论(0编辑  收藏  举报