zoukankan      html  css  js  c++  java
  • BZOJ-3339: Rmq Problem BZOJ-3585: mex (莫队算法)

     

    3339: Rmq Problem

    Time Limit: 20 Sec  Memory Limit: 128 MB
    Submit: 1362  Solved: 721
    [Submit][Status][Discuss]

    Description

    Input

    Output

    Sample Input

    7 5
    0 2 1 0 1 3 2
    1 3
    2 3
    1 4
    3 6
    2 7

    Sample Output

    3
    0
    3
    2
    4

    HINT

    Source

    By Xhr

     

    3585: mex

    Time Limit: 20 Sec  Memory Limit: 128 MB
    Submit: 1198  Solved: 615
    [Submit][Status][Discuss]

    Description

      有一个长度为n的数组{a1,a2,...,an}。m次询问,每次询问一个区间内最小没有出现过的自然数。

    Input

      第一行n,m。
      第二行为n个数。
      从第三行开始,每行一个询问l,r。

    Output

      一行一个数,表示每个询问的答案。

    Sample Input

    5 5
    2 1 0 2 1
    3 3
    2 3
    2 4
    1 2
    3 5

    Sample Output

    1
    2
    3
    0
    3

    HINT

    数据规模和约定

      对于100%的数据:

      1<=n,m<=200000

      0<=ai<=109

      1<=l<=r<=n


      对于30%的数据:


      1<=n,m<=1000

    Source

    By 佚名提供

    哈哈哈竟然BZOJ上有两道一模一样的题哈哈哈哈哈 可惜是两道权限题(⊙_⊙)

     1 #include "bits/stdc++.h"
     2 using namespace std;
     3 typedef long long LL;
     4 const int MAX=2e5+5;
     5 int n,m;
     6 int a[MAX],b[MAX],pos[MAX],an[MAX],bas,ans;
     7 struct Node{
     8     int id;
     9     int l,r;
    10     bool operator < (const Node &tt) const {
    11         if (pos[l]!=pos[tt.l])
    12             return pos[l]<pos[tt.l];
    13         return r<tt.r;
    14     }
    15 }que[MAX];
    16 inline int read(){
    17     int an=0,x=1;char c=getchar();
    18     while (c<'0' || c>'9') {if (c=='-') x=-1;c=getchar();}
    19     while (c>='0' && c<='9') {an=an*10+c-'0';c=getchar();}
    20     return an*x;
    21 }
    22 void update(int x,int y){
    23     if (a[x]>n) return;
    24     b[a[x]]+=y;
    25     if (!b[a[x]]){
    26         if (a[x]<ans)
    27             ans=a[x];
    28     }
    29     else if (a[x]==ans){
    30         while (b[ans]) ans++;
    31     }
    32 }
    33 int main(){
    34     freopen ("mex.in","r",stdin);
    35     freopen ("mex.out","w",stdout);
    36     int i,j;
    37     n=read();m=read();bas=(int)sqrt(n*1.0);
    38     for (i=1;i<=n;i++) a[i]=read(),pos[i]=i/bas;
    39     memset(b,0,sizeof(b));
    40     for (i=1;i<=m;i++){
    41         que[i].id=i;
    42         que[i].l=read(),que[i].r=read();
    43     }
    44     sort(que+1,que+m+1);
    45     int L=1,R=0;
    46     for (i=1;i<=m;i++){
    47         while (R<que[i].r) update(++R,1);
    48         while (L>que[i].l) update(--L,1);
    49         while (R>que[i].r) update(R--,-1);
    50         while (L<que[i].l) update(L++,-1);
    51         an[que[i].id]=ans;
    52     }
    53     for (i=1;i<=m;i++)
    54         printf("%d
    ",an[i]);
    55     return 0;
    56 }
    未来是什么样,未来会发生什么,谁也不知道。 但是我知道, 起码从今天开始努力, 肯定比从明天开始努力, 要快一天实现梦想。 千里之行,始于足下! ——《那年那兔那些事儿》
  • 相关阅读:
    C#中的委托(delegate)用法简介 dodo
    SqlServer2000日志文件过大问题处理 dodo
    prototype.js 显示等待状态 dodo
    linux常用命令 dodo
    关于NavigateUrl中绑定Eval()方法时出现"服务器标记的格式不正确"的解决方法 dodo
    DataGridViewRowHeadersWidthSizeMode属性和ColumnHeadersHeightSizeMode属性 dodo
    注销时跳出框架 dodo
    DriveInfo类取得计算机的磁盘信息 dodo
    类序列化 dodo
    CutEditor在线编辑器的使用 dodo
  • 原文地址:https://www.cnblogs.com/keximeiruguo/p/7680011.html
Copyright © 2011-2022 走看看