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.

InputThe 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.)OutputFor 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 求一个区间比小于等于K的个数 我们就可以去查询第R个版本的线段树-第L-1个版本的线段树的数量
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<cmath> const int maxn=1e5+;
typedef long long ll;
using namespace std;
struct node
{
int l,r;
int sum;
}tree[maxn*];
int cnt,root[maxn];
int a[maxn];
vector<int>v;
int getid(int x)
{
return lower_bound(v.begin(),v.end(),x)-v.begin()+;
}
void build(int &u,int l,int r)
{
u=++cnt;
tree[u].sum=;
if(l==r)return ;
int mid=(l+r)/;
build(tree[u].l,l,mid);
build(tree[u].r,mid+,r);
}
void update(int l,int r,int pre,int &now,int p)
{
tree[++cnt]=tree[pre];
now=cnt;
tree[now].sum++;
if(l==r)
{
return ;
}
int mid=(l+r)>>;
if(p<=mid)
{
update(l,mid,tree[pre].l,tree[now].l,p);
}
else
{
update(mid+,r,tree[pre].r,tree[now].r,p);
}
}
int query(int l,int r,int L,int R,int k)
{
if(l==r)
{
return tree[R].sum-tree[L].sum;
}
int mid=(l+r)>>;
if(k<=mid)
{
return query(l,mid,tree[L].l,tree[R].l,k);
}
else
{
ll ans=tree[tree[R].l].sum-tree[tree[L].l].sum;
ans+=query(mid+,r,tree[L].r,tree[R].r,k);
return ans;
}
}
int main()
{
int T;
cin>>T;
int cc=;
while(T--)
{
int n,m;
scanf("%d%d",&n,&m);
cnt=;
for(int t=;t<=n;t++)
{
v.clear();
}
for(int t=;t<=n;t++)
{
scanf("%d",&a[t]);
v.push_back(a[t]);
} sort(v.begin(),v.end());
v.erase(unique(v.begin(),v.end()),v.end());
build(root[],,n);
for(int t=;t<=n;t++)
{
update(,n,root[t-],root[t],getid(a[t]));
}
int x,y,k;
printf("Case %d:\n",cc++);
while(m--)
{
scanf("%d%d%d",&x,&y,&k);
k=upper_bound(v.begin(),v.end(),k)-v.begin();
if(k==)
{
puts("");
continue;
}
x++;
y++;
printf("%d\n",query(,n,root[x-],root[y],k));
}
}
return ;
}

HDU-4417-Super Mario(主席树解法)的更多相关文章

  1. HDU 4417 Super Mario 主席树

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

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

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

  3. HDU 4417 Super Mario(划分树)

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

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

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

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

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

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

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

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

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

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

  10. hdu 4417 Super Mario (主席树)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=4417 题意: 给你段长为n的序列,有q个询问,每次询问区间[l.r]内有多少个数小于等于k 思路: 之前用 ...

随机推荐

  1. requests-html库轻体验-HTMLSession下载表情包

    requests-html实战,HTMLSession下载斗图啦最新表情包 前言 在这篇文章之前,我写了requests入门实践02_下载斗图拉最新表情包用正则表达式提取url,来下载斗图啦最新表情包 ...

  2. MySQL面试题!新鲜出炉~

    01.Mysql 的存储引擎,myisam和innodb的区别? 答:1.MyISAM 是非事务的存储引擎,适合用于频繁查询的应用.表锁,不会出现死锁,适合小数据,小并发. 2.innodb是支持事务 ...

  3. Android BottomNavigationView的用法

    BottomNavigationView是相当于一个导航的标签,但是它的形式就是像QQ,微信之类的界面 这三个图标就是BottomNavigationView的体现. 至于写出后怎样绑定这三个界面,就 ...

  4. cxuan读者的外包面试之旅

    最开始是打算面试外包公司刷经验,等经验差不多了,再去甲方尝试,可惜不太顺利. 一.迈思(面试了30分钟) 自我介绍 左连接(a 表左连接 b 表,a 表全部数据出来,b 表没有的数据为空) a 表左连 ...

  5. Python Tricks —— 使用 pywinrm 远程控制 Windows 主机

    启用 WinRM 远程服务: winrm quickconfig 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经做案例的人,却不 ...

  6. 修改jar包配置文件的正确操作,jar包解压出来的文件夹重新打成jar,不依靠开发工具!!!!

    修改jar包配置文件的正确操作,有的时候通过一些解压工具可以对内部的文件进行修改,但是有时候会无效.这就很烦了 一.背景:       有一个springboot项目,事先我已经用编译好打成jar包以 ...

  7. WebLogic 省略项目名称

    希望 WebLogic 部署的项目,不需要输入项目名,直接通过IP端口访问. 在 WEB-INF 目录下添加文件 weblogic.xml <?xml version="1.0&quo ...

  8. JavaScript calss语法糖

    JavaScript calss语法糖 基础知识 严格意义上来讲,在Js中是没有类这一概念的. 我们可以运用前面章节提到的构造函数来模拟出类这一概念,并且可以通过原型对象的继承来完美的实现实例对象方法 ...

  9. Python安装工具

    1.官网下载地址是:https://www.python.org/downloads/  默认下载安装时记得勾选配置PATH路径 PIP工具包(我是选择Python 3.5的) 2.Windows 下 ...

  10. Oracle元数据信息

    一.schema操作 1)查看当前schema select user, sys_context('userenv','current_schema') from dual; 2)切换schema a ...