• <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://m.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;
            }

             

            久久综合久久综合亚洲| 日韩乱码人妻无码中文字幕久久 | 久久精品国产影库免费看| 亚洲国产精品无码久久久不卡 | 久久91精品久久91综合| 亚洲一区中文字幕久久| 狠狠色丁香久久婷婷综合蜜芽五月| 国内精品人妻无码久久久影院导航 | 囯产精品久久久久久久久蜜桃| 欧美熟妇另类久久久久久不卡 | 久久国产精品99精品国产| 国产精品亚洲美女久久久| 精品综合久久久久久98| 国产AV影片久久久久久| 久久国产精品无| 久久亚洲综合色一区二区三区| 亚洲欧美国产日韩综合久久| 女人香蕉久久**毛片精品| 中文字幕人妻色偷偷久久| 久久播电影网| 婷婷综合久久狠狠色99h| 久久久久久精品久久久久| 日韩十八禁一区二区久久| 久久综合综合久久狠狠狠97色88| 久久无码高潮喷水| 97精品伊人久久大香线蕉| 国产精品久久久久乳精品爆 | 国产成人精品白浆久久69| 色青青草原桃花久久综合| 精品国产热久久久福利| 99久久国产热无码精品免费| 亚洲精品无码久久久久久| 精品多毛少妇人妻AV免费久久| 久久久久国色AV免费看图片| 久久久久无码专区亚洲av| 久久播电影网| 久久成人国产精品一区二区| 久久综合亚洲色HEZYO国产| 国产农村妇女毛片精品久久| 久久国产视频99电影| 亚洲国产小视频精品久久久三级|