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 ...
随机推荐
- C++类、函数、指针
1.初始化所有指针. 2. (1)指向常量的指针: (2)常量指针:指针本身为常量: 3.若循环体内部包含有向vector对象添加元素的语句,则不能使用范围for循环. 4.字符数组要注意字符串字面值 ...
- hashMap在jdk1.7与jdk1.8中的原理及不同
在分析jdk1.7中HashMap的hash冲突时,不知大家是否有个疑问就是万一发生碰撞的节点非常多怎么版?如果说成百上千个节点在hash时发生碰撞,存储一个链表中,那么如果要查找其中一个节点,那就不 ...
- HashMap:从源码分析到面试题
1 HashMap简介 HashMap是实现map接口的一个重要实现类,在我们无论是日常还是面试,以及工作中都是一个经常用到角色.它的结构如下: 它的底层是用我们的哈希表和红黑树组成的.所以我们在学习 ...
- 数据结构进阶:ST表
简介 ST 表是用于解决 可重复贡献问题 的数据结构. 什么是可重复贡献问题? 可重复贡献问题 是指对于运算 \(\operatorname{opt}\) ,满足 \(x\operatorname ...
- mysql存储引擎InnoDB详解,从底层看清InnoDB数据结构
InnoDB一个支持事务安全的存储引擎,同时也是mysql的默认存储引擎.本文主要从数据结构的角度,详细介绍InnoDB行记录格式和数据页的实现原理,从底层看清InnoDB存储引擎. 本文主要内容是根 ...
- Zookeeper学习(二)
一.Znode节点属性 dataVersion 数据版本, 每次当 Znode 中的数据发生变化的时候, dataVersion都会自增一下cversion 节点版本, 每次当 Znode 的节点发生 ...
- Django context must be a dict ranther than Context
1.1 错误描述 TypeError at /time/ context must be a dict rather than Context. Request Method: GET Request ...
- ybt1107题解和方法总结
今天花了三个小时的时间刷了些基础题,虽说是简单题,但是有一些还是有点难度的 比如ybt1107,我死嗑了半个小时. [题目描述] 某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是1米. ...
- 【期外】(三)Linux下集成开发环境Geany
今天小编发现了一个很好的软件,它的名字就叫做Geany. 这是Linux系统中的开发工具,相当的好用. Linux与windows最大的不同正是不是集成开发环境,所以写起代码来总是用文档写好后,然后再 ...
- mac启动 Apache JMeter 5.3 语言选择中文界面出现乱码 问题解决
问题重现 问题修复 出现这个问题,是因为,语言与外观不兼容导致,语言选“中文”,外观选“Metal” 细心的你,可能发现,为啥要重启2次呢???第一次设置完语言后,在设置外观,发现菜单不能选择,第二次 ...