Luo Gu P3377 [template] leftist tree (and may heap)

2019-06-01

Title: Luo Gu P3377 [template] leftist tree (and may heap): https://www.luogu.org/problemnew/show/P3377


Title Description

As stated, a small root began N stack, each stack includes only a few. Next we need to support two modes of operation:

Operation 1: 1 xy x number of the first number and where y rootlets combined stack (if the number of x or y-x has been deleted and the first or the second number in y with a stack, this is ignored operating)

Operation 2: 2 x where x is the number of output of the minimum number of stacks, and remove it (if the number of x has been deleted, and ignoring the -1 output deletion)

Input and output formats

Input formats:

The first line contains two positive integers N, M, respectively, a beginning heap root small number of operations and the number of the next.

The second line contains N a positive integer, wherein an i-th positive integer i contains the first small number of root and only the initial stack contains.

Next M lines each two or three positive integer representing an operation in the following format:

Operation 1: 1 xy

Operation 2: 2 x

Output formats:

Output contains an integer number of lines, each corresponding to the result of each operation sequentially obtained 2.

Sample input and output

Input Sample # 1:
5 5
1 5 4 2 3
1 1 5
1 2 5
2 2
1 4 2
2 2
Output Sample # 1:
1
2

Explanation

When there are multiple pile minimum, delete the original front of the priority sequence, otherwise it will affect subsequent operations 1 lead WA.

Constraints of time: 1000ms, 128M

Data Scale:

For 30% of the data: N <= 10, M <= 10

For 70% of the data: N <= 1000, M <= 1000

To 100% of the data: N <= 100000, M <= 100000

Sample Description:

Initially, five small heap root are: {1}, {5}, {4}, {2}, {3}.

The first operation, a small number of root stack where the first and the fifth root of the number of any of the small stack, are combined, it becomes the root of four small stacks: {1,3}, {5}, {4}, {2}.

The second operation, the second small number of root stack where the fifth root of the number of any of the small stack, are combined, it becomes the root of three small stacks: {1,3,5}, {4}, {2 }.

The third operation, the output of the second minimum number of stack where the rootlets and remove, so the output 1, the first number is deleted, the root of three small stacks of: {3,5}, {4}, {2}.

The fourth operation, a small number of root 4 of the stack is located and where the second number of rootlets stack, are combined so that the stack becomes two small root: {2,3,5}, {4}.

The fifth operation, where the second number of the minimum value of the output stack rootlets and remove, so the output 2, the fourth number is deleted, two stacks of rootlets: {3,5}, {4}.

So the output sequence is 1, 2.


 

First of all, this question will be very objective questions clearly tell us - this is a template problem.

Is a template problem, it should proceed from theoretical, universal common sense, and finally get a long list of code (crazy recitation)

But here, I will not speak leftist tree's basic posture drop .. (in fact, I do not) , if necessary, their own Baidu ~~~~~~~~~~

I am here to introduce the "PBDS" (flat-panel TVs) of the solution?

I do not know pbds students please poke here , here , and here .

So obvious, This question is all of a sudden there is no difficulty. Perhaps some children's shoes have reference pbds gesture made out.

Since we all, then directly on the code:

 1 //
 2 #include <bits/stdc++.h>
 3 #include <ext/pb_ds/priority_queue.hpp>
 4 using namespace std;
 5 using namespace __gnu_pbds;
 6 typedef unsigned long long ll;
 7 #define ri register ll
 8 
 9 struct A
