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

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

            POJ 3468 A Simple Problem with Integers Splay樹區(qū)間操作

                接連做了維修數(shù)列和這個題,看來線段樹能做的區(qū)間操作,用splay樹應(yīng)該也是可以的(都是打一個延遲標(biāo)記嘛),做這題時發(fā)現(xiàn)有一點很重要,就是當(dāng)我們壓入延遲標(biāo)記的時候(不管是push_down還是C a,b,c時都需要)一定要保證這個結(jié)點代表區(qū)間的最新信息。
               
            #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;//設(shè)置一個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)//下壓延遲標(biāo)記后要將子樹信息更新到最新
            {
                
            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 閱讀(2021) 評論(0)  編輯 收藏 引用


            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            精品国产99久久久久久麻豆 | 青青草原综合久久大伊人导航 | 久久久久久午夜精品| 久久精品中文无码资源站| 91精品国产乱码久久久久久| 国产国产成人久久精品| 久久精品国产免费观看| 欧美精品一本久久男人的天堂| 一级做a爰片久久毛片免费陪| 久久99精品久久久久久hb无码| 麻豆久久| 久久综合综合久久97色| 狠狠色丁香久久婷婷综合| 久久久久国产精品嫩草影院| 日韩精品无码久久久久久| 日日狠狠久久偷偷色综合免费 | 狠狠色丁香婷婷久久综合不卡| 亚洲а∨天堂久久精品9966| 国产精品久久永久免费| 亚洲av伊人久久综合密臀性色| 久久久久亚洲AV无码专区桃色 | 久久青青草原精品国产不卡| 久久精品国产亚洲AV电影| 久久久久亚洲AV成人网人人网站| 91久久精品电影| 久久精品aⅴ无码中文字字幕不卡 久久精品aⅴ无码中文字字幕重口 | 久久久精品人妻一区二区三区蜜桃 | 偷偷做久久久久网站| 久久精品国产色蜜蜜麻豆| 国产美女久久久| 久久精品毛片免费观看| 嫩草伊人久久精品少妇AV| 精品国产乱码久久久久久呢| 久久久黄色大片| 久久精品中文无码资源站| 九九精品久久久久久噜噜| 久久久久一本毛久久久| 国产午夜精品久久久久九九| 久久综合中文字幕| 99热成人精品免费久久| 久久男人AV资源网站|