#include <bits/stdc++.h>
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("-O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define vec vector
#define sz(x) ( int)x.size()
#define m_p make_pair
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
using namespace std;
typedef long long ll;
typedef pair<ll, int> pli;
typedef pair<int,ll> pil;
typedef pair<int,int> pii;
typedef unsigned long long ull;
auto rng=bind(uniform_int_distribution<int>(1,1000),mt19937(time(0)));
template<class T> bool umax(T &a,const T &b){return (a<b?a=b,1:0);}
template<class T> bool umin(T &a,const T &b){return (a>b?a=b,1:0);}
const int N=3e4+1;
const int K=165;
const int M=2001;
vec<int> g[N];
int mt[N];
int wh[N];
int u[N],tt,n,m;
int tl,tr;
bool kuhn(int v){
if(u[v]==tt) return 0;
u[v]=tt;
for(auto &z : g[v]){
if(z>=tl && z<=tr && mt[z]==-1){
mt[z]=v;
wh[v]=z;
return 1;
}
}
for(auto &z : g[v]){
if(z>=tl && z<=tr && kuhn(mt[z])){
mt[z]=v;
wh[v]=z;
return 1;
}
}
return 0;
}
int check(int l,int r){
tl=l;tr=r;
for(int i=0;i<m;i++){
if(wh[i]==-1) continue;
mt[wh[i]]=-1;
wh[i]=-1;
}
for(int i=0;i<m;i++){
tt++;
if(!kuhn(i)) return 0;
}
return 1;
}
signed main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int k;
cin>>n>>m>>k;
for(int i=0;i<k;i++){
int v,u;
cin>>v>>u;--v;--u;
g[u].pb(v);
}
for(int i=0;i<N;i++){
mt[i]=-1;
wh[i]=-1;
}
int l=n-1;
// cout<<check(1,4);
while(l>=0 && !check(l,n-1)) l--;
int r=n-1;
ll ans=0;
// cout<<check(1,4);
for(int i=l;i>=0;i--){
// cerr<<i<<' '<<r<<endl;
while(check(i,r-1)) r--;
// cout<<i<<' '<<r<<endl;
ans+=(n-r);
}
cout<<ans;
return 0;
}
/*
5
1 2
2 3
3 4
4 5
5 1
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1228 KB |
Output is correct |
2 |
Correct |
1 ms |
1228 KB |
Output is correct |
3 |
Correct |
1 ms |
1228 KB |
Output is correct |
4 |
Correct |
1 ms |
1228 KB |
Output is correct |
5 |
Correct |
1 ms |
1228 KB |
Output is correct |
6 |
Correct |
1 ms |
1228 KB |
Output is correct |
7 |
Correct |
1 ms |
1228 KB |
Output is correct |
8 |
Correct |
1 ms |
1228 KB |
Output is correct |
9 |
Correct |
1 ms |
1228 KB |
Output is correct |
10 |
Correct |
1 ms |
1228 KB |
Output is correct |
11 |
Correct |
1 ms |
1228 KB |
Output is correct |
12 |
Correct |
1 ms |
1228 KB |
Output is correct |
13 |
Correct |
1 ms |
1228 KB |
Output is correct |
14 |
Correct |
1 ms |
1228 KB |
Output is correct |
15 |
Correct |
1 ms |
1228 KB |
Output is correct |
16 |
Correct |
1 ms |
1228 KB |
Output is correct |
17 |
Correct |
1 ms |
1228 KB |
Output is correct |
18 |
Correct |
1 ms |
1228 KB |
Output is correct |
19 |
Correct |
5 ms |
1228 KB |
Output is correct |
20 |
Correct |
2 ms |
1228 KB |
Output is correct |
21 |
Correct |
1 ms |
1228 KB |
Output is correct |
22 |
Correct |
1 ms |
1228 KB |
Output is correct |
23 |
Correct |
1 ms |
1228 KB |
Output is correct |
24 |
Correct |
1 ms |
1228 KB |
Output is correct |
25 |
Correct |
82 ms |
1356 KB |
Output is correct |
26 |
Correct |
17 ms |
1292 KB |
Output is correct |
27 |
Correct |
1 ms |
1228 KB |
Output is correct |
28 |
Correct |
2 ms |
1228 KB |
Output is correct |
29 |
Correct |
3 ms |
1228 KB |
Output is correct |
30 |
Correct |
3 ms |
1228 KB |
Output is correct |
31 |
Correct |
607 ms |
1572 KB |
Output is correct |
32 |
Correct |
8 ms |
1228 KB |
Output is correct |
33 |
Correct |
2 ms |
1228 KB |
Output is correct |
34 |
Correct |
8 ms |
1228 KB |
Output is correct |
35 |
Correct |
221 ms |
1760 KB |
Output is correct |
36 |
Correct |
170 ms |
1768 KB |
Output is correct |
37 |
Execution timed out |
1585 ms |
1708 KB |
Time limit exceeded |
38 |
Correct |
53 ms |
1740 KB |
Output is correct |
39 |
Correct |
434 ms |
1408 KB |
Output is correct |
40 |
Correct |
26 ms |
1360 KB |
Output is correct |
41 |
Correct |
129 ms |
1464 KB |
Output is correct |
42 |
Correct |
38 ms |
1476 KB |
Output is correct |
43 |
Correct |
39 ms |
1456 KB |
Output is correct |
44 |
Correct |
95 ms |
1784 KB |
Output is correct |
45 |
Correct |
1112 ms |
1728 KB |
Output is correct |
46 |
Execution timed out |
1593 ms |
1728 KB |
Time limit exceeded |
47 |
Correct |
128 ms |
1740 KB |
Output is correct |
48 |
Correct |
127 ms |
1736 KB |
Output is correct |
49 |
Execution timed out |
1579 ms |
1876 KB |
Time limit exceeded |
50 |
Correct |
1486 ms |
1400 KB |
Output is correct |