hdu4177:Super Mario
主席树+离散化。给一段区间。多次询问[l,r]中有多少个数小于k。啊主席树用指针版写出来优美多了QAQ。。。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define REP(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define clr(x,c) memset(x,c,sizeof(x))
int read(){
int x=0;char c=getchar();bool f=true;
while(!isdigit(c)){
if(c=='-') f=false;c=getchar();
}
while(isdigit(c)) x=x*10+c-'0',c=getchar();
return f?x:-x;
}
const int nmax=100005;
const int inf=0x7f7f7f7f;
struct node{
node *l,*r;int sum;
};
node *root[nmax],nodes[nmax*20],*pt;
int a[nmax],b[nmax];
node* build(int l,int r){
node* op=pt++;op->sum=0;
if(l==r) return op;
int mid=(l+r)>>1;
op->l=build(l,mid);op->r=build(mid+1,r);
return op;
}
node* update(int p,int add,node* t,int l,int r){
node* op=pt++;op->sum=t->sum+add;
if(l==r) return op;
int mid=(l+r)>>1;
if(p<=mid) op->r=t->r,op->l=update(p,add,t->l,l,mid);
else op->l=t->l,op->r=update(p,add,t->r,mid+1,r);
return op;
}
int query(node* t,node* s,int x,int l,int r){
if(l==r) return t->sum-s->sum;
int mid=(l+r)>>1;
if(x<=mid) return query(t->l,s->l,x,l,mid);
else return query(t->r,s->r,x,mid+1,r)+t->l->sum-s->l->sum;
}
int main(){
int cas=read();
REP(i,1,cas){
pt=nodes;
int n=read(),m=read();
REP(j,1,n) a[j]=b[j]=read();
sort(b+1,b+n+1);
int N=unique(b+1,b+n+1)-b-1;
N++;b[N]=inf; root[0]=build(1,N);
REP(j,1,n){
int k=lower_bound(b+1,b+N+1,a[j])-b;
root[j]=update(k,1,root[j-1],1,N);
} printf("Case %d:\n",i);
REP(j,1,m){
int l=read(),r=read(),v=read();l++,r++;
int k=upper_bound(b+1,b+1+N,v)-b-1;
int ans=0;
if(k>0) ans=query(root[r],root[l-1],k,1,N);
printf("%d\n",ans);
}
}
return 0;
}
Super Mario
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4662 Accepted Submission(s):
2148
amazing jumping ability reminded in our memory. Now the poor princess is in
trouble again and Mario needs to save his lover. We regard the road to the
boss’s castle as a line (the length is n), on every integer point i there is a
brick on height hi. Now the question is how many bricks in [L, R] Mario can hit
if the maximal height he can jump is H.
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.)
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.
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
4
0
0
3
1
2
0
1
5
1
hdu4177:Super Mario的更多相关文章
- HDU 4417 Super Mario(主席树求区间内的区间查询+离散化)
Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- Teaching Your Computer To Play Super Mario Bros. – A Fork of the Google DeepMind Atari Machine Learning Project
Teaching Your Computer To Play Super Mario Bros. – A Fork of the Google DeepMind Atari Machine Learn ...
- 主席树:HDU 4417 Super Mario
Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdu4417 Super Mario 树阵离线/划分树
http://acm.hdu.edu.cn/showproblem.php?pid=4417 Super Mario Time Limit: 2000/1000 MS (Java/Others) ...
- Super Mario
Super Mario Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...
- hdu4417(Super Mario)—— 二分+划分树
Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdu 4417 Super Mario 树状数组||主席树
Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Prob ...
- 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 ...
随机推荐
- 国产CPU研究单位及现状
1.国产CPU主要研制单位 (1)高性能通用CPU(“大CPU”,主要应用于高性能计算及服务器等) 主要研发单位:中国科学院计算所.北大众志.国防科技大学.上海高性能集成电路设计中心 (2)安全适用计 ...
- Spark Streaming揭秘 Day26 JobGenerator源码图解
Spark Streaming揭秘 Day26 JobGenerator源码图解 今天主要解析一下JobGenerator,它相当于一个转换器,和机器学习的pipeline比较类似,因为最终运行在Sp ...
- 1043. Is It a Binary Search Tree
http://www.patest.cn/contests/pat-a-practise/1043 #include <stdio.h> #include <vector> u ...
- C语言数据结构之栈:中缀表达式的计算
*注:本人技术不咋的,就是拿代码出来和大家看看,代码漏洞百出,完全没有优化,主要看气质,是吧 学了数据结构——栈,当然少不了习题.习题中最难的也是最有意思的就是这个中缀表达式的计算了(可以算+-*/和 ...
- Invalid argument supplied for foreach()
将需要被遍历的数组强制转换为数组类型即可 <?php $array = null; foreach((array)$array as $value){ #..code.... } ?>
- Microsoft Office Excel 不能访问文件
问题描述: Microsoft Office Excel 不能访问文件“XX.xls”.可能的原因有: 1 文件名称或路径不存在.2 文件正被其他程序使用.3 您正要保存的工作簿与当前打开的工作簿同名 ...
- HTML网页中添加音频视频动画...(转)
在网页中适当嵌入音频和视频能够充分显示网页的多媒体特性,特别是随着宽带网的普及,使得网络广播和网络视频成为现实,网页音频和视频的重要性也日益突显.具体来说,网页音频和视频的嵌入方法主要有三种: 一.b ...
- SAX EntityResolver 的作用
1.1 何为 EntityResolver : 官方解释: 如果SAX应用程序叙事实现自定义处理外部实体,则必须实现此接口, 并使用setEntityResolver方法向SAX 驱动器注册一个实例. ...
- E437: terminal capability "cm" required
执行 vi 的时候出现:E437: terminal capability "cm" required 临时解决: export TERM=xterm
- 分享SCI写作经验和一些工具