#include "bits/stdc++.h"
using namespace std;
#define ar array
typedef long long ll;
//~ #define int ll
signed main(){
ios::sync_with_stdio(0); cin.tie(0);
int n, m; cin >> n >> m;
int sz; cin >> sz;
vector<int> x(sz), y(sz), t;
for(int i=0;i<sz;i++){
cin >> x[i] >> y[i];
}
t = x;
sort(t.begin(), t.end());
t.erase(unique(t.begin(), t.end()), t.end());
vector<int> cnt(t.size());
vector<int> id(sz);
for(int i=0;i<sz;i++){
id[i] = lower_bound(t.begin(), t.end(), x[i]) - t.begin() + 1;
}
vector<ar<int, 2>> tot;
vector<int> v, is;
vector<ar<int, 3>> Mn;
set<int> mn, used;
int res = 2 * (n + m);
auto check = [&](int d){
tot.clear(); v.clear(), is.clear(), Mn.clear();
for(int i=0;i<sz;i++){
//~ cout<<max(1LL, y[i] - w)<<" "<<min(y[i] + e, m)<<"\n";
tot.push_back({y[i], id[i]});
tot.push_back({y[i] + d + 1, -id[i]});
}
sort(tot.begin(), tot.end());
auto add = [&](int x){
if(used.empty()){
used.insert(x);
return;
}
auto it = used.upper_bound(x);
if(it == used.begin()){
mn.insert(*it - x - 1);
used.insert(x);
return;
}
if(it == used.end()){
it--;
mn.insert(x - *it - 1);
used.insert(x);
return;
}
auto l = it; l--;
mn.erase(*it - *l - 1);
mn.insert(*it - x - 1);
mn.insert(x - *l - 1);
used.insert(x);
};
auto del = [&](int x){
used.erase(x);
if(used.empty()) return;
auto it = used.upper_bound(x);
if(it == used.begin()){
mn.erase(*it - x - 1);
return;
}
if(it == used.end()){
it--;
mn.erase(x - *it - 1);
return;
}
auto l = it; l--;
mn.erase(*it - x - 1);
mn.erase(x - *l - 1);
mn.insert(*it - *l - 1);
};
for(int i=0;i<(int)tot.size();){
int j = i;
while(j < (int)tot.size() && tot[j][0] == tot[i][0]){
if(tot[j][1] > 0){
if(!cnt[tot[j][1] - 1]) add(t[tot[j][1] - 1]);
cnt[tot[j][1] - 1]++;
} else{
cnt[-tot[j][1] - 1]--;
if(!cnt[-tot[j][1] - 1]) del(t[-tot[j][1] - 1]);
}
j++;
}
v.push_back(tot[i][0]);
if(used.empty()){
Mn.push_back({0, 0, 0});
is.push_back(1);
} else {
//~ assert(!mn.empty());
Mn.push_back({*used.begin() - 1, n - *--used.end(), (mn.empty() ? 0 : *--mn.end())});
is.push_back(0);
}
i = j;
}
{
multiset<int> mnl, mnr, mn;
int r = 0, cnt = 0;
for(int i=0;i + 1 < (int)v.size();i++){
while(r < (int)v.size() && v[r] - v[i] < m){
mn.insert(Mn[r][2]);
mnl.insert(Mn[r][0]);
mnr.insert(Mn[r][1]);
cnt += is[r];
r++;
}
if(cnt == 0){
res = min(res, d + max(*--mn.end(), *--mnl.end() + *--mnr.end()));
}
mn.erase(mn.find(Mn[i][2]));
mnl.erase(mnl.find(Mn[i][0]));
mnr.erase(mnr.find(Mn[i][1]));
cnt -= is[i];
}
}
};
for(int i=0;i<sz;i++){
for(int j=0;j<sz;j++){
check(y[i] - 1 + m - y[j]);
}
for(int k=0;k<sz;k++){
if(y[i] >= y[k]) continue;
check(y[k] - y[i] - 1);
}
}
cout<<res<<"\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
2 ms |
256 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
320 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
2 ms |
256 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
320 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
5 ms |
320 KB |
Output is correct |
18 |
Correct |
159 ms |
308 KB |
Output is correct |
19 |
Correct |
19 ms |
212 KB |
Output is correct |
20 |
Correct |
6 ms |
324 KB |
Output is correct |
21 |
Correct |
101 ms |
212 KB |
Output is correct |
22 |
Correct |
1037 ms |
316 KB |
Output is correct |
23 |
Correct |
29 ms |
212 KB |
Output is correct |
24 |
Execution timed out |
2075 ms |
212 KB |
Time limit exceeded |
25 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
2 ms |
256 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
320 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
5 ms |
320 KB |
Output is correct |
18 |
Correct |
159 ms |
308 KB |
Output is correct |
19 |
Correct |
19 ms |
212 KB |
Output is correct |
20 |
Correct |
6 ms |
324 KB |
Output is correct |
21 |
Correct |
101 ms |
212 KB |
Output is correct |
22 |
Correct |
1037 ms |
316 KB |
Output is correct |
23 |
Correct |
29 ms |
212 KB |
Output is correct |
24 |
Execution timed out |
2075 ms |
212 KB |
Time limit exceeded |
25 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
2 ms |
256 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
320 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
5 ms |
320 KB |
Output is correct |
18 |
Correct |
159 ms |
308 KB |
Output is correct |
19 |
Correct |
19 ms |
212 KB |
Output is correct |
20 |
Correct |
6 ms |
324 KB |
Output is correct |
21 |
Correct |
101 ms |
212 KB |
Output is correct |
22 |
Correct |
1037 ms |
316 KB |
Output is correct |
23 |
Correct |
29 ms |
212 KB |
Output is correct |
24 |
Execution timed out |
2075 ms |
212 KB |
Time limit exceeded |
25 |
Halted |
0 ms |
0 KB |
- |