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> # ...
随机推荐
- python学习笔记八:文件与目录
一.文件的打开和创建 1.打开 open(file,mode): >>>fo = open('test.txt', 'r') >>>fo.read() 'hello ...
- MQ消息中间件
MQ是什么? MQ是Message Queue消息队列的缩写.消息队列是一种应用程序对应用程序的通信方法.应用程序通过写和检索入列队的针对应用程序的数据(消息)来进行通信,而不需要专用连接来链接它们. ...
- python tarfile模块打压缩包,arcname的用法
D:\szh\noses文件夹下有子文件夹和文件 with tarfile.open('E:\\szh.tar', "w") as tar: tar.add('D:\\ ...
- CodeForces-1061D TV Shows
题目链接 https://vjudge.net/problem/CodeForces-1061D 题面 Description There are nn TV shows you want to wa ...
- CentOS7 编译安装nodejs,配置环境变量记录
每次都装,每次都查 阿里云备案了一个域名,续费了好多年,但是没钱买服务器,就挂在github上.今天收到消息:域名解析服务器不在阿里云,要被GG.只能咬牙买了个阿里云乞丐版. 所有服务都装好了,pin ...
- [译]如何去除pandas dataframe里面的Unnamed的列?
原文来源: https://stackoverflow.com/questions/43983622/remove-unnamed-columns-in-pandas-dataframe 问:我有一个 ...
- 八、ISP 接口隔离原则
ISP应用的场景是某些类不符合SRP原则,但使用这些类的客户端应该根据它们的父类来使用(我感觉这句话应该改为:客户端应该根据它们的抽象类\接口来使用它们),而不是直接使用它们. 定义: 客户端不应该依 ...
- SimpleMDE编辑器 + 提取HTML + 美化输出
开发步骤: 1. 安装和引入(npm或者bower都可以) $ bower install simplemde --save //css - debug目录下为开发版本 <link rel=&q ...
- Qt编程的一些技巧
1.Qt程序在运行过程中,调用函数(如lcdNumber->display(num))显示数据到界面上时,并不会马上刷新屏幕显示,而是要等主程序运行到函数a.exec()时,才刷新屏幕,如下 因 ...
- RocketMQ 源码分析 RouteInfoManager(四)
在上一章分析了NamesrvController的构造函数时,会生成一个RouteInfoManager对象,该对象存放着整个消息集群的相关消息,所以这里单独拿出来分析.其实试想一下namesrv的功 ...