清华学堂 Range
Descriptioin
Let S be a set of n integral points on the x-axis. For each given interval [a, b], you are asked to count the points lying inside.
Input
The first line contains two integers: n (size of S) and m (the number of queries).
The second line enumerates all the n points in S.
Each of the following m lines consists of two integers a and b and defines an query interval [a, b].
Output
The number of points in S lying inside each of the m query intervals.
Example
Input
5 2
1 3 7 9 11
4 6
7 12
Output
0
3
Restrictions
0 <= n, m <= 5 * 10^5
For each query interval [a, b], it is guaranteed that a <= b.
Points in S are distinct from each other.
Coordinates of each point as well as the query interval boundaries a and b are non-negative integers not greater than 10^7.
Time: 2 sec
Memory: 256 MB
这道题目,好早以前就接触过,那时候数据结构刚学,什么也不懂,今年又来刷这套题了,感觉还好
题目求 [a, b] 内所有的元素个数,二分搜索,求下界,对a求它严格的下界, 对b求它不严格的下界,搞定。
这道题目不用二分,只能拿一部分分数。
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int MAX_SIZE = 5E5 + ;
int num[MAX_SIZE];
int a[MAX_SIZE]; void quick_sort(int s[], int l, int r)
{
if(l < r)
{
int i = l, j = r, x = s[l];
while(i < j)
{
while(i < j && s[j] >= x)
j--;
if(i < j)
s[i++] = s[j]; while(i < j && s[i] < x)
i++;
if(i < j)
s[j--] = s[i];
}
s[i] = x;
quick_sort(s, l, i-);
quick_sort(s, i+, r);
}
} ///二分查下届
int BSearchLowerBound(int arry[], int low, int high, int target)
{
if(high < low || target <= arry[low])
return -;
int mid = (low + high + ) / ;
while(low < high)
{
if(arry[mid] < target)
low = mid;
else
high = mid - ;
mid = (low + high + )/;
}
return mid;
} int BSearchLowerBound_1(int arry[], int low, int high, int target)
{
if(high < low || target < arry[low])
return -;
int mid = (low + high + ) / ;
while(low < high)
{
if(arry[mid] <= target)
low = mid;
else
high = mid - ;
mid = (low + high + )/;
}
return mid;
} int main()
{
int n, m;
int x, y, ans;
while(~scanf("%d %d", &n, &m))
{
for(int i = ; i < n; i++)
{
scanf("%d", num+i);
}
//quick_sort 下标从 0 开始
quick_sort(num, , n-); for(int i = ; i < m; i ++)
{
scanf("%d %d", &x, &y); ans = BSearchLowerBound_1(num, , n-, y) - BSearchLowerBound(num, , n-, x); printf("%d\n", ans);
}
}
return ;
}
清华学堂 Range的更多相关文章
- 清华学堂 LightHouse
灯塔(LightHouse) Description As shown in the following figure, If another lighthouse is in gray area, ...
- 清华学堂 列车调度(Train)
列车调度(Train) Description Figure 1 shows the structure of a station for train dispatching. Figure 1 In ...
- 记2014“蓝桥杯全国软件大赛"决赛北京之行
5月29,30日 最终到了这一天.晚上有数据结构课,10点多的火车,我们就没有去上课,下午在宿舍里收拾东西,晚上8点左右从南校出发,9点半多到达火车站和老师学长学姐们会和. 第一次去北京,第一次买的卧 ...
- WEB入门三 CSS样式表基础
学习内容 Ø CSS的基本语法 Ø CSS选择器 Ø 常见的CSS样式 Ø 网页中3种使用CSS的方式 能力目标 Ø 理解CSS的 ...
- 【推荐】适合本科生的网络公开课(MOOC为主),不断更新……
题记:身在海大(湛江),是幸运还是不幸,每一个人有自己的定义.人生不能再来一次,唯有把握当下.提高自己,才可能在不能拼爹的年代靠自身实力前行.或许,我们做不了富二代.但我们每一个人.都有机会成为富二代 ...
- 教育O2O在学校落地,学堂在线瞄准混合式教学
(大讲台—国内首个it在线教育混合式自适应学习平台.) 进入2015年,互联网教育圈最火的词非“教育O2O”莫属.不断刷新的融资金额和速度,不断曝光的正面和负面新闻,都让教育O2O公司赚足了眼球.然并 ...
- SQL Server 合并复制遇到identity range check报错的解决
最近帮一个客户搭建跨洋的合并复制,由于数据库非常大,跨洋网络条件不稳定,因此只能通过备份初始化,在初始化完成后向海外订阅端插入数据时发现报出如下错误: Msg 548, Level 16, S ...
- Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range
在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...
- [LeetCode] Range Addition 范围相加
Assume you have an array of length n initialized with all 0's and are given k update operations. Eac ...
随机推荐
- BASH_SUBSHELL 变量不生效的情况
BASH_SUBSHELL 实现于 Bash 3.0,我一直想不到它在实际编码中有什么用,后来在 Bash 的 Change Log 里找到一句话,才知道它是作调试用的: New variables ...
- 在OSX和Windows版本Docker上运行GUI程序
看到很多人在Docker问题区讨论:如何在OS X和Windows的Docker上运行GUI程序, 随手记录几个参考资料: https://github.com/docker/docker/issue ...
- Python的垃圾回收机制
Python的GC模块主要运用了“引用计数”(reference counting)来跟踪和回收垃圾.在引用计数的基础上,还可以通过“标记-清除”(mark and sweep)解决容器对象可能产生的 ...
- BMP文件格式
- JavaScript深入浅出5-数组
慕课网教程视频地址:Javascript深入浅出 数组:值的有序集合 创建数组:字面量,构造器new array() 数组的读写:push() 尾部加入新元素 unshift() 头部加入新元素 po ...
- 调整 ANTD 组件菜单的字体大小。
调整 ANTD 组件菜单的字体大小:经过多次试验,原有字体太小,只有12px,通过下列CSS 强制加大. 1.分组子菜单(标题) .ant-menu-inline > .ant-menu-sub ...
- Entity framwork的数据库分页
网上查了很多,原以为多么复杂的事情,其实很简单: list = list.OrderBy(orderBy, ascending).Skip((pageIndex - 1) * pageSize).Ta ...
- Ubuntu 14.4 配置
1.安装 Orcal Java 使用下面的命令安装,只需一些时间,它就会下载许多的文件,所及你要确保你的网络环境良好: sudo add-apt-repository ppa:webupd8team/ ...
- [mysql]数据库基础知识
数据库管理系统DBMS 软件 bin config db (保存数据) 写程序: 数据库在本地 找到目录 添加数据 数据库在远程 socket连接上远程机器 socket发送命令 需要做的事情 程序 ...
- iOS-OC-基本控件之UIPageControl
UIPageControl(页面控制器,就是桌面的那些小点点,每个点代表一个界面) 父类是 UIControl. iOS开发中常用的基本控件,主要和UIScrollView一起使用,比较常用的就是有些 ...