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

这题需要用到离散化,因为10^9建立线段树会超时,而给的数字总共只有2*n+m,所以可以先离散化,(这里注意因为最后询问的时候所查询的时间可能没有在前n对出现,如果只对n对数字离散化,后面询问的时候会出错),我的离散化是先构造一个结构体储存输入的2*n+m的数的数字num和编号id,然后对关键词num排序,去重后用map<int,int>匹配编号,匹配完后再对id排序复原。离散化的另一个方法是定义一个结构体记录数的id(编号),num(数的大小),先根据num排序,然后依次赋值为i,最后再按id排序。

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
#define maxn 300006
int sum,pos[maxn];
struct node{
int l,r,sum;
}b[4*maxn]; struct edge{
int id,num;
}a[maxn]; bool cmp1(edge a,edge b){
return a.num<b.num;
}
bool cmp2(edge a,edge b){
return a.id<b.id;
} void build(int l,int r,int i)
{
int mid;
b[i].l=l;b[i].r=r;b[i].sum=0;
if(l==r)return;
mid=(l+r)/2;
build(l,mid,i*2);
build(mid+1,r,i*2+1);
} void update(int l,int r,int i)
{
int mid;
if(b[i].l==l && b[i].r==r){
b[i].sum++;return;
}
mid=(b[i].l+b[i].r)/2;
if(r<=mid)update(l,r,i*2);
else if(l>mid)update(l,r,i*2+1);
else {
update(l,mid,i*2);
update(mid+1,r,i*2+1);
}
} void question(int id,int i)
{
int mid;
if(b[i].l==b[i].r){
sum=b[i].sum;return;
}
b[i*2].sum+=b[i].sum;
b[i*2+1].sum+=b[i].sum;
b[i].sum=0;
mid=(b[i].l+b[i].r)/2;
if(id<=mid)question(id,i*2);
else question(id,i*2+1);
} int main()
{
int n,m,i,j,T,h,c,d,t;
map<int,int>hash;
scanf("%d",&T);
for(h=1;h<=T;h++){
printf("Case #%d:\n",h);
scanf("%d%d",&n,&m);
build(1,maxn,1);
for(i=1;i<=n;i++){
scanf("%d%d",&a[i].num,&a[i+n].num);
a[i].id=i;a[i+n].id=i+n;
}
for(i=1;i<=m;i++){
scanf("%d",&a[i+2*n].num);
a[i+2*n].id=i+2*n;
}
sort(a+1,a+2*n+m+1,cmp1);
hash[a[1].num]=1;t=1;
for(i=2;i<=2*n+m;i++){
if(a[i].num!=a[i-1].num){
t++;hash[a[i].num]=t;
}
}
sort(a+1,a+2*n+m+1,cmp2);
for(i=1;i<=n;i++){
c=hash[a[i].num];d=hash[a[i+n].num];
//printf("%d %d\n",c,d);
update(c,d,1);
} for(i=1;i<=m;i++){
c=hash[a[i+2*n].num];
sum=0;
question(c,1);
printf("%d\n",sum);
}
}
return 0;
}

hdu4325 Flowers的更多相关文章

  1. CF451E Devu and Flowers (隔板法 容斥原理 Lucas定理 求逆元)

    Codeforces Round #258 (Div. 2) Devu and Flowers E. Devu and Flowers time limit per test 4 seconds me ...

  2. poj 3262 Protecting the Flowers

    http://poj.org/problem?id=3262 Protecting the Flowers Time Limit: 2000MS   Memory Limit: 65536K Tota ...

  3. Codeforces Round #381 (Div. 2)B. Alyona and flowers(水题)

    B. Alyona and flowers Problem Description: Let's define a subarray as a segment of consecutive flowe ...

  4. poj1157LITTLE SHOP OF FLOWERS

    Description You want to arrange the window of your flower shop in a most pleasant way. You have F bu ...

  5. CF459B Pashmak and Flowers (水

    Pashmak and Flowers Codeforces Round #261 (Div. 2) B. Pashmak and Flowers time limit per test 1 seco ...

  6. HDU4325 树状数组

    Flowers Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  7. 线段树或树状数组---Flowers

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=4325 Description As is known to all, the blooming tim ...

  8. sgu 104 Little shop of flowers 解题报告及测试数据

    104. Little shop of flowers time limit per test: 0.25 sec. memory limit per test: 4096 KB 问题: 你想要将你的 ...

  9. 【CodeForces 621C】Wet Shark and Flowers

    题 There are n sharks who grow flowers for Wet Shark. They are all sitting around the table, such tha ...

随机推荐

  1. 【ORA】ORA-27090: Unable to reserve kernel resources for asynchronous disk I/O

    操作系统是CentOS 5.11 数据库 10.2.0.5.0 晚上查看数据库,发现数据库报错查看相关的trace文件内容如下: *** SERVICE NAME:(SYS$BACKGROUND) 2 ...

  2. ctfhub技能树—web前置技能—http协议—302跳转

    开启靶机 打开环境,查看显示 点击Give me Flag后发生跳转 根据题目提示为HTTP临时重定向 简单记录一下HTTP临时重定向是什么 HTTP重定向:服务器无法处理浏览器发送过来的请求(req ...

  3. 【译】Async/Await(三)——Aysnc/Await模式

    原文标题:Async/Await 原文链接:https://os.phil-opp.com/async-await/#multitasking 公众号: Rust 碎碎念 翻译 by: Praying ...

  4. mysql:如何解决数据修改冲突(事务+行级锁的实际运用)

    摘要:最近做一个接诊需求遇到一个问题,假设一个订单咨询超过3次就不能再接诊,但如果两个医生同时对该订单进行咨询,查数据库的时候查到的接诊次数都是2次,那两个医生都能接诊,所谓接诊可以理解为更新了接诊次 ...

  5. linux下删除文件夹及下面所有文件

    使用rm -rf 目录名字 命令即可 -r 就是向下递归,不管有多少级目录,一并删除-f 就是直接强行删除,不作任何提示的意思 rm 不带参数 只能删除文件 rm test.txt mkdir /us ...

  6. [Usaco2007 Dec]宝石手镯

    题目描述 贝茜在珠宝店闲逛时,买到了一个中意的手镯.很自然地,她想从她收集的 N(1 <= N <= 3,402)块宝石中选出最好的那些镶在手镯上.对于第i块宝石,它的重量为W_i(1 & ...

  7. 盼望着,盼望着。它终于来了!!!剪辑Windows PC测试版!

    盼望着,盼望着.它终于来了!!!剪辑Windows PC测试版! 喜欢短视频抖音的小伙伴们,是不是都对手机抖音剪映操作不是很满意.期待WINDOWS电脑版剪映的来临.在此之前只有MAC版本,不能满足广 ...

  8. 脑裂 CAP PAXOS 单元化 网络分区 最终一致性 BASE

    阿里技术专家甘盘:浅谈双十一背后的支付宝LDC架构和其CAP分析 https://mp.weixin.qq.com/s/Cnzz5riMc9RH19zdjToyDg 汤波(甘盘) 技术琐话 2020- ...

  9. Java面向对象(三)—— 继承

    标签: java 继承 抽象类 this super abstract 概述 多个类中存在相同的属性和行为的时候,将这些内容抽取到单独一个类中,那么多个类无需在定义这些属性和行为,只要继承那个类即可. ...

  10. SQL解析工具

    面对复杂的SQL可用这个SQL解析工具,分析出用到了哪些表哪些字段: http://107.170.101.241:8080/getTableColumn/