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. SQL:42601

    以前遇到SQL中触发器的问题, 添加一个字段后,发现以前的程序的SQL报错 看了下表定义,有这样的一行代码 CREATE TRIGGER chintai_keiyaku_audit AFTER INS ...

  2. Axiom3D学习日记 0.Axiom基础知识

    Axiom 3D Engine An open-source, cross-platform, managed 3D rendering engine for DirectX, XNA and Ope ...

  3. ASP.NET Excel数据导入数据库

    <identity impersonate="true"/> 是指模拟IIS身份验证 導入錯誤時可刪除 protected void btnImport_Click(o ...

  4. iOS 小知识 - #if , #ifdef , #ifndef.

    Q : 在项目的 .h 文件中, #ifndef XXX_h #define XXX_h //程序段1 #endif  /* XXX_h */ 的作用? A : 如果 XXX.h 不存在,就引入 XX ...

  5. [转]mysql导入导出数据中文乱码解决方法小结

    本文章总结了mysql导入导出数据中文乱码解决方法,出现中文乱码一般情况是导入导入时编码的设置问题,我们只要把编码调整一致即可解决此方法,下面是搜索到的一些方法总结,方便需要的朋友. linux系统中 ...

  6. csv文本编辑引号问题

    今天发现一个csv的一个问题,csv工具类对于引号默认有特殊的处理.我希望写出来的结果是 1,"1",1 原来的代码是 CsvWriter cw=new CsvWriter(&qu ...

  7. 防止iframe嵌套

    如果你哪个页面不想被嵌套 下面js代码可以解决(我的是火狐) 慎用 <script type="text/javascript">          window.on ...

  8. iOS图片的伪裁剪(改变图片的像素值)

    0x00 原理 利用一张图片事先画好的图片(以下称为蒙板),盖在要被裁剪的的图片上,然后遍历蒙板上的像素点,修改被裁剪图片对应位置的像素的色值即可得到一些我们想要的不规则图片了(比如人脸) 0x01 ...

  9. Extjs4.2.1学习笔记[更新]

    心血来潮准备学习一下Extjs,就从官方网站http://extjs.org.cn/下载了最新版本4.2.1,开始从头学习,记一下笔记,让自己能够持之以恒. 先说一下基本文件类库引用吧, 每个项目一开 ...

  10. YII 小部件 yii小部件查看方法 小物件做的表单

    要使用小部件,可以先到总文件去找   framework/yiilite文件里面搜索“CAtiveForm” (如果觉得小部件的radio布局有点难看,可以在外面定义,具体可以在控制器里面定义) 如下 ...