hdoj 4325 Flowers【线段树+离散化】
Flowers
Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2616    Accepted Submission(s):
1287
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.
<= 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.
print M lines. Each line contains an integer, meaning the number of blooming
flowers.
Sample outputs are available for more details.
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define LL long long
#define MAX 100100
using namespace std;
int s[MAX],e[MAX],q[MAX];
int rec[MAX];//记录所有值排序后的下标
int add[MAX<<2];
int sum[MAX<<2];
int le[MAX],ri[MAX];
void pushup(int o)
{
sum[o]=sum[o<<1]+sum[o<<1|1];
}
void pushdown(int o,int m)
{
if(add[o])
{
add[o<<1]+=add[o];
add[o<<1|1]+=add[o];
sum[o<<1]+=add[o]*(m-(m>>1));
sum[o<<1|1]+=add[o]*(m>>1);
add[o]=0;
}
}
void gettree(int o,int l,int r)
{
add[o]=0;
if(l==r)
{
sum[o]=0;
return ;
}
int mid=(l+r)>>1;
gettree(o<<1,l,mid);
gettree(o<<1|1,mid+1,r);
pushup(o);
}
void update(int o,int l,int r,int L,int R,int val)
{
if(L<=l&&R>=r)
{
add[o]+=val;
sum[o]+=val*(r-l+1);
return ;
}
pushdown(o,r-l+1);
int mid=(l+r)>>1;
if(L<=mid)
update(o<<1,l,mid,L,R,val);
if(R>mid)
update(o<<1|1,mid+1,r,L,R,val);
pushup(o);
}
int find(int o,int l,int r,int pos)
{
if(l==r)
{
return sum[o];
}
pushdown(o,r-l+1);
int ans=0;
int mid=(l+r)>>1;
if(pos<=mid)
ans=find(o<<1,l,mid,pos);
else
ans=find(o<<1|1,mid+1,r,pos);
return ans;
}
int query(int l,int r,int pos)//查找输入当前值,在树中对应的位置
{
while(r>=l)
{
int mid=(l+r)>>1;
if(rec[mid]==pos)
return mid;
else if(rec[mid]>pos)
r=mid-1;
else
l=mid+1;
}
return -1;
}
int main()
{
int t,n,m,k,i;
scanf("%d",&t);
k=1;
int maxx;
while(t--)
{
scanf("%d%d",&n,&m);
int p=1;
for(i=0;i<n;i++)
{
scanf("%d%d",&s[i],&e[i]);
rec[p++]=s[i];
rec[p++]=e[i];
}
for(i=0;i<m;i++)
{
scanf("%d",&q[i]);
rec[p++]=q[i];
}
sort(rec+1,rec+p);//
int R=2;
for(i=2;i<p;i++)//去除数组中重复的点
{
if(rec[i]!=rec[i-1])
rec[R++]=rec[i];
}
sort(rec+1,rec+R);
gettree(1,1,R-1);//对下标建树
for(int i=0;i<n;i++)
{
int x=query(1,R-1,s[i]);
int y=query(1,R-1,e[i]);
update(1,1,R-1,x,y,1);
}
printf("Case #%d:\n",k++);
for(i=0;i<m;i++)
{
int x=query(1,R-1,q[i]);
printf("%d\n",find(1,1,R-1,x));
}
}
}
hdoj 4325 Flowers【线段树+离散化】的更多相关文章
- hdoj 4325 Flowers 线段树+离散化
		
hdoj 4325 Flowers 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4325 思路: 直接线段树,按照花的开放区间的大小建树,要注意虽然 ...
 - POJ 2528 Mayor's posters(线段树+离散化)
		
Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...
 - poj 2528 Mayor's posters(线段树+离散化)
		
