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 ...
随机推荐
- TransH中的Hinge Loss Function
Hinge Loss Function Hinge Loss 函数一种目标函数,有时也叫max-margin objective. 在Trans系列中,有一个 \[ \max(0,f(h,r,t) + ...
- python踩坑系列——报错后修改了.py文件,但是依然报错
一开始.py文件中的函数名大小写错了,但是在终端是对的,报错: 'module' object has no attribute '某函数名' 后来就去修改.py文件.结果重新import该.py文件 ...
- 【瓜分5000元奖金】Wannafly挑战赛13
链接:https://www.nowcoder.com/acm/contest/80/A来源:牛客网 zzy的小号 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他 ...
- 以前刷过的数位dp
TOJ1688: Round Numbers Description The cows, as you know, have no fingers or thumbs and thus are una ...
- iOS学习笔记27-摄像头
一.摄像头 在iOS中,手机摄像头的使用有以下两种方法: UIImagePickerController拍照和视频录制 优点:使用方便,功能强大 缺点:高度封装性,无法实现一些自定义工作 AVFoun ...
- 我最喜欢的XML(三种方式)
我最喜欢的方式 下面的三个 XML 文档包含完全相同的信息: 第一个例子中使用了 date 属性: <note date="08/08/2008"> <to> ...
- TCP面试题之为什么需要三次握手才能建立连接/四次挥手才能断开连接
为什么需要三次握手才能建立连接? 答:为了初始化Sequence Number(序列号)的初始值,要通知双方数据包的序号,作为以后通讯的序号,以保证在网络传输过程,不会因为网络原因而导致乱序: 为什么 ...
- python 面向对象与类的基本知识
一 什么是面向对象,面向对象与类的关系. 面向对象的程序设计是用来解决扩展性. 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 面向对象 ...
- Spring Boot 配置大全
Spring Boot 允许通过外部配置让你在不同的环境使用同一应用程序的代码,简单说就是可以通过配置文件来注入属性或者修改默认的配置. SpringBoot的配置方式有很多,它们的优先级如下所示(优 ...
- 我要好offer之 搜索算法大总结
1. 二分搜索 详见笔者博文:二分搜索的那些事儿,非常全面 2. 矩阵二分搜索 (1) 矩阵每行递增,且下一行第一个元素大于上一个最后一个元素 (2) 矩阵每行递增,且每列也递增 3. DFS 深度优 ...