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

            ArcTan

            dfs
            隨筆 - 16, 文章 - 117, 評論 - 6, 引用 - 0
            數(shù)據(jù)加載中……

            poj 3468 線段樹

            http://poj.org/problem?id=3468

            線段數(shù)的構(gòu)造,查詢,修改!
            struct node{
               int l,r,m;
               long long sum;//當(dāng)前區(qū)間的和
               long long add;//區(qū)間需要加上的數(shù)值
            }

            題意:
                  給定n個(gè)數(shù)字A1,A2,..,An,Q個(gè)操作。
                  C x y z,把區(qū)間x y內(nèi)的數(shù)都加上z
                  Q x y,求區(qū)間x y內(nèi)所有數(shù)的和。
            總結(jié):
                  基礎(chǔ)代碼一定要對,調(diào)試了半天,最后發(fā)現(xiàn)是swap(x,y,t)寫錯(cuò)了!!!!
                  WA了一次,數(shù)據(jù)是long long才行,要分析清楚數(shù)據(jù)范圍!!!

            #include <cstdio>
            #include 
            <cstdlib>
            #include 
            <cstring>
            #include 
            <cmath>
            #include 
            <ctime>
            #include 
            <cassert>
            #include 
            <iostream>
            #include 
            <sstream>
            #include 
            <fstream>
            #include 
            <map>
            #include 
            <set>
            #include 
            <vector>
            #include 
            <queue>
            #include 
            <algorithm>
            #define min(x,y) (x<y?x:y)
            #define max(x,y) (x>y?x:y)
            #define swap(x,y,t) (t=x,x=y,y=t)
            #define clr(list) memset(list,0,sizeof(list))
            #define LL long long
            #define maxn 200005
            using namespace std;
            struct node{
                
            int l,r,m;
                LL sum;
                LL add;
            } tree[
            2*maxn];
            int a[maxn];
            LL build(
            int l,int r,int root)
            {
                tree[root].l
            =l;
                tree[root].r
            =r;
                tree[root].m
            =(l+r)/2;
                tree[root].add
            =0;
                
            if (l==r)
                {
                    tree[root].sum
            =a[l];
                    
            return tree[root].sum;
                }
                
            long long sum_l=build(l,(l+r)/2,root*2);
                
            long long sum_r=build((l+r)/2+1,r,root*2+1);
                tree[root].sum
            =sum_l+sum_r;
                
            return tree[root].sum;
            }
            int insert(int l,int r,int add,int root)
            {
                
            if (l<=tree[root].l && r>=tree[root].r)
                {
                    tree[root].add
            +=add;
                    tree[root].sum
            +=add * (tree[root].r-tree[root].l+1);
                    
            return 0;
                }
                
            if (tree[root].add) //向下更新
                {
                    tree[
            2*root].sum+=tree[root].add * (tree[root*2].r-tree[root*2].l+1);
                    tree[
            2*root].add+=tree[root].add;
                    tree[
            2*root+1].sum+=tree[root].add * (tree[root*2+1].r-tree[root*2+1].l+1);
                    tree[
            2*root+1].add+=tree[root].add;
                    tree[root].add
            =0;
                }
                
            if (l<=tree[root].m)
                    insert(l,r,add,root
            *2);
                
            if (r>tree[root].m)
                    insert(l,r,add,root
            *2+1);
                tree[root].sum
            =tree[root*2].sum+tree[root*2+1].sum;   //這里回溯啊
                return 0;
            }
            LL search(
            int l,int r,int root)
            {
                
            if (l<=tree[root].l && r>=tree[root].r)
                    
            return tree[root].sum;
                
            if (tree[root].add)  //向下更新
                {
                    tree[
            2*root].sum+=tree[root].add * (tree[root*2].r-tree[root*2].l+1);
                    tree[
            2*root].add+=tree[root].add;
                    tree[
            2*root+1].sum+=tree[root].add * (tree[root*2+1].r-tree[root*2+1].l+1);
                    tree[
            2*root+1].add+=tree[root].add;
                    tree[root].add
            =0;
                }
                LL sum_l
            =0,sum_r=0;
                
            if (l<=tree[root].m)
                    sum_l
            =search(l,r,2*root);
                
            if (r>tree[root].m)
                    sum_r
            =search(l,r,2*root+1);
                
            return sum_l+sum_r;
            }
            int main()
            {
                
            int n,m;
                
            int x,y,z;
                
            int tmp;
                
            char ch;
                scanf(
            "%d%d",&n,&m);
                
            for (int i=1;i<=n;i++)
                    scanf(
            "%d",&a[i]);
                build(
            1,n,1);

                
            for (int i=1;i<=m;i++)
                {
                    scanf(
            "\n%c",&ch);
                    
            if (ch=='C')
                    {
                        scanf(
            "%d%d%d",&x,&y,&z);
                        
            if (x>y)
                            swap(x,y,tmp);
                        insert(x,y,z,
            1);
                    }
                    
            else
                    {
                        scanf(
            "%d%d",&x,&y);
                        
            if (x>y)
                            swap(x,y,tmp);
                        printf(
            "%I64d\n",search(x,y,1));
                    }
                }
                
            return 0;
            }


            posted on 2012-07-25 10:54 wangs 閱讀(407) 評論(0)  編輯 收藏 引用 所屬分類: ACM-數(shù)據(jù)結(jié)構(gòu)

            国产精品内射久久久久欢欢| 狠狠综合久久综合88亚洲| 韩国三级大全久久网站| 久久99国产综合精品| 精品综合久久久久久97超人| 久久成人精品| 久久精品国产2020| 亚洲伊人久久大香线蕉苏妲己| 久久精品国产亚洲7777| 亚洲精品无码久久久影院相关影片| 一本伊大人香蕉久久网手机| 亚洲国产日韩综合久久精品| 久久国产高潮流白浆免费观看| 久久久久久久国产免费看| 久久久精品国产sm调教网站| 久久久久国产日韩精品网站| 99久久婷婷免费国产综合精品| 亚洲国产日韩欧美久久| 无码人妻久久一区二区三区蜜桃| 日日噜噜夜夜狠狠久久丁香五月| 久久播电影网| 99久久精品久久久久久清纯| 国产精品99久久久精品无码| 久久激情亚洲精品无码?V| 久久线看观看精品香蕉国产| 久久精品国产AV一区二区三区| 久久青青国产| 国产综合成人久久大片91| 久久精品国产免费| 国产99精品久久| 久久精品人人槡人妻人人玩AV| 麻豆精品久久久久久久99蜜桃| 久久久久婷婷| 久久人人爽人人澡人人高潮AV | 伊人久久综在合线亚洲2019 | 精品无码久久久久国产动漫3d| 国产精品美女久久久久av爽| 精品综合久久久久久88小说| 99久久精品无码一区二区毛片| 久久99精品久久久久久| 久久香蕉一级毛片|