• <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>
            獨立博客: 哲學與程序

            哲學與程序

            ZOJ@3018 二維線段樹

            解法:
            // 2391826      2011-01-24 14:47:58        Accepted      3018      C++      840      17756      redsea
            // 2391828      2011-01-24 14:54:21        Accepted      3018      C++      840      17756      redsea
            #include<stdio.h>
            #include
            <string.h>
            #define MAXN 500000
            #define N 20002
            struct seg
            {
                
            int dx, dy, ux, uy;
                
            int ch[4];
                
            int total;
            }sgt[MAXN];
            int sgtn;
            int ans;
            int w(int x, int y, int dx, int dy, int ux, int uy)
            {
                
            int midx = (dx+ux)/2;
                
            int midy = (dy+uy)/2;
                
            if(x>=dx && x<=midx){
                    
            if(y>=dy && y<=midy)
                        
            return 0;
                    
            else
                        
            return 1;
                }
                
            else
                {
                    
            if(y>=dy && y<=midy)
                        
            return 2;
                    
            else
                        
            return 3;
                }
            }
            int init(int dx, int ux, int dy, int uy)
            {
                sgt[sgtn].dx 
            = dx;
                sgt[sgtn].ux 
            = ux;
                sgt[sgtn].dy 
            = dy;
                sgt[sgtn].uy 
            = uy;
                sgt[sgtn].total 
            = 0;
                
            for(int i = 0; i < 4; i++)
                    sgt[sgtn].ch[i] 
            = -1;
                sgtn
            ++;
                
            return sgtn-1;
            }
            void insert(int root, int x, int y, int a)
            {
                
            if(sgt[root].dx == sgt[root].ux && sgt[root].dx == x && sgt[root].dy == sgt[root].uy && sgt[root].dy == y){
                    sgt[root].total 
            += a;
                    
            return;
                }
                
            int id = w(x,y,sgt[root].dx, sgt[root].dy, sgt[root].ux, sgt[root].uy);
                
            int midx = (sgt[root].dx+sgt[root].ux)/2;
                
            int midy = (sgt[root].dy+sgt[root].uy)/2;
                sgt[root].total 
            += a;
                
            if(id == 0){
                    
            if(sgt[root].ch[id] == -1){
                        sgt[root].ch[id] 
            = init(sgt[root].dx,midx,sgt[root].dy,midy);
                    }
                    insert(sgt[root].ch[id],x,y,a);    
                }
            else if(id == 1){
                    
            if(sgt[root].ch[id] == -1){
                        sgt[root].ch[id] 
            = init(sgt[root].dx,midx,midy+1,sgt[root].uy);
                    }
                    insert(sgt[root].ch[id],x,y,a);
                }
            else if(id == 2){
                    
            if(sgt[root].ch[id] == -1){
                        sgt[root].ch[id] 
            = init(midx+1,sgt[root].ux,sgt[root].dy,midy);
                    }
                    insert(sgt[root].ch[id],x,y,a);
                }
            else{
                    
            if(sgt[root].ch[id] == -1){
                        sgt[root].ch[id] 
            = init(midx+1,sgt[root].ux,midy+1,sgt[root].uy);
                    }
                    insert(sgt[root].ch[id],x,y,a);
                }
            }
            void sum(int root, int dx, int dy, int ux, int uy)
            {
                
            if(root < 0)return;
                
            if(sgt[root].dx > ux || sgt[root].ux < dx || sgt[root].dy > uy || sgt[root].uy < dy)
                    
            return;
                
            if(sgt[root].dx >= dx && sgt[root].ux <= ux && sgt[root].dy >= dy && sgt[root].uy <= uy)
                {
                    ans 
            += sgt[root].total;
                    
            return;
                }
            else{
                    
            for(int i = 0; i < 4; i++)
                    {
                        sum(sgt[root].ch[i],dx,dy,ux,uy);
                    }
                }
            }
            int main()
            {
                
            char opes[500];
                
            char now;
                
            int x, y, z;
                
            int x1, x2, y1, y2;    
                
            while(scanf("%s", opes) != EOF){
                    now 
            = opes[0];
                       getchar();
                       sgtn 
            = 1;
                    sgt[
            0].total = 0;
                    sgt[
            0].dx = 1;
                    sgt[
            0].dy = 1;
                    sgt[
            0].ux = 20000;
                    sgt[
            0].uy = 20000;
                    
            for(int i = 0; i < 4; i++)
                        sgt[
            0].ch[i] = -1;
                      
            while(true){
                        
            if(now == 'E'break;
                        gets(opes);
                        
            if(opes[0< '0' || opes[0> '9'){
                             now 
            = opes[0];
                             
            continue;
                        }
                        
            if(now == 'I'){
                            sscanf(opes, 
            "%d %d %d"&x, &y, &z);
                            insert(
            0,x,y,z);
                        }
            else{
                             sscanf(opes, 
            "%d %d %d %d"&x1, &x2 , &y1, &y2);
                             ans 
            = 0;
                             sum(
            0,x1,y1,x2,y2);    
                            printf(
            "%d\n", ans);
                        }
                    }
                   }
                   
            return 0;
            }
            二維線段樹。


            posted on 2011-01-24 14:53 哲學與程序 閱讀(615) 評論(0)  編輯 收藏 引用 所屬分類: Algorithm

            導航

            公告

            歡迎訪問 http://zhexue.sinaapp.com

            常用鏈接

            隨筆分類(37)

            隨筆檔案(41)

            Algorithm

            最新隨筆

            搜索

            最新評論

            獨立博客: 哲學與程序
            欧美综合天天夜夜久久| 久久本道久久综合伊人| 精品人妻伦九区久久AAA片69| 欧美亚洲国产精品久久| 亚洲国产美女精品久久久久∴| 人妻精品久久久久中文字幕69| 久久免费视频网站| 久久婷婷色香五月综合激情| 国产69精品久久久久777| 精品多毛少妇人妻AV免费久久| 久久人人爽人人爽人人片av麻烦 | 亚洲va久久久久| 久久Av无码精品人妻系列| 国产精自产拍久久久久久蜜| 欧美黑人又粗又大久久久| 国产—久久香蕉国产线看观看 | 久久久久亚洲AV片无码下载蜜桃 | 久久99国产一区二区三区| 囯产精品久久久久久久久蜜桃| 久久99国产一区二区三区| 香蕉久久av一区二区三区| 欧美日韩成人精品久久久免费看 | 国产精品久久久久jk制服| 久久国产精品无| 日韩影院久久| 亚洲午夜福利精品久久| 久久精品国产清自在天天线| 国产精品一区二区久久| 久久久久亚洲av无码专区| 国产69精品久久久久9999APGF | 久久精品国产精品亚洲| 久久精品国产免费一区| 久久久久久亚洲Av无码精品专口| 香蕉aa三级久久毛片| 国产成人精品久久一区二区三区av| 久久久久久亚洲精品成人| 久久精品国产亚洲AV无码偷窥| 青青草原精品99久久精品66| 一本色道久久99一综合| 色妞色综合久久夜夜| 国产精品青草久久久久婷婷|