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. Registry Connect failed,Windows服务诊断

    Message:Connection failed for 192.168.32.38_e-futrueserer. Details:Windows Registry Datasource: Regi ...

  2. c语言实现皇帝翻牌游戏

    一个综合的游戏,对所学的知识进行整合!融会贯通! #include <stdio.h> #include <stdlib.h> #include <string.h> ...

  3. 【IOI 2011】Race

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2599 [算法] 点分治 [代码] #include<bits/stdc++.h ...

  4. Microsoft Edge 首个 Chromium 内核版释出

    翻译功能释出 navigator.userAgent"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, ...

  5. <video> controlsList

    Audio/Video Updates in Chrome 58 <video controls controlsList="nofullscreen nodownload norem ...

  6. Codeforces Round #283 (Div. 2) A

    解题思路:给出一个递增数列,a1,a2,a3,-----,an.问任意去掉a2到a3之间任意一个数之后, 因为注意到该数列是单调递增的,所以可以先求出原数列相邻两项的差值的最大值max, 得到新的一个 ...

  7. 3ds Max制作欧式风格的墙壁路灯效果

    在这篇文章中,我将解释我创建我的形象元宵节的步骤.我只是在寻找一个很好的参考图像在互联网上的东西,我觉得我想要的模型,这个形象.我发现了一个巨大的灯笼形象,但在白天的图片拍摄.我想改变我的形象和显示的 ...

  8. 路飞学城Python-Day40(第四模块复习题)

    数据库 一.简答题 1.说说你所知道的MySQL数据库存储引擎,InnoDB存储引擎和MyISM存储引擎的区别? 1.InnoDB存储引擎(MySQL默认存储引擎),支持事务,其设计目标主要面向联机事 ...

  9. Vue学习之路第七篇:跑马灯项目实现

    前面六篇讲解了Vue的一些基础知识,正所谓:学以致用,今天我们将用前六篇的基础知识,来实现类似跑马灯的项目. 学前准备: 需要掌握定时器的两个函数:setInterval和clearInterval以 ...

  10. [USACO18JAN] Cow at Large G (dfs)

    题目大意:有一只狐狸从给定的S点开始逃跑(出发),向叶节点移动以逃离这棵树,叶节点可能出现农民去抓捕狐狸,当农民和狐狸出现在同一个节点的时候,狐狸会被抓住,农民和狐狸移动速度相同,求抓捕狐狸所需要的最 ...