牛客网暑期ACM多校训练营(2018.第一场)J 题

链接:https://www.nowcoder.com/acm/contest/139/J
来源:牛客网
 

题目描述

Given a sequence of integers a1, a2, ..., an and q pairs of integers (l1, r1), (l2, r2), ..., (lq, rq), find count(l1, r1), count(l2, r2), ..., count(lq, rq) where count(i, j) is the number of different integers among a1, a2, ..., ai, aj, aj + 1, ..., an.

输入描述:

The input consists of several test cases and is terminated by end-of-file.
The first line of each test cases contains two integers n and q.
The second line contains n integers a1, a2, ..., an.
The i-th of the following q lines contains two integers li and ri.

输出描述:

For each test case, print q integers which denote the result.

示例1

输入

复制

3 2
1 2 1
1 2
1 3
4 1
1 2 3 4
1 3

输出

复制

2
1
3

备注:

* 1 ≤ n, q ≤ 1e5
* 1 ≤ ai ≤ n
* 1 ≤ li, ri ≤ n
* The number of test cases does not exceed 10.

题意:

给出一个数列,长度为k,q次查询,每给出一个数对(lq, rq)都要查询(1,lq),(rq,k)中不同数字种类的数量

思路:

主席树,和莫队TLE了(不过听说过有人莫队卡过了);

先将数组变为原来的两倍 , 这样就变成了区间问题

先用一个前缀数组per , per [ i ] 记录前面出现的数字的种类 , 那么 cout ( lq , rq )  =  per [ lq + k ] - per [ rq ] +  (  lq 与 rq 中 共同出现的数字种类数);

可是   [ lq 与 rq 中 共同出现的数字种类数 ] 如何求呢?

可以考虑用树状数组维护求得

复杂度O ( n * lg ( n ) ) 

思路结束

-------------------------------------------------------------------------------AC code-------------------------------------------------------------------------

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>

using namespace std;

const int maxn = 2e5+5;

int bit[maxn],p[maxn],nxt[maxn];
int per[maxn],v[maxn],vis[maxn];
int n,q;

struct node{
    int l,r;
    int id,ans;
}ss[100000 + 5];

int cmp(node x,node y){
    return x.l < y.l;
}

int cmp1(node x,node y){
    return x.id < y.id;
}

void update(int pos,int val){
    while( pos < n ) {
        bit[pos] += val;
        pos += (pos & (-pos));
    }
}

int Query(int pos){
    int res = 0;
    while ( pos > 0 ) {
        res += bit[pos];
        pos -= (pos & (-pos));
    }
    return res;
}

int query(int l,int r){
    return Query(r) - Query(l - 1);
}

void init(){
    memset(vis,0,sizeof(vis));
    memset(p,-1,sizeof(p));
    memset(nxt,-1,sizeof(nxt));
    memset(bit,0,sizeof(bit));
}

int main(){
    while(cin>>n>>q)
    {
        init();
        for (int i = 1;i<=n;i++){
            scanf("%d",&v[i]);
            v[i+n] = v[i];
        }
        n = n*2; per[0] = 0;
        for (int i = 1;i<=n;i++){
            if( !vis[v[i]] ){
                per[i] = per[i-1] + 1;
                vis[v[i]] = 1;
            } else {
                per[i] = per[i-1];
            }
            if ( ~p[v[i]] ) {
                nxt[p[v[i]]] = i;
            }
            p[v[i]] = i;
        }
        for (int i = 0;i<q;i++){
            scanf("%d %d",&ss[i].l,&ss[i].r);
            ss[i].id = i;
            ss[i].l += (n/2);
            swap(ss[i].l,ss[i].r);
        }
        int r = 1;
        sort(ss,ss+q,cmp);
        ///for (int i = 1 ;i <= n ;i++)
        ///    printf("%d ",nxt[i]);
        ///printf("\n");
        ///for (int i = 1 ;i <= n ;i++)
        ///    printf("%d ",per[i]);
        ///printf("\n");
        for (int i = 0;i<q;i++){
            while ( r < ss[i].l ) {
                if ( ~nxt[r] ) {
                    update(nxt[r],1);
                }
                r++;
            }
            ss[i].ans = per[ss[i].r] - per[ss[i].l - 1] + query(ss[i].l,ss[i].r );
            ///printf("ss[i].l = %d ss[i].r = %d\n",ss[i].l,ss[i].r);
            ///printf("%d %d\n",Query(ss[i].l),Query(ss[i].r));
        }
        sort(ss,ss+q,cmp1);
        for (int i = 0;i<q;i++){
            printf("%d\n",ss[i].ans);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Acer12138/article/details/81153312
今日推荐