题目链接

  1. /*
  2. 25832 kb 26964 ms
  3. 莫队+树状数组:增加/删除/查询 都是O(logn)的,总时间复杂度O(m*sqrt(n)*logn),卡不过
  4. 莫队+分块:这样查询虽然变成了sqrt(n),但是修改是O(1)的
  5. 考虑对权值进行分块
  6. 细节...
  7. */
  8. #include<cmath>
  9. #include<cstdio>
  10. #include<cctype>
  11. #include<algorithm>
  12. using namespace std;
  13. const int N=1e5+5,M=1e6+5;
  14. int n,m,A[N],size,Ans[M],belong[N],times[N],bloans[N];
  15. struct Ques
  16. {
  17. int l,r,a,b,id;
  18. bool operator <(const Ques &x)const
  19. {
  20. return belong[l]==belong[x.l] ? r<x.r : l<x.l;
  21. }
  22. }q[M];
  23. inline int read()
  24. {
  25. int now=0,f=1;register char c=getchar();
  26. for(;!isdigit(c);c=getchar())
  27. if(c=='-') f=-1;
  28. for(;isdigit(c);now=now*10+c-'0',c=getchar());
  29. return now*f;
  30. }
  31. int Query(int l,int r)
  32. {
  33. int res=0,t=min(r,belong[l]*size);
  34. for(int i=l;i<=t;++i)
  35. if(times[i]) ++res;
  36. if(belong[l]!=belong[r])
  37. for(int i=(belong[r]-1)*size+1;i<=r;++i)
  38. if(times[i]) ++res;
  39. for(int i=belong[l]+1;i<belong[r];++i)
  40. res+=bloans[i];
  41. return res;
  42. }
  43. inline void Add(int p)
  44. {
  45. if(!times[p]) ++bloans[belong[p]];
  46. ++times[p];
  47. }
  48. inline void Subd(int p)
  49. {
  50. --times[p];
  51. if(!times[p]) --bloans[belong[p]];
  52. }
  53. int main()
  54. {
  55. n=read(),m=read();size=sqrt(n);
  56. for(int i=1;i<=n;++i)
  57. A[i]=read(), belong[i]=(i-1)/size+1;
  58. for(int i=1;i<=m;++i)
  59. q[i].l=read(), q[i].r=read(), q[i].a=read(), q[i].b=read(), q[i].id=i;
  60. sort(q+1,q+1+m);
  61. for(int i=1,l=1,r=0;i<=m;++i)
  62. {
  63. while(l<q[i].l) Subd(A[l++]);
  64. while(l>q[i].l) Add(A[--l]);
  65. while(r<q[i].r) Add(A[++r]);
  66. while(r>q[i].r) Subd(A[r--]);
  67. Ans[q[i].id]=Query(q[i].a,q[i].b);
  68. }
  69. for(int i=1;i<=m;++i)
  70. printf("%d\n",Ans[i]);
  71. return 0;
  72. }

2019.11.14:

  1. //2.04s 25.05MB 没以前跑得快...自闭了
  2. #include <cmath>
  3. #include <cstdio>
  4. #include <cctype>
  5. #include <algorithm>
  6. #define gc() getchar()
  7. //#define MAXIN 500000
  8. //#define gc() (SS==TT&&(TT=(SS=IN)+fread(IN,1,MAXIN,stdin),SS==TT)?EOF:*SS++)
  9. typedef long long LL;
  10. const int N=1e5+5,M=1e6+5;
  11. int size,bel[N],val[N],bloans[N],times[N],Ans[M];
  12. //char IN[MAXIN],*SS=IN,*TT=IN;
  13. struct Queries
  14. {
  15. int l,r,a,b,id;
  16. bool operator <(const Queries &x)const
  17. {
  18. return bel[l]==bel[x.l]?r<x.r:l<x.l;
  19. }
  20. }q[M];
  21. inline int read()
  22. {
  23. int now=0;register char c=gc();
  24. for(;!isdigit(c);c=gc());
  25. for(;isdigit(c);now=now*10+c-48,c=gc());
  26. return now;
  27. }
  28. inline void Add(int x)
  29. {
  30. !times[x]&&(++bloans[bel[x]]), ++times[x];
  31. }
  32. inline void Subd(int x)
  33. {
  34. --times[x], !times[x]&&(--bloans[bel[x]]);
  35. }
  36. inline int Query(int l,int r)
  37. {
  38. int res=0;
  39. for(int i=bel[l]+1; i<bel[r]; ++i) res+=bloans[i];
  40. for(int i=l,t=std::min(bel[l]*size,r); i<=t; ++i) res+=(times[i]!=0);
  41. if(bel[l]!=bel[r]) for(int i=(bel[r]-1)*size+1; i<=r; ++i) res+=(times[i]!=0);
  42. return res;
  43. }
  44. int main()
  45. {
  46. const int n=read(),m=read(),size=sqrt(n); ::size=size;
  47. for(int i=1; i<=n; ++i) val[i]=read(), bel[i]=(i-1)/size+1;
  48. for(int i=1; i<=m; ++i) q[i]=(Queries){read(),read(),read(),read(),i};
  49. std::sort(q+1,q+1+m);
  50. for(int i=1,l=1,r=0; i<=m; ++i)
  51. {
  52. int ln=q[i].l,rn=q[i].r;
  53. while(l>ln) Add(val[--l]);
  54. while(r<rn) Add(val[++r]);
  55. while(l<ln) Subd(val[l++]);
  56. while(r>rn) Subd(val[r--]);
  57. Ans[q[i].id]=Query(q[i].a,q[i].b);
  58. }
  59. for(int i=1; i<=m; printf("%d\n",Ans[i++]));
  60. return 0;
  61. }

