Super Mario

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

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   |   We have carefully selected several
similar problems for you:  6014 6013 6012 6011 6010 
 
 
 
思路:
  主席树+二分查找;
 
 
来,上代码:

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <iostream>
  4. #include <algorithm>
  5.  
  6. #define maxn 100005
  7.  
  8. using namespace std;
  9.  
  10. struct TreeNodeType {
  11. int lc,rc,dis;
  12. };
  13. struct TreeNodeType tree[maxn*];
  14.  
  15. int if_z,t,n,m,num[maxn],Hash[maxn],size,cnt,root[maxn];
  16.  
  17. char Cget;
  18.  
  19. inline void in(int &now)
  20. {
  21. now=,if_z=,Cget=getchar();
  22. while(Cget>''||Cget<'')
  23. {
  24. if(Cget=='-') if_z=-;
  25. Cget=getchar();
  26. }
  27. while(Cget>=''&&Cget<='')
  28. {
  29. now=now*+Cget-'';
  30. Cget=getchar();
  31. }
  32. now*=if_z;
  33. }
  34.  
  35. void tree_build(int &now,int l,int r)
  36. {
  37. now=++cnt,tree[now].dis=;
  38. if(l==r) return ;
  39. int mid=(l+r)>>;
  40. tree_build(tree[now].lc,l,mid);
  41. tree_build(tree[now].rc,mid+,r);
  42. }
  43.  
  44. void tree_add(int pre,int &now,int to,int l,int r)
  45. {
  46. now=++cnt;
  47. tree[now].dis=tree[pre].dis+;
  48. if(l==r) return ;
  49. int mid=(l+r)>>;
  50. if(to<=mid)
  51. {
  52. tree_add(tree[pre].lc,tree[now].lc,to,l,mid);
  53. tree[now].rc=tree[pre].rc;
  54. }
  55. else
  56. {
  57. tree_add(tree[pre].rc,tree[now].rc,to,mid+,r);
  58. tree[now].lc=tree[pre].lc;
  59. }
  60. }
  61.  
  62. int find(int x)
  63. {
  64. int l=,r=size,mid,ans;
  65. while(l<=r)
  66. {
  67. mid=(l+r)>>;
  68. if(x<=Hash[mid]) ans=mid,r=mid-;
  69. else l=mid+;
  70. }
  71. return ans;
  72. }
  73.  
  74. int tree_query(int pre,int now,int to,int l,int r)
  75. {
  76. if(l==r) return tree[now].dis-tree[pre].dis;
  77. int pos=tree[tree[now].lc].dis-tree[tree[pre].lc].dis;
  78. int mid=(l+r)>>;
  79. if(to<=mid) return tree_query(tree[pre].lc,tree[now].lc,to,l,mid);
  80. else return pos+tree_query(tree[pre].rc,tree[now].rc,to,mid+,r);
  81. }
  82.  
  83. int main()
  84. {
  85. in(t);
  86. for(int Case=;Case<=t;Case++)
  87. {
  88. in(n),in(m);cnt=;
  89. for(int i=;i<=n;i++) in(num[i]),Hash[i]=num[i];
  90. sort(Hash+,Hash+n+);
  91. size=unique(Hash+,Hash+n+)-Hash-;
  92. tree_build(root[],,size);
  93. for(int i=;i<=n;i++)
  94. {
  95. num[i]=lower_bound(Hash+,Hash+size+,num[i])-Hash;
  96. tree_add(root[i-],root[i],num[i],,size);
  97. }
  98. printf("Case %d:\n",Case);
  99. int u,v,w;
  100. while(m--)
  101. {
  102. in(u),in(v),in(w);
  103. if(w<Hash[])
  104. {
  105. printf("0\n");
  106. continue;
  107. }
  108. int pos=find(w);
  109. if(Hash[pos]>w) pos--;
  110. printf("%d\n",tree_query(root[u],root[v+],pos,,size));
  111. }
  112. }
  113. return ;
  114. }

