#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
const int MOD = 1000000007;
const ll INF = 1e18;
const int MX = 100001;
int R,C,N;
ll ans = INF;
vpi p;
vi totest;
void init() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> R >> C >> N; p.resize(N);
F0R(i,N) {
cin >> p[i].f >> p[i].s;
p[i].f --, p[i].s --;
}
F0R(i,N) F0R(j,N) totest.pb(p[i].f+R-1-p[j].f);
F0R(i,N) F0R(j,N) totest.pb(max(p[i].f-p[j].f-1,0));
sort(all(totest)); totest.erase(unique(all(totest)),totest.end());
}
struct MaxDeque {
deque<pi> d;
void ad(int ind, int val) {
while (sz(d) && d.back().f <= val) d.pop_back();
d.pb({val,ind});
}
void rem(int ind) {
if (d.front().s == ind) d.pop_front();
}
int get() { return d.front().f; }
};
multiset<int> cur, cdif;
array<int,3> get() {
if (!sz(cur)) return {MOD,MOD,MOD};
int dif = sz(cdif)?*cdif.rbegin():0;
dif = max(dif,max(*cur.begin(),C-1-*cur.rbegin()));
return {*cur.begin(),C-1-*cur.rbegin(),dif};
}
void process(array<int,3> x) {
if (x[1] == 1) {
auto it = cur.insert(x[2]);
if (it != cur.begin() && next(it) != cur.end()) cdif.erase(cdif.find(*next(it)-*prev(it)-1));
if (it != cur.begin()) cdif.insert(*it-*prev(it)-1);
if (next(it) != cur.end()) cdif.insert(*next(it)-*it-1);
} else {
auto it = cur.find(x[2]);
if (it != cur.begin()) cdif.erase(cdif.find(*it-*prev(it)-1));
if (next(it) != cur.end()) cdif.erase(cdif.find(*next(it)-*it-1));
if (it != cur.begin() && next(it) != cur.end()) cdif.insert(*next(it)-*prev(it)-1);
cur.erase(it);
}
}
void tri(int r) {
vector<array<int,3>> change;
F0R(i,N) {
change.pb({p[i].f,1,p[i].s});
change.pb({p[i].f+r+1,-1,p[i].s});
}
sort(all(change));
vector<pair<int,array<int,3>>> dat;
for (int i = 0; i < sz(change); ) {
int I = i;
while (i < sz(change) && change[i][0] == change[I][0]) process(change[i++]);
dat.pb({change[I][0],get()});
}
MaxDeque D[3];
int nex = 0;
F0R(i,sz(dat)) if (dat[i].f <= r) {
while (nex < sz(dat) && dat[nex].f < dat[i].f+R) {
F0R(j,3) D[j].ad(nex,dat[nex].s[j]);
nex ++;
}
if (D[0].get() < MOD) ans = min(ans,(ll)r+max(D[0].get()+D[1].get(),D[2].get()));
F0R(j,3) D[j].rem(i);
}
// cout << r << " " << ans << "\n";
// 0 <= x <= r
// consider x to x+R-1
}
int main() {
init();
for (int i: totest) tri(i);
cout << ans;
}
/* Look for:
* the exact constraints (multiple sets are too slow for n=10^6 :( )
* special cases (n=1?)
* overflow (ll vs int?)
* array bounds
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
248 KB |
Output is correct |
2 |
Correct |
3 ms |
360 KB |
Output is correct |
3 |
Correct |
2 ms |
472 KB |
Output is correct |
4 |
Correct |
4 ms |
640 KB |
Output is correct |
5 |
Correct |
3 ms |
640 KB |
Output is correct |
6 |
Correct |
3 ms |
640 KB |
Output is correct |
7 |
Correct |
3 ms |
640 KB |
Output is correct |
8 |
Correct |
3 ms |
644 KB |
Output is correct |
9 |
Correct |
4 ms |
644 KB |
Output is correct |
10 |
Correct |
4 ms |
652 KB |
Output is correct |
11 |
Correct |
3 ms |
652 KB |
Output is correct |
12 |
Correct |
3 ms |
700 KB |
Output is correct |
13 |
Correct |
4 ms |
704 KB |
Output is correct |
14 |
Correct |
2 ms |
708 KB |
Output is correct |
15 |
Correct |
3 ms |
844 KB |
Output is correct |
16 |
Correct |
4 ms |
844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
248 KB |
Output is correct |
2 |
Correct |
3 ms |
360 KB |
Output is correct |
3 |
Correct |
2 ms |
472 KB |
Output is correct |
4 |
Correct |
4 ms |
640 KB |
Output is correct |
5 |
Correct |
3 ms |
640 KB |
Output is correct |
6 |
Correct |
3 ms |
640 KB |
Output is correct |
7 |
Correct |
3 ms |
640 KB |
Output is correct |
8 |
Correct |
3 ms |
644 KB |
Output is correct |
9 |
Correct |
4 ms |
644 KB |
Output is correct |
10 |
Correct |
4 ms |
652 KB |
Output is correct |
11 |
Correct |
3 ms |
652 KB |
Output is correct |
12 |
Correct |
3 ms |
700 KB |
Output is correct |
13 |
Correct |
4 ms |
704 KB |
Output is correct |
14 |
Correct |
2 ms |
708 KB |
Output is correct |
15 |
Correct |
3 ms |
844 KB |
Output is correct |
16 |
Correct |
4 ms |
844 KB |
Output is correct |
17 |
Correct |
2 ms |
844 KB |
Output is correct |
18 |
Correct |
6 ms |
844 KB |
Output is correct |
19 |
Correct |
4 ms |
844 KB |
Output is correct |
20 |
Correct |
3 ms |
844 KB |
Output is correct |
21 |
Correct |
8 ms |
844 KB |
Output is correct |
22 |
Correct |
16 ms |
932 KB |
Output is correct |
23 |
Correct |
4 ms |
932 KB |
Output is correct |
24 |
Correct |
26 ms |
1448 KB |
Output is correct |
25 |
Correct |
19 ms |
1448 KB |
Output is correct |
26 |
Correct |
36 ms |
1964 KB |
Output is correct |
27 |
Correct |
39 ms |
2000 KB |
Output is correct |
28 |
Correct |
27 ms |
2000 KB |
Output is correct |
29 |
Correct |
35 ms |
2000 KB |
Output is correct |
30 |
Correct |
38 ms |
2000 KB |
Output is correct |
31 |
Correct |
44 ms |
2004 KB |
Output is correct |
32 |
Correct |
37 ms |
2004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
248 KB |
Output is correct |
2 |
Correct |
3 ms |
360 KB |
Output is correct |
3 |
Correct |
2 ms |
472 KB |
Output is correct |
4 |
Correct |
4 ms |
640 KB |
Output is correct |
5 |
Correct |
3 ms |
640 KB |
Output is correct |
6 |
Correct |
3 ms |
640 KB |
Output is correct |
7 |
Correct |
3 ms |
640 KB |
Output is correct |
8 |
Correct |
3 ms |
644 KB |
Output is correct |
9 |
Correct |
4 ms |
644 KB |
Output is correct |
10 |
Correct |
4 ms |
652 KB |
Output is correct |
11 |
Correct |
3 ms |
652 KB |
Output is correct |
12 |
Correct |
3 ms |
700 KB |
Output is correct |
13 |
Correct |
4 ms |
704 KB |
Output is correct |
14 |
Correct |
2 ms |
708 KB |
Output is correct |
15 |
Correct |
3 ms |
844 KB |
Output is correct |
16 |
Correct |
4 ms |
844 KB |
Output is correct |
17 |
Correct |
2 ms |
844 KB |
Output is correct |
18 |
Correct |
6 ms |
844 KB |
Output is correct |
19 |
Correct |
4 ms |
844 KB |
Output is correct |
20 |
Correct |
3 ms |
844 KB |
Output is correct |
21 |
Correct |
8 ms |
844 KB |
Output is correct |
22 |
Correct |
16 ms |
932 KB |
Output is correct |
23 |
Correct |
4 ms |
932 KB |
Output is correct |
24 |
Correct |
26 ms |
1448 KB |
Output is correct |
25 |
Correct |
19 ms |
1448 KB |
Output is correct |
26 |
Correct |
36 ms |
1964 KB |
Output is correct |
27 |
Correct |
39 ms |
2000 KB |
Output is correct |
28 |
Correct |
27 ms |
2000 KB |
Output is correct |
29 |
Correct |
35 ms |
2000 KB |
Output is correct |
30 |
Correct |
38 ms |
2000 KB |
Output is correct |
31 |
Correct |
44 ms |
2004 KB |
Output is correct |
32 |
Correct |
37 ms |
2004 KB |
Output is correct |
33 |
Correct |
9 ms |
2004 KB |
Output is correct |
34 |
Correct |
30 ms |
2008 KB |
Output is correct |
35 |
Correct |
42 ms |
2008 KB |
Output is correct |
36 |
Correct |
51 ms |
2020 KB |
Output is correct |
37 |
Correct |
46 ms |
2020 KB |
Output is correct |
38 |
Correct |
45 ms |
2020 KB |
Output is correct |
39 |
Correct |
42 ms |
2020 KB |
Output is correct |
40 |
Correct |
47 ms |
2024 KB |
Output is correct |
41 |
Correct |
33 ms |
2028 KB |
Output is correct |
42 |
Correct |
40 ms |
2032 KB |
Output is correct |
43 |
Correct |
42 ms |
2036 KB |
Output is correct |
44 |
Correct |
46 ms |
2036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2072 ms |
2036 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2072 ms |
2036 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
248 KB |
Output is correct |
2 |
Correct |
3 ms |
360 KB |
Output is correct |
3 |
Correct |
2 ms |
472 KB |
Output is correct |
4 |
Correct |
4 ms |
640 KB |
Output is correct |
5 |
Correct |
3 ms |
640 KB |
Output is correct |
6 |
Correct |
3 ms |
640 KB |
Output is correct |
7 |
Correct |
3 ms |
640 KB |
Output is correct |
8 |
Correct |
3 ms |
644 KB |
Output is correct |
9 |
Correct |
4 ms |
644 KB |
Output is correct |
10 |
Correct |
4 ms |
652 KB |
Output is correct |
11 |
Correct |
3 ms |
652 KB |
Output is correct |
12 |
Correct |
3 ms |
700 KB |
Output is correct |
13 |
Correct |
4 ms |
704 KB |
Output is correct |
14 |
Correct |
2 ms |
708 KB |
Output is correct |
15 |
Correct |
3 ms |
844 KB |
Output is correct |
16 |
Correct |
4 ms |
844 KB |
Output is correct |
17 |
Correct |
2 ms |
844 KB |
Output is correct |
18 |
Correct |
6 ms |
844 KB |
Output is correct |
19 |
Correct |
4 ms |
844 KB |
Output is correct |
20 |
Correct |
3 ms |
844 KB |
Output is correct |
21 |
Correct |
8 ms |
844 KB |
Output is correct |
22 |
Correct |
16 ms |
932 KB |
Output is correct |
23 |
Correct |
4 ms |
932 KB |
Output is correct |
24 |
Correct |
26 ms |
1448 KB |
Output is correct |
25 |
Correct |
19 ms |
1448 KB |
Output is correct |
26 |
Correct |
36 ms |
1964 KB |
Output is correct |
27 |
Correct |
39 ms |
2000 KB |
Output is correct |
28 |
Correct |
27 ms |
2000 KB |
Output is correct |
29 |
Correct |
35 ms |
2000 KB |
Output is correct |
30 |
Correct |
38 ms |
2000 KB |
Output is correct |
31 |
Correct |
44 ms |
2004 KB |
Output is correct |
32 |
Correct |
37 ms |
2004 KB |
Output is correct |
33 |
Correct |
9 ms |
2004 KB |
Output is correct |
34 |
Correct |
30 ms |
2008 KB |
Output is correct |
35 |
Correct |
42 ms |
2008 KB |
Output is correct |
36 |
Correct |
51 ms |
2020 KB |
Output is correct |
37 |
Correct |
46 ms |
2020 KB |
Output is correct |
38 |
Correct |
45 ms |
2020 KB |
Output is correct |
39 |
Correct |
42 ms |
2020 KB |
Output is correct |
40 |
Correct |
47 ms |
2024 KB |
Output is correct |
41 |
Correct |
33 ms |
2028 KB |
Output is correct |
42 |
Correct |
40 ms |
2032 KB |
Output is correct |
43 |
Correct |
42 ms |
2036 KB |
Output is correct |
44 |
Correct |
46 ms |
2036 KB |
Output is correct |
45 |
Execution timed out |
2072 ms |
2036 KB |
Time limit exceeded |
46 |
Halted |
0 ms |
0 KB |
- |