跳到正文

剪贴板

剪贴板 6a89gp0d

原文
剪贴板信息

剪贴板信息

收录
更新
C++
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
const int maxm = 2e4;
const int maxk = maxm + 10;
int n,Len,tot,block[maxn];
int a[maxn],b[maxn];
int ans[maxn],cnt[maxn],vis[maxn];
bitset<maxn> U[maxm],u;

struct node
{
    int id,l,r;
}query[maxn << 2];

int main()
{
    int q;
    cin >> n >> q;
    Len = sqrt(n);
    for(int i = 1;i <= n;i++)
    {
        cin >> a[i];
        b[i] = a[i];
        block[i] = (i - 1) / Len + 1;
    }
    sort(b + 1,b + n + 1);
    for(int i = 1;i <= n;i++)
    {
        a[i] = lower_bound(b + 1,b + n + 1,a[i]) - b;
    }
    auto cmp = [&](node a,node b){return block[a.l] == block[b.l] ? a.r < b.r : block[a.l] < block[b.l];};
    auto Add = [&](int id)
    {
        int x = a[id];
        cnt[x]++;
        u[x + cnt[x] - 1] = 1;
    };
    auto Del = [&](int id)
    {
        int x = a[id];
        cnt[x]--;
        u[x + cnt[x]] = 0;
    };
    auto solve = [&](int op)
    {
        tot = 0;
        memset(vis,0,sizeof(vis));
        memset(ans,0,sizeof(ans));
        memset(cnt,0,sizeof(cnt));
        for(int i = 1;i <= op;i++)
        {
            for(int j = 1;j <= 3;j++)
            {
                query[++tot].id = i;
                cin >> query[tot].l >> query[tot].r;
                ans[i] += query[tot].r - query[tot].l + 1;
            }
        }
        sort(query + 1,query + tot + 1,cmp);
        u.reset();
        int l = 1,r = 0;
        for(int i = 1;i <= tot;i++)
        {
            while(l > query[i].l)
            {
                Add(--l);
            }
            while(r < query[i].r)
            {
                Add(++r);
            }
            while(l < query[i].l)
            {
                Del(l++);
            }
            while(r > query[i].r)
            {
                Del(r--);
            }
            if(!vis[query[i].id])
            {
                U[query[i].id] = u;
                vis[query[i].id] = 1;
            }
            else
            {
                U[query[i].id] &= u;
            }
        }
        for(int i = 1;i <= op;i++)
        {
            cout << ans[i] - U[i].count() * 3 << '\n';
        }
    };
    while(q)
    {
        if(q < maxm - 5)
        {
            solve(q);
            break;
        }
        else
        {
            solve(maxm - 5);
            q -= maxm - 5;
        }
    }
    return 0;
}