• <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
            <2006年9月>
            272829303112
            3456789
            10111213141516
            17181920212223
            24252627282930
            1234567

            潛心看書研究!

            常用鏈接

            留言簿(19)

            隨筆分類(81)

            文章分類(89)

            相冊

            ACM OJ

            My friends

            搜索

            •  

            積分與排名

            • 積分 - 216452
            • 排名 - 117

            最新評論

            閱讀排行榜

            評論排行榜

            USE?并查集和線段樹

            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];?//?加權合并
            ????????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:30 閱讀(806) 評論(4)  編輯 收藏 引用 所屬分類: 算法&ACM

            FeedBack:
            # re: 第一次用兩種數據結構解的題目, 紀念一下 2006-09-08 23:01 Optimistic
            哇...偶木了  回復  更多評論
              
            # re: 第一次用兩種數據結構解的題目, 紀念一下 2006-09-08 23:11 
            其實線段樹比較好懂, 但是難在怎么運用-_-個人感覺, 摸索中!~~~  回復  更多評論
              
            # re: 第一次用兩種數據結構解的題目, 紀念一下 2006-09-28 12:21 踏雪赤兔
            進步很快哩~~贊一個!
            P.S.博客手拉手弄好了~  回復  更多評論
              
            # re: 第一次用兩種數據結構解的題目, 紀念一下 2006-09-28 12:57 
            thx!~:)  回復  更多評論
              
            久久99精品久久久久婷婷| 99久久做夜夜爱天天做精品| 久久综合色老色| 久久激情五月丁香伊人| 国产91色综合久久免费分享| 成人国内精品久久久久一区| 久久精品国产亚洲av麻豆蜜芽| 亚洲国产成人精品女人久久久 | 久久SE精品一区二区| 久久亚洲AV无码西西人体| 精品久久久久久国产免费了| 国内精品久久久久久久coent| 久久精品视频网| 国产精品日韩欧美久久综合| 精品国产青草久久久久福利| 久久久国产精品| 亚洲一级Av无码毛片久久精品| 精品久久久一二三区| 亚洲AV日韩精品久久久久久久| 人妻精品久久久久中文字幕69| 99久久久精品| 久久精品亚洲精品国产欧美| 亚洲精品99久久久久中文字幕| 2020久久精品亚洲热综合一本| 精品久久久无码人妻中文字幕| 久久av无码专区亚洲av桃花岛| 久久免费视频网站| 亚洲国产成人精品91久久久| 久久精品aⅴ无码中文字字幕不卡 久久精品aⅴ无码中文字字幕重口 | 国内精品久久久久影院薰衣草| 国产午夜福利精品久久2021 | 久久天堂AV综合合色蜜桃网| 伊人热人久久中文字幕| 久久亚洲精品国产亚洲老地址| 久久天天躁狠狠躁夜夜96流白浆 | 久久久久黑人强伦姧人妻| 久久午夜福利无码1000合集| 亚洲国产精久久久久久久| 久久国产劲爆AV内射—百度| 亚洲午夜久久影院| 欧美亚洲色综久久精品国产|