HDU-4419-colourful rectangle-line segment tree

Source: Internet
Author: User

Ask http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4419

Calculate the coverage area of different colors.

# Include <cstring> # include <cstdio> # include <algorithm> using namespace STD; typedef _ int64 LLD; # define lson l, m, RT <1 # define rson m + 1, R, RT <1 | 1 const int maxn = 65537; LLD sum [maxn] [1 <3]; int X [20010]; int cover [maxn] [4]; struct seg {int L, R, H, flag; seg () {} seg (INT _ L, int _ r, int _ H, int _ flag): L (_ L), R (_ r), H (_ H), flag (_ flag) {} bool operator <(const seg & CMP) const {If (H! = CMP. h) Return H <CMP. h; return flag <CMP. flag ;}} ss [20010]; inline void pushup (int rt, int L, int R) {int S = 0, I; // If cover exists, this interval is completely overwritten and cannot be changed by subintervals for (I = 1; I <= 3; I ++) if (cover [RT] [I]) S | = 1 <(I-1); // The status for (I = 0; I <8; I ++) sum [RT] [I] = 0; If (L = r) {sum [RT] [s] = x [R + 1]-X [l];} else {for (I = 0; I <8; I ++) /* keys */sum [RT] [I | S] + = sum [RT <1] [I] + sum [RT <1 | 1] [I]; }} void Update (int l, int R, int C, int L, int R, int RT) {If (L <= L & R <= r) {If (C> 0) cover [RT] [c] ++; else cover [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);} void build (int l, int R, int RT) {int I; for (I = 1; I <= 3; I ++) cover [RT] [I] = 0; for (I = 1; I <8; I ++) sum [RT] [I] = 0; sum [RT] [0] = x [R + 1]-X [l]; If (L = r) {return;} int M = L + r> 1; build (lson); Build (rson);} LLD ans [1 <3]; int main () {char Col [5]; int N, I, j, TOT, CA = 1, t, flag; int X1, Y1, X2, Y2; scanf ("% d ", & T); While (t --) {scanf ("% d", & N); for (I = 1, TOT = 0; I <= N; I ++) {scanf ("% S % d", Col, & X1, & Y1, & X2, & Y2); X [tot] = x1; if (COL [0] = 'R') Flag = 1; else if (COL [0] = 'G') Flag = 2; else flag = 3; ss [tot ++] = seg (x1, x2, Y1, flag); X [tot] = x2; SS [tot ++] = seg (x1, x2, Y2, -Flag);} Sort (x, x + ToT); sort (SS, SS + ToT); n = unique (x, x + ToT)-X; build (0, n-2 (1); memset (ANS, 0, sizeof (ANS); for (I = 0; I <tot-1; I ++) {int left = lower_bound (x, x + N, ss [I]. l)-X; int right = lower_bound (x, x + N, ss [I]. r)-X-1; Update (left, right, ss [I]. flag, 0, N-2, 1); For (j = 1; j <8; j ++) ans [J] + = sum [1] [J] * (ss [I + 1]. h-ss [I]. h);} 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 ;}

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.