标签:
Time Limit: 20 Sec
Memory Limit: 256 MB
http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=18802
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 identied 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 nd 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=MemberProle.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
题意
给你n个矩形,然后问你整个平面上被覆盖k次及以上的面积总和是多少
题解:
扫描线,我们线段树记录一下sum[k]表示区间被覆盖k次的长度是多少
updata的时候,从儿子更新过来就好了
代码:
#include<iostream> #include<stdio.h> #include<cstring> #include<algorithm> #include<vector> #include<map> using namespace std; #define maxn 5000000 struct line { int x,y1,y2,flag; }; bool cmp(line A,line B) { return A.x<B.x; } int n,k; typedef long long SgTreeDataType; struct treenode { int L , R ; SgTreeDataType sum[12] , lazy; void init() { memset(sum,0,sizeof(sum)); } }; map<int,int> H; vector<int> Y; treenode tree[maxn]; line p[maxn]; int tot = 0; long long ans; inline void push_up(int o) { tree[o].init(); if(tree[o].L==tree[o].R) { int T = min(tree[o].lazy,k*1LL); tree[o].sum[T]=Y[tree[o].R]-Y[tree[o].L-1]; } else { for(int i=0;i<=k;i++) { int T = min(i+tree[o].lazy,k*1LL); tree[o].sum[T]+=tree[o*2].sum[i]+tree[o*2+1].sum[i]; } } } inline void build_tree(int L , int R , int o) { tree[o].L = L , tree[o].R = R, tree[o].lazy = 0; tree[o].init(); if (R > L) { int mid = (L+R) >> 1; build_tree(L,mid,o*2); build_tree(mid+1,R,o*2+1); } push_up(o); } inline void updata(int QL,int QR,SgTreeDataType v,int o) { int L = tree[o].L , R = tree[o].R; if (QL <= L && R <= QR) tree[o].lazy+=v; else { //push_down(o); int mid = (L+R)>>1; if (QL <= mid) updata(QL,QR,v,o*2); if (QR > mid) updata(QL,QR,v,o*2+1); } push_up(o); } int main() { int t;scanf("%d",&t); for(int i=1;i<=t;i++) { tot = 0;ans=0; memset(p,0,sizeof(p)); Y.clear();H.clear(); scanf("%d%d",&n,&k); for(int i=1;i<=n;i++) { int x1,y1,x2,y2; scanf("%d%d%d%d",&x1,&y1,&x2,&y2); p[tot].x = x1,p[tot].y1=y1,p[tot].y2=y2+1,p[tot].flag=1;tot++; p[tot].x = x2+1,p[tot].y1=y1,p[tot].y2=y2+1,p[tot].flag=-1;tot++; Y.push_back(y1); Y.push_back(y2+1); } sort(p,p+tot,cmp); sort(Y.begin(),Y.end()); Y.erase(unique(Y.begin(),Y.end()),Y.end()); for(int i=0;i<Y.size();i++) H[Y[i]]=i+1; build_tree(0,Y.size()+5,1); for(int i=0;i<tot-1;i++) { updata(H[p[i].y1],H[p[i].y2]-1,p[i].flag,1); ans+=tree[1].sum[k]*(p[i+1].x-p[i].x); //cout<<p[i].y1<<" "<<p[i].y2<<" "<<p[i].flag<<endl; //cout<<tree[1].sum[k]<<" "<<(p[i+1].x-p[i].x)<<endl; } printf("Case %d: %lld\n",i,ans); } }
uva 11983 Weird Advertisement 扫描线
标签:
原文地址:http://www.cnblogs.com/qscqesze/p/5004806.html