Zoj 2301/HDU 1199 color the ball discretization + continuous maximum sum of Line Segment trees

Source: Internet
Author: User

Question: Let's arrange n balls in a row. They are black at first. Now, let's perform some operations (L, R, color) and color the results in the [L, R] interval, 'W' is white, and 'B' is black. Ask the start point and end point of the longest white interval.

Solution: First discretization, in order to prevent discrete error, not only will l, r discrete, but also add L + 1, L-1, R + 1, R-1 discrete together, in this way, no problem will occur. Create a line segment tree and maintain four values:

1. Col range color 0 indicates black 1 indicates white-1 indicates no mark

2. The length of the maximum white interval in the maxi interval. Because the white is expressed as 1, the maximum white Interval Length is the maximum continuous and

3. Maximum length of the Lmax white interval starting from the left end of the Interval

4. Maximum white Interval Length of rmax starting from the right end of the Interval

Then update and query, which is the maximum continuous and no difference from the common interval.

Code:

# Include <iostream> # include <cmath> # include <iostream> # include <cstdio> # include <cstring> # include <cstdlib> # include <cmath> # include <algorithm> # include <map> using namespace STD; # define n 14007 struct node {int Lmax, rmax, Maxi, Col;} tree [4 * n]; int num [N], X [N]; int L [2005], R [2005]; char ss [2005] [3]; Map <int, int> MP; void pushup (int l, int R, int RT) {tree [RT]. lmax = tree [2 * RT]. lmax; tree [RT]. rmax = tree [2 * RT + 1 ]. Rmax; tree [RT]. maxi = max (tree [2 * RT]. maxi, tree [2 * RT + 1]. maxi), tree [2 * RT]. rmax + tree [2 * RT + 1]. lmax); int mid = (L + r)/2; int L = x [Mid]-x L-1]; // true length int r = x [R]-X [Mid]; If (tree [2 * RT]. lmax = L) tree [RT]. lmax + = tree [2 * RT + 1]. lmax; If (tree [2 * RT + 1]. rmax = r) tree [RT]. rmax + = tree [2 * RT]. rmax;} void Pushdown (int l, int R, int RT) {If (tree [RT]. col! =-1) {tree [2 * RT]. col = tree [2 * RT + 1]. col = tree [RT]. col; int mid = (L + r)/2; int L = x [Mid]-x L-1]; int r = x [R]-X [Mid]; tree [2 * RT]. maxi = tree [2 * RT]. lmax = tree [2 * RT]. rmax = L * tree [RT]. col; tree [2 * RT + 1]. maxi = tree [2 * RT + 1]. lmax = tree [2 * RT + 1]. rmax = r * tree [RT]. col; tree [RT]. col =-1 ;}} void build (int l, int R, int RT) {tree [RT]. maxi = tree [RT]. lmax = tree [RT]. rmax = 0; tree [RT]. col =-1; if (L = r) Return; int mid = (L + r)/2; build (L, mid, 2 * RT); Build (Mid + 1, R, 2 * RT + 1 );} void Update (int l, int R, int AA, int BB, int Col, int RT) {If (AA <= L & BB> = r) {tree [RT]. col = Col; tree [RT]. maxi = tree [RT]. lmax = tree [RT]. rmax = Col * (X [R]-L-1]); return;} Pushdown (L, R, RT); int mid = (L + r)/2; if (AA <= mid) Update (L, mid, AA, BB, Col, 2 * RT); If (BB> mid) Update (Mid + 1, R, AA, BB, Col, 2 * RT + 1); pushup (L, R, RT);} int query (Int L, int R, int RT) {If (L = r) return X [l]; int mid = (L + r)/2; Pushdown (L, R, RT); If (tree [2 * RT]. maxi = tree [1]. maxi) // tree [1] Not tree [RT] Return query (L, mid, 2 * RT); else if (tree [2 * RT]. rmax + tree [2 * RT + 1]. lmax = tree [1]. maxi) return X [Mid]-tree [2 * RT]. rmax + 1; else return query (Mid + 1, R, 2 * RT + 1);} int main () {int N, I, J, K; while (scanf ("% d", & N )! = EOF) {MP. clear (); for (I = 1; I <= N; I ++) {scanf ("% d % s", & L [I], & R [I], ss [I]); num [6 * I-5] = L [I]-1; num [6 * I-4] = L [I]; num [6 * I-3] = L [I] + 1; num [6 * I-2] = R [I]-1; num [6 * I-1] = R [I]; num [6 * I] = R [I] + 1;} Sort (Num + 1, num + 6 * n + 1); int ind = unique (Num + 1, num + 6 * n + 1)-num-1; int now = 0; X [0] = 0; for (I = 1; I <= ind; I ++) {x [++ now] = num [I]; MP [num [I] = now;} build (1, now, 1); for (I = 1; I <= N; I ++) {int Ka = MP [L [I]; int kb = MP [R [I]; if (ss [I] [0] = 'W') Update (1, now, ka, kb, 1, 1); else update (1, now, ka, kb, 0, 1);} If (tree [1]. maxi <= 0) puts ("Oh, my God"); else {int left = query (1, now, 1); int right = left + tree [1]. maxi-1; printf ("% d \ n", left, right) ;}} return 0 ;}
View code

 

Zoj 2301/HDU 1199 color the ball discretization + continuous maximum sum of Line Segment trees

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.