#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 2e9;
ll n, m; int k;
ll X[302], Y[302];
int rx[302], ry[302], sx, sy;
ll ans = LLONG_MAX;
struct Event{
ll x, y, v;
Event(){}
Event(ll x, ll y, ll v): x(x), y(y), v(v){}
bool operator<(const Event &r)const{
if(x!=r.x) return x<r.x;
return v > r.v;
}
};
struct Interval{
ll l, r; /// ���� ����
ll ldist, rdist, mdist; /// �ּ� ����
Interval(){}
Interval(ll l, ll r, ll ldist, ll rdist, ll mdist): l(l), r(r), ldist(ldist), rdist(rdist), mdist(mdist){}
bool operator<(const Interval &nxt)const{
return l<nxt.l;
}
};
vector<ll> vx, vy;
struct diffTree{
int cnt[1024];
ll lv[1024], rv[1024], diff[1024];
inline void merge(int i){
cnt[i] = cnt[i*2] + cnt[i*2+1];
lv[i] = min(lv[i*2], lv[i*2+1]);
rv[i] = max(rv[i*2], rv[i*2+1]);
diff[i] = max({diff[i*2], diff[i*2+1], (cnt[i*2] && cnt[i*2+1]) ? (lv[i*2+1] - rv[i*2] - 1) : 0});
}
void init(int i, int l, int r){
if(l==r){
cnt[i] = 0;
lv[i] = INF, rv[i] = 0, diff[i] = 0;
return;
}
int m = (l+r)>>1;
init(i*2, l, m);
init(i*2+1, m+1, r);
merge(i);
}
void update(int i, int l, int r, int x, int v){
if(l==r){
cnt[i] += v;
if(!cnt[i]) lv[i] = INF, rv[i] = 0, diff[i] = 0;
else lv[i] = rv[i] = vy[l-1], diff[i] = 0;
return;
}
int m = (l+r)>>1;
if(x<=m) update(i*2, l, m, x, v);
else update(i*2+1, m+1, r, x, v);
merge(i);
}
} difftree;
void renumber(){
vx = vector<ll> (X+1, X+k+1);
vy = vector<ll> (Y+1, Y+k+1);
sort(vx.begin(), vx.end());
vx.erase(unique(vx.begin(), vx.end()), vx.end());
sort(vy.begin(), vy.end());
vy.erase(unique(vy.begin(), vy.end()), vy.end());
sx = (int)vx.size(), sy = (int)vy.size();
for(int i=1; i<=k; i++){
rx[i] = lower_bound(vx.begin(), vx.end(), X[i]) - vx.begin() + 1;
ry[i] = lower_bound(vy.begin(), vy.end(), Y[i]) - vy.begin() + 1;
}
}
int main(){
scanf("%lld %lld %d", &n, &m, &k);
// n=m=1000000000, k=300;
for(int i=1; i<=k; i++){
scanf("%lld %lld", &X[i], &Y[i]);
// X[i] = Y[i] = i*i+i+2;
}
renumber();
vector<ll> udlength (1, n-1); /// ����Ǵ� ������
for(int i=1; i<=k; i++){
udlength.push_back(X[i] - 1);
udlength.push_back(n - X[i]);
for(int j=1; j<=k; j++){
if(X[i] < X[j]) udlength.push_back(X[j] - X[i] - 1);
udlength.push_back(X[i] + n - X[j] - 1);
}
}
sort(udlength.begin(), udlength.end());
udlength.erase(unique(udlength.begin(), udlength.end()), udlength.end());
for(ll ud: udlength){ /// �� - �Ʒ� ����
if(ud<0) continue;
vector<Event> vec;
for(int i=1; i<=k; i++){
vec.push_back(Event(X[i], ry[i], 1));
vec.push_back(Event(X[i]+ud+1, ry[i], -1));
}
sort(vec.begin(), vec.end());
vector<Interval> info;
difftree.init(1, 1, sy);
bool bad = 0;
for(int s=0; s<(int)vec.size(); s++){
int e = s;
while(e+1 < (int)vec.size() && vec[e+1].x == vec[s].x) e++;
if(e+1 == (int)vec.size()) break;
/// [s, e] ���� �������� ó��
for(int i=s; i<=e; i++){
if(vec[i].v == 1) difftree.update(1, 1, sy, vec[i].y, 1);
else difftree.update(1, 1, sy, vec[i].y, -1);
}
/// ��� ������ �Ѿ
if(difftree.cnt[1] == 0){
bad = 1;
break;
}
info.push_back(Interval(vec[s].x, vec[e+1].x-1, difftree.lv[1] - 1, m - difftree.rv[1], difftree.diff[1]));
s = e;
}
if(bad || info.empty()) continue;
deque<pair<ll, int> > dqL, dqR, dqLen;
int j = 0;
dqL.push_back(make_pair(info[0].ldist, 0));
dqR.push_back(make_pair(info[0].rdist, 0));
dqLen.push_back(make_pair(info[0].mdist, 0));
for(int i=0; i<(int)info.size(); i++){
if(!dqL.empty() && dqL.front().second < i) dqL.pop_front();
if(!dqR.empty() && dqR.front().second < i) dqR.pop_front();
if(!dqLen.empty() && dqLen.front().second < i) dqLen.pop_front();
while(j < (int)info.size() && info[j].r - info[i].l + 1 < n){
j++;
while(!dqL.empty() && dqL.back().first <= info[j].ldist) dqL.pop_back();
dqL.push_back(make_pair(info[j].ldist, j));
while(!dqR.empty() && dqR.back().first <= info[j].rdist) dqR.pop_back();
dqR.push_back(make_pair(info[j].rdist, j));
while(!dqLen.empty() && dqLen.back().first <= info[j].mdist) dqLen.pop_back();
dqLen.push_back(make_pair(info[j].mdist, j));
}
if(j == (int)info.size()) break;
ll minL = dqL.front().first, minR = dqR.front().first, len = dqLen.front().first;
ans = min(ans, ud + max(len, minL+minR));
}
}
printf("%lld", ans);
}
Compilation message
cultivation.cpp: In function 'int main()':
cultivation.cpp:86:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
86 | scanf("%lld %lld %d", &n, &m, &k);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
cultivation.cpp:89:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
89 | scanf("%lld %lld", &X[i], &Y[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
3 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
6 ms |
848 KB |
Output is correct |
25 |
Correct |
4 ms |
848 KB |
Output is correct |
26 |
Correct |
10 ms |
2504 KB |
Output is correct |
27 |
Correct |
10 ms |
2504 KB |
Output is correct |
28 |
Correct |
7 ms |
848 KB |
Output is correct |
29 |
Correct |
10 ms |
2504 KB |
Output is correct |
30 |
Correct |
12 ms |
2504 KB |
Output is correct |
31 |
Correct |
10 ms |
2504 KB |
Output is correct |
32 |
Correct |
10 ms |
2504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
3 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
6 ms |
848 KB |
Output is correct |
25 |
Correct |
4 ms |
848 KB |
Output is correct |
26 |
Correct |
10 ms |
2504 KB |
Output is correct |
27 |
Correct |
10 ms |
2504 KB |
Output is correct |
28 |
Correct |
7 ms |
848 KB |
Output is correct |
29 |
Correct |
10 ms |
2504 KB |
Output is correct |
30 |
Correct |
12 ms |
2504 KB |
Output is correct |
31 |
Correct |
10 ms |
2504 KB |
Output is correct |
32 |
Correct |
10 ms |
2504 KB |
Output is correct |
33 |
Correct |
4 ms |
1400 KB |
Output is correct |
34 |
Correct |
14 ms |
2504 KB |
Output is correct |
35 |
Correct |
12 ms |
2504 KB |
Output is correct |
36 |
Correct |
13 ms |
2504 KB |
Output is correct |
37 |
Correct |
12 ms |
2504 KB |
Output is correct |
38 |
Correct |
12 ms |
2504 KB |
Output is correct |
39 |
Correct |
13 ms |
2504 KB |
Output is correct |
40 |
Correct |
12 ms |
2504 KB |
Output is correct |
41 |
Correct |
8 ms |
1484 KB |
Output is correct |
42 |
Correct |
10 ms |
1484 KB |
Output is correct |
43 |
Correct |
11 ms |
2504 KB |
Output is correct |
44 |
Correct |
11 ms |
2504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
212 KB |
Output is correct |
2 |
Correct |
5 ms |
340 KB |
Output is correct |
3 |
Correct |
5 ms |
336 KB |
Output is correct |
4 |
Correct |
5 ms |
212 KB |
Output is correct |
5 |
Correct |
5 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
5 ms |
340 KB |
Output is correct |
9 |
Correct |
3 ms |
340 KB |
Output is correct |
10 |
Correct |
5 ms |
340 KB |
Output is correct |
11 |
Correct |
5 ms |
340 KB |
Output is correct |
12 |
Correct |
5 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
212 KB |
Output is correct |
16 |
Correct |
5 ms |
340 KB |
Output is correct |
17 |
Correct |
5 ms |
212 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
5 ms |
340 KB |
Output is correct |
20 |
Correct |
5 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
212 KB |
Output is correct |
2 |
Correct |
5 ms |
340 KB |
Output is correct |
3 |
Correct |
5 ms |
336 KB |
Output is correct |
4 |
Correct |
5 ms |
212 KB |
Output is correct |
5 |
Correct |
5 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
5 ms |
340 KB |
Output is correct |
9 |
Correct |
3 ms |
340 KB |
Output is correct |
10 |
Correct |
5 ms |
340 KB |
Output is correct |
11 |
Correct |
5 ms |
340 KB |
Output is correct |
12 |
Correct |
5 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
212 KB |
Output is correct |
16 |
Correct |
5 ms |
340 KB |
Output is correct |
17 |
Correct |
5 ms |
212 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
5 ms |
340 KB |
Output is correct |
20 |
Correct |
5 ms |
340 KB |
Output is correct |
21 |
Correct |
196 ms |
468 KB |
Output is correct |
22 |
Correct |
330 ms |
468 KB |
Output is correct |
23 |
Correct |
340 ms |
468 KB |
Output is correct |
24 |
Correct |
259 ms |
468 KB |
Output is correct |
25 |
Correct |
340 ms |
468 KB |
Output is correct |
26 |
Correct |
114 ms |
468 KB |
Output is correct |
27 |
Correct |
325 ms |
468 KB |
Output is correct |
28 |
Correct |
344 ms |
468 KB |
Output is correct |
29 |
Correct |
303 ms |
468 KB |
Output is correct |
30 |
Correct |
323 ms |
472 KB |
Output is correct |
31 |
Correct |
342 ms |
472 KB |
Output is correct |
32 |
Correct |
310 ms |
468 KB |
Output is correct |
33 |
Correct |
322 ms |
468 KB |
Output is correct |
34 |
Correct |
358 ms |
468 KB |
Output is correct |
35 |
Correct |
345 ms |
564 KB |
Output is correct |
36 |
Correct |
305 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
3 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
6 ms |
848 KB |
Output is correct |
25 |
Correct |
4 ms |
848 KB |
Output is correct |
26 |
Correct |
10 ms |
2504 KB |
Output is correct |
27 |
Correct |
10 ms |
2504 KB |
Output is correct |
28 |
Correct |
7 ms |
848 KB |
Output is correct |
29 |
Correct |
10 ms |
2504 KB |
Output is correct |
30 |
Correct |
12 ms |
2504 KB |
Output is correct |
31 |
Correct |
10 ms |
2504 KB |
Output is correct |
32 |
Correct |
10 ms |
2504 KB |
Output is correct |
33 |
Correct |
4 ms |
1400 KB |
Output is correct |
34 |
Correct |
14 ms |
2504 KB |
Output is correct |
35 |
Correct |
12 ms |
2504 KB |
Output is correct |
36 |
Correct |
13 ms |
2504 KB |
Output is correct |
37 |
Correct |
12 ms |
2504 KB |
Output is correct |
38 |
Correct |
12 ms |
2504 KB |
Output is correct |
39 |
Correct |
13 ms |
2504 KB |
Output is correct |
40 |
Correct |
12 ms |
2504 KB |
Output is correct |
41 |
Correct |
8 ms |
1484 KB |
Output is correct |
42 |
Correct |
10 ms |
1484 KB |
Output is correct |
43 |
Correct |
11 ms |
2504 KB |
Output is correct |
44 |
Correct |
11 ms |
2504 KB |
Output is correct |
45 |
Correct |
2 ms |
212 KB |
Output is correct |
46 |
Correct |
5 ms |
340 KB |
Output is correct |
47 |
Correct |
5 ms |
336 KB |
Output is correct |
48 |
Correct |
5 ms |
212 KB |
Output is correct |
49 |
Correct |
5 ms |
340 KB |
Output is correct |
50 |
Correct |
2 ms |
212 KB |
Output is correct |
51 |
Correct |
1 ms |
212 KB |
Output is correct |
52 |
Correct |
5 ms |
340 KB |
Output is correct |
53 |
Correct |
3 ms |
340 KB |
Output is correct |
54 |
Correct |
5 ms |
340 KB |
Output is correct |
55 |
Correct |
5 ms |
340 KB |
Output is correct |
56 |
Correct |
5 ms |
340 KB |
Output is correct |
57 |
Correct |
1 ms |
340 KB |
Output is correct |
58 |
Correct |
2 ms |
340 KB |
Output is correct |
59 |
Correct |
2 ms |
212 KB |
Output is correct |
60 |
Correct |
5 ms |
340 KB |
Output is correct |
61 |
Correct |
5 ms |
212 KB |
Output is correct |
62 |
Correct |
2 ms |
340 KB |
Output is correct |
63 |
Correct |
5 ms |
340 KB |
Output is correct |
64 |
Correct |
5 ms |
340 KB |
Output is correct |
65 |
Correct |
196 ms |
468 KB |
Output is correct |
66 |
Correct |
330 ms |
468 KB |
Output is correct |
67 |
Correct |
340 ms |
468 KB |
Output is correct |
68 |
Correct |
259 ms |
468 KB |
Output is correct |
69 |
Correct |
340 ms |
468 KB |
Output is correct |
70 |
Correct |
114 ms |
468 KB |
Output is correct |
71 |
Correct |
325 ms |
468 KB |
Output is correct |
72 |
Correct |
344 ms |
468 KB |
Output is correct |
73 |
Correct |
303 ms |
468 KB |
Output is correct |
74 |
Correct |
323 ms |
472 KB |
Output is correct |
75 |
Correct |
342 ms |
472 KB |
Output is correct |
76 |
Correct |
310 ms |
468 KB |
Output is correct |
77 |
Correct |
322 ms |
468 KB |
Output is correct |
78 |
Correct |
358 ms |
468 KB |
Output is correct |
79 |
Correct |
345 ms |
564 KB |
Output is correct |
80 |
Correct |
305 ms |
468 KB |
Output is correct |
81 |
Execution timed out |
2057 ms |
1484 KB |
Time limit exceeded |
82 |
Halted |
0 ms |
0 KB |
- |