首先介绍这两种函数是什么意思

upper_bound是找到大于t的最小地址,如果没有就指向末尾

lower_bound是找到大于等于t的最小地址

题目链接:https://vjudge.net/contest/231314#problem/E

You are given n integers a1, a2, ..., an. Find the number of pairs of indexes i, j (i < j) that ai + aj is a power of 2 (i. e. some integer x exists so that ai + aj = 2x).

Input

The first line contains the single positive integer n (1 ≤ n ≤ 105) — the number of integers.

The second line contains n positive integers a1, a2, ..., an (1 ≤ ai ≤ 109).

Output

Print the number of pairs of indexes i, j (i < j) that ai + aj is a power of 2.

Examples

Input
4
7 3 2 1
Output
2
Input
3
1 1 1
Output
3

Note

In the first example the following pairs of indexes include in answer: (1, 4) and (2, 4).

In the second example all pairs of indexes (i, j) (where i < j) include in answer.

题目大意:输入n,代表有n个数,接下来有n个数,问你两个数相加的和是2的整数次幂的个数

个人思路:觉得这道题并不难,然后自己写一遍超时了(没有用二分查找),然后改为用二分,还是超时,这就有有点难受了,后来实在不知道

哪里可以优化,只能百度了

先看一下自己超时的代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<string.h>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=1e9+;
const int maxn=1e5+;
const ll maxa=1e10;
#define INF 0x3f3f3f
ll b[];
//#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
void solve()
{
ll p=;
for(int i=;i<=;i++)
{
p*=;
b[i]=p;
}
}
bool judge(ll n,int l,int r)
{
int mid=(l+r)/;
while(l<=r)
{
if(n>b[mid])
{
l=mid+;
}
else if(n<b[mid])
r=mid-;
else if(n==b[mid])
return true;
mid=(l+r)/;
}
return false;
}
int main()
{
solve();
ll ans=;
ll a[maxn];
int n;
//cin>>n;
scanf("%d",&n);
for(int i=;i<n;i++)
scanf("%lld",&a[i]);
//cin>>a[i];
for(int i=;i<n;i++)
{
for(int j=i+;j<n;j++)//其实这里也是可以优化的,自己没想到罢了
{
if(judge(a[i]+a[j],,))
ans++;
}
}
printf("%lld\n",ans);
// cout<<ans<<endl;
return ;
}

然后看一下ac 代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<string.h>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=1e9+;
const int maxn=1e5+;
const ll maxa=1e10;
#define INF 0x3f3f3f
ll b[];
//#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
void solve()
{
ll p=;
for(int i=;i<=;i++)
{
p*=;
b[i]=p;
}
}
int main()
{
solve();
ll ans=,tmp;
ll a[maxn];
int n;
//cin>>n;
scanf("%d",&n);
for(int i=;i<n;i++)
scanf("%lld",&a[i]);
sort(a,a+n);
//cin>>a[i];
for(int i=;i<n;i++)//只要遍历一遍就够了
{
for(int j=;j<=;j++)
{
tmp=b[j]-a[i];//tmp是剩下的那个数
if(tmp>)
ans+=upper_bound(a+i+,a+n,tmp)-lower_bound(a+i+,a+n,tmp);//大于tmp的数的下标减去大于等于tmp的数的下标,就知道有没有等于tmp的数了
}
}
printf("%lld\n",ans);
// cout<<ans<<endl;
return ;
}

关于lower_bound和upper_bound的第二种用法

int t=lower_bound(a,a+n,k)-a   返回第一个大于等于k的下标,如果k比数组里面所有的数都大,就返回a+n,如果k比所有数都小,返回第一个元素下标

int t=upper_bound(a,a+n,k)-a  返回第一个大于k的下标,如果k比数组所有元素都大,就返回a+n,如果k比所有数都小,返回第一个元素下标

题目链接:https://vjudge.net/contest/231315#problem/D

Vasiliy likes to rest after a hard work, so you may often meet him in some bar nearby. As all programmers do, he loves the famous drink "Beecola", which can be bought in n different shops in the city. It's known that the price of one bottle in the shop i is equal to xi coins.

Vasiliy plans to buy his favorite drink for q consecutive days. He knows, that on the i-th day he will be able to spent mi coins. Now, for each of the days he want to know in how many different shops he can buy a bottle of "Beecola".

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of shops in the city that sell Vasiliy's favourite drink.

The second line contains n integers xi (1 ≤ xi ≤ 100 000) — prices of the bottles of the drink in the i-th shop.

The third line contains a single integer q (1 ≤ q ≤ 100 000) — the number of days Vasiliy plans to buy the drink.

Then follow q lines each containing one integer mi (1 ≤ mi ≤ 109) — the number of coins Vasiliy can spent on the i-th day.

Output

Print q integers. The i-th of them should be equal to the number of shops where Vasiliy will be able to buy a bottle of the drink on the i-th day.

Example

Input
5
3 10 8 6 11
4
1
10
3
11
Output
0
4
1
5

Note

On the first day, Vasiliy won't be able to buy a drink in any of the shops.

On the second day, Vasiliy can buy a drink in the shops 1, 2, 3 and 4.

On the third day, Vasiliy can buy a drink only in the shop number 1.

Finally, on the last day Vasiliy can buy a drink in any shop.

题目大意:输入n,代表有n个商店,下面n个数代表每个商店卖酒的价格,输入m,接下来m个数,代表有多少前,问每一次能在多少个商店买酒

