如何在不超出时间限制的情况下解决此问题
http://codeforces.com/problemset/problem/474/B
我尝试将所有范围放在2D矢量中,然后使用二分搜索查找所需的索引,但似乎fn中的循环BS()需要花费很多时间才能执行,因为矢量的大小可以是10 ^ 6.
这是我的代码:
#include <iostream>
#include <vector>
using namespace std;
int Search(vector <vector<int> > a,int key){
int start = 0;
int end = a.size() - 1;
while (start <= end){
int mid = start + (end - start) / 2;
if (a[mid][0] > key && a[mid][1] > key){
end = mid - 1;
}
else if (a[mid][0] < key && a[mid][1] < key){
start = mid + 1;
}
else {
return mid;
}
} …Run Code Online (Sandbox Code Playgroud)