分析:找一个区间里小于等于h的数量,然后这个题先离散化一下,很简单

然后我写这个题主要是熟悉一下主席树,其实这个题完全可以离线做,很简单

但是学了主席树以后,我发现,在线做,一样简单,而且不需要思考

(主席树大法好)无限仰慕

  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <algorithm>
  5. using namespace std;
  6. const int N = 1e5+;
  7. const int INF=0x3f3f3f3f;
  8. typedef unsigned long long ULL;
  9. typedef long long LL;
  10. int n,m,a[N],c[N],T;
  11. int root[N],sz;
  12. struct Node{
  13. int l,r,v;
  14. }o[N*];
  15. void update(int &rt,int l,int r,int pos){
  16. o[++sz]=o[rt],rt=sz;
  17. ++o[rt].v;
  18. if(l==r)return;
  19. int mid=(l+r)>>;
  20. if(pos<=mid)update(o[rt].l,l,mid,pos);
  21. else update(o[rt].r,mid+,r,pos);
  22. }
  23. int query(int rt1,int rt2,int l,int r,int x,int y){
  24. if(x<=l&&r<=y)
  25. return o[rt2].v-o[rt1].v;
  26. int mid=(l+r)>>;
  27. int ans=;
  28. if(x<=mid)ans+=query(o[rt1].l,o[rt2].l,l,mid,x,y);
  29. if(y>mid)ans+=query(o[rt1].r,o[rt2].r,mid+,r,x,y);
  30. return ans;
  31. }
  32. int main(){
  33. int cas=;
  34. scanf("%d",&T);
  35. while(T--){
  36. printf("Case %d:\n",++cas);
  37. scanf("%d%d",&n,&m);
  38. for(int i=;i<=n;++i)
  39. scanf("%d",&a[i]),c[i]=a[i];
  40. sort(a+,a++n);
  41. int cnt=unique(a+,a++n)-a-;
  42. root[]=sz=;
  43. for(int i=;i<=n;++i){
  44. c[i]=lower_bound(a+,a++cnt,c[i])-a;
  45. update(root[i]=root[i-],,cnt,c[i]);
  46. }
  47. for(int i=;i<=m;++i){
  48. int l,r,h;
  49. scanf("%d%d%d",&l,&r,&h),++l,++r;
  50. h=upper_bound(a+,a++cnt,h)-a;
  51. if(h==){
  52. printf("0\n");
  53. continue;
  54. }
  55. printf("%d\n",query(root[l-],root[r],,cnt,,h-));
  56. }
  57. }
  58. return ;
  59. }

HDU 4417 Super Mario 主席树的更多相关文章

  1. HDU 4417 Super Mario 主席树查询区间小于某个值的个数

    #include<iostream> #include<string.h> #include<algorithm> #include<stdio.h> ...

  2. HDU 4417 Super Mario(划分树)

    Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  3. HDU 4417 Super Mario(划分树问题求不大于k的数有多少)

    Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  4. HDU 4417 - Super Mario ( 划分树+二分 / 树状数组+离线处理+离散化)

    题意:给一个数组,每次询问输出在区间[L,R]之间小于H的数字的个数. 此题可以使用划分树在线解决. 划分树可以快速查询区间第K小个数字.逆向思考,判断小于H的最大的一个数字是区间第几小数,即是答案. ...

  5. HDU 4417 Super Mario ( 离线树状数组 )

    把数值和查询放在一起从小到大排序,纪录每个数值的位置,当遇到数值时就更新到树状数组中,遇到查询就直接查询该区间和. #include <cstdio> #include <cstri ...

  6. HDU 4417 Super Mario(划分树+二分)

    题目链接 #include <cstdio> #include <cstring> #include <algorithm> using namespace std ...

  7. HDU 4417 Super Mario(主席树求区间内的区间查询+离散化)

    Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  8. 主席树:HDU 4417 Super Mario

    Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  9. hdu 4417 Super Mario (主席树)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=4417 题意: 给你段长为n的序列,有q个询问,每次询问区间[l.r]内有多少个数小于等于k 思路: 之前用 ...

随机推荐

  1. Understanding Manycore Scalability of File Systems

    多核场景下,不同文件系统,文件操作的性能评估.

  2. Linux C 程序 基础(FOUR)

    1.标识符:C语言本身不限制变量长度,但是某些编译器会限制变量长度,命名最好不要超过8位.         以数字开头,保留字,*,空格非法 2.关键字:类型说明符,int , 语句定义符,if el ...

  3. Web前端新人笔记之height、min-height的区别

     浏览器参照基准:Firefox, Chrome, Safari, Opera, IE: * IE6不支持CSS min-height属性.最小高度的定义:1. 元素拥有默认高度:2. 当内容超出元素 ...

  4. IE兼容性问题

    1.H5标签兼容.解决:js:document.createElement("footer");css:display: block;或者直接使用    html5shiv.js ...

  5. 51nod贪心算法入门-----任务分配问题

    任务执行顺序 有N个任务需要执行,第i个任务计算时占R[i]个空间,而后会释放一部分,最后储存计算结果需要占据O[i]个空间(O[i] < R[i]). 分析: 可以抽象成,从一个整数开始,每次 ...

  6. Python 基础篇:编码、变量、模块

    1. 编码 python解释器在加载 .py 文件中的代码时,会对内容进行编码(默认ASCII). 2. 变量 变量定义的规则: 变量名只能是 字母.数字或下划线的任意组合 变量名的第一个字符不能是数 ...

  7. CentOS安装SetupTools(easy_install)

    确保Py版本在2.6或以上 (旧版本需升级或参考旧版本安装) cd /opt wget https://pypi.python.org/packages/source/s/setuptools/set ...

  8. Python设计模式——模版方法模式

    1.模版方法模式 做题的列子: 需求:有两个学生,要回答问题,写出自己的答案 #encoding=utf-8 __author__ = 'kevinlu1010@qq.com' class Stude ...

  9. stream的Read、Write方法实例

    , bytes.Length)) > ) , readBytes-);//8为偏移量,10为数量                    }                }            ...

  10. hdu 2191 珍惜现在,感恩生活 多重背包入门题

    背包九讲下载CSDN 背包九讲内容 多重背包: hdu 2191 珍惜现在,感恩生活 多重背包入门题 使用将多重背包转化为完全背包与01背包求解: 对于w*num>= V这时就是完全背包,完全背 ...