HDU-4417-Super Mario(主席树解法)
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(主席树解法)的更多相关文章
- HDU 4417 Super Mario 主席树
分析:找一个区间里小于等于h的数量,然后这个题先离散化一下,很简单 然后我写这个题主要是熟悉一下主席树,其实这个题完全可以离线做,很简单 但是学了主席树以后,我发现,在线做,一样简单,而且不需要思考 ...
- HDU 4417 Super Mario 主席树查询区间小于某个值的个数
#include<iostream> #include<string.h> #include<algorithm> #include<stdio.h> ...
- HDU 4417 Super Mario(划分树)
Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 4417 Super Mario(划分树问题求不大于k的数有多少)
Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 4417 - Super Mario ( 划分树+二分 / 树状数组+离线处理+离散化)
题意:给一个数组,每次询问输出在区间[L,R]之间小于H的数字的个数. 此题可以使用划分树在线解决. 划分树可以快速查询区间第K小个数字.逆向思考,判断小于H的最大的一个数字是区间第几小数,即是答案. ...
- HDU 4417 Super Mario ( 离线树状数组 )
把数值和查询放在一起从小到大排序,纪录每个数值的位置,当遇到数值时就更新到树状数组中,遇到查询就直接查询该区间和. #include <cstdio> #include <cstri ...
- HDU 4417 Super Mario(划分树+二分)
题目链接 #include <cstdio> #include <cstring> #include <algorithm> using namespace std ...
- HDU 4417 Super Mario(主席树求区间内的区间查询+离散化)
Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- 主席树:HDU 4417 Super Mario
Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdu 4417 Super Mario (主席树)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=4417 题意: 给你段长为n的序列,有q个询问,每次询问区间[l.r]内有多少个数小于等于k 思路: 之前用 ...
随机推荐
- Linux恢复删除后数据文件
简介 在使用Linux系统时,有时候会不小心误删除数据,由于Linux系统也没有与Windows系统下回收站类似的功能,一般会认为该文件将无法找回. 本文主要以CentOS7操作系统为例,介绍如何使用 ...
- cryptopp使用Qt mingw编译,以及海思平台交叉编译
编译工程生成,使用qmake生成qt工程文件(海思平台时,要用海思平台的qmake),将 TEMPLATE = app 修改为: TEMPLATE = lib 添加如下: win32:LIBS += ...
- 开启CAN通信学习(二)——基于Kvaser的CAN通信案例
1 案例硬件介绍 Kvaser是瑞典的一家专门提供CAN和LIN总线分析仪及数据记录仪的公司,在CAN产品开发领域已经有近30年的经验,本案例选择的CAN通信硬件型号是Kvaser Leaf Ligh ...
- 老板让我从上千个Excel中筛选数据,利用Python分分钟解决!
大家好,又到了Python办公自动化系列. 今天分享一个真实的办公自动化需求,大家一定要仔细阅读需求说明,在理解需求之后即可体会Python的强大! 很多人学习python,不知道从何学起.很多人学习 ...
- WebService简单Demo
看了网上好多关于webservice的例子,基本上对初学者来说都是模棱两可云里雾里,现在,我将网上关于webservice的讲解提炼出来,通过一个最简单使用并且方便的例子,告诉大家什么是webserv ...
- jquery 效果笔记
jquery效果 显示隐藏 show() 语法 show([speed,[easing],[fn]]) 参数可以省略,无动画直接使用 hide() to ...
- 【模式识别与机器学习】——4.3离散K-L变换
全称:Karhunen-Loeve变换(卡洛南-洛伊变换) 前面讨论的特征选择是在一定准则下,从n个特征中选出k个来反映原有模式. 这种简单删掉某n-k个特征的做法并不十分理想,因为一般来说,原来的n ...
- 2020-03-27:JDK1.8中在数据结构上,对HashMap做了什么样的改进?为什么?
福哥答案2020-04-04:头插改尾插,解决链表成环的问题.链表改成链表和红黑树.
- JavaScript 数组中根据某个属性值的中文进行排序
普通排序 const arr = [] arr.sort((x, y) => x.prop - y.prop) 中文属性值排序 const arr = [] arr.sort((x, y) =& ...
- 火题小战 C. 情侣?给我烧了!
火题小战 C. 情侣?给我烧了! 题目描述 有 \(n\) 对情侣来到电影院观看电影.在电影院,恰好留有 \(n\) 排座位,每排包含 \(2\) 个座位,共 \(2×n\) 个座位. 现在,每个人将 ...