HUD-4419 Colourful Rectangle line segment tree + scanning line + discrete

Source: Internet
Author: User

Different colors overlap to produce different colors: RG, RB, BG, RGB, calculate the area of each color.
Rectangular area and enhanced scanning line. The line segment tree records the length of 7 colors respectively, and then records the number of times each color overwrites.
My code:
[Cpp]
// STATUS: C ++ _ AC_250MS_5260KB
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Include <algorithm>
# Include <string>
# Include <vector>
# Include <queue>
# Include <stack>
# Include <math. h>
# Include <map>
# Include <set>
Using namespace std;
# Define LL _ int64
# Define pii pair <int, int>
# Define mem (a, B) memset (a, B, sizeof ())
# Define lrt rt <1
# Define rrt rt <1 | 1
# Define lson l, mid, rt <1
# Define rson mid + 1, r, rt <1 | 1
# Define Max (x, y) (x)> (y )? (X) :( y ))
# Define Min (x, y) (x) <(y )? (X) :( y ))
Const int MAX = 10010, INF = 200000000;
Const double esp = 1e-6;
 
Struct Tree {
Int len [8], cou [5];
} Ret [MAX <3];
Struct Node {
Int x, y1, y2, lorr, val;
} Nod [MAX <1];
 
Int cmp1 (const void * a, const void * B ){
Return (Node *) a)-> x-(Node *) B)-> x;
}
Int cmp2 (const void * a, const void * B ){
Return * (int *) a-* (int *) B;
}
 
Void update (int l, int r, int rt );
Void pushup (int rt, int l, int r );
LL ans [8];
Int y [MAX <1];
Int T, n, k, a, B, cc, x1, x2, jorj;
Map <int, int> q;
Int main ()
{
// Freopen ("in.txt", "r", stdin );
Int I, j, ca = 1;
Char str [2];

Scanf ("% d", & T );
While (T --)
{
K = 0;
Q. clear ();
Mem (ans, 0 );
Mem (ret, 0 );
Scanf ("% d", & n );

N <= 1;
For (I = 0; I <n; I + = 2 ){
Scanf ("% s % d", str, & nod [I]. x, & nod [I]. y1,
& Nod [I + 1]. x, & nod [I + 1]. y2 );
Nod [I]. lorr = 1;
Nod [I + 1]. lorr = 0;
Nod [I + 1]. val = nod [I]. val = (str [0] = 'R '? 1 :( str [0] = 'G '? 2: 4 ));
Nod [I]. y2 = nod [I + 1]. y2;
Nod [I + 1]. y1 = nod [I]. y1;
If (! Q [nod [I]. y1]) {
Q [nod [I]. y1] = 1;
Y [k ++] = nod [I]. y1;
}
If (! Q [nod [I]. y2]) {
Q [nod [I]. y2] = 1;
Y [k ++] = nod [I]. y2;
}
}

Qsort (nod, n, sizeof (Node), cmp1 );
Qsort (y, k, sizeof (int), cmp2 );
Q. clear ();
For (I = 0; I <k; I ++)
Q [y [I] = I;
 
For (I = 0; I <n; I ++ ){
Jorj = nod [I]. lorr;
A = q [nod [I]. y1];
B = q [nod [I]. y2]-1;
Cc = nod [I]. val;
Update (0, K-1, 1 );
If (nod [I]. x! = Nod [I + 1]. x ){
For (j = 1; j <8; j ++)
Ans [j] + = (LL) (nod [I + 1]. x-nod [I]. x) * (LL) ret [1]. len [j];
}
}
 
Printf ("Case % d: \ n", ca ++ );
Printf ("% I64d \ n % I64d \ n % I64d \ n % I64d \ n % I64d \ n % I64d \ n % I64d \ n ",
Ans [1], ans [2], ans [4], ans [3], ans [5], ans [6], ans [7]);
}
Return 0;
}
 
Void pushup (int rt, int l, int r)
{
Int I, sta = (ret [rt]. cou [1]? 1:0) | (ret [rt]. cou [2]? 2: 0) | (ret [rt]. cou [4]? 4: 0 ));
If (sta ){
Mem (ret [rt]. len, 0 );
Ret [rt]. len [sta] = y [r + 1]-y [l];
For (I = 1; I <8; I ++ ){
If (sta! = (Sta | I )){
Int t = ret [lrt]. len [I] + ret [rrt]. len [I];
Ret [rt]. len [sta | I] + = t;
Ret [rt]. len [sta]-= t;
}
}
}
Else if (l! = R ){
For (I = 1; I <8; I ++)
Ret [rt]. len [I] = ret [lrt]. len [I] + ret [rrt]. len [I];
}
Else mem (ret [rt]. len, 0 );
}
 
Void update (int l, int r, int rt)
{
If (a <= l & r <= B ){
Jorj? Ret [rt]. cou [cc] ++: ret [rt]. cou [cc] --;
Pushup (rt, l, r );
Return;
}
Int mid = (l + r)> 1;
If (a <= mid) update (lson );
If (B> mid) update (rson );
Pushup (rt, l, r );
}

Related Article

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.