• <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)寒,忽然年以殘,念往昔,語默心酸。二十光陰無一物,韶光賤,寐難安; 不畏形影單,道途阻且慢,哪曲折,如渡飛湍。斬浪劈波酬壯志,同把酒,共言歡! -如夢令

            關(guān)于浙大月賽B題 快速尋找第k小數(shù)...

            這個題我是用線段樹來做的,結(jié)果居然是超時。。。后來foreverlin同學(xué)告訴我他用樹狀數(shù)組過的,但我記得樹狀數(shù)組能解決的問題,線段樹一定能解決,而且線段樹的復(fù)雜度是logL級別,為什么我會超時呢?還請各位大牛指點。。。
            我的代碼如下:

            #include<iostream>
            #include
            <cstdio>
            #include
            <cstring>
            #include
            <cmath>
            using namespace std;
            #define MAX 300001
            int a[MAX];


            struct node
            {
                
                
            int left;
                
            int right;
                
            int level;
                
            int cnt;
                node 
            *lchild;
                node 
            *rchild;
            }
            dotset[MAX];
            int cou;
            node 
            *Newnode()
            {
                
                node 
            *p=&dotset[cou];
                cou
            ++;
                p
            ->level=0;
                p
            ->cnt=0;
                p
            ->left=0;
                p
            ->right=0;
                p
            ->lchild=NULL;
                p
            ->rchild=NULL;
                
            return p;
            }



            void Build(node *&tree,int left,int right)
            {
                tree
            =Newnode();
                tree
            ->left=left;
                tree
            ->right=right;
                
            if(left==right)
                
            {
                    
            return;
                }

                
            int mid=(left+right)>>1;
                Build(tree
            ->lchild,left,mid);
                Build(tree
            ->rchild,mid+1,right);
            }

            int p=1;
            void Insert(node *tree,int k,int num)
            {
                tree
            ->cnt+=num;
                
            if(tree->left==tree->right)
                
            {
                    tree
            ->level=p;
                    p
            ++;
                    
            return;
                }

                
            int mid=(tree->left+tree->right)>>1;
                
            if(k>mid)
                    Insert(tree
            ->rchild,k,num);
                
            else
                    Insert(tree
            ->lchild,k,num);
            }

            void Insert2(node *tree,int k,int num)
            {
                tree
            ->cnt-=a[k];
                tree
            ->cnt+=num;
                
            if(tree->left==tree->right)
                
            {
                    
            return;
                }

                
            if(k>(tree->left+tree->right)>>1)
                    Insert2(tree
            ->rchild,k,num);
                
            else
                    Insert2(tree
            ->lchild,k,num);
            }


            int Query(node *tree,int k)
            {
                
            if(tree->left==tree->right)
                    
            return tree->level;
                
            int t=tree->lchild->cnt;
                
            if(k<=t)
                    Query(tree
            ->lchild,k);
                
            else 
                    Query(tree
            ->rchild,k-t);
            }



            int main()
            {

                
            int n;
                
            int q;
                node 
            *root;
                
            while(scanf("%d",&n)!=EOF)
                
            {
                    cou
            =0;
                    Build(root,
            1,n);
                    p
            =1;
                    
            int i;
                    
            for(i=1;i<=n;i++)
                    
            {
                        
            int t;
                        scanf(
            "%d",&a[i]);
                        Insert(root,i,a[i]);
                    }

                    scanf(
            "%d",&q);
                    
            for(i=1;i<=q;i++)
                    
            {
                        
            int t;
                        
            char s;
                        cin.ignore();
                        scanf(
            "%c",&s);
                        
            if(s=='q')
                        
            {
                            scanf(
            "%d",&t);
                            printf(
            "%d\n",Query(root,t));
                        }

                        
            else if(s=='p')
                        
            {
                            
            int a1,b1;
                            scanf(
            "%d%d",&a1,&b1);
                            Insert2(root,a1,b1);
                            a[a1]
            =b1;
                        }




                    }

                    
                }

                
            return 0;
            }

            順便發(fā)幾句牢騷,為啥我做浙大的題總是不順呢(當(dāng)然其實我也是第二次做浙大的比賽)。。。浙大好像總喜歡出極限數(shù)據(jù),即使算法對了,也不讓我們快速地通過,總是要優(yōu)化到最佳才行。這樣做好處是比賽的時候?qū)嵙^硬一些吧,畢竟可以說 我們連浙大那么BT的數(shù)據(jù)都過了,算法肯定是最好的了。。。下次提前做好心理準(zhǔn)備吧。。。

            下午花了一個小時 終于弄懂了樹狀數(shù)組,總算把這題過了,感覺樹狀數(shù)組實現(xiàn)起來比線段樹容易得多,效率也高得多,非常實用。
            #include<iostream>
            #include
            <cmath>
            #include
            <algorithm>
            #include
            <cstring>
            using namespace std;

            int n,q;
            int a[100001],c[100001];

            int lowbit(int k)
            {
                
            return k&(k^(k-1));
            }


            void update(int x,int num)
            {
                
            while(x<=n)
                
            {
                    c[x]
            +=num;
                    x
            +=lowbit(x);     
                }
             
            }
                
            int query(int x)
            {
                
            int sum=0;
                
            while(x>0)
                
            {
                    sum
            +=c[x];
                    x
            -=lowbit(x);    
                }

                
            return sum;      
            }


            int find(int t)
            {

                
            int l=1;
                
            int r=n;
                
            int min;
                
                
            while(l<r)
                
            {
                    
            int mid=(l+r)>>1;
                    
            if(query(mid)>=t&&query(mid-1)<t)
                        
            return mid;
                    
            else if(query(mid)>=t)
                        r
            =mid-1;
                    
            else 
                        l
            =mid+1;
                }

                
            return r;//剛好為最后一個查找點
            }



            int main()
            {
                
            int i,j;
                
            int t;
                
            char s[2];
                
            while(scanf("%d",&n)!=EOF)
                
            {

                    
            for(i=1;i<=n;i++)c[i]=0;
                    
            for(i=1;i<=n;i++)
                    
            {

                        scanf(
            "%d",&a[i]);
                        update(i,a[i]);
                    }

                    scanf(
            "%d",&q);
                    
            while(q--)
                    
            {
                        scanf(
            "%s",s);
                        
            if(s[0]=='q')
                        
            {

                            scanf(
            "%d",&t);
                            printf(
            "%d\n",find(t));
                        }

                        
            else 
                        
            {
                            
            int t1,t2;
                            scanf(
            "%d%d",&t1,&t2);
                            update(t1,
            -a[t1]);
                            a[t1]
            =t2;
                            update(t1,a[t1]);
                        }


                    }

                    
                }

                
            return 0;
            }



            posted on 2009-12-15 00:36 abilitytao 閱讀(1825) 評論(7)  編輯 收藏 引用

            評論

            # re: 關(guān)于浙大月賽B題 快速尋找第k小數(shù)... 2009-12-17 11:19 diwayou

            我有個問題納悶,你用了一堆的C++頭文件,程序里用的全是C的函數(shù),why  回復(fù)  更多評論   

            # re: 關(guān)于浙大月賽B題 快速尋找第k小數(shù)... 2009-12-17 12:40 abilitytao

            @diwayou
            我用的就是C的 --!  回復(fù)  更多評論   

            # re: 關(guān)于浙大月賽B題 快速尋找第k小數(shù)... 2009-12-17 17:00 wxq

            你整的太復(fù)雜了...沒那個必要!  回復(fù)  更多評論   

            # re: 關(guān)于浙大月賽B題 快速尋找第k小數(shù)... 2009-12-21 00:26 abilitytao

            @wxq
            請問有什么更好的方法嗎?請指教:-)  回復(fù)  更多評論   

            # re: 關(guān)于浙大月賽B題 快速尋找第k小數(shù)... 2009-12-27 23:41 MasterLuo

            樹狀數(shù)組與線段樹應(yīng)該都是可以過的。不過可以用樹狀數(shù)組的地方就沒必要用線段樹了。  回復(fù)  更多評論   

            # re: 關(guān)于浙大月賽B題 快速尋找第k小數(shù)... 2011-05-03 17:54 seeyou

            呃~~能寫點注釋么?這樣看不明白呀,謝謝啦  回復(fù)  更多評論   


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


            伊人久久大香线蕉av不卡| 久久不射电影网| 国产一区二区精品久久凹凸| 久久精品国产亚洲AV高清热| 中文字幕乱码久久午夜| 亚洲精品国产综合久久一线| 久久国产香蕉一区精品| 国产三级观看久久| 99久久精品免费看国产| 久久天堂电影网| 久久免费精品视频| 亚洲午夜久久影院| 国产精品日韩欧美久久综合| 99久久99久久精品国产| 情人伊人久久综合亚洲| 亚洲一区中文字幕久久| 久久亚洲国产欧洲精品一| 色综合久久88色综合天天| 国产成人香蕉久久久久| 曰曰摸天天摸人人看久久久| 国产福利电影一区二区三区,免费久久久久久久精 | 久久人人爽人人爽人人爽| 91麻豆国产精品91久久久| 久久久久99这里有精品10| 精品国产乱码久久久久久人妻| 99精品久久精品一区二区| 久久精品国产精品亚洲毛片| 久久国产精品久久精品国产| 狠狠色综合久久久久尤物| 久久久久亚洲精品无码网址| 色妞色综合久久夜夜| 久久精品中文字幕无码绿巨人| 狠狠色丁香婷婷综合久久来 | 久久露脸国产精品| 色老头网站久久网| 久久精品国产亚洲AV麻豆网站| 久久免费线看线看| 狠狠色丁香久久婷婷综合图片| 无码精品久久久久久人妻中字| 日韩精品久久久久久| 久久免费看黄a级毛片|