1009(树状数组)

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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值