HDU 4325&& nyoj 600 Flowers【线段树+坐标离散化】
2015-12-04 22:12
393 查看
Flowers
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2658 Accepted Submission(s): 1308
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
2
1 1
5 10
4
2 3
1 4
4 8
1
4
6
[align=left]Sample Output[/align]
Case #1:
0
Case #2:
1
2
1
坐标离散化,区间延迟更新,自己掌握的还不行,继续努力吧!
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define maxn 100005
using namespace std;
int sumtree[maxn*4],add[maxn*4],li[maxn],ri[maxn],site[maxn*2],q[maxn];
int bin(int len,int x)//二分查找
{
int l=0,r=len-1,mid;
while(l<=r)
{
mid=(l+r)>>1;
if(site[mid]==x)
{
return mid;
}
if(x>site[mid])
{
l=mid+1;
}
else
{
r=mid-1;
}
}
}
void pushdown(int rt)
{
if(add[rt])
{
int tp=rt<<1;
add[tp]+=add[rt];add[tp|1]+=add[rt];
sumtree[tp]+=add[rt];sumtree[tp|1]+=add[rt];
add[rt]=0;
}
}
void update(int rt,int l,int r,int a,int b)
{
if(l>=a&&r<=b)
{
++add[rt];
++sumtree[rt];
return;
}
pushdown(rt);
int mid=(l+r)>>1,tp=rt<<1;
if(mid>=a)
{
update(tp,l,mid,a,b);
}
if(mid<b)
{
update(tp|1,mid+1,r,a,b);
}
sumtree[rt]=sumtree[tp]+sumtree[tp|1];
}
int find(int rt,int l,int r,int p)
{
if(l==r)
{
return sumtree[rt];
}
int mid=(l+r)>>1,tp=rt<<1;
pushdown(rt);
if(mid>=p)
{
return find(tp,l,mid,p);
}
else
{
return find(tp|1,mid+1,r,p);
}
}
int main()
{
int t;
// freopen("shuju.txt","r",stdin);
scanf("%d",&t);
for(int kase=1;kase<=t;++kase)
{
printf("Case #%d:\n",kase);
memset(add,0,sizeof(add));
memset(sumtree,0,sizeof(sumtree));
int n,m,k=0;
scanf("%d%d",&n,&m);
for(int i=0;i<n;++i)
{
scanf("%d%d",&li[i],&ri[i]);
site[k++]=li[i];site[k++]=ri[i];
}
for(int i=0;i<m;++i)
{
scanf("%d",&q[i]);
site[k++]=q[i];
}
sort(site,site+k);
k=unique(site,site+k)-site;
for(int i=0;i<n;++i)
{
int a=bin(k,li[i])+1,b=bin(k,ri[i])+1;
update(1,1,k,a,b);
}
for(int i=0;i<m;++i)
{
int p=bin(k,q[i])+1;
printf("%d\n",find(1,1,k,p));
}
}
return 0;
}
相关文章推荐
- Svn内外网切换技巧
- Windows 8安装Open edX
- 带你走进ajax(2)
- 初学Linux的一些理解及简单介绍
- 带你走进ajax(2)
- 第二个Sprint冲刺总结
- leetcode Lowest Common Ancestor of a Binary Tree
- web项目中路径的获取和遇到问题的解决方法
- 一起学libcef--搭建自己的libcef运行环境(Win32程序,错误C2220解决方案)
- 关于Android的EditText的部分属性
- lesson1 ODE的几何解法:方向场,积分曲线
- (NO.00004)iOS实现打砖块游戏(十五):导弹发射道具的实现(上)
- UML用例图中包含(include)、扩展(extend)和泛化(generalization)三种关系详解
- (NO.00004)iOS实现打砖块游戏(十五):导弹发射道具的实现(上)
- (NO.00004)iOS实现打砖块游戏(十五):导弹发射道具的实现(上)
- 一起学libcef--搭建自己的libcef运行环境(Win32程序,错误C2220解决方案)
- 深入浅出RxJava(基础篇)
- mac办公 xcode编程 提高生产力工具
- 猎人与猎狗的故事
- 剑指offer11 二进制中1的个数