Flowers

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 2585    Accepted Submission(s): 1271

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

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define MAX 100000
struct tree
{
int l,r;
int m;
}num[MAX*10];
void build(int node,int l,int r)
{
num[node].l=l;
num[node].r=r;
num[node].m=0;
if(l==r)
return ;
int mid=(l+r)/2;
build(node*2,l,mid);
build(node*2+1,mid+1,r);
}
void updata(int node,int l,int r)
{
if(num[node].l==l&&num[node].r==r)
{
num[node].m++;
return ;
}
int mid=(num[node].l+num[node].r)/2;
if(r<=mid)
updata(node*2,l,r);
else if(l>mid)
updata(node*2+1,l,r);
else
{
updata(node*2,l,mid);
updata(node*2+1,mid+1,r);
}
}
int query(int node,int l,int r)
{
if(num[node].l==l&&num[node].r==r)
{
return num[node].m;
}
int mid=(num[node].l+num[node].r)/2;
if(r<=mid)
return query(node*2,l,r)+num[node].m;
else
{
if(l>mid)
return query(node*2+1,l,r)+num[node].m;
else
return query(node*2,l,mid)+query(node*2+1,mid+1,r);
}
}
int main()
{
int t;
int Case=1;
scanf("%d",&t);
while(t--)
{
int m,n,x,y,z;
scanf("%d%d",&n,&m);
build(1,1,MAX);
while(n--)
{
scanf("%d%d",&x,&y);
updata(1,x,y);
}
printf("Case #%d:\n",Case++);
while(m--)
{
scanf("%d",&z);
printf("%d\n",query(1,z,z));
}
}
return 0;
} 醉了醉了,二分都可以 <pre name="code" class="cpp">#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define MAX 100010
int a[MAX],b[MAX];
int main()
{
int t;
int Case=1;
scanf("%d",&t);
while(t--)
{
int m,n,x,y,z;
scanf("%d%d",&n,&m);
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
for(int i=0;i<n;i++)
scanf("%d%d",&a[i],&b[i]);
sort(a,a+n);sort(b,b+n);
printf("Case #%d:\n",Case++);
for(int i=0;i<m;i++)
{
scanf("%d",&z);
x=upper_bound(a,a+n,z)-a;
y=lower_bound(b,b+n,z)-b;
printf("%d\n",x-y);
}
}
return 0;
}

hdoj--4325--Flowers(线段树+二分)的更多相关文章

  1. hdoj 4325 Flowers 线段树+离散化

    hdoj 4325 Flowers 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4325 思路: 直接线段树,按照花的开放区间的大小建树,要注意虽然 ...

  2. hdu4614 Vases and Flowers 线段树+二分

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4614 题意: 给你N个花瓶,编号是0  到 N - 1 ,初始状态花瓶是空的,每个花瓶最多插一朵花. ...

  3. HDU 4614 Vases and Flowers(线段树+二分)

    题目链接 比赛的时候一直想用树状数组,但是树状数组区间更新之后,功能有局限性.线段树中的lz标记很强大,这个题的题意也挺纠结的. k = 1时,从a开始,插b个花,输出第一个插的位置,最后一个的位置, ...

  4. hdu4614 线段树+二分 插花

    Alice is so popular that she can receive many flowers everyday. She has N vases numbered from 0 to N ...

  5. Codeforces Gym 100803G Flipping Parentheses 线段树+二分

    Flipping Parentheses 题目连接: http://codeforces.com/gym/100803/attachments Description A string consist ...

  6. Codeforces Gym 100231B Intervals 线段树+二分+贪心

    Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...

  7. 洛谷P4344 脑洞治疗仪 [SHOI2015] 线段树+二分答案/分块

    !!!一道巨恶心的数据结构题,做完当场爆炸:) 首先,如果你用位运算的时候不小心<<打成>>了,你就可以像我一样陷入疯狂的死循环改半个小时 然后,如果你改出来之后忘记把陷入死循 ...

  8. luogu4422 [COCI2017-2018#1] Deda[线段树二分]

    讨论帖:线段树二分的题..我还考场切过..白学 这题我一年前的模拟赛考场还切过,现在就不会了..好菜啊. 显然直接线段树拆成$\log n$个区间,然后每个区间在进行线段树二分即可. UPD:复杂度分 ...

  9. bzoj4399 魔法少女LJJ 线段树合并+线段树二分+并查集

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4399 题解 毒瘤题 \(9\) 种操作还有支持动态图的连通性 仔细读题 $ c<=7$. ...

  10. [BZOJ 2653] middle(可持久化线段树+二分答案)

    [BZOJ 2653] middle(可持久化线段树+二分答案) 题面 一个长度为n的序列a,设其排过序之后为b,其中位数定义为b[n/2],其中a,b从0开始标号,除法取下整. 给你一个长度为n的序 ...

随机推荐

  1. HDU 4912 lca贪心

    Paths on the tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  2. 在外星人电脑上安装windows10和ubuntu16.04双系统小记

    最近刚刚入手了一台Alienware Aurora R6,买这货的主要目的是为了研究Deep Learning.之所以没有买组装机的原因,主要是担心组装机的不稳定,而实验经费中的设备费也还相对充足,于 ...

  3. Unable to access the IIS metabase

    https://stackoverflow.com/questions/12859891/error-unable-to-access-the-iis-metabase 解决方法1 On Window ...

  4. nyoj--99--单词拼接(欧垃图判定+输出)

    单词拼接 时间限制:3000 ms  |  内存限制:65535 KB 难度:5 描述 给你一些单词,请你判断能否把它们首尾串起来串成一串. 前一个单词的结尾应该与下一个单词的道字母相同. 如 alo ...

  5. Spark RDD概念学习系列之如何创建Pair RDD

    不多说,直接上干货! 创建Pair RDD Python语言 pairs = lines.map(lambda x: (x.split(], x))  scala语言 val pairs = line ...

  6. Paper阅读总结Day1

    Paper阅读总结Day1 1.Convolutional Neural Networks For Facial Expression Recognition 文章思想 简单的一篇关于表情识别的文章, ...

  7. c++的map有关

    Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字(key),每个关键字只能在map中出现一次,第二个可能称为该关键字的值(value))的数据 处理能力,由于这个特性,它完成有可能 ...

  8. BZOJ 3527: [Zjoi2014]力 FFT_卷积

    Code: #include <cmath> #include <cctype> #include <cstdio> #include <cstring> ...

  9. These relative modules were not found:...{"sourceM ap":false} 报错解决

    今天在使用vue2.0 + webpack 时,没有动过任何配置文件,也没更新依赖,但是报下面的错误: These relative modules were not found: * ./star1 ...

  10. ZOJ 3203 Light Bulb( 三分求极值 )

    链接:传送门 题意: 求影子长度 L 的最大值 思路:如果 x = 0 ,即影子到达右下角时,如果人继续向后走,那么影子一定是缩短的,所以不考虑这种情况.根据图中的辅助线外加相似三角形定理可以得到 L ...