Language: C++

#include <cmath>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <list>
#include <time.h>
#include <math.h>
#include <random>
#include <deque>
#include <queue>
#include <cassert>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <chrono>



#define real ORLY
#define endl '\n'
#define mp make_pair
#define fc first
#define sc second

using namespace std;

typedef long long ll;

const int MAXN = 2000000 + 7;
int t[4 * MAXN];
int d[4 * MAXN];
vector <int> bomb[MAXN];

void push(int v, int l, int r) {
    if (d[v] != -1) {
        int m = (l + r) / 2;
        t[v * 2 + 1] = d[v] * (m - l);
        t[v * 2 + 2] = d[v] * (r - m);
        d[v * 2 + 1] = d[v];
        d[v * 2 + 2] = d[v];
        d[v] = -1;
    }
}

void put(int v, int l, int r, int tl, int tr, int x) {
    if (tl >= r || tr <= l) {
        return;
    } else if (tl >= l && tr <= r) {
        t[v] = (tr - tl) * x;
        d[v] = x;
    } else {
        int tm = (tl + tr) / 2;
        push(v, tl, tr);
        put(v * 2 + 1, l, r, tl, tm, x);
        put(v * 2 + 2, l, r, tm, tr, x);
        t[v] = t[v * 2 + 1] + t[v * 2 + 2];
    }
}

int got(int v, int l, int r, int tl, int tr) {
    if (tl >= r || tr <= l) {
        return 0;
    } else if (tl >= l && tr <= r) {
        return t[v];
    } else {
        int tm = (tl + tr) / 2;
        push(v, tl, tr);
        return got(v * 2 + 1, l, r, tl, tm) + got(v * 2 + 2, l, r, tm, tr);
    }
}

int get(int v, int l, int r, int x) {
    if (r - l == 1) {
        return l;
    } else {
        int m = (l + r) / 2;
        push(v, l, r);
        if (t[v * 2 + 1] >= x) {
            return get(v * 2 + 1, l, m, x);
        } else {
            x -= t[v * 2 + 1];
            return get(v * 2 + 2, m, r, x);
        }
    }
}

int last;

int next(int p) {
    int d = got(0, 0, p, 0, last);
    if (got(0, 0, last, 0, last) == d) {
        return -1;
    } else {
        return get(0, 0, last, d + 1);
    }
}

int lst(int v, int l, int r)
{
    if (r - l == 1)
    {
        return l;
    }
    else
    {
        int m = (l + r) / 2;
        push(v, l, r);
        if (t[v * 2 + 1] == m - l)
        {
            return lst(v * 2 + 2, m, r);
        }
        else
        {
            return lst(v * 2 + 1, l, m);
        }
    }
}

int main() {
    ios_base::sync_with_stdio(0);
#ifdef ONPC
    freopen("a.in", "r", stdin);
#endif
    memset(d, -1, sizeof d);
    int h, w, m, a, b;
    cin >> h >> w >> m;
    vector <pair <int, int> > crd;
    vector <int> x, y;
    x.push_back(1);
    y.push_back(1);
    x.push_back(h);
    y.push_back(w);
    for (int i = 0; i < m; i++) {
        cin >> a >> b;
        x.push_back(a);
        y.push_back(b);
        crd.push_back({a, b});
    }
    sort(x.begin(), x.end());
    sort(y.begin(), y.end());
    vector <int> layer;
    for (int i = 0; i < (int) x.size() - 1; i++) {
        layer.push_back(x[i]);
        if (x[i + 1] - x[i] > 1) {
            layer.push_back(x[i] + 1);
        }
    }
    layer.push_back(x.back());
    sort(layer.begin(), layer.end());
    layer.resize(unique(layer.begin(), layer.end()) - layer.begin());
    for (int i = 0; i < m; i++) {
        crd[i].fc = lower_bound(layer.begin(), layer.end(), crd[i].fc) - layer.begin();
    }   
    int end = lower_bound(layer.begin(), layer.end(), h) - layer.begin();     
    int start = lower_bound(layer.begin(), layer.end(), 1) - layer.begin();     
    auto mem = layer;
    layer.clear();
    for (int i = 0; i < (int) y.size() - 1; i++) {
        layer.push_back(y[i]);
        if (y[i + 1] - y[i] > 1) {
            layer.push_back(y[i] + 1);
        }
    }
    layer.push_back(y.back());
    sort(layer.begin(), layer.end());
    layer.resize(unique(layer.begin(), layer.end()) - layer.begin());
    for (int i = 0; i < m; i++) {
        crd[i].sc = lower_bound(layer.begin(), layer.end(), crd[i].sc) - layer.begin();
    }
    for (int i = 0; i < m; i++) {
        bomb[crd[i].fc].push_back(crd[i].sc);
    }
    last = layer.size();
    put(0, 0, 1, 0, last, 1);
    int ans = 0;
    for (int i = start; i <= end; i++) {
        sort(bomb[i].begin(), bomb[i].end());
        for (int j = 0; j < (int) bomb[i].size(); j++) {
            put(0, bomb[i][j], bomb[i][j] + 1, 0, last, 0);
        }
        bomb[i].push_back(last);
        int l = 0;
        int r = bomb[i][0];
        int p = next(l);
        if (p >= r) {
            
        } else if (p != -1) {
            put(0, p, r, 0, last, 1);
        }
        for (int j = 1; j < (int) bomb[i].size(); j++) {
            int l = bomb[i][j - 1];
            int r = bomb[i][j];
            int p = next(l);
            if (p >= r) {
                continue;
            } else if (p != -1) {
                put(0, p, r, 0, last, 1);
            }
        }
        if (t[0])
        {
            int go = 0;
            if (t[0] == last) go = last;
            else go = lst(0, 0, last);
            if (go != 0)
            {
                ans = max(ans, mem[i] - 1);
            }
        }
    }
    cout << ans << '\n';
}


Compiler output:
a.cc: In function 'int main()':
a.cc:129:5: error: 'memset' was not declared in this scope
  129 |     memset(d, -1, sizeof d);
      |     ^~~~~~
a.cc:21:1: note: 'memset' is defined in header '<cstring>'; this is probably fixable by adding '#include <cstring>'
   20 | #include <chrono>
  +++ |+#include <cstring>
   21 |