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. datagrid后台给每列添加js方法

    protected void dgExhList_ItemDataBound(object sender, DataGridItemEventArgs e) { string param = &quo ...

  2. StudioStyle 使用 厌倦了默认的Visutal Studio样式了,到这里找一个酷的试试

    厌倦了默认的Visutal Studio样式了,到这里找一个酷的试试 http://studiostyl.es/ 去下载个自己喜欢的编码样式吧 如果你有想法 有能力 可以自己去做一个自己喜欢的  OK ...

  3. JSONModel的基本使用

    JSONModel 是一个库,它能智能并且快速的创建出数据 model,你可以在你的 iOS 项目或者 OSX 项目上使用它. 使用前准备 添加 JSONModel 到你的工程中 1.需要的环境: A ...

  4. iOS里面消除使用代理调用方法时间警告问题

    iOS里面有三种调用函数的方式: 直接调用方法   [对象名 方法]; performselector:    [对象名 perform方法]; NSInvocation     调用 在使用代理调用 ...

  5. Spring中@Autowired注解与自动装配

    1 使用配置文件的方法来完成自动装配我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. ...

  6. 插入排序(C++)

    插入排序(C++) 插入排序: 写这篇博文是为了增加对数据结构和算法的理解,同事增加编程的基本功. 当要对如下数据进行排序: 2,8,5,4,6,7,1 2,8,5,4,6,7,1    采用插入排序 ...

  7. js转换/Date(........)/

    eval('new ' + (datetime.replace(/\//g, ''))); 好记性不如烂笔头,记下以备后用.

  8. photoshop cc 版本安装失败解决办法

    好久没有碰ps,看了下在ps版本都到cc了.忍不住也想尝试最新版本,但是安装出现了很多问题,导致我花了很多时间才搞定,现在分享给大家几点经验吧. Exit Code: Please see speci ...

  9. CSS实现DIV三角形

    本文内容收集来自网络 #triangle-up { width:; height:; border-left: 50px solid transparent; border-right: 50px s ...

  10. apache rewrite .htaccess 站点内容重定向实例

    <IfModule mod_rewrite.c> Options +FollowSymlinks RewriteEngine On RewriteCond %{REQUEST_FILENA ...