清华学堂 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 ...
随机推荐
- 广播接收者Receiver
一,动态创建网络状态监控 思路: 1:需要注册一个广播接收者,registerReceiver()需要两个参数 public Intent registerReceiver( BroadcastRec ...
- mysql中find_in_set()函数的使用
首先举个例子来说: 有个文章表里面有个type字段,它存储的是文章类型,有 1头条.2推荐.3热点.4图文等等 .现在有篇文章他既是头条,又是热点,还是图文,type中以 1,3,4 的格式存储.那我 ...
- [Head First设计模式]生活中学设计模式——迭代器模式
系列文章 [Head First设计模式]山西面馆中的设计模式——装饰者模式 [Head First设计模式]山西面馆中的设计模式——观察者模式 [Head First设计模式]山西面馆中的设计模式— ...
- Ubuntu下su被拒绝
ubuntu@ubuntu:~$ sudo passwd root输入新的 UNIX 密码: 重新输入新的 UNIX 密码: passwd:已成功更新密码前提是你肯定得知道当前用户的密码. 然后登录: ...
- 快速傅里叶(FFT)的快速深度思考
关于按时间抽取快速傅里叶(FFT)的快速理论深度思考 对于FFT基本理论参考维基百科或百度百科. 首先谈谈FFT的快速何来?大家都知道FFT是对DFT的改进变换而来,那么它究竟怎样改进,它改进的思想在 ...
- java中的等于
数字的比较等于用“==” 不等于用“!=” 字符的比较等于用“.equals”不等于用”!s1.equals(s2)“
- 如何修改Xampp中MySQL的root密码?
MySQL 的“root”用户默认状态是没有密码的,所以在 PHP 中您可以使用 mysql_connect("localhost","root"," ...
- JS事件对象与事件委托
事件对象 包含事件相关的信息,如鼠标.时间.触发的DOM对象等 js默认将事件对象封装好,并自动的以参数的形式,传递给事件处理函数的第1个参数,如下: document.getElementsByTa ...
- (备忘)自定义viewgroup与点击分发事件
public class ScoreButton extends ViewGroup 在类中重写onTouchEvent方法 @Override public boolean onTouchEvent ...
- linux git安装及配置(包括更新)
1.在终端运行命令 sudo apt-get install git 2.查看版本号 git --version (若不是最新可更新 自选) 更新提示: sudo add-apt-repositor ...