uva11983 - Weird Advertisement 矩形面积并 重叠大于等于K次的面积

G

Weird Advertisement

Renat Mullakhanov (rem), oneof 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 herest in peace. This problem is dedicated to him.

2DPlaneLand is a land just like a huge 2Dplane. The range of X axis is 0 to 109 and therange of Y axis is also 0 to 109. People builthouses only in integer co-ordinates and there is exactly one house in eachinteger 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, theyselected n persons for this task. Each person will be given arectangular region. He will advertise the product to all the houses that lie inhis region. Each rectangular region is identified by 4 integers x1,y1, x2 and y2. That means thisperson will advertise in all the houses whose x co-ordinate is between x1and x2 (inclusive) and y co-ordinate is between y1and y2 (inclusive).

Now after a while they realized that some houses are being advertisedby more than one person. So, they want to find the number of houses that areadvertised by at least k persons. Since you are one of the bestprogrammers 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 nlines 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 ofhouses that are advertised by at least k people.

Sample Input

Output for Sample Input

2
2 1
0 0 4 4
1 1 2 5
2 2
0 0 4 4
1 1 2 5

Case 1: 27

Case 2: 8

 

  covered[o]代表有多少条线段完全覆盖区间,sum[o][i]代表有多少区间覆盖次数大于等于i的线段长度,矩形面积并问题中不需要pushdown,因为最后算的时候只用的到sum[1]。

#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<algorithm>
#define INF 0x3f3f3f3f
#define eps 1e-9
#define MAXN 60010
#define MAXM 2000010
#define MAXNODE MAXN*8
#define MOD 999983
typedef long long LL;
using namespace std;
int T,N,K,M,covered[MAXNODE],sum[MAXNODE][12],x[MAXN];
struct Line{
    int x1,x2,y,cover;
    bool operator < (const Line& a) const{
        return y<a.y;
    }
}line[MAXN];
void add_line(int x1,int y1,int x2,int y2){
    line[M].x1=x1;
    line[M].x2=x2;
    line[M].y=y1;
    line[M++].cover=1;
    line[M].x1=x1;
    line[M].x2=x2;
    line[M].y=y2;
    line[M++].cover=-1;
}
void maintain(int o,int L,int R){
    if(L>=R){
        for(int i=1;i<=K;i++){
            if(covered[o]>=i) sum[o][i]=x[R]-x[L];
            else sum[o][i]=0;
        }
        return;
    }
    for(int i=1;i<=K;i++){
        if(covered[o]>=i) sum[o][i]=x[R]-x[L];
        else sum[o][i]=sum[o<<1][i-covered[o]]+sum[o<<1|1][i-covered[o]];
    }
}
void update(int o,int L,int R,int ql,int qr,int cover){
    if(ql<=L&&qr>=R){
        covered[o]+=cover;
        maintain(o,L,R);
        return;
    }
    int mid=(L+R)>>1;
    if(ql<mid) update(o<<1,L,mid,ql,qr,cover);
    if(qr>mid) update(o<<1|1,mid,R,ql,qr,cover);
    maintain(o,L,R);
}
int main(){
    freopen("in.txt","r",stdin);
    int cas=0;
    scanf("%d",&T);
    while(T--){
        memset(sum,0,sizeof(sum));
        memset(covered,0,sizeof(covered));
        M=0;
        int nx=1;
        scanf("%d%d",&N,&K);
        while(N--){
            int x1,y1,x2,y2;
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            x2++;
            y2++;
            add_line(x1,y1,x2,y2);
            x[nx++]=x1;
            x[nx++]=x2;
        }
        sort(line,line+M);
        sort(x+1,x+nx);
        nx=unique(x+1,x+nx)-x-1;
        LL ans=0;
        for(int i=0;i<M-1;i++){
            int ql=lower_bound(x+1,x+nx+1,line[i].x1)-x,qr=lower_bound(x+1,x+nx+1,line[i].x2)-x;
            update(1,1,nx,ql,qr,line[i].cover);
            ans+=(LL)sum[1][K]*(line[i+1].y-line[i].y);
        }
        printf("Case %d: %lld\n",++cas,ans);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值