hdu 1828 Picture(線段樹,掃描線之周長並)
阿新 • • 發佈:2019-01-24
Picture
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4529 Accepted Submission(s): 2237
Problem Description A number of rectangular posters, photographs and other pictures of the same shape are pasted on a wall. Their sides are all vertical or horizontal. Each rectangle can be partially or totally covered by the others. The length of the boundary of the union of all rectangles is called the perimeter.
Write a program to calculate the perimeter. An example with 7 rectangles is shown in Figure 1.
The corresponding boundary is the whole set of line segments drawn in Figure 2.
The vertices of all rectangles have integer coordinates.
Input Your program is to read from standard input. The first line contains the number of rectangles pasted on the wall. In each of the subsequent lines, one can find the integer coordinates of the lower left vertex and the upper right vertex of each rectangle. The values of those coordinates are given as ordered pairs consisting of an x-coordinate followed by a y-coordinate.
0 <= number of rectangles < 5000
All coordinates are in the range [-10000,10000] and any existing rectangle has a positive area.
Please process to the end of file.
Output Your program is to write to standard output. The output must contain a single line with a non-negative integer which corresponds to the perimeter for the input rectangles.
Sample Input 7 -15 0 5 10 -5 8 20 25 15 -4 24 14 0 -6 16 4 2 15 10 22 30 10 36 20 34 0 40 16
Sample Output 228
Source 線段樹,掃描線之周長並。 有兩種解法:
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <stack> #include <map> #include <set> #include <vector> #include <queue> #define mem(p,k) memset(p,k,sizeof(p)); #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define inf 0x6fffffff #define LL long long #define MAXN 20000 using namespace std; int col[MAXN<<2]; int sum[MAXN<<2],X[MAXN],lbd[MAXN<<2],rbd[MAXN<<2],numseg[MAXN<<2]; struct Seg{ int l,r,h; int cur; Seg(){} Seg(int a,int b,int c,int d):l(a),r(b),h(c),cur(d){} bool operator <(const Seg &a)const{ if(a.h==h)return cur>a.cur; return h<a.h; } }ss[MAXN]; void pushup(int rt,int l,int r){ if(col[rt]){ sum[rt]=X[r+1]-X[l]; lbd[rt]=rbd[rt]=1; numseg[rt]=2; } else if(l==r)sum[rt]=lbd[rt]=rbd[rt]=numseg[rt]=0; else { lbd[rt]=lbd[rt<<1]; rbd[rt]=rbd[rt<<1|1]; sum[rt]=sum[rt<<1]+sum[rt<<1|1]; numseg[rt]=numseg[rt<<1]+numseg[rt<<1|1]; if(rbd[rt<<1]&&lbd[rt<<1|1])numseg[rt]-=2; } } void update(int L,int R,int c,int l, int r,int rt){ if(l>=L&&r<=R){ col[rt]+=c; pushup(rt,l,r); return ; } int m=(l+r)>>1; if(L<=m)update(L,R,c,lson); if(R>m) update(L,R,c,rson); pushup(rt,l,r); } int main(){ int n,k,pp=0; while(cin>>n){ int ans=0,la=0; k=0; mem(sum,0); mem(col,0); mem(numseg,0); for(int i=0;i<n;i++){ int a,b,c,d; scanf("%d%d%d%d",&a,&b,&c,&d); X[k]=a; ss[k++]=Seg(a,c,b,1); X[k]=c; ss[k++]=Seg(a,c,d,-1); } sort(ss,ss+k); sort(X,X+k); int kk=1; for(int i=1;i<k;i++){ if(X[i]!=X[i-1])X[kk++]=X[i]; } for(int i=0;i<k;i++){ int l=lower_bound(X,X+kk,ss[i].l)-X; int r=lower_bound(X,X+kk,ss[i].r)-X-1; if(l<=r)update(l,r,ss[i].cur,0,kk-1,1); //cout<<numseg[1]<<"::"<<sum[1]<<endl; ans+=numseg[1]*(ss[i+1].h-ss[i].h); ans+=abs(sum[1]-la); la=sum[1]; } printf("%d\n",ans); } return 0; }