Luogu4137:Rmq Problem/mex

题面

传送门

Sol

这题可能是假的
离线莫队搞一搞,把数字再分块搞一搞,就行了

# include <bits/stdc++.h>
# define IL inline
# define RG register
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(2e5 + 5);

IL ll Input(){
    RG char c = getchar(); RG ll x = 0, z = 1;
    for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
    for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    return x * z;
}

int n, q, bl[_], val[_], ans[_], cnt[_], sum[_];
struct Qry{
    int l, r, id;
    IL bool operator <(RG Qry B) const{
		return bl[l] != bl[B.l] ? bl[l] < bl[B.l] : r < B.r;
	}
} qry[_];

IL void Modify(RG int x, RG int d){
	if(d > 0){
		if(!cnt[x]) ++sum[x / 500];
		++cnt[x];
	}
	else{
		--cnt[x];
		if(!cnt[x]) --sum[x / 500];
	}
}

IL int Calc(){
	RG int ret = 0;
	for(RG int i = 0; ; ++i)
		for(RG int j = 0; j < 500 && sum[i] != 500; ++j)
			if(!cnt[i * 500 + j]) return i * 500 + j;
}

int main(RG int argc, RG char* argv[]){
    n = Input(); q = Input();
	RG int blo = sqrt(n);
    for(RG int i = 1; i <= n; ++i){
		val[i] = Input();
		bl[i] = (i - 1) / blo + 1;
	}
    for(RG int i = 1; i <= q; ++i) qry[i] = (Qry){Input(), Input(), i};
    sort(qry + 1, qry + q + 1);
    for(RG int L = qry[1].l, R = qry[1].l - 1, i = 1; i <= q; ++i){
        while(L < qry[i].l) Modify(val[L], -1), ++L;
        while(L > qry[i].l) --L, Modify(val[L], 1);
        while(R < qry[i].r) ++R, Modify(val[R], 1);
        while(R > qry[i].r) Modify(val[R], -1), --R;
        ans[qry[i].id] = Calc();
    }
    for(RG int i = 1; i <= q; ++i) printf("%d
", ans[i]);
    return 0;
}

原文地址:https://www.cnblogs.com/cjoieryl/p/8365238.html