• <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年9月>
            2930311234
            567891011
            12131415161718
            19202122232425
            262728293012
            3456789

            常用鏈接

            留言簿(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久久丝袜| 国产精品久久久久国产A级| 亚洲精品乱码久久久久久按摩| 欧美性猛交xxxx免费看久久久| 国产精品美女久久久久AV福利| 国产Av激情久久无码天堂| 久久久久亚洲AV无码永不| 一本久久a久久精品亚洲| 久久这里都是精品| 一极黄色视频久久网站| 少妇无套内谢久久久久| 精品国产青草久久久久福利| 三级三级久久三级久久| 无码超乳爆乳中文字幕久久| 97久久国产综合精品女不卡| 亚洲欧美伊人久久综合一区二区| 亚洲AV无码久久| 久久精品国产亚洲av日韩| 99久久精品国产高清一区二区| 狠狠色噜噜狠狠狠狠狠色综合久久| 国产精品久久永久免费| 久久香蕉一级毛片| 国产精品女同一区二区久久| 免费精品久久久久久中文字幕| 精品人妻伦九区久久AAA片69| 久久久久久久久66精品片| 性做久久久久久久久浪潮| 伊人久久无码中文字幕| 97久久天天综合色天天综合色hd | 精品久久人人做人人爽综合| 国产午夜精品久久久久九九电影 | 久久无码专区国产精品发布 | 国产精品久久久久久久久鸭| 国产2021久久精品| 久久久久久久免费视频| 国内精品伊人久久久久av一坑| 91久久成人免费| 色妞色综合久久夜夜| 欧美精品一区二区精品久久|