zoukankan      html  css  js  c++  java
  • Different Integers

    链接: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 a

    1

    , a

    2

    , ..., a

    n

    .
    The i-th of the following q lines contains two integers l

    i

     and r

    i

    .

    输出描述:

    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 ≤ 10

    5


    * 1 ≤ a

    i

     ≤ n
    * 1 ≤ l

    i

    , r

    i

     ≤ n
    * The number of test cases does not exceed 10.

    不行了。n忘乘了2,卡了一小时。
    就是延长数组一倍,树状数组维护。
    #include <bits/stdc++.h>
    #define maxn 100005
    using namespace std;
    int n,m;
    int vis[maxn*2],a[maxn*2],tree[maxn*2];
    struct node
    {
        int l,r,id;
        bool operator <(const node &b)
        {
            return r<b.r;
        }
    }query[maxn];
    int lowbit(int x)
    {
        return x&(-x);
    }
    
    int sum(int x)
    //int query(int x)
    {
        int res=0;
        while(x)
        {
            res+=tree[x];
            x-=lowbit(x);
        }
        return res;
    }
    void add(int x,int val)
    {
        while(x<=n)
        {
            tree[x]+=val;
            x+=lowbit(x);
        }
    }
    int ans[maxn];
    int main()
    {
        while(~scanf("%d%d",&n,&m))
        {   int i;
            memset(tree,0,sizeof(tree));
            memset(vis,0,sizeof(vis));
            for(i=1;i<=n;i++)
            {
              scanf("%d",&a[i]);
              a[i+n]=a[i];
            }
            for(i=1;i<=m;i++)
            {   int u,v;
                scanf("%d%d",&u,&v);
                query[i].l=v;
                query[i].r=u+n;
                query[i].id=i;
            }
            sort(query+1,query+1+m);
            int cur=1;
            n*=2;
            for(i=1;i<=m;i++)
            {
                for(int j=cur;j<=query[i].r;j++)
                {
                    if(vis[a[j]]!=0)
                    {
                        add(vis[a[j]],-1);
                    }
                    add(j,1);
                    vis[a[j]]=j;
                }
                cur=query[i].r+1;
                //cout<<query[i].r<<" "<<sum(query[i].r)<<endl;
                //cout<<query[i].l<<" "<<sum(query[i].l-1)<<endl;
                ans[query[i].id]=sum(query[i].r)-sum(query[i].l-1);
            }
            for(i=1;i<=m;i++)
           {
                printf("%d
    ",ans[i]);
            }
    }
    }
  • 相关阅读:
    全排列(next_permutation)
    Codeforces Round #321 (Div. 2)C(tree dfs)
    cf_ducational Codeforces Round 16_D(gcd)
    cf455a(简单dp)
    cf584a(水题)
    cf112a(水题)
    冒泡排序
    Python内置类型性能分析
    常见时间复杂度
    MongoDB 备份
  • 原文地址:https://www.cnblogs.com/zyf3855923/p/9357571.html
Copyright © 2011-2022 走看看