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

隨筆 - 87  文章 - 279  trackbacks - 0
<2007年11月>
28293031123
45678910
11121314151617
18192021222324
2526272829301
2345678

潛心看書研究!

常用鏈接

留言簿(19)

隨筆分類(81)

文章分類(89)

相冊

ACM OJ

My friends

搜索

  •  

積分與排名

  • 積分 - 220442
  • 排名 - 118

最新評論

閱讀排行榜

評論排行榜

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 閱讀(827) 評論(4)  編輯 收藏 引用 所屬分類: ACM題目

FeedBack:
# re: pku2985 第一次用兩種數(shù)據(jù)結(jié)構(gòu)解題, 并查集+線段樹 2006-09-22 13:24 A3
可否講解一下線段樹部分  回復(fù)  更多評論
  
# 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;

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

初始化(1-10) = 10;
因?yàn)殚_始時(shí), 集合大小為1, 1, 1, 1, 1, 1, 1, 1, 1, 1  回復(fù)  更多評論
  
# re: pku2985 第一次用兩種數(shù)據(jù)結(jié)構(gòu)解題, 并查集+線段樹 2006-09-24 19:53 Optimistic
偶的第一次呢 靜待。。。  回復(fù)  更多評論
  
# re: pku2985 第一次用兩種數(shù)據(jù)結(jié)構(gòu)解題, 并查集+線段樹 2006-09-24 22:23 
+U ^_^  回復(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>
              一区二区三区视频在线播放| 亚洲特级毛片| 久久综合图片| 小黄鸭精品aⅴ导航网站入口| 亚洲黄色三级| 亚洲第一视频| 亚洲欧洲日产国产网站| 亚洲精品久久| 亚洲在线观看| 久久精品国产亚洲一区二区三区| 久久久999| 欧美激情一区| 一区二区三区免费网站| 亚洲一区二区免费看| 欧美一区二区三区在| 久久久久久夜| 欧美日韩免费观看一区| 国产精品海角社区在线观看| 国产亚洲欧美在线| 亚洲黄色免费电影| 午夜精品久久久久久99热软件| 午夜精品久久久久99热蜜桃导演| 久久综合影音| 一本一本久久a久久精品牛牛影视| 亚洲天堂网在线观看| 久久高清国产| 欧美日韩三级视频| 国产欧美日韩一区二区三区在线| 亚洲第一视频| 久久精品国产精品亚洲| 亚洲国产精彩中文乱码av在线播放| 免费视频一区二区三区在线观看| 亚洲国产日韩一级| 欧美一区二区三区在线视频| 免费观看成人网| 国产欧美在线视频| 夜夜夜久久久| 欧美α欧美αv大片| 亚洲婷婷免费| 欧美国产精品中文字幕| 国产亚洲制服色| 亚洲一区美女视频在线观看免费| 欧美.com| 欧美影院精品一区| 国产精品青草久久| 这里只有精品丝袜| 亚洲国产精品久久久久久女王| 欧美一区亚洲| 国产一区二区三区不卡在线观看| 亚洲一区二区免费| 亚洲国产三级网| 欧美成人第一页| 亚洲欧美综合v| 国产午夜久久| 午夜精品在线视频| 欧美大胆人体视频| 欧美先锋影音| 狠狠色丁香婷婷综合影院| 亚洲一区二区三区视频播放| 亚洲日本aⅴ片在线观看香蕉| 久久综合九色欧美综合狠狠| 国产主播一区二区三区四区| 欧美在线地址| 久久久国产视频91| 永久免费精品影视网站| 久久免费国产精品1| 性欧美xxxx大乳国产app| 国产精品一二三四区| 亚洲欧美日韩视频一区| 一区二区三区四区国产精品| 欧美日韩在线观看视频| 亚洲一卡二卡三卡四卡五卡| 这里只有精品视频| 国产欧美一区二区精品忘忧草| 欧美亚洲三区| 久久精品最新地址| 最新成人在线| 亚洲精品中文字幕在线| 国产精品激情av在线播放| 欧美在线观看你懂的| 欧美一区二区三区视频在线| 亚洲第一精品夜夜躁人人爽| 亚洲第一黄色网| 国产精品二区三区四区| 久久久噜久噜久久综合| 欧美成人影音| 性久久久久久久久久久久| 欧美综合二区| 亚洲精品乱码久久久久久久久 | 国产模特精品视频久久久久| 午夜精品视频在线观看一区二区| 欧美一区网站| 亚洲欧洲日本国产| 亚洲午夜精品久久久久久app| 国产欧美大片| 亚洲黄网站黄| 国产一区二区三区自拍| 亚洲国产精品一区在线观看不卡| 国产精品av一区二区| 免费观看日韩av| 欧美视频精品在线| 久久综合免费视频影院| 欧美日韩视频一区二区| 老司机午夜精品| 欧美小视频在线| 欧美好吊妞视频| 国产一区二区久久久| 亚洲人体一区| 亚洲福利电影| 午夜视频一区在线观看| 久久精品国产亚洲a| 久久久蜜桃一区二区人| 久久综合伊人77777麻豆| 一本色道久久综合亚洲二区三区| 欧美在线亚洲在线| 美女久久一区| 麻豆久久久9性大片| 欧美日韩网址| 亚洲大胆女人| 国产一区二区三区久久 | 一本久久综合亚洲鲁鲁| 久久精品免费看| 欧美一区二区在线播放| 欧美日本亚洲韩国国产| 欧美大片一区| 极品尤物一区二区三区| 午夜欧美大尺度福利影院在线看| 在线一区欧美| 国产精品第一区| 亚洲天堂视频在线观看| 亚洲在线观看免费| 欧美视频一二三区| 一本色道88久久加勒比精品| 亚洲日本欧美日韩高观看| 久久艳片www.17c.com| 久久久免费精品视频| 国产免费亚洲高清| 午夜欧美精品| 亚洲欧洲日本mm| 欧美韩国在线| 亚洲精品免费在线| 亚洲伦理一区| 欧美精品在线播放| 亚洲美女啪啪| 亚洲欧美精品伊人久久| 国产精品九九久久久久久久| 一区二区不卡在线视频 午夜欧美不卡' | 欧美激情一区二区三区在线视频观看| 国产视频精品免费播放| 亚洲欧美日韩直播| 久久久爽爽爽美女图片| 一区二区三区在线看| 久久永久免费| 亚洲三级网站| 亚洲欧美日韩国产精品| 国产精品乱码| 欧美亚洲综合另类| 欧美不卡一区| 亚洲色图制服丝袜| 国产日韩欧美精品| 免费91麻豆精品国产自产在线观看| 亚洲国产精品一区二区尤物区| 99精品热6080yy久久| 国产精品久久国产精品99gif| 性欧美video另类hd性玩具| 看欧美日韩国产| 日韩小视频在线观看| 国产精品久久久久久久久久久久久久| 亚洲欧美大片| 欧美精品性视频| 欧美日本韩国| 亚洲午夜精品久久久久久app| 又紧又大又爽精品一区二区| 国产精品99免费看| 欧美午夜精品久久久| 久久国产99| 欧美怡红院视频| 一本久道久久综合狠狠爱| 国产精品嫩草99av在线| 久久久久一区二区三区| 一本大道久久a久久精品综合| 久久久精品日韩| 一区二区三区视频在线观看| 国产一区在线看| 欧美三级视频在线| 另类图片综合电影| 香蕉久久精品日日躁夜夜躁| 91久久精品国产91久久| 久久久蜜臀国产一区二区| 亚洲嫩草精品久久| 日韩一级欧洲| 在线观看欧美成人| 国产精品亚洲综合久久| 欧美高清视频一二三区| 久久精品人人做人人爽| 亚洲一区二区3| 艳女tv在线观看国产一区| 欧美承认网站| 蜜臀a∨国产成人精品| 欧美在线观看一二区|