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 xco-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.
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 |
题意:扫描线求重复K次的面积。
思路:没什么好说的,和普通的扫描线差不多。
AC代码如下:
#include<cstdio>
#include<cstring>
#include<map>
#include<algorithm>
using namespace std;
typedef long long ll;
map<int,int> match;
int n,n2,m,K,px[60010];
struct node1
{
int x1,x2,y1,y2;
}area[30010];
struct node2
{
int l,r,h,f;
}line[60010];
bool cmp(node2 a,node2 b)
{
return a.h<b.h;
}
struct node3
{
int l,r,lazy,sum[11];
}tree[240010];
void build(int o,int l,int r)
{
tree[o].l=l;
tree[o].r=r;
memset(tree[o].sum,0,sizeof(tree[o].sum));
tree[o].sum[0]=px[r]-px[l-1];
tree[o].lazy=0;
if(l==r)
return;
int mi=(l+r)/2;
build(o<<1,l,mi);
build(o<<1|1,mi+1,r);
}
void solve(int o)
{
int i,p=tree[o].lazy;
if(tree[o].l==tree[o].r)
{
for(i=1;i<=p && i<=K;i++)
tree[o].sum[i]=tree[o].sum[0];
for(;i<=K;i++)
tree[o].sum[i]=0;
}
else
{
for(i=1;i<=p && i<=K;i++)
tree[o].sum[i]=tree[o].sum[0];
for(;i<=K;i++)
tree[o].sum[i]=tree[o<<1].sum[i-p]+tree[o<<1|1].sum[i-p];
}
}
void update(int o,int l,int r,int f)
{
if(tree[o].l==l && tree[o].r==r && tree[o].lazy+f>=0)
{
tree[o].lazy+=f;
solve(o);
return;
}
int mi=(tree[o].l+tree[o].r)/2;
if(r<=mi)
update(o<<1,l,r,f);
else if(l>mi)
update(o<<1|1,l,r,f);
else
{
update(o<<1,l,mi,f);
update(o<<1|1,mi+1,r,f);
}
solve(o);
}
int main()
{
int T,t,i,j,k;
ll ans;
scanf("%d",&T);
for(t=1;t<=T;t++)
{
scanf("%d%d",&n,&K);
n2=n*2;
for(i=1;i<=n;i++)
{
scanf("%d%d%d%d",&area[i].x1,&area[i].y1,&area[i].x2,&area[i].y2);
area[i].x1--;area[i].y1--;
}
for(i=1;i<=n;i++)
{
px[i*2-1]=area[i].x1;
px[i*2]=area[i].x2;
}
sort(px+1,px+1+n2);
px[0]=px[1]-1;
m=-1;match.clear();
for(i=1;i<=n2;i++)
if(px[i]!=px[i-1])
{
px[++m]=px[i];
match[px[i]]=m;
}
for(i=1;i<=n;i++)
{
area[i].x1=match[area[i].x1];area[i].x2=match[area[i].x2];
line[i*2-1].l=area[i].x1+1;line[i*2-1].r=area[i].x2;line[i*2-1].h=area[i].y2;line[i*2-1].f=-1;
line[i*2].l=area[i].x1+1;line[i*2].r=area[i].x2;line[i*2].h=area[i].y1;line[i*2].f=1;
}
sort(line+1,line+1+n2,cmp);
build(1,1,m);
ans=0;
for(i=1;i<n2;i++)
{
update(1,line[i].l,line[i].r,line[i].f);
ans+=(ll)tree[1].sum[K]*(line[i+1].h-line[i].h);
}
printf("Case %d: %lld\n",t,ans);
}
}