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

题目大意:

线段树的区间查询,查询区间[l,r]中<=h的数的个数。

核心思想:

线段树+离线。

代码如下:

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=1e5+20;
struct node{int x,id;
}a[N];
struct Node{int l,r,h,id,chu;
}que[N];
struct tnode{int l,r,sum;
}tr[N<<2];
bool cmp0(node p,node q)
{return p.x<q.x;
}
bool cmp1(Node p,Node q)
{return p.h<q.h;
}
bool cmp2(Node p,Node q)
{return p.id<q.id;
}
void pushup(int m)
{tr[m].sum=tr[m<<1].sum+tr[m<<1|1].sum;return;
}
void build(int m,int l,int r)
{tr[m].l=l;tr[m].r=r;if(l==r){tr[m].sum=0;return;}int mid=(l+r)>>1;build(m<<1,l,mid);build(m<<1|1,mid+1,r);pushup(m);return;
}
void update(int m,int x)
{if(tr[m].l==x&&tr[m].r==x){tr[m].sum=1;return;}int mid=(tr[m].l+tr[m].r)>>1;if(x<=mid)update(m<<1,x);elseupdate(m<<1|1,x);pushup(m);return;
}
int query(int m,int l,int r)
{if(tr[m].l==l&&tr[m].r==r)return tr[m].sum;int mid=(tr[m].l+tr[m].r)>>1;if(r<=mid)return query(m<<1,l,r);if(l>mid)return query(m<<1|1,l,r);return query(m<<1,l,mid)+query(m<<1|1,mid+1,r);
}
int main()
{int T,ca=0;cin>>T;while(T--){int n,m;//输入并建树 scanf("%d%d",&n,&m);build(1,1,n);for(int i=1;i<=n;i++){scanf("%d",&a[i].x);a[i].id=i;}//排序,小的先进树 sort(a+1,a+1+n,cmp0);//离线处理 for(int i=1;i<=m;i++){scanf("%d%d%d",&que[i].l,&que[i].r,&que[i].h);que[i].l++;que[i].r++;que[i].id=i;}sort(que+1,que+1+m,cmp1);//边更新边离线查询 int k=1;for(int i=1;i<=m;i++){while(k<=n&&a[k].x<=que[i].h){update(1,a[k].id);k++;}que[i].chu=query(1,que[i].l,que[i].r);}//把顺序sort回来,输出 sort(que+1,que+1+m,cmp2);printf("Case %d:\n",++ca);for(int i=1;i<=m;i++)printf("%d\n",que[i].chu);}return 0;
}

HDU 4417-Super Mario-线段树+离线相关推荐

  1. HDU 4417 Super Mario(线段树离线处理/主席树)

    Mario is world-famous plumber. His "burly" figure and amazing jumping ability reminded in ...

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

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

  3. HDU 4417 Super Mario(划分树)

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

  4. #HDU 4417 Super Mario (主席树 + 二分)

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

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

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

  6. hdu 4417 Super Mario 划分树+二分

    http://acm.hdu.edu.cn/showproblem.php?pid=4417 题意: 给定一个长度为n的序列,求区间[L,R]中小于h的个数: 思路: 分三种情况: 1:如果该区间最小 ...

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

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

  8. hdu 4417 Super Mario

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

  9. HDU 4417 Super Mario(离线线段树or树状数组)

    Problem Description Mario is world-famous plumber. His "burly" figure and amazing jumping ...

  10. HDU 4417 Super Mario(线段树)

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

最新文章

  1. 前端 CSS层叠性 CSS选择器优先级
  2. python抽奖概率设计_辞职转行不如学Python,那些下载安装的坑,你真的都避开了嘛?...
  3. 搜索推荐中的召回匹配模型综述(一):传统方法
  4. WPF利用动画实现圆形进度条
  5. 算法六之直接插入排序
  6. 计算机控制的点火系统由,第八节(点火系统)
  7. 励志!从中专生到清华博士的逆袭人生
  8. php 图片印章_php工具型代码之印章抠图
  9. c++如何将两个if函数合并_Excel中的VLOOKUP函数,8种使用技巧与你分享
  10. 校园综合服务平台小程序
  11. dart js转换_基于dart生态的FaaS前端一体化建设
  12. Linux 系统服务漏洞PwnKit 已存在12年,可获得所有主流发布版本的root 权限
  13. 【流程发现算法概述】
  14. Prolog编程求解图搜索问题
  15. 分享国内常用的免费MD5在线解密网站,这5个网站很实用
  16. 七牛云存储使用经历到底怎么样
  17. 20165309 实验四 Android程序设计
  18. 106、七氟丙烷灭火系统的灭火机理
  19. 神经网络的激活函数总结
  20. 网站正式上线之前的ICP备案和公安联网备案

热门文章

  1. win10控制面板快捷键_常用的几个win10使用小窍门
  2. 利盟设备提示“没有将模拟电话线连接到调制解调器,传真被禁用。”解决办法
  3. WIFI 功率计算方式
  4. linux用命令查网口速率,Linux命令行查看当前上传/下载速度
  5. 【华为ICT备赛】生成树基础
  6. 日语翻译必备_合同协议篇
  7. 解决Found an unexpected Mach-O header code: 0x72613c21问题
  8. 青龙面板——抖抖健身 脚本+超详细教程
  9. C++ 基础复习系列2(打印图形类(循环)经典问题类)
  10. IDEA中Maven项目中的pom文件出现小蜘蛛