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> # ...
随机推荐
- CodeForces-999D Equalize the Remainders
题目链接 https://vjudge.net/problem/CodeForces-999D 题面 Description You are given an array consisting of ...
- shell之一些测试脚本
比较文件有无修改,通过修改时间判别 # !/bin/bash dir=$ for file in `ls $dir` do if [ -d $dir/$file ] then echo $file i ...
- finally代码块不被执行的情况总结
以前跟别人讨论finally关键字,我总是简单的说:“fianly代码块中的代码一定会执行,一般用来清除IO资源等非内存资源(内存资源由GC机制回收)”. 今天翻书发现了几种不会执行的情况,现在总结下 ...
- LACP链路聚合控制协议
LACP链路聚合控制协议 来源: https://www.cnblogs.com/taosim/articles/4378691.html http://storage.chinabyte.com/6 ...
- Spring Boot RabbitMQ 延迟消息实现完整版
概述 曾经去网易面试的时候,面试官问了我一个问题,说 下完订单后,如果用户未支付,需要取消订单,可以怎么做 我当时的回答是,用定时任务扫描DB表即可.面试官不是很满意,提出: 用定时任务无法做到准实时 ...
- JS获取照片拍摄的角度属性,用于旋转控制
我工作十余年,从负责一个模块,到负责一个产品,再到负责整个支付平台的架构设计,包括业务架构.产品架构到应用架构,再到技术架构,是一个从点到面逐渐转型的过程,同样是个“自相似”的现象,我一开始写博客,再 ...
- hdu 6102 GCDispower
多校6 GCDispower(容斥) 题意: 给一个长度为\(n\)的排列 给q组询问 每次查询\(L,R\)内的答案 \(\sum_{i=L}^{R}\sum_{j=i+1}^{R}\sum_{k= ...
- BZOJ1951 [Sdoi2010]古代猪文 【费马小定理 + Lucas定理 + 中国剩余定理 + 逆元递推 + 扩展欧几里得】
题目 "在那山的那边海的那边有一群小肥猪.他们活泼又聪明,他们调皮又灵敏.他们自由自在生活在那绿色的大草坪,他们善良勇敢相互都关心--" --选自猪王国民歌 很久很久以前,在山的那 ...
- 用ServletContext做一个简单的聊天室
这里主要是ServletContext的一个特性:ServletContext是一个公共的空间,可以被所有的客户访问.由此可见ServletContext比cookie和session的作用范围要大[ ...
- Angular(一)
一.Angular优点: 1.MVC:职责清晰,代码模块化. 2.模块化 3.指定系统——directive:我们已经在模板中看到了一些新的属性,这些属性不属于HTML规范.例如:我们引入了双花括号用 ...