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

             

            日本精品久久久久中文字幕| 久久99精品久久久久婷婷| 久久久久免费精品国产| 久久国产精品成人影院| 91久久精品国产免费直播| 久久av免费天堂小草播放| 精品国产青草久久久久福利| 亚洲综合伊人久久综合| 久久精品国产亚洲AV高清热| 国产成人AV综合久久| 久久久久亚洲国产| 99久久久久| 久久精品国产亚洲av影院| 人人狠狠综合久久亚洲| 国产麻豆精品久久一二三| 尹人香蕉久久99天天拍| 99久久99这里只有免费费精品| 九九久久99综合一区二区| 国产精品久久新婚兰兰| 国产一级做a爰片久久毛片| 久久亚洲国产成人影院| 国产精品伊人久久伊人电影 | 久久九色综合九色99伊人| 久久精品国产免费观看三人同眠| 国产成人AV综合久久| 久久精品一区二区| 久久久久亚洲AV无码网站| 国产精品成人久久久| 手机看片久久高清国产日韩| 国产精品狼人久久久久影院 | 久久精品国产第一区二区三区 | 97久久香蕉国产线看观看| 久久国产免费直播| 人妻无码αv中文字幕久久琪琪布| 久久久久亚洲AV无码专区网站| 国产综合精品久久亚洲| 久久97久久97精品免视看秋霞| 国内精品久久久久久麻豆 | 中文字幕无码久久人妻| 亚洲国产小视频精品久久久三级| 国产亚州精品女人久久久久久 |