3809: Gty的二逼妹子序列

链接

分析:

  和这道AHOI2013 作业差不多。权值是1~n的,所以对权值进行分块。$O(1)$修改,$O(\sqrt n)$查询。

代码:

  1. #include<cstdio>
  2. #include<algorithm>
  3. #include<cstring>
  4. #include<iostream>
  5. #include<cmath>
  6. #include<cctype>
  7. #include<set>
  8. #include<queue>
  9. #include<vector>
  10. #include<map>
  11. using namespace std;
  12. typedef long long LL;
  13.  
  14. inline int read() {
  15. int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
  16. for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
  17. }
  18.  
  19. const int N = ;
  20.  
  21. int bel[N], sum[N], a[N], Ans[N * ], cnt[N], B;
  22. struct Que{
  23. int l, r, a, b, id;
  24. bool operator < (const Que &A) const {
  25. return bel[l] == bel[A.l] ? r < A.r : bel[l] < bel[A.l];
  26. }
  27. }Q[N * ];
  28.  
  29. inline void add(int x) {
  30. cnt[x] ++;
  31. if (cnt[x] == ) sum[bel[x]] ++;
  32. }
  33. inline void del(int x) {
  34. cnt[x] --;
  35. if (cnt[x] == ) sum[bel[x]] --;
  36. }
  37. inline int query(int l,int r) {
  38. int res = ;
  39. for (int i = l, lim = min(r, bel[l] * B); i <= lim; ++i) res += (cnt[i] > );
  40. if (bel[l] != bel[r])
  41. for (int i = (bel[r] - ) * B + ; i <= r; ++i) res += (cnt[i] > );
  42. for (int i = bel[l] + ; i <= bel[r] - ; ++i) res += sum[i];
  43. return res;
  44. }
  45. int main() {
  46. int n = read(), m = read(); B = sqrt(n);
  47. for (int i = ; i <= n; ++i) a[i] = read();
  48. for (int i = ; i <= m; ++i)
  49. Q[i].l = read(), Q[i].r = read(), Q[i].a = read(), Q[i].b = read(), Q[i].id = i;
  50. for (int i = ; i <= n; ++i) bel[i] = (i - ) / B + ;
  51. sort(Q + , Q + m + );
  52. int L = , R = ;
  53. for (int i = ; i <= m; ++i) {
  54. while (L > Q[i].l) add(a[--L]);
  55. while (R < Q[i].r) add(a[++R]);
  56. while (L < Q[i].l) del(a[L++]);
  57. while (R > Q[i].r) del(a[R--]);
  58. Ans[Q[i].id] = query(Q[i].a, Q[i].b);
  59. }
  60. for (int i = ; i <= m; ++i) printf("%d\n",Ans[i]);
  61. return ;
  62. }

3809: Gty的二逼妹子序列的更多相关文章

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

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

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

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

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

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

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

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

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

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

  6. 【BZOJ】3809: Gty的二逼妹子序列

    http://www.lydsy.com/JudgeOnline/problem.php?id=3809 题意:n个元素(1<=n<=100000)每个元素有一权值<=n.q个询问, ...

  7. bzoj 3809 Gty的二逼妹子序列——莫队+分块

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3809 容易想到树状数组维护值域.但修改和查询都是 log 太慢. 考虑有 nsqrt(n) ...

  8. bzoj 3809 Gty的二逼妹子序列 —— 莫队+分块

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3809 据说一开始应该想到莫队+树状数组,然而我想的却是莫队+权值线段树... 如果用权值线段 ...

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

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

随机推荐

  1. luogu P3381【模板】最小费用最大流

    嘟嘟嘟 没错,我开始学费用流了! 做法也是比较朴素的\(spfa\). 就是每一次以费用为权值跑一遍\(spfa\)找到一条最短路,然后把这条道全流满,并把这一次的流量和费用累加到答案上.因此我们需要 ...

  2. Level/levelup-1-简介

    https://github.com/Level/levelup A node.js wrapper for abstract-leveldown compliant stores 一个为实现抽象le ...

  3. Apollo GPS调试笔记

    前言:在Apollo美研团队的支持下,最近完成了Apollo推荐的Novatel PP7与我们的基站调试,在这里对Apollo的笔记做一个补充,希望以后的开发者不用在踩我们踩过的坑. 在自己调导航(h ...

  4. 通讯协议(一)HTTP协议

    协议是指计算机通信网络中两台计算机之间进行通信所必须共同遵守的规定或规则,超文本传输协议(HTTP)是一种通信协议,它允许将超文本标记语言(HTML)文档从Web服务器传送到客户端的浏览器.目前我们使 ...

  5. iOS OC与JS的交互(JavaScriptCore实现)

    本文包括JS调用OC方法并传值,OC调用JS方法并传值 本来想把html放进服务器里面,然后访问,但是觉得如果html在本地加载更有助于理解,特把html放进项目里 HTML代码 <!DOCTY ...

  6. -L -Wl,-rpath-link -Wl,-rpath区别精讲

    目录 前言 源码准备 源码内容 尝试编译,保证源码没有问题 编译 首先编译world.c 编译并链接hello.c 调试编译test.c 结论 转载请注明出处,谢谢 https://www.cnblo ...

  7. $.ajax(),$.get(),$.post()的区别,以及一些参数注意规则

    $.ajax()方法和$.get(),$.post()方法的对比 $.ajax()方法是最完整的写法,可以完成所有的ajax请求(包含get类型和post类型) $.get()和$.post()都是简 ...

  8. Bootstrap源码解读之栅格化篇

    本文纯属自己研究所写笔记,如果有错误还请多多指教提出 版心(container) 版心:class名为.container的容器,其版心的宽度在各个屏幕设备下是不一样的值,版心两边就是留白. 各尺寸下 ...

  9. Elasticsearch入门和查询语法分析(ik中文分词)

    全文搜索现在已经是很常见的功能了,当然你也可以用mysql加Sphinx实现.但开源的Elasticsearch(简称ES)目前是全文搜索引擎的首选.目前像GitHub.维基百科都使用的是ES,它可以 ...

  10. android4.4 重启的开机不播放开机铃声,按power键的开机播放开机铃声

    平台:A33Android4.4Linux3.4 功能描述:实现重启的开机不播放开机铃声,按power键的开机播放开机铃声 一,无论关机还是重启都会经过rebootOrShutdown这个方法,在方法 ...