hdoj 4325 Flowers 【线段树||二分】

9 篇文章 0 订阅
6 篇文章 0 订阅


Flowers

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2597    Accepted Submission(s): 1278


Problem Description
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.
 

Input
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 S i and T i (1 <= S i <= T i <= 10^9), means i-th flower will be blooming at time [S i, T i].
In the next M lines, each line contains an integer T i, means the time of i-th query.
 

Output
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.
 

Sample Input
  
  
2 1 1 5 10 4 2 3 1 4 4 8 1 4 6
 

Sample Output
  
  
Case #1: 0 Case #2: 1 2 1
分析:
这道题本来是用线段树写的,但是这道题杭电的数据有点弱,1e9竟然没有超时。
第二种方法是看的男神的,用二分写的很巧妙,佩服佩服。
代码1【二分】:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<stack>
#include<cmath>
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn =1e5+100;
#define mem(x,y) memset(x,y,sizeof(x));
int s[maxn],e[maxn];
int main()
{
   int t;
   int n,m,cnt=0;
   scanf("%d",&t);
   while(t--)
   {
       scanf("%d%d",&n,&m);
       for(int i=0;i<n;i++)
        scanf("%d%d",&s[i],&e[i]);
       sort(s,s+n);
       sort(e,e+n);
//       for(int i=0;i<n;i++)
//           printf("%d ",s[i]);
//           puts("");
//           for(int i=0;i<n;i++)
//           printf("%d ",e[i]);
//           puts(" ");

       int q;
       printf("Case #%d:\n",++cnt);
       while(m--)
       {
           scanf("%d",&q);
           int x=upper_bound(s,s+n,q)-s;
           int y=lower_bound(e,e+n,q)-e;
           printf("%d\n",x-y);
       }
   }
   system("pause");
    return 0;
代码2:【线段树】

<pre class="cpp" name="code">#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;


const int maxn=100001;
struct node{
    int l;
    int r;
    int val;
}t[maxn<<2];

void build(int l,int r,int k)
{
    t[k].l=l;
    t[k].r=r;
    t[k].val=0;
    if(l==r)
        return;
    int mid=(l+r)>>1;
    build(l,mid,k<<1);
    build(mid+1,r,k<<1|1);
}

void update(int l,int r,int k)
{
    if(t[k].l==l&&t[k].r==r)
    {
        t[k].val++;
        return ;
    }
    int mid=(t[k].l+t[k].r)>>1;
    if(l>mid)
        update(l,r,k<<1|1);
    else if(r<=mid)
        update(l,r,k<<1);
    else{
        update(l,mid,k<<1);
        update(mid+1,r,k<<1|1);
    }
}

int query(int l,int r,int k)
{
    if(t[k].l==l&&t[k].r==l)
        return t[k].val;
    //if(t[k].l==t[k].r)
        //return 0;
    int mid=(t[k].l+t[k].r)>>1;
    if(l>mid)
        return query(l,r,k<<1|1)+t[k].val;
    else if(r<=mid)
        return query(l,r,k<<1)+t[k].val;
    else
        return query(l,mid,k<<1)+query(mid+1,r,k<<1|1);
}

int main()
{
    int T;
    int n,m;
    int cnt=0;
    scanf("%d",&T);
       while(T--)
       {
           int a,b,c;
            build(1,maxn,1);
            scanf("%d%d",&n,&m);
            while(n--)
            {
                scanf("%d%d",&a,&b);
                update(a,b,1);
            }
            printf("Case #%d:\n",++cnt);
            while(m--)
            {
                scanf("%d",&c);
                printf("%d\n",query(c,c,1));
            }
       }
    return 0;
}


 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值