hdu4533 Willie Cat Series story--Bask in quilt

Source: Internet
Author: User

Problem Description because of the marathon in the chicken leg of the topic let many people regret and return, Willie Cat always feel unworthy to everyone, these days he quietly moved to the Cartesian coordinate system to live.
Life will continue, the sun will also rise, today, Willie Cat in the first quadrant of a rectangular quilt, each side of the quilt and axis parallel to each other, some parts of the quilt may be stacked together. At this point, at the origin of a sudden flooding, time t, the flood will spread to (T, T), that is, the lower left corner is (0, 0), the upper right corner (T, T) in the rectangle has water.
Tragic Willie Cats want to know, in time T1, T2, T3 ... tx, how much area of his quilt is wet?
Input data first contains a positive integer t, indicating that there is a T group of test data;
The first line of each group of data is first an integer n, indicating that there are n quilts;
Next n rows, each row contains four integers x1, y1, x2, Y2, representing the lower-left corner of a quilt and the coordinates of the upper-right corner;
Then enter an integer x on the next line to indicate an X-query;
Next x lines, enter x strictly monotonically incrementing integers, one per line, representing the time ti Willie Cats want to know.

[Technical specification]
T <= 5
0 < N <= 20000
1 <= x1 < x2 <= 200000
1 <= y1 < y2 <= 200000
1 <= x <= 20000
1 <= ti <= 200000 (1 <= i <= x)

Output for each query, calculate and output ti when the quilt of how much area is wet, each output takes up one row.

Sample Input
121 1 3 3 2 2 4 4512345

Sample Output
01588

The area of the problem is the area of each rectangle and is not rectangular area and, that is, not affect each other, because for each period, the area of the rectangle can be represented by a*t^2+b*t+c, so we can use a tree array or line segment tree to maintain the a,b,c, each read into a rectangle, It records its effect on different time periods T, and then adds the corresponding coefficients to the segment tree.

When a rectangle is read (the lower-left coordinate (x1,y1), the upper-right coordinate (x2,y2)), there are four kinds of effects:

1.0~max (X1,Y1) This period because the T-time formation of the rectangle under the rectangle, the area has no effect, so do not consider.

2. If there is a rectangular part of T time that covers the rectangle but does not touch or exceed the top or right of the rectangle, update a,b,c at the same time, and the expression is (t-x1) * (t-y1). The judgment expression here is to update Max (X1,Y1) ~min (x2,y2) if (max (x1,y1) <min (x2,y2)).

3.t time in the formation of a rectangle just hit or over the right or the top of a bar, that time the rectangular area changes (x2-x1) or (y2-y1) is a constant, then to update, note, here update the time to classify the discussion, specific look at the code.

4.Max (x2,y2) ~MAXN, this period because the T-time formation of the rectangle has completely covered the rectangle, so to update C, plus area constants.

When you write a line tree, note that three variables should be stored together, not open three segment tree, will be super memory.

Line segment Tree Code:

/p>

