【题目链接】 http://www.lydsy.com/JudgeOnline/problem.php?id=3809

【题目大意】

  给定一个长度为n(1<=n<=100000)的正整数序列s(1<=si<=n),
  对于m(1<=m<=1000000)次询问“l,r,a,b”,
  每次输出sl...sr中,权值∈[a,b]的权值的种类数。

【题解】

  用莫队维护,对于修改操作用树状数组,
  发现复杂度为O(msqrtnlogn),难以接受,
  考虑用分块处理整数序列,修改操作O(1),查询操作O(sqrt(n))
  总复杂度O(msqrt(n)+mlog(m))

【代码】

  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <cmath>
  4. const int N=100001,M=1000001;
  5. using namespace std;
  6. typedef long long LL;
  7. int size[N],ans[M],pos[N],color[N],c[400],n,m,limit,bl[400],br[400];
  8. struct Q{
  9. int l,r,id,a,b;
  10. friend bool operator < (const Q &a,const Q &b){
  11. return pos[a.l]<pos[b.l]||(pos[a.l]==pos[b.l]&&a.r<b.r);
  12. }
  13. }ask[M];
  14. bool cmp(const Q &a,const Q &b){return a.id<b.id;}
  15. void read(int&a){
  16. char ch;while(!((ch=getchar())>='0')&&(ch<='9'));
  17. a=ch-'0';while(((ch=getchar())>='0')&&(ch<='9'))a*=10,a+=ch-'0';
  18. }
  19. int query(int x,int y){
  20. int res=0;
  21. int L=pos[x],R=pos[y];
  22. for(int i=L+1;i<R;i++)res+=c[i];
  23. if(L==R){for(int i=x;i<=y;i++)if(size[i])res++;}
  24. else{
  25. for(int i=x;i<=br[L];i++)if(size[i])res++;
  26. for(int i=bl[R];i<=y;i++)if(size[i])res++;
  27. }return res;
  28. }
  29. void modify(int u,int x){
  30. if(size[color[u]]==1&&x==-1)c[pos[color[u]]]--;
  31. if(size[color[u]]==0&&x==1)c[pos[color[u]]]++;
  32. size[color[u]]+=x;
  33. }
  34. int main(){
  35. read(n);read(m);
  36. limit=(int)sqrt(n+0.5);
  37. for(int i=1;i<=n;i++){read(color[i]);pos[i]=(i-1)/limit+1;}
  38. for(int i=1;i<=n;i++){br[pos[i]]=i;if(!bl[pos[i]])bl[pos[i]]=i;}
  39. for(int i=1;i<=m;i++){read(ask[i].l);read(ask[i].r);read(ask[i].a);read(ask[i].b);ask[i].id=i;}
  40. sort(ask+1,ask+m+1);
  41. for(int i=1,l=1,r=0;i<=m;i++){
  42. while(r<ask[i].r)modify(++r,1);
  43. while(r>ask[i].r)modify(r--,-1);
  44. while(l<ask[i].l)modify(l++,-1);
  45. while(l>ask[i].l)modify(--l,1);
  46. ans[ask[i].id]=query(ask[i].a,ask[i].b);
  47. }for(int i=1;i<=m;i++)printf("%d\n",ans[i]);
  48. return 0;
  49. }

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

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

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

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

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

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

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

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

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

  5. [BZOJ3809]Gty的二逼妹子序列[莫队+分块]

    题意 给出长度为 \(n\) 的序列,\(m\) 次询问,每次给出 \(l,r,a,b\) ,表示询问区间 \([l,r]\) 中,权值在 \([a,b]\) 范围的数的种类数. \(n\leq 10 ...

  6. 【BZOJ3809】Gty的二逼妹子序列 莫队 分块

    题目描述 给你一个长度为\(n\)的数列,还有\(m\)个询问,对于每个询问\((l,r,a,b)\),输出区间\([l,r]\)有多少范围在\([a,b]\)的权值. \(n\leq 100000, ...

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

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

  8. [AHOI2013]作业 & Gty的二逼妹子序列 莫队

    ---题面--- 题解: 题目要求统计一个区间内数值在[a, b]内的数的个数和种数,而这个是可以用树状数组统计出来的,所以可以考虑莫队. 考虑区间[l, r]转移到[l, r + 1],那么对于维护 ...

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

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

随机推荐

  1. 关于cocos2d-x 中 CCEditBox 的输入位置和IOS虚拟键盘位置不重合的bug

    这个文章的名字起的实在是有点长,主要是怕说不清楚. 在IOS上,输入时,我看到过的比较少,就两种,如附件两张图.一个是虚拟键盘的输入框完全是在CCEditBox上,另一张虚拟键盘的输入区域是紧挨着键盘 ...

  2. Python 源码学习之内存管理 -- (转)

    Python 的内存管理架构(Objects/obmalloc.c): _____ ______ ______ ________ [ int ] [ dict ] [ list ] ... [ str ...

  3. 在使用ubuntu16.04+python3.5 下使用pip3出现pip3 error - '_NamespacePath' object has no attribute 'sort'

    使用pip3安装tensorflow以及gensim等时,出现如下错误: Traceback (most recent call last): File "/usr/local/bin/pi ...

  4. ribbon使用eureka的meta进行动态路由

    序 使用eureka的元数据信息,再配上ribbon的路由功能,就可以在api-gateway实现很多功能,比如灰度测试.生产调试等等.下面介绍一下,怎么使用jmnarloch大神提供的ribbon- ...

  5. arduino 用电位器调节LED闪烁频率

    int dianwei; int led = 13; void setup() {  // put your setup code here, to run once:  Serial.begin(9 ...

  6. Python脚本 - 常用单位转换

    测试系统为:Centos 6.7 Python版本为: 3.6.4 脚本功能:常用单位的转换,这里用内存来模拟 import pstuil def bytes2human(n): symbols = ...

  7. Linux C中内联汇编的语法格式及使用方法(Inline Assembly in Linux C)【转】

    转自:http://www.linuxidc.com/Linux/2013-06/85221p3.htm 阅读Linux内核源码或对代码做性能优化时,经常会有在C语言中嵌入一段汇编代码的需求,这种嵌入 ...

  8. java===java基础学习(5)---文件读取,写入操作

    文件的写入读取有很多方法,今天学到的是Scanner和PrintWriter 文件读取 Scanner in = new Scanner(Paths.get("file.txt") ...

  9. monkey测试===通过monkey测试检查app内存泄漏和cpu占用

    最近一直在研究monkey测试.网上资料很多,但都是一个抄一个的.原创的很少 我把检查app内存泄漏的情况梳理一下: 参考资料: Monkey测试策略:https://testerhome.com/t ...

  10. C# 笔记——覆盖和重写

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...