Zoj-3324 Machine

Source: Internet
Author: User

The line segment tree really struggled with me for several days. Since there is almost no foundation, we should first study other people's AC code.

Link: http://hi.baidu.com/forverlin1204/blog/item/5b0b533110d9d595a8018e5b.html

 

I don't know why the height of an interval is not 0, so I don't know whether there is a continuous block in its subinterval that satisfies the meaning of the question.

 

Later, I found that the meaning of the question was wrong. A push corresponds to a release. Therefore, if the height of the parent interval is not 0, the height of the Child interval cannot be 0.

 

Finally, I stumbled into writing the code and found that I submitted the WA. Comparing the AC code, we find that our code is poor: For the discretization point, if the distance between two points is greater than 1

 

Then, the end points of these two points must be added between them.

 

It means that you cannot understand why this is done, and the points after Discretization can correctly represent each interval. So why add a dot? Later, I simulated the insertion of two different representations on the paper. If this vertex is not added, the two non-consecutive points are considered continuous after discretization.

 

For example, the input coordinates include 1, 2, 4, and 5.

 

After conversion, the values are 0 (1), 1 (2), 2 (4), and 3 (5 ). As a result, the original point is regarded as continuous after. Therefore, we only need to insert a point between the two points.

 

The details of the problem are as follows:

 

I. Structure of the first tree:

 

Struct node {

Int height, right, left, CNT, R, L;

};

Height: the height of the interval. 1 is added for push, and 1 is subtracted for release. (From the image, it seems a little against common sense)

Right, left: whether the rightmost (left) of the record interval belongs to a continuous block.

CNT: records the number of consecutive blocks with a height of 0 in this interval.

R, L: left and right boundary of the interval.

 

 

2. How to mark

 

We only need to mark the maximum range that can be overwritten.

Since the height of an interval is 0, it is certainly not a continuous block that matches the meaning of the question. Therefore, after a push or release interval, if heigh is not 0, the CNT, right, and

 

Left values are 0.

 

3. How to update (pass tag)

 

The practice is always recursive update. The problem is how to update the parent Interval Based on the subinterval status.

 

First, if the height of the parent interval is not 0, you do not need to consider the specific circumstances of the subinterval. CNT is always 0.

 

If the height of the parent interval is 0, you must consider the subinterval. CNT equals the addition of CNT in two subintervals. Pay attention to the subtraction of repeated computations (if any ).

 

In addition, we need to mark the right and left of the subinterval. Finally, when updating a node, you must check whether the interval is a leaf node.

 

 

PS. This time I finally realized what is actually discretization: Just record what is needed. The line segment that we used to calculate is just the first experience of discretization, and we do not feel the obvious discrete -_-

 

 

