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的更多相关文章

  1. 清华学堂 LightHouse

    灯塔(LightHouse) Description As shown in the following figure, If another lighthouse is in gray area, ...

  2. 清华学堂 列车调度(Train)

    列车调度(Train) Description Figure 1 shows the structure of a station for train dispatching. Figure 1 In ...

  3. 记2014“蓝桥杯全国软件大赛&quot;决赛北京之行

    5月29,30日 最终到了这一天.晚上有数据结构课,10点多的火车,我们就没有去上课,下午在宿舍里收拾东西,晚上8点左右从南校出发,9点半多到达火车站和老师学长学姐们会和. 第一次去北京,第一次买的卧 ...

  4. WEB入门三 CSS样式表基础

    学习内容 Ø        CSS的基本语法 Ø        CSS选择器 Ø        常见的CSS样式 Ø        网页中3种使用CSS的方式 能力目标 Ø        理解CSS的 ...

  5. 【推荐】适合本科生的网络公开课(MOOC为主),不断更新……

    题记:身在海大(湛江),是幸运还是不幸,每一个人有自己的定义.人生不能再来一次,唯有把握当下.提高自己,才可能在不能拼爹的年代靠自身实力前行.或许,我们做不了富二代.但我们每一个人.都有机会成为富二代 ...

  6. 教育O2O在学校落地,学堂在线瞄准混合式教学

    (大讲台—国内首个it在线教育混合式自适应学习平台.) 进入2015年,互联网教育圈最火的词非“教育O2O”莫属.不断刷新的融资金额和速度,不断曝光的正面和负面新闻,都让教育O2O公司赚足了眼球.然并 ...

  7. SQL Server 合并复制遇到identity range check报错的解决

        最近帮一个客户搭建跨洋的合并复制,由于数据库非常大,跨洋网络条件不稳定,因此只能通过备份初始化,在初始化完成后向海外订阅端插入数据时发现报出如下错误: Msg 548, Level 16, S ...

  8. Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range

    在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...

  9. [LeetCode] Range Addition 范围相加

    Assume you have an array of length n initialized with all 0's and are given k update operations. Eac ...

随机推荐

  1. animate对颜色设置不起作用

    今天了解了一下stop的使用方法,但是实例中加入color:red的时候,动画效果没有实现,具体实例如下: http://jsbin.com/fezaroyene/edit?html,js,outpu ...

  2. myBatis foreach详解【转】

    MyBatis的foreach语句详解 foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合.foreach元素的属性主要有 item,index,collection,ope ...

  3. eclipse中 linked resource的使用

    一.关于linked resource   eclipse 中的linkded resources 是指存放在项目所在位置以外某个地方的文件或者文件夹:这些特定的资源必须有一个项目作为他们的父资源.l ...

  4. 2015.4.23 贪吃蛇、canvas动画,各种上传工具,url信息匹配以及最全前端面试题等

    1.面向对象贪吃蛇   2.css中:hover 改变图片 页面加载完 第一次鼠标移入会闪一下 这是为啥? 解决方法:你把两张图合成一张图或者是先把图片加载到页面上,然后再hover出来. 解析:图片 ...

  5. [译]git log进阶

    格式化log输出 oneline --oneline标记将每个commit压缩成一行. 默认情况下显示一个commit ID和commit描述的第一行. 输出如下: 0e25143 Merge bra ...

  6. Cufflinks

    Transcriptome assembly and differential expression analysis for RNA-Seq. Cufflinks assembles transcr ...

  7. CentOS6.3编译安装Nginx1.4.7 + MySQL5.5.25a + PHP5.3.28

    [准备工作] #在编译安装lnmp之前,首先先卸载已存在的rpm包. rpm -e httpd rpm -e mysql rpm -e php yum -y remove httpd yum -y r ...

  8. 【Alpha版本】冲刺阶段——Day 2

    我说的都队 031402304 陈燊 031402342 许玲玲 031402337 胡心颖 03140241 王婷婷 031402203 陈齐民 031402209 黄伟炜 031402233 郑扬 ...

  9. C#高级编程笔记 Day 7, 2016年9月 19日 (泛型)

    1.协变和抗变 泛型接口的协变 如果泛型类型用 out  关键字标注,泛型接口就是协变的.这也意味着返回类型只能是 T. 接口IIndex 与类型T 是协变的,并从一个制度索引器中返回这个类型. pu ...

  10. python模块引用梳理

    文件组织结构: t ├── __init__.py ├── main.py ├── t1 │   ├── A.py │   └── __init__.py └── t2 ├── B.py └── __ ...