hdu4417(Super Mario)—— 二分+划分树
Super Mario
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9714 Accepted Submission(s): 4113
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.)
#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)—— 二分+划分树的更多相关文章
- HDU 4417 Super Mario (划分树)(二分)
Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDOJ题目4417 Super Mario(划分树求区间比k小的个数+二分)
Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- HDU4417 Super Mario(主席树)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=4417 Description Mario is world-famous plumber. ...
- [HDU4417]Super Mario(主席树+离散化)
传送门 又是一道主席树模板题,注意数组从0开始,还有主席树耗费空间很大,数组开大点,之前开小了莫名其妙TLE.QAQ ——代码 #include <cstdio> #include < ...
- HDU--4417 Super Mario (主席树模版题)
题目链接 题目让求 L R区间 不大于H 的数有多少 数据太大需要离散化 #include<bits/stdc++.h> using namespace std; #define maxn ...
- HDU-4417 Super Mario,划分树+二分!
Super Mario 这个题也做了一天,思路是很清晰,不过二分那里写残了,然后又是无限RE.. 题意:就是查询区间不大于k的数的个数. 思路:裸划分树+二分答案.将区间长度作为二分范围.这个是重点. ...
- hdu4417 Super Mario 树阵离线/划分树
http://acm.hdu.edu.cn/showproblem.php?pid=4417 Super Mario Time Limit: 2000/1000 MS (Java/Others) ...
- hdu-4417 Super Mario(树状数组 + 划分树)
题目链接: Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- HDU 4417 Super Mario(主席树求区间内的区间查询+离散化)
Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
随机推荐
- 表单如何与Servlet控制器关联
<form action="loing.do" method="post"> <div> ...
- Excel 2003-单元格输入中带记忆功能
最近有个同事问我,如何在Excel单元格输入中带记忆功能?其实很简单: 工具ó选项ó编辑ó将“记忆式键入”项选中ó确定: //附图[效果图]:
- Numpy随机数
Numpy随机数 np.random随机数子库 1: 基本函数 .rand(d0,d1,..dn):创建d0-dn维度的随机数数组,浮点数,范围从0-1,均匀分布 .randn(d0,d1,..dn) ...
- oracle与DB2
1.体系结构,DB2的实例和数据库分开的做法,我个人还是比较喜欢的,因为实例可以创建多个,数据库的恢复直接恢复到实例下就可以了,相对ORACLE简单多了. 2.管理工具,DB2的管理工具做得太简陋了, ...
- conductor FAQ
在一段时间后(如1小时,1天等),您如何安排将任务放入队列中? 轮询任务后,更新任务的状态IN_PROGRESS并将其callbackAfterSeconds设置为所需的时间.任务将保留在队列中,直到 ...
- 2.Add Two Numbers (List)
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- .net的内置对象
一 . 获取客户端,服务器端信息: Response.Write("客户端信息:"); Response.Write("<br>浏览器类型,版本:" ...
- 岛屿的个数12 · Number of Islands 12
[抄题]: [思维问题]: [一句话思路]: [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入): [画图]: [一刷]: [二刷]: [三刷]: [四刷] ...
- [leetcode]199. Binary Tree Right Side View二叉树右侧视角
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...
- php的静态化
原理,将动态的页面,存储为静态的HTML静态页,使浏览器直接请求该静态页. 测试:一个PHP动态页面与一个静态页面所消耗的时间 一般可以使用apache自带的ab(apache bench)程序来测试 ...