zoukankan      html  css  js  c++  java
  • cogs 421. HH的项链

    421. HH的项链

    http://218.28.19.228/cogs/problem/problem.php?pid=421

    ★★★   输入文件:diff.in   输出文件:diff.out   简单对比
    时间限制:1 s   内存限制:256 MB

    问题描述:
    HH有一串由各种漂亮的贝壳组成的项链。HH相信不同的贝壳会带来好运,所以每次散步
    完后,他都会随意取出一段贝壳,思考它们所表达的含义。HH不断地收集新的贝壳,因此,
    他的项链变得越来越长。有一天,他突然提出了一个问题:某一段贝壳中,包含了多少种不同
    的贝壳?这个问题很难回答。。。因为项链实在是太长了。于是,他只好求助睿智的你,来解
    决这个问题。
    输入格式:
    第一行:一个整数N,表示项链的长度。
    第二行:N个整数,表示依次表示项链中贝壳的编号(编号为0到1000000之间的整数)。
    第三行:一个整数M,表示HH询问的个数。
    接下来M行:每行两个整数,L和R(1 ≤ L ≤ R ≤ N),表示询问的区间。
    输出格式:
    M行,每行一个整数,依次表示询问对应的答案。
    样例输入:
    6
    1 2 3 4 3 5
    3
    1 2
    3 5
    2 6
    样例输出:
    2
    2
    4
    数据范围:
    对于20%的数据,N ≤ 100,M ≤ 1000;
    对于40%的数据,N ≤ 3000,M ≤ 200000;
    对于100%的数据,N ≤ 50000,M ≤ 200000。 

    /*其实和上个题一样啦,就是update换了换,记不同的个数*/
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    int ans,block,n,m,c[50010],s[1000010],pos[50010];
    struct node{
        int l,r,id,as;
    }a[200010];
    
    int cmp1(node x,node y){
        if(pos[x.l]==pos[y.l])return x.r<y.r;
        return x.l<y.l;
    }
    int cmp2(node x,node y){
        return x.id<y.id;
    }
    void update(int i,int add){
        if(s[c[i]]==0&&add==1)ans++;
        if(s[c[i]]==1&&add==-1)ans--;
        s[c[i]]+=add;
    }
    void solve(){
        int l=1,r=0;
        for(int i=1;i<=m;i++){
            while(r<a[i].r)
            r++,update(r,1);
            while(r>a[i].r)
            update(r,-1),r--;
            while(l>a[i].l)
            l--,update(l,1);
            while(l<a[i].l)
            update(l,-1),l++;
            a[i].as=ans;
        }
    }
    int main(){
        //freopen("cola.txt","r",stdin);
        freopen("diff.in","r",stdin);
        freopen("diff.out","w",stdout);
        scanf("%d",&n);block=(int)(sqrt(n));
        for(int i=1;i<=n;i++)pos[i]=(i-1)/block+1;
        for(int i=1;i<=n;i++)scanf("%d",&c[i]);
        scanf("%d",&m);
        for(int i=1;i<=m;i++)scanf("%d%d",&a[i].l,&a[i].r),a[i].id=i;
        sort(a+1,a+m+1,cmp1);
        solve();
        sort(a+1,a+m+1,cmp2);
        for(int i=1;i<=m;i++)printf("%d
    ",a[i].as);
    } 
  • 相关阅读:
    简直不敢相信...
    halcon 保存Region [原创]
    VS2015 下载链接
    C#【数据转换】十进制yte[]相互转换
    C# 复制窗体问题完美解决办法
    TextBox 保持固定长度,添加新行滚动到最后,跨线程。
    mysql isnull
    C#跨线程访问控件[我的记录]
    C# 常用类-IO-ClassExcelExport
    C# 常用类-IO-ClassXML
  • 原文地址:https://www.cnblogs.com/thmyl/p/6936235.html
Copyright © 2011-2022 走看看