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 ...
随机推荐
- 5中IO模型整理总结
1.5中IO模型: 阻塞I/O(blocking IO) 非阻塞I/O(noblocking IO) I/O复用 (IO multiplexing ) 信号驱动I/O (signal drive ...
- Django模板(filter过滤器{{ }}与tag标签{% %}应用)
模板里面过滤器与标签的应用 templates模板里面的应用参考(主要应用在这里面) <!DOCTYPE html> <html lang="en"> & ...
- [uiautomator篇][10] uiautomator进阶
http://coderlin.coding.me/2016/07/02/Android-%E8%BF%9B%E9%98%B6%E4%B9%8B%E8%87%AA%E5%8A%A8%E5%8C%96% ...
- 深度理解STL之map、set
课程设计做了这个一直没有整理(搬运 set算是关键字和相同的特殊map set应该更加被强调理解为“集合”,而集合所涉及的操作并.交.差等,即STL提供的如交集set_intersection().并 ...
- ubuntu14 安装和使用docker
Docker简介 Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化.容器是完全使用沙箱机制,相互 ...
- 用js判断浏览器类型及设备
<!DOCTYPE html> <html> <head> <title>JS判断是什么设备是什么浏览器</title> <meta ...
- LaTeX:毕设模版设计问题1 各级标题编号设为黑体
我用'titlesec'宏包设计各级标题格式指定字体为黑体,结果标题编号仍为Times New Roman .
- BZOJ4556 [Tjoi2016&Heoi2016]字符串 【后缀数组 + 主席树 + 二分 + ST表】
题目 佳媛姐姐过生日的时候,她的小伙伴从某东上买了一个生日礼物.生日礼物放在一个神奇的箱子中.箱子外边写了 一个长为n的字符串s,和m个问题.佳媛姐姐必须正确回答这m个问题,才能打开箱子拿到礼物,升职 ...
- yield的概念及使用姿势
概念: 当调用Thread.yield方法时,会给线程调度器一个当前线程愿意让出CPU使用的暗示,但是线程调度器可能会忽略这个暗示. 代码演示: public class YieldDemo impl ...
- 你所在的公司是如何实施DEVOPS的?
工欲善其事,必先利其器,现在大家在DevOps领域最关注的还是在工具层面.下面是我跟这么多公司接触下来,大家使用比较多的工具:1.监控工具比较老牌的就是Zabbix,Nagios,用Zabbix的感觉 ...