山东第四届省赛: Boring Counting 线段树
http://acm.sdibt.edu.cn/JudgeOnline/problem.php?id=3237
Problem H:Boring Counting
Time Limit: 3 Sec Memory Limit: 128 MB
Submit: 6 Solved: 2
[Submit][Status][Discuss]
Description
In this problem you are given a number sequence P consisting of N integer and Pi is the ith element in the sequence. Now you task is to answer a list of queries, for each query, please tell us among [L, R], how many Pi is not less than A and not greater than B( L<= i <= R). In other words, your task is to count the number of Pi (L <= i <= R, A <= Pi <= B).
Input
In the first line there is an integer T (1 < T <= 50), indicates the number of test cases.
For each case, the first line contains two numbers N and M (1 <= N, M <= 50000), the size of sequence P, the number of queries. The second line contains N numbers Pi(1 <= Pi <= 10^9), the number sequence P. Then there are M lines, each line contains four number L, R, A, B(1 <= L, R <= n, 1 <= A, B <= 10^9)
Output
For each case, at first output a line ‘Case #c:’, c is the case number start from 1. Then for each query output a line contains the answer.
Sample Input
1
13 5
6 9 5 2 3 6 8 7 3 2 5 1 4
1 13 1 10
1 13 3 6
3 6 3 6
2 8 2 8
1 9 1 9
Sample Output
Case #1:
13
7
3
6
9
HINT
Source
比赛的时候,没有头绪,今天别人说起划分树,想了一下,还真是可以做的。
但是要一点变形,这样的变形,让我已经看不清 划分树的影子了。
划分树----求区间第K 小/大 值
这道题是求区间求区间[l,r]中 满足 A<=xi<=B; xi属于[l,r]。求xi的个数。
如何转化的呢?
枚举区间第k小数==A。此时能求出一个值 hxl
枚举区间第K小数==B,此时得到一个值 tom
显然满足tom>=hxl。
那在这范围的数,是不是就满足了 A<=xi<=B !~~
这么一说,其实好简单。为什么没有想到呢? 划分树做的时候是求 第 k 小的是什么什么数字,现在换了回来而已,
求的是某个数字是区间里面第几小。
其实有个问题就又来了,A,B在区间[l,r]未必存在,这该怎么办?????
其实上面的说法,不严谨,(求的是某个数字是区间里面第几小。)应该说是求A的下届,B的上届值。
二分枚举,平均的时间会更小。在求下届和上届的过程中也要注意一些细节的问题、
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#define N 100010
using namespace std; int toleft[][N];
int Tree[][N];
int Sort[N]; void build(int l,int r,int dep)
{
int mid=(l+r)/,sum=mid-l+,lpos,rpos,i;
if(l==r) return ;
for(i=l;i<=r;i++)
if(Tree[dep][i]<Sort[mid]) sum--;
lpos=l;
rpos=mid+;
for(i=l;i<=r;i++)
{
if(Tree[dep][i]<Sort[mid])
{
Tree[dep+][lpos++]=Tree[dep][i];
}
else if(Tree[dep][i]==Sort[mid] && sum>)
{
Tree[dep+][lpos++]=Tree[dep][i];
sum--;
}
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)
{
int mid=(L+R)/,newl,newr,cur;
if(l==r) return Tree[dep][l];
cur=toleft[dep][r]-toleft[dep][l-];
if(cur>=k)
{
newl=L+toleft[dep][l-]-toleft[dep][L-];
newr=newl+cur-;
return query(L,mid,newl,newr,dep+,k);
}
else
{
newr=r+(toleft[dep][R]-toleft[dep][r]);
newl=newr-(r-l-cur);
return query(mid+,R,newl,newr,dep+,k-cur);
}
}
int EF1(int l,int r,int num,int n) //枚举下届
{
int low=,right=r-l+,mid,tmp;
mid=(low+right)/;
while(low<right)
{
tmp=query(,n,l,r,,mid);
if(tmp>=num)
right=mid-;
else low=mid; //!!!!
mid=(low+right+)/; //!!! 加了一个1。
}
return low; } int EF2(int l,int r,int num,int n)
{
int low=,right=r-l+,mid,tmp;
mid=(low+right)/;
while(low<right)
{
tmp=query(,n,l,r,,mid);
if(tmp>num)
right=mid; //!!!
else low=mid+;
mid=(low+right)/; //木有加1
}
return low;
} int main()
{
int T,n,m,i,l,r,k,a,b,hxl,tom,qq;
scanf("%d",&T);
{
for(qq=;qq<=T;qq++)
{
scanf("%d%d",&n,&m);
memset(Tree,,sizeof(Tree));
for(i=;i<=n;i++)
{
scanf("%d",&Tree[][i]);
Sort[i]=Tree[][i];
}
sort(Sort+,Sort++n);
build(,n,);
printf("Case #%d:\n",qq);
while(m--)
{
scanf("%d%d%d%d",&l,&r,&a,&b);
hxl=query(,n,l,r,,);
if(a<=hxl) hxl=; //对临界条件的判断。如果A就是最小的,木有下届
else
{
hxl=EF1(l,r,a,n);
hxl++;
}
tom=query(,n,l,r,,r-l+);
if(b>=tom) tom=r-l+; //如果B是该区间[l,r]最大的,显然木有上届
else
{
tom=EF2(l,r,b,n);
tom--;
}
hxl=tom-hxl+;
printf("%d\n",hxl);
}
}
}
return ;
}
山东第四届省赛: Boring Counting 线段树的更多相关文章
- UPC 2224 Boring Counting ★(山东省第四届ACM程序设计竞赛 tag:线段树)
[题意]给定一个长度为N的数列,M个询问区间[L,R]内大于等于A小于等于B的数的个数. [题目链接]http://acm.upc.edu.cn/problem.php?id=2224 省赛的时候脑抽 ...
- UPC 2224 / “浪潮杯”山东省第四届ACM大学生程序设计竞赛 1008 Boring Counting 主席树
Problem H:Boring Counting Time Limit : 6000/3000ms (Java/Other) Memory Limit : 65535/32768K (Java/ ...
- 13年山东省赛 Boring Counting(离线树状数组or主席树+二分or划分树+二分)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud 2224: Boring Counting Time Limit: 3 Sec ...
- SDIBT 3237 Boring Counting( 划分树+二分枚举 )
http://acm.sdibt.edu.cn/JudgeOnline/problem.php?id=3237 Problem H:Boring Counting Time Limit: 3 Sec ...
- Subsequence Count 2017ccpc网络赛 1006 dp+线段树维护矩阵
Problem Description Given a binary string S[1,...,N] (i.e. a sequence of 0's and 1's), and Q queries ...
- 【BZOJ 2957】楼房重建&&Codechef COT5 Count on a Treap&&【NOIP模拟赛】Weed 线段树的分治维护
线段树是一种作用于静态区间上的数据结构,可以高效查询连续区间和单点,类似于一种静态的分治.他最迷人的地方在于“lazy标记”,对于lazy标记一般随我们从父区间进入子区间而下传,最终给到叶子节点,但还 ...
- ACM学习历程—HDU 5023 A Corrupt Mayor's Performance Art(广州赛区网赛)(线段树)
Problem Description Corrupt governors always find ways to get dirty money. Paint something, then sel ...
- 3.28 省选模拟赛 染色 LCT+线段树
发现和SDOI2017树点涂色差不多 但是当时这道题模拟赛的时候不会写 赛后也没及时订正 所以这场模拟赛的这道题虽然秒想到了LCT和线段树但是最终还是只是打了暴力. 痛定思痛 还是要把这道题给补了. ...
- UVAlive7141 BombX 14年上海区域赛D题 线段树+离散化
题意:一个无限大的棋盘, 有n个小兵, 给出了n个兵的坐标, 现在有一个长为width 高为height的炸弹放在棋盘上, 炸弹只能上下左右平移, 不能旋转. 且放炸弹的区域不能含有士兵, 炸弹可以一 ...
随机推荐
- Python廖雪峰学习笔记——操作文件和目录
查看当前目录的绝对路径: >>>os.path.abspath('.') # 在某个目录下创建一个新目录, # 首先把新目录的完整路径表示出来: >>> os.pa ...
- 5.python内置函数详解
内置函数 声明,转载至这位大哥,感谢之至 http://blog.csdn.net/oaa608868/article/details/53506188 关于分类 数学运算(7个) 类型转换(24个) ...
- linux下各个目录里面都装了什么
文章来源:http://blog.csdn.net/sunstars2009918/article/details/7038772 搞电脑的人总想知道自己的系统里到底有些什么东西,于是我就在Linux ...
- svn自己的一些使用方法总结
1,先创建一个空的文件夹,该文件夹是放置你们的项目代码用的.右击该文件夹,点击SVN Checkout.拿到项目负责人给你的项目目录url(例:https://192.168.0.127/svn/yo ...
- [Python] 模拟登录网站(。。为了之后操作数据。。)
我司的内部管理(Web)系统(日报)着实..(mafan).. 所以,就想自己动手增加一下便利性. 计划是, - 桌面程序 用来方便记录(按自己格式,数据随时保存到sqlite中,备用) 通过一览来确 ...
- js面试题-数组去重
今天,在聊天群里看到数组去重的话题,面试者的答案如下: 参考答案如下: 程序员思维,做出如下测试: 未考虑到:1,‘1’是不同的,应该不去重 未考虑到对象 所以,参考答案只能去重基础类型 根据以往看过 ...
- MySQL实例crash的案例分析
[作者] 王栋:携程技术保障中心数据库专家,对数据库疑难问题的排查和数据库自动化智能化运维工具的开发有强烈的兴趣. [问题描述] 我们生产环境有一组集群的多台MySQL服务器(MySQL 5.6.21 ...
- html5聊天案例|趣聊h5|仿微信界面聊天|红包|语音聊天|地图
之前有开发过一个h5微直播项目,当时里面也用到过聊天模块部分,今天就在之前聊天部分的基础上重新抽离模块,开发了这个h5趣聊项目,功能效果比较类似微信聊天界面.采用html5+css3+Zepto+sw ...
- zabbix数据库表结构解析
下面开始介绍: 1.添加监控表结构详解 (1)hosts,存储被监控的机器的信息,表结构如下: (2)items (3)hosts_templates,存储机器和模版或者模版和模版之间的关系 由于模 ...
- Java之集合(十)EnumMap
转载请注明源出处:http://www.cnblogs.com/lighten/p/7371744.html 1.前言 本章介绍Map体系中的EnumMap,该类是专门针对枚举类设计的一个集合类.集合 ...