BZOJ.3809.Gty的二逼妹子序列(分块 莫队)的更多相关文章

  1. BZOJ 3809 Gty的二逼妹子序列(莫队+分块)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3809 [题目大意] 给定一个长度为n(1<=n<=100000)的正整数序 ...

  2. bzoj 3809 Gty的二逼妹子序列(莫队算法,块状链表)

    [题意] 回答若干个询问,(l,r,a,b):区间[l,r]内权值在[a,b]的数有多少[种]. [思路] 考虑使用块状链表实现莫队算法中的插入与删除. 因为权值处于1..n之间,所以我们可以建一个基 ...

  3. 【BZOJ 3809】 3809: Gty的二逼妹子序列 (莫队+分块)

    3809: Gty的二逼妹子序列 Time Limit: 80 Sec  Memory Limit: 28 MBSubmit: 1728  Solved: 513 Description Autumn ...

  4. 【BZOJ-3809】Gty的二逼妹子序列 分块 + 莫队算法

    3809: Gty的二逼妹子序列 Time Limit: 80 Sec  Memory Limit: 28 MBSubmit: 1072  Solved: 292[Submit][Status][Di ...

  5. BZOJ 3809: Gty的二逼妹子序列

    3809: Gty的二逼妹子序列 Time Limit: 80 Sec  Memory Limit: 28 MBSubmit: 1387  Solved: 400[Submit][Status][Di ...

  6. Bzoj 3809: Gty的二逼妹子序列 莫队,分块

    3809: Gty的二逼妹子序列 Time Limit: 35 Sec  Memory Limit: 28 MBSubmit: 868  Solved: 234[Submit][Status][Dis ...

  7. [bzoj3809]Gty的二逼妹子序列_莫队_分块

    Gty的二逼妹子序列 bzoj-3809 题目大意:给定一个n个正整数的序列,m次询问.每次询问一个区间$l_i$到$r_i$中,权值在$a_i$到$b_i$之间的数有多少个. 注释:$1\le n\ ...

  8. BZOJ 3809 Gty的二逼妹子序列 莫队算法+分块

    Description Autumn和Bakser又在研究Gty的妹子序列了!但他们遇到了一个难题. 对于一段妹子们,他们想让你帮忙求出这之内美丽度∈[a,b]的妹子的美丽度的种类数. 为了方便,我们 ...

  9. [ AHOI 2013 ] 作业 & [ BZOJ 3809 ] Gty的二逼妹子序列

    \(\\\) Description 给出一个长为 \(n\) 的数列 \(A\) 和 \(k\),多次询问: 对于一个区间 \([L_i,R_i]\),问区间内有多少个数在 \([a_i,b_i]\ ...

随机推荐

  1. Python3 configparse模块(配置)

    ConfigParser模块在python中是用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section),每个节可以有多个参数(键=值). 注意:在 ...

  2. 【转】Python数据类型之“序列概述与基本序列类型(Basic Sequences)”

    [转]Python数据类型之“序列概述与基本序列类型(Basic Sequences)” 序列是指有序的队列,重点在"有序". 一.Python中序列的分类 Python中的序列主 ...

  3. springboot系列六、springboot配置错误页面及全局异常

    一.spring1.x中处理方式 @Bean public EmbeddedServletContainerCustomizer containerCustomizer() { return new ...

  4. mybatis框架之foreach标签

    foreach一共有三种类型,分别为List,[](array),Map三种,下面表格是我总结的各个属性的用途和注意点. foreach属性 属性 描述 item 循环体中的具体对象.支持属性的点路径 ...

  5. javaScript——原型继承四步曲

    <script> //js模拟类的创建以及继承 //第一步:创建父类 function Parent(name){ this.name = name; } //给父类添加属性方法 Pare ...

  6. Lodash JavaScript 实用工具库

    地址:https://www.lodashjs.com/ Lodash 是一个一致性.模块化.高性能的 JavaScript 实用工具库.

  7. lnmp创建站点

    一.创建站点 1.输入命令 lnmp vhost add 输入域名 www.xxx.com 回车 回车 回车 y创建 n不创建 网站如果有目录权限 更改目录权限 chown -R www:www /h ...

  8. wpf 来回拉动滚动条抛异常

    其中的控件,来回快速的来动滚动条,抛如下异常,但是完全代码捕捉不到. 这个树用到了VirtualizingStackPanel.IsVirtualizing="True".去掉该句 ...

  9. python 全栈开发,Day110(django ModelForm,客户管理之 编辑权限(一))

    昨日内容回顾 1. 简述权限管理的实现原理. 粒度控制到按钮级别的权限控制 - 用户登陆成功之后,将权限和菜单信息放入session - 每次请求时,在中间件中做权限校验 - inclusion_ta ...

  10. python 全栈开发,Day1(python介绍,变量,if,while)

    python基础一 一,Python介绍 python的出生与应用 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆(中文名字:龟叔)为 ...