Luo Gu [P4688] [Ynoi2016] Down the Rabbit Hole (bitset, MO team)

Topic Link
First track Ynoi
Obviously the answer is the length of each inquiry and public figures by subtracting the number three intervals * 3.
If the number is a public figure, then you can kind of team with Mo + bitset memory status of each interval, then three intervals Bitwise AND on the line.
But now is the number, bitset in addition to save each number appears, but also save the number of occurrences.
Then we found that the number of each number appears and \ (= n \)
so expect after each discrete digital account bitset in a grid.
Remember \ (SA \) in the radix sort it? This enables the section \ (n-\) with the addition of a digital time interval has a fixed mounting position.
So the team will be able Mo.
However, a look at the data range, if no less open \ (1E5 \) length of \ (1E5 \) of bitset ah.
It does not matter, the answers were divided into three groups, one to a group on the list.

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <bitset>
#include <cmath>
#include <iostream>
using namespace std;
const int MAXN = 100010;
const int MAXM = 34010;
bitset <MAXN> ans[MAXM], now;
int n, m, a[MAXN];
inline int read(){
    int s = 0;
    char ch = getchar();
    while(ch < '0' || ch > '9') ch = getchar();
    while(ch >= '0' && ch <= '9'){ s = s * 10 + ch - '0'; ch = getchar(); }
    return s;
}
int Q;
struct lsh{
    int val, id;
    int operator < (const lsh A) const{
        return val < A.val;
    }
}p[MAXN];
struct ask{
    int l, r, id;
    int operator < (const ask A) const{
        return l / Q == A.l / Q ? r < A.r : l < A.l;
    }
}q[MAXM * 3];
int val[MAXN], cnt, sum[MAXN], v[MAXN], all, Ans[MAXM];
void work(){
    sort(q + 1, q + all + 1);
    now.reset();
    memset(v, 0, sizeof v);
    int l = 1, r = 1; now.set(val[1]); v[val[1]] = 1;
    for(int i = 1; i <= all; ++i){
        while(r < q[i].r){ ++r; now.set(val[r] - v[val[r]]); ++v[val[r]]; }
        while(l > q[i].l){ --l; now.set(val[l] - v[val[l]]); ++v[val[l]]; }
        while(r > q[i].r){ --v[val[r]]; now.set(val[r] - v[val[r]], 0); --r; }
        while(l < q[i].l){ --v[val[l]]; now.set(val[l] - v[val[l]], 0); ++l; }
        ans[q[i].id] &= now;
    }
}
int main(){
    n = read(); m = read(); Q = sqrt(n);
    for(int i = 1; i <= n; ++i)
        p[i].val = read(), p[i].id = i;
    for(int i = 1; i <= 33337; ++i)
       ans[i].set();
    sort(p + 1, p + n + 1);
    for(int i = 1; i <= n; ++i)
        if(p[i].val != p[i - 1].val)
            val[p[i].id] = ++cnt;
        else val[p[i].id] = cnt;
    for(int i = 1; i <= n; ++i)
        ++sum[val[i]];
    for(int i = 1; i <= cnt; ++i)
        sum[i] += sum[i - 1];
    for(int i = 1; i <= n; ++i)
        val[i] = sum[val[i]];
    int o = m / 3;
    if(o){
        for(int qqc = 1; qqc <= 2; ++qqc){
            cnt = 0;
            for(int i = 1; i <= o; ++i){
                q[++cnt].id = i; q[cnt].l = read(); q[cnt].r = read(); Ans[i] += q[cnt].r - q[cnt].l + 1;
                q[++cnt].id = i; q[cnt].l = read(); q[cnt].r = read(); Ans[i] += q[cnt].r - q[cnt].l + 1;
                q[++cnt].id = i; q[cnt].l = read(); q[cnt].r = read(); Ans[i] += q[cnt].r - q[cnt].l + 1;
            }
            all = o * 3; work();
            for(int i = 1; i <= o; ++i){
                printf("%d\n", Ans[i] - int(ans[i].count()) * 3);
                Ans[i] = 0; ans[i].set();
            }
        }
    }
    m -= o * 2; cnt = 0;
    for(int i = 1; i <= m; ++i){
        q[++cnt].id = i; q[cnt].l = read(); q[cnt].r = read(); Ans[i] += q[cnt].r - q[cnt].l + 1;
        q[++cnt].id = i; q[cnt].l = read(); q[cnt].r = read(); Ans[i] += q[cnt].r - q[cnt].l + 1;
        q[++cnt].id = i; q[cnt].l = read(); q[cnt].r = read(); Ans[i] += q[cnt].r - q[cnt].l + 1;
    }
    all = m * 3; work();
    for(int i = 1; i <= m; ++i)
        printf("%d\n", Ans[i] - int(ans[i].count()) * 3);
    return 0;
}

Guess you like

Origin www.cnblogs.com/Qihoo360/p/11026356.html