您的位置:首页 > 其它

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: