202992026-01-05 23:51:46ubormaciRobotokcpp17Time limit exceeded 16/50500ms10216 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;

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

	vector<ll> block(n+1, 0);

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

	ll ans = 0;
	for(ll i = n; i >= 1; i--) {

		if(s[i].empty()) {
			continue;
		}

		if(block[i] < *s[i].rbegin()) {

			//cerr << "\n\nnew start";
			//cerr << "\nblocks=" << block;

			//cerr << "\nrow=" << s[i];

			ans++;
			
			ll x = i;
			ll j = *s[x].upper_bound(block[x]);

			block[x] = m+1;

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

			while(x > 0) {

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

				auto fst = s[x].upper_bound(block[x]);

				//cerr << "\nfst=" << *fst;

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

					block[x] = max(block[x], j);

					//cerr << "\nmove up";

					//s[x].erase(j);
					for(ll up = x -1; up >= 0; up--) {

						if(up == 0) {
							x = 0;
							break;
						}

						if(s[up].empty()) {
							continue;
						}

						auto fstup = s[up].upper_bound(block[up]);
						if(fstup == s[up].end()) {
							continue;
						}

						//cerr << "\nup=" << up << "; fst=" << *fstup;

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

					}

				}else{

					//cerr << "\nmove to front of the row";

					block[x] = max(block[x], j);
					j = *fst;

					//cerr << "\nj=" << j << "; block[x]=" << x;
					//cerr << "\nstay on the same row";

				}
			}
		}

	}

	cout << ans << '\n';
	

}

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

	solve();

	return 0;
}
SubtaskSumTestVerdictTimeMemory
base16/50
1Accepted0/01ms500 KiB
2Time limit exceeded0/0483ms9268 KiB
3Accepted2/21ms316 KiB
4Accepted2/21ms508 KiB
5Accepted2/21ms316 KiB
6Wrong answer0/23ms564 KiB
7Wrong answer0/23ms564 KiB
8Accepted2/22ms564 KiB
9Accepted2/22ms756 KiB
10Accepted2/21ms564 KiB
11Accepted2/28ms1188 KiB
12Wrong answer0/227ms2692 KiB
13Accepted2/23ms820 KiB
14Time limit exceeded0/2500ms9976 KiB
15Time limit exceeded0/2500ms10208 KiB
16Time limit exceeded0/2500ms7476 KiB
17Time limit exceeded0/4488ms10216 KiB
18Time limit exceeded0/6481ms10212 KiB
19Time limit exceeded0/6485ms10032 KiB
20Time limit exceeded0/6500ms10212 KiB