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
#include
#include
#include
#include
#include
#include
#include
#include
#include
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]


你可能感兴趣的:(2017暑假集训题目,树状数组)