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

            久久久久九国产精品| 国产精品福利一区二区久久| 久久国产香蕉一区精品| 久久成人18免费网站| 久久精品综合网| 色偷偷888欧美精品久久久| 久久精品国产第一区二区| 亚洲综合伊人久久综合| 91精品国产高清久久久久久国产嫩草 | 久久伊人五月天论坛| 欧美熟妇另类久久久久久不卡| 久久99国产精品一区二区| 日韩电影久久久被窝网| 国产三级久久久精品麻豆三级| 国内精品久久久久国产盗摄| 亚洲精品乱码久久久久久蜜桃不卡| 99久久婷婷国产一区二区| 伊人久久大香线蕉亚洲五月天| 国产ww久久久久久久久久| 久久久噜噜噜久久中文福利| 久久久久久国产精品免费免费| 久久成人国产精品| 久久精品国产亚洲AV影院| 国产福利电影一区二区三区,免费久久久久久久精 | 久久综合色老色| 美女写真久久影院| AAA级久久久精品无码片| 伊人久久五月天| 久久无码人妻精品一区二区三区| 国产精品无码久久综合| 亚洲欧美日韩久久精品第一区| 亚洲国产精品嫩草影院久久 | 久久久久九九精品影院| 大蕉久久伊人中文字幕| 91麻精品国产91久久久久| 国产精品一久久香蕉产线看| 漂亮人妻被黑人久久精品| 久久综合综合久久综合| 久久久一本精品99久久精品66| 中文字幕久久波多野结衣av| 伊人久久大香线焦AV综合影院|