超快能解决,用upper_bound

先把所有的商店价格按从小到大排序,然后找到第一个大于自己拥有钱的下标就行了

#include<iostream>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<string.h>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=1e9+;
const int maxn=1e5+;
const ll maxa=1e10;
#define INF 0x3f3f3f3f3f3f
int main()
{
ll n,m,mo;
ll a[maxn];
cin>>n;
for(ll i=;i<n;i++)
cin>>a[i];
sort(a,a+n);
cin>>m;
for(int i=;i<=m;i++)
{
cin>>mo;
cout<<(upper_bound(a,a+n,mo)-a)<<endl;
}
return ;
}

upper_bound和lower_bound的用法的更多相关文章

  1. 【刷题记录】 && 【算法杂谈】折半枚举与upper_bound 和 lower_bound

    [什么是upper_bound 和 lower_bound] 简单来说lower_bound就是你给他一个非递减数列[first,last)和x,它给你返回非递减序列[first, last)中的第一 ...

  2. upper_bound()与lower_bound()的使用

    upper_bound()与lower_bound()的使用 c++中的许多库函数可以使我们的代码量大大减少,也可使问题简单化.很早之前就接触了upper_bound()与lower_bound(), ...

  3. [C++] upper_bound和lower_bound

    upper_bound 源码 template <class ForwardIterator, class T> ForwardIterator upper_bound (ForwardI ...

  4. 【模板】关于vector的lower_bound和upper_bound以及vector基本用法 STL

    关于lower_bound和upper_bound 共同点 函数组成: 一个数组元素的地址(或者数组名来表示这个数组的首地址,用来表示这个数组的开头比较的元素的地址,不一定要是首地址,只是用于比较的& ...

  5. 二分查找、upper_bound、lower_bound

    整理及总结二分查找的判断和边界细节 修改版 package com.leej.binarysearch; import java.util.Arrays; /** * @author jerry * ...

  6. upper_bound()和lower_bound()

    ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一个非递减序列[first, la ...

  7. C++中二分法之upper_bound()、lower_bound、binary_search()函数

    前言 数组.容器vector都适用,在头文件"algorithm"中 下面的例子是针对容器的,注意返回的是距离元素3最近的指针it,输出的是*it结果为元素4,假如我想得到位置而非 ...

  8. 关于lower_bound()的用法--NYOJ 201作业题

    lower_bound它有三个参数, 第一个和第二个是给定区间起点和终点的指针,第三个参数是要查找的数,它的作用原理是在给定的区间中进行二分查找,这个二分区间是前开后闭的,他返回第一个大于等于它的函数 ...

  9. lower_bound && upper_bound

     用lower_bound进行二分查找 ●在从小到大排好序的基本类型数组上进行二分查找. 这是二分查找的一种版本,试图在已排序的[first,last)中寻找元素value.如果[first,last ...

随机推荐

  1. bzoj 3924 幻想乡战略游戏 —— 动态点分治

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3924 参考了博客:https://blog.csdn.net/qq_34564984/art ...

  2. Poj 3356 ACGT(LCS 或 带备忘的递归)

    题意:把一个字符串通过增.删.改三种操作变成另外一个字符串,求最少的操作数. 分析: 可以用LCS求出最大公共子序列,再把两个串中更长的那一串中不是公共子序列的部分删除. 分析可知两个字符串的距离肯定 ...

  3. Wireshark抓包常见问题解析

    1.   tcp out-of-order(tcp有问题) 解答: 1).    应该有很多原因.但是多半是网络拥塞,导致顺序包抵达时间不同,延时太长,或者包丢失,需要重新组合数据单元 因为他们可能是 ...

  4. 【转】 Pro Android学习笔记(五七):Preferences(1):ListPreference

    目录(?)[-] 例子1ListPreference小例子 定义一个preferences XML文件 继承PreferenceActivity 用户定制偏好的读取 第一次运行时设置缺省值 设置Cat ...

  5. 关于cin

    今天同学调试一个简单的程序的时候发现了问题,我们两个讨论的时候弄出了好多乐子 #include <iostream> using namespace std; int main() { ; ...

  6. 韩顺平循序渐进学JAVA从入门到精通 视频全套,需要的联系我

    0讲-开山篇.avi 10讲-访问修饰符.重载.覆盖.avi 11讲-约瑟夫问题.avi 12讲-多态.avi 13讲-抽象类.接口.avi 14讲-final.作业评讲.avi 15讲-作业.测试题 ...

  7. 项目积累demo-01

    1 搭建Spring-Boot项目 在这里我使用intellij新建spring boot工程: 点击next; 输入Group以及artifact之后.点击next.之后点击web.接着finish ...

  8. js验证文本框数字

    输入框 <input name="title" type="text" oninput="onlyNum(this,'')" titl ...

  9. Maven 命令格式及一些常用命令

    Maven自身指定定义了一套对项目进行编译,测试,打包,运行,部署等工作的抽象.Maven自己是不实际负责这些工作的,而是把它们交给了插件.所以Maven命令的实际工作执行者是各种各样的插件. 要了解 ...

  10. 点云视窗类CloudViewer

    博客转载自:http://www.pclcn.org/study/shownews.php?lang=cn&id=149 点云视窗类CloudViewer是简单显示点云的可视化工具类,可以让用 ...