题目链接

 #include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define N 100100
struct node
{
int l,r;
}tree[*N];
int sorted[N];
int lnum[][N];
int val[][N];
void build(int d,int l,int r,int rt)
{
int m,lsame,i,lpos,rpos,same;
tree[rt].l = l;
tree[rt].r = r;
if(l == r) return ;
m = (l+r)>>;
lsame = (m - l + );
for(i = l;i <= r;i ++)
{
if(val[d][i] < sorted[m])
lsame --;
}
lpos = l;
rpos = m+;
same = ;
for(i = l;i <= r;i ++)
{
if(i == l)
lnum[d][i] = ;
else
lnum[d][i] = lnum[d][i-];
if(val[d][i] < sorted[m])
{
lnum[d][i] ++;
val[d+][lpos++] = val[d][i];
}
else if(val[d][i] > sorted[m])
{
val[d+][rpos++] = val[d][i];
}
else
{
if(same < lsame)
{
same ++;
lnum[d][i] ++;
val[d+][lpos++] = val[d][i];
}
else
{
val[d+][rpos++] = val[d][i];
}
}
}
build(d+,lson);
build(d+,rson);
}
int query(int d,int k,int l,int r,int rt)
{
int s,ss,nl,nr,m,rs,rss;
if(l == r)
{
return val[d][l];
}
if(l == tree[rt].l)
{
s = lnum[d][r];
ss = ;
}
else
{
s = lnum[d][r] - lnum[d][l-];
ss = lnum[d][l-];
}
if(s >= k)
{
nl = ss + tree[rt].l;
nr = ss + tree[rt].l + s - ;
return query(d+,k,nl,nr,rt<<);
}
else
{
m = (tree[rt].l + tree[rt].r)>>;
rss = l - tree[rt].l - ss;
rs = r - l + - s;
nl = m + rss + ;
nr = m + rs + rss;
return query(d+,k-s,nl,nr,rt<<|);
}
}
int bin(int l,int r,int key)
{
int str,end,mid,temp;
str = ;end = r-l+;
while(str < end)
{
mid = (str + end + )/;
temp = query(,mid,l,r,);
if(temp > key)
end = mid - ;
else
str = mid;
}
return end;
}
int main()
{
int i,n,m,x,y,k,t,cas = ;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(i = ;i <= n;i ++)
{
scanf("%d",&val[][i]);
sorted[i] = val[][i];
}
sort(sorted+,sorted+n+);
build(,,n,);
printf("Case %d:\n",cas++);
for(i = ;i < m;i ++)
{
scanf("%d%d%d",&x,&y,&k);
x ++;
y ++;
if(k > query(,y-x+,x,y,))
printf("%d\n",y-x+);
else if(k < query(,,x,y,))
printf("0\n");
else
printf("%d\n",bin(x,y,k));
}
}
return ;
}

HDU 4417 Super Mario(划分树+二分)的更多相关文章

  1. HDU 4417 - Super Mario ( 划分树+二分 / 树状数组+离线处理+离散化)

    题意:给一个数组,每次询问输出在区间[L,R]之间小于H的数字的个数. 此题可以使用划分树在线解决. 划分树可以快速查询区间第K小个数字.逆向思考,判断小于H的最大的一个数字是区间第几小数,即是答案. ...

  2. HDU 4417 Super Mario(划分树)

    Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  3. HDU 4417 Super Mario(划分树问题求不大于k的数有多少)

    Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  4. HDU-4417 Super Mario,划分树+二分!

    Super Mario 这个题也做了一天,思路是很清晰,不过二分那里写残了,然后又是无限RE.. 题意:就是查询区间不大于k的数的个数. 思路:裸划分树+二分答案.将区间长度作为二分范围.这个是重点. ...

  5. HDU 4417 Super Mario ( 离线树状数组 )

    把数值和查询放在一起从小到大排序,纪录每个数值的位置,当遇到数值时就更新到树状数组中,遇到查询就直接查询该区间和. #include <cstdio> #include <cstri ...

  6. HDU 4417 Super Mario 主席树

    分析:找一个区间里小于等于h的数量,然后这个题先离散化一下,很简单 然后我写这个题主要是熟悉一下主席树,其实这个题完全可以离线做,很简单 但是学了主席树以后,我发现,在线做,一样简单,而且不需要思考 ...

  7. HDU 4417 Super Mario 主席树查询区间小于某个值的个数

    #include<iostream> #include<string.h> #include<algorithm> #include<stdio.h> ...

  8. HDU 4417 Super Mario (划分树)(二分)

    Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  9. 【划分树+二分】HDU 4417 Super Mario

    第一次 耍划分树.. . 模板是找第k小的 #include <stdio.h> #include <string.h> #include <stdlib.h> # ...

随机推荐

  1. 搭建自己的SIP服务器:开源sip服务器opensips的搭建及终端TwInkle的使用

    搭建自己的SIP服务器:开源sip服务器opensips的搭建及终端TwInkle的使用 分类: linux编译相关2013-01-05 21:38 17983人阅读 评论(24) 收藏 举报 先下载 ...

  2. jQuery mobile 开发问题记录

    一.动态加载页面问题 1.存在这样一个页面布局: main.html 为主界面A,B为该页面中的三个page,其中A为splitview左部分页面,B为右半部页面 a1.html 为一个独立的页面 a ...

  3. Longest Consecutive Sequence

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. C ...

  4. 更改SharePoint 2010 顶部导航为下拉菜单样式

      更改SharePoint 2010 顶部导航为下拉菜单样式 最后的效果图: 假如一个网站集顶级站点下面有子网站:sub site1,该子站点下面又有两个子站点:sub site1_1,sub si ...

  5. OpenWRT

    如何查日志:http://blog.appdevp.com/archives/382 logread OpenWrt下创建crontab任务: 确保/etc/crontab/下有名为"roo ...

  6. phpmyadmin 主机名自动补全

    2015年2月9日 14:29:25 新安装的phpmyadmin在登录界面中, 主机名的输入框没有自动补全功能, 导致每次都得手动输入ip地址 找到 phpmyadmin/libraries/aut ...

  7. Java for LeetCode 027 Remove Element

    Given an array and a value, remove all instances of that value in place and return the new length. T ...

  8. ShortestPath:Six Degrees of Cowvin Bacon(POJ 2139)

    牛与电影 题目大意:就是一群牛,又在玩游戏了(怎么你们经常玩游戏),这个游戏规则如下,把牛拆分成一个一个组,并且定义一个“度”,规定在一个组的牛与他自己的度为0,与其他牛的度为1,不同组的牛不存在度, ...

  9. Greedy:Fence Repair(POJ 3252)

    Fence Repair 问题大意:农夫约翰为了修理栅栏,要将一块很长的木块切割成N块,准备切成的木板的长度为L1,L2...LN,未切割前的木板的长度恰好为切割后木板的长度的总和,每次切断木板的时候 ...

  10. July 25th, Week 31st Monday, 2016

    We will not go quietly into the night. 今夜,我们将奋战到底. We will be the champion. We will not stop fightin ...