青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

隨筆 - 87  文章 - 279  trackbacks - 0
<2006年1月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
2930311234

潛心看書研究!

常用鏈接

留言簿(19)

隨筆分類(81)

文章分類(89)

相冊

ACM OJ

My friends

搜索

  •  

積分與排名

  • 積分 - 219411
  • 排名 - 118

最新評論

閱讀排行榜

評論排行榜

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 閱讀(827) 評論(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!~:)  回復  更多評論
  
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
              欧美激情精品久久久六区热门| 久久久久91| 亚洲在线第一页| 鲁大师影院一区二区三区| 国产精品成人国产乱一区| 在线观看日韩| 久久免费少妇高潮久久精品99| 亚洲人成网站在线播| 亚洲欧美精品suv| 国产午夜精品久久久久久免费视| 亚洲综合日韩在线| 亚洲一级片在线观看| 国产精品亚洲综合一区在线观看| 亚洲中字在线| 亚洲一区二区在线免费观看视频| 欧美精品一区二区三区很污很色的 | 欧美女同视频| 亚洲午夜精品久久久久久app| 国产在线视频欧美| 久久综合九色| 欧美人与禽性xxxxx杂性| 亚洲综合第一页| 欧美在线免费播放| 亚洲综合视频网| 国内偷自视频区视频综合| 久久亚洲综合| 欧美午夜一区二区福利视频| 午夜精品视频在线| 久久九九精品| 亚洲嫩草精品久久| 欧美激情在线狂野欧美精品| 亚洲一区二区免费| 欧美成人午夜激情视频| 性伦欧美刺激片在线观看| 欧美精品18+| 久久九九久久九九| 国产精品一二三四| 欧美不卡高清| 亚洲国产美国国产综合一区二区| 一本色道久久88亚洲综合88| 黄色成人av| 久久精品91| 乱中年女人伦av一区二区| 国产精品网站在线| 一区二区三区国产精华| 亚洲国产精品va在线看黑人动漫| 欧美福利一区二区| 激情婷婷亚洲| 久久尤物视频| 亚洲欧洲日本专区| 亚洲视频一二三| 欧美婷婷久久| 亚洲欧美影音先锋| 久久久精品国产免大香伊 | 亚洲网站在线| 亚洲女ⅴideoshd黑人| 国产精品女主播| 欧美在线观看一区二区三区| 欧美精品97| 99国产精品久久久| 欧美影院成人| 亚洲国产你懂的| 欧美日韩成人一区二区| 亚洲欧美精品一区| 亚洲国产成人91精品 | 黄页网站一区| 欧美大片在线看免费观看| 日韩一级精品视频在线观看| 久久久国产精品一区二区中文| 国产欧美日韩激情| 国产色产综合产在线视频| 久久精品亚洲乱码伦伦中文| 亚洲福利在线看| 国产日产欧美a一级在线| 欧美电影打屁股sp| 久久男人资源视频| 性一交一乱一区二区洋洋av| 日韩系列在线| 美女免费视频一区| 久久九九有精品国产23| 亚洲天堂男人| 亚洲国产免费看| 在线电影国产精品| 国产一区二区在线观看免费播放| 欧美韩日精品| 欧美日韩精品久久久| 蜜乳av另类精品一区二区| 久久精品国产一区二区三| 欧美淫片网站| 老巨人导航500精品| 免费观看不卡av| 欧美成人免费小视频| 一本色道久久88综合亚洲精品ⅰ| 久久综合中文| 欧美成人国产| 午夜欧美大片免费观看| 在线看日韩欧美| 亚洲免费观看视频| 亚洲一区在线播放| 一级成人国产| 欧美成人黑人xx视频免费观看| 久久精品国产亚洲aⅴ| 午夜欧美电影在线观看| 亚洲欧美综合网| 久久xxxx精品视频| 亚洲电影在线看| 在线视频一区观看| 久久婷婷国产综合精品青草| 欧美激情第五页| 在线播放豆国产99亚洲| 正在播放日韩| 久久综合九色综合久99| 一区二区免费看| 欧美日韩的一区二区| 欧美日韩亚洲一区三区 | 亚洲视频专区在线| 午夜精品久久久久99热蜜桃导演| 欧美一级黄色网| 欧美日韩一卡二卡| 亚洲人成毛片在线播放女女| 亚洲欧美日韩久久精品| 亚洲精品久久久久久久久久久久| 亚洲视频观看| 欧美日韩一区在线| 日韩视频在线免费观看| 男女精品视频| 久久精品国产一区二区三区免费看| 欧美日韩一区二区在线观看| 亚洲激情成人在线| 欧美福利视频网站| 久久看片网站| 伊人色综合久久天天| 免费成人在线观看视频| 最新国产拍偷乱拍精品| 欧美高清不卡| 亚洲午夜在线视频| 亚洲人成网站精品片在线观看| 久久精品国产欧美亚洲人人爽| 一本久道久久综合中文字幕| 欧美精品一区二区在线观看| 国产精品99久久99久久久二8 | 宅男噜噜噜66一区二区| 99精品福利视频| 亚洲一区二区三区777| 国产精品久久久久久久久免费樱桃| 亚洲永久视频| 欧美一区二区三区在线播放| 亚洲国产精品成人久久综合一区| 亚洲国产激情| 国产精品系列在线| 亚洲第一综合天堂另类专| 国产精品xvideos88| 麻豆国产va免费精品高清在线| 免费精品视频| 亚洲第一精品影视| 亚洲视频日本| 一本色道久久综合一区| 久久av最新网址| 国产毛片精品视频| 亚洲午夜激情网页| 亚洲精品欧洲| 亚洲精品一区二区三| 欧美精品日韩综合在线| 一区二区三区精品| 欧美有码在线视频| 亚洲精品综合精品自拍| 国产一区再线| 亚洲一区二区在线视频| 欧美r片在线| 欧美.www| 国产一区二区看久久| 午夜欧美不卡精品aaaaa| 午夜精品国产更新| 欧美日韩国产综合视频在线观看 | 亚洲二区在线观看| 久久精品国产77777蜜臀| 午夜精品一区二区三区电影天堂| 欧美日韩国产999| 亚洲在线观看免费| 日韩小视频在线观看| 免播放器亚洲一区| 在线成人www免费观看视频| 久久国产视频网| 亚洲第一网站| 亚洲一区二区三区国产| 国产精品毛片| 午夜在线精品偷拍| 欧美aa国产视频| 亚洲欧美日产图| 国产精品第一页第二页第三页| 亚洲天堂av图片| 久久影院午夜论| 国语自产精品视频在线看抢先版结局 | 亚洲欧洲在线免费| 国产精品入口麻豆原神| 久久亚洲欧美| 亚洲国产日韩欧美在线图片| 欧美日韩在线精品| 狠狠色丁香久久婷婷综合_中| 亚洲欧美在线看|