题目链接: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. CF351E Jeff and Permutation

    CF351E Jeff and Permutation 贪心好题 考虑每个对能否最小化贡献和 先不考虑绝对值相同情况 发现,对于a,b假设|a|<|b|,那么有无贡献只和b的正负有关!如果a在b ...

  2. 2018-11-2-win10-uwp-通过-win2d-画出笔迹

    title author date CreateTime categories win10 uwp 通过 win2d 画出笔迹 lindexi 2018-11-2 20:11:0 +0800 2018 ...

  3. linux自旋锁函数

    我们已经看到 2 个函数, spin_lock 和 spin_unlock, 可以操作自旋锁. 有其他几个函 数, 然而, 有类似的名子和用途. 我们现在会展示全套. 这个讨论将带我们到一个我们无 法 ...

  4. linux Completions 机制

    内核编程的一个普通模式包括在当前线程之外初始化某个动作, 接着等待这个动作结束. 这个动作可能是创建一个新内核线程或者用户空间进程, 对一个存在着的进程的请求, 或 者一些基于硬件的动作. 在这些情况 ...

  5. linux ioctl 方法

    ioctl, 我们在第 1 章展示给你如何使用, 是一个系统调用, 作用于一个文件描述符; 它 接收一个确定要进行的命令的数字和(可选地)另一个参数, 常常是一个指针. 作为一个使 用 /proc 文 ...

  6. Echarts构建图表

    Echarts学习-构建图表 相信有很多的前端开发人员在开发Echarts图表的过程中都遇到对图表结构过无从下手,面对一大堆的专业词汇一脸懵逼的样子,在经过了一段时间的踩坑后,终于摸索出了一套完善的学 ...

  7. Linux 内核PC/104 和 PC/104+

    当前在工业世界中, 2 个总线体系是非常时髦的: PC/104 和 PC/104+. 2 个在 PC-类 的 单板计算机中都是标准的. 2 个标准都是印刷电路板的特殊形式, 包括板互连的电子的/机械的 ...

  8. C. 【UNR #3】配对树

    题解: 首先可以贪心 于是问题可以等价成一条边被算当且仅当子树中个数为奇数个 题解的做法比较简单 考虑每条边,加入其子树内的点 然后为了保证区间长度为偶数 分成f0,0 f0,1 f1,0 f1,1即 ...

  9. torch or numpy

    黄色:重点 粉色:不懂 Torch 自称为神经网络界的 Numpy, 因为他能将 torch 产生的 tensor 放在 GPU 中加速运算 (前提是你有合适的 GPU), 就像 Numpy 会把 a ...

  10. mysql:数据库与实例的区别

    题记:最近想更深入的了解mysql,所以买了一些书在学习,趁着这个机会开个坑,整理一下一些我认为重要的知识点. 刚工作那会经常能听到组长提到实例这个词,一开始我以为是服务器... 数据库(databa ...