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 ...
随机推荐
- maven3常用命令\创建Project
转自 http://blog.csdn.net/edward0830ly/article/details/8748986 ------------------------------maven3常用命 ...
- POJ 3904 Sky Code
题意:给定n个数ai, ai <= 10000, n <= 10000, 从中选出4个数要求gcd为1,这样的集合有多少个? 分析:首先总共集合nCr(n, 4) = n*(n-1)*(n ...
- Servlet中乱码问题
页面编码方式为utf-8 当使用post传值时 request.setContentType("utf-8"); 当使用get传值时 String str = request.ge ...
- java,<E>什么意思?
泛型 就是指任意类型 比如 HashMap<K,V> 你用的时候 你可以将K,V 设置成任意类 HashMap<String,Integer> K,V 和那个E 一样 Hash ...
- Fibonacci sequence 求余数
#include <iostream> using namespace std; int f(int n); int main() { int n; cin>>n; doubl ...
- POJ2632——Crashing Robots
Crashing Robots DescriptionIn a modernized warehouse, robots are used to fetch the goods. Careful pl ...
- Permutation Test 置换检验(转)
Permutation Test 置换检验 显著性检验通常可以告诉我们一个观测值是否是有效的,例如检测两组样本均值差异的假设检验可以告诉我们这两组样本的均值是否相等(或者那个均值更大).我们在实验中经 ...
- socket.io 使用
socket.io是一个以实现跨浏览器.跨平台的实时应用为目的的项目.针对不同的浏览器版本或者不同客户端会做自动降级处理,选择合适的实现方式(websocket, long pull..),隐藏实现只 ...
- centos下apache+mysql+php安装及配置
今天难得休闲,自从加盟当前公司以来好像就基本没有写过博客了.难得闲下来和前同事聊天,他们几个人合伙买了VPS在用.这对我们搞WEB开发的童鞋来说是非常重要的,我来这家公司有许久了,但是竟然到现在连一台 ...
- How does it work in C#? - Part 3 (C# LINQ in detail)
http://www.codeproject.com/Articles/383749/How-does-it-work-in-Csharp-Part-Csharp-LINQ-in-d