Problem Description
As is known to all, the blooming time and duration varies between different kinds of flowers. Now there is a garden planted full of flowers. The gardener wants to know how many flowers will bloom in the garden in a specific time. But there are too many flowers in the garden, so he wants you to help him.
Input
The first line contains a single integer t (1 <= t <= 10), the number of test cases.
For each case, the first line contains two integer N and M, where N (1 <= N <= 10^5) is the number of flowers, and M (1 <= M <= 10^5) is the query times.
In the next N lines, each line contains two integer S i and T i (1 <= S i <= T i <= 10^9), means i-th flower will be blooming at time [S i, T i].
In the next M lines, each line contains an integer T i, means the time of i-th query.
For each case, the first line contains two integer N and M, where N (1 <= N <= 10^5) is the number of flowers, and M (1 <= M <= 10^5) is the query times.
In the next N lines, each line contains two integer S i and T i (1 <= S i <= T i <= 10^9), means i-th flower will be blooming at time [S i, T i].
In the next M lines, each line contains an integer T i, means the time of i-th query.
Output
For each case, output the case number as shown and then print M lines. Each line contains an integer, meaning the number of blooming flowers.
Sample outputs are available for more details.
Sample outputs are available for more details.
Sample Input
2 1 1 5 10 4 2 3 1 4 4 8 1 4 6
Sample Output
Case #1: 0 Case #2: 1 2 1
题目大意很简单,每组数据给你n,m;n代表n段种花范围;m代表m次询问,每次询问有一个数字,问当前位置花的多少;
思路很简单,离散化加线段树;
需要注意的是把询问的位置也要加入离散化,做的时候没加,一直错,,,,
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct point
{
int x,y;
};
struct SegmentTree
{
int l,r,x;
};
point a[100005];
SegmentTree tree[1200005];
int p[300005],f[100005];
void update_down(int k)
{
tree[k<<1].x += tree[k].x ;
tree[k<<1|1].x += tree[k].x;
tree[k].x = 0;
}
void build(int l,int r, int k)
{
tree[k].l=l, tree[k].r=r, tree[k].x=0;
if(l==r)
return;
int mid =(l+r)>>1;
build(l,mid,k<<1);
build(mid+1,r,k<<1|1);
}
void add(int l,int r,int k,int x)
{
if(tree[k].l>=l&&tree[k].r<=r)
{
tree[k].x += x;
return ;
}
if(tree[k].x) update_down(k);
if(tree[k<<1].r>=l) add(l,r,k<<1,x);
if(tree[k<<1|1].l<=r) add(l,r,k<<1|1,x);
}
int query(int l,int r, int k)
{
if(tree[k].l>=l&&tree[k].r<=r)
{
return tree[k].x;
}
if(tree[k].x) update_down(k);
int ans=0;
if(tree[k<<1].r>=l) ans += query(l,r,k<<1);
if(tree[k<<1|1].l<=r) ans += query(l,r,k<<1|1);
return ans;
}
int main()
{
int t;
scanf("%d",&t);
for(int c=1; c<=t; c++)
{
int n,m,s=0;
scanf("%d%d",&n,&m);
build(1,2*n+m,1);
for(int i=0; i<n; i++)
{
scanf("%d%d",&a[i].x,&a[i].y);
p[s++] = a[i].x;
p[s++] = a[i].y;
}
for(int i=0; i<m; i++)
{
scanf("%d",&f[i]);
p[s++] = f[i];
}
sort(p,p+s);
for(int i=0; i<n; i++)
{
a[i].x = lower_bound(p,p+s,a[i].x) - p + 1;
a[i].y = lower_bound(p,p+s,a[i].y) - p + 1;
}
for(int i=0; i<n; i++)
add(a[i].x, a[i].y,1,1);
printf("Case #%d:\n",c);
for(int i=0; i<m; i++)
{
int k = lower_bound(p,p+s,f[i]) - p + 1;
printf("%d\n",query(k,k,1));
}
}
return 0;
}