HDU 1540 Tunnel Warfare
生活随笔
收集整理的這篇文章主要介紹了
HDU 1540 Tunnel Warfare
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=1540
解題思路:線段樹節點增加一個ld----區間左端點起始的最大長度,rd----區間右端點起始的最大長度,md----覆蓋區間中點的最大長度,那么對于每次更新,更新到點然后更新父節點值;對于每次查詢,若為中點,則返回md,否則從左右孩子中選擇。需要注意的是ld,rd與md可能會有重疊部分,因此更新與查詢的時候需要判斷一下。
代碼:
1 const int maxn = 5e4 + 5; 2 3 int ld[maxn * 4], rd[maxn * 4], md[maxn * 4]; 4 int ok[maxn]; 5 int n, m; 6 stack<int> s; 7 8 void build(int l, int r, int k){ 9 ld[k] = rd[k] = md[k] = (r - l + 1); 10 if(l == r) return; 11 int mid = (l + r) >> 1, lc = k << 1, rc = k << 1 | 1; 12 build(l, mid, lc); 13 build(mid + 1, r, rc); 14 } 15 void update(int u, int x, int l, int r, int k){ 16 if(u == l && l == r){ 17 if(x == 0) 18 ld[k] = rd[k] = md[k] = 0; 19 else 20 ld[k] = rd[k] = md[k] = 1; 21 return; 22 } 23 int mid = (l + r) >> 1, lc = k << 1, rc = k << 1 | 1; 24 if(u <= mid) update(u, x, l, mid, lc); 25 else update(u, x, mid + 1, r, rc); 26 ld[k] = (ld[lc] >= mid - l + 1)? ld[lc] + ld[rc]: ld[lc]; 27 rd[k] = (rd[rc] >= r - mid)? rd[rc] + rd[lc]: rd[rc]; 28 md[k] = rd[lc] == 0? 0: ld[rc] + rd[lc]; 29 } 30 int query(int u, int l, int r, int k){ 31 int mid = (l + r) >> 1, lc = k << 1, rc = k << 1 | 1; 32 if(u == mid) return md[k]; 33 if(u <= mid) { 34 int q1 = query(u, l, mid, lc); 35 if(rd[lc] >= mid - u + 1) q1 += ld[rc]; 36 return q1; 37 } 38 else{ 39 int q2 = query(u, mid + 1, r, rc); 40 if(ld[rc] >= u - mid) q2 += rd[lc]; 41 return q2; 42 } 43 } 44 int main(){ 45 while(scanf("%d %d", &n, &m) != EOF){ 46 while(!s.empty()) s.pop(); 47 build(1, n, 1); 48 for(int i = 1; i <= n; i++) ok[i] = 1; 49 while(m--){ 50 char ch; 51 scanf(" %c", &ch); 52 if(ch == 'D'){ 53 int u; 54 scanf("%d", &u); 55 if(ok[u]) update(u, 0, 1, n, 1); 56 ok[u] = 0; 57 s.push(u); 58 } 59 else if(ch == 'Q'){ 60 int u; 61 scanf("%d", &u); 62 int tmans = query(u, 1, n, 1); 63 printf("%d\n", tmans); 64 } 65 else{ 66 if(!s.empty()){ 67 int u = s.top(); 68 s.pop(); 69 if(!ok[u]) update(u, 1, 1, n, 1); 70 ok[u] = 1; 71 } 72 } 73 } 74 } 75 }題目:
Tunnel Warfare
Time Limit: 4000/2000 MS (Java/Others)????Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9320????Accepted Submission(s): 3633
Frequently the invaders launched attack on some of the villages and destroyed the parts of tunnels in them. The Eighth Route Army commanders requested the latest connection state of the tunnels and villages. If some villages 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 villages 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 villages 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 9 D 3 D 6 D 5 Q 4 Q 5 R Q 4 R Q 4 ??
Sample Output 1 0 2 4 ??
Source POJ Monthly?
轉載于:https://www.cnblogs.com/bolderic/p/7302645.html
總結
以上是生活随笔為你收集整理的HDU 1540 Tunnel Warfare的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Beaglebone Black USB
- 下一篇: Python__random模块