Sgu 177. Square DFS Floating Method Reverse Dyeing

Source: Internet
Author: User
177. squaretime limit per test: 2.5 sec.
Memory limit per test: 65536 kbinput: Standard
Output: Standard

You have given the square nxn on a checkered sheet. Size of each cell is 1x1, (1, 1) is leftmost top corner and (n, n) is rightmost bottom corner.
Initially all cells are white.
There are m repaintings inside of the square, where one repainting changes color of specified rectangle to some color (white or black). All sides of repainted rectangles are parallel to sides of Square.
You need to find amount of white cells after all repaintings. Input The first line of input consists of two numbers N and M (1 <= n <= 1000, 1 <= m <= 5000 ). each of the next M lines consists of X1 Y1 X2 Y2 C, where (x1, Y1) and (X2, Y2) Are indexes of opposite corners of the rectangular, and C is a symbol 'B' or 'W' ('B' means black color and 'w'-white) (1 <= x1, x2, Y1, y2 <= N ). all numbers in input are integer. Output Write amount of white cells after all repaintings. Sample test (s) Input 9 6
2 2 4 6 B
4 3 3 3 W
6 2 8 6 B
5 3 6 9 W
8 3 9 9 W
1 5 3 5 W Output 63

 

This is basically the same as usaco 3.1.4 shaping regions...

This problem can be done by cutting the segment of linear moment, or reverse dyeing by DFS Floating Method.

What is float?

First, we begin to dye all the dyeing methods, and then start dyeing from the front.

When a cursor is colored by the subsequent dyeing method, it cannot be colored by the cursor.

For example, as shown in the following example: When the cursor that needs to be dyed before, false to I-1, base label for {[lx, ly], [RX, Ry]}, then start from the beginning of the I branch and then dye the back. So we only need to take the test of the I-1 did not have the I was covered in the area of dyeing. {[Lx, ly], [p [I]. lx-1, Ry]} and{[P [I]. lx, ly], [RX, P [I]. ry-1]} the two parts are stained.(Note that you need to renew your account in some places). {[P [I]. lx, P [I]. ly], [RX, Ry]} is overwritten, so you do not need to dye it. In the same way, there are other intersection conditions, which allow users to look at their behalf.

 

 

 

 

/* Subject: moment sequence dyeing analysis: Floating sequence dyeing. */# Include <set> # include <map> # include <cmath> # include <queue> # include <stack> # include <string> # include <vector> # include <cstdio> # include <cstring> # include <iostream> # include <algorithm> using namespace STD; typedef long ll; typedef unsigned long ull; # define lx (x) (x <1) # define rx (x) (x <1 | 1) # define debug puts ("here") # define rep (I, n) for (INT I = 0; I <n; I ++) # define rep1 (I, n) for (INT I = 1; I <= N; I ++) # define rep (I, a, B) for (INT I = A; I <= B; I ++) # define foreach (I, VEC) for (unsigned I = 0; I <Vec. size (); I ++) # define Pb push_back # define RD (n) scanf ("% d", & N) # define RD2 (x, y) scanf ("% d", & X, & Y) # define rd3 (x, y, z) scanf ("% d", & X, & Y, & Z) # define RD4 (X, Y, Z, W) scanf ("% d", & X, & Y, & Z, & W)/******************************/const int maxn = 5005; int n, m, ans; struct node {int lx, Ly, RX, Ry, Col; void RD () {RD4 (LX, Ly, RX, ry ); char op [2]; scanf ("% s", OP); Col = op [0] = 'B'; If (LX> RX) Swap (LX, RX); If (LY> ry) Swap (ly, ry) ;}} P [maxn]; void DFS (INT lx, int ly, int RX, int ry, int Col, int I) {If (LX> RX | LY> ry) return; // does not conform to while (I <= M & (LX> P [I]. RX | LY> P [I]. ry | RX <p [I]. lx | ry <p [I]. ly) I ++; // The two regions do not intersection if (I> m) {// The colored if (COL) ans + = (RX-lx + 1) * (ry-Ly + 1); // update the black semi return;} If (LX <p [I]. lx) {DFS (LX, Ly, P [I]. lx-1, Ry, Col, I + 1); Lx = P [I]. lx; // update} If (RX> P [I]. RX) {DFS (P [I]. RX + 1, Ly, RX, Ry, Col, I + 1); RX = P [I]. RX;} If (ly <p [I]. ly) DFS (LX, Ly, RX, P [I]. LY-1, Col, I + 1); If (RY> P [I]. ry) DFS (LX, P [I]. ry + 1, RX, Ry, Col, I + 1);} int main () {# ifndef online_judgefreopen ("sum. in "," r ", stdin); // freopen (" sum. out "," W ", stdout); # endif RD2 (n, m); rep1 (I, m) P [I]. rd (); For (INT I = m; I --) DFS (P [I]. lx, P [I]. ly, P [I]. RX, P [I]. ry, P [I]. col, I + 1); cout <n * n-ans <Endl; // cout <ans <Endl; 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.