Lightoj 1088 - Points in Segments 【二分】
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1088
题意: 有一维的n个点和q条线段。询问每条线段上的点有多少个。
思路:寻找这些点中对于每条线段的上下界就可以。
代码:
#include <stdio.h>
#include <ctime>
#include <math.h>
#include <limits.h>
#include <complex>
#include <string>
#include <functional>
#include <iterator>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <bitset>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <iostream>
#include <ctime>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <time.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>
using namespace std;
int n, q;
int a[100010];
int x, y;
int ok;
int query(int t,int &ok)
{
ok = 0;
int left = 1;
int right = n;
int mid;
while (left <= right)
{
int mid = (left + right) / 2;
if (a[mid] >= t)
{
if (a[mid] == t)
ok = 1;
right = mid - 1;
}
else
left = mid + 1;
}
return left;
}
int main()
{
int t;
scanf("%d",&t);
int cases = 1;
while (t--)
{
scanf("%d%d",&n,&q);
for (int i = 1; i <= n; i++)
scanf("%d",&a[i]);
printf("Case %d:\n", cases++);
while (q--)
{
scanf("%d%d", &x, &y);
int ok1 = 0, ok2 = 0, tmp = 0;
int t1 = query(x,ok1);
int t2 = query(y,ok2);
if (ok2) tmp = 1;
//printf(" %d %d\n",t1,t2);
printf("%d\n", t2 - t1 + tmp);
}
}
return 0;
}
Lightoj 1088 - Points in Segments 【二分】的更多相关文章
- LightOJ 1088 - Points in Segments 二分
http://www.lightoj.com/volume_showproblem.php?problem=1088 题意:给出N个点,Q个查询,问在区间内的点数有多少个. 思路:直接在线二分,注意边 ...
- LightOj 1088 - Points in Segments (二分枚举)
题目链接: http://www.lightoj.com/volume_showproblem.php?problem=1088 题目描述: 给出一个n位数升序排列的数列,然后q个查询,每个查询问指定 ...
- LightOJ 1089 - Points in Segments (II) 线段树区间修改+离散化
http://www.lightoj.com/volume_showproblem.php?problem=1089 题意:给出许多区间,查询某个点所在的区间个数 思路:线段树,由于给出的是区间,查询 ...
- 【CF429E】Points and Segments(欧拉回路)
[CF429E]Points and Segments(欧拉回路) 题面 CF 洛谷 题解 欧拉回路有这样一个性质,如果把所有点在平面内排成一行,路径看成区间的覆盖,那么每个点被从左往右的覆盖次数等于 ...
- 【CF429E】Points and Segments 欧拉回路
[CF429E]Points and Segments 题意:给你数轴上的n条线段$[l_i,r_i]$,你要给每条线段确定一个权值+1/-1,使得:对于数轴上的任一个点,所有包含它的线段的权值和只能 ...
- Codeforces Round #245 (Div. 2) A. Points and Segments (easy) 贪心
A. Points and Segments (easy) Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/con ...
- 『ACM C++』 Codeforces | 1066A - Points in Segments
大一生活真 特么 ”丰富多彩“ ,多彩到我要忙到哭泣,身为班长,很多班级的事情需要管理,也是,什么东西都得体验学一学,从学生会主席.团委团总支.社团社长都体验过一番了,现在差个班长也没试过,就来体验了 ...
- Codeforces Round #501 (Div. 3) 1015A Points in Segments (前缀和)
A. Points in Segments time limit per test 1 second memory limit per test 256 megabytes input standar ...
- CF429E Points and Segments
链接 CF429E Points and Segments 给定\(n\)条线段,然后给这些线段红蓝染色,求最后直线上上任意一个点被蓝色及红色线段覆盖次数之差的绝对值不大于\(1\),构造方案,\(n ...
随机推荐
- 101 Hack 50
101 Hack 50 闲来无事.也静不下心,打个代码压压压惊 Hard Questions by kevinsogo Vincent and Catherine are classmates who ...
- javascript学习笔记 - 执行环境及作用域
一 执行环境(环境) 1.每个执行环境都有一个关联的全局变量对象.例如:web浏览器中,window对象为全局变量对象.环境中定义的所有变量和函数都保存在该对象中.全局执行环境是最外围的环境. 2.执 ...
- spring scope 属性的取值
Spring 容器是通过单例模式创建 Bean 对象的,也就是说,默认情况下,通过调用 ac.getBean("mybean")方法获得的对象都是同一个 mybean 对象 使用单 ...
- iOS-----openGL--openGL ES iOS 入门篇4---> 离屏渲染
http://www.cnblogs.com/CoderAlex/p/6604618.html 通常情况下,我们使用openGL将渲染好的图片绘制到屏幕上,但有时候我们不想显示处理结果,这时候就需要使 ...
- ACM程序设计选修课——1041: XX's easy problem(神烦的多次字符串重定向处理)
1041: XX's easy problem Time Limit: 1 Sec Memory Limit: 128 MB Submit: 41 Solved: 7 [Submit][Statu ...
- mybatis学习(八)——resultMap之association&&collection解析
一.resultMap的使用 resultMap 也是定义返回值类型,返回值为用户自定义的类型,可用于解决JavaBean中的属性名和数据库中的列名不一致的情况 之前对于JavaBean中属性名和数据 ...
- [转] Makefile 基础 (4) —— Makefile 书写命令
该篇文章为转载,是对原作者系列文章的总汇加上标注. 支持原创,请移步陈浩大神博客:(最原始版本) http://blog.csdn.net/haoel/article/details/2886 我转自 ...
- hust 1605 - Gene recombination(bfs+字典树)
1605 - Gene recombination Time Limit: 2s Memory Limit: 64MB Submissions: 264 Solved: 46 DESCRIPTION ...
- jQuery控件之分页控件-- kkpager v1.3使用简介
js分页展示控件,传入简单参数就能使用的分页效果控件 在线测试链接: http://pgkk.github.io/kkpager/example/pager_test.html http://pgkk ...
- BZOJ 4077 Messenger
Messenger [问题描述] alice和bob各自在两条折线上行进,一个邮递员要从alice那拿一个包裹,并以直线移动到bob处,alice和bob.邮递员的速度均为1单位/s,问邮递员最少要走 ...