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 代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<map>
#include<cmath>
const int maxn=1e5+;
typedef long long ll;
using namespace std; struct node
{
int l,r;
int num;
}tree[maxn<<]; struct node1
{
int pos,l,r,h;
bool friend operator <(node1 x, node1 y)
{
return x.h<y.h;
}
}h[maxn];
struct node2
{
int pos,h;
bool friend operator <(node2 x,node2 y)
{
return x.h<y.h;
}
}p[maxn]; void pushup(int m)
{
tree[m].num=(tree[m<<].num+tree[m<<|].num);
return;
}
void build(int m,int l,int r)
{
tree[m].l=l;
tree[m].r=r;
tree[m].num=;
if(l==r)
{
return ;
}
int mid=(tree[m].l+tree[m].r)>>;
build(m<<,l,mid);
build(m<<|,mid+,r);
return;
}
void update(int m,int index,int val)
{
if(tree[m].l==index&&tree[m].l==tree[m].r)
{
tree[m].num=val;
return ;
}
int mid=(tree[m].l+tree[m].r)>>;
if(index<=mid)
{
update(m<<,index,val);
}
else
{
update(m<<|,index,val);
}
pushup(m);
return ;
}
int query(int m,int l,int r)
{
if(tree[m].l==l&&tree[m].r==r)
{
return tree[m].num;
}
int mid=(tree[m].l+tree[m].r)>>;
if(r<=mid)
{
return query(m<<,l,r);
}
else if(l>mid)
{
return query(m<<|,l,r);
}
else
{
return query(m<<,l,mid)+query(m<<|,mid+,r);
}
}
int a[maxn];
int main()
{
int T;
cin>>T;
for(int k=;k<=T;k++)
{
int n,m;
scanf("%d%d",&n,&m);
for(int t=;t<=n;t++)
{
scanf("%d",&p[t].h);
p[t].pos=t;
}
for(int t=;t<=m;t++)
{
scanf("%d%d%d",&h[t].l,&h[t].r,&h[t].h);
h[t].l++;
h[t].r++;
h[t].pos=t;
} printf("Case %d:\n",k);
build(,,n);
sort(p+,p+n+);
sort(h+,h+m+);
int j=;
int s=;
while(s<=n)
{
if(p[s].h>h[j].h)
{
// cout<<h[j].l<<" "<<h[j].r<<endl;
a[h[j].pos]=query(,h[j].l,h[j].r);
j++;
if(j>m)
{
break;
}
}
else
{
update(,p[s].pos,);
s++;
}
}
while(j<=m)
{
a[h[j].pos]=query(,h[j].l,h[j].r);
j++;
}
for(int t=;t<=m;t++)
{
printf("%d\n",a[t]);
}
} return ;
}
HDU-4417-Super Mario(线段树+离线处理)的更多相关文章
- HDOJ 4417 - Super Mario 线段树or树状数组离线处理..
题意: 同上 题解: 抓着这题作死的搞~~是因为今天练习赛的一道题.SPOJ KQUERY.直到我用最后一种树状数组通过了HDOJ这题后..交SPOJ的才没超时..看排名...时间能排到11名了..有 ...
- 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 主席树查询区间小于某个值的个数
#include<iostream> #include<string.h> #include<algorithm> #include<stdio.h> ...
- HDU 4417 - Super Mario ( 划分树+二分 / 树状数组+离线处理+离散化)
题意:给一个数组,每次询问输出在区间[L,R]之间小于H的数字的个数. 此题可以使用划分树在线解决. 划分树可以快速查询区间第K小个数字.逆向思考,判断小于H的最大的一个数字是区间第几小数,即是答案. ...
- HDU 4417 Super Mario 主席树
分析:找一个区间里小于等于h的数量,然后这个题先离散化一下,很简单 然后我写这个题主要是熟悉一下主席树,其实这个题完全可以离线做,很简单 但是学了主席树以后,我发现,在线做,一样简单,而且不需要思考 ...
- HDU 4417 Super Mario(划分树+二分)
题目链接 #include <cstdio> #include <cstring> #include <algorithm> using namespace std ...
- HDU 4417 Super Mario(2012杭州网络赛 H 离线线段树)
突然想到的节约时间的方法,感觉6翻了 给你n个数字,接着m个询问.每次问你一段区间内不大于某个数字(不一定是给你的数字)的个数 直接线段树没法做,因为每次给你的数字不一样,父节点无法统计.但是离线一 ...
- hdu 4417 Super Mario 离线线段树
思路:将点按值从小到大排序,询问按h从小到大排序. 在建立线段树,按h的大小更新树并得到该次查询的结果! 代码如下: #include<iostream> #include<stdi ...
- HDU 4417 Super Mario(线段树)
Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
随机推荐
- HiddenHttpMethodFilter进行请求过滤
基于 HiddentHttpMethodFilter 的示例 作用: 由于浏览器 form 表单只支持 GET 与 POST 请求,而 DELETE.PUT 等 method 并不支持,Spring3 ...
- 移动端与Web端疫情数据展示
1.题目要求 2.整体思想 首先是在前两阶段已经完成的echarts可视化.利用Jsoup爬取疫情数据基础上来进行调用与完善.大致思想是在Android Studio上完成交互去调用ecplise中的 ...
- 无法加载文件或程序集“System.Net.Http,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a”
原因是:System.Net.Http.dll 使用了net4.6的版本的.而System.Net.Http.Formatting.dll使用了4.5的版本. 解决方案:将webconfig文件下的n ...
- PHP7 生产环境队列 Beanstalkd 正确使用姿势
应用场景 为什么要用呢,有什么好处?这应该放在最开头说,一件东西你只有了解它是干什么的,适合干什么,才能更好的与自己的项目相结合,用到哪里学到哪里,学了不用等于不会,我们平时就应该多考虑一些这样的问题 ...
- 用Python来搞副业?这届大学生到底有多野……
最近,我在知乎上偶然发现一个有意思的问题: 「大学生实习被当作廉价劳动力,你怎么看?」 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很 ...
- CentOS yum 安装nginx
当使用以下命令安装Nginx时,发现无法安装成功 yum install -y nginx 需要做一点处理. 安装Nginx源 执行以下命令: rpm -ivh http://nginx.org/pa ...
- myBatis源码解析-数据源篇(3)
前言:我们使用mybatis时,关于数据源的配置多使用如c3p0,druid等第三方的数据源.其实mybatis内置了数据源的实现,提供了连接数据库,池的功能.在分析了缓存和日志包的源码后,接下来分析 ...
- 我搭的神经网络不work该怎么办!看看这11条新手最容易犯的错误
1. 忘了数据规范化 2. 没有检查结果 3. 忘了数据预处理 4. 忘了正则化 5. 设置了过大的批次大小 6. 使用了不适当的学习率 7. 在最后一层使用了错误的激活函数 8. 网络含有不良梯度 ...
- [HNOI2009]最小圈 题解
题目大意 给你一个有向图,求出图中环的平均值的最小值 环的平均值定义:环中所有的边权和/环中点数量 思路 看到使平均值最大或最小,可以考虑分数规划 分数规划用于解决一些要让平均值最大或最小的问题 具体 ...
- JS精度损失toFixed
1234*0.01=12.3400000001 很明显后缀00001跟预期想要的不一致,起初面临这个问题我的处理方式是这样的: (1234*0.01).toString().substring(0,2 ...