zoukankan      html  css  js  c++  java
  • 【BZOJ3809】Gty的二逼妹子序列 [莫队][分块]

    Gty的二逼妹子序列

    Time Limit: 80 Sec  Memory Limit: 28 MB
    [Submit][Status][Discuss]

    Description

      Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了一个难题。
      对于一段妹子们,他们想让你帮忙求出这之内美丽度∈[a,b]的妹子的美丽度的种类数。
      为了方便,我们规定妹子们的美丽度全都在[1,n]中。
      给定一个长度为n的正整数序列s(1<=si<=n),对于m次询问“l,r,a,b”,每次输出sl...sr中,权值∈[a,b]的权值的种类数。

    Input

      第一行包括两个整数n,m,表示数列s中的元素数和询问数。
      第二行包括n个整数s1...sn(1<=si<=n)。
      接下来m行,每行包括4个整数l,r,a,b(1<=l<=r<=n,1<=a<=b<=n),意义见题目描述。
      保证涉及的所有数在C++的int内。
      保证输入合法。

    Output

      对每个询问,单独输出一行,表示sl...sr中权值∈[a,b]的权值的种类数。

    Sample Input

      10 10
      4 4 5 1 4 1 5 1 2 1
      5 9 1 2
      3 4 7 9
      4 4 2 5
      2 3 4 7
      5 10 4 4
      3 9 1 1
      1 4 5 9
      8 9 3 3
      2 2 1 6
      8 9 1 4

    Sample Output

      2
      0
      0
      2
      1
      1
      1
      0
      1
      2

    HINT

      1<=n<=100000,1<=m<=1000000

    Main idea

      求区间[l,r]内,权值在[a,b]内的权值种数。

    Solution

      我们直接运用莫队算法,对权值分块,记录C[x]表示权值x的个数Bc[x]表示块x权值在a~b中的种数

    Code

     1 #include<iostream>  
     2 #include<string>  
     3 #include<algorithm>  
     4 #include<cstdio>  
     5 #include<cstring>  
     6 #include<cstdlib>  
     7 #include<cmath>
     8 using namespace std; 
     9 typedef long long s64;
    10 
    11 const int ONE = 100005;
    12 const int INF = 2147483640;
    13 
    14 int n,m,Q;
    15 int a[ONE],block[ONE];
    16 int C[ONE],Bc[ONE];
    17 int Ans[ONE*10];
    18 
    19 struct power
    20 {
    21         int id;
    22         int l,r,a,b;
    23 }oper[ONE*10];
    24 
    25 int get() 
    26 {
    27         int res=1,Q=1;  char c;
    28         while( (c=getchar())<48 || c>57)
    29         if(c=='-')Q=-1;
    30         if(Q) res=c-48; 
    31         while((c=getchar())>=48 && c<=57) 
    32         res=res*10+c-48;
    33         return res*Q; 
    34 }
    35 
    36 int cmp(const power &a,const power &b)
    37 {
    38         if(block[a.l] != block[b.l]) return block[a.l] < block[b.l];
    39         return a.r < b.r;
    40 }
    41 
    42 void increa(int x) {C[x]++; if(C[x]==1) Bc[block[x]]++;}
    43 void reduce(int x) {C[x]--; if(C[x]==0) Bc[block[x]]--;}
    44 
    45 int Query(int a,int b)
    46 {
    47         int res = 0;
    48         if(block[a] == block[b])
    49         {
    50             for(int i=a;i<=b;i++)
    51                 res += C[i]>=1;
    52             return res;
    53         }
    54         
    55         for(int i=block[a]+1; i<=block[b]-1; i++) res += Bc[i];
    56         for(int i=a; i<=block[a]*Q; i++)        res += C[i]>=1;
    57         for(int i=(block[b]-1)*Q+1; i<=b; i++)    res += C[i]>=1;
    58         return res;
    59 }
    60 
    61 int main()
    62 {
    63         n = get();    m = get();
    64         Q = sqrt(n);
    65         for(int i=1;i<=n;i++) a[i]=get(), block[i] = (i-1)/Q+1;
    66         
    67         for(int i=1;i<=m;i++)
    68         {
    69             oper[i].id = i;
    70             oper[i].l = get();    oper[i].r = get();
    71             oper[i].a = get();    oper[i].b = get();
    72         }
    73         sort(oper+1, oper+m+1, cmp);
    74         
    75         int l = 1, r = 0;
    76         for(int i=1;i<=m;i++)
    77         {
    78             while(r < oper[i].r) increa(a[++r]);
    79             while(oper[i].l < l) increa(a[--l]);
    80             while(r > oper[i].r) reduce(a[r--]);
    81             while(oper[i].l > l) reduce(a[l++]);
    82             Ans[oper[i].id] = Query(oper[i].a, oper[i].b);
    83         }
    84         
    85         for(int i=1;i<=m;i++)
    86             printf("%d
    ", Ans[i]);
    87         
    88 }
    View Code
  • 相关阅读:
    linux下oracle启动关闭
    win10安装JDK详细教程
    Spring MVC中用@ResponseBody转json,对json进行处理方法汇总
    js实现横向跑马灯效果
    Oracle的ORA-02292报错:违反完整性约束,已找到子记录
    echarts中legend如何换行
    java中split特殊符号
    Tomcat开启SSL协议支持
    Oracle获取表字段名,字段类型,字段长度,注释
    Oracle根据符合条件的数据循环批量更新
  • 原文地址:https://www.cnblogs.com/BearChild/p/6728962.html
Copyright © 2011-2022 走看看