Skip to content

Commit 4f8270c

Browse files
committed
Solve MonoQueue & Kruskal
1 parent 1384cb9 commit 4f8270c

File tree

2 files changed

+48
-82
lines changed

2 files changed

+48
-82
lines changed

KolejkaMonotoniczna.cpp

+22-40
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,24 @@
1-
#include<bits/stdc++.h>
1+
#include <bits/stdc++.h>
2+
23
using namespace std;
3-
#define FOR(i,a,b) for(int i = (a); i <= (b); ++i)
4-
#define FORD(i,a,b) for(int i = (b); i >= (a); --i)
5-
#define TRAV(x,T) for(auto& x: (T))
6-
#define ALL(T) T.begin(), T.end()
7-
#define TAB(T,a,b) (T)+a, (T)+((b)+1)
8-
#define VAR(x) #x<<" = "<<x<<" "
9-
#define sz(x) (int)(x).size()
10-
#define nwd __gcd
11-
#define pb push_back
12-
#define st first
13-
#define nd second
14-
#define lc (v<<1)
15-
#define rc (v<<1|1)
16-
typedef long long ll;
17-
typedef long double ld;
18-
typedef pair<int, int> pii;
19-
typedef pair<int, ll> pil;
20-
typedef pair<ll, int> pli;
21-
typedef pair<ll, ll> pll;
22-
typedef vector<int> vi;
23-
#define deb if(0)
24-
const ll INF = (ll)1e18 + 2;
254

26-
struct Monqueue {
27-
int pops = 0, pushes = 0;
28-
deque<pli> Q;
29-
void push(const ll &val) {
30-
while(!Q.empty() and Q.back().st <= val) Q.pop_back();
31-
pushes++;
32-
Q.push_back({val, pushes});
33-
}
34-
void pop() {
35-
pops++;
36-
if(!Q.empty() and pops == Q.front().nd) Q.pop_front();
37-
}
38-
ll max() {
39-
if(Q.empty()) return -INF;
40-
return Q.front().st;
41-
}
42-
};
5+
struct MonoQueue {
6+
int pops = 0, pushes = 0;
7+
deque<pair<long long, int>> Q;
8+
void push(const long long &val) {
9+
while (!Q.empty() and Q.back().first <= val)
10+
Q.pop_back();
11+
pushes++;
12+
Q.push_back({val, pushes});
13+
}
14+
void pop() {
15+
pops++;
16+
if (!Q.empty() and pops == Q.front().second)
17+
Q.pop_front();
18+
}
19+
long long max() {
20+
if (Q.empty())
21+
return -LONG_LONG_MAX;
22+
return Q.front().first;
23+
}
24+
};

Kruskal.cpp

+26-42
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,45 @@
1-
#include<bits/stdc++.h>
1+
#include <bits/stdc++.h>
2+
23
using namespace std;
3-
#define FOR(i,a,b) for(int i = (a); i <= (b); ++i)
4-
#define FORD(i,a,b) for(int i = (b); i >= (a); --i)
5-
#define TRAV(x,T) for(auto& x: (T))
6-
#define ALL(T) T.begin(), T.end()
7-
#define TAB(T,a,b) (T)+a, (T)+((b)+1)
8-
#define VAR(x) #x<<" = "<<x<<" "
9-
#define sz(x) (int)(x).size()
10-
#define nwd __gcd
11-
#define pb push_back
12-
#define st first
13-
#define nd second
14-
#define lc (v<<1)
15-
#define rc (v<<1|1)
16-
typedef long long ll;
17-
typedef long double ld;
18-
typedef pair<int, int> pii;
19-
typedef pair<ll, ll> pll;
20-
typedef vector<int> vi;
21-
#define deb if(0)
4+
225
const int N = 1e6, NT = N + 2;
236

247
int REP[NT], ILE[NT];
258

269
void Init(int n) {
27-
FOR(i, 1, n) {
28-
REP[i] = i;
29-
ILE[i] = 1;
30-
}
10+
for (int i = 1; i <= n; i++) {
11+
REP[i] = i;
12+
ILE[i] = 1;
13+
}
3114
}
3215

3316
void Union(int a, int b) { // O(1)
34-
if(ILE[a] > ILE[b]) swap(a, b);
35-
REP[a] = b;
36-
ILE[b] += ILE[a];
17+
if (ILE[a] > ILE[b])
18+
swap(a, b);
19+
REP[a] = b;
20+
ILE[b] += ILE[a];
3721
}
3822

3923
int Find(int a) { // O(stala ackermana)
40-
if(REP[a] != a) REP[a] = Find(REP[a]);
41-
return REP[a];
24+
if (REP[a] != a)
25+
REP[a] = Find(REP[a]);
26+
return REP[a];
4227
}
4328

4429
struct Edge {
45-
int a, b, w;
30+
int a, b, w;
4631
} K[NT];
4732

4833
void Kruskal(int n, int m) {
49-
Init(n);
50-
sort(TAB(K, 1, m), [](const Edge &a, const Edge &b) {
51-
return a.w < b.w;
52-
});
53-
int a, b;
54-
FOR(i, 1, m) {
55-
a = Find(K[i].a);
56-
b = Find(K[i].b);
57-
if(a != b) {
58-
Union(a, b);
59-
}
34+
Init(n);
35+
sort(K + 1, K + (m + 1),
36+
[](const Edge &a, const Edge &b) { return a.w < b.w; });
37+
int a, b;
38+
for (int i = 1; i <= m; i++) {
39+
a = Find(K[i].a);
40+
b = Find(K[i].b);
41+
if (a != b) {
42+
Union(a, b);
6043
}
61-
}
44+
}
45+
}

0 commit comments

Comments
 (0)