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]);
            }
    }
    }
  • 相关阅读:
    Sql中关于日期的格式化
    linq中存储过程返回集合存在的问题
    当页面请求数据是从静态页中获取的Post方法会报405的错误
    jquery中load方法在ie下会卡住
    jquery用div实现下拉列表的效果
    jquery获取服务器端控件的方法
    【转】jquery实现文本框有提示输入的信息
    toString()方法浅谈
    Object类与上下转型
    多态
  • 原文地址:https://www.cnblogs.com/zyf3855923/p/9357571.html
Copyright © 2011-2022 走看看