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 
#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>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



你可能感兴趣的:(线段树)