Super Mario

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

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
 
题意:给出n个数组成的无序序列,序列中的每个数表示一块砖的高度,有m个查询,每个查询三个数,前两个数表示序列的一个子区间,第三个数表示马里奥能跳到的最大高度,问你在这个子区间中,马里奥共能跳过多少个砖头。
题解:我们可以这么想,对于一个子区间,我们将它按从小到大排好序,然后对这个区间进行二分,找到这个区间排序后的中间值,若马里奥能跳过的最大高度大于中间值,则可得知马里奥可以跳过排序后区间的前半段,然后对区间后边段进行二分;若最大高度小于中间值,则对排序后区间前半部分进行二分,一直持续二分下去,直到结束。最终二分的停止位置就是可以被跳过的砖的数量。
  而每次查询都对子区间进行排序的话时间复杂度太高,所以我们可以用划分树对区间进行维护。划分树的作用就是查找区间当中的第k大的值,这里我就不讲解划分树了,不会的可以先去学一学。每次二分都用划分树找出当前的第mid个值,然后与马里奥能跳过的最大值进行比较。
 
具体看代码:
 #include<iostream>
#include<iomanip>
#include<cstring>
#include<cstdio>
#include<string>
#include<cmath>
#include<algorithm>
#include<fstream>
#include<stack>
#include<climits>
#include<queue>
#define eps 1e-7
//#define ll long long
#define inf 0x3f3f3f3f
#define pi 3.141592653589793238462643383279
using namespace std;
const int MAXN = 1e5 + ;
int n,m,tree[][MAXN],sorted[MAXN],toleft[][MAXN]; void build(int l,int r,int dep) //建立划分树
{
if(l == r) return; int mid = (l + r)>>;
int lpos = l;
int rpos = mid + ;
int same = mid - l + ;
for(int i=l; i<=r; ++i)
if(tree[dep][i] < sorted[mid])
same--; for(int i=l; i<=r; ++i)
{
if(tree[dep][i] < sorted[mid])
tree[dep+][lpos++] = tree[dep][i];
else if(tree[dep][i] == sorted[mid] && same > )
{
tree[dep+][lpos++] = tree[dep][i];
same--;
}
else tree[dep+][rpos++] = tree[dep][i];
toleft[dep][i] = toleft[dep][l-] + lpos - l;
} build(l,mid,dep+);
build(mid+,r,dep+);
} int query(int l,int r,int L,int R,int dep,int k) //划分树查询区间第k大值
{
if(l == r)
return tree[dep][l];
int mid = (L + R) >> ;
int cnt = toleft[dep][r] - toleft[dep][l-]; int newl,newr;
if(cnt >= k)
{
newl = L + toleft[dep][l-] - toleft[dep][L-];
newr = newl + cnt - ;
return query(newl,newr,L,mid,dep+,k);
}
else
{
newr = r + (toleft[dep][R] - toleft[dep][r]);
newl = newr - r + l + cnt;
return query(newl,newr,mid+,R,dep+,k - cnt);
}
} int solve(int ll,int rr,int h) //计算区间当中可以被跳过的砖头的数量
{
int l = ,r = (rr-ll) + ,ans=;
while(l <= r)
{
int mid = (l + r) >> ; //取当前区间的中间值下标
if(query(ll,rr,,n,,mid) <= h) //划分树查询得到中间值,若中间值 <= h
{
l = mid + ; //缩小区间
ans = mid;
}
else r = mid - ;
}
return ans;
} int main()
{
int t,cnt = ;
cin>>t;
while(t--)
{
cin>>n>>m;
for(int i=; i<=n; ++i)
{
scanf("%d",&sorted[i]);
tree[][i] = sorted[i];
}
sort(sorted+, sorted++n);
build(,n,); printf("Case %d:\n",++cnt);
int l,r,h;
while(m--)
{
scanf("%d%d%d",&l,&r,&h);
printf("%d\n",solve(l+,r+,h));
}
}
return ;
}