#include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> #include < math.h> #include <vector> #include <map> #include <set> #include <queue> #include <stack > #include <string> #include <algorithm>using namespace std; #define LL __int64#define maxn 200050LL a,b,c; ll A[10];struct node{ll L,r,cnt1,cnt2,cnt3,flag;}    B[4*maxn];void Build (ll l,ll r,ll i) {ll mid;    b[i].l=l;b[i].r=r;b[i].cnt1=b[i].cnt2=b[i].cnt3=0;b[i].flag=1;    if (l==r) return;    Mid= (L+R)/2;    Build (L,MID,I*2); Build (mid+1,r,i*2+1);}    void Update (LL l,ll r,ll a[],ll i) {ll mid;    if (b[i].l==l && b[i].r==r) {B[i].cnt1+=a[1];b[i].cnt2+=a[2];b[i].cnt3+=a[3];return;    } mid= (B[I].L+B[I].R)/2;    if (r<=mid) update (L,R,A,I*2);    else if (l>mid) update (L,R,A,I*2+1);        else{Update (L,MID,A,I*2);    Update (MID+1,R,A,I*2+1);    }}void question (ll id,ll i) {ll mid; if (B[i].l==id && b[i].r==id) {a=b[I].cnt1; B=b[i].cnt2;    C=b[i].cnt3;return;        } if (b[i].cnt1) {b[i*2].cnt1+=b[i].cnt1;        B[i*2+1].cnt1+=b[i].cnt1;    b[i].cnt1=0;        } if (B[i].cnt2) {b[i*2].cnt2+=b[i].cnt2;        B[i*2+1].cnt2+=b[i].cnt2;    b[i].cnt2=0;        } if (B[i].cnt3) {b[i*2].cnt3+=b[i].cnt3;        B[i*2+1].cnt3+=b[i].cnt3;    b[i].cnt3=0;    } mid= (B[I].L+B[I].R)/2;    if (Id<=mid) question (id,i*2); else question (id,i*2+1);}    int main () {ll i,j,x1,x2,y1,y2,t,t1,t2;    int t,n,m;    scanf ("%d", &t);        while (t--) {scanf ("%d", &n);        Build (1,maxn,1);            for (i=1;i<=n;i++) {scanf ("%i64d%i64d%i64d%i64d", &x1,&y1,&x2,&y2);                if (max (x1,y1) <min (x2,y2)) {a[1]=1;a[2]=-(x1+y1); a[3]=x1*y1;            Update (max (x1,y1), Min (x2,y2) -1,a,1);                } if (X2<y2) {a[1]=0;a[2]=-x1+x2;a[3]=y1* (X1-X2);              Update (max (x2,y1), y2-1,a,1); }           if (y2<x2) {a[1]=0;a[2]=-y1+y2;a[3]=x1* (y1-y2);              Update (max (Y2,X1), x2-1,a,1);            } a[1]=0;a[2]=0;a[3]= (x2-x1) * (Y2-Y1);                    Update (max (x2,y2), maxn,a,1);        } scanf ("%d", &m);            for (i=1;i<=m;i++) {scanf ("%i64d", &t);            a=b=c=0;            Question (t,1);        printf ("%i64d\n", a*t*t+b*t+c); }} return 0;}


Tree-like array code://Speed faster than = =

#include <iostream> #include <cstdio> #include <cstring> #define MAXN 200010 #define LL __int64 using Namespace Std;ll Max (ll A,ll b) {return a>b?a:b;} ll min (ll a,ll b) {return a<b?a:b;} struct Segment{ll b[maxn],ans;void Clear () {memset (b,0,sizeof (b));} int Lowbit (ll x) {return x& (x);} void update (ll pos,ll num) {while (POS&LT;=MAXN) {b[pos]+=num;pos+=lowbit (pos);}} ll Getsum (ll pos) {ans=0;while (pos>0) {Ans+=b[pos];p os-=lowbit (POS);} return ans;}} A,b,c;void Fun (ll l,ll r,ll num1,ll num2,ll num3) {a.update (L,NUM1); A.update (R+1,-NUM1); B.update (L,NUM2); B.update (R+1,-NUM2); C.update (L,NUM3); C.update (R+1,-NUM3);}      int main () {int t;      scanf ("%d", &t); while (t--) {a.clear (); B.clear ();         C.clear ();          int n,m;          scanf ("%d", &n);              for (int i=0;i<n;i++) {ll x1,y1,x2,y2;                scanf ("%i64d%i64d%i64d%i64d", &x1,&y1,&x2,&y2);        if (max (x1,y1) <min (x2,y2))          Fun (Max (x1,y1), Min (x2,y2), 1,-(x1+y1), x1*y1);              if (x2<y2) Fun (max (x2,y1) +1,y2,0,-x1+x2,y1* (x1-x2));                if (y2<x2) Fun (max (y2,x1) +1,x2,0,-y1+y2,x1* (y1-y2));          Fun (Max (x2,y2) +1,maxn,0,0, (x2-x1) * (y2-y1));          } scanf ("%d", &m);              while (m--) {ll t;              scanf ("%i64d", &t);              ll Ans=0;              Ans+=a.getsum (t) *t*t;              Ans+=b.getsum (t) *t;                Ans+=c.getsum (t);          printf ("%i64d\n", ans);  }} return 0;  }



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

hdu4533 Willie Cat Series story--Bask in quilt

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.