日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HDU 1540 Tunnel Warfare

發布時間:2025/3/15 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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


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, villages connected by tunnels lay in a line. Except the two at the ends, every village was directly connected with two neighboring ones.

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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。