HDU 4417 Super Mario (划分树)(二分)
Super Mario
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6077 Accepted Submission(s): 2645
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.
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.)
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.
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
4
0
0
3
1
2
0
1
5
1
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
#define lson(x) ((x<<1))
#define rson(x) ((x<<1)+1)
using namespace std;
typedef long long ll;
const int N=1e5+;
const int M=N*N+;
long long ans;
struct P_Tree {
int n;
int tree[][N];
int sorted[N];
int toleft[][N];
ll sum[N];
ll lsum[][N]; void init(int len) {
n=len;
sum[]=;
for(int i=; i<; i++)tree[i][]=toleft[i][]=lsum[i][]=;
for(int i=; i<=n; i++) {
scanf("%d",&sorted[i]);
tree[][i]=sorted[i];
sum[i]=sum[i-]+sorted[i];
}
sort(sorted+,sorted+n+);
build(,n,);
}
void build(int l,int r,int dep) {
if(l==r)return;
int mid=(l+r)>>;
int same=mid-l+;
for(int i=l; i<=r; i++) {
if(tree[dep][i]<sorted[mid])
same--;
}
int lpos = l;
int rpos = mid+;
for(int i = l; i <= r; i++) {
if(tree[dep][i] < sorted[mid]) {
tree[dep+][lpos++] = tree[dep][i];
lsum[dep][i] = lsum[dep][i-] + tree[dep][i];
} else if(tree[dep][i] == sorted[mid] && same > ) {
tree[dep+][lpos++] = tree[dep][i];
lsum[dep][i] = lsum[dep][i-] + tree[dep][i];
same --;
} else {
tree[dep+][rpos++] = tree[dep][i];
lsum[dep][i] = lsum[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) {
if(l==r)return tree[dep][l];
int mid=(L+R)>>;
int cnt=toleft[dep][r]-toleft[dep][l-];
int s=toleft[dep][l-]-toleft[dep][L-];
if(cnt>=k) {
int newl=L+toleft[dep][l-]-toleft[dep][L-];
int newr=newl+cnt-;
return query(L,mid,newl,newr,dep+,k);//×¢Òâ
} else {
ans+=(ll)(lsum[dep][r]-lsum[dep][l-]);
//printf("l: %d r: %d ans: %lld %lld %lld\n",l,r,ans,lsum[dep][r],lsum[dep][l-1]);
int newr=r+toleft[dep][R]-toleft[dep][r];
int newl=newr-(r-l-cnt);
return query(mid+,R,newl,newr,dep+,k-cnt);//×¢Òâ
}
}
} tre;
int main() {
int iCase=;
int n,m,k,T;
int l,r,h;
scanf("%d\n",&T);
while(T--) {
scanf("%d%d",&n,&m);
tre.init(n);
printf("Case %d:\n",++iCase);
while(m--) {
int Ans=;
ans=;
scanf("%d%d%d",&l,&r,&h);
l++;
r++;
int ll=,rr=(r-l)+;
while(rr-ll>=){
k=(rr+ll)/;
int res=tre.query(,n,l,r,,k);
if(res>h)rr=k-;
else {
Ans=k;
ll=k+;
}
//printf("l: %d r: %d k: %d ans: %d\n",l,r,k,Ans);system("pause");
}
printf("%d\n",Ans);
}
}
return ;
}
HDU 4417 Super Mario (划分树)(二分)的更多相关文章
- HDU 4417 - Super Mario ( 划分树+二分 / 树状数组+离线处理+离散化)
题意:给一个数组,每次询问输出在区间[L,R]之间小于H的数字的个数. 此题可以使用划分树在线解决. 划分树可以快速查询区间第K小个数字.逆向思考,判断小于H的最大的一个数字是区间第几小数,即是答案. ...
- HDU 4417 Super Mario(划分树+二分)
题目链接 #include <cstdio> #include <cstring> #include <algorithm> using namespace std ...
- HDU 4417 Super Mario(划分树)
Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 4417 Super Mario(划分树问题求不大于k的数有多少)
Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU-4417 Super Mario,划分树+二分!
Super Mario 这个题也做了一天,思路是很清晰,不过二分那里写残了,然后又是无限RE.. 题意:就是查询区间不大于k的数的个数. 思路:裸划分树+二分答案.将区间长度作为二分范围.这个是重点. ...
- HDU 4417 Super Mario ( 离线树状数组 )
把数值和查询放在一起从小到大排序,纪录每个数值的位置,当遇到数值时就更新到树状数组中,遇到查询就直接查询该区间和. #include <cstdio> #include <cstri ...
- HDU 4417 Super Mario 主席树
分析:找一个区间里小于等于h的数量,然后这个题先离散化一下,很简单 然后我写这个题主要是熟悉一下主席树,其实这个题完全可以离线做,很简单 但是学了主席树以后,我发现,在线做,一样简单,而且不需要思考 ...
- HDU 4417 Super Mario 主席树查询区间小于某个值的个数
#include<iostream> #include<string.h> #include<algorithm> #include<stdio.h> ...
- 【划分树+二分】HDU 4417 Super Mario
第一次 耍划分树.. . 模板是找第k小的 #include <stdio.h> #include <string.h> #include <stdlib.h> # ...
随机推荐
- mac虚拟机上(centos系统)怎样实现共享本机文件
首先加载vboxadditions,可以从https://download.virtualbox.org/virtualbox/下载,记得一定要跟virtualBox版本对应 然后打开virtualb ...
- Oracle数据库存量数据抽取使用spool控制命令
spool是oracle sqlplus提供的一个控制命令.可以利用spool和select语句的组合动态生成一些失去了脚本或者一些数据. 1.spool作用: 在sqlplus中用来保存或打印查询 ...
- DataSource的设置
1.Centos和redhat,Fedora等版本无须设置,直接在cloud.cfg指定,默认是EC2 datasource_list: ['ConfigDrive','OpenStack'] 2.u ...
- PEAR DB 初学笔记
1.数据查询 i. DB_common::getAll() DB_FETCHMODE_ORDERED . DB_FETCHMODE_ASSOC . DB_FETCHMODE_OBJECT ii. DB ...
- SQLSTATE[HY000] [2003] Can't connect to MySQL server on 'XXX' (13)
SQLSTATE[HY000] [2003] Can't connect to MySQL server on 'XXX' (13) 我可以真见识了 SELinux 的利害了, 这个问题让我找了好长时 ...
- 【linux】如何解决VMWare上linux虚拟机连不上外网的问题?
>>>故障现象:虚拟机连接不到外网? >>>故障背景: Centos7.4发行版本: 虚拟机和VM软件都是nat模式: 注意这里默认的VMWare的DHCP服务时开 ...
- SQL小助手——SQL Prompt
背景: 当数据库设计的比较复杂.庞大时,我们如果对脚本不是很熟悉,就会很难完成看似简单的增.删.改.查的操作.我们需要一款软件来给出相应的提示或帮助,来提高代码的可读性,更快更好的完成任务. 简介: ...
- PHP面向对象 封装与继承
知识点: PHP封装三个关键词: 一.public 公有的,被public修饰的属性和方法,对象可以任意访问和调用 二.private 私有的,被private修饰的属性和方法,只能在类内部的方法可以 ...
- InputStream 、 InputStreamReader 、 BufferedReader
1.InputStream.OutputStream 处理字节流的抽象类 InputStream 是字节输入流的所有类的超类,一般我们使用它的子类,如FileInputStream等. OutputS ...
- Search Engine —— Regular Expression(Spider)
Regular Expression,即正则表达式:用来查找符合某些负责规则的字符串的需要.它真是用于描述这些规则的工具. 1. \b 是一个元字符,用来匹配一个位置,代表着单词的开头或结尾,也就是单 ...