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. U3D 实现子弹发射效果

    首先,这里子弹要模拟的相似的话,用2D刚体比较好,会有重力,自由落体运动. using UnityEngine; using System.Collections; public class gun ...

  2. SQL SERVER字符集的研究(中英文字符集,varchar,nvarchar).

    一. 试验归类测试SQL: drop table a )) insert into a values('a') insert into a values(N'a') insert into a val ...

  3. 如何设置MySQL Workbench EER Diagram 尺寸?

    ER Diagram -> Model -> Diagram Properties and Size...

  4. >=ios8 应用内跳转到系统设置界面-openURL

    iOS8以后,苹果允许从应用内跳转到系统设置,但是调试结果表明,跳不到具体的设置项,使用前应该判断当前是否能够跳转到系统设置. 代码: NSURL *url = [NSURL URLWithStrin ...

  5. C语言字符串库函数的实现

    1.strlen(字符串的长度) size_t Strlen(const char* str) { assert(str); ;; ++i) { if (str[i] == '\0') return ...

  6. latex引用多篇参考文献

    1.如何使连续的参考文献能够中间用破折号连起来?比如[6,7,8,9]变成[6-9]? 方法:在文档开始前加上下面的语句命令 \usepackage[numbers,sort&compress ...

  7. 【vc】1_Windows程序内部运行机制

    创建一个Win32应用程序步骤: 1.编写WinMain函数; 2.创建窗口(步骤如下): a.设计(一个)窗口类(WNDCLASS) b.注册(该)窗口类. c.创建窗口. d.显示并更新窗口. 3 ...

  8. mysql如何将一个表导出为excel表格

    方法一:进入到mysql的控制台,输入: 1. SELECT * INTO OUTFILE ‘./test.xls‘ FROM tb1 WHERE 1 ORDER BY id DESC  LIMIT ...

  9. ROW_NUMBER分页的注意事项

    之前在使用ROW_NUMBER分页获取数据的时候,直接用ROW_NUMBER里的SELECT语句查出了所有的数据. like this: select * from ( select row_numb ...

  10. 转载-Linux下svn搭建配置流程

    Linux下svn搭建配置流程     一.    源文件编译安装.源文件共两个,为: 1.   下载subversion源文件 subversion-1.6.1.tar.gz http://d136 ...