• 1878: [SDOI2009]HH的项 莫队算法-离线查询区间内部不同数字的个数


    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    #include<math.h>
    using namespace std;
    const int maxx = 1e6+6;
    int a[maxx];
    int vis[maxx];
    int ans[maxx];
    int block;
    int res;
    struct node
    {
        int l,r;
        int id;
        friend bool operator < (node &a,node &b)
        {
            if (a.l/block==b.l/block)
            {
                return a.r<b.r;
            }
            return a.l/block<b.l/block;
        }
    } que[maxx];
    void add(int x)
    {
        vis[a[x]]++;
        if (vis[a[x]]==1)
            res++;
    }
    void del(int x)
    {
        vis[a[x]]--;
        if (vis[a[x]]==0)
        {
            res--;
        }
    }
    int main()
    {
        int n,q;
        while(~scanf("%d",&n))
        {
            res=0;
            block=sqrt(n);
            memset(vis,0,sizeof(vis));
            for (int i=1; i<=n; i++)
            {
                scanf("%d",&a[i]);
            }
            scanf("%d",&q);
            int l=1,r=0;
            for (int i=1; i<=q; i++)
            {
                scanf("%d%d",&que[i].l,&que[i].r);
                que[i].id=i;
            }
            sort(que+1,que+1+q);
            for (int i=1; i<=q; i++)
            {
                while(l<que[i].l)del(l),l++;
                while(l>que[i].l)l--,add(l);
                while(r<que[i].r)r++,add(r);
                while(r>que[i].r)del(r),r--;
                ans[que[i].id]=res;
            }
            for (int i=1;i<=q;i++){
                if (i-1)printf(" %d",ans[i]);
                else printf("%d",ans[i]);
            }
        }
        return 0;
    }
  • 相关阅读:
    mojoportal中弹出窗口
    css 层居中
    mojoportal中添加自定义javascript
    C#执行cmd [转载]
    异步委托 学习笔记
    Windows Sysinternals
    有关int,Int32的疑惑解答
    WEB Debug tools汇总
    规范很重要
    [笔记]VGA 接口电阻网络阻抗
  • 原文地址:https://www.cnblogs.com/bluefly-hrbust/p/11526279.html
Copyright © 2020-2023  润新知