#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);
}
set<int> unmatched;
for(int i = 1;i<=m;i++){
unmatched.insert(i);
}
r = 0;
int ans = 0;
for(int i = 1;i<=n;i++){
l = i;
reset(m);
while(unmatched.size() && try_kuhn(*unmatched.begin())){
reset(m);
unmatched.erase(unmatched.begin());
}
while(r != n && unmatched.size()){
r++;
reset(m);
while(unmatched.size() && try_kuhn(*unmatched.begin())){
reset(m);
unmatched.erase(unmatched.begin());
}
}
if(r == n && unmatched.size())break;
ans += n - r + 1;
if(mt[i]){
unmatched.insert(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
}
# |
Verdict |
Execution time |
Memory |
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 |
1108 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 |
2 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 |
32 ms |
1144 KB |
Output is correct |
26 |
Correct |
4 ms |
1072 KB |
Output is correct |
27 |
Correct |
1 ms |
980 KB |
Output is correct |
28 |
Correct |
2 ms |
980 KB |
Output is correct |
29 |
Correct |
3 ms |
980 KB |
Output is correct |
30 |
Correct |
2 ms |
980 KB |
Output is correct |
31 |
Correct |
229 ms |
1456 KB |
Output is correct |
32 |
Correct |
2 ms |
1108 KB |
Output is correct |
33 |
Correct |
1 ms |
980 KB |
Output is correct |
34 |
Correct |
4 ms |
1108 KB |
Output is correct |
35 |
Correct |
58 ms |
1580 KB |
Output is correct |
36 |
Correct |
43 ms |
1492 KB |
Output is correct |
37 |
Correct |
740 ms |
1504 KB |
Output is correct |
38 |
Correct |
15 ms |
1620 KB |
Output is correct |
39 |
Correct |
86 ms |
1260 KB |
Output is correct |
40 |
Correct |
4 ms |
1236 KB |
Output is correct |
41 |
Correct |
6 ms |
1236 KB |
Output is correct |
42 |
Correct |
10 ms |
1236 KB |
Output is correct |
43 |
Correct |
10 ms |
1324 KB |
Output is correct |
44 |
Correct |
19 ms |
1620 KB |
Output is correct |
45 |
Correct |
11 ms |
1368 KB |
Output is correct |
46 |
Correct |
22 ms |
1764 KB |
Output is correct |
47 |
Correct |
19 ms |
1760 KB |
Output is correct |
48 |
Correct |
17 ms |
1748 KB |
Output is correct |
49 |
Correct |
29 ms |
1780 KB |
Output is correct |
50 |
Correct |
203 ms |
1312 KB |
Output is correct |