#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;
vpl p;
vl 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,0LL));
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<ll,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);
}
}
int co = 0;
void tri(int r) {
vector<array<ll,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));
co += sz(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 |
4 ms |
248 KB |
Output is correct |
2 |
Correct |
3 ms |
484 KB |
Output is correct |
3 |
Correct |
3 ms |
484 KB |
Output is correct |
4 |
Correct |
3 ms |
484 KB |
Output is correct |
5 |
Correct |
4 ms |
484 KB |
Output is correct |
6 |
Correct |
3 ms |
612 KB |
Output is correct |
7 |
Correct |
4 ms |
612 KB |
Output is correct |
8 |
Correct |
3 ms |
624 KB |
Output is correct |
9 |
Correct |
3 ms |
624 KB |
Output is correct |
10 |
Correct |
3 ms |
624 KB |
Output is correct |
11 |
Correct |
3 ms |
664 KB |
Output is correct |
12 |
Correct |
3 ms |
664 KB |
Output is correct |
13 |
Correct |
3 ms |
664 KB |
Output is correct |
14 |
Correct |
3 ms |
664 KB |
Output is correct |
15 |
Correct |
3 ms |
664 KB |
Output is correct |
16 |
Correct |
3 ms |
664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
248 KB |
Output is correct |
2 |
Correct |
3 ms |
484 KB |
Output is correct |
3 |
Correct |
3 ms |
484 KB |
Output is correct |
4 |
Correct |
3 ms |
484 KB |
Output is correct |
5 |
Correct |
4 ms |
484 KB |
Output is correct |
6 |
Correct |
3 ms |
612 KB |
Output is correct |
7 |
Correct |
4 ms |
612 KB |
Output is correct |
8 |
Correct |
3 ms |
624 KB |
Output is correct |
9 |
Correct |
3 ms |
624 KB |
Output is correct |
10 |
Correct |
3 ms |
624 KB |
Output is correct |
11 |
Correct |
3 ms |
664 KB |
Output is correct |
12 |
Correct |
3 ms |
664 KB |
Output is correct |
13 |
Correct |
3 ms |
664 KB |
Output is correct |
14 |
Correct |
3 ms |
664 KB |
Output is correct |
15 |
Correct |
3 ms |
664 KB |
Output is correct |
16 |
Correct |
3 ms |
664 KB |
Output is correct |
17 |
Correct |
4 ms |
664 KB |
Output is correct |
18 |
Correct |
7 ms |
748 KB |
Output is correct |
19 |
Correct |
5 ms |
748 KB |
Output is correct |
20 |
Correct |
3 ms |
748 KB |
Output is correct |
21 |
Correct |
8 ms |
748 KB |
Output is correct |
22 |
Correct |
17 ms |
1004 KB |
Output is correct |
23 |
Correct |
4 ms |
1004 KB |
Output is correct |
24 |
Correct |
32 ms |
1768 KB |
Output is correct |
25 |
Correct |
22 ms |
1768 KB |
Output is correct |
26 |
Correct |
42 ms |
2788 KB |
Output is correct |
27 |
Correct |
48 ms |
2788 KB |
Output is correct |
28 |
Correct |
27 ms |
2788 KB |
Output is correct |
29 |
Correct |
44 ms |
2788 KB |
Output is correct |
30 |
Correct |
43 ms |
2788 KB |
Output is correct |
31 |
Correct |
54 ms |
2788 KB |
Output is correct |
32 |
Correct |
48 ms |
2872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
248 KB |
Output is correct |
2 |
Correct |
3 ms |
484 KB |
Output is correct |
3 |
Correct |
3 ms |
484 KB |
Output is correct |
4 |
Correct |
3 ms |
484 KB |
Output is correct |
5 |
Correct |
4 ms |
484 KB |
Output is correct |
6 |
Correct |
3 ms |
612 KB |
Output is correct |
7 |
Correct |
4 ms |
612 KB |
Output is correct |
8 |
Correct |
3 ms |
624 KB |
Output is correct |
9 |
Correct |
3 ms |
624 KB |
Output is correct |
10 |
Correct |
3 ms |
624 KB |
Output is correct |
11 |
Correct |
3 ms |
664 KB |
Output is correct |
12 |
Correct |
3 ms |
664 KB |
Output is correct |
13 |
Correct |
3 ms |
664 KB |
Output is correct |
14 |
Correct |
3 ms |
664 KB |
Output is correct |
15 |
Correct |
3 ms |
664 KB |
Output is correct |
16 |
Correct |
3 ms |
664 KB |
Output is correct |
17 |
Correct |
4 ms |
664 KB |
Output is correct |
18 |
Correct |
7 ms |
748 KB |
Output is correct |
19 |
Correct |
5 ms |
748 KB |
Output is correct |
20 |
Correct |
3 ms |
748 KB |
Output is correct |
21 |
Correct |
8 ms |
748 KB |
Output is correct |
22 |
Correct |
17 ms |
1004 KB |
Output is correct |
23 |
Correct |
4 ms |
1004 KB |
Output is correct |
24 |
Correct |
32 ms |
1768 KB |
Output is correct |
25 |
Correct |
22 ms |
1768 KB |
Output is correct |
26 |
Correct |
42 ms |
2788 KB |
Output is correct |
27 |
Correct |
48 ms |
2788 KB |
Output is correct |
28 |
Correct |
27 ms |
2788 KB |
Output is correct |
29 |
Correct |
44 ms |
2788 KB |
Output is correct |
30 |
Correct |
43 ms |
2788 KB |
Output is correct |
31 |
Correct |
54 ms |
2788 KB |
Output is correct |
32 |
Correct |
48 ms |
2872 KB |
Output is correct |
33 |
Correct |
12 ms |
2872 KB |
Output is correct |
34 |
Correct |
39 ms |
2872 KB |
Output is correct |
35 |
Correct |
57 ms |
2872 KB |
Output is correct |
36 |
Correct |
49 ms |
2872 KB |
Output is correct |
37 |
Correct |
49 ms |
2872 KB |
Output is correct |
38 |
Correct |
49 ms |
2872 KB |
Output is correct |
39 |
Correct |
47 ms |
2872 KB |
Output is correct |
40 |
Correct |
49 ms |
2872 KB |
Output is correct |
41 |
Correct |
37 ms |
2872 KB |
Output is correct |
42 |
Correct |
41 ms |
2872 KB |
Output is correct |
43 |
Correct |
46 ms |
2872 KB |
Output is correct |
44 |
Correct |
54 ms |
2872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
2872 KB |
Output is correct |
2 |
Correct |
24 ms |
2872 KB |
Output is correct |
3 |
Correct |
26 ms |
2872 KB |
Output is correct |
4 |
Correct |
18 ms |
2872 KB |
Output is correct |
5 |
Correct |
21 ms |
2872 KB |
Output is correct |
6 |
Correct |
11 ms |
2872 KB |
Output is correct |
7 |
Correct |
5 ms |
2872 KB |
Output is correct |
8 |
Correct |
23 ms |
2872 KB |
Output is correct |
9 |
Correct |
14 ms |
2872 KB |
Output is correct |
10 |
Correct |
25 ms |
2872 KB |
Output is correct |
11 |
Correct |
26 ms |
2872 KB |
Output is correct |
12 |
Correct |
25 ms |
2872 KB |
Output is correct |
13 |
Correct |
7 ms |
2872 KB |
Output is correct |
14 |
Correct |
14 ms |
2872 KB |
Output is correct |
15 |
Correct |
8 ms |
2872 KB |
Output is correct |
16 |
Correct |
22 ms |
2872 KB |
Output is correct |
17 |
Correct |
22 ms |
2872 KB |
Output is correct |
18 |
Correct |
12 ms |
2872 KB |
Output is correct |
19 |
Correct |
22 ms |
2872 KB |
Output is correct |
20 |
Correct |
15 ms |
2872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
2872 KB |
Output is correct |
2 |
Correct |
24 ms |
2872 KB |
Output is correct |
3 |
Correct |
26 ms |
2872 KB |
Output is correct |
4 |
Correct |
18 ms |
2872 KB |
Output is correct |
5 |
Correct |
21 ms |
2872 KB |
Output is correct |
6 |
Correct |
11 ms |
2872 KB |
Output is correct |
7 |
Correct |
5 ms |
2872 KB |
Output is correct |
8 |
Correct |
23 ms |
2872 KB |
Output is correct |
9 |
Correct |
14 ms |
2872 KB |
Output is correct |
10 |
Correct |
25 ms |
2872 KB |
Output is correct |
11 |
Correct |
26 ms |
2872 KB |
Output is correct |
12 |
Correct |
25 ms |
2872 KB |
Output is correct |
13 |
Correct |
7 ms |
2872 KB |
Output is correct |
14 |
Correct |
14 ms |
2872 KB |
Output is correct |
15 |
Correct |
8 ms |
2872 KB |
Output is correct |
16 |
Correct |
22 ms |
2872 KB |
Output is correct |
17 |
Correct |
22 ms |
2872 KB |
Output is correct |
18 |
Correct |
12 ms |
2872 KB |
Output is correct |
19 |
Correct |
22 ms |
2872 KB |
Output is correct |
20 |
Correct |
15 ms |
2872 KB |
Output is correct |
21 |
Correct |
1114 ms |
2872 KB |
Output is correct |
22 |
Correct |
1838 ms |
2872 KB |
Output is correct |
23 |
Correct |
1833 ms |
2872 KB |
Output is correct |
24 |
Correct |
1831 ms |
2872 KB |
Output is correct |
25 |
Correct |
1888 ms |
2872 KB |
Output is correct |
26 |
Correct |
723 ms |
2872 KB |
Output is correct |
27 |
Correct |
1219 ms |
2872 KB |
Output is correct |
28 |
Correct |
1866 ms |
2872 KB |
Output is correct |
29 |
Correct |
1958 ms |
2872 KB |
Output is correct |
30 |
Correct |
1811 ms |
2872 KB |
Output is correct |
31 |
Correct |
1831 ms |
2872 KB |
Output is correct |
32 |
Correct |
1714 ms |
2872 KB |
Output is correct |
33 |
Correct |
934 ms |
2872 KB |
Output is correct |
34 |
Correct |
1860 ms |
2872 KB |
Output is correct |
35 |
Correct |
1541 ms |
2872 KB |
Output is correct |
36 |
Correct |
1955 ms |
2872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
248 KB |
Output is correct |
2 |
Correct |
3 ms |
484 KB |
Output is correct |
3 |
Correct |
3 ms |
484 KB |
Output is correct |
4 |
Correct |
3 ms |
484 KB |
Output is correct |
5 |
Correct |
4 ms |
484 KB |
Output is correct |
6 |
Correct |
3 ms |
612 KB |
Output is correct |
7 |
Correct |
4 ms |
612 KB |
Output is correct |
8 |
Correct |
3 ms |
624 KB |
Output is correct |
9 |
Correct |
3 ms |
624 KB |
Output is correct |
10 |
Correct |
3 ms |
624 KB |
Output is correct |
11 |
Correct |
3 ms |
664 KB |
Output is correct |
12 |
Correct |
3 ms |
664 KB |
Output is correct |
13 |
Correct |
3 ms |
664 KB |
Output is correct |
14 |
Correct |
3 ms |
664 KB |
Output is correct |
15 |
Correct |
3 ms |
664 KB |
Output is correct |
16 |
Correct |
3 ms |
664 KB |
Output is correct |
17 |
Correct |
4 ms |
664 KB |
Output is correct |
18 |
Correct |
7 ms |
748 KB |
Output is correct |
19 |
Correct |
5 ms |
748 KB |
Output is correct |
20 |
Correct |
3 ms |
748 KB |
Output is correct |
21 |
Correct |
8 ms |
748 KB |
Output is correct |
22 |
Correct |
17 ms |
1004 KB |
Output is correct |
23 |
Correct |
4 ms |
1004 KB |
Output is correct |
24 |
Correct |
32 ms |
1768 KB |
Output is correct |
25 |
Correct |
22 ms |
1768 KB |
Output is correct |
26 |
Correct |
42 ms |
2788 KB |
Output is correct |
27 |
Correct |
48 ms |
2788 KB |
Output is correct |
28 |
Correct |
27 ms |
2788 KB |
Output is correct |
29 |
Correct |
44 ms |
2788 KB |
Output is correct |
30 |
Correct |
43 ms |
2788 KB |
Output is correct |
31 |
Correct |
54 ms |
2788 KB |
Output is correct |
32 |
Correct |
48 ms |
2872 KB |
Output is correct |
33 |
Correct |
12 ms |
2872 KB |
Output is correct |
34 |
Correct |
39 ms |
2872 KB |
Output is correct |
35 |
Correct |
57 ms |
2872 KB |
Output is correct |
36 |
Correct |
49 ms |
2872 KB |
Output is correct |
37 |
Correct |
49 ms |
2872 KB |
Output is correct |
38 |
Correct |
49 ms |
2872 KB |
Output is correct |
39 |
Correct |
47 ms |
2872 KB |
Output is correct |
40 |
Correct |
49 ms |
2872 KB |
Output is correct |
41 |
Correct |
37 ms |
2872 KB |
Output is correct |
42 |
Correct |
41 ms |
2872 KB |
Output is correct |
43 |
Correct |
46 ms |
2872 KB |
Output is correct |
44 |
Correct |
54 ms |
2872 KB |
Output is correct |
45 |
Correct |
10 ms |
2872 KB |
Output is correct |
46 |
Correct |
24 ms |
2872 KB |
Output is correct |
47 |
Correct |
26 ms |
2872 KB |
Output is correct |
48 |
Correct |
18 ms |
2872 KB |
Output is correct |
49 |
Correct |
21 ms |
2872 KB |
Output is correct |
50 |
Correct |
11 ms |
2872 KB |
Output is correct |
51 |
Correct |
5 ms |
2872 KB |
Output is correct |
52 |
Correct |
23 ms |
2872 KB |
Output is correct |
53 |
Correct |
14 ms |
2872 KB |
Output is correct |
54 |
Correct |
25 ms |
2872 KB |
Output is correct |
55 |
Correct |
26 ms |
2872 KB |
Output is correct |
56 |
Correct |
25 ms |
2872 KB |
Output is correct |
57 |
Correct |
7 ms |
2872 KB |
Output is correct |
58 |
Correct |
14 ms |
2872 KB |
Output is correct |
59 |
Correct |
8 ms |
2872 KB |
Output is correct |
60 |
Correct |
22 ms |
2872 KB |
Output is correct |
61 |
Correct |
22 ms |
2872 KB |
Output is correct |
62 |
Correct |
12 ms |
2872 KB |
Output is correct |
63 |
Correct |
22 ms |
2872 KB |
Output is correct |
64 |
Correct |
15 ms |
2872 KB |
Output is correct |
65 |
Correct |
1114 ms |
2872 KB |
Output is correct |
66 |
Correct |
1838 ms |
2872 KB |
Output is correct |
67 |
Correct |
1833 ms |
2872 KB |
Output is correct |
68 |
Correct |
1831 ms |
2872 KB |
Output is correct |
69 |
Correct |
1888 ms |
2872 KB |
Output is correct |
70 |
Correct |
723 ms |
2872 KB |
Output is correct |
71 |
Correct |
1219 ms |
2872 KB |
Output is correct |
72 |
Correct |
1866 ms |
2872 KB |
Output is correct |
73 |
Correct |
1958 ms |
2872 KB |
Output is correct |
74 |
Correct |
1811 ms |
2872 KB |
Output is correct |
75 |
Correct |
1831 ms |
2872 KB |
Output is correct |
76 |
Correct |
1714 ms |
2872 KB |
Output is correct |
77 |
Correct |
934 ms |
2872 KB |
Output is correct |
78 |
Correct |
1860 ms |
2872 KB |
Output is correct |
79 |
Correct |
1541 ms |
2872 KB |
Output is correct |
80 |
Correct |
1955 ms |
2872 KB |
Output is correct |
81 |
Execution timed out |
2041 ms |
2872 KB |
Time limit exceeded |
82 |
Halted |
0 ms |
0 KB |
- |