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. LINUX --- echo修改GPIO状态

    GPIO sysfs Interface The GPIO sysfs interface allows users to manipulate any GPIO from userspace (al ...

  2. CentOS 7.0防火墙设置

    CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙.1.关闭firewall:systemctl stop firewalld.servicesystemctl ...

  3. 前端自适应样式(reset.css)

    @charset "utf-8"; /* CSS Document */ html, body, ul, li, ol, dl, dd, dt, p, h1, h2, h3, h4 ...

  4. JS学习第六天

    匿名函数: 定义:function(参数列表){ 要执行的语句块: } 定义名(): 创建日期对象:Date var date=new Date(); alert(date);  不输入则是默认月,日 ...

  5. java流程控制语句switch

    switch 条件语句也是一种很常用的选择语句,它和if条件语句不同,它只能针对某个表达 式的值作出判断,从而决定程序执行哪一段代码. 格式: switch (表达式){ case 目标值1: 执行语 ...

  6. C#LeetCode刷题之#771-宝石与石头(Jewels and Stones)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3812 访问. 给定字符串J 代表石头中宝石的类型,和字符串 S代 ...

  7. externaltrafficpolicy的有关问题说明

    环境描述 生产环境通过gitlab-running实现自动化发布业务,现需要收集客户端的真实ip,需要将externaltrafficpolicy改为lacal模式(原来是cluster模式),前天开 ...

  8. VUE——添加组件模块(图表)

    Vue是由一个个小模块组成的,模块可以让页面简介还可以复用: 1.不固定数据数量传到子组件 父组件: <chartVue v-for="(item, index) in chartLi ...

  9. Python中ansible的使用

    #!/usr/bin/env python # -*- coding: utf8 -*- # @Author: huangmanyao from ansible import constants fr ...

  10. 《JavaScript语言入门教程》记录整理:面向对象

    目录 面向对象编程 实例对象与 new 命令 this关键字 对象的继承 Object对象的方法 严格模式(strict mode) 本系列基于阮一峰老师的<JavaScrip语言入门教程> ...