#include "circus.h"
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
vector<pii> mnstkl,mnstkr;
typedef unsigned int uint;
const int INF = 1e9;
struct ST {
int sz;
vector<pii> mn, mn_offset;
vector<int> lz;
void pull(int t) {
mn[t] = min(mn[t*2+1], mn[t*2+2]);
mn_offset[t] = min(mn_offset[t*2+1], mn_offset[t*2+2]);
}
void push(int t) {
lz[t*2+1] = min(lz[t*2+1], lz[t]);
lz[t*2+2] = min(lz[t*2+2], lz[t]);
mn[t*2+1] = min(mn[t*2+1], {mn_offset[t*2+1].first + lz[t], mn_offset[t*2+1].second});
mn[t*2+2] = min(mn[t*2+2], {mn_offset[t*2+2].first + lz[t], mn_offset[t*2+2].second});
lz[t] = INF;
}
int _qry(int t, int l, int r, int x) {
if (r - l == 1) return mn[t].first;
else {
int m = l+r>>1;
push(t);
if (x < m) return _qry(t*2+1, l, m, x);
else return _qry(t*2+2, m, r, x);
}
}
int qry(int x) { return _qry(0,0,sz,x); }
void _upd(int t, int l, int r, int x, int v) {
if (x <= l) {
mn[t] = min(mn[t], {mn_offset[t].first + v, mn_offset[t].second});
lz[t] = min(lz[t], v);
} else {
push(t);
int m = l+r>>1;
if (x < m) _upd(t*2+1, l, m, x, v);
_upd(t*2+2, m, r, x, v);
pull(t);
}
}
void upd(int x, int v) { if (x < sz) _upd(0, 0, sz, x, v); }
void _set_offset(int t, int l, int r, int x, int v) {
if (r - l == 1) {
mn_offset[t] = {v, l};
mn[t] = {v + INF, l};
} else {
int m = l+r>>1;
push(t);
if (x < m) _set_offset(t*2+1, l, m, x, v);
else _set_offset(t*2+2, m, r, x, v);
pull(t);
}
}
void set_offset(int x, int v) { _set_offset(0, 0, sz, x, v); }
void init(int N) {
sz = N;
mn.resize(4*N, {INF, INF});
mn_offset.resize(4*N, {INF, INF});
lz.resize(4*N, INF);
}
} A, B;
void init(int N, int M, int _P[]){
vector<int> P(_P, _P+N), bst(N+1);
P.push_back(M);
sort(P.begin(), P.end());
A.init(N+1);
B.init(N+1);
for (int i = 0; i < N+1; ++i) {
A.set_offset(i, P[i]);
B.set_offset(N-i, M-P[i]);
}
A.upd(N, -M);
for (int i = 0; i < N+1; ++i) {
pii mn = A.mn[0];
if (B.mn[0] < mn) {
mn = {B.mn[0].first, N - B.mn[0].second};
}
int x = mn.first + P[mn.second];
A.upd(lower_bound(P.begin(), P.end(), x) - P.begin(), -P[mn.second]);
lower_bound(P.rbegin(), P.rend(), x, greater<int>()) - P.rbegin();
x = P[mn.second] - mn.first;
B.upd(lower_bound(P.rbegin(), P.rend(), x, greater<int>()) - P.rbegin(), P[mn.second]-M);
bst[mn.second] = min(A.qry(mn.second), B.qry(N-mn.second));
A.set_offset(mn.second, INF);
B.set_offset(N-mn.second, INF);
}
priority_queue<pii> pqr;
for (int i = 0; i < N+1; ++i) pqr.emplace(P[i] - bst[i], P[i]);
mnstkr.push_back(pqr.top());
pqr.pop();
while (pqr.size()) {
pii u = pqr.top(); pqr.pop();
if (u.second < mnstkr.back().second) mnstkr.push_back(u);
}
priority_queue<pii, vector<pii>, greater<pii>> pql;
for (int i = 0; i < N+1; ++i) pql.emplace(P[i] + bst[i], P[i]);
mnstkl.push_back(pql.top());
pql.pop();
while (pql.size()) {
pii u = pql.top(); pql.pop();
if (u.second > mnstkl.back().second) mnstkl.push_back(u);
}
}
int minLength(int D) {
int ans = INF;
auto l = lower_bound(mnstkl.begin(), mnstkl.end(), make_pair(D,INF));
if (l != mnstkl.begin()) {
l--;
ans = min(ans, D - l->second);
}
auto r = lower_bound(mnstkr.rbegin(), mnstkr.rend(), make_pair(D,0));
ans = min(ans, r->second - D);
return ans;
}
Compilation message
circus.cpp: In member function 'int ST::_qry(int, int, int, int)':
circus.cpp:27:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
27 | int m = l+r>>1;
| ~^~
circus.cpp: In member function 'void ST::_upd(int, int, int, int, int)':
circus.cpp:40:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
40 | int m = l+r>>1;
| ~^~
circus.cpp: In member function 'void ST::_set_offset(int, int, int, int, int)':
circus.cpp:52:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
52 | int m = l+r>>1;
| ~^~
grader.cpp: In function 'int main()':
grader.cpp:14:12: warning: unused variable 'max_code' [-Wunused-variable]
14 | long long max_code;
| ^~~~~~~~
grader.cpp:16:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
16 | scanf("%d%d", &N, &M);
| ~~~~~^~~~~~~~~~~~~~~~
grader.cpp:18:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
18 | scanf("%d", &P[i]);
| ~~~~~^~~~~~~~~~~~~
grader.cpp:21:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
21 | scanf("%d", &Q);
| ~~~~~^~~~~~~~~~
grader.cpp:23:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
23 | scanf("%d", &d);
| ~~~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
411 ms |
21736 KB |
Output is correct |
2 |
Correct |
432 ms |
21644 KB |
Output is correct |
3 |
Correct |
400 ms |
21864 KB |
Output is correct |
4 |
Correct |
402 ms |
21636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
8 ms |
748 KB |
Output is correct |
6 |
Correct |
6 ms |
748 KB |
Output is correct |
7 |
Correct |
9 ms |
748 KB |
Output is correct |
8 |
Correct |
6 ms |
748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
8 ms |
748 KB |
Output is correct |
6 |
Correct |
6 ms |
748 KB |
Output is correct |
7 |
Correct |
9 ms |
748 KB |
Output is correct |
8 |
Correct |
6 ms |
748 KB |
Output is correct |
9 |
Correct |
415 ms |
18028 KB |
Output is correct |
10 |
Correct |
387 ms |
11116 KB |
Output is correct |
11 |
Correct |
377 ms |
10476 KB |
Output is correct |
12 |
Correct |
419 ms |
19052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
411 ms |
21736 KB |
Output is correct |
2 |
Correct |
432 ms |
21644 KB |
Output is correct |
3 |
Correct |
400 ms |
21864 KB |
Output is correct |
4 |
Correct |
402 ms |
21636 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
8 ms |
748 KB |
Output is correct |
10 |
Correct |
6 ms |
748 KB |
Output is correct |
11 |
Correct |
9 ms |
748 KB |
Output is correct |
12 |
Correct |
6 ms |
748 KB |
Output is correct |
13 |
Correct |
399 ms |
21864 KB |
Output is correct |
14 |
Correct |
417 ms |
21864 KB |
Output is correct |
15 |
Correct |
405 ms |
21608 KB |
Output is correct |
16 |
Correct |
397 ms |
21608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
411 ms |
21736 KB |
Output is correct |
2 |
Correct |
432 ms |
21644 KB |
Output is correct |
3 |
Correct |
400 ms |
21864 KB |
Output is correct |
4 |
Correct |
402 ms |
21636 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
8 ms |
748 KB |
Output is correct |
10 |
Correct |
6 ms |
748 KB |
Output is correct |
11 |
Correct |
9 ms |
748 KB |
Output is correct |
12 |
Correct |
6 ms |
748 KB |
Output is correct |
13 |
Correct |
415 ms |
18028 KB |
Output is correct |
14 |
Correct |
387 ms |
11116 KB |
Output is correct |
15 |
Correct |
377 ms |
10476 KB |
Output is correct |
16 |
Correct |
419 ms |
19052 KB |
Output is correct |
17 |
Correct |
399 ms |
21864 KB |
Output is correct |
18 |
Correct |
417 ms |
21864 KB |
Output is correct |
19 |
Correct |
405 ms |
21608 KB |
Output is correct |
20 |
Correct |
397 ms |
21608 KB |
Output is correct |
21 |
Correct |
996 ms |
30748 KB |
Output is correct |
22 |
Correct |
1000 ms |
31036 KB |
Output is correct |
23 |
Correct |
1016 ms |
34200 KB |
Output is correct |
24 |
Correct |
1026 ms |
36540 KB |
Output is correct |