题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4417

 
Problem Description
Mario is world-famous plumber. His “burly” figure and amazing jumping ability reminded in our memory. Now the poor princess is in trouble again and Mario needs to save his lover. We regard the road to the boss’s castle as a line (the length is n), on every integer point i there is a brick on height hi. Now the question is how many bricks in [L, R] Mario can hit if the maximal height he can jump is H.
 
Input
The first line follows an integer T, the number of test data.
For each test data:
The first line contains two integers n, m (1 <= n <=10^5, 1 <= m <= 10^5), n is the length of the road, m is the number of queries.
Next line contains n integers, the height of each brick, the range is [0, 1000000000].
Next m lines, each line contains three integers L, R,H.( 0 <= L <= R < n 0 <= H <= 1000000000.)
 
Output
For each case, output "Case X: " (X is the case number starting from 1) followed by m lines, each line contains an integer. The ith integer is the number of bricks Mario can hit for the ith query.
 
Sample Input
1
10 10
0 5 2 7 5 4 3 8 7 7
2 8 6
3 5 0
1 3 1
1 9 4
0 1 0
3 5 5
5 5 1
4 6 3
1 5 7
5 7 3
 
Sample Output
Case 1:
4
0
0
3
1
2
0
1
5
1
 
主席树板子题,求区间小于等于h的数有多少,注意题目给的查询区间是从0开始的
  1. #include<iostream>
  2. #include<algorithm>
  3. using namespace std;
  4. #define ll long long
  5. #define maxn 100005
  6. int a[maxn],b[maxn],T[maxn<<],L[maxn<<],R[maxn<<],sum[maxn<<],tot;
  7. inline int update(int pre,int l,int r,int x)
  8. {
  9. int rt=++tot;
  10. L[rt]=L[pre];
  11. R[rt]=R[pre];
  12. sum[rt]=sum[pre]+;
  13. if(l<r)
  14. {
  15. int mid=l+r>>;
  16. if(x<=mid)L[rt]=update(L[pre],l,mid,x);
  17. else R[rt]=update(R[pre],mid+,r,x);
  18. }
  19. return rt;
  20. }
  21. inline int query(int u,int v,int ql,int qr,int l,int r)
  22. {
  23. if(ql<=l&&qr>=r)return sum[v]-sum[u];
  24. int mid=l+r>>,ans=;
  25. if(ql<=mid)ans+=query(L[u],L[v],ql,qr,l,mid);
  26. if(qr>mid)ans+=query(R[u],R[v],ql,qr,mid+,r);
  27. return ans;
  28. }
  29. int main()
  30. {
  31. int t;
  32. cin>>t;
  33. for(int W=;W<=t;W++)
  34. {
  35. int n,m;
  36. cin>>n>>m;
  37. for(int i=;i<=n;i++)
  38. {
  39. cin>>a[i];
  40. b[i]=a[i];
  41. }
  42. sort(b+,b++n);
  43. int len=unique(b+,b++n)-b-;
  44. T[]=sum[]=L[]=R[]=tot=;
  45. for(int i=;i<=n;i++)
  46. {
  47. int pos=lower_bound(b+,b++len,a[i])-b;
  48. T[i]=update(T[i-],,len,pos);
  49. }
  50. cout<<"Case "<<W<<":"<<endl;
  51. for(int i=;i<=m;i++)
  52. {
  53. int l,r,h;
  54. cin>>l>>r>>h;
  55. int pos=upper_bound(b+,b++len,h)-b;
  56. pos--;
  57. if(!pos)cout<<""<<endl;
  58. else
  59. {
  60. cout<<query(T[l],T[r+],,pos,,len)<<endl;
  61. }
  62. }
  63. }
  64. return ;
  65. }
 