# Include <cstdio> <br/> # include <algorithm> <br/> using namespace STD; <br/> # define maxn 40010 <br/> struct op {<br/> int L, R; <br/> char C; <br/> }; <br/> struct node {<br/> int height, right, left, CNT, R, L; <br/> }; <br/> node tree [maxn * 4]; <br/> op P [maxn]; <br/> int T [maxn], TT [maxn]; <br/> void build (int K, int L, int R) {<br/> tree [K]. right = tree [K]. left = 1; <br/> tree [K]. L = L; tree [K]. R = r; <br/> Tree [K]. height = 0; <br/> tree [K]. CNT = 1; <br/> If (L = r) return; <br/> int mid = (L + r)/2 ;; <br/> build (k <1, L, mid); <br/> build (k <1 | 1, Mid + 1, R ); <br/>}< br/> // This interval is updated only when the height of the interval is 0. <Br/> void Update (int K) {<br/> If (tree [K]. L = tree [K]. r) {<br/> tree [K]. CNT = 1; <br/> tree [K]. left = tree [K]. right = 1; <br/> return; <br/>}< br/> int lson = k <1, rson = k <1 | 1; <br/> tree [K]. CNT = tree [lson]. CNT + tree [rson]. CNT; <br/> If (tree [lson]. right & tree [rson]. left) {<br/> tree [K]. CNT --; <br/>}< br/> // update the continuity of the subinterval based on whether the subinterval is consecutive. <br/> tree [K]. left = tree [lson]. left; <br/> tree [K]. right = tree [rson]. Right; <br/>}< br/> void add (int K, int L, int R) {<br/> int lson = k <1, rson = k <1 | 1; <br/> If (L <= tree [K]. L & tree [K]. r <= r) {<br/> tree [K]. height ++; <br/> // If (tree [K]. height = 0) {<br/> // Update (k ); <br/>/}< br/> // else {<br/> tree [K]. left = tree [K]. right = 0; <br/> tree [K]. CNT = 0; <br/>//} <br/> return; <br/>}< br/> int mid = (tree [K]. L + tree [K]. r)> 1; <br/> If (L <= mid) {<br/> Add (lson, L, R); <br />}< Br/> If (r> mid) {<br/> Add (rson, L, R ); <br/>}< br/> If (tree [K]. height = 0) {// when tree [K]. if the height value is not 0, the k-th interval must be discontinuous. <Br/> Update (k); <br/>}< br/> void del (int K, int L, int R) {<br/> int lson = k <1, rson = k <1 | 1; <br/> If (L <= tree [K]. L & tree [K]. r <= r) {<br/> tree [K]. height --; <br/> If (tree [K]. height = 0) {<br/> Update (k); <br/>}< br/> else {<br/> tree [K]. left = tree [K]. right = 0; <br/> tree [K]. CNT = 0; <br/>}< br/> return; <br/>}< br/> int mid = (tree [K]. L + tree [K]. r)> 1; <br/> If (L <= mid) {<br/> Del (lson, l, R); <br/>}< br/> If (r> mid) {<br/> Del (rson, L, R ); <br/>}< br/> If (tree [K]. height = 0) {<br/> Update (k); <br/>}< br/> int find (int x, int l, int R) {<br/> while (L <r) {<br/> int mid = (L + r)> 1; <br/> If (TT [Mid] = x) {<br/> return mid; <br/>}< br/> else if (TT [Mid] <X) {<br/> L = Mid + 1; <br/>}< br/> else {<br/> r = mid; <br/>}< br/> return-1; <br/>}< br/> int main () {<br/> int T, CAS = 1, Tlen, ttlen, I; <br/> char op [4]; <br/> scanf ("% d", & T ); <br/> while (t --) {<br/> printf ("case # % d:/N", CAS ++); <br/> int n, m; <br/> tlen = 0; <br/> scanf ("% d", & N, & M); <br/> for (I = 0; I <m; I ++) {<br/> scanf ("% S % d", op, & P [I]. l, & P [I]. r); <br/> P [I]. C = op [0]; <br/> T [tlen ++] = P [I]. l; <br/> T [tlen ++] = P [I]. r; <br/>}< br/> T [tlen ++] = 0; <br/> T [tlen ++] = n-1; <br/> sort (t, t + tlen); <br/> ttlen = 1; <Br/> for (I = 1; I <tlen; I ++) {<br/> If (T [I]! = T [I-1]) {<br/> T [ttlen ++] = T [I]; <br/>}< br/> tlen = ttlen; <br/> ttlen = 0; <br/> TT [ttlen ++] = T [0]; <br/> for (I = 1; I <tlen; I ++) {<br/> // if no value is added, the original sequence 1, 2, 3, 5 is converted to 0, 1, 2, and 3. <Br/> // 3 and 5 corresponding to 3 are regarded as continuous blocks <br/> If (T [I]> T [I-1] + 1) TT [ttlen ++] = (T [I] + T [I-1])> 1; <br/> TT [ttlen ++] = T [I]; <br/>}< br/> build (1, 0, ttlen-1); <br/> for (I = 0; I <m; I ++) {<br/> int L = find (P [I]. l, 0, ttlen); <br/> int r = find (P [I]. r, 0, ttlen); <br/> If (P [I]. C = 'P') {<br/> Add (1, L, R); <br/>}< br/> else {<br/> Del (1, l, R); <br/>}< br/> printf ("% d/N", tree [1]. CNT); <br/>}< br/> return 0; <br/>}< br/> 

 

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.