Problem I
Time Limit : 4000/2000ms (Java/Other) Memory Limit : 65536/65536K (Java/Other)
Total Submission(s) : 30 Accepted Submission(s) : 18
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 <cstring>
#include <stdio.h>
#include <algorithm>
const int maxn=1e5+10;
using namespace std;
int a[maxn];
int n,m;
int lowbit(int i)
{
return i&-i;
}
int sum(int i)
{
int summ=0;
while(i>0){
summ+=a[i];
i-=lowbit(i);
}
return summ;
}
void add(int i,int x){
while(i<=100005){
a[i]+=x;
i+=lowbit(i);
}
}
int main()
{
int t;
int i,j;
int s,e;
scanf("%d",&t);
for(int ss=1;ss<=t;ss++)
{
memset(a,0,sizeof(a));
printf("Case #%d:\n",ss);
scanf("%d%d",&n,&m);
while(n--){
scanf("%d%d",&s,&e);
add(s,1);
add(e+1,-1);
}
while(m--){
scanf("%d",&j);
printf("%d\n",sum(j));
}
}
return 0;
}