CodeForces 940F Machine Learning

S o u r c e : Codeforces Round #466 (Div. 2)
P r o b l e m : n 个整数, m 种操作,一种是查询 [ l , r ] 的mex{ c 1 , . . . , c 10 9 }, c x x 出现的次数,另一种是修改一个整数的值。
I d e a : 带修改的莫队算法,就是把查询分块,按顺序暴力修改。然后注意下越界的问题…

C o d e :

#include<bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define lson o<<1
#define rson o<<1|1
#define pb push_back
#define ALL(A) (A).begin(), (A).end()
#define bitcount(X) __builtin_popcountll(X)
#define UNI(A) (unique(ALL(A))-A.begin())
#define CLR(A, X) memset(A, X, sizeof(A))
#define LBD(A, X) (lower_bound(ALL(A), X)-A.begin())
#define UBD(A, X) (upper_bound(ALL(A), X)-A.begin())
typedef long long LL;
typedef pair<int, int> PII;
const int MOD = 1e9+7;
const double eps = 1e-10;
const double PI = acos(-1.0);
const auto INF = 0x3f3f3f3f;
int dcmp(double x) { if(fabs(x) < eps) return 0; return x<0?-1:1; }

const int MAXN = 1e5+10;

int l = 1, r = 0;
int a[MAXN], b[MAXN], now[MAXN], num[MAXN<<1], cnt[MAXN], ans[MAXN];
map<int, int> ms;

struct Change { int p, x, y; }c[MAXN];

struct Query {
    int l, r, t, id;
    bool operator < (const Query &A) const {
        if(b[l] == b[A.l]) return b[r]==b[A.r]?t<A.t:b[r]<b[A.r];
        return b[l] < b[A.l];
    }
}q[MAXN];

void update(int x, int d) {
    int &y = num[x];
    if(y > 0)cnt[y]--;
    y += d;
    if(y > 0) cnt[y]++;
}

void modify(int p, int d) {
    if(l<=p && p<=r) {
        update(a[p], -1);
        update(d, 1);
    }
    a[p] = d;
}

int get() {
    for(int i = 1; ; i++) {
        if(!cnt[i]) return i;
    }
}

int main() {
    int n, m, id = 0, Time = 0, sz = 0;
    scanf("%d%d", &n, &m);
    int B = 2000;
    for(int i = 1; i <= n; i++) {
        scanf("%d", &a[i]);
        int &t = ms[a[i]];
        if(!t) t = ++sz;
        now[i] = a[i] = t;
        b[i] = i/B+1;
    }
    while(m--) {
        int op, x, y;
        scanf("%d%d%d", &op, &x, &y);
        if(op == 1) q[++id] = (Query){x, y, Time, id};
        else {
            int &t = ms[y];
            if(!t) t = ++sz;
            c[++Time] = (Change){x, y=t, now[x]};
            now[x] = y;
        }
    }
    sort(q+1, q+id+1);
    int v = 0;
    for(int i = 1; i <= id; i++) {
        while(v < q[i].t) modify(c[v+1].p, c[v+1].x), v++;
        while(v > q[i].t) modify(c[v].p, c[v].y), v--;
        while(l < q[i].l) update(a[l], -1), l++;
        while(l > q[i].l) update(a[l-1], 1), l--;
        while(r < q[i].r) update(a[r+1], 1), r++;
        while(r > q[i].r) update(a[r], -1), r--;
        ans[q[i].id] = get();
    }
    for(int i = 1; i <= id; i++) {
        printf("%d\n", ans[i]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_32506797/article/details/79391168