题目链接: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;
}