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[sub]i[/sub] and T[sub]i[/sub] (1 <= S[sub]i[/sub] <= T[sub]i[/sub] <= 10^9), means i-th flower will be blooming at time [S[sub]i[/sub], T[sub]i[/sub]]. In the next M lines, each line contains an integer T[sub]i[/sub], 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 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
区间更新,单点求和的一维树状数组。
模板题就不多说了。
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
long long c[100005];
long long a[100005];
long long lowbit(long long x)
{
return x&(-x);
}
long long add(long long x,long long v)
{
while(x<=100001)
{
c[x]+=v;
x=x+lowbit(x);
}
}
long long sum(int x)
{
long long su=0;
while(x>0)
{
su+=c[x];
x-=lowbit(x);
}
return su;
}
int main()
{
int t;
scanf("%d",&t);
int j=1;
while(j<=t)
{ memset(c,0,sizeof(c));
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
long long q,w;
scanf("%I64d%I64d",&q,&w);
add(q,1);
add(w+1,-1);
}
for(int i=1;i<=m;i++)
{
scanf("%I64d",&a[i]);
}
printf("Case #%d:\n",j);
for(int i=1;i<=m;i++)
{
printf("%I64d\n",sum(a[i]));
}
j++;
}
return 0;
}