AC日记——Super Mario hdu 4417的更多相关文章

  1. Super Mario HDU 4417 主席树区间查询

    Super Mario HDU 4417 主席树区间查询 题意 给你n个数(编号从0开始),然后查询区间内小于k的数的个数. 解题思路 这个可以使用主席树来处理,因为这个很类似查询区间内的第k小的问题 ...

  2. J - Super Mario HDU - 4417 线段树 离线处理 区间排序

    J - Super Mario HDU - 4417 这个题目我开始直接暴力,然后就超时了,不知道该怎么做,直接看了题解,这个习惯其实不太好. 不过网上的思路真的很厉害,看完之后有点伤心,感觉自己应该 ...

  3. Super Mario HDU - 4417 (主席树)

    Mario is world-famous plumber. His “burly” figure and amazing jumping ability reminded in our memory ...

  4. Super Mario HDU - 4417 (主席树询问区间比k小的个数)

    Mario is world-famous plumber. His “burly” figure and amazing jumping ability reminded in our memory ...

  5. AC日记——Keywords Search hdu 2222

    2222 思路: ac自动机模板题: 代码: #include <cstdio> #include <cstring> #include <iostream> #i ...

  6. AC日记——Number Sequence hdu 1711

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  7. AC日记——统计难题 hdu 1251

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

  8. AC日记——病毒侵袭 hdu 2896

    2896 思路: 好题: 代码: #include <queue> #include <cstdio> #include <cstring> using names ...

  9. AC日记——Paint Pearls hdu 5009

    Paint Pearls 思路: 离散化+dp+剪枝: dp是个n方的做法: 重要就在剪枝: 如果一个长度为n的区间,有大于根号n种颜色,还不如一个一个涂: 来,上代码: #include <c ...

随机推荐

  1. Docker 在容器中部署静态网站

    Docker 在容器中部署静态网站 在容器中部署静态网站 设置容器的端口映射 run -P``--publish-all=true|false:容器暴露的所有端口进行映射 -p``--publish= ...

  2. nginx下配置laravel+rewrite重写

    server { listen ; server_name ha.d51v.cn; #access_log /data/wwwlogs/access_nginx.log combined; root ...

  3. javascript实现原生ajax的几种方法介绍

    自从javascript有了各种框架之后,比如jquery,使用ajax已经变的相当简单了.但有时候为了追求简洁,可能项目中不需要加载jquery这种庞大的js插件.但又要使用到ajax这种功能该如何 ...

  4. for_each_node(node)

    遍历各个pg_data_t节点. 1.定义在include/linux/nodemask.h中 /* * Bitmasks that are kept for all the nodes. */ en ...

  5. CentOS 7.X 中systemctl命令用法详解

    systemctl是RHEL 7 的服务管理工具中主要的工具,它融合之前service和chkconfig的功能于一体.可以使用它永久性或只在当前会话中启用/禁用服务,下面来看CentOS 7.X 中 ...

  6. 求数组中两两相加等于20的组合(Python实现)

    题目 求数组中两两相加等于20的组合. 例:给定一个数组[1, 7, 17, 2, 6, 3, 14],这个数组中满足条件的有两对:17+3=20, 6+14=20. 解析 分为两个步骤: 先采用堆排 ...

  7. P3369 【模板】普通平衡树FHQtreap

    P3369 [模板]普通平衡树(Treap/SBT) 题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入x数 删除x数(若有多个相同的数,因只删除一个) 查询 ...

  8. LoadRunner 11破解方法

    名称:HP Loadrunner Software 11.00 版本号:11.00.0.0 安装环境:Win 7 软件安装成功后,会弹出提示告知license的有效期为10天. 破解方法: 1.下载破 ...

  9. linux随笔三

    1.ps   结果输出: PID TTY TIME CMD pts/ :: bash pts/ :: ps显示了程序的进程ID,其运行的终端和进程使用的cpu时间

  10. EM算法简易推导

    EM算法推导 网上和书上有关于EM算法的推导,都比较复杂,不便于记忆,这里给出一个更加简短的推导,用于备忘. 在不包含隐变量的情况下,我们求最大似然的时候只需要进行求导使导函数等于0,求出参数即可.但 ...