Weird Advertisement (线段树+扫描线)

2DPlaneLand is a land just like a huge 2D plane. The range of X axis is 0 to 109 and the range of
Y axis is also 0 to 109
. People built houses only in integer co-ordinates and there is exactly one house
in each integer co-ordinate.
Now UseAndSmile Soap Company is launching a new soap. That’s why they want to advertise
this product as much as possible. So, they selected n persons for this task. Each person will be given
a rectangular region. He will advertise the product to all the houses that lie in his region. Each
rectangular region is identified by 4 integers x1, y1, x2 and y2. That means this person will advertise
in all the houses whose x co-ordinate is between x1 and x2 (inclusive) and y co-ordinate is between y1
and y2 (inclusive).
Now after a while they realized that some houses are being advertised by more than one person.
So, they want to find the number of houses that are advertised by at least k persons. Since you are
one of the best programmers in the city; they asked you to solve this problem.
Input
Input starts with an integer T (≤ 13), denoting the number of test cases.
Each case starts with a line containing two integers n (1 ≤ n ≤ 30000), k (1 ≤ k ≤ 10). Each of the
next n lines will contain 4 integers x1, y1, x2, y2 (0 ≤ x1, y1, x2, y2 ≤ 109
, x1 < x2, y1 < y2) denoting a
rectangular region for a person.
Output
For each case, print the case number and the total number of houses that are advertised by at least k
people.
Renat Mullakhanov (rem. See http://www.topcoder.com/tc?module=MemberProfile.cr=8394868),
one of the most talented programmers in the world, passed away on March 11, 2011. This is very
sad news for all of us. His team went to ACM ICPC World Finals - 2004, placed 4th and won gold
medals. He really was a great programmer. May he rest in peace. This problem is dedicated to him.
Sample Input
2
2 1
0 0 4 4
1 1 2 5
2 2
0 0 4 4
1 1 2 5
Sample Output
Case 1: 27
Case 2: 8

题目大概:

有t组样例,每组样例,给出n个矩形,k。求出,被k个矩形覆盖的点的数量。

思路:

这类题,最简单的题是求矩形面积并,直接求覆盖面积。这个题有些地方不一样。一是求点的数量,那么当k个覆盖的矩形是一条线的话,面积是0,但点的数量不是0。所以我们可以把给的矩形扩大一下,求的点的数量就是求面积。二是求k覆盖的面积。原来用一颗线段树求面积并就行了。这里需要建立k颗线段树,分别求1-k的覆盖面积。除了这两点,这个题和普通的求矩形面积并差不多,都是扫描线,然后去重,枚举矩形的边。计算面积。

代码:

#include <bits/stdc++.h>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
using namespace std;
//
const int maxn=3e4+10;
LL sum[maxn<<3][20];
LL c[maxn<<3];
int b[maxn<<1];
LL k;
struct poin
{
    int l,r,h;
    int s;
    bool operator<(poin e)const
    {
        return h<e.h;
    }
}st[maxn<<1];
void build(int l,int r,int rt)
{
    sum[rt][0]=b[r+1]-b[l];
    if (l==r) return ;
    int m=(l+r)/2;
    build(lson);
    build(rson);
}
void pushup(int rt ,int l,int r)
{
    memset(sum[rt],0,sizeof(sum[rt]));
    if(l==r)
    {
        int tem=min(k,c[rt]);
        sum[rt][tem]=b[r+1]-b[l];
    }
    else
    {
        for(int i=0;i<=k;i++)
        {
            int tem=min(k,i+c[rt]);
            sum[rt][tem]+=sum[rt<<1][i]+sum[rt<<1|1][i];
        }
    }
}
void update(int L,int R,int w,int l,int r,int rt)
{
    if(L<=l&&r<=R)
    {
        c[rt]+=w;
        pushup(rt,l,r);
        return;
    }
    int m=(l+r)>>1;
    if(L<=m)update(L,R,w,lson);
    if(R>m)update(L,R,w,rson);
    pushup(rt,l,r);
}

int main()
{
    int t;
    int tt=1;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        int x1,x2,y1,y2;
        scanf("%d%lld",&n,&k);
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            y2++;x2++;
            b[++ans]=x1;
            st[ans].l=x1;
            st[ans].r=x2;
            st[ans].h=y1;
            st[ans].s=1;
            b[++ans]=x2;
            st[ans].l=x1;
            st[ans].r=x2;
            st[ans].h=y2;
            st[ans].s=-1;
        }
        sort(b+1,b+1+ans);
        sort(st+1,st+1+ans);
        int ans_1=unique(b+1,b+1+ans)-b-1;
        memset(c,0,sizeof(c));
        memset(sum,0,sizeof(sum));
        build(1,ans_1,1);
        LL ret=0;
        for(int i=1;i<ans;i++)
        {
            int l=lower_bound(b+1,b+1+ans_1,st[i].l)-b;
            int r=lower_bound(b+1,b+1+ans_1,st[i].r)-b-1;
            update(l,r,st[i].s,1,ans_1,1);
            ret+=sum[1][k]*(st[i+1].h-st[i].h);
        }
        printf("Case %d: %lld\n",tt++,ret);
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值