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的数有几个,用主席树可以直接做,只要修改一下query,二分统计个数即可。
注意砖块高度和跳跃高度都要离散化,空间要开2*10^5。
不过网上的普遍做法是线段树离线处理,确实比主席树巧妙得多,把询问和数据按高度从小到大排序,每次只要把<=h的数据插入线段树,直接查询就行,因为后面插入的>h的高度对前面的答案没有影响,根本没必要记录状态。而查询第k大要用主席树记录所有状态,是因为读取了所有数据才能比较大小。
离线处理还是考验思维的灵活性,多留意是否所有的数据对每次查询都有用。
这次只写了主席树,有空再补一下线段树的写法。

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;const int MAXN = 200010;
const int M = MAXN * 30;
int x,n,q,m,tot;
int a[MAXN], t[MAXN],b[MAXN],L[MAXN],R[MAXN];
int T[M], lson[M], rson[M], c[M];void Init_hash()
{for(int i = 1; i <= n;i++)t[i] = a[i];for(int i=1;i<=q;i++)t[i+n]=b[i];sort(t+1,t+1+n+q);m = unique(t+1,t+1+n+q)-t-1;
}
int build(int l,int r)
{int root = tot++;c[root] = 0;if(l != r){int mid = (l+r)>>1;lson[root] = build(l,mid);rson[root] = build(mid+1,r);}return root;
}
int has(int x)
{return lower_bound(t+1,t+1+m,x) - t;
}
int update(int root,int pos,int val)
{int newroot = tot++, tmp = newroot;c[newroot] = c[root] + val;int l = 1, r = m;while(l < r){int mid = (l+r)>>1;if(pos <= mid){lson[newroot] = tot++; rson[newroot] = rson[root];newroot = lson[newroot]; root = lson[root];r = mid;}else{rson[newroot] = tot++; lson[newroot] = lson[root];newroot = rson[newroot]; root = rson[root];l = mid+1;}c[newroot] = c[root] + val;}return tmp;
}
int query(int left_root,int right_root,int k)
{int l = 1, r = m, ans = 0;while( l < r){int mid = (l+r)>>1;if(mid <= k ){ans+=c[lson[left_root]]-c[lson[right_root]];l = mid+1;left_root = rson[left_root];right_root = rson[right_root];}else{r=mid;left_root = lson[left_root];right_root = lson[right_root];}}return ans;
}
int main()
{//freopen("in.txt","r",stdin);scanf("%d",&x);int l,r,k,y=0;while(x--){y++;scanf("%d%d",&n,&q);tot = 0;for(int i = 1;i <= n;i++)scanf("%d",&a[i]);for(int i=1;i<=q;i++){scanf("%d%d%d",&l,&r,&k);b[i]=k;L[i]=l+1;R[i]=r+1;}Init_hash();T[n+1] = build(1,m);for(int i = n;i ;i--){int pos = has(a[i]);T[i] = update(T[i+1],pos,1);}printf("Case %d:\n",y);for(int i=1;i<=q;i++){l=L[i];r=R[i];k=has(b[i]);printf("%d\n",query(T[l],T[r+1],k));}}return 0;
}

HDU 4417 Super Mario(线段树离线处理/主席树)相关推荐

  1. HDU - 4417 Super Mario(主席树/线段树+离线)

    题目链接:点击查看 题目大意:给出由 n 个数的数列,再给出 m 次查询,每次查询需要输出 [ l , r ] 内小于等于 h 的数有多少个 题目分析:大晚上睡不着觉随便做做题,发现这个题目原来可以用 ...

  2. HDU 4417 Super Mario(线段树)

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

  3. hdu 4417 Super Mario(可持久化线段树)

    题意:给你一些数,有多次询问,问你在l,r区间内小于k的数有多少个 思路:主席树大发好,虽然树状数组和线段树离线也可以做 代码: #include <set> #include <m ...

  4. HDU 4417 Super Mario(莫队 + 树状数组 + 离散化)

    Super Mario 思路 区间查找问题,容易想到离线莫队,确实这题就是莫队,接下来我们考虑如何维护区间高度值问题. 既然是离线嘛,我们容易想到离散化和他挂钩,想想这题是否需要离散化,高度的最大值是 ...

  5. HDU 4417 Super Mario(划分树)

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

  6. hdu 4417 Super Mario 树状数组||主席树

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

  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(划分树问题求不大于k的数有多少)

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

  9. hdu 4417 Super Mario

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

最新文章

  1. Chrome不支持showModalDialog的解决方案
  2. 服务器控件 原生html,应用样式到HTML服务器控件
  3. Jquery给基本控件的取值、赋值
  4. sqlserver 导入/导出Excel
  5. 后端技术:JDK 8 Stream 数据流效率测试
  6. 优秀学生专栏——董超
  7. AutoItLibrary安装和常见问题解决
  8. 【LeetCode】【数组】题号:59,螺旋数组2
  9. 怎么批量查找关键词-批量查找关键词软件工具
  10. latex安装教程以及入门
  11. struct dirent 和 struct stat 结构体
  12. python+django+mysql校园失物招领系统毕业设计毕设开题报告
  13. 苹果x和xsmax有什么区别_苹果12和12pro有什么区别?参数对比拍照续航,哪个值得买?...
  14. CKEditor/FCKEditor 使用-CKEditor(FCKeditor)精简版大全
  15. ios和Android的PK
  16. VB编程:取整函数Int、CInt、Fix区别-30
  17. 全球及中国冶金工业市场产量分析及投资战略决策报告2021版
  18. 20款有趣的英文卡通免费字体
  19. 安全漏洞SCAP规范标准
  20. noip模拟赛 斐波那契

热门文章

  1. 重返天梯-L2-036 网红点打卡攻略 (25 分)
  2. 大专java方向校招面试找工作知识点技术栈以及实习感受分享-简历分享
  3. python glob.glob
  4. iOS开发创建App内购买项目发现元数据丢失
  5. 从零开始学习菜鸟晋级黑客之黑客之“名词介绍”
  6. 安全和运维工具脑图v1.0
  7. Word目录制作,添加目录自动跳转
  8. 改版后的PMP值得考吗?
  9. Word中并排放置图片的方法
  10. 物理机安装linux系统,U盘在物理机安装linux系统