1. 程式人生 > >P3377 【模板】左偏樹(可並堆)

P3377 【模板】左偏樹(可並堆)

return 表示 style 三次 限制 continue n) sta print

P3377 【模板】左偏樹(可並堆)

題目描述

如題,一開始有N個小根堆,每個堆包含且僅包含一個數。接下來需要支持兩種操作:

操作1: 1 x y 將第x個數和第y個數所在的小根堆合並(若第x或第y個數已經被刪除或第x和第y個數在用一個堆內,則無視此操作)

操作2: 2 x 輸出第x個數所在的堆最小數,並將其刪除(若第x個數已經被刪除,則輸出-1並無視刪除操作)

輸入輸出格式

輸入格式:

第一行包含兩個正整數N、M,分別表示一開始小根堆的個數和接下來操作的個數。

第二行包含N個正整數,其中第i個正整數表示第i個小根堆初始時包含且僅包含的數。

接下來M行每行2個或3個正整數,表示一條操作,格式如下:

操作1 : 1 x y

操作2 : 2 x

輸出格式:

輸出包含若幹行整數,分別依次對應每一個操作2所得的結果。

輸入輸出樣例

輸入樣例#1: 復制
5 5
1 5 4 2 3
1 1 5
1 2 5
2 2
1 4 2
2 2
輸出樣例#1: 復制
1
2

說明

當堆裏有多個最小值時,優先刪除原序列的靠前的,否則會影響後續操作1導致WA。

時空限制:1000ms,128M

數據規模:

對於30%的數據:N<=10,M<=10

對於70%的數據:N<=1000,M<=1000

對於100%的數據:N<=100000,M<=100000

樣例說明:

初始狀態下,五個小根堆分別為:{1}、{5}、{4}、{2}、{3}。

第一次操作,將第1個數所在的小根堆與第5個數所在的小根堆合並,故變為四個小根堆:{1,3}、{5}、{4}、{2}。

第二次操作,將第2個數所在的小根堆與第5個數所在的小根堆合並,故變為三個小根堆:{1,3,5}、{4}、{2}。

第三次操作,將第2個數所在的小根堆的最小值輸出並刪除,故輸出1,第一個數被刪除,三個小根堆為:{3,5}、{4}、{2}。

第四次操作,將第4個數所在的小根堆與第2個數所在的小根堆合並,故變為兩個小根堆:{2,3,5}、{4}。

第五次操作,將第2個數所在的小根堆的最小值輸出並刪除,故輸出2,第四個數被刪除,兩個小根堆為:{3,5}、{4}。

故輸出依次為1、2。

code

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<iostream>
 6 
 7 using namespace std;
 8 
 9 const int N = 200100;
10 int rs[N],ls[N],fa[N],dis[N],val[N];
11 
12 inline char nc() {
13     static char buf[100000],*p1 = buf,*p2 = buf;
14     return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
15 }
16 inline int read() {
17     int x = 0,f = 1;char ch = nc();
18     for (; ch<0||ch>9; ch = nc()) if (ch==-) f = -1;
19     for (; ch>=0&&ch<=9; ch = nc()) x = x * 10 + ch - 0;
20     return x * f;
21 }
22 int merge(int x,int y) {
23     if (!x||!y) return x + y;
24     if ((val[x]>val[y])||(val[x]==val[y]&&x>y)) swap(x,y);
25     rs[x] = merge(rs[x],y);
26     fa[rs[x]] = x;
27     if (dis[ls[x]]<dis[rs[x]]) swap(ls[x],rs[x]);
28     dis[x] = dis[rs[x]] + 1;
29     return x;
30 }
31 inline int getmn(int x) {
32     while (fa[x]) x = fa[x];
33     return x;
34 }
35 inline void del(int x) {
36     val[x] = -1;
37     fa[ls[x]] = fa[rs[x]] = 0;
38     merge(ls[x],rs[x]);
39 }
40 int main() {
41     int n = read(),m = read();
42     for (int i=1; i<=n; ++i) val[i] = read();
43     while (m--) {
44         int opt = read();
45         if (opt==1) {
46             int x = read(),y = read();
47             if (val[x]==-1||val[y]==-1||x==y) continue;
48             merge(getmn(x),getmn(y));
49         }
50         else {
51             int x = read();
52             if (val[x]==-1) {puts("-1");continue;}
53             int y = getmn(x);
54             printf("%d\n",val[y]);
55             del(y);
56         }
57     }
58     return 0;
59 }

P3377 【模板】左偏樹(可並堆)