Super Mario

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1437    Accepted Submission(s): 690

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
 
Source
 
Recommend
liuyiding
 

这题就是查询一个区间内小于等于一个数的数的个数。

用树状数组离线搞过。

修改下划分树模板也可以搞定

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <algorithm>
  4. #include <string.h>
  5. using namespace std;
  6. const int MAXN = ;
  7. int tree[][MAXN];
  8. int sorted[MAXN];
  9. int toleft[][MAXN];
  10.  
  11. void build(int l,int r,int dep)
  12. {
  13. if(l == r)return;
  14. int mid = (l+r)>>;
  15. int same = mid-l+;
  16. for(int i = l;i <= r;i++)
  17. if(tree[dep][i] < sorted[mid])
  18. same--;
  19. int lpos = l;
  20. int rpos = mid+;
  21. for(int i = l;i <= r;i++)
  22. {
  23. if(tree[dep][i] < sorted[mid])
  24. tree[dep+][lpos++] = tree[dep][i];
  25. else if(tree[dep][i] == sorted[mid] && same > )
  26. {
  27. tree[dep+][lpos++] = tree[dep][i];
  28. same--;
  29. }
  30. else
  31. tree[dep+][rpos++] = tree[dep][i];
  32. toleft[dep][i] = toleft[dep][l-] + lpos - l;
  33. }
  34. build(l,mid,dep+);
  35. build(mid+,r,dep+);
  36. }
  37. //查询区间[l,r]上比k小于等于的数的个数
  38. int query(int L,int R,int l,int r,int dep,int k)
  39. {
  40. //printf("%d %d %d %d %d %d\n",L,R,l,r,dep,k);
  41. if(l == r)
  42. {
  43. if(tree[dep][l] <= k)return ;
  44. else return ;
  45. }
  46. int mid = (L+R)>>;
  47. int cnt = toleft[dep][r] - toleft[dep][l-];
  48. if(sorted[mid] <= k)
  49. {
  50. int newr = r + toleft[dep][R] - toleft[dep][r];
  51. int newl = newr - (r-l+-cnt) + ;
  52. return cnt + query(mid+,R,newl,newr,dep+,k);
  53. }
  54. else
  55. {
  56. int newl = L + toleft[dep][l-] - toleft[dep][L-];
  57. int newr = newl + cnt -;
  58. if(newr >= newl)return query(L,mid,newl,newr,dep+,k);
  59. else return ;
  60. }
  61. }
  62. int main()
  63. {
  64. int T;
  65. int iCase = ;
  66. scanf("%d",&T);
  67. int n;
  68. while(T--)
  69. {
  70. iCase ++;
  71. int m;
  72. scanf("%d%d",&n,&m);
  73. memset(tree,,sizeof(tree));
  74. memset(toleft,,sizeof(toleft));
  75. for(int i = ;i <= n;i++)
  76. {
  77. scanf("%d",&tree[][i]);
  78. sorted[i] = tree[][i];
  79. }
  80. sort(sorted+,sorted+n+);
  81. build(,n,);
  82. int L,R,H;
  83. printf("Case %d:\n",iCase);
  84. while(m--)
  85. {
  86. scanf("%d%d%d",&L,&R,&H);
  87. L++;R++;
  88. printf("%d\n",query(,n,L,R,,H));
  89. }
  90. }
  91. return ;
  92. }

HDU 4417 Super Mario(划分树)的更多相关文章

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

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

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

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

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

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

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

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

  5. HDU 4417 Super Mario 主席树

    分析:找一个区间里小于等于h的数量,然后这个题先离散化一下,很简单 然后我写这个题主要是熟悉一下主席树,其实这个题完全可以离线做,很简单 但是学了主席树以后,我发现,在线做,一样简单,而且不需要思考 ...

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

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

  7. HDU 4417 Super Mario (划分树)(二分)

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

  8. 【划分树+二分】HDU 4417 Super Mario

    第一次 耍划分树.. . 模板是找第k小的 #include <stdio.h> #include <string.h> #include <stdlib.h> # ...

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

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

随机推荐

  1. gnu app url[web][5星]

    http://www.gnu.org/software/software.zh-cn.html http://linux.chinaunix.net/news/2010/12/07/1175310.s ...

  2. python的时间和日期--time、datetime应用

    time >>> import time >>> time.localtime() #以time.struct_time类型,打印本地时间 time.struct_ ...

  3. 2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6156 数位DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6156 题意:如题. 解法:数位DP,暴力枚举进制之后,就转化成了求L,R区间的回文数的个数,这个直接做 ...

  4. 利用python对WiderFace数据解析及画框

    #注:此代码稍作修改也可以用于WFLW人脸数据集的标注文件解析,#参见其README.md文件了解其每一行的信息,从中解析出相应字#段即可. import os import cv2 def draw ...

  5. Ruby-Clamp

    require "clamp" class ClampTest < Clamp::Command # 1.命令行的参数使用主要分两类,一种是参数名称后面带参数值的方式, #我 ...

  6. cpu占用高 20180108

    1.top 中的mysql占用高,在mysql中开启慢查询,用tail -f  监控慢查询日志,发现是有表的索引不合理: 2.top 中的php_fpm的进程数高,修改了一下php_fpm的配置文件p ...

  7. tomcat - gc日志输出

    原创 2017年01月04日 14:32:37 2090 tomcat/bin catalina.sh JAVA_OPTS='-server -Xms4g -Xmx4g -Xss256k -XX:Pe ...

  8. redis之(六)redis的列表类型的命令

    [一]向列表两端添加元素 -->命令:LPUSH key value [value ...] -->向列表的左侧添加元素,返回值表示增加元素后列表的长度 -->命令:RPUSH ke ...

  9. Windows7 + OSG3.6 + VS2017 + Qt5.11

    一.准备工作 下载需要的材料: 1. OSG稳定版源代码, 3.6.3版本 2. 第三方库,选择VS2017对应的版本  https://download.osgvisual.org/3rdParty ...

  10. AC日记——Mato的文件管理 bzoj 3289

    3289 思路: 莫队求区间逆序对个数,树状数组维护: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 500 ...