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

    ★★★   输入文件: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。

    调了1h竟然是数组开小了:

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    
    using namespace std;
    const int N=2000100;
    
    int a[N],pos[N],number[N],answer[N];
    bool vis[N];
    int n,m,ans;
    struct node{
        int l,r,num;
    }E[N<<2];
    
    inline int read()
    {
        int x=0;char c=getchar();int f=1;
        while(c<'0'||c>'9'){if(f=='-')f=-1;c=getchar();    }
        while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();
        return x;
    }
    
    bool cmp(node a,node b)
    {
        if(pos[a.l]==pos[b.l])
            return a.r<b.r;
        else
            return pos[a.l]<pos[b.l];
    }
    
    int add(int x)
    {
        number[x]++;
        if(number[x]==1)
            ans++;
    }
    
    int dale(int x)
    {
        number[x]--;
        if(number[x]==0)
            ans--;
    }
    
    int MD()
    {
        int ll=1,rr=0;
        for(int i=1;i<=m;i++)
        {
            for(;E[i].l<ll;ll--)add(a[ll-1]);
            for(;E[i].l>ll;ll++)dale(a[ll]);
            for(;E[i].r>rr;rr++)add(a[rr+1]);
            for(;E[i].r<rr;rr--)dale(a[rr]);
            answer[E[i].num]=ans;
        }
    }
    
    int main()
    {
        freopen("diff.in","r",stdin);
        freopen("diff.out","w",stdout);
        n=read();
        for(int i=1;i<=n;i++)
            a[i]=read();
        m=read();
        for(int i=1;i<=m;i++)
            {E[i].l=read();E[i].r=read();E[i].num=i;}
        int base=sqrt(n);
        for(int i=1;i<=n;i++)
            pos[i]=(i-1)/base+1;
        sort(E+1,E+m+1,cmp);
        MD();
        for(int i=1;i<=m;i++)
            printf("%d
    ",answer[i]);
        return 0;
    }
    /*
    6
    1 2 3 4 3 5
    3
    1 2
    3 5
    2 6
    */
  • 相关阅读:
    POJ 1795 DNA Laboratory
    CodeForces 303B Rectangle Puzzle II
    HDU 2197 本源串
    HDU 5965 扫雷
    POJ 3099 Go Go Gorelians
    CodeForces 762D Maximum path
    CodeForces 731C Socks
    HDU 1231 最大连续子序列
    HDU 5650 so easy
    大话接口隐私与安全 转载
  • 原文地址:https://www.cnblogs.com/lyqlyq/p/7138054.html
Copyright © 2011-2022 走看看