hdu4417 主席树求区间小于等于K的更多相关文章

  1. 主席树——求区间第k个不同的数字(向右密集hdu5919)

    和向左密集比起来向右密集只需要进行小小的额修改,就是更新的时候从右往左更新.. 自己写的被卡死时间.不知道怎么回事,和网上博客的没啥区别.. /* 给定一个n个数的序列a 每次询问区间[l,r],求出 ...

  2. 主席树--动态区间第k小

    主席树--动态区间第\(k\)小 模板题在这里洛谷2617. 先对几个问题做一个总结: 阅读本文需要有主席树的基础,也就是通过区间kth的模板题. 静态整体kth: sort一下找第k小,时间复杂度\ ...

  3. poj 2104 主席树(区间第k大)

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 44940   Accepted: 14946 Ca ...

  4. A - 低阶入门膜法 - K-th Number (主席树查询区间第k小)

    题目链接:https://cn.vjudge.net/contest/284294#problem/A 题目大意:主席树查询区间第k小. 具体思路:主席树入门. AC代码: #include<i ...

  5. HDU 5919 - Sequence II (2016CCPC长春) 主席树 (区间第K小+区间不同值个数)

    HDU 5919 题意: 动态处理一个序列的区间问题,对于一个给定序列,每次输入区间的左端点和右端点,输出这个区间中:每个数字第一次出现的位子留下, 输出这些位子中最中间的那个,就是(len+1)/2 ...

  6. [csu/coj 1080]划分树求区间前k大数和

    题意:从某个区间内最多选择k个数,使得和最大 思路:首先题目给定的数有负数,如果区间前k大出现负数,那么负数不选和更大,于是对于所有最优选择,负数不会出现,所以用0取代负数,问题便转化为区间的前k大数 ...

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

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

  8. 主席树——求区间[l,r]不同数字个数的模板(向左密集 D-query)

    主席树的另一种用途,,(还有一种是求区间第k大,区间<=k的个数) 事实上:每个版本的主席树维护了每个值最后出现的位置 这种主席树不是以权值线段树为基础,而是以普通的线段树为下标的 /* 无修改 ...

  9. hdu 5919--Sequence II(主席树--求区间不同数个数+区间第k大)

    题目链接 Problem Description Mr. Frog has an integer sequence of length n, which can be denoted as a1,a2 ...

随机推荐

  1. H3C 多路径网络中环路产生过程(1)

  2. linux scull 函数open 方法

    open 方法提供给驱动来做任何的初始化来准备后续的操作. 在大部分驱动中, open 应当 进行下面的工作: 检查设备特定的错误(例如设备没准备好, 或者类似的硬件错误 如果它第一次打开, 初始化设 ...

  3. 配置DNS代理

  4. 1134 最长上升子序列 (序列型 DP)

    思路: 由于一般的动态规划时间复杂度是O(n^2)(哈哈哈哈 第一次用的就是这个!)用在这里由于n最大为50000 所以会超时 到这里我们可以用一个数组来动态维护这个最长上升的子序列,将你要输入的子序 ...

  5. activiti工作流引擎学习(三)

    5.接收任务活动(receiveTask,即等待活动)不是一个任务节点 接收任务是一个简单任务,他会等待回应消息的到达,当前,官方只实现了这个任务的java语义,当流程达到接受任务,流程状态会保存到数 ...

  6. 相似文本文档分析之SimHash算法

    Simhash算法: Simhash算法由Google的Charikar提出,是将一篇文档转化为n位的签名,通过比较签名的相似度来计算原文档的相似度.签名越相近,则文档越相近.因此,整个过程就不会涉及 ...

  7. 关于electron中入口文件main.js一些重要参数(持续更新maybe)

    const {app, BrowserWindow} = require('electron') const path = require('path') let mainWindow functio ...

  8. windows下的redis和redismyadmin

    redis默认是16个数据库,从0-15 由于项目需要,我使用了19号数据库,然而再向19号数据库添加数据的时候,通过redismyadmin查看发现添加到19号数据库的数据会同步到0,16,17,1 ...

  9. SSI(服务器端嵌入)

    简介 SSI(服务器端嵌入)是一组放在 HTML 页面中的指令,当服务器向客户端访问提供这些页面时,会解释执行这些指令.它们能为已有的 HTML 页面添加动态生成内容,不需要通过 CGI 程序来或其他 ...

  10. Python3 安装pylint 及与PyCharm关联

    使用如下命令: pip3 install pylint 安装完后可以看到在你的python3的目录底下的Scripts目录下有pylint.exe了 然后就可以使用pylint 评估你的代码了,如: ...