HDU4325——二分——Flowers

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

/*
upper_bound 找大于a[i]的最近的下标
lower_bound 找大于等于a[i]的最近的下标
1 2 4 4 4 5 6 ...
l r l r l l l ...
此时q = 4
upper_bound 值为4
lower_bound 值为2
l : (4,5]
r : [4,5)
l - r : [4]
 */
/************************************************
* Author        :Powatr
* Created Time  :2015-8-25 15:03:50
* File Name     :F.cpp
 ************************************************/

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std;

#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int MAXN = 1e5 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;

int l[MAXN], r[MAXN];
int main(){
    int T;
    int n, m;
    int q;
    scanf("%d", &T);
    for(int cas = 1; cas <= T; cas++){
        scanf("%d%d", &n, &m);
        for(int i = 1; i <= n; i++)
            scanf("%d%d", &l[i], &r[i]);
        sort(l + 1, l + n + 1);
        sort(r + 1, r + n + 1);
        printf("Case #%d:\n", cas);
        for(int i = 1; i <= m; i++){
            scanf("%d", &q);
            int ans = (upper_bound(l + 1, l + n + 1, q) - (l + 1)) - (lower_bound(r + 1, r + n + 1, q) - (r + 1));
            printf("%d\n", ans);
        }
    }
        return 0;
}

  

时间: 2024-11-08 20:40:11

HDU4325——二分——Flowers的相关文章

HDU 4614 Vases and Flowers(线段树区间更新+二分)

Problem Description Alice is so popular that she can receive many flowers everyday. She has N vases numbered from 0 to N-1. When she receive some flowers, she will try to put them in the vases, one flower in one vase. She randomly choose the vase A a

HDU 4614 Vases and Flowers 线段树+二分

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4614 题意:N个花瓶,两种操作. 操作1:从第a个花瓶开始放花,放最多f个,如果花瓶已经有花就跳过,直到放完,或者无更多花瓶,要求输出这次放花的第一个位置和最后一个位置,如果没放就输出Cannot... 操作2:将花瓶a到b区间内的花都扔了,然后输出扔掉的花的数目. 解题思路:花瓶有花为0,无花为1,那么实际上这是一个区间更新以及求和,求左右端点的问题.线段树节点维护一个 sum--区间和,lid-

hdu4325 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.

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

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

HDU4614Vases and Flowers 二分+线段树;

参考:https://blog.csdn.net/ophunter_lcm/article/details/9879495 题意: 有n个花瓶,有两种操作,1.从a开始放b朵花,有花的花瓶跳过,2.把a到b间的花全部拿下来. 思路: 线段树+二分 利用二分确定区间,这样就可以是线段树实现更简单的问题: 1)对区间进行全部设置为1的操作 2)对区间进行全部清零的操作 #include <iostream> #include <cstdio> #include <algorith

Vases and Flowers (二分 + 线段树)

题目链接:https://vjudge.net/contest/332656#problem/H 题意: n个花瓶,m个操作,花瓶里面有的有花,有的是空的. 1 x y 表示从第x个位置开始查y多花,若一朵花也插不上输出"Can not put any one.",反之输出插花的左位置和右位置. 2 操作是清除区间[a,b]的花.并输出清除了多少花. 思路:线段树维护区间,然后每次二分查找满足要求的第一个位置. 1 #include <math.h> 2 #include

Flowers(二分水过。。。)

Flowers Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2579    Accepted Submission(s): 1265 Problem Description As is known to all, the blooming time and duration varies between different kinds

hdu4614 Vases and Flowers

Vases and Flowers Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 2148    Accepted Submission(s): 836 Problem Description Alice is so popular that she can receive many flowers everyday. She has

HDU - 4614 【二分+线段树维护】

Vases and Flowers Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 3263    Accepted Submission(s): 1299 Problem Description Alice is so popular that she can receive many flowers everyday. She has