• <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
            <2025年6月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            293012345

            常用鏈接

            留言簿(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;
            }

             

            久久66热人妻偷产精品9| 欧美伊人久久大香线蕉综合69| 蜜桃麻豆WWW久久囤产精品| 久久精品国产男包| 精品永久久福利一区二区 | 久久久久亚洲精品无码网址 | 久久综合久久美利坚合众国 | 婷婷久久精品国产| 久久亚洲私人国产精品| 久久久WWW成人免费毛片| 久久国产欧美日韩精品| 久久九九亚洲精品| 久久综合国产乱子伦精品免费| 精品久久人人爽天天玩人人妻| 人妻少妇久久中文字幕一区二区| 亚洲伊人久久大香线蕉苏妲己| 久久精品国产亚洲av麻豆图片| 99精品久久久久久久婷婷| 一本色道久久HEZYO无码| 久久国产精品一区| 久久国产精品国产自线拍免费| 久久狠狠爱亚洲综合影院| 日本加勒比久久精品| 久久本道久久综合伊人| 欧美久久综合性欧美| 国产产无码乱码精品久久鸭| 伊人色综合久久天天人守人婷 | 99久久免费国产精品特黄| 久久国产综合精品五月天| 亚洲狠狠综合久久| 中文字幕成人精品久久不卡| 97精品国产91久久久久久| 精品久久久噜噜噜久久久| 久久精品国产清高在天天线| 久久天天躁狠狠躁夜夜躁2014| 青青草国产97免久久费观看| 色偷偷91久久综合噜噜噜噜 | 精品国产一区二区三区久久久狼 | 日本三级久久网| 狠狠色丁香婷婷久久综合不卡| 久久久久人妻一区精品色|