萌新刚学莫队,求助
查看原帖
萌新刚学莫队,求助
86973
花园Serena楼主2020/12/2 10:24

WA 了最后一个点,用的值域分块 + 莫队, 不知道为什么错了,求助。 代码如下

#include <bits/stdc++.h>
using namespace std;
#define R register int
const int N = 2e6 + 10;
int key[N], cnt[N], a[N], size;
int n, m, ans[N], num[N];
struct node {
    int l, r, op, id;
}q[N];
inline int read () {
    int x = 0; char ch = getchar();
    while(!isdigit(ch)) ch = getchar();
    while(isdigit(ch)) x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
    return x;
}
void write(int x) {
	if(x > 9) write(x / 10);
	putchar(x % 10 + 48);
}
bool inline cmp (register node x, register node y) {
    return x.op != y.op ? x.l < y.l : ((x.op & 1) ? x.r < y.r : x.r > y.r);
}
void inline add(int x) {
    if(!cnt[a[x]]) num[a[x] / size] ++;
    cnt[a[x]] ++;
}
void inline del(int x) {
    -- cnt[a[x]];
    if(!cnt[a[x]]) num[a[x] / size] --;
}
void inline query (int x) {
    for(R i = 1; i <= size; i ++)
            if(num[i - 1] != size)
                for(R j = (i - 1) * size; j < i * size; j ++)
                    if(!cnt[j]) {ans[x] = j; return;}
}
int main ()  {
    n = read(); m = read(); size = sqrt(n);
    for(R i = 1; i <= n; i ++)
        a[i] = min(read(), n + 1);
    for(R i = 1; i <= m; i ++) {
        q[i].l = read(); q[i].r = read();
        q[i].id = i;  q[i].op = (q[i].l - 1) / size - 1;
    }
    sort(q + 1, q + m + 1, cmp);
    int l = 1, r = 0;
    for(R i = 1; i <= m; i ++) {
        bool tag = false;
        while(l > q[i].l) add(-- l);
        while(l < q[i].l) del(l ++);
        while(r < q[i].r) add(++ r);
        while(r > q[i].r) del(r --);
        query(q[i].id);
    }
    for(R i = 1; i <= m; i ++)
        printf("%d\n", ans[i]);
    return 0;
}
2020/12/2 10:24
加载中...