• <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)  編輯 收藏 引用

            久久久久国产成人精品亚洲午夜| 久久综合久久鬼色| 久久天堂AV综合合色蜜桃网 | 久久精品这里热有精品| 国产精品免费久久久久影院| 亚洲伊人久久综合中文成人网| 久久精品亚洲AV久久久无码| 久久精品国产99国产电影网| 久久受www免费人成_看片中文| 77777亚洲午夜久久多喷| 亚洲色欲久久久久综合网| 精品久久久久久中文字幕| 香蕉99久久国产综合精品宅男自| 久久久久无码精品国产不卡| 久久精品成人| 久久精品一区二区国产| 久久久久国产精品嫩草影院| 精品久久久久一区二区三区| 国产高潮国产高潮久久久| 国内高清久久久久久| 国产精品嫩草影院久久| 2021久久国自产拍精品| 久久久一本精品99久久精品88| 午夜精品久久久久久影视777| 久久精品视频网| 99国产精品久久| 青青草原精品99久久精品66| 国产偷久久久精品专区| 久久久久精品国产亚洲AV无码| 久久av免费天堂小草播放| 国内精品久久久久影院网站| 99热精品久久只有精品| 香港aa三级久久三级| 品成人欧美大片久久国产欧美...| 国产一区二区三区久久精品| 99精品久久精品一区二区| 狠狠色丁香久久综合五月| 久久99国产亚洲高清观看首页| 久久精品免费观看| 热re99久久精品国产99热| 国产精品狼人久久久久影院|