• <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>
            隨筆 - 87  文章 - 279  trackbacks - 0
            <2007年9月>
            2627282930311
            2345678
            9101112131415
            16171819202122
            23242526272829
            30123456

            潛心看書研究!

            常用鏈接

            留言簿(19)

            隨筆分類(81)

            文章分類(89)

            相冊(cè)

            ACM OJ

            My friends

            搜索

            •  

            積分與排名

            • 積分 - 217857
            • 排名 - 117

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            The k-th Largest Group
            Time Limit:2000MS? Memory Limit:131072K
            Total Submit:1222 Accepted:290

            Description

            Newman likes playing with cats. He possesses lots of cats in his home. Because the number of cats is really huge, Newman wants to group some of the cats. To do that, he first offers a number to each of the cat (1, 2, 3, …, n). Then he occasionally combines the group cat i is in and the group cat j is in, thus creating a new group. On top of that, Newman wants to know the size of the k-th biggest group at any time. So, being a friend of Newman, can you help him?

            Input

            1st line: Two numbers N and M (1 ≤ N, M ≤ 200,000), namely the number of cats and the number of operations.

            2nd to (m + 1)-th line: In each line, there is number C specifying the kind of operation Newman wants to do. If C = 0, then there are two numbers i and j (1 ≤ i, jn) following indicating Newman wants to combine the group containing the two cats (in case these two cats are in the same group, just do nothing); If C = 1, then there is only one number k (1 ≤ k ≤ the current number of groups) following indicating Newman wants to know the size of the k-th largest group.

            Output

            For every operation “1” in the input, output one number per line, specifying the size of the kth largest group.

            Sample Input

            10 10
            0 1 2
            1 4
            0 3 4
            1 2
            0 5 6
            1 1
            0 7 8
            1 1
            0 9 10
            1 1

            Sample Output

            1
            2
            2
            2
            2

            Hint

            When there are three numbers 2 and 2 and 1, the 2nd largest number is 2 and the 3rd largest number is 1.

            Source
            POJ Monthly--2006.08.27, zcgzcgzcg

            #include? < iostream >
            using ? namespace ?std;
            const ? int ?MAXN? = ? 200001 ;

            class ?UFset
            {
            public :
            ????
            int ?parent[MAXN];
            ????UFset();
            ????
            int ?Find( int );
            ????
            void ?Union( int ,? int );
            }
            ;

            UFset::UFset()
            {
            ????memset(parent,?
            - 1 ,? sizeof (parent));
            }


            int ?UFset::Find( int ?x)
            {
            ????
            if ?(parent[x]? < ? 0 )
            ????????
            return ?x;
            ????
            else
            ????
            {
            ????????parent[x]?
            = ?Find(parent[x]);
            ????????
            return ?parent[x];
            ????}
            // ?壓縮路徑
            }


            void ?UFset::Union( int ?x,? int ?y)
            {
            ????
            int ?pX? = ?Find(x);
            ????
            int ?pY? = ?Find(y);
            ????
            int ?tmp;
            ????
            if ?(pX? != ?pY)
            ????
            {
            ????????tmp?
            = ?parent[pX]? + ?parent[pY];? // ?加權(quán)合并
            ???????? if ?(parent[pX]? > ?parent[pY])
            ????????
            {
            ????????????parent[pX]?
            = ?pY;
            ????????????parent[pY]?
            = ?tmp;
            ????????}

            ????????
            else
            ????????
            {
            ????????????parent[pY]?
            = ?pX;
            ????????????parent[pX]?
            = ?tmp;
            ????????}

            ????}

            }


            int ?f[(MAXN + 1 ) * 3 ]? = ? { 0 } ;
            int ?n,?m;

            void ?initTree()
            {
            ????
            int ?l? = ? 1 ,?r? = ?n;
            ????
            int ?c? = ? 1 ;
            ????
            while ?(l? < ?r)
            ????
            {
            ????????f[c]?
            = ?n;
            ????????c?
            = ?c? * ? 2 ;
            ????????r?
            = ?(l? + ?r)? / ? 2 ;
            ????}

            ????f[c]?
            = ?n; // 葉子初始化
            }


            void ?insertTree( int ?k)
            {
            ????
            int ?l? = ? 1 ,?r? = ?n;
            ????
            int ?c? = ? 1 ;
            ????
            int ?mid;

            ????
            while ?(l? < ?r)
            ????
            {
            ????????f[c]
            ++ ;
            ????????mid?
            = ?(r? + ?l)? / ? 2 ;
            ????????
            if ?(k? > ?mid)
            ????????
            {
            ????????????l?
            = ?mid? + ? 1 ;
            ????????????c?
            = ?c? * ? 2 ? + ? 1 ;
            ????????}

            ????????
            else
            ????????
            {
            ????????????r?
            = ?mid;
            ????????????c?
            = ?c? * ? 2 ;
            ????????}

            ????}

            ????f[c]
            ++ ; // 葉子增加1
            }


            void ?delTree( int ?k)
            {
            ????
            int ?l? = ? 1 ,?r? = ?n;
            ????
            int ?c? = ? 1 ;
            ????
            int ?mid;

            ????
            while ?(l? < ?r)
            ????
            {
            ????????f[c]
            -- ;
            ????????mid?
            = ?(r? + ?l)? / ? 2 ;
            ????????
            if ?(k? > ?mid)
            ????????
            {
            ????????????l?
            = ?mid? + ? 1 ;
            ????????????c?
            = ?c? * ? 2 ? + ? 1 ;
            ????????}

            ????????
            else
            ????????
            {
            ????????????r?
            = ?mid;
            ????????????c?
            = ?c? * ? 2 ;
            ????????}

            ????}

            ????f[c]
            -- ; // 葉子減少1
            }


            int ?searchTree( int ?k)
            {
            ????
            int ?l? = ? 1 ,?r? = ?n;
            ????
            int ?c? = ? 1 ;
            ????
            int ?mid;

            ????
            while ?(l? < ?r)
            ????
            {
            ????????mid?
            = ?(l? + ?r)? / ? 2 ;
            ????????
            if ?(k? <= ?f[ 2 * c + 1 ])
            ????????
            {
            ????????????l?
            = ?mid? + ? 1 ;
            ????????????c?
            = ?c? * ? 2 ? + ? 1 ;
            ????????}

            ????????
            else
            ????????
            {
            ????????????k?
            -= ?f[ 2 * c + 1 ];
            ????????????r?
            = ?mid;
            ????????????c?
            = ?c? * ? 2 ;
            ????????}

            ????}

            ????
            return ?l;
            }


            int ?main()
            {
            ????
            int ?i,?j;
            ????
            int ?x,?y;
            ????
            int ?k;
            ????
            int ?l,?r;
            ????
            int ?cmd;
            ????
            int ?px,?py;
            ????
            int ?tx,?ty,?tz;
            ????UFset?UFS;

            ????
            ????scanf(
            " %d%d " ,? & n,? & m);
            ????initTree();
            ????
            for ?(i = 0 ;?i < m;?i ++ )
            ????
            {
            ????????scanf(
            " %d " ,? & cmd);
            ????????
            if ?(cmd? == ? 0 )
            ????????
            {
            ????????????scanf(
            " %d%d " ,? & x,? & y);
            ????????????px?
            = ?UFS.Find(x);
            ????????????py?
            = ?UFS.Find(y);
            ????????????
            if ?(px? != ?py)
            ????????????
            {
            ????????????????tx?
            = ? - UFS.parent[px];
            ????????????????ty?
            = ? - UFS.parent[py];
            ????????????????tz?
            = ?tx? + ?ty;
            ????????????????UFS.Union(x,?y);
            ????????????????insertTree(tz);
            ????????????????delTree(tx);
            ????????????????delTree(ty);
            ????????????}

            ????????}

            ????????
            else
            ????????
            {
            ????????????scanf(
            " %d " ,? & k);
            ????????????printf(
            " %d\n " ,?searchTree(k));
            ????????}

            ????}

            ????
            return ? 0 ;
            }
            posted on 2006-09-06 13:28 閱讀(816) 評(píng)論(4)  編輯 收藏 引用 所屬分類: ACM題目

            FeedBack:
            # re: pku2985 第一次用兩種數(shù)據(jù)結(jié)構(gòu)解題, 并查集+線段樹 2006-09-22 13:24 A3
            可否講解一下線段樹部分  回復(fù)  更多評(píng)論
              
            # re: pku2985 第一次用兩種數(shù)據(jù)結(jié)構(gòu)解題, 并查集+線段樹 2006-09-22 17:47 
            把區(qū)間劃出來, 節(jié)點(diǎn)(非葉子), 表示該區(qū)間里面含有多少個(gè)元素。
            如果 n = 10;
            而集合大小分別是 1, 1, 2, 6;

            則 區(qū)間(1-10) = 4; 區(qū)間(1-5) = 3;

            就這樣用線段樹動(dòng)態(tài)維護(hù)每次集合合并后的集合大小。

            初始化(1-10) = 10;
            因?yàn)殚_始時(shí), 集合大小為1, 1, 1, 1, 1, 1, 1, 1, 1, 1  回復(fù)  更多評(píng)論
              
            # re: pku2985 第一次用兩種數(shù)據(jù)結(jié)構(gòu)解題, 并查集+線段樹 2006-09-24 19:53 Optimistic
            偶的第一次呢 靜待。。。  回復(fù)  更多評(píng)論
              
            # re: pku2985 第一次用兩種數(shù)據(jù)結(jié)構(gòu)解題, 并查集+線段樹 2006-09-24 22:23 
            久久亚洲国产成人影院| 99国产精品久久| 亚洲va久久久久| 国产美女亚洲精品久久久综合| 欧美一区二区三区久久综合| 亚洲午夜精品久久久久久人妖| 伊人久久亚洲综合影院| 精品久久久久久中文字幕人妻最新| 亚洲天堂久久精品| 亚洲午夜久久久影院| 久久亚洲国产精品123区| 久久综合88熟人妻| 一级做a爰片久久毛片免费陪| 精品熟女少妇a∨免费久久| 亚洲午夜久久久| 久久777国产线看观看精品| 尹人香蕉久久99天天拍| 99久久精品费精品国产| 久久精品国产亚洲AV无码麻豆 | 久久国产成人午夜AV影院| 久久精品国产男包| 久久精品中文字幕有码| 精品亚洲综合久久中文字幕| 欧洲精品久久久av无码电影 | 久久国产成人亚洲精品影院| 91久久精品91久久性色| 久久精品国产久精国产一老狼| 亚洲人AV永久一区二区三区久久| 国产精品一区二区久久精品无码 | 2021最新久久久视精品爱| 久久一区二区免费播放| 观看 国产综合久久久久鬼色 欧美 亚洲 一区二区 | 久久伊人亚洲AV无码网站| 国产午夜久久影院| 国产亚洲精久久久久久无码| 婷婷久久香蕉五月综合加勒比| 亚洲伊人久久成综合人影院 | 日日躁夜夜躁狠狠久久AV| 色欲久久久天天天综合网| 亚洲国产另类久久久精品| 久久久久女人精品毛片|