Codeforces Round #136 (Div. 1) B. Little Elephant and Array(简单莫队)

来源:互联网 发布:网络管理软件哪个好 编辑:程序博客网 时间:2024/05/29 02:46

题意:给你n个数,m次询问,每次询问一个区间[l, r],问这个区间内元素出现个数等于元素本身的有几个。(n, m <= 1e5)


思路:裸的莫队,因为每个数比较大,离散化下就好了。


代码:

#include<bits/stdc++.h>using namespace std;typedef long long ll;const int maxn = 1e5+5;int n, m, unit, tmp;int a[maxn], aa[maxn], Hash[maxn];int num[maxn], ans[maxn];struct node{    int id, l, r, blk;    bool operator <(const node &a) const    {        if(blk == a.blk) return r < a.r;        else return blk < a.blk;    }}op[maxn];void add(int id){    num[aa[id]]++;    if(num[aa[id]] == a[id]) tmp++;    if(num[aa[id]] == a[id]+1) tmp--;}void del(int id){    num[aa[id]]--;    if(num[aa[id]] == a[id]) tmp++;    if(num[aa[id]] == a[id]-1) tmp--;}void solve(){    tmp = 0;    memset(num, 0, sizeof(num));    int l = 1, r = 0;    for(int i = 1; i <= m; i++)    {        while(r < op[i].r) add(++r);        while(r > op[i].r) del(r--);        while(l < op[i].l) del(l++);        while(l > op[i].l) add(--l);        ans[op[i].id] = tmp;    }    for(int i = 1; i <= m; i++)        printf("%d\n", ans[i]);}int main(void){    while(cin >> n >> m)    {        unit = (int)sqrt(n);        for(int i = 1; i <= n; i++)            scanf("%d", &a[i]), Hash[i] = a[i];        sort(Hash+1, Hash+1+n);        int d = unique(Hash+1, Hash+1+n)-Hash-1;        for(int i = 1; i <= n; i++)            aa[i] = lower_bound(Hash+1, Hash+1+d, a[i])-Hash;        for(int i = 1; i <= m; i++)            scanf("%d%d", &op[i].l, &op[i].r), op[i].id = i, op[i].blk = op[i].l/unit;        sort(op+1, op+1+m);        solve();    }    return 0;}


阅读全文
1 0
原创粉丝点击