Flowers--(树状数组,离散化)

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


题目运用了树状数组的模式2,区间修改,单点求值,关键在于怎么离散化

代码:

#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<queue>
#include<cstring>
#include<map>
#include<vector>
using namespace std;
typedef long long ll;
#define M 200008
int n,m,maxn,it1,it2;
int tree[M],s[M],e[M],q[M],x[M];
inline int lowbit(int i)
{
    return i&(-i);
}
void add(int i,int v)
{
    while(i<=maxn)
    {
        tree[i]+=v;
        i+=lowbit(i);
    }
}
ll sum(int i)
{
    ll res=0;
    while(i>0)
    {
        res+=tree[i];
        i-=lowbit(i);
    }
    return res;
}
int bin(int obj,int y)
{
    int left=0,right=y-1,mid;
    while(left<=right)
    {
        mid=(left+right)/2;
        if(x[mid]==obj) return mid;
        if(x[mid]<obj) left=mid+1;
        else right=mid-1;
    }
}
int main()
{
    int i,T,a,b,cas=0;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        it1=0; it2=1;
        memset(x,0,sizeof(x));
        for(i=0;i<n;i++)
        {
            scanf("%d%d",&s[i],&e[i]);
            x[it1++]=s[i];
            x[it1++]=e[i];
        }
        for(i=0;i<m;i++)
        {
            scanf("%d",&q[i]);
            x[it1++]=q[i];
        }
        sort(x,x+it1);
        for(i=1;i<it1;i++)
            if(x[i]!=x[i-1]) x[it2++]=x[i];
        sort(x,x+it2);
        maxn=it2+1;
        memset(tree,0,sizeof(tree));
        for(i=0;i<n;i++)
        {
            a=bin(s[i],it2);
            b=bin(e[i],it2);
            add(a+1,1);
            add(b+2,-1);
        }
        printf("Case #%d:\n",++cas);
        for(i=0;i<m;i++)
        {
            a=bin(q[i],it2);
            printf("%I64d\n",sum(a+1));
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值