hdu4417(Super Mario)—— 二分+划分树的更多相关文章

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

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

  2. HDOJ题目4417 Super Mario(划分树求区间比k小的个数+二分)

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

  3. HDU4417 Super Mario(主席树)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=4417 Description Mario is world-famous plumber. ...

  4. [HDU4417]Super Mario(主席树+离散化)

    传送门 又是一道主席树模板题,注意数组从0开始,还有主席树耗费空间很大,数组开大点,之前开小了莫名其妙TLE.QAQ ——代码 #include <cstdio> #include < ...

  5. HDU--4417 Super Mario (主席树模版题)

    题目链接 题目让求 L R区间 不大于H 的数有多少 数据太大需要离散化 #include<bits/stdc++.h> using namespace std; #define maxn ...

  6. HDU-4417 Super Mario,划分树+二分!

    Super Mario 这个题也做了一天,思路是很清晰,不过二分那里写残了,然后又是无限RE.. 题意:就是查询区间不大于k的数的个数. 思路:裸划分树+二分答案.将区间长度作为二分范围.这个是重点. ...

  7. hdu4417 Super Mario 树阵离线/划分树

    http://acm.hdu.edu.cn/showproblem.php?pid=4417 Super Mario Time Limit: 2000/1000 MS (Java/Others)    ...

  8. hdu-4417 Super Mario(树状数组 + 划分树)

    题目链接: Super Mario Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 32768/32768 K (Java/Other ...

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

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

随机推荐

  1. 抽象类(abstract class)

    package com.bjsxt.oop.abstractClass; //抽象类 public abstract class Animal { //因为父类的方法总是被重写 那就没写的必要了 但是 ...

  2. 30分钟新手git教程

    本文转载自:http://igeekbar.com/igeekbar/post/82.htm Git近些年的火爆程度非同一般,这个版本控制系统被广泛地用在大型开源项目(比如Linux),不同规模的团队 ...

  3. ping,telnet,tracert分别用的是什么协议

    Telnet使用的是tcp协议使用示例:telnet 192.168.1.20 80 ping命令使用的是icmp协议示例:ping www.sina.com.cn或ping 192.168.1.10 ...

  4. Unicode、UTF8与UTF16

    1 概念 Unicode是国际组织制定的可以容纳世界上所有文字和符号的字符编码方案 UTF是“Unicode Transformation Format”的缩写,可以翻译成Unicode字符集转换格式 ...

  5. ubuntu下安装.deb包的安装方法

    ubuntu16.04的软件中心应该是有bug,安装不了第三方.deb文件,我们只有使用dpkg -i 或者gdebi的方式安装,我使用的是后者,因为后者功能更加强大.要使用gdebi命令先要安装它: ...

  6. selinux介绍及关闭selinux指南

    selinux简介 SELinux(Security-Enhanced Linux) 是美国国家安全局(NSA)对于强制访问控制的 实现,是 Linux历史上最杰出的新安全子系统.NSA是在Linux ...

  7. C++ define与const

    C++中不但可以用define定义常量还可以用const定义常量,它们的区别如下: 用#define MAX 255定义的常量是没有类型的,所给出的是一个立即数,编译器只是把所定义的常量值与所定义的常 ...

  8. c#中@的3种作用

    以前只知道@在C#中为了写文件路径的\不要加转义符而在前面加上@标识符,没想到@还有其他的作用 1.忽略转义字符例如 string fileName = "D:\\文本文件\\text.tx ...

  9. php的opcode缓存原理

    opcode是什么? 它是一种PHP脚本编译后的中间语言,类似java的字节码.   PHP代码执行(Zend引擎)的步骤如下: 1.Scanning(Lexing) ,将PHP代码转换为语言片段(T ...

  10. 【笔记】C#往TextBox的方法AppendText加入的内容里插入换行符

    C# TextBox换行[huan hang]时你往往会想到直接付给一个含有换行[huan hang]符"\n"的字符[zi fu]串[zi fu chuan]给Text属性[sh ...