• <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>

            The Fourth Dimension Space

            枯葉北風寒,忽然年以殘,念往昔,語默心酸。二十光陰無一物,韶光賤,寐難安; 不畏形影單,道途阻且慢,哪曲折,如渡飛湍。斬浪劈波酬壯志,同把酒,共言歡! -如夢令

            POJ 3468 A Simple Problem with Integers Splay樹區間操作

                接連做了維修數列和這個題,看來線段樹能做的區間操作,用splay樹應該也是可以的(都是打一個延遲標記嘛),做這題時發現有一點很重要,就是當我們壓入延遲標記的時候(不管是push_down還是C a,b,c時都需要)一定要保證這個結點代表區間的最新信息。
               
            #include <iostream>
            using namespace std;
            int const maxn=300000;

            #define bint __int64
            struct node
            {
                
            int value,size;
                
            int add;
                bint sum;
                node 
            *ch[2], *pre;
            }
            tree[maxn],*Null, *root;//設置一個Null指針是一個亮點

            int num[maxn];
            int n,m;

            int top=0;
            node 
            *New_Node(int x)
            {
                node 
            *p=&tree[top++];
                p
            ->ch[0]=p->ch[1]=p->pre=Null;
                p
            ->add=0;
                p
            ->sum=x;
                p
            ->size=1;
                p
            ->value=x;
                
            return p;
            }


            void Push_Down(node *x)//下壓延遲標記后要將子樹信息更新到最新
            {
                
            if (x == Null) return;
                x
            ->value+=x->add;
                x
            ->ch[0]->add+=x->add;
                x
            ->ch[1]->add+=x->add;
                
            if(x->ch[0]!=Null)
                    x
            ->ch[0]->sum+=(bint)(x->add)*(x->ch[0]->size);
                
            if(x->ch[1]!=Null)
                    x
            ->ch[1]->sum+=(bint)(x->add)*(x->ch[1]->size);
                x
            ->add=0;
            }


            void Update(node *x)
            {
                
            if (x == Null) return;
                x
            ->size = x->ch[0]->size + x->ch[1]->size + 1;
                x
            ->sum = (bint)x->value+ x->ch[0]->sum + x->ch[1]->sum;
            }


            void Rotate(node *x, int c)
            {
                node 
            *= x->pre;
                Push_Down(y), Push_Down(x);
                y
            ->ch[! c] = x->ch[c], x->pre = y->pre;
                
            if (x->ch[c] != Null) x->ch[c]->pre = y;
                
            if (y->pre != Null) y->pre->ch[y->pre->ch[1== y] = x;
                y
            ->pre = x, x->ch[c] = y;
                
            if (y == root) root = x;
                Update(y);
            }


            void Splay(node *x, node *f)
            {
                
            for (Push_Down(x); x->pre != f; )
                
            {
                    
            if (x->pre->pre == f)
                        Rotate(x, x
            ->pre->ch[0== x);
                    
            else
                    
            {
                        node 
            *= x->pre, *= y->pre;
                        
            if (z->ch[0== y)
                            
            if (y->ch[0== x)
                                Rotate(y, 
            1), Rotate(x, 1);
                            
            else
                                Rotate(x, 
            0), Rotate(x, 1);
                        
            else
                            
            if (y->ch[1== x)
                                Rotate(y, 
            0), Rotate(x, 0);
                            
            else
                                Rotate(x, 
            1), Rotate(x, 0);
                    }

                }

                Update(x);
            }


            void Select(int k, node *f)
            {
                node 
            *now=root;
                
            while(true)
                
            {
                    Push_Down(now);
                    
            int tmp = now->ch[0]->size;
                    
            if (tmp + 1 == k) break;
                    
            if (k <= tmp)
                        now 
            = now->ch[0];
                    
            else
                        now 
            = now->ch[1], k -= tmp + 1;
                }

                Splay(now, f);
            }




            node 
            *Make_Tree(int l, int r, node *fa)
            {
                
            if (l > r) return Null;
                
            int mid = l + r >> 1;
                node 
            *= New_Node(num[mid]);
                p
            ->ch[0= Make_Tree(l, mid-1, p);
                p
            ->ch[1= Make_Tree(mid+1, r, p);
                p
            ->pre = fa, Update(p);
                
            return p;
            }



            int main()
            {
                top
            =0;
                scanf(
            "%d%d",&n,&m);
                
            for(int i=1;i<=n;i++)
                
            {
                    scanf(
            "%d",&num[i]);
                }

                
                Null
            =New_Node(0);
                Null
            ->size=0;

                root
            =New_Node(-1);
                root
            ->ch[1]=New_Node(-1);
                root
            ->ch[1]->pre=root;
                Update(root);
                
                root
            ->ch[1]->ch[0]=Make_Tree(1,n,root->ch[1]);

                
            char s[2];
                
            for(int i=0;i<m;i++)
                
            {

                    scanf(
            "%s",s);
                    
            if(s[0]=='C')
                    
            {
                        
            int a,b,c;
                        scanf(
            "%d%d%d",&a,&b,&c);
                        Select(a,Null);
                        Select(b
            +2,root);
                        root
            ->ch[1]->ch[0]->add+=c;
                        root
            ->ch[1]->ch[0]->sum+=(bint)c*(root->ch[1]->ch[0]->size);
                    }

                    
            else
                    
            {
                        
            int a,b;
                        scanf(
            "%d%d",&a,&b);
                        Select(a,Null);
                        Select(b
            +2,root);
                        printf(
            "%I64d\n",root->ch[1]->ch[0]->sum);
                    }

                }

                
            return 0;
            }








            posted on 2010-10-22 22:26 abilitytao 閱讀(2025) 評論(0)  編輯 收藏 引用

            亚洲狠狠婷婷综合久久蜜芽| 久久久婷婷五月亚洲97号色| AV无码久久久久不卡蜜桃| 99久久婷婷免费国产综合精品| 亚洲а∨天堂久久精品9966| 亚洲&#228;v永久无码精品天堂久久 | 91精品国产91久久综合| 久久免费的精品国产V∧| 国产精品99久久久久久人| 91亚洲国产成人久久精品网址 | www.久久热.com| 欧美综合天天夜夜久久| 久久精品国产一区二区三区| 久久精品国产久精国产果冻传媒| 久久精品国产乱子伦| 99久久精品免费看国产免费| 天堂无码久久综合东京热| 91精品国产综合久久婷婷| 欧美无乱码久久久免费午夜一区二区三区中文字幕 | 一本色综合网久久| 一本伊大人香蕉久久网手机| 久久亚洲精品无码VA大香大香| 国产精品99久久久久久人| 伊人色综合九久久天天蜜桃| 久久精品男人影院| 精品熟女少妇AV免费久久 | 久久久久人妻一区二区三区| 99久久伊人精品综合观看| 一本久久a久久精品亚洲| 久久性生大片免费观看性| 久久精品这里热有精品| 久久婷婷国产综合精品| 久久久久国产精品人妻| 欧美国产精品久久高清| 亚洲午夜久久久精品影院| 久久国产精品一国产精品金尊| 伊人久久大香线蕉综合5g| 久久免费观看视频| 国内精品久久久久久久久电影网 | 久久乐国产综合亚洲精品| 国产精品美女久久久网AV|