Hdu 1540 Tunnel Warfare (merge line segment tree segments)

Source: Internet
Author: User

Hdu 1540 Tunnel Warfare (merge line segment tree segments)

Question link: http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 1540

 

Tunnel WarfareTime Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission (s): 6065 Accepted Submission (s): 2344

Problem Description During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast areas of north China Plain. generally speaking, ages connected by tunnels lay in a line. cannot the two at the ends, every village was directly connected with two neighboring ones.

Frequently the invaders launched attack on some of the versions and destroyed the parts of tunnels in them. the Eighth Route Army commanders requested the latest connection state of the tunnels and ages. if some versions are severely isolated, restoration of connection must be done immediately!

Input The first line of the input contains two positive integers n and m (n, m ≤ 50,000) indicating the number of versions and events. Each of the next m lines describes an event.

There are three different events described in different format shown below:

D x: The x-th village was destroyed.

Q x: The Army commands requested the number of versions that x-th village was directly or indirectly connected with including itself.

R: The village destroyed last was rebuilt.

Output the answer to each of the Army commanders 'request in order on a separate line.

Sample Input
7 9D 3D 6D 5Q 4Q 5RQ 4RQ 4

Sample Output
1024

Source POJ Monthly
Recommend LL | We have carefully selected several similar problems for you: 1542 1255 1698 1828 2871
Input n, m: n indicates that n cities are connected, and m indicates that there are m lines of commands below. D x indicates that the city X is damaged, R indicates that the city that was last damaged is repaired, and q x indicates to query the maximum continuous number of X points in a row and output the results. Draw Based on the question to facilitate understanding:
Solution: first, build a tree. In the struct, define ls to record the maximum number of consecutive left endpoints of the interval, and rs to record the maximum number of consecutive records starting from the right endpoint of the interval, ms indicates the maximum number of consecutive values in the interval. The comments in the Code are clearly explained.
For details, see the code.
# Include
 
  
# Include
  
   
# Include using namespace std; struct node {int l, r; int ls, rs, MS; // indicate the maximum continuity on the left and the maximum continuity on the right, respectively, and the maximum continuous length of the entire range} s [50050*3]; int n, m; int op [50010]; void InitTree (int l, int r, int k) {s [k]. l = l; s [k]. r = r; s [k]. ls = s [k]. rs = s [k]. ms = r-l + 1; // It is connected at the beginning. So the length is r-l + 1 if (l = r) return; int mid = (l + r)/2; InitTree (l, mid, k * 2 ); initTree (mid + 1, r, k * 2 + 1);} void UpdataTree (int x, int flag, int k) // x indicates the number to be repaired or damaged, flag is used to mark whether it is damaged or repaired {if (s [k]. l = s [k]. r) {if (flag = 1) s [k]. ls = s [k]. rs = s [k]. ms = 1; // fix else s [k]. ls = s [k]. rs = s [k]. ms = 0; // destroy return;} int mid = (s [k]. l + s [k]. r)/2; if (x <= mid) UpdataTree (x, flag, 2 * k); else UpdataTree (x, flag, 2 * k + 1 ); if (s [2 * k]. ls = s [2 * k]. r-s [2 * k]. L + 1) // The left continuity of the Left interval = the length of the Left subtree, that is, the number of the Left interval is all consecutive (the left subtree interval is full ), the left part of the entire interval should be added with the left part of the interval. S [k]. ls = s [2 * k]. ls + s [2 * k + 1]. ls; else s [k]. ls = s [2 * k]. ls; if (s [2 * k + 1]. rs = s [2 * k + 1]. r-s [2 * k + 1]. l + 1) // Similarly, s [k]. rs = s [2 * k + 1]. rs + s [2 * k]. rs; else s [k]. rs = s [2 * k + 1]. rs; s [k]. ms = max (s [2 * k]. ms, s [2 * k + 1]. ms), s [2 * k]. rs + s [2 * k + 1]. ls); // The maximum continuity of the entire interval should be: the maximum interval of the Left subtree, the maximum interval of the right subtree, and the middle interval of the left and right subtree merging, max} int SearchTree (int x, int k) {if (s [k]. l = s [k]. r | s [k]. ms = 0 | s [k]. ms = s [k]. r-s [k]. l + 1) // you do not need to go down to return s [k] When the leaf node is reached or the access range is empty or full. ms; Int mid = (s [k]. l + s [k]. r)/2; if (x <= mid) {if (x> = s [2 * k]. r-s [2 * k]. rs + 1) // determine whether the current number is in the right continuous direction of the Left range, where s [2 * k]. r-s [2 * k]. rs + 1 indicates the left Boundary Value of the continuous interval on the right of the Left subtree, that is, return s [2 * k] of the start point of the continuous interval. rs + s [2 * k + 1]. ls; // SearchTree (x, 2 * k) + SearchTree (mid + 1, 2 * k + 1); else return SearchTree (x, 2 * k );} else {if (x <= s [2 * k + 1]. l + s [2 * k + 1]. ls-1) // determine whether the current number is in the right consecutive left range, where s [2 * k]. r-s [2 * k]. rs + 1 indicates the left Boundary Value of the continuous interval on the right of the Left subtree, that is, return s [2 * k] of the start point of the continuous interval. rs + s [2 * k + 1]. ls; // This Search Method Tree (x, 2 * k + 1) + SearchTree (mid, 2 * k); yes, but it is a waste of time else return SearchTree (x, 2 * k + 1) ;}} int main () {int x; char ch [2]; while (~ Scanf ("% d", & n, & m) {int top = 0; InitTree (1, n, 1); while (m --) {scanf ("% s", ch); if (ch [0] = 'D') {scanf ("% D", & x ); op [top ++] = x; UpdataTree (x, 0, 1);} else if (ch [0] = 'q') {scanf ("% d ", & x); printf ("% d \ n", SearchTree (x, 1);} else {if (x> 0) {x = op [-- top]; updataTree (x, 1, 1) ;}}} return 0 ;}
  
 

 

Related Article

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.