码迷,mamicode.com
首页 > 其他好文 > 详细

Little Elephant and Array CodeForces - 220B(莫队)

时间:2018-07-21 14:44:25      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:update   with   end   algorithm   stream   oid   fine   r++   stack   

给一段长为n的序列和m个关于区间的询问,求出每个询问的区间中有多少种数字是 该种数字出现的次数等于该数字 的。

 

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _  ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = 200010, INF = 0x7fffffff;
LL n, m;
LL pos[maxn], c[maxn], s[maxn], ans;
LL pre[maxn], t[maxn];
struct node
{
    LL l, r, res;
    int id;
}Node[maxn];

bool cmp(node x, node y)
{
    if(pos[x.l] == pos[y.l]) return x.r < y.r;
    return x.l < y.l;
}

bool cmp_id(node x, node y)
{
    return x.id < y.id;
}

void update(int p, int add)
{
    if(add == 1)
    {
        s[c[p]]++;
        if(s[c[p]] == pre[p])
            ans++;
        else if(s[c[p]] == pre[p] + 1)
            ans--;

    }
    else
    {
        s[c[p]]--;
        if(s[c[p]] == pre[p])
            ans++;
        else if(s[c[p]] + 1 == pre[p])
            ans--;
    }
}


int main()
{
    ans = 0;
    scanf("%lld%lld", &n, &m);
    for(int i=1; i<=n; i++)
    {
        scanf("%lld", &c[i]);
        pre[i] = t[i] = c[i];
    }
    sort(t+1, t+n+1);
    int len = unique(t+1, t+n+1) - (t+1);
    for(int i=1; i<=n; i++)
        c[i] = lower_bound(t+1, t+len+1, c[i]) - t;
    int block = sqrt(n);
    for(int i=1; i<=n; i++)
        pos[i] = (i-1)/block + 1;
    for(int i=1; i<=m; i++)
    {
        scanf("%lld%lld", &Node[i].l, &Node[i].r);
        Node[i].id = i;
    }
    sort(Node+1, Node+m+1, cmp);
    for(int i=1, l=1, r=0; i<=m; i++)
    {
        for(; r < Node[i].r; r++)
            update(r+1, 1);
        for(; r > Node[i].r; r--)
            update(r, -1);
        for(; l < Node[i].l; l++)
            update(l, -1);
        for(; l > Node[i].l; l--)
            update(l-1, 1);

        Node[i].res = ans;
    }
    sort(Node+1, Node+m+1, cmp_id);
    for(int i=1; i<=m; i++)
        cout<< Node[i].res <<endl;


    return 0;
}

 

Little Elephant and Array CodeForces - 220B(莫队)

标签:update   with   end   algorithm   stream   oid   fine   r++   stack   

原文地址:https://www.cnblogs.com/WTSRUVF/p/9345791.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!