• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            ACM___________________________

            ______________白白の屋
            posts - 182, comments - 102, trackbacks - 0, articles - 0
            <2010年10月>
            262728293012
            3456789
            10111213141516
            17181920212223
            24252627282930
            31123456

            常用鏈接

            留言簿(24)

            隨筆分類(332)

            隨筆檔案(182)

            FRIENDS

            搜索

            積分與排名

            最新隨筆

            最新評論

            閱讀排行榜

            評論排行榜

            MiYu原創, 轉帖請注明 : 轉載自 ______________白白の屋    

             

            題目地址:

            http://acm.hdu.edu.cn/showproblem.php?pid=1540

            題目描述:

            Tunnel Warfare

            Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
            Total Submission(s): 1009    Accepted Submission(s): 334


            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
             

             題目分析:

            題目有三種操作 :

            D:  摧毀村莊

            Q: 查詢相連的村莊

            R: 修復上次被摧毀的村莊

            這個題目的關鍵部分就是 對線段的修改部分, 也是最難的部分, 這部分理解了, 這個題目就基本會了.

              在結構體里面, 需要保存 3個量, lVal : 從線段左端點能夠向右延伸的最大長度,

                 rVal:從線段右端點能夠向左延伸的最大長度,

                mVal:當前線段的最大連續長度

                     seg[rt].mVal = max ( seg[LL].rVal + seg[RR].lVal, max ( seg[LL].mVal, seg[RR].mVal ) ); //當前節點的最大連續長度 

                     seg[rt].lVal = seg[LL].lVal + ( seg[LL].cov() ? seg[RR].lVal : 0 );   //當前節點的左端點能夠向右延伸的最大長度 

                     seg[rt].rVal = seg[RR].rVal + ( seg[RR].cov() ? seg[LL].rVal : 0 );   //當前節點的右端點能夠向左延伸的最大長度  

            查詢的時候也要注意幾種 情況 :  1 :  就是當前整個線段

                  2 :  橫跨 左右子樹

                  3 :  只在 左 或 右 子樹

            詳細請看代碼注釋. 

            代碼如下 : 

            代碼
            /*
            Mail to   : miyubai@gamil.com
            Link      : http://www.cnblogs.com/MiYu  || http://www.shnenglu.com/MiYu
            Author By : MiYu
            Test      : 1
            Complier  : g++ mingw32-3.4.2
            Program   : 1540
            Doc Name  : Tunnel Warfare
            */
            //#pragma warning( disable:4789 )
            #include <iostream>
            #include <fstream>
            #include <sstream>
            #include <algorithm>
            #include <string>
            #include <set>
            #include <map>
            #include <utility>
            #include <queue>
            #include <stack>
            #include <list>
            #include <vector>
            #include <cstdio>
            #include <cstdlib>
            #include <cstring>
            #include <cmath>
            #include <ctime>
            using namespace std;
            inline int max ( int &a, int &b ) {
                   
            return a > b ? a : b;       
            }
            struct segTree {
                   
            int left, right, lVal, rVal, mVal; //lVal: 從線段左端點能夠向右延伸的最大長度
                                                      
            //rVal: 從線段右端點能夠向左延伸的最大長度 
                                                      
            //mVal: 當前線段的最大連續長度 
                   int mid () { return (left + right)>>1;}  
                   
            int dis () { return right-left+1; }  // 線段的長度 
                   void prs (int flag) { lVal = rVal = mVal = flag ? 0 : dis(); }   //按flag標記處理線段 
                   bool cov () { return mVal == dis(); }   // 當前線段是否被覆蓋 , 即 這條線段上的點沒有任何一點被破壞 
            }seg[160000];
            inline void creat ( int left, int right, int rt = 1 ) {
                 
            int LL = rt << 1, RR = rt << 1 | 1
                 seg[rt].left = left;
                 seg[rt].right = right;
                 seg[rt].prs (0);
                 
            if ( left == right ) return
                 
            int mid = seg[rt].mid();      
                 creat ( left, mid, LL );
                 creat ( mid + 1, right, RR );     
            }
            inline void modify ( int pos, int flag, int rt = 1 ) {
                 
            int LL = rt << 1, RR = rt << 1 | 1
                 
            if ( seg[rt].left == seg[rt].right ) {
                      seg[rt].prs ( flag ); return;      
                 } 
                 
            int mid = seg[rt].mid();
                 
            if ( pos <= mid ) modify ( pos, flag, LL );
                 
            else modify ( pos, flag, RR );
                 
            // 經典部分: 
                 seg[rt].mVal = max ( seg[LL].rVal + seg[RR].lVal, max ( seg[LL].mVal, seg[RR].mVal ) ); //當前節點的最大連續長度 
                 seg[rt].lVal = seg[LL].lVal + ( seg[LL].cov() ? seg[RR].lVal : 0 );   //當前節點的左端點能夠向右延伸的最大長度 
                 seg[rt].rVal = seg[RR].rVal + ( seg[RR].cov() ? seg[LL].rVal : 0 );   //當前節點的右端點能夠向左延伸的最大長度 
            }
            inline int query ( int pos, int rt = 1 ) {
                
            if ( seg[rt].cov() || seg[rt].mVal == 0 || seg[rt].left == seg[rt].right )
                    
            return seg[rt].mVal;
                
            int LL = rt << 1, RR = rt << 1 | 1, mid = seg[rt].mid();        
                
            if ( pos <= mid ) {
                     
            if ( pos > mid - seg[LL].rVal )  // 查詢節點在左孩子節點所處的位置 
                         return seg[LL].rVal + query ( mid+1, RR ); // 如果在線段的右端, 則還需要查詢右孩子節點的左端 
                     else return query ( pos, LL );    //左端只需查詢左端 
                } else {
                     
            if ( pos <= mid + seg[RR].lVal )  // 同上 
                         return seg[RR].lVal + query ( mid, LL );
                     
            else return query ( pos, RR );   
                }

            inline int _getint(int &n){  //輸入加速 
             char c;for(c=getchar();!isdigit(c);c=getchar());for(;isdigit(c);c=getchar())n=n*10+c-'0';return n;
            }
            deque <int> st; // stack 沒有clear() 杯具了 
            int main ()
            {
                
            int N, M;
                
            char ask[3];
                
            while ( scanf ( "%d%d"&N, &M ) == 2 ) {
                       creat ( 1, N );
                       st.clear();
                       
            while ( M -- ) {
                              scanf ( "%s",ask ); N = 0;
                              
            switch ( ask[0] ) {
                                     
            case 'D':   _getint (N);
                                                 modify ( N, 1 );
                                                 st.push_back ( N );
                                                 
            break;
                                     
            case 'Q':   _getint (N);          
                                                 printf ( "%d\n",query ( N ) );        
                                                 
            break;
                                     
            case 'R':   if ( st.empty() ) break;
                                                 modify ( st.back(), 0 );
                                                 st.pop_back();
                              }
                       }
                }
                
            return 0;
            }

             

            久久久久亚洲AV成人网人人软件| 中文字幕无码免费久久| 久久精品99久久香蕉国产色戒| 国产免费久久精品丫丫| 久久精品女人天堂AV麻| 99久久精品费精品国产| 日韩人妻无码一区二区三区久久99 | 久久精品中文字幕一区| 色婷婷综合久久久久中文字幕| 超级97碰碰碰碰久久久久最新| 亚洲AV日韩精品久久久久久| 久久久亚洲欧洲日产国码二区 | 久久久久久A亚洲欧洲AV冫| 精品无码久久久久久久动漫| 久久精品中文字幕一区| 精品久久久久久无码免费| 一本久久a久久精品vr综合| A级毛片无码久久精品免费| 无码国内精品久久人妻| 成人精品一区二区久久久| 久久久精品人妻一区二区三区蜜桃 | 无码任你躁久久久久久久| 久久精品国产99久久无毒不卡 | 久久这里有精品视频| AV色综合久久天堂AV色综合在| 日日狠狠久久偷偷色综合96蜜桃 | 精品久久久久中文字| 国产偷久久久精品专区| 天堂无码久久综合东京热| 国产福利电影一区二区三区久久久久成人精品综合 | 理论片午午伦夜理片久久| 欧美伊香蕉久久综合类网站| 色综合久久久久久久久五月| 少妇人妻综合久久中文字幕| 亚洲午夜精品久久久久久app| 国产午夜精品久久久久九九电影| 国内精品九九久久久精品| 久久久久久久久久久久中文字幕| 久久99国产精品久久99小说| 亚洲精品国产自在久久| 欧美激情精品久久久久久|