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

隨筆 - 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>
              精品动漫3d一区二区三区免费版 | 亚洲国产网站| 亚洲午夜精品久久久久久app| 亚洲精品乱码久久久久久| 尤物在线精品| 亚洲国产黄色片| 日韩五码在线| 欧美一区二区三区久久精品| 久久久国产视频91| 欧美**人妖| 日韩视频中文| 午夜精品久久99蜜桃的功能介绍| 欧美伊人久久大香线蕉综合69| 欧美怡红院视频一区二区三区| 久久免费视频在线| 欧美喷潮久久久xxxxx| 国产精品久久网站| 亚洲国产精品成人| 亚洲综合视频一区| 看欧美日韩国产| 亚洲精品一区二区三区在线观看 | 久久久av网站| 欧美日韩国产一区| 国产女优一区| 亚洲高清中文字幕| 午夜精品一区二区三区在线播放| 欧美日韩国产片| 在线观看日韩av先锋影音电影院 | 亚洲一区亚洲二区| 久久性天堂网| 一区二区欧美日韩| 老司机成人在线视频| 国产精品美女在线| 亚洲精品国产无天堂网2021| 欧美专区福利在线| 亚洲精品久久久久中文字幕欢迎你| 午夜精品视频在线| 欧美天天综合网| 亚洲国内在线| 久久一二三国产| 久久av一区二区三区漫画| 亚洲精品乱码久久久久久久久| 久久精品首页| 国产日韩av一区二区| 亚洲视频成人| 日韩香蕉视频| 欧美精品成人在线| 亚洲国产成人在线视频| 久久亚洲免费| 新片速递亚洲合集欧美合集| 欧美日韩国产欧| 一区二区三区视频在线观看| 亚洲国产一二三| 欧美成人一区二区三区片免费| 一区在线视频| 另类尿喷潮videofree| 欧美一级日韩一级| 国产午夜精品福利| 欧美在线电影| 欧美在线影院| 好吊色欧美一区二区三区视频| 久久国产精品久久国产精品| 亚洲一区观看| 国产日韩欧美一区二区三区四区| 午夜欧美精品| 午夜视频在线观看一区二区三区| 国产欧美视频一区二区| 欧美一区二区观看视频| 亚洲欧美国产毛片在线| 国产欧美日韩在线视频| 久久午夜精品一区二区| 久久午夜视频| 亚洲精品中文字幕有码专区| 亚洲日本中文| 国产精品v欧美精品∨日韩| 午夜久久一区| 久久国产黑丝| 亚洲精品在线观| 一区二区三区四区在线| 国产精品免费观看在线| 久久国产精品一区二区| 久久男人资源视频| 一本色道久久99精品综合 | 欧美黑人国产人伦爽爽爽| 久久躁日日躁aaaaxxxx| 你懂的亚洲视频| 国产精品99久久久久久久vr| 亚洲性感激情| 一区免费视频| 日韩午夜av| 国产亚洲一区二区精品| 欧美国产日本| 欧美三级午夜理伦三级中视频| 午夜精品亚洲| 欧美高清视频一区二区三区在线观看| 宅男噜噜噜66国产日韩在线观看| 亚洲欧美在线一区二区| 亚洲欧洲在线免费| 性久久久久久久| 亚洲精品国产精品国产自| 国产精品99久久久久久宅男| 激情五月***国产精品| 亚洲精品久久久久久久久久久久久 | 欧美国产精品日韩| 国产精品高潮视频| 欧美国产视频一区二区| 国产精品拍天天在线| 亚洲国产日韩欧美在线动漫| 国产女主播视频一区二区| 亚洲精品在线三区| 亚洲国产高潮在线观看| 欧美一区二区三区日韩| 在线亚洲欧美视频| 另类欧美日韩国产在线| 久久精品国产一区二区三区| 欧美日韩一区三区| 亚洲第一精品久久忘忧草社区| 国产美女诱惑一区二区| 夜色激情一区二区| 亚洲毛片在线看| 久久久综合网站| 久久久蜜桃精品| 国产精品自在欧美一区| 99在线|亚洲一区二区| 亚洲美女毛片| 欧美第一黄网免费网站| 嫩草成人www欧美| 国产在线精品二区| 亚洲综合色婷婷| 午夜国产精品影院在线观看| 欧美午夜精彩| 中文在线一区| 亚洲一区二区三区中文字幕| 欧美日韩午夜激情| 亚洲免费电影在线观看| 夜夜嗨av一区二区三区网页| 欧美片第一页| 99精品国产高清一区二区| 99riav国产精品| 欧美日韩国产区一| 亚洲深夜福利| 欧美亚洲一区三区| 国产午夜精品美女视频明星a级 | 久久综合九色综合网站| 亚洲成色精品| 欧美一区二区三区在线免费观看| 久久国产精彩视频| 国产亚洲一区精品| 久久久久国产免费免费| 欧美成人免费大片| 99riav1国产精品视频| 欧美日韩三级电影在线| 中文亚洲欧美| 久久天天躁狠狠躁夜夜av| 在线观看成人av| 欧美精品乱人伦久久久久久| 99爱精品视频| 欧美一区二区三区视频免费| 国内精品久久久久久久影视麻豆| 久久久久久九九九九| 欧美激情视频一区二区三区在线播放 | 激情欧美国产欧美| 欧美福利精品| 亚洲亚洲精品在线观看| 久久免费的精品国产v∧| 亚洲国产三级| 国产精品成av人在线视午夜片| 午夜精品婷婷| 亚洲国产成人高清精品| 亚洲一区二区三区涩| 激情久久婷婷| 国产精品久久久久av| 久久激情五月婷婷| 日韩午夜三级在线| 久久三级视频| 亚洲神马久久| 亚洲高清资源| 国产美女扒开尿口久久久| 久久综合亚洲社区| 亚洲网站在线| 欧美激情第一页xxx| 亚洲欧美精品在线| 91久久香蕉国产日韩欧美9色| 国产精品久久二区| 欧美xart系列在线观看| 午夜免费日韩视频| 亚洲人成小说网站色在线| 久久在线免费观看视频| 午夜精品理论片| 日韩午夜在线电影| 伊人天天综合| 国产欧美日韩三区| 欧美性猛片xxxx免费看久爱| 免费久久99精品国产自在现线| 亚洲一区三区视频在线观看| 亚洲欧洲精品天堂一级| 欧美成人激情视频| 美女爽到呻吟久久久久| 欧美影片第一页| 午夜精品美女自拍福到在线|