zoukankan      html  css  js  c++  java
  • BZOJ 1878: [SDOI2009]HH的项链 | 莫队

    题解:

    http://www.lydsy.com/JudgeOnline/problem.php?id=1878


    题解:

    莫队板子题

    核心思想是对区间的询问离线之后按照合理的顺序来优化复杂度

    一般的做法是先分块,以左端点所在块为第一关键字,右端点位置为第二关键字排序

    用两个指针来跑,这样可以证明的是时间复杂度为O(n√n)

    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<cmath>
    #define N 50005
    #define M 1000005
    #define Q 200005
    using namespace std;
    int n,m,S,l,r,tl,tr,tot,ans[Q],a[N],cnt[M];
    struct node
    {
        int l,r,bl,id;
        bool operator < (const node &x)const
        {
            return bl<x.bl || bl==x.bl && r<x.r;
        }
    }q[Q];
    int main()
    {
        scanf("%d",&n);S=sqrt(n);
        for  (int i=1;i<=n;i++)
        scanf("%d",a+i);
        scanf("%d",&m);
        for (int i=1;i<=m;i++)
        {
        scanf("%d%d",&l,&r);
        q[i].id=i;q[i].l=l;q[i].r=r;q[i].bl=(l-1)/S+1;
        }
        sort(q+1,q+1+m);
        for (int i=1;i<=m;i++)
        {
        l=q[i].l,r=q[i].r;
        while (tl<l) if (!--cnt[a[tl++]]) tot--;
        while (tl>l) if (!cnt[a[--tl]]++) tot++;
        while (tr<r) if (!cnt[a[++tr]]++) tot++;
        while (tr>r) if (!--cnt[a[tr--]]) tot--;
        ans[q[i].id]=tot;
        }
        for (int i=1;i<=m;i++)
        printf("%d
    ",ans[i]);
        return 0;
    }
  • 相关阅读:
    web性能优化
    9.1_the end
    8.28_the end
    1.获取元素绝对位置
    8.14_end
    JavaScript 函数用途
    JavaScirpt事件处理
    《JavaScript语言精粹》读书笔记
    《图解http协议》之HTTPs学习笔记
    Laya 1.x 按文件夹TS代码合并
  • 原文地址:https://www.cnblogs.com/mrsheep/p/8182026.html
Copyright © 2011-2022 走看看