• <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 哲學與程序 閱讀(609) 評論(0)  編輯 收藏 引用 所屬分類: Algorithm

            導航

            公告

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

            常用鏈接

            隨筆分類(37)

            隨筆檔案(41)

            Algorithm

            最新隨筆

            搜索

            最新評論

            獨立博客: 哲學與程序
            99久久精品免费观看国产| 久久SE精品一区二区| 久久精品国内一区二区三区 | 精品久久久久久国产三级| 久久av无码专区亚洲av桃花岛| 久久亚洲春色中文字幕久久久| 国内精品久久久久影院免费| 国产精品内射久久久久欢欢| 亚洲国产成人久久精品99 | 亚洲午夜无码久久久久小说| 亚洲午夜无码久久久久| 日韩精品国产自在久久现线拍| 无码人妻久久一区二区三区蜜桃 | 久久亚洲高清综合| 99久久99久久精品免费看蜜桃| 久久综合精品国产一区二区三区| 亚洲国产精品成人久久| 精品久久人人妻人人做精品| 精品久久久久香蕉网| 亚洲精品综合久久| 久久国产精品99精品国产987| 影音先锋女人AV鲁色资源网久久| 日本福利片国产午夜久久| 久久超碰97人人做人人爱| 欧美无乱码久久久免费午夜一区二区三区中文字幕 | 色天使久久综合网天天| 久久夜色精品国产亚洲| 亚洲va国产va天堂va久久| 久久久久国产视频电影| 老司机国内精品久久久久| 亚洲国产精品无码久久久秋霞2| 中文字幕无码久久精品青草| 久久久精品视频免费观看| 精品久久人人爽天天玩人人妻| 久久99中文字幕久久| 99久久综合狠狠综合久久止| 精品蜜臀久久久久99网站| 久久久久亚洲av无码专区导航| 亚洲av伊人久久综合密臀性色| 亚洲午夜久久久久久噜噜噜| 亚洲中文字幕无码久久精品1|