206372026-01-08 12:05:23ubormaciRobotokcpp17Time limit exceeded 22/50500ms14400 KiB
#include <iostream>
#include <algorithm> // for sort, mainly
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <array>
#include <string>
#include <cstdio>
#include <iterator>
#include <unordered_set>
#include <cstdint> // for int64_t, int32_t, etc
#include <queue>
#include <stack>
#include <deque>
#include <numeric> // gcd, lcm
#include <fstream>
#include <bitset> // for bitset
#include <iomanip>
#include <cassert> // for set with custom ordering
#include <type_traits> // for set with custom ordering
#include <utility> // for swap, forward, etc
using namespace std;

#pragma GCC optimize("O2")
// #pragma GCC optimize("O1","O2","O3","Ofast","unroll-loops")
// #pragma GCC target("sse","sse2","sse3","sse4.1","sse4.2","avx","avx2","fma")

template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cout << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << H; dbg_out(T...); }
#ifdef LOCAL
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

/*

notes:

int64_t
stoi(string s) -> string to int
to_string() -> int (or else) to string

vector declaration:
vector<ll> v(n, 0)
vector<vector<ll>> v(n, vector<ll>(n, 0));

{if statement} ? {truth value} : {false value}

#ifdef LOCAL
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
#endif

std::lcm(ll a, ll b), std::gcd(int a, int b)

cout << fixed << setprecision(n);

set with custom ordering
set<ll, decltype(&cmp)> qu(cmp);

*/

typedef int32_t ll;

void solve() {

	ll n, m, k;
	cin >> n >> m >> k;

    set<ll> remcol;
	vector<set<ll>> s(n+1); // for each column

	for(ll q = 0; q < k; q++) {
		ll i, j;
		cin >> i >> j;
		s[i].insert(j);
        remcol.insert(i);
    }

	ll ans = 0;
    while(!remcol.empty()) {

        //cerr << "\nremcol=" << remcol;

        ll curr = *remcol.rbegin();
        remcol.erase(curr);
        ans++;

        ll x = curr;
        ll j = *s[x].rbegin();

        //cerr << "\nstartval: ";
        //cerr << "x=" << x << "; j=" << j;

		while(x > 0) {

			//cerr << "\nx=" << x << "; j=" << j;

            if(j == *s[x].begin()) {

                //cerr << "\nmove up";

                s[x].erase(j);
                if(s[x].empty()) {
                    remcol.erase(x);
                }

                if(remcol.empty()) {
                    x = 0;
                    break;
                }

                if(x <= *remcol.begin()) {
                    x = 0;
                    break;
                }

                auto it = prev(remcol.upper_bound(x));
                while(1) {

                    ll up = *it;

                    if(*s[up].begin() <= j) {
                        x = up;
                        j = *prev(s[x].upper_bound(j));
                        break;
                    }

                    if(it == remcol.begin()) {
                        x = 0;
                        break;
                    }else{
                        it = prev(it);
                    }
                    
                }

            }else{

                //cerr << "\nstay on the same row";
                s[x].erase(j);
                j = *prev(s[x].upper_bound(j));

            }
        }
    }
    
	cout << ans << '\n';
	

}

int main()
{
	std::ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);

	solve();

	return 0;
}
SubtaskSumTestVerdictTimeMemory
base22/50
1Accepted0/01ms316 KiB
2Time limit exceeded0/0483ms11300 KiB
3Accepted2/21ms316 KiB
4Accepted2/21ms324 KiB
5Accepted2/21ms316 KiB
6Accepted2/23ms488 KiB
7Accepted2/23ms564 KiB
8Accepted2/22ms564 KiB
9Accepted2/22ms564 KiB
10Accepted2/22ms564 KiB
11Accepted2/28ms1332 KiB
12Accepted2/239ms2760 KiB
13Accepted2/22ms820 KiB
14Time limit exceeded0/2500ms12888 KiB
15Time limit exceeded0/2500ms14316 KiB
16Time limit exceeded0/2479ms9788 KiB
17Time limit exceeded0/4486ms14392 KiB
18Time limit exceeded0/6485ms14400 KiB
19Time limit exceeded0/6488ms14388 KiB
20Time limit exceeded0/6500ms12084 KiB