HDU 3624 City Planning (violence, can also scan the line)

Source: Internet
Author: User

City planningTime limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 503 Accepted Submission (s): 213


Problem Descriptionafter Many years, the buildings in HDU have become very old. It need to rebuild the buildings now. So Mr Dragon (the president of HDU's logistics department) Ask Mr Wan (a very famous engineer) for help.
Mr Wan only draw one building in a construction design drawings (all the buildings is rectangle and each edge of buildings ' is paraller or perpendicular to others buildings ' edge. and total draw n drawings (all the drawings has same width and length. And Bottomleft Point is (0, 0)). Due to possible overlap of conditions, so when they build a new building, they should to remove all the overlapping part O F it. And for each building, HDU has a jury evaluate the value per unit area. Now Mr. Dragon want to know what to arrange the order of build these buildings can make the highest value.
Inputthe first line of input is a number T which indicate the number of cases. (1 < T < 3000);
Each test case would begin with a and a single line containing a single integer n (where 1 <= n <= 20).
Next N line would contain five integers x1, y1, x2, y2, value. X1,y1 is Bottomleft point and X2,y2 are TopRight point, value is the value of the Buildings ' unit area. ((0 <= x1, y1, x2, y2 <= 10000) (X1 < x2, && y1 < y2) (1 <= value <= 22)
Outputfor each case. You just ouput the highest value on one line.
Sample Input
131 1 10 10 44 4 15 5 57 8 20 30 6

Sample Output
Case 1:2047Test Instructions: give you n rectangle each rectangle has its own Val, for Coincident area, Val large can be small cover, seek total Val maximum value . Ideas:I heard that the scan line, and then to learn the scan line, found in fact violence can also. Code:(violence)
#include <cstdio> #include <cstring> #include <algorithm> using namespace std;  typedef long Long LL;  const int N = 50;  int y[n], x[n], N, M;  ll Val[n][n];      struct Rect {int x1, y1, x2, y2, V;      BOOL operator< (const Rect &r) const{return v < R.V;   }} R[n];  int fid (int a[], int k) {return Lower_bound (a, A + M, K)-A;      } int main () {int T, x1, y1, x2, y2, cas = 0;      scanf ("%d", &t);          while (t--) {scanf ("%d", &n); for (int i = m = 0; i < n; ++i, M + = 2) {scanf ("%d%d%d%d%d", &r[i].x1, &r[i].y1, &r[i              ].X2, &r[i].y2, &AMP;R[I].V);              X[m] = r[i].x1, x[m + 1] = r[i].x2;          Y[m] = r[i].y1, y[m + 1] = R[i].y2; } sort (r, R + N); Place the value of the small building in front of sort (x, x + m); discretization x sort (y, y + m);          Discretization of y memset (val, 0, sizeof (Val));         for (int i = 0; i < n; ++i) {     X1 = FID (x, r[i].x1), x2 = FID (x, R[I].X2);  Get x discretized coordinates y1 = FID (y, r[i].y1), y2 = FID (y, r[i].y2); Coordinates for the y discretization (int j = x1; j < x2; ++j) for (int k = y1; k < y2; ++k) val[j][k] = r[          I].V;          } ll ans = 0; for (int i = 0, i < m-1; ++i) for (int j = 0; j < m-1; ++j) ans + = val[i][j] * (x[i          + 1]-x[i]) * (Y[j + 1]-y[j]);      printf ("Case%d:%i64d\n", ++cas, ans);  } return 0;   }
Scan Line (volume and):
#include <cstdio> #include <algorithm> #include <cstring>using namespace std;const int n=50; #define    Lson l,mid,ls#define Rson mid+1,r,rstypedef long long ll;struct node{int x1,x2,h,val,tag; Node (int a=0,int b=0,int c=0,int d=0,int e=0): X1 (a), X2 (b), H (c), Val (d), tag (e) {} bool operator< (const node &AMP;OP) CO    NST {return h<op.h;    }}seg[n],tp[n];int len[n<<2],cov[n<<2],val[n],h[n];void Build (int l,int R,int RT) {len[rt]=cov[rt]=0;    if (l==r) return;    int ls=rt<<1,rs=ls|1,mid= (L+R) >>1;    Build (Lson); Build (Rson);}        void update (int l,int r,int rt,int l,int r,int D) {if (l<=l&&r<=r) {cov[rt]+=d; LEN[RT]=COV[RT]?        H[R]-H[L-1]:(l==r?0:len[rt<<1]+len[rt<<1|1]);    Return    } int ls=rt<<1,rs=ls|1,mid= (L+R) >>1;    if (l<=mid) update (LSON,L,R,D);    if (r>mid) update (RSON,L,R,D); LEN[RT]=COV[RT]? H[R]-H[L-1]:LEN[LS]+LEN[RS];} int main () {int t,n,x1,x2,y1,y2,v,f=1;    scanf ("%d", &t);        while (t--) {scanf ("%d", &n);        int ct=0,m=0,nv=1;        val[0]=0;            for (int i=0;i<n;i++) {scanf ("%d%d%d%d%d", &x1,&y1,&x2,&y2,&v);            Seg[ct++]=node (x1,x2,y1,v,1), Seg[ct++]=node (x1,x2,y2,v,-1);            h[m++]=x1,h[m++]=x2;        Val[nv++]=v;        } sort (SEG,SEG+CT);        Sort (h,h+m);        M=unique (h,h+m)-H;        Sort (VAL,VAL+NV);        ll Ans=0;            for (int i=0;i<nv;i++) {int nt=0;            for (int j=0;j<ct;j++) if (Seg[j].val>val[i]) tp[nt++]=seg[j];            Build (1,m-1,1);            ll tt=0;                for (int j=0;j<nt-1;j++) {int l=lower_bound (h,h+m,tp[j].x1)-h+1;                int R=lower_bound (H,H+M,TP[J].X2)-H;                Update (1,M-1,1,L,R,TP[J].TAG); tt+= (LL) len[1]* (tp[j+1].h-tp[j].h); Request a height area} ans+=tt* (Val[i+1]-val[i]); volume       } printf ("Case%d:%i64d\n", F++,ans); } return 0;}
 
<pre name= "code" class= "CPP" > #include <cstdio> #include <algorithm>using namespace Std;const int MAXN =100010; #define Lson l,mid,ls#define rson mid+1,r,rsstruct node{int x1,x2,cmd;} seg[maxn];int x[maxn<<1];int len [Maxn<<2],cov[maxn<<2];//len[rt] is the length of the node being overwritten.    COV[RT] Indicates whether the entire cover void build (int l,int r,int RT)///Segment Tree l,r represents the X[l]~x[r+1] segment {len[rt]=cov[rt]=0;    if (l==r) return;    int ls=rt<<1,rs=ls|1,mid= (L+R) >>1;    Build (Lson); Build (Rson);}    void pushdown (int l,int r,int RT) {int ls=rt<<1,rs=ls|1,mid= (L+R) >>1;        if (cov[rt]==1) {cov[ls]=cov[rs]=1; len[ls]=x[mid]-x[l-1];//due to the X subscript starting from 0. So l,r are going to lose 1.    hereinafter Len[rs]=x[r]-x[mid];        } else {cov[ls]=cov[rs]=-1;    len[ls]=len[rs]=0; } cov[rt]=0;} void update (int l,int r,int rt,int l,int r,int D) {if (l<=l&&r<=r) {if (d==1)//= Overwrite C        OV[RT]=1,LEN[RT]=X[R]-X[L-1];  else cov[rt]=-1,len[rt]=0;      Return    } int ls=rt<<1,rs=ls|1,mid= (L+R) >>1;    if (Cov[rt]) pushdown (L,R,RT);    if (l<=mid) update (LSON,L,R,D);    if (r>mid) update (RSON,L,R,D);    LEN[RT]=LEN[LS]+LEN[RS]; printf ("%d->%d len%d\n", X[l-1],x[r],len[rt]);}    int main () {int t,n,m,i; scanf ("%d", &t),//t Group test data while (t--) {scanf ("%d", &n),//2 operations. 1 Insert Segment x1,x2.        -1 Delete the segment between x1,x2.           m=0; The length of the output x-axis is overwritten after each operation for (i=1;i<=n;i++) {scanf ("%d%d%d", &seg[i].x1,&seg[i].x2,&seg[i]            . cmd);        x[m++]=seg[i].x1,x[m++]=seg[i].x2;        } sort (x,x+m);        M=unique (x,x+m)-x;//m points have m-1 line segment I points represent segment x[i]~x[i+1] Build (1,m-1,1);            for (i=1;i<=n;i++) {int l=lower_bound (x,x+m,seg[i].x1)-x+1;            int R=lower_bound (X,X+M,SEG[I].X2)-x+1;            printf ("Update%d->%d\n", X[l]) update (1,M-1,1,L,R-1,SEG[I].CMD);        printf ("%d\n", len[1]); }    }    return 0;} 331 2 12 3 13 4 141 2 12 3 13 4 12 3-1
Also area and: Click to open link

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 3624 City Planning (violence, can also scan the line)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.