/// wa
#pragma GCC optimize("O3")
//#pragma comment(linker, "/STACK:36777216")
#include<bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define sz(x) (int) x.size()
#define all(z) (z).begin(), (z).end()
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int MOD = 1e9 + 7;
const int INF = 1e9 + 1e2;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void fin() {
#ifdef AM
freopen(".in", "r", stdin);
#endif
}
const bool flag = 0;
const int N = 1e5+10;
int was[N];
int mt[N], p[N], tmp;
vector<int> adj[N], g[N];
bool dfs(int x) {
if (was[x] == tmp) {
return 0;
}
was[x] = tmp;
for (int to : adj[x]) {
if (mt[to] == -1) {
mt[to] = x;
p[x] = to;
return 1;
}
}
for (int to : adj[x]) {
if (dfs(mt[to])) {
mt[to] = x;
p[x] = to;
return 1;
}
}
return 0;
}
int n, m, k;
bool check(int l, int r) {
for (int i = 0; i < m; ++i) {
adj[i].clear();
p[i] = -1;
}
for (int i = l; i <= r; ++i) {
for (int to : g[i]) {
adj[to].pb(i);
}
}
for (int i = 0; i < m; ++i) {
tmp++;
dfs(i);
}
int ok = 1;
for (int i = 0; i < m; ++i) {
if (p[i] == -1) {
ok = 0;
} else {
mt[p[i]] = -1;
}
}
return ok;
}
void ma1n() {
cin >> n >> m >> k;
for (int u, v; k--;) {
cin >> u >> v;
u--, v--;
g[u].pb(v);
}
ll ans = 0;
memset(mt, -1, sizeof(mt));
for (int i = 0; i < n; ++i) {
int l = i;
int r = n-1;
int j = -1;
while (l <= r) {
int mid = l+r >> 1;
if (check(i, mid)) {
j = mid, r = mid-1;
} else {
l = mid+1;
}
}
if (~j) {
ans += n-j;
} else break;
}
cout << ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(nullptr), fin();
int ts = 1;
if (flag) {
cin >> ts;
}
while (ts--) {
ma1n();
}
return 0;
}
Compilation message
marriage.cpp: In function 'void ma1n()':
marriage.cpp:103:24: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
103 | int mid = l+r >> 1;
| ~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
5484 KB |
Output is correct |
2 |
Correct |
4 ms |
5484 KB |
Output is correct |
3 |
Correct |
4 ms |
5484 KB |
Output is correct |
4 |
Correct |
4 ms |
5484 KB |
Output is correct |
5 |
Correct |
4 ms |
5484 KB |
Output is correct |
6 |
Correct |
6 ms |
5484 KB |
Output is correct |
7 |
Correct |
5 ms |
5484 KB |
Output is correct |
8 |
Correct |
4 ms |
5484 KB |
Output is correct |
9 |
Correct |
7 ms |
5484 KB |
Output is correct |
10 |
Correct |
4 ms |
5484 KB |
Output is correct |
11 |
Correct |
5 ms |
5484 KB |
Output is correct |
12 |
Correct |
4 ms |
5484 KB |
Output is correct |
13 |
Correct |
4 ms |
5484 KB |
Output is correct |
14 |
Correct |
4 ms |
5484 KB |
Output is correct |
15 |
Correct |
4 ms |
5484 KB |
Output is correct |
16 |
Correct |
4 ms |
5484 KB |
Output is correct |
17 |
Correct |
4 ms |
5484 KB |
Output is correct |
18 |
Correct |
4 ms |
5484 KB |
Output is correct |
19 |
Correct |
14 ms |
5528 KB |
Output is correct |
20 |
Correct |
8 ms |
5484 KB |
Output is correct |
21 |
Correct |
4 ms |
5484 KB |
Output is correct |
22 |
Correct |
6 ms |
5484 KB |
Output is correct |
23 |
Correct |
6 ms |
5484 KB |
Output is correct |
24 |
Correct |
6 ms |
5484 KB |
Output is correct |
25 |
Correct |
291 ms |
5628 KB |
Output is correct |
26 |
Correct |
60 ms |
5484 KB |
Output is correct |
27 |
Correct |
5 ms |
5484 KB |
Output is correct |
28 |
Correct |
12 ms |
5484 KB |
Output is correct |
29 |
Correct |
57 ms |
5600 KB |
Output is correct |
30 |
Correct |
42 ms |
5484 KB |
Output is correct |
31 |
Execution timed out |
1580 ms |
6076 KB |
Time limit exceeded |
32 |
Correct |
251 ms |
5596 KB |
Output is correct |
33 |
Correct |
4 ms |
5484 KB |
Output is correct |
34 |
Correct |
63 ms |
5484 KB |
Output is correct |
35 |
Execution timed out |
1571 ms |
6500 KB |
Time limit exceeded |
36 |
Execution timed out |
1574 ms |
6508 KB |
Time limit exceeded |
37 |
Execution timed out |
1579 ms |
6124 KB |
Time limit exceeded |
38 |
Correct |
785 ms |
6784 KB |
Output is correct |
39 |
Execution timed out |
1568 ms |
5740 KB |
Time limit exceeded |
40 |
Correct |
10 ms |
5740 KB |
Output is correct |
41 |
Execution timed out |
1587 ms |
5868 KB |
Time limit exceeded |
42 |
Correct |
169 ms |
5996 KB |
Output is correct |
43 |
Correct |
322 ms |
6124 KB |
Output is correct |
44 |
Correct |
441 ms |
6652 KB |
Output is correct |
45 |
Execution timed out |
1591 ms |
6208 KB |
Time limit exceeded |
46 |
Execution timed out |
1563 ms |
6896 KB |
Time limit exceeded |
47 |
Correct |
807 ms |
6916 KB |
Output is correct |
48 |
Correct |
619 ms |
6916 KB |
Output is correct |
49 |
Execution timed out |
1572 ms |
6960 KB |
Time limit exceeded |
50 |
Execution timed out |
1568 ms |
5740 KB |
Time limit exceeded |