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

隨筆 - 87  文章 - 279  trackbacks - 0
<2010年7月>
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

潛心看書研究!

常用鏈接

留言簿(19)

隨筆分類(81)

文章分類(89)

相冊

ACM OJ

My friends

搜索

  •  

積分與排名

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

FeedBack:
# re: 第一次用兩種數(shù)據(jù)結(jié)構(gòu)解的題目, 紀(jì)念一下 2006-09-08 23:01 Optimistic
哇...偶木了  回復(fù)  更多評論
  
# re: 第一次用兩種數(shù)據(jù)結(jié)構(gòu)解的題目, 紀(jì)念一下 2006-09-08 23:11 
其實線段樹比較好懂, 但是難在怎么運用-_-個人感覺, 摸索中!~~~  回復(fù)  更多評論
  
# re: 第一次用兩種數(shù)據(jù)結(jié)構(gòu)解的題目, 紀(jì)念一下 2006-09-28 12:21 踏雪赤兔
進步很快哩~~贊一個!
P.S.博客手拉手弄好了~  回復(fù)  更多評論
  
# re: 第一次用兩種數(shù)據(jù)結(jié)構(gòu)解的題目, 紀(jì)念一下 2006-09-28 12:57 
thx!~:)  回復(fù)  更多評論
  
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
              欧美大片在线观看| 亚洲成在线观看| 久久久99国产精品免费| 亚洲欧美国产视频| 亚洲永久免费视频| 午夜精品福利一区二区三区av| 亚洲专区免费| 久久精品亚洲精品国产欧美kt∨| 久久国产精品第一页 | 亚洲欧美中日韩| 久久久久.com| 欧美伦理视频网站| 国产日产精品一区二区三区四区的观看方式| 欧美视频一区二| 国产日韩一区| 亚洲另类自拍| 久久国产福利国产秒拍| 欧美高清一区二区| 99国产精品视频免费观看一公开| 午夜免费电影一区在线观看| 久久综合伊人77777尤物| 欧美日韩精品三区| 韩国av一区二区| 99re6热只有精品免费观看| 欧美在线观看一区二区三区| 欧美第一黄网免费网站| 一区二区av| 老色鬼精品视频在线观看播放| 欧美日韩视频在线| 精品成人一区二区三区| 亚洲一区二区毛片| 欧美电影免费观看网站| 亚洲欧美日韩在线观看a三区| 蜜桃伊人久久| 国产资源精品在线观看| 亚洲小视频在线| 欧美激情视频一区二区三区不卡| 午夜精品久久久久久| 欧美日韩国产成人在线免费| 亚洲高清毛片| 久久天天狠狠| 欧美一区二区三区婷婷月色| 国产精品女人久久久久久| 日韩一级在线观看| 欧美激情视频一区二区三区免费| 久久av最新网址| 国产伦精品一区二区三区免费迷 | 狠狠网亚洲精品| 国产精品99久久久久久有的能看 | 136国产福利精品导航| 亚洲欧美制服另类日韩| 亚洲激情在线播放| 久久久久一区二区三区| 国产欧美视频一区二区三区| 亚洲中字黄色| 亚洲深爱激情| 国产精品v欧美精品v日韩 | 欧美伊久线香蕉线新在线| 亚洲精品婷婷| 欧美精品v国产精品v日韩精品| 亚洲大胆在线| 欧美成人资源| 欧美freesex交免费视频| 一区二区在线观看视频在线观看| 欧美在线视频网站| 亚洲欧美日韩精品久久奇米色影视| 欧美亚洲第一页| 亚洲欧美日韩国产综合| 亚洲一区二区动漫| 国产欧美精品xxxx另类| 欧美中文在线观看国产| 久久精品国产69国产精品亚洲 | 欧美亚洲第一区| 午夜精品一区二区三区在线视 | 欧美激情精品久久久六区热门| 亚洲成人在线视频播放| 亚洲丁香婷深爱综合| 欧美精品激情在线| 亚洲永久免费视频| 欧美在线免费| 亚洲国产小视频在线观看| 亚洲人成网站在线播| 欧美日韩在线视频观看| 亚洲欧美日韩综合aⅴ视频| 亚洲电影免费观看高清完整版在线 | 国产精品99久久不卡二区| 亚洲第一综合天堂另类专| 欧美成人久久| 欧美精彩视频一区二区三区| 亚洲一区制服诱惑| 欧美自拍偷拍午夜视频| 亚洲精品在线一区二区| 亚洲午夜高清视频| 激情小说亚洲一区| 亚洲精品乱码久久久久久蜜桃91| 欧美视频不卡| 久久综合色一综合色88| 欧美日本在线视频| 久久久国产精彩视频美女艺术照福利 | 欧美三级特黄| 六月丁香综合| 国产精品v欧美精品∨日韩| 老司机精品视频网站| 欧美日韩一区二区视频在线观看| 欧美一区国产一区| 欧美成人资源| 久久久99国产精品免费| 欧美久久久久久| 久久深夜福利免费观看| 国产精品久久网站| 亚洲国产日韩欧美| 国产专区一区| 亚洲一区二区精品在线| 亚洲娇小video精品| 性高湖久久久久久久久| 一本色道综合亚洲| 欧美xxxx在线观看| 久久婷婷国产综合精品青草| 国产精品网站在线观看| 日韩亚洲欧美一区| 日韩视频不卡中文| 欧美福利网址| 欧美黄色一区二区| 亚洲国产精品第一区二区| 久久成人综合视频| 久久国内精品视频| 国产精品影视天天线| 亚洲午夜一区二区三区| av不卡在线看| 欧美日韩成人一区二区| 91久久久久久久久| 亚洲国产精品电影在线观看| 久久免费视频网| 男人的天堂亚洲| 在线成人国产| 久久久在线视频| 欧美成人视屏| 亚洲国产婷婷香蕉久久久久久99| 久久久天天操| 亚洲国产精品久久久久秋霞不卡| 亚洲二区视频在线| 美女脱光内衣内裤视频久久影院 | 91久久久亚洲精品| 99精品国产福利在线观看免费| 国产偷久久久精品专区| 99re热这里只有精品视频| 亚洲黄一区二区三区| 榴莲视频成人在线观看| 免费成人在线视频网站| 亚洲国产精品久久人人爱蜜臀 | 老鸭窝毛片一区二区三区| 欧美成人午夜视频| 国产欧美日韩另类视频免费观看| 久久精品水蜜桃av综合天堂| 亚洲国产婷婷香蕉久久久久久| 国产日韩精品视频一区| 亚洲欧美激情视频| 久久久久久久久岛国免费| 精品成人国产| 欧美精品18+| 亚洲视频高清| 久久久免费av| 亚洲美女免费视频| 国产精品成人v| 久久国产天堂福利天堂| 欧美激情精品久久久久久久变态 | 久久成人一区| 亚洲国产精品123| 欧美日韩午夜在线| 麻豆精品网站| 亚洲免费在线看| 欧美日韩在线影院| 久久深夜福利| 激情亚洲成人| 亚洲女人小视频在线观看| 99国产精品99久久久久久粉嫩| 欧美一区二区三区免费看| 亚洲图片欧美午夜| 欧美午夜美女看片| 欧美国产亚洲另类动漫| 国产精品欧美风情| 亚洲精品国精品久久99热| 欧美性片在线观看| 亚洲精品国产无天堂网2021| 狠狠爱综合网| 欧美一区二区三区四区高清| 一区二区三区欧美在线观看| 久久久九九九九| 久久精品一区二区三区不卡牛牛| 国产精品女同互慰在线看| 一区二区三区色| 日韩视频在线观看| av成人毛片| 亚洲欧美日韩在线不卡| 亚洲国语精品自产拍在线观看| 一区二区欧美视频| 国产欧美精品国产国产专区| 欧美理论片在线观看| 久久成人这里只有精品| 亚洲午夜性刺激影院|