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 ...
随机推荐
- Linux系统中玩到让你停不下来的命令行游戏!
大家好,我是良许. 在使用 Linux 系统时,命令行不仅可以让我们在工作中提高效率,它还可以在生活上给我们提供各种娱乐活动,因为你可以使用它玩许多非常有意思的游戏,这些游戏可都不需要使用专用显卡. ...
- SpringBoot2.x集成Redis (StringTemplate与redisTemplate的用法)
1. Redis介绍Redis数据库是一个完全开源免费的高性能Key-Value数据库.它支持存储的value类型有五种,包括string(字符串).list(链表).set(集合).zset(sor ...
- CSS动画实例:图文切换
先准备好一张图片,在页面中放置一个类名为container的层作为图文容器,在该层中再定义两个层:一个类名为image-box的层放置图片,一个类名为text-desc的层放置文本描述,HTML代码描 ...
- java 异常二
一 捕获异常try…catch…finally 捕获:Java中对异常有针对性的语句进行捕获,可以对出现的异常进行指定方式的处理 捕获异常格式: try { //需要被检测的语句. } catch(异 ...
- 2020-07-14:es用过冷热分离吗?假如现在有些数据热变冷,有些数据冷变热,怎么解决?
福哥答案2020-07-14: 热变冷: 有x台机器tag设置为hot. 有y台机器tag设置为cool. hot集群中只存最近两天的. 有一个定时任务每天将前一天的索引标记为cool. es看到有新 ...
- C#设计模式之7-桥接模式
桥接模式(Bridge Pattern) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/401 访问. 桥接模式属于结构 ...
- sql server 查询表字段的说明备注信息
SELECT 表名 = case when a.colorder= then d.name else '' end, 表说明 = case when a.colorder= then isnull(f ...
- 0x01 基本算法-位运算 a^b
#include<bits/stdc++.h>using namespace std;int power(int a, int b, int p){ int ans = 1%p; ...
- HDFS的数据流读写数据 (面试开发重点)
1 HDFS写数据流程 1.1 剖析文件写入 HDFS写数据流程,如图所示 1)客户端通过Distributed FileSystem模块向NameNode请求上传文件,NameNode检查目标文件是 ...
- 如何处理遇到的错误-lammps
一.认清模型——data文件: 二.读懂in文件: 三.当遇到error时,we can do: 1.查错: (1)从out文件中,复制里边的错误信息(copy一部分,不用全部): (2)进入到lam ...