hdu4325 Flowers
in the garden, so he wants you to help him.
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.
Sample outputs are available for more details.
1 1
5 10
4
2 3
1 4
4 8
1
4
6
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的更多相关文章
- CF451E Devu and Flowers (隔板法 容斥原理 Lucas定理 求逆元)
Codeforces Round #258 (Div. 2) Devu and Flowers E. Devu and Flowers time limit per test 4 seconds me ...
- poj 3262 Protecting the Flowers
http://poj.org/problem?id=3262 Protecting the Flowers Time Limit: 2000MS Memory Limit: 65536K Tota ...
- 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 ...
- poj1157LITTLE SHOP OF FLOWERS
Description You want to arrange the window of your flower shop in a most pleasant way. You have F bu ...
- CF459B Pashmak and Flowers (水
Pashmak and Flowers Codeforces Round #261 (Div. 2) B. Pashmak and Flowers time limit per test 1 seco ...
- HDU4325 树状数组
Flowers Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- 线段树或树状数组---Flowers
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=4325 Description As is known to all, the blooming tim ...
- sgu 104 Little shop of flowers 解题报告及测试数据
104. Little shop of flowers time limit per test: 0.25 sec. memory limit per test: 4096 KB 问题: 你想要将你的 ...
- 【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 ...
随机推荐
- oracle坚决不挂01(表,索引,视图的创建,修改,删除,查询)
考试快来了,来篇oracle干货,复习一下(挣扎一下) 废话不多说,开始写! 这篇是数据库对象的有关操作的总结! 数据库对象有熟悉的表,视图,索引,序列,同义词等(这个oracle东西真不少,小声bb ...
- kubernets集群的安全防护(下)
一 集群角色以及集群角色绑定 1.1 前面我们提到过角色以及角色绑定,那么现在为什么会出现集群级别的角色以及角色绑定,作用有如下所示 我们如果需要在所有的命名的空间创建某个角色或者角色绑定的时候 ...
- go语言循环变量
阅读go语言圣经第五章第六节介绍到了捕获迭代变量 package main import ( "fmt" ) func main() { var lis []func() for ...
- 与图论的邂逅05:最近公共祖先LCA
什么是LCA? 祖先链 对于一棵树T,若它的根节点是r,对于任意一个树上的节点x,从r走到x的路径是唯一的(显然),那么这条路径上的点都是并且只有这些点是x的祖先.这些点组成的链(或者说路径)就是x的 ...
- Java集合List-差集、并集、交集
Java集合List的差集.并集.交集 转载于:https://www.cnblogs.com/qlqwjy/p/9812919.html 一.List的差集 @Test public void te ...
- .NET Core使用Source Link提高源代码调试体验和生产效率
前言: 在我们日常开发过程中常常会使用到很多其他封装好的第三方中间件(NuGet依赖项).类库或者是.NET框架中自带的库.但是当你想要对这些类库的方法设置断点调试,然后发现无法F11(逐语句)调试进 ...
- 从软件(Java/hotspot/Linux)到硬件(硬件架构)分析互斥操作的本质
先上结论: 一切互斥操作的依赖是 自旋锁(spin_lock),互斥量(semaphore)等其他需要队列的实现均需要自选锁保证临界区互斥访问. 而自旋锁需要xcmpchg等类似的可提供CAS操作的硬 ...
- 关掉IE提示“当前安全设置会使计算机有风险”
我们先来看一下IE浏览器出现的提示窗口,该窗口位于最顶端,不点击设置的话,无法进行下一步的操作. 这时我们点击开始按钮 ,在弹出菜单中选择"运行"菜单项. 在打开的Windows运 ...
- 基于Vue的npm组件库
前言(*❦ω❦) 思维导图可能有点高糊,有点太大了,项目和导图文件放到github或giteee上,这个思维导图也是我文章的架构,思维导图是用FeHelper插件生成的,这个是一款开源chrome插件 ...
- Mybatis参数预编译
Mybatis参数预编译 一.数据库预编译介绍 1.数据库SQL语句编译特性: 数据库接受到sql语句之后,需要词法和语义解析,优化sql语句,制定执行计划.这需要花费一些时间.但是很多情况,我们的一 ...