题目大意:
给你n朵花,m个查询,对于每一朵花给出这朵花的开花期,对于每一个查询,输出该时间的开花数。
题目很简单,一看就知道线段树或者是树状数组就可以做了,但是数据范围是10的9次方,数组开不了这么大,于是离散化,把10的9次方的数离散成10的5次方,题目就可以解决了。
我这里选择的是用树状数组,因为代码量少好多,因为在查询的时候,查询的时间不一定是开花期的端点,所以我把查询的时间保存下来,加入到数组中离散。
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 100000 + 10;
int sum[maxn];
typedef struct _node
{
int start, end;
}Node;
Node node[maxn];
int a[maxn * 4];
int save[maxn];
int lowbit (int x)
{
return x & (-x);
}
void add (int i, int value, int n)
{
while (i <= n)
{
sum[i] += value;
i += lowbit (i);
}
}
int Sum (int i)
{
int count = 0;
while (i > 0)
{
count += sum[i];
i -= lowbit (i);
}
return count;
}
int main ()
{
int t;
scanf ("%d", &t);
int cnt = 1;
while (t--)
{
int n, m;
scanf ("%d%d", &n, &m);
memset (sum, 0, sizeof (sum));
int i, j;
int count = 0;
for (i = 0; i < n; i++)
{
scanf ("%d%d", &node[i].start, &node[i].end);
a[count++] = node[i].start;
a[count++] = node[i].end;
}
for (i = 0; i < m; i++)
{
scanf ("%d", &save[i]);
a[count++] = save[i];
}
sort (a, a + count);//排序
count = unique (a, a + count) - a;//去重
for (i = 0; i < n; i++)
{
int x = lower_bound (a, a + count, node[i].start) - a + 1;//二分查找
int y = lower_bound (a, a + count, node[i].end) - a + 1;
add (x, 1, count);
add (y + 1, -1, count);
}
printf ("Case #%d:\n", cnt++);
for (i = 0; i < m; i++)
{
int ans = lower_bound (a, a + count, save[i]) - a + 1;
printf ("%d\n", Sum (ans));
}
}
return 0;
}