#include <bits/stdc++.h>
#define N 30005
using namespace std;
vector<int> adj[N];
int mt[N];
int l,r;
bool vis[N];
void reset(int x){
for(int i = 0;i<=x;i++){
vis[i] = 0;
}
}
bool try_kuhn(int v){
if(vis[v])
return 0;
vis[v] = 1;
for(auto u:adj[v]){
if(u < l || r < u)continue;
if(mt[u] == 0 || try_kuhn(mt[u])){
mt[u] = v;
return 1;
}
}
return 0;
}
void solve(){
int n,m,k;
cin >> n >> m >> k;
for(int i = 1;i<=k;i++){
int u,v;
cin >> u >> v;
adj[v].push_back(u);
}
vector<int> unmatched;
for(int i = 1;i<=m;i++){
unmatched.push_back(i);
}
r = 0;
int ans = 0;
for(int i = 1;i<=n;i++){
l = i;
r = max(r,i+m-1);
reset(m);
while(unmatched.size() && try_kuhn(unmatched.back())){
reset(m);
unmatched.pop_back();
}
while(r < n && unmatched.size()){
r++;
reset(m);
while(unmatched.size() && try_kuhn(unmatched.back())){
reset(m);
unmatched.pop_back();
}
}
if(unmatched.size())break;
ans += n - r + 1;
if(mt[i]){
unmatched.push_back(mt[i]);
}
}
cout << ans;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef Local
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int t = 1;
//cin >> t;
while(t--){
solve();
}
#ifdef Local
cout << endl << fixed << setprecision(2) << 1000.0*clock()/CLOCKS_PER_SEC << " milliseconds.";
#endif
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
1 ms |
980 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
1 ms |
980 KB |
Output is correct |
15 |
Correct |
1 ms |
980 KB |
Output is correct |
16 |
Correct |
1 ms |
980 KB |
Output is correct |
17 |
Correct |
1 ms |
980 KB |
Output is correct |
18 |
Correct |
1 ms |
980 KB |
Output is correct |
19 |
Correct |
1 ms |
980 KB |
Output is correct |
20 |
Correct |
1 ms |
980 KB |
Output is correct |
21 |
Correct |
1 ms |
980 KB |
Output is correct |
22 |
Correct |
1 ms |
980 KB |
Output is correct |
23 |
Correct |
1 ms |
980 KB |
Output is correct |
24 |
Correct |
1 ms |
980 KB |
Output is correct |
25 |
Correct |
4 ms |
1040 KB |
Output is correct |
26 |
Correct |
4 ms |
1072 KB |
Output is correct |
27 |
Correct |
1 ms |
980 KB |
Output is correct |
28 |
Correct |
1 ms |
980 KB |
Output is correct |
29 |
Correct |
2 ms |
980 KB |
Output is correct |
30 |
Correct |
2 ms |
980 KB |
Output is correct |
31 |
Correct |
13 ms |
1264 KB |
Output is correct |
32 |
Correct |
1 ms |
980 KB |
Output is correct |
33 |
Correct |
1 ms |
980 KB |
Output is correct |
34 |
Correct |
2 ms |
980 KB |
Output is correct |
35 |
Correct |
33 ms |
1532 KB |
Output is correct |
36 |
Correct |
23 ms |
1584 KB |
Output is correct |
37 |
Correct |
50 ms |
1364 KB |
Output is correct |
38 |
Correct |
15 ms |
1536 KB |
Output is correct |
39 |
Correct |
45 ms |
1136 KB |
Output is correct |
40 |
Correct |
3 ms |
1108 KB |
Output is correct |
41 |
Correct |
5 ms |
1236 KB |
Output is correct |
42 |
Correct |
7 ms |
1236 KB |
Output is correct |
43 |
Correct |
9 ms |
1364 KB |
Output is correct |
44 |
Correct |
17 ms |
1608 KB |
Output is correct |
45 |
Correct |
10 ms |
1364 KB |
Output is correct |
46 |
Correct |
23 ms |
1724 KB |
Output is correct |
47 |
Correct |
22 ms |
1700 KB |
Output is correct |
48 |
Correct |
19 ms |
1672 KB |
Output is correct |
49 |
Correct |
30 ms |
1732 KB |
Output is correct |
50 |
Correct |
158 ms |
1108 KB |
Output is correct |