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

题解:

因为是线段树专题的最后几题了。。还以为很难没看题就搜了博客,知道题意后发现用离线操作这题根本不难。。

题意:

给你n个序列,m个询问,每个询问有区间[x,y],和后面的数组h,问你区间内不大于h的数字和是多少

思路:

显然是个离线操作题,先将子序列存起来,按照子序列的高度从小到大排序,再把询问存起来,把询问按照高度从小到大排序,然后建树,赋初值为0,然后就遍历询问,每次看当前更新的子序列高度是否大于了询问高度,如果是就处理询问,就是个简单的区间求和了,如果不是就更新子序列的位置,直到子序列更新到底或者已经大于了询问高度线段树和树状数组都可以做吧,我先用线段树写一遍,作为练习

ps:

刚刚完成的树状数组版的代码:附上链接点击打开链接,速度比线段树快了一倍多,内存也小多了

代码:

#include<algorithm>
#include<iostream>
#include<cstring>
#include<stdio.h>
#include<math.h>
#include<string>
#include<stdio.h>
#include<queue>
#include<stack>
#include<map>
#include<deque>
#define M (t[k].l+t[k].r)/2
#define lson k*2
#define rson k*2+1
using namespace std;
struct node
{int l,r;int num;//存区间内已经更新了几个数
}t[100005*4];
void Build(int l,int r,int k)//日常建树
{t[k].l=l;t[k].r=r;t[k].num=0;if(l==r)return;int mid=M;Build(l,mid,lson);Build(mid+1,r,rson);
}
void update(int pos,int k)//日常更新
{if(t[k].l==t[k].r){t[k].num=1;return;}int mid=M;if(pos<=mid)update(pos,lson);elseupdate(pos,rson);t[k].num=t[lson].num+t[rson].num;
}
int query(int l,int r,int k)//日常询问
{if(t[k].l==l&&t[k].r==r){return t[k].num;}int mid=M;int p;if(r<=mid)return query(l,r,lson);else if(l>mid)return query(l,r,rson);elsereturn query(l,mid,lson)+query(mid+1,r,rson);
}
struct qu
{int id;//存询问的idint h;//询问高度int l,r;
}q[100005];
struct sub
{int pos;//子序列初始位置int h;//子序列高度
}s[100005];
int cmp1(qu x,qu y)//比较函数,用于询问按高度从小到大排序
{return x.h<y.h;
}
int cmp2(sub x,sub y)//比较函数,用于子序列按高度从小到大排序
{return x.h<y.h;
}
int answer[100005];/存答案
int main()
{int i,j,k,n,m,test,o,tot;scanf("%d",&test);for(o=1;o<=test;o++){scanf("%d%d",&n,&m);for(i=0;i<n;i++){scanf("%d",&s[i].h);s[i].pos=i;}for(i=0;i<m;i++){scanf("%d%d%d",&q[i].l,&q[i].r,&q[i].h);q[i].id=i;}sort(s,s+n,cmp2);sort(q,q+m,cmp1);//排序tot=0;Build(0,n-1,1);for(i=0;i<m;i++){while(tot<n&&q[i].h>=s[tot].h)//更新到子序列高度大于当前询问高度为止{update(s[tot].pos,1);tot++;}answer[q[i].id]=query(q[i].l,q[i].r,1);//询问}printf("Case %d:\n",o);for(i=0;i<m;i++)printf("%d\n",answer[i]);}return 0;
}

HDU 4417 Super Mario(线段树||树状数组+离线操作 之线段树篇)相关推荐

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

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

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

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

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

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

  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(可持久化线段树)

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

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

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

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

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

  8. HDU 4417 Super Mario(划分树)

    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) Tota ...

最新文章

  1. 又有一个霸榜的 Linux 神器
  2. opencv图像处理常用函数
  3. Vim改装编辑器的安装与使用简介
  4. 字符设备驱动高级篇2——字符设备驱动注册代码分析
  5. C语言int r(int m),INT(M)表示什么意思?
  6. 如何进行IT项目的需求调研?[读后有得所以转之]
  7. C语言函数大全 chm含示例
  8. Typora+PicGo+LskyPro打造舒适写作环境
  9. 路由器管理页面html,小米路由器管理后台(小米AIoT路由器登录管理页面地址)...
  10. Spring AOP(面向切面)
  11. Excel演示神经网络原理(黑白数字0、1识别)
  12. python编程从入门到实践 项目一:外星人入侵
  13. Oracle SQL:update更新语句总结
  14. Django的列表分页
  15. Docker中的资源分配详解
  16. 接口要怎么测试?接口自动化可以怎么做?
  17. c语言能保留x后两位并且四舍五入的,二级C语言真题笔记
  18. 关于VS2017中VB.NET打开重新打开工程后程序设计界面无法显示的问题
  19. 西门子smart200能用C语言吗,【项目详解】200SMART+V20在收卷机械上的应用
  20. CDA学习-------描述性统计分析

热门文章

  1. b2c项目基础架构分析(二)前端框架 以及补漏的第一篇名词解释
  2. 2023最新SSM计算机毕业设计选题大全(附源码+LW)之java大学生学科竞赛管理系统t16zl
  3. 猿创征文 | DevOps运维的10个日常使用工具分享
  4. python3思维导图.xmind_我常用的3款脑图工具
  5. 第一章 matlab 学习入门之matlab基础
  6. 如何在 Excel VBA 中插入行
  7. 后BT时代,我们该怎么办?
  8. NX/UG二次开发-其他-打包对话框\图标到DLL
  9. Oracle 11gR2新特性--延迟段创建(Deferred Segment Creation)和exp不能导出空表
  10. APP测试基本流程以及APP测试要点梳理,成功入职就靠它了