Tunnel Warfare HDU - (merge different sub-tree tree line) 1540

During the Anti-Japanese War, vast areas of North China Plain on a large scale tunnel warfare. In general, through the village tunnel connected in a row. In addition to the two ends, each directly connected to the two villages neighboring villages. 
Intruder attacks often for some villages and destroyed part of the tunnel of them. Eighth Route Army commanders and village requirements of the latest tunnel connection status. If some villages severely isolation, you must restore the connection immediately!

Input

The first input line comprises two positive integers n and m (n, m≤50,000), it represents the number of events and villages. Each row of the m-th row next describe an event. 
Different format shown below describes three different events: 
D x: x-villages were destroyed. 
Q x: x-Commander inquiry number villages villages directly or indirectly related thereto. 
R: final destruction of villages have been rebuilt.

Output

The output of each commander in order answer to the query.

Sample Input

7 9
D 3
D 6
D 5
Q 4
Q 5
R
Q 4
R
Q 4

Sample Output

1 
0 
2 
. 4 

Analysis:
Maintenance interval corresponding to each node left the longest string of 1, up to the right string and a string of the longest interval. If the current query time interval the maximum run length of 0 or 1 full, directly returns 0 \ full; if x is in the current section "Central" (right-left and right children of the left child connected together), the length of the central return directly ; in addition to continue to search.

Code:
 1 #include <bits/stdc++.h>//题目也没说多组输入啊,WA了好多次,想哭
 2 using namespace std;
 3 const int maxn = 5 * 1e4 + 10;
 4 struct node
 5 {
 6     int l, r;
 7     int ln, rn, mn;
 8 }t[maxn << 2];
 9 
10 int n, m;
11 
12 void pushup(int tar)
13 {
14     t[tar].ln = t[tar << 1].ln, t[tar].rn = t[tar << 1 | 1].rn;
15     t[tar].mn = max(t[tar << 1].mn, t[tar << 1 | 1].mn);
16     t[tar].mn = max(t[tar].mn, t[tar << 1].rn + t[tar << 1 | 1].ln);
17     if (t[tar << 1].ln == t[tar << 1].r - t[tar << 1].l + 1) t[tar].ln = t[tar << 1].ln + t[tar << 1 | 1].ln;
18     if (t[tar << 1 | 1].rn == t[tar << 1 | 1].r - t[tar << 1 | 1].l + 1) t[tar].rn = t[tar << 1 | 1].rn + t[tar << 1].rn;
19 }
20 
21 void build(int l, int r, int tar)
22 {
23     t[tar].l = l, t[tar].r = r;
24     t[tar].ln = t[tar].rn = t[tar].mn = r - l + 1;
25     if (l == r) return;
26     int mid = (l + r) >> 1;
27     build(l, mid, tar << 1);
28     build(mid + 1, r, tar << 1 | 1);
29 }
30 
31 void update(int x, int state, int tar)
32 {
33     if (t[tar].l == t[tar].r)
34     {
35         t[tar].ln = t[tar].rn = t[tar].mn = state;
36         return;
37     }
38     int mid = (t[tar].l + t[tar].r) >> 1;
39     if (x <= mid) update(x, state, tar << 1);
40     else if (x > mid) update(x, state, tar << 1 | 1);
41     pushup(tar);
42 }
43 
44 int query(int x, int tar)
45 {
46     if (t[tar].mn == 0 || t[tar].mn == t[tar].r - t[tar].l + 1)
47         return t[tar].mn;
48     int mid = (t[tar].l + t[tar].r) >> 1;
49     if (x >= t[tar << 1].r - t[tar << 1].rn + 1 && x <= t[tar << 1 | 1].l + t[tar << 1 | 1].ln - 1) return t[tar << 1].rn + t[tar << 1 | 1].ln;
50     else if (x <= mid) return query(x, tar << 1);
51     else return query(x, tar << 1 | 1);
52 }
53 
54 int main()
55 {
56     int n, m;
57 
58     while (cin >> n >> m)
59     {
60         stack<int> s;
61         char ope[2];
62         int x;
63 
64         build(1, n, 1);
65         while (m--)
66         {
67             cin >> ope;
68             if (ope[0] == 'D')
69             {
70                 cin >> x;
71                 s.push(x);
72                 update(x, 0, 1);
73             }
74             else if (ope[0] == 'R')
75             {
76                 x = s.top();
77                 s.pop();
78                 update(x, 1, 1);
79             }
80             else
81             {
82                 cin >> x;
83                 cout << query(x, 1) << endl;
84             }
85         }
86     }
87 }
 

 

 

Guess you like

Origin www.cnblogs.com/liuwenhan/p/11334603.html