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> # ...
随机推荐
- Action参数和View、Json、重定向
一.Action 1.Action参数: 普通参数.Model类.FormCollection (1).普通参数 Index(string name,int age) 框架会自动把用户请求的Que ...
- 使用dib element proliant-tools制作deploy image
element proliant-tools会在ipa ramdisk中安装一个rpm包hpssacli(HP的RAID管理工具),和一个python module proliantutils(里面P ...
- android studio 配置网络代理
1.首先在vultr网站购买服务器. 然后使用shadowsocksR给服务器配置FQ,再在本地机器配置好shadowsocksR. 参考网址:https://github.com/getlanter ...
- 网络--路由表&IP选路
路由表的 flags 字段显示路由状态: A 活动的休眠网关检测在路由上被启用.本字段只适用于 AIX 5.1 或更新版本. U :Up. H :路由至主机而不是网络. G :路由至网关. 不带G表示 ...
- 在Struts2 Action中快速简便的访问Request、Session等变量
前言——正常情况下如何在Action中获取到这些变量 全部方法(共四种)可以参考:http://blog.csdn.net/itmyhome1990/article/details/7019476 这 ...
- eclipse 运行错误:在类XXX中找不到 main 方法, 请将 main 方法定义为: public static void main(String[] args) 否则 JavaFX 应用程序类必须扩展javafx.application.Application
新建了一个类Hello: 代码: 第一次运行报错: 点击关闭该类的界面时出现: 点击是,然后再次打开,可以正确执行,结果为: 这是为什么....,后来发现了原因:是每次运行或调试前没有自动保存编辑的内 ...
- jquery select chosen禁用某一项option
$("#tbParBudCode").chosen().change(function () { $("#tbParBudCode option[value='" ...
- WSingle主题 – 可能是最好的WordPress小说主题,美观大方,功能强大
今天,waitig给大家带来了一款强大WordPress小说主题 – WSingle主题. 一.概览 WSingle主题2.0版本已经发布,点击查看详情:[重磅]WSingle主题2.0版本发布,新增 ...
- 【bzoj4378】[POI2015]Logistyka 离散化+树状数组
题目描述 维护一个长度为n的序列,一开始都是0,支持以下两种操作:1.U k a 将序列中第k个数修改为a.2.Z c s 在这个序列上,每次选出c个正数,并将它们都减去1,询问能否进行s次操作.每次 ...
- idiots
idiots 题目描述 给定 $n$ 个长度分别为 $a_i$ 的木棒,问随机选择 $3$ 个木棒能够拼成三角形的概率. 输入格式 第一行一个正整数 nn. 第二行 nn 个正整数,第 ii 个数表示 ...