HDU 4417 划分树+二分
题意:有n个数。m个询问(l,r,k),问在区间[l,r] 有多少个数小于等于k。
划分树——查找区间第k大的数。。。。
利用划分树的性质。二分查找在区间[l,r]小于等于k的个数。
假设在区间第 i 大的数tmp>k,则往下找,假设tmp<k,往上找。
#include<cstdio>
#include<stdlib.h>
#include<string.h>
#include<string>
#include<map>
#include<cmath>
#include<iostream>
#include <queue>
#include <stack>
#include<algorithm>
#include<set>
using namespace std;
#define INF 1e8
#define eps 1e-8
#define LL long long
#define N 100010
#define mod 1000000007 int a[N],s[N],t[20][N],num[20][N],n,m; void build(int c,int l,int r)
{
if(l==r) return;
int mid=(l+r)/2;
int lm=mid-l+1,lp=l,rp=mid+1;
for(int i=l;i<=r;i++)
lm-=s[i]<s[mid];
for(int i=l;i<=r;i++)
{
if(i==l)
num[c][i]=0;
else num[c][i]=num[c][i-1];
if(t[c][i]==s[mid])
{
if(lm)
{
lm--;
num[c][i]++;
t[c+1][lp++]=t[c][i];
}
else t[c+1][rp++]=t[c][i];
}
else if(t[c][i]<s[mid])
{
num[c][i]++;
t[c+1][lp++]=t[c][i];
}
else t[c+1][rp++]=t[c][i];
}
build(c+1,l,mid);
build(c+1,mid+1,r);
} int query(int c,int l,int r,int ql,int qr,int k)
{
if(l==r)
return t[c][l];
int s,ss,mid=(l+r)/2;
if(ql==l)
s=0,ss=num[c][qr];
else s=num[c][ql-1],ss=num[c][qr]-num[c][ql-1];
if(k<=ss)
return query(c+1,l,mid,l+s,s+l+ss-1,k);
else return query(c+1,mid+1,r,mid+1+ql-l-s,mid+1+qr-l-s-ss,k-ss);
} int main()
{
int T,ca=1;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
s[i]=t[0][i]=a[i];
}
sort(s+1,s+n+1);
build(0,1,n);
printf("Case %d:\n",ca++);
while(m--)
{
int l,r,k;
scanf("%d%d%d",&l,&r,&k);
l++;r++;
if(query(0,1,n,l,r,r-l+1)<=k)
printf("%d\n",r-l+1);
else if(query(0,1,n,l,r,1)>k)
puts("0");
else
{
int ll=0,rr=r-l+1,mid;
while(ll<=rr)
{
mid=(ll+rr)>>1;
int tmp=query(0,1,n,l,r,mid);//假设在区间[l,r]第mid大的数大于k,rr=mid-1
if(tmp>k)
rr=mid-1;
else ll=mid+1;//否则ll=mid+1
}
printf("%d\n",ll-1);
}
}
}
return 0;
}
/*
1
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
*/
HDU 4417 划分树+二分的更多相关文章
- HDU 4417 (划分树+区间小于k统计)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4417 题目大意:给定一个区间,以及一个k值,求该区间内小于等于k值的数的个数.注意区间是从0开始的 ...
- hdu 4417 划分树
思路:二分枚举区间第k大.用划分树查找是否符合要求的高度. #include<iostream> #include<algorithm> #include<cstdio& ...
- HDU 4417 划分树写法
Problem Description Mario is world-famous plumber. His “burly” figure and amazing jumping ability re ...
- sdut 2610:Boring Counting(第四届山东省省赛原题,划分树 + 二分)
Boring Counting Time Limit: 3000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 In this problem you a ...
- HDU-4417 Super Mario,划分树+二分!
Super Mario 这个题也做了一天,思路是很清晰,不过二分那里写残了,然后又是无限RE.. 题意:就是查询区间不大于k的数的个数. 思路:裸划分树+二分答案.将区间长度作为二分范围.这个是重点. ...
- Super Mario HDU 4417 主席树区间查询
Super Mario HDU 4417 主席树区间查询 题意 给你n个数(编号从0开始),然后查询区间内小于k的数的个数. 解题思路 这个可以使用主席树来处理,因为这个很类似查询区间内的第k小的问题 ...
- HDU 4417 - Super Mario ( 划分树+二分 / 树状数组+离线处理+离散化)
题意:给一个数组,每次询问输出在区间[L,R]之间小于H的数字的个数. 此题可以使用划分树在线解决. 划分树可以快速查询区间第K小个数字.逆向思考,判断小于H的最大的一个数字是区间第几小数,即是答案. ...
- 【划分树+二分】HDU 4417 Super Mario
第一次 耍划分树.. . 模板是找第k小的 #include <stdio.h> #include <string.h> #include <stdlib.h> # ...
- hdu4417 划分树+二分
//Accepted 14796 KB 453 ms //划分树 //把查询的次数m打成n,也是醉了一晚上!!! //二分l--r区间第k大的数和h比较 #include <cstdio> ...
随机推荐
- Java JVM:内存溢出(栈溢出,堆溢出,持久代溢出以及 nable to create native thread)
转载自https://github.com/pzxwhc/MineKnowContainer/issues/25 包括:1. 栈溢出(StackOverflowError)2. 堆溢出(OutOfMe ...
- Maximum Product of Word Lengths -- LeetCode
Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...
- [POI2014]FarmCraft
题目大意: 一个$n(n\le5\times10^5)$个点的树,每个点有一个权值$c_i$,从$1$出发进行欧拉遍历,每个单位时间移动一条边,记每个点$i$被访问到的时间是$t_i$,问最后$\ma ...
- Oracle触发器简单入门记录
写在前面: 最近,老项目新增了日报优化的需求,丽姐让我用触发器去实现当数据插入或者更新的时候,实现对日报表数据更新操作.嗯嗯嗯呢,之前学习数据库的时候,有碰到过触发器,但都是一跳而过,也没怎么去真正的 ...
- 在C#中用RX库和await来实现直观的状态机
在程序的设计过程中,我们经常会遇到一些需要使用状态机的场景,相信状态机的编写和维护是令每一个程序员都非常头大的事情.到了C# 5.0后,由于引进了await语法糖,我们可以通过await和Reacti ...
- 【MySQL性能优化】MySQL常见SQL错误用法
https://yq.aliyun.com/articles/72501?utm_content=m_14899
- linux字符设备文件的打开操作
2.7 字符设备文件的打开操作(1) 作为例子,这里假定前面对应于/dev/demodev设备节点的驱动程序在自己的代码里实现了如下的struct file_operations对象fops: st ...
- 可以使用foreach遍历循环的条件
大话C#中能使用foreach的集合的实现 转自:http://www.cnblogs.com/tangzhengyue/p/3339936.html 大家都知道foreach的语法: forea ...
- cubemap
cubemap 画的时候 是一张一张画 并不是画成 ------- | | | | | | | | | 这样一个位置 而是一 ...
- Android服务Service具体解释(作用,生命周期,AIDL)系列文章-为什么须要服务呢?
Android服务Service具体解释(作用,生命周期,AIDL) 近期沉迷于上班,没有时间写博客了.解衣入睡,未眠.随起床写一篇博客压压惊! ##我们android系统为什么须要服务Service ...