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 ...
随机推荐
- Welcome-to-Swift-01基础部分
Swift 是 iOS 和 OS X 应用开发的一门新语言.然而,如果你有 C 或者 Objective-C 开发经验的话,你会发现 Swift 的很多内容都是你熟悉的. Swift 的类型是在 C ...
- 【Luogu】P3313旅行(树链剖分)
题目链接 动态开点的树链剖分qwq. 跟小奇的花园一模一样,不做过多讲解. #include<cstdio> #include<cstring> #include<cct ...
- Java设计模式(Design Patterns)——可复用面向对象软件的基础
设计模式(Design Patterns) 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结. 使用设计模式是为了可重用代码.让代码更容易被他 ...
- 【bzoj1717】[Usaco2006 Dec]Milk Patterns 产奶的模式 SA+二分
Description 农夫John发现他的奶牛产奶的质量一直在变动.经过细致的调查,他发现:虽然他不能预见明天产奶的质量,但连续的若干天的质量有很多重叠.我们称之为一个“模式”. John的牛奶按质 ...
- 转 Python常见数据结构整理
http://www.cnblogs.com/jeffwongishandsome/archive/2012/08/05/2623660.html Python常见数据结构整理 Python中常见的数 ...
- 使用 PHPMailer 发邮件
/** * 发邮件 * * @param array $receiver 接收人信息 * @param array $attachment_info 附件信息 * @param string $is_ ...
- Python远程视频监控程序
老板由于事务繁忙无法经常亲临教研室,于是让我搞个监控系统,让他在办公室就能看到教研室来了多少人.o(>﹏<)o||| 最初我的想法是直接去网上下个软件,可是找来找去不是有毒就是收费,无奈技 ...
- AC日记——Andryusha and Socks Codeforces 780a
A. Andryusha and Socks time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- ListView+EditText使用遇到的坑
最近项目中某功能需要ListView嵌套EditText来实现,使用过程中遇到一些问题: 1.点击弹出编辑框,edittext会失去焦点. 解决焦点丢失的问题 解决思路:软键盘弹出的时候会重新绘制界面 ...
- html-屏蔽按键盘空格键是滚动条向下滚动
document.onkeydown = function(ev){ var e = ev || event; if(e.keyCode == 32){ return false; } }