#include <bits/stdc++.h>
#define file ""
#define all(x) x.begin(), x.end()
#define sc second
#define fr first
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const ll inf = 1e18 + 5;
const ll mod = 1e9 + 7;
const int N = 5e5 + 5;
int dx[] = {+1, 0, -1, 0};
int dy[] = {0, +1, 0, -1};
int n, m, k, timer;
#define int unsigned short
int used[N], mt[N], pr[N];
vector<int> g[N], all[N];
bool go(int v) {
if (used[v] == timer) return false;
used[v] = timer;
for (int to : g[v])
if (mt[to] == false) {
mt[to] = v;
pr[v] = to;
return true;
}
for (int to : g[v])
if (go(mt[to])) {
mt[to] = v;
pr[v] = to;
return true;
}
return false;
}
bool check(int y, int p) {
fill (pr + 1, pr + 1 + m, false);
for (int i = 1; i <= m; i++)
g[i].clear();
for (int i = y; i <= p; i++) {
for (int j : all[i])
g[j].pb(i);
}
for (int i = 1; i <= m; i++) {
timer++;
go(i);
}
int ok = true;
for (int i = 1; i <= m; i++)
if (pr[i] == 0) ok = false;
else mt[pr[i]] = false;
return ok;
}
signed main() {
ios_base :: sync_with_stdio(false);
cin.tie(nullptr);
srand(time(nullptr));
cin >> n >> m >> k;
for (int x, y, i = 1; i <= k; i++) {
cin >> x >> y;
all[x].pb(y);
}
ll res = 0;
for (int i = 1; i <= n; i++) {
int l = i + m - 1;
int r = n;
int ans = -1;
while (l <= r) {
int mid = l + r >> 1;
if (check(i, mid)) ans = mid, r = mid - 1;
else l = mid + 1;
}
if (ans == -1) break;
else res += (n - ans + 1);
}
cout << res;
return 0;
}
Compilation message
marriage.cpp: In function 'int main()':
marriage.cpp:85:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
85 | int mid = l + r >> 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
17 ms |
23788 KB |
Output isn't correct |
2 |
Incorrect |
19 ms |
23788 KB |
Output isn't correct |
3 |
Incorrect |
22 ms |
23788 KB |
Output isn't correct |
4 |
Incorrect |
19 ms |
23792 KB |
Output isn't correct |
5 |
Incorrect |
18 ms |
23788 KB |
Output isn't correct |
6 |
Incorrect |
20 ms |
23788 KB |
Output isn't correct |
7 |
Incorrect |
16 ms |
23788 KB |
Output isn't correct |
8 |
Correct |
15 ms |
23900 KB |
Output is correct |
9 |
Incorrect |
23 ms |
23788 KB |
Output isn't correct |
10 |
Incorrect |
16 ms |
23788 KB |
Output isn't correct |
11 |
Incorrect |
16 ms |
23788 KB |
Output isn't correct |
12 |
Incorrect |
17 ms |
23788 KB |
Output isn't correct |
13 |
Correct |
17 ms |
23788 KB |
Output is correct |
14 |
Incorrect |
16 ms |
23788 KB |
Output isn't correct |
15 |
Incorrect |
18 ms |
23788 KB |
Output isn't correct |
16 |
Incorrect |
16 ms |
23788 KB |
Output isn't correct |
17 |
Incorrect |
15 ms |
23788 KB |
Output isn't correct |
18 |
Incorrect |
17 ms |
23788 KB |
Output isn't correct |
19 |
Incorrect |
18 ms |
23836 KB |
Output isn't correct |
20 |
Incorrect |
23 ms |
23788 KB |
Output isn't correct |
21 |
Incorrect |
17 ms |
23788 KB |
Output isn't correct |
22 |
Incorrect |
16 ms |
23788 KB |
Output isn't correct |
23 |
Incorrect |
19 ms |
23788 KB |
Output isn't correct |
24 |
Incorrect |
18 ms |
23788 KB |
Output isn't correct |
25 |
Runtime error |
298 ms |
262148 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
26 |
Runtime error |
175 ms |
262148 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
27 |
Runtime error |
165 ms |
262148 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
28 |
Runtime error |
179 ms |
262148 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
29 |
Incorrect |
115 ms |
23916 KB |
Output isn't correct |
30 |
Incorrect |
102 ms |
23836 KB |
Output isn't correct |
31 |
Runtime error |
307 ms |
262148 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
32 |
Runtime error |
202 ms |
262148 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
33 |
Runtime error |
170 ms |
262148 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
34 |
Runtime error |
161 ms |
262148 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
35 |
Execution timed out |
1588 ms |
155580 KB |
Time limit exceeded |
36 |
Execution timed out |
1549 ms |
155588 KB |
Time limit exceeded |
37 |
Runtime error |
230 ms |
262148 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
38 |
Execution timed out |
1580 ms |
155656 KB |
Time limit exceeded |
39 |
Runtime error |
174 ms |
262148 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
40 |
Runtime error |
180 ms |
262148 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
41 |
Runtime error |
181 ms |
262144 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
42 |
Execution timed out |
1572 ms |
24360 KB |
Time limit exceeded |
43 |
Execution timed out |
1546 ms |
24428 KB |
Time limit exceeded |
44 |
Execution timed out |
1600 ms |
155728 KB |
Time limit exceeded |
45 |
Execution timed out |
1519 ms |
24556 KB |
Time limit exceeded |
46 |
Execution timed out |
1576 ms |
156344 KB |
Time limit exceeded |
47 |
Execution timed out |
1578 ms |
156188 KB |
Time limit exceeded |
48 |
Execution timed out |
1565 ms |
156240 KB |
Time limit exceeded |
49 |
Execution timed out |
1581 ms |
156348 KB |
Time limit exceeded |
50 |
Runtime error |
184 ms |
262148 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |