#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m, k;
cin >> n >> m >> k;
vector<vector<int>> g(m);
vector<vector<int>> r(n);
for (int i = 0; i < k; i++) {
int x, y;
cin >> x >> y;
--x; --y;
g[y].push_back(x);
r[x].push_back(y);
}
for (int i = 0; i < m; i++) {
sort(g[i].begin(), g[i].end());
}
vector<int> f(m);
int ptr = 0;
auto Good = [&]() {
vector<bool> used(m);
vector<int> to(n, -1);
function<bool(int)> Dfs = [&](int v) {
if (used[v]) {
return false;
}
used[v] = true;
for (int j = f[v]; j < (int) g[v].size(); j++) {
if (g[v][j] > ptr) {
break;
}
if (to[g[v][j]] == -1 || Dfs(to[g[v][j]])) {
to[g[v][j]] = v;
return true;
}
}
return false;
};
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
used[j] = false;
}
Dfs(i);
}
int cnt = 0;
for (int i = 0; i < n; i++) {
if (to[i] != -1) {
cnt += 1;
}
}
return cnt == m;
};
long long res = 0;
for (int i = 0; i < n; i++) {
while (ptr < n && (ptr - i + 1 < m || !Good())) {
ptr += 1;
}
res += n - ptr;
for (int j : r[i]) {
f[j] += 1;
}
}
cout << res << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
452 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
5 ms |
348 KB |
Output is correct |
20 |
Correct |
2 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
2 ms |
348 KB |
Output is correct |
23 |
Correct |
6 ms |
460 KB |
Output is correct |
24 |
Correct |
5 ms |
348 KB |
Output is correct |
25 |
Correct |
125 ms |
604 KB |
Output is correct |
26 |
Correct |
41 ms |
516 KB |
Output is correct |
27 |
Correct |
8 ms |
344 KB |
Output is correct |
28 |
Correct |
40 ms |
348 KB |
Output is correct |
29 |
Correct |
15 ms |
600 KB |
Output is correct |
30 |
Correct |
13 ms |
600 KB |
Output is correct |
31 |
Execution timed out |
1539 ms |
1372 KB |
Time limit exceeded |
32 |
Correct |
594 ms |
604 KB |
Output is correct |
33 |
Correct |
199 ms |
500 KB |
Output is correct |
34 |
Correct |
1471 ms |
548 KB |
Output is correct |
35 |
Execution timed out |
1551 ms |
2140 KB |
Time limit exceeded |
36 |
Execution timed out |
1536 ms |
2140 KB |
Time limit exceeded |
37 |
Execution timed out |
1555 ms |
1688 KB |
Time limit exceeded |
38 |
Execution timed out |
1524 ms |
2624 KB |
Time limit exceeded |
39 |
Execution timed out |
1595 ms |
1112 KB |
Time limit exceeded |
40 |
Execution timed out |
1556 ms |
1116 KB |
Time limit exceeded |
41 |
Execution timed out |
1526 ms |
1448 KB |
Time limit exceeded |
42 |
Execution timed out |
1570 ms |
1628 KB |
Time limit exceeded |
43 |
Execution timed out |
1532 ms |
1880 KB |
Time limit exceeded |
44 |
Execution timed out |
1539 ms |
2664 KB |
Time limit exceeded |
45 |
Execution timed out |
1551 ms |
2392 KB |
Time limit exceeded |
46 |
Execution timed out |
1527 ms |
3796 KB |
Time limit exceeded |
47 |
Execution timed out |
1560 ms |
3668 KB |
Time limit exceeded |
48 |
Execution timed out |
1524 ms |
3672 KB |
Time limit exceeded |
49 |
Execution timed out |
1555 ms |
4000 KB |
Time limit exceeded |
50 |
Execution timed out |
1542 ms |
1368 KB |
Time limit exceeded |