(线段树 区间运算求点)Flowers -- hdu -- 4325

http://acm.hdu.edu.cn/showproblem.php?pid=4325

Flowers

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2577    Accepted Submission(s): 1263

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 Si and Ti (1 <= Si <= Ti <= 10^9), means i-th flower will be blooming at time [Si, Ti].
In the next M lines, each line contains an integer Ti, 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

Author

BJTU

Source

2012 Multi-University Training Contest 3

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<queue>
using namespace std;

#define N 110000
#define MOD 100000007
#define Lson r<<1
#define Rson r<<1|1

struct node
{
    int L, R, e;
    int Mid()
    {
        return (L+R)/2;
    }
}a[N<<2];

void BuildTree(int r, int L, int R)
{
    a[r].L=L, a[r].R=R, a[r].e=0;

    if(L==R)
        return ;

    BuildTree(Lson, L, a[r].Mid());
    BuildTree(Rson, a[r].Mid()+1, R);
}

void Update(int r, int L, int R)
{
    if(a[r].L==L && a[r].R==R)
    {
        a[r].e++;
        return ;
    }

    if(R<=a[r].Mid())
        return Update(Lson, L, R);
    else if(L>a[r].Mid())
        return Update(Rson, L, R);
    else
    {
        Update(Lson, L, a[r].Mid());
        Update(Rson, a[r].Mid()+1, R);
    }
}

void UP(int r, int L, int R)
{
    if(L==R)
        return ;

    if(a[r].L==L && a[r].R==R )
    {
        a[Lson].e += a[r].e;
        a[Rson].e += a[r].e;
    }

    if(R<=a[r].Mid())
         UP(Lson, L, R);
    else if(L>a[r].Mid())
         UP(Rson, L, R);
    else
    {
         UP(Lson, L, a[r].Mid());
         UP(Rson, a[r].Mid()+1, R);
    }
}

int Query(int r, int x)
{
    if(a[r].L==a[r].R && a[r].L==x)
       return a[r].e;

    if(x<=a[r].Mid())
        return Query(Lson, x);
    else
        return Query(Rson, x);
}

int main()
{
    int T, iCase=1;
    scanf("%d", &T);
    while(T--)
    {
        int n, m, i, L, R, x;

        scanf("%d%d", &n, &m);

        BuildTree(1, 1, 100005);

        for(i=0; i<n; i++)
        {
            scanf("%d%d", &L, &R);
            Update(1, L, R);
        }

        UP(1, 1, 100005);

        printf("Case #%d:\n", iCase++);
        for(i=0; i<m; i++)
        {
            scanf("%d", &x);
            printf("%d\n", Query(1, x));
        }
    }
    return 0;
}
时间: 2024-12-13 10:38:30

(线段树 区间运算求点)Flowers -- hdu -- 4325的相关文章

hdu 1255 覆盖的面积 线段树扫描线求重叠面积

稀里糊涂打完了没想到1A了,心情还是很舒畅的,c和c++的四舍五入还是四舍六入遇积进位遇偶舍,我感觉很混乱啊,这道题我输出的答案是7.62,也就是遇偶舍了,可是我就手动处理一下进位问题,发现0.005 系统自动进位0.01了,尼玛啊,我一下子就混乱了,不是遇偶舍么,0也是偶数啊,怎么就进位了呢.最后我就不手动处理进位问题了,直接0.2lf%,虽然我输出的结果是7.62,但是提交也过了 这道题和poj1151,hdu1542差不多,扫描线详细讲解http://blog.csdn.net/young

hdu4521-小明系列问题——小明序列(线段树区间求最值)

题意:求最长上升序列的长度(LIS),但是要求相邻的两个数距离至少为d,数据范围较大,普通dp肯定TLE.线段树搞之就可以了,或者优化后的nlogn的dp. 代码为  线段树解法. 1 #include <set> 2 #include <map> 3 #include <cmath> 4 #include <ctime> 5 #include <queue> 6 #include <stack> 7 #include <cct

线段树2 求区间最小值

线段树2 求区间最小值 从数组arr[0...n-1]中查找某个数组某个区间内的最小值,其中数组大小固定,但是数组中的元素的值可以随时更新. 数组[2, 5, 1, 4, 9, 3]可以构造如下的二叉树(背景为白色表示叶子节点,非叶子节点的值是其对应数组区间内的最小值,例如根节点表示数组区间arr[0...5]内的最小值是1): 线段树的四种操作: 1.线段树的创建 2.线段树的查询 3.线段树的更新单节点 4.线段树的更新区间 直接上完整代码吧 1 #include <bits/stdc++.

hdu2852--KiKi&#39;s K-Number(线段树,求第k个数)

KiKi's K-Number Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2546    Accepted Submission(s): 1174 Problem Description For the k-th number, we all should be very familiar with it. Of course,t

线段树+扫描线+离散化解poj1151 hdu 1542 ( Atlantis )

受此链接很大启发才明白扫描线: http://www.cnblogs.com/scau20110726/archive/2013/04/12/3016765.html 我的代码如下: #include<iostream> #include<map> #include<string> #include<cstring> #include<cstdio> #include<cstdlib> #include<cmath> #i

线段树 : 求矩形面积的并 ---- hnu : 12884 Area Coverage

Area Coverage Time Limit: 10000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit users: 16, Accepted users: 12 Problem 12884 : No special judgement Problem description In this day and age, a lot of the spying on other countries is done

HDU - 1754 I Hate It (线段树区间求最值)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754 题意:线段树的单点更新和区间求最值 模板题,,,???,, 1 #include <cstdio> 2 #include <iostream> 3 using namespace std; 4 5 typedef long long LL; 6 const int N=200010; 7 8 LL ans; 9 LL max(LL a,LL b){ 10 if(a>b) r

[HDU] 2795 Billboard [线段树区间求最值]

Billboard Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 11861    Accepted Submission(s): 5223 Problem Description At the entrance to the university, there is a huge rectangular billboard of s

离散化+线段树/二分查找/尺取法 HDOJ 4325 Flowers

题目传送门 题意:给出一些花开花落的时间,问某个时间花开的有几朵 分析:这题有好几种做法,正解应该是离散化坐标后用线段树成端更新和单点询问.还有排序后二分查找询问点之前总花开数和总花凋谢数,作差是当前花开的数量,放张图易理解: 还有一种做法用尺取法的思想,对暴力方法优化,对询问点排序后再扫描一遍,花开+1,花谢-1.详细看代码. 收获:一题收获很多:1. 降低复杂度可以用二分 2. 线段计数问题可以在端点标记1和-1 3. 离散化+线段树 终于会了:) (听说数据很水?) 代码1:离散化+线段树