• HDU 4325


    4325 Flowers

    题目描述:n种花的开放时间,m次询问;问你在询问的时间点,能看见几种花开;

    线段树+离散化,通过这道题了解到了low_bound,upper_bound,unique的用法。

    #include<bits/stdc++.h>
    using namespace std;
    #define maxx 100010
    struct node
    {
        int ll;
        int rr;
        int u;
    };
    node tree[maxx<<1];
    int st[maxx],ed[maxx],q[maxx],tt[maxx<<1],n,m;
    void build(int t,int l,int r)
    {
        tree[t].ll=l;
        tree[t].rr=r;
        tree[t].u=0;
        if(l==r)
        {
            return;
        }
        else
        {
            build(t<<1,l,(r+l)/2);
            build(t<<1|1,(r+l)/2+1,r);
        }
    }
    void upper(int t,int l,int r)
    {
        if(l<=tree[t].ll && r>=tree[t].rr)
        {
            tree[t].u++;
        }
        else if(l>tree[t].rr || r<tree[t].ll)
        {
            return;
        }
        else
        {
            tree[t<<1].u+=tree[t].u;
            tree[t<<1|1].u+=tree[t].u;
            tree[t].u=0;
            if(l<=tree[t<<1].rr ) upper(t<<1,l,r);
            if(r>=tree[t<<1|1].ll) upper(t<<1|1,l,r);
        }
    }
    int quer(int t,int l)
    {
        if(tree[t].rr==tree[t].ll)
        {
            return tree[t].u;
        }
        else
        {
            tree[t<<1].u+=tree[t].u;
            tree[t<<1|1].u+=tree[t].u;
            tree[t].u=0;
            if(l<=tree[t<<1].rr) return quer(t<<1,l);
            else return quer(t<<1|1,l);
        }
    }
    int main()
    {
        int t;
        scanf("%d",&t);
        int casee=1;
        while(t--)
        {
            scanf("%d%d",&n,&m);
            int cnt=0;
            for(int i=0; i<n; i++)
            {
                scanf("%d%d",&st[i],&ed[i]);
                tt[cnt++]=st[i];
                tt[cnt++]=ed[i];
            }
            for(int i=0; i<m; i++)
            {
                scanf("%d",&q[i]);
                tt[cnt++]=q[i];
            }
            sort(tt,tt+cnt);
            cnt=unique(tt,tt+cnt)-tt;
            build(1,1,cnt);
            for(int i=0; i<n; i++)
            {
                st[i]=lower_bound(tt,tt+cnt,st[i])-tt+1;
                ed[i]=lower_bound(tt,tt+cnt,ed[i])-tt+1;
                //cout<<st[i]<<' '<<ed[i]<<endl;
                upper(1,st[i],ed[i]);
            }
            printf("Case #%d:
    ",casee++);
            for(int i=0; i<m; i++)
            {
                q[i]=lower_bound(tt,tt+cnt,q[i])-tt+1;
                int ans=quer(1,q[i]);
                printf("%d
    ",ans);
            }
        }
        return 0;
    }
  • 相关阅读:
    javafx DragDropped file
    javafx style and cssFile
    javafx ComboBox Event and change cell color
    javafx clipboard
    javafx Cursor
    javafx DropShadow
    javafx checkbox
    javafx image button
    GNS3连接虚拟机
    cain使用教程
  • 原文地址:https://www.cnblogs.com/zzulipomelo/p/4977630.html
Copyright © 2020-2023  润新知