/* poj 2528 Mayor's posters 线段树 + 离散化 离散化的理解: 给你一系列的正整数, 例如 1, 4 , 100, 1000000000, 如果利用线段树求解的话,很明显 ...
 - [poj2528] Mayor's posters (线段树+离散化)
		
线段树 + 离散化 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayor ...
 - [UESTC1059]秋实大哥与小朋友(线段树, 离散化)
		
题目链接:http://acm.uestc.edu.cn/#/problem/show/1059 普通线段树+离散化,关键是……离散化后建树和查询都要按照基本法!!!RE了不知道多少次………………我真 ...
 - poj 2528 Mayor's posters 线段树+离散化技巧
		
poj 2528 Mayor's posters 题目链接: http://poj.org/problem?id=2528 思路: 线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来) ...
 - BZOJ_4653_[Noi2016]区间_线段树+离散化+双指针
		
BZOJ_4653_[Noi2016]区间_线段树+离散化+双指针 Description 在数轴上有 n个闭区间 [l1,r1],[l2,r2],...,[ln,rn].现在要从中选出 m 个区间, ...
 - D - Mayor's posters(线段树+离散化)
		
题目: The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campai ...
 - 主席树||可持久化线段树+离散化 || 莫队+分块 ||BZOJ 3585: mex || Luogu P4137 Rmq Problem / mex
		
题面:Rmq Problem / mex 题解: 先离散化,然后插一堆空白,大体就是如果(对于以a.data<b.data排序后的A)A[i-1].data+1!=A[i].data,则插一个空 ...
 - HDU5124:lines(线段树+离散化)或(离散化思想)
		
http://acm.hdu.edu.cn/showproblem.php?pid=5124 Problem Description John has several lines. The lines ...
 
随机推荐
- 跨平台查询文件时间,如果超过7天,删除该文件(windows和linxu测试过)
			
windows调用的是_stat函数,linux调用的是stat函数. #include <time.h> #include <sys/types.h> #include &l ...
 - 思科模拟器软件教程---教你如何划分Vlan
			
方法/步骤 1.打开Cisco Packet Tracer,点击[交换机],选择第三个图标2960交换机,按住鼠标左键拖动到工作区.这里有很多类型的交换机,但是我们比较常用的是这个. 2.我们选择[终 ...
 - Android:属性scaleType与图片的显示
			
ImageView是Android中的基础图片显示控件,该控件有个重要的属性是ScaleType,该属性用以表示显示图片的方式, 共有8种取值 matrix 用矩阵来绘制(从左上角起始的矩阵区域) f ...
 - nchar 和 nvarchar
			
字符数据类型(nchar 长度固定,nvarchar 长度可变)和 Unicode 数据使用 UNICODE UCS-2 字符集. nchar [ ( n ) ] n 个字符的固定长度的 Unicod ...
 - Spring中的实例生成方式及其生命周期
			
三种实例化bean的方式1.使用类构造器实例化 <!-- 使用类构造器实例化,class属性表示要使用的类的全限定名 --> <bean id="userDao1" ...
 - 先说一下JS的获取方法,其要比JQUERY的方法麻烦很多,后面以JQUERY的方法作对比。
			
先说一下JS的获取方法,其要比JQUERY的方法麻烦很多,后面以JQUERY的方法作对比. JS的方法会比JQUERY麻烦很多,主要则是因为FF浏览器,FF浏览器会把你的换行也当最DOM元素 复制代码 ...
 - 如何在给快满的Linux分区"无伤"扩容
			
1. 首先在虚拟机设置里面设置磁盘的扩展,前提条件是该虚拟机没有快照. 2. 在虚拟机设置好以后,需要开机在系统里扩容磁盘 3. 使用 # fdisk /dev/sda 扩展磁盘,具体操作使用 m 选 ...
 - FastScroll(1)ListView打开FastScroll及自定义它的样式
			
打开 FastScroll 方式 android:fastScrollEnabled="true" 它是AbsListView的属性. <?xml version=" ...
 - 【HDOJ】4345 Permutation
			
即求P1^n1+P2^n2 + ... + Pk^nk <= n,其中Pk为素数的所有可能组合.思路是DP.1~1000的素数就不到200个.dp[i][j]表示上式和不超过且当前最小素数为P[ ...
 - IPC:Sockets
			
Please refer to http://www.cs.cf.ac.uk/Dave/C/node28.html.