Hdu 4419 Colourful Rectangle (line segment tree scanning line), hdu4419

Source: Internet
Author: User

Hdu 4419 Colourful Rectangle (line segment tree scanning line), hdu4419

Question:

Multiple rectangles of different colors are given, and the area of the last covered color is obtained.


Train of Thought Analysis:

I manually perform brute force enumeration.

A problem is missed during the competition.

RGB can be combined with RG + RB (for example, it can be combined with two colors ).

Then you only need to maintain all colors

It is determined by scanning lines.


# Include <iostream> # include <cstdio> # include <algorithm> # include <cstring> # define MAXN 42222 using namespace std; typedef long ll; struct node {ll s, e, h, type, color; // records the bool operator (const node & cmp) const {return h <cmp. h ;}} line [MAXN]; // is the bottom or the high bottom is 1 High is-1 means that the bottom is the coverage. High is to delete ll tree [MAXN <2] [10]; ll cnt [MAXN <2] [10]; ll x [MAXN <2]; void pushup (int num, int l, int r) {int chs = 0; for (int I = 0; I <3; I ++) {if (cnt [num] [I]) chs | = (1 <I) ;}if (chs = 0) {if (l = r) {for (int I = 0; I <7; I ++) tree [num] [I] = 0; return;} for (int I = 0; I <7; I ++) tree [num] [I] = tree [num <1] [I] + tree [num <1 | 1] [I];} else if (chs = 1) {if (l = r) {for (int I = 0; I <7; I ++) {if (I = 0) tree [num] [I] = x [r + 1]-x [l]; else tree [num] [I] = 0;} return;} tree [num] [1] = 0; tree [num] [2] = 0; tree [num] [3] = tree [num <1] [1] + tree [num <1 | 1] [1] + tree [num <1] [3] + tree [num <1 | 1] [3]; tree [num] [4] = tree [num <1] [2] + tree [num <1 | 1] [2] + tree [num <1] [4] + tree [num <1 | 1] [4]; tree [num] [5] = 0; tree [num] [6] = tree [num <1] [5] + tree [num <1 | 1] [5] + tree [num <1] [6] + tree [num <1 | 1] [6]; ll tmp = x [r + 1]-x [l]; for (int I = 0; I <= 6; I ++) if (I! = 0) tmp-= tree [num] [I]; tree [num] [0] = tmp;} else if (chs = 2) {if (l = r) {for (int I = 0; I <7; I ++) {if (I = 1) tree [num] [I] = x [r + 1]-x [l]; else tree [num] [I] = 0;} return ;} tree [num] [0] = tree [num] [2] = 0; tree [num] [3] = tree [num <1] [0] + tree [num <1 | 1] [0] + tree [num <1] [3] + tree [num <1 | 1] [3]; tree [num] [4] = 0; tree [num] [5] = tree [num <1] [2] + tree [num <1 | 1] [2] + tree [num <1] [5] + tree [num <1 | 1] [5]; tree [num] [6] = tree [num <1] [4] + tr Ee [num <1 | 1] [4] + tree [num <1] [6] + tree [num <1 | 1] [6]; ll tmp = x [r + 1]-x [l]; for (int I = 0; I <= 6; I ++) if (I! = 1) tmp-= tree [num] [I]; tree [num] [1] = tmp;} else if (chs = 3) {if (l = r) {for (int I = 0; I <7; I ++) {if (I = 3) tree [num] [I] = x [r + 1]-x [l]; else tree [num] [I] = 0;} return ;} tree [num] [0] = tree [num] [2] = tree [num] [1] = 0; tree [num] [4] = 0; tree [num] [5] = 0; tree [num] [6] = tree [num <1] [2] + tree [num <1 | 1] [2] + tree [num <1] [6] + tree [num <1 | 1] [6] + tree [num <1] [4] + tree [num <1 | 1] [4] + tree [num <1] [5] + tree [num <1 | 1] [5]; ll tmp = x [r + 1]-x [L]; for (int I = 0; I <= 6; I ++) if (I! = 3) tmp-= tree [num] [I]; tree [num] [3] = tmp;} else if (chs = 4) {if (l = r) {for (int I = 0; I <7; I ++) {if (I = 2) tree [num] [I] = x [r + 1]-x [l]; else tree [num] [I] = 0;} return ;} tree [num] [0] = tree [num] [1] = 0; tree [num] [3] = 0; tree [num] [4] = tree [num <1] [0] + tree [num <1 | 1] [0] + tree [num <1] [4] + tree [num <1 | 1] [4]; tree [num] [5] = tree [num <1] [1] + tree [num <1 | 1] [1] + tree [num <1] [5] + tree [num <1 | 1] [5]; tree [num] [6] = tree [num <1] [3] + tr Ee [num <1 | 1] [3] + tree [num <1] [6] + tree [num <1 | 1] [6]; ll tmp = x [r + 1]-x [l]; for (int I = 0; I <= 6; I ++) if (I! = 2) tmp-= tree [num] [I]; tree [num] [2] = tmp;} else if (chs = 5) {if (l = r) {for (int I = 0; I <7; I ++) {if (I = 4) tree [num] [I] = x [r + 1]-x [l]; else tree [num] [I] = 0;} return ;} tree [num] [0] = tree [num] [2] = tree [num] [1] = 0; tree [num] [3] = 0; tree [num] [5] = 0; tree [num] [6] = tree [num <1] [1] + tree [num <1 | 1] [1] + tree [num <1] [6] + tree [num <1 | 1] [6] + tree [num <1] [3] + tree [num <1 | 1] [3] + tree [num <1] [5] + tree [num <1 | 1] [5]; ll tmp = x [r + 1]-x [L]; for (int I = 0; I <= 6; I ++) if (I! = 4) tmp-= tree [num] [I]; tree [num] [4] = tmp;} else if (chs = 6) {if (l = r) {for (int I = 0; I <7; I ++) {if (I = 5) tree [num] [I] = x [r + 1]-x [l]; else tree [num] [I] = 0;} return ;} tree [num] [0] = tree [num] [2] = tree [num] [1] = 0; tree [num] [3] = 0; tree [num] [4] = 0; tree [num] [6] = tree [num <1] [0] + tree [num <1 | 1] [0] + tree [num <1] [6] + tree [num <1 | 1] [6] + tree [num <1] [3] + tree [num <1 | 1] [3] + tree [num <1] [4] + tree [num <1 | 1] [4]; ll tmp = x [r + 1]-x [L]; for (int I = 0; I <= 6; I ++) if (I! = 5) tmp-= tree [num] [I]; tree [num] [5] = tmp;} else if (chs = 7) {if (l = r) {for (int I = 0; I <7; I ++) {if (I = 6) tree [num] [I] = x [r + 1]-x [l]; else tree [num] [I] = 0;} return ;} tree [num] [0] = tree [num] [2] = tree [num] [1] = 0; tree [num] [3] = 0; tree [num] [4] = 0; tree [num] [5] = 0; tree [num] [6] = x [r + 1]-x [l] ;}} void update (int num, int s, int e, int l, int r, ll val, ll color) {if (l <= s & r> = e) {cnt [num] [color] + = val; pushup (num, s, e); return;} int mid = (s + e)> 1; if (l <= mid) update (num <1, s, mid, l, r, val, color); if (r> mid) update (num <1 | 1, mid + 1, e, l, r, val, color); pushup (num, s, e);} void debug (int num, int s, int e) {printf ("s = % d e = % d \ n", s, e ); for (int I = 0; I <= 6; I ++) printf ("% d", tree [num] [I]); puts (""); if (s = e) return; int mid = (s + e)> 1; debug (num <1, s, mid ); debug (num <1 | 1, mid + 1, e);} ll ans [7]; int main () {int n, T; int cas = 1; for (scanf ("% d", & T); T --;) {scanf ("% d", & n); int m = 0; for (int I = 1; I <= n; I ++) {char str [5]; ll x1, y1, x2, y2; scanf ("% s % I64d % I64d % I64d % I64d", str, & x1, & y1, & x2, & y2); ll co; if (str [0] = 'R') co = 0; else if (str [0] = 'G') co = 1; else if (str [0] = 'B') co = 2; m ++; x [m] = x1; line [m]. s = x1, line [m]. e = x2, line [m]. h = y1, line [m]. type = 1, line [m]. color = co; m ++; x [m] = x2; line [m]. s = x1, line [m]. e = x2, line [m]. h = y2, line [m]. type =-1, line [m]. color = co;} sort (line + 1, line + 1 + m); sort (x + 1, x + 1 + m); int tot = unique (x + 1, x + 1 + m)-X-1; memset (ans, 0, sizeof ans); memset (tree, 0, sizeof tree); memset (cnt, 0, sizeof cnt ); for (int I = 1; I <m; I ++) {int L = lower_bound (x + 1, x + tot + 1, line [I]. s)-x; int R = lower_bound (x + 1, x + tot + 1, line [I]. e)-X-1; if (L <= R) update (, tot, L, R, line [I]. type, line [I]. color); for (int j = 0; j <= 6; j ++) ans [j] + = tree [1] [j] * (line [I + 1]. h-line [I]. h);} printf ("Case % d: \ n", cas ++); for (int I = 0; I <= 6; I ++) printf ("% I64d \ n", ans [I]);} return 0 ;}





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.