二分--LIGHTOJ 1088查找区间(水题)
- #include <iostream>
- #include <cstdio>
- #include <cmath>
- using namespace std;
- const int maxN = 100005;
- int a[maxN];
- int t, tt;
- int n, q, x, y;
- int Bsearch_lower_bound(int x)
- {
- int l = 0, r = n - 1, mid = 0;
- while (l <= r)
- {
- mid = (l + r) >> 1;
- if (a[mid] < x) l = mid + 1;
- else r = mid - 1;
- }
- return l;
- }
- int Bsearch_upper_bound(int x)
- {
- int l = 0, r = n - 1, mid = 0;
- while (l <= r)
- {
- mid = (l + r) >> 1;
- if (a[mid] <= x) l = mid + 1;
- else r = mid - 1;
- }
- return l;
- }
- void solve()
- {
- scanf("%d%d", &n, &q);
- printf("Case %d:\n", ++tt);
- for (int i = 0; i < n; i++)
- scanf("%d", &a[i]);
- for (int i = 0; i < q; i++)
- {
- scanf("%d%d", &x, &y);
- int l = Bsearch_lower_bound(x);
- int r = Bsearch_upper_bound(y);
- printf("%d\n", r - l);
- }
- }
- int main()
- {
- scanf("%d", &t);
- while (t--) solve();
- return 0;
- }
版权声明:本文为博主原创文章,未经博主允许不得转载。
二分--LIGHTOJ 1088查找区间(水题)的更多相关文章
- LightOJ 1248 Dice (III) (水题,期望DP)
题意:给出一个n面的色子,问看到每个面的投掷次数期望是多少. 析:这个题很水啊,就是他解释样例解释的太...我鄙视他,,,,, dp[i] 表示 已经看到 i 面的期望是多少,然后两种选择一种是看到新 ...
- lightoj 1020 (博弈水题)
lightoj 1020 A Childhood Game 链接:http://lightoj.com/volume_showproblem.php?problem=1020 题意:一堆石子有 m 个 ...
- LightOJ 1023 Discovering Permutations 水题
http://www.lightoj.com/volume_showproblem.php?problem=1023 题意:26字母全排列 思路:用next_permutation或者思维想一下都可以 ...
- LightOJ 1214 Large Division 水题
java有大数模板 import java.util.Scanner; import java.math.*; public class Main { public static void main( ...
- LightOJ 1220 Mysterious Bacteria 水题
暴力就行了,找出素因子,正的最多是30,然后负的最多是31(这一点wa了一次) #include <cstdio> #include <iostream> #include & ...
- SPOJ CNTPRIME 13015 Counting Primes (水题,区间更新,求区间的素数个数)
题目连接:http://www.spoj.com/problems/CNTPRIME/ #include <iostream> #include <stdio.h> #incl ...
- SPOJ 3693 Maximum Sum(水题,记录区间第一大和第二大数)
#include <iostream> #include <stdio.h> #include <algorithm> #define lson rt<< ...
- SPOJ 7259 Light Switching (水题,区间01取反)
#include <iostream> #include <stdio.h> #include <algorithm> #define lson rt<< ...
- lightoj 1010 (水题,找规律)
lightoj 1010 Knights in Chessboard 链接:http://lightoj.com/volume_showproblem.php?problem=1010 题意:国际象棋 ...
随机推荐
- windows 8.1 MessageDialog
private Popup p; private void Button_Click(object sender, RoutedEventArgs e) { p=new Popup(); Denglu ...
- NSDateFormatter中时间格式串的含义
a: AM/PM (上午/下午) A: 0~86399999 (一天的第A微秒) c/cc: 1~7 (一周的第一天, 周天为1) ccc: Sun/Mon/Tue/Wed/Thu/Fri/Sat ( ...
- Conditional Counting In SQL
If you ever want to conditionally count the number of times a particular condition occurs in SQL, ...
- 为 HTML 添加新元素
你可以为 HTML 添加新的元素. 该实例向 HTML 添加的新的元素,并为该元素定义样式,元素名为 <myHero> : 实例 <!DOCTYPE html> <htm ...
- ngx_http_upstream_module模块学习笔记
ngx_http_upstream_module用于将多个服务器定义成服务器组,而由proxy_pass,fastcgi_pass等指令引用 (1)upstream name {...} 定义一个后 ...
- Java 中的构造方法
首先创建一个Transport类,定义好类的属性和方法,并且写好构造方法,先看下无参数的构造方法: public class Transport { //名字 public String name; ...
- PHP代码优化的53个细节
PHP代码优化的53个细节,常见而重要的php优化策略. 用单引号代替双引号来包含字符串,这样做会更快一些.因为PHP会在双引号包围的字符串中搜寻变量,单引号则不会,注意:只有echo能这么做,它是一 ...
- 小鸟哥哥博客 For SAE
独立博客地址:http://www.zhujiawei.com.cn/ 辞职后出去玩了几个月,把积蓄都快花光了,打算熬到年底再找工作.最近闲来无聊,想起自己一年前趁着活动便宜,一口气买了10年的域名一 ...
- 【设计模式】策略模式 (Strategy Pattern)
策略模式是一种很简单的基础模式,用于封装一系列算法,使客户端的访问独立于算法的实现.我们可以”井中取水”来形象的描述策略模式.“取水”是一个动作,完成这个动作的方式有很多中,可以直接用手提.可以用水车 ...
- EMVTag系列4《5A 应用主账号》
L:var.最大10 -M(必备):此数据应存在并提供给终端,终端在读应用数据过程中,如果没有读到必备数据,终端中止交易:等同磁条上的应用主帐户. 银行卡号一般是16位或者19位.由如下三部分构成: ...