10 {
11     ll x;
12     ll rank;
13     bool operator< (const A& p)const
14     {
15         if(x==p.x)
16         {
17             return rank>p.rank;
18         }
19         return x>p.x;
20 //        */
21     }
22 };
23 
24 __gnu_pbds::priority_queue<A,less<A>,pairing_heap_tag> q[100005];
25 ll n,m;
26 ll father[100005];
27 bool vis[100005];
28 
29 ll findf(ll a)
30 {
31     ri k=a;
32     while(father[k]!=k)
33     {
34         k=father[k];
35     }
36     father[a]=k;
37     return k;
38 }
39 
40 void joinf(ll x,ll y)
41 {
42     ri xx=findf(x);
43     ri yy=findf(y);
44     father[yy]=xx;
45 }
46 
47 signed main()
48 {
49     ios::sync_with_stdio(0),cin.tie(0);
50     cin>>n>>m;
51     for(ri i=1;i<=n;i++)
52     {
53         ri p;cin>>p;
54         q[i].push((A){p,i});
55         father[i]=i;
56     }
57     
58     for(ri i=1;i<=m;i++)
59     {
60         ri rerinput;cin>>rerinput;
61         if(rerinput==1)
62         {
63             ri x,y;cin>>x>>y;
64             ri xx=findf(x);
65             ri yy=findf(y);
66             if(q[xx].size()>q[yy].size())swap(xx,yy);
67             q[xx].join(q[yy]);
68             joinf(xx,yy);
69         }
70         else if(rerinput==2)
71         {
72             ri k;cin>>k;
73             if(vis[k])
74             {
75                 cout<<-1<<'\n';
76                 continue;
77             }
78             ri wh=findf(k);
79             cout<<q[wh].top().x<<'\n';
80             vis[q[wh].top().rank]=1;
81             q[wh].pop();
82         }
83     }
84     
85 //    */
86     return 0;
87 }
88 //

Ferguson then obtained the following results:

Good good.

My fault, my fault.

Certainly it is where I was omitted.

That review once again the subject: I found that I missed a parentheses behind the contents of the operation.

So once again changed, increasing the content of the line 66, 67

 1 //
 2 #include <bits/stdc++.h>
 3 #include <ext/pb_ds/priority_queue.hpp>
 4 using namespace std;
 5 using namespace __gnu_pbds;
 6 typedef unsigned long long ll;
 7 #define ri register ll
 8 
 9 struct A
10 {
11     ll x;
12     ll rank;
13     bool operator< (const A& p)const
14     {
15         if(x==p.x)
16         {
17             return rank>p.rank;
18         }
19         return x>p.x;
20 //        */
21     }
22 };
23 
24 __gnu_pbds::priority_queue<A,less<A>,pairing_heap_tag> q[100005];
25 ll n,m;
26 ll father[100005];
27 bool vis[100005];
28 
29 ll findf(ll a)
30 {
31     ri k=a;
32     while(father[k]!=k)
33     {
34         k=father[k];
35     }
36     father[a]=k;
37     return k;
38 }
39 
40 void joinf(ll x,ll y)
41 {
42     ri xx=findf(x);
43     ri yy=findf(y);
44     father[yy]=xx;
45 }
46 
47 signed main()
48 {
49     ios::sync_with_stdio(0),cin.tie(0);
50     cin>>n>>m;
51     for(ri i=1;i<=n;i++)
52     {
53         ri p;cin>>p;
54         q[i].push((A){p,i});
55         father[i]=i;
56     }
57     
58     for(ri i=1;i<=m;i++)
59     {
60         ri rerinput;cin>>rerinput;
61         if(rerinput==1)
62         {
63             ri x,y;cin>>x>>y;
64             ri xx=findf(x);
65             ri yy=findf(y);
66             if(vis[x]||vis[y])continue;
67             if(xx==yy)continue;
68             if(q[xx].size()>q[yy].size())swap(xx,yy);
69             q[xx].join(q[yy]);
70             joinf(xx,yy);
71         }
72         else if(rerinput==2)
73         {
74             ri k;cin>>k;
75             if(vis[k])
76             {
77                 cout<<-1<<'\n';
78                 continue;
79             }
80             ri wh=findf(k);
81             cout<<q[wh].top().x<<'\n';
82             vis[q[wh].top().rank]=1;
83             q[wh].pop();
84         }
85     }
86     
87 //    */
88     return 0;
89 }
90 //

Then happily passed. ↑↑↑↑↑↑↑↑↑↑ AC Code ↑↑↑↑↑↑↑↑↑↑

 

but! ! !

----- look at the problem solution .

dalao are already talking about a very detailed (did not understand) .

From the vast code, I found an important word ------

------ dalao has made a statement concluding the rough

 I suddenly click into place, remove the above code path compression

result:

The amount of ~ ~ Well, I do not know why

But I need the code seems to be the path compression drops.

 

summary:

This essay tells us - look at the question carefully, limit the subject is given must be considered!

Further addition of code then AC line 68: "if (q [xx] .size ()> q [yy] .size ()) swap (xx, yy);" This seems to be q [xx] .size () < q [yy] .size ().

But to be honest, I was not very clear,

If there dalao know, but also hope the exhibitions ~~

 

Guess you like

Origin www.cnblogs.com/leprechaun-kdl/p/10958223.html