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 思路: 之前用 ...
随机推荐
- 重学c#系列——异常续[异常注意事项](七)
前言 对上节异常的补充,也可以说是异常使用的注意事项. 正文 减少try catch的使用 前面提及到,如果一个方法没有实现该方法的效果,那么就应该抛出异常. 如果有约定那么可以按照约定,如果约定有歧 ...
- 串口通信—USB转串口
如何使用c库printf
- Qt编译出现cc1plus.exe: out of memory allocating 65536 bytes问题
今天编译Qt程序,出现这个问题: cc1plus.exe: out of memory allocating 65536 bytes 这个还没有遇到过,上网查了下.问题原因是资源文件过大. qt的资源 ...
- 039_go语言中的排序
代码演示: package main import "fmt" import "sort" func main() { strs := []string{&qu ...
- python设计模式之代理模
python设计模式之代理模式 在某些应用中,我们想要在访问某个对象之前执行一个或多个重要的操作,例如,访问敏感信息--在允许用户访问敏感信息之前,我们希望确保用户具备足够的权限.操作系统中也存在类似 ...
- JAVA 下载单个文件
public void toDownLoad(String ape505, HttpServletRequest request, HttpServletResponse response) thro ...
- 记录一次idae和maven设置的巨坑
这个忽略pom.xml文件千万别勾选,不然会导致项目的pom.xml怎么填写都无法导入新的依赖包!
- JUC----04
目录 1.1 读写问题 1.1 读写问题 ReadWriteLockUnsafeDemo: public class ReadWriteLockUnsafeDemo { // TODO: 2020/7 ...
- 8.oracle 表查询
演示如何使用select语句,接下来对emp.dept.salgrade表结构进行解说. emp 雇员表 字段名称 数据类型 是否为空 备注 -------- ----------- -------- ...
- openstack nova 创建虚机流程
1文件 nova.api.openstack.coumpute.servers1函数 def create(self, req, body):1调用 (instances, resv_id) = se ...