题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4417

 
Problem 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
 
主席树板子题,求区间小于等于h的数有多少,注意题目给的查询区间是从0开始的
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
#define maxn 100005
int a[maxn],b[maxn],T[maxn<<],L[maxn<<],R[maxn<<],sum[maxn<<],tot;
inline int update(int pre,int l,int r,int x)
{
int rt=++tot;
L[rt]=L[pre];
R[rt]=R[pre];
sum[rt]=sum[pre]+;
if(l<r)
{
int mid=l+r>>;
if(x<=mid)L[rt]=update(L[pre],l,mid,x);
else R[rt]=update(R[pre],mid+,r,x);
}
return rt;
}
inline int query(int u,int v,int ql,int qr,int l,int r)
{
if(ql<=l&&qr>=r)return sum[v]-sum[u];
int mid=l+r>>,ans=;
if(ql<=mid)ans+=query(L[u],L[v],ql,qr,l,mid);
if(qr>mid)ans+=query(R[u],R[v],ql,qr,mid+,r);
return ans;
}
int main()
{
int t;
cin>>t;
for(int W=;W<=t;W++)
{
int n,m;
cin>>n>>m;
for(int i=;i<=n;i++)
{
cin>>a[i];
b[i]=a[i];
}
sort(b+,b++n);
int len=unique(b+,b++n)-b-;
T[]=sum[]=L[]=R[]=tot=;
for(int i=;i<=n;i++)
{
int pos=lower_bound(b+,b++len,a[i])-b;
T[i]=update(T[i-],,len,pos);
}
cout<<"Case "<<W<<":"<<endl;
for(int i=;i<=m;i++)
{
int l,r,h;
cin>>l>>r>>h;
int pos=upper_bound(b+,b++len,h)-b;
pos--;
if(!pos)cout<<""<<endl;
else
{
cout<<query(T[l],T[r+],,pos,,len)<<endl;
}
}
}
return ;
}
 

hdu4417 主席树求区间小于等于K的更多相关文章

  1. 主席树——求区间第k个不同的数字(向右密集hdu5919)

    和向左密集比起来向右密集只需要进行小小的额修改,就是更新的时候从右往左更新.. 自己写的被卡死时间.不知道怎么回事,和网上博客的没啥区别.. /* 给定一个n个数的序列a 每次询问区间[l,r],求出 ...

  2. 主席树--动态区间第k小

    主席树--动态区间第\(k\)小 模板题在这里洛谷2617. 先对几个问题做一个总结: 阅读本文需要有主席树的基础,也就是通过区间kth的模板题. 静态整体kth: sort一下找第k小,时间复杂度\ ...

  3. poj 2104 主席树(区间第k大)

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 44940   Accepted: 14946 Ca ...

  4. A - 低阶入门膜法 - K-th Number (主席树查询区间第k小)

    题目链接:https://cn.vjudge.net/contest/284294#problem/A 题目大意:主席树查询区间第k小. 具体思路:主席树入门. AC代码: #include<i ...

  5. HDU 5919 - Sequence II (2016CCPC长春) 主席树 (区间第K小+区间不同值个数)

    HDU 5919 题意: 动态处理一个序列的区间问题,对于一个给定序列,每次输入区间的左端点和右端点,输出这个区间中:每个数字第一次出现的位子留下, 输出这些位子中最中间的那个,就是(len+1)/2 ...

  6. [csu/coj 1080]划分树求区间前k大数和

    题意:从某个区间内最多选择k个数,使得和最大 思路:首先题目给定的数有负数,如果区间前k大出现负数,那么负数不选和更大,于是对于所有最优选择,负数不会出现,所以用0取代负数,问题便转化为区间的前k大数 ...

  7. HDU 4417 Super Mario(主席树求区间内的区间查询+离散化)

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

  8. 主席树——求区间[l,r]不同数字个数的模板(向左密集 D-query)

    主席树的另一种用途,,(还有一种是求区间第k大,区间<=k的个数) 事实上:每个版本的主席树维护了每个值最后出现的位置 这种主席树不是以权值线段树为基础,而是以普通的线段树为下标的 /* 无修改 ...

  9. hdu 5919--Sequence II(主席树--求区间不同数个数+区间第k大)

    题目链接 Problem Description Mr. Frog has an integer sequence of length n, which can be denoted as a1,a2 ...

随机推荐

  1. 【u128】又一个数字游戏

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 小明拿出了一个素数集合,{2, 3, 5, 7, 11, 13, -, 127, -},他发现,从小到 ...

  2. P1104 最大公约数和最小公倍数问题

    题目描述 输入2个正整数 \(x0, y0 (2 \le x0 \lt 100000, 2 \le y0 \le 1000000)\) ,求出满足下列条件的 P,Q 的个数. 条件: P,Q是正整数 ...

  3. P1036 最大公约数

    题目描述 给你两个正整数A和B,求它们的最大公约数. 输入格式 两个正整数 \(A,B(1 \le A,B \le 10^9)\) . 输出格式 一个整数,表示A和B的最大公约数. 样例输入 6 8 ...

  4. H3C端口状态

  5. linux 后备缓存

    一个设备驱动常常以反复分配许多相同大小的对象而结束. 如果内核已经维护了一套相同 大小对象的内存池, 为什么不增加一些特殊的内存池给这些高容量的对象? 实际上, 内核 确实实现了一个设施来创建这类内存 ...

  6. 列表内容自动向上滚动(原生JS)

    效果展示 (鼠标移入,滚动停止:鼠标移出,滚动继续) 实现原理 1. html结构:核心是ul > li,ul外层包裹着div.因为想要内容循环滚动无缝衔接,所以在原有ul后面还要有一个一样内容 ...

  7. video视频标签一些设置,包括封面、播放结束后的封面、视频占满屏幕的方式、视频播放暂停、展示控制栏、触发全屏播放事件

    video视频标签一些设置,包括封面.播放结束后的封面.视频占满屏幕的方式.视频链接.视频播放暂停.展示控制栏.触发全屏播放事件 <video id="video" auto ...

  8. oracle解除被锁定的表的状态

      select b.owner,b.object_name,a.session_id,a.locked_mode,c.serial#,c.sid||','||c.serial# from v$loc ...

  9. Keras lstm 文本分类示例

    #基于IMDB数据集的简单文本分类任务 #一层embedding层+一层lstm层+一层全连接层 #基于Keras 2.1.1 Tensorflow 1.4.0 代码: '''Trains an LS ...

  10. 第二阶段:2.商业需求文档MRD:1.M版本管理

    版本管理的例子.V=Version.注意大中小版本的区分.V1.2.2 第一个数字1就是大版本 中间的2就是中版本 末尾的2就是小版本.大版本就是方向的变更,比如我的用户之前主要是面向男性,现在要面向 ...