Super Mario

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4090    Accepted Submission(s): 1883

Problem Description
Mario is world-famous plumber. His “burly” figure and 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.
 
Input
The 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.)
 
Output
For 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
 
  题意:对于一个序列,查询每个h在区间中的大小排名,用主席树就可以了。
  然而这一题内存限制32Mb,经过不断地优化,过的时候还占用32711Kb,唉,别人没用结构体的,内存占用只有3000Kb。
  还有一个遗憾,这里的Solve函数其实可以用一个lower_bound()函数替代。
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct Node{
int rs,ls,sum;
}tr[];
int A[],B[];
int rt[],pos,cnt;
void Build(int &node,int a,int b)
{
node=++cnt;
if(a==b)return;
int mid=(a+b)>>;
Build(tr[node].ls,a,mid);
Build(tr[node].rs,mid+,b);
} void Insert(int pre,int &node,int a,int b)
{
node=++cnt;
tr[node].ls=tr[pre].ls;
tr[node].rs=tr[pre].rs;
tr[node].sum=tr[pre].sum+;
if(a==b)return;
int mid=(a+b)>>;
if(mid>=pos)Insert(tr[pre].ls,tr[node].ls,a,mid);
else Insert(tr[pre].rs,tr[node].rs,mid+,b);
}
int Query(int pre,int node,int p,int a,int b)
{
if(b<=p)return tr[node].sum-tr[pre].sum;
int ret=;
ret=Query(tr[pre].ls,tr[node].ls,p,a,(a+b)>>);
if(p>(a+b)>>)
ret+=Query(tr[pre].rs,tr[node].rs,p,((a+b)>>)+,b);
return ret;
}
void Solve(int pre,int node,int h,int a,int b)
{
if(a==b){
pos=a;
return;
}
if(B[((a+b)>>)+]<=h)Solve(tr[pre].rs,tr[node].rs,h,((a+b)>>)+,b);
if(pos==-&&B[a]<=h)Solve(tr[pre].ls,tr[node].ls,h,a,(a+b)>>);
}
void Init()
{
memset(tr,,sizeof(tr));
cnt=;
}
int main()
{
int Q,n,q,kase=;
scanf("%d",&Q);
while(Q--)
{
Init();
printf("Case %d:\n",++kase);
scanf("%d%d",&n,&q);
for(int i=;i<=n;B[i]=A[i],i++)
scanf("%d",&A[i]);
sort(B+,B+n+);
Build(rt[],,n);
for(int i=;i<=n;i++)
{
pos=lower_bound(B+,B+n+,A[i])-B;
Insert(rt[i-],rt[i],,n);
}
int l,r,h;
for(int i=;i<=q;i++)
{
scanf("%d%d%d",&l,&r,&h);l++;r++;
pos=-;Solve(rt[l-],rt[r],h,,n);
if(pos==-){
printf("0\n");
continue;
}
printf("%d\n",Query(rt[l-],rt[r],pos,,n));
}
}
return ;
}

主席树:HDU 4417 Super Mario的更多相关文章

  1. HDU 4417 Super Mario(主席树求区间内的区间查询+离散化)

    Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  2. hdu 4417 Super Mario (主席树)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=4417 题意: 给你段长为n的序列,有q个询问,每次询问区间[l.r]内有多少个数小于等于k 思路: 之前用 ...

  3. hdu 4417 Super Mario 树状数组||主席树

    Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Prob ...

  4. HDU 4417 Super Mario 主席树

    分析:找一个区间里小于等于h的数量,然后这个题先离散化一下,很简单 然后我写这个题主要是熟悉一下主席树,其实这个题完全可以离线做,很简单 但是学了主席树以后,我发现,在线做,一样简单,而且不需要思考 ...

  5. HDU 4417 Super Mario(主席树 区间不超过k的个数)题解

    题意:问区间内不超过k的个数 思路:显然主席树,把所有的值离散化一下,然后主席树求一下小于等于k有几个就行.注意,他给你的k不一定包含在数组里,所以问题中的询问一起离散化. 代码: #include& ...

  6. HDU 4417 Super Mario(线段树)

    Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  7. HDU 4417 Super Mario(划分树)

    Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  8. HDU 4417 Super Mario (划分树)(二分)

    Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  9. HDU 4417 Super Mario(划分树问题求不大于k的数有多少)

    Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

随机推荐

  1. Servlet创建、编译、部署、运行

    最近在做一个通过Servlet实现后台批量接收上传文件的东西,现将Servlet的开发.运行配置.调用记录下来.我们以最简单的FileUpload为例,目前所有的http协议相关的Servlet均继承 ...

  2. 10.25 noip模拟试题

    今天题目略水2333 依旧不粘题目了23333 T1 /*数学题 给定n个斜率 求有多少个三元组 保证两两斜率不同 ans=C(n,3)-ΣC(len[i],2)*(n-len[i])-ΣC(len[ ...

  3. JQ 日期格式化

    将字符转换为日期格式: function getDate(strDate) { var date = eval('new Date(' + strDate.replace(/\d+(?=-[^-]+$ ...

  4. Android studio混淆

    看了一篇关于Android studio混淆的文章http://blog.csdn.net/qq_23547831/article/details/51581491,感觉有必要总结一个简单的混淆版本设 ...

  5. iis6 下发布MVC2项目的方法

    1.安装MVC2运行库,否则会出现错误 [以下转载]http://blog.csdn.net/xw13106209/article/details/6323695 错误:”未能加载文件或程序集“Sys ...

  6. oracle-绑定变量学习笔记(未完待续)

    --定义变量SQL> var a number; --给绑定变量赋值SQL> exec :a :=123; PL/SQL procedure successfully completed. ...

  7. Deep Learning学习随记(二)Vectorized、PCA和Whitening

    接着上次的记,前面看了稀疏自编码.按照讲义,接下来是Vectorized, 翻译成向量化?暂且这么认为吧. Vectorized: 这节是老师教我们编程技巧了,这个向量化的意思说白了就是利用已经被优化 ...

  8. 设置css三种方法的优先级

    有的小伙伴问了,如果有一种情况:对于同一个元素我们同时用了三种方法设置css样式,那么哪种方法真正有效呢?在下面代码中就出现了这种情况 1.使用内联式CSS设置“超酷的互联网”文字为粉色. 2.然后使 ...

  9. [转]mysql 导入导出数据库以及函数、存储过程的介绍

    本篇文章是对mysql中的导入导出数据库命令以及函数.存储过程进行了详细的分析介绍,需要的朋友参考下: mysql常用导出数据命令:1.mysql导出整个数据库  mysqldump -hhostna ...

  10. angular.js 简单的表达式

    <!doctype html> <html> <head> <meta charset ="utf-8"> <script s ...