POJ-2892-Tunnel Warfare (line tree)

Source: Internet
Author: User

POJ-2892-Tunnel Warfare (line tree)

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 integersNAndM(N,M≤ 50,000) indicating the number of versions and events. Each of the nextMLines describes an event.

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

D x: X-Th village was destroyed. Q x: The Army commands requested the number of commands ages that X-Th village was directly or indirectly connected with including itself. R: The village destroyed last was rebuilt.

Output

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

Hint

An authentication of the sample input:

      OOOOOOOD 3   OOXOOOOD 6   OOXOOXOD 5   OOXOXXOR     OOXOOXOR     OOXOOOO

Source

POJ Monthly -- 2006.07.30, updog


Idea: The line segment tree maintains the maximum continuous intervals between the left and right sides of each interval to the center. Note that the left and right subtree may have the maximum continuous interval size equal to the interval size of the node.

# Include
 
  
Struct {int l, r; // record the maximum continuous interval between the left and right sides respectively} node [200000]; int stk [50000], top, n, m; void build (int idx, int s, int e) {node [idx]. l = node [idx]. r = e-s + 1; if (s! = E) {int mid = (s + e)> 1; build (idx <1, s, mid); build (idx <1 | 1, mid + 1, e) ;}} void update (int idx, int s, int e, int pos, int val) {if (s = e) node [idx]. l = node [idx]. r = val; // if val is 0, the village is destroyed. if val is 1, the village else {int mid = (s + e)> 1 is repaired. if (pos <= mid) update (idx <1, s, mid, pos, val); else update (idx <1 | 1, mid + 1, e, pos, val ); if (node [idx <1]. l = mid-s + 1) node [idx]. l = mid-s + 1 + node [idx <1 | 1]. l; // The left subtree is full of else node [idx]. l = node [idx <1]. l; if (nod E [idx <1 | 1]. r = e-mid) node [idx]. r = e-mid + node [idx <1]. r; // The right subtree is full of else node [idx]. r = node [idx <1 | 1]. r ;}} int query (int idx, int s, int e, int pos) {if (s = e) return 0; // if it reaches the leaf node, it must be 0 int mid = (s + e)> 1; if (pos <= mid) {if (pos> = mid-node [idx <1]. r + 1) return node [idx <1]. r + node [idx <1 | 1]. l; // if it is included in the right range of the Left subtree, add the left range else return query (idx <1, s, mid, pos) of the right subtree );} else {if (pos <= mid + node [idx <1 | 1]. l) return node [idx <1 | 1]. L + node [idx <1]. r; // Add the right else return query (idx <1 | 1, mid + 1, e, pos) ;}} int main () {int t; char s [5]; while (~ Scanf ("% d", & n, & m) {build (1, 1, n); top = 0; while (m --) {scanf ("% s", s); if (s [0] = 'D') {scanf ("% D", & t ); stk [top ++] = t; update (, n, t, 0);} else if (s [0] = 'R') {update, n, stk [-- top], 1) ;}else {scanf ("% d", & t); printf ("% d \ n", query (1, 1, n, t ));}}}}
 


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.