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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

poj 2892---Tunnel Warfare(线段树单点更新、区间合并)

發布時間:2025/4/9 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 poj 2892---Tunnel Warfare(线段树单点更新、区间合并) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接

?

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

    Hint

    An illustration of the sample input:

    OOOOOOO
    D 3 OOXOOOO
    D 6 OOXOOXO
    D 5 OOXOXXO
    R OOXOOXO
    R OOXOOOO

    Source

    POJ Monthly--2006.07.30, updog 題意:有n個村莊編號為1,2,3...n 它們按照序號一一相連,現在有m次操作,有以下幾種操作: 1、D ?x ?表示將x號村莊摧毀。 2、Q ?x ?表示查詢x村莊能到達的村莊數(包括x村莊)。 3、R ? ? ?表示修復最近一個被摧毀的村莊。 每次查詢輸出一個值。 思路:線段樹單點更新、區間合并,用棧存儲被摧毀的村莊號。 代碼如下: #include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <stack> using namespace std; const int maxn=50005; stack<int> s; struct Node{int l,r,m; }tr[4*maxn];void build(int i,int l,int r) {tr[i].l=tr[i].r=tr[i].m=r-l+1;if(l==r) return;int mid=(l+r)/2;build(2*i,l,mid);build(2*i+1,mid+1,r); }void update(int i,int l,int r,int x,int y) {if(l==r){tr[i].l=tr[i].r=tr[i].m=y;return;}int mid=(l+r)/2;if(x<=mid) update(2*i,l,mid,x,y);else update(2*i+1,mid+1,r,x,y);if(tr[2*i].m==mid-l+1) tr[i].l=tr[2*i].m+tr[2*i+1].l;else tr[i].l=tr[2*i].l;if(tr[2*i+1].m==r-mid) tr[i].r=tr[2*i+1].m+tr[2*i].r;else tr[i].r=tr[2*i+1].r;tr[i].m=max(max(tr[2*i].m,tr[2*i+1].m),tr[2*i].r+tr[2*i+1].l); }int query(int i,int l,int r,int x) {int sum=0;if(l==r) return tr[i].m;if(r-l+1==tr[i].m) return tr[i].m;int mid=(l+r)/2;if(x<=mid){if(mid-tr[2*i].r+1<=x)return tr[2*i].r+tr[2*i+1].l;else return query(2*i,l,mid,x);}else {if(tr[2*i+1].l+mid>=x)return tr[2*i].r+tr[2*i+1].l;else return query(2*i+1,mid+1,r,x);} }int main() {int n,m;while(scanf("%d",&n)!=EOF){scanf("%d",&m);build(1,1,n);int x;char str[5];while(!s.empty()) s.pop();while(m--){scanf("%s",str);if(str[0]=='D'){scanf("%d",&x);s.push(x);update(1,1,n,x,0);}else if(str[0]=='Q'){scanf("%d",&x);printf("%d\n",query(1,1,n,x));}else{update(1,1,n,s.top(),1);s.pop();}}}return 0; }

    ?

    轉載于:https://www.cnblogs.com/chen9510/p/6568385.html

    《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

    總結

    以上是生活随笔為你收集整理的poj 2892---Tunnel Warfare(线段树单点更新、区间合并)的全部內容,希望文章能夠幫你解決所遇到的問題。

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