#include<bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define endl "\n"
using namespace std;
const int N = 3e5 + 10;
int n, k, q, sz;
int x[N * 3], t[N], a[N], b[N], cost[N], c[N], y[N], ans[N];
int pre[N * 3], nxt[N * 3];
vector<int> rrh, open[N * 3], close[N * 3], event[N * 3];
set<pair<int, int> > s[N];
bool alive[N * 3];
bool bug;
priority_queue<pair<int, int> > bit0[N], bit1[N];
void add0 (int pos, int border, pair<int, int> tmp) {
while (pos >= border) {
bit0[pos].push(tmp);
pos -= pos & -pos;
}
}
int get0(int pos, int _pos) {
int ret = 0;
while (pos <= sz) {
while (!bit0[pos].empty()) {
int i = bit0[pos].top().second;
int j = nxt[i];
if (alive[i] == 0 || x[i] == x[j] || abs(x[i] - _pos) > abs(x[j] - _pos)) bit0[pos].pop();
else {
ret = max(ret, _pos - x[i]);
break;
}
}
pos += pos & -pos;
}
return ret;
}
void add1 (int pos, int border, pair<int, int> tmp) {
while (pos <= border) {
bit1[pos].push(tmp);
pos += pos & -pos;
}
}
int get1(int pos, int _pos) {
int ret = 0;
while (pos) {
while (!bit1[pos].empty()) {
int i = bit1[pos].top().second;
int j = pre[i];
if (alive[i] == 0 || x[i] == x[j] || abs(x[i] - _pos) > abs(x[j] - _pos)) bit1[pos].pop();
else {
ret = max(ret, x[i] - _pos);
break;
}
}
pos -= pos & -pos;
}
return ret;
}
void compress () {
sort(all(rrh));
rrh.resize(unique(all(rrh)) - rrh.begin());
}
int id (int x) {
return upper_bound(all(rrh), x) - rrh.begin();
}
int32_t main() {
#define task ""
cin.tie(0) -> sync_with_stdio(0);
if (fopen("task.inp", "r")) {
freopen("task.inp", "r", stdin);
freopen("task.out", "w", stdout);
}
if (fopen(task".inp", "r")) {
freopen (task".inp", "r", stdin);
freopen (task".out", "w", stdout);
}
cin >> n >> k >> q;
for(int i = 1; i <= n; i++) cin >> x[i] >> t[i] >> a[i] >> b[i];
for(int i = 1; i <= q; i++) cin >> c[i] >> y[i];
for(int i = 1; i <= n; i++) {
rrh.push_back(a[i]);
rrh.push_back(b[i]);
}
for(int i = 1; i <= q; i++) rrh.push_back(y[i]);
compress();
for(int i = 1; i <= n; i++) {
a[i] = id(a[i]);
b[i] = id(b[i]);
open[a[i]].push_back(i);
close[b[i]].push_back(i);
}
for(int i = 1; i <= q; i++) {
y[i] = id(y[i]);
event[y[i]].push_back(i);
}
int m = rrh.size();
rrh.clear();
for(int i = 1; i <= q; i++) rrh.push_back(c[i]);
compress();
int cnt = n;
sz = rrh.size();
for(int i = 1; i <= k; i++) {
x[cnt + 1] = -1e9;
x[cnt + 2] = 1e9;
alive[cnt + 1] = alive[cnt + 2] = 1;
s[i].insert({x[cnt + 1], cnt + 1});
s[i].insert({x[cnt + 2], cnt + 2});
add1(1, sz, make_pair(x[cnt + 2], cnt + 2));
nxt[cnt + 1] = cnt + 2;
pre[cnt + 2] = cnt + 1;
cnt += 2;
}
for(int cur = 1; cur <= m; cur++) {
for(int &j : open[cur]) {
auto iter = s[t[j]].lower_bound(make_pair(x[j], j));
int to = iter -> second;
--iter;
int from = iter -> second;
int middle;
middle = id(x[from] + x[j] >> 1);
int L = id(x[from] - 1) + 1, R = id(x[to]), pos = id(x[j]);
add0(middle, L, make_pair(-x[from], from));
add1(middle + 1, pos, make_pair(x[j], j));
middle = id(x[j] + x[to] >> 1);
pos = id(x[j] - 1) + 1;
add0(middle, pos, make_pair(-x[j], j));
add1(middle + 1, R, make_pair(x[to], to));
nxt[from] = j;
pre[j] = from;
nxt[j] = to;
pre[to] = j;
s[t[j]].insert({x[j], j});
alive[j] = 1;
}
for(int &j : event[cur]) {
int tmp = id(c[j]);
int cost1 = get0(tmp, c[j]);
int cost2 = get1(tmp, c[j]);
ans[j] = max(cost1, cost2);
}
for(int &j : close[cur]) {
auto iter = s[t[j]].find(make_pair(x[j], j));
++iter;
int to = iter -> second;
--iter; --iter;
int from = iter -> second;
int L = id(x[from] - 1) + 1, R = id(x[to]), middle = id(x[from] + x[to] >> 1);
add0(middle, L, make_pair(-x[from], from));
add1(middle + 1, R, make_pair(x[to], to));
nxt[from] = to;
pre[to] = from;
s[t[j]].erase(s[t[j]].find(make_pair(x[j], j)));
alive[j] = 0;
}
}
cerr << 1.0 * clock() / CLOCKS_PER_SEC << endl;
for(int i = 1; i <= q; i++) {
if (ans[i] > 1e8) ans[i] = -1;
cout << ans[i] << endl;
}
}
Compilation message
new_home.cpp: In function 'int32_t main()':
new_home.cpp:150:27: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
150 | middle = id(x[from] + x[j] >> 1);
| ~~~~~~~~^~~~~~
new_home.cpp:156:24: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
156 | middle = id(x[j] + x[to] >> 1);
| ~~~~~^~~~~~~
new_home.cpp:182:71: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
182 | int L = id(x[from] - 1) + 1, R = id(x[to]), middle = id(x[from] + x[to] >> 1);
| ~~~~~~~~^~~~~~~
new_home.cpp:87:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
87 | freopen("task.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
new_home.cpp:88:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
88 | freopen("task.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
new_home.cpp:92:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
92 | freopen (task".inp", "r", stdin);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
new_home.cpp:93:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
93 | freopen (task".out", "w", stdout);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
111196 KB |
Output is correct |
2 |
Correct |
32 ms |
111448 KB |
Output is correct |
3 |
Correct |
33 ms |
111416 KB |
Output is correct |
4 |
Correct |
34 ms |
111196 KB |
Output is correct |
5 |
Correct |
34 ms |
111448 KB |
Output is correct |
6 |
Correct |
35 ms |
111452 KB |
Output is correct |
7 |
Correct |
35 ms |
111708 KB |
Output is correct |
8 |
Correct |
33 ms |
111452 KB |
Output is correct |
9 |
Correct |
33 ms |
111452 KB |
Output is correct |
10 |
Correct |
35 ms |
111452 KB |
Output is correct |
11 |
Correct |
37 ms |
111560 KB |
Output is correct |
12 |
Correct |
34 ms |
111532 KB |
Output is correct |
13 |
Correct |
35 ms |
111448 KB |
Output is correct |
14 |
Correct |
35 ms |
111452 KB |
Output is correct |
15 |
Correct |
36 ms |
111448 KB |
Output is correct |
16 |
Correct |
33 ms |
111448 KB |
Output is correct |
17 |
Correct |
35 ms |
111572 KB |
Output is correct |
18 |
Correct |
34 ms |
111692 KB |
Output is correct |
19 |
Correct |
37 ms |
111544 KB |
Output is correct |
20 |
Correct |
35 ms |
111452 KB |
Output is correct |
21 |
Correct |
33 ms |
111400 KB |
Output is correct |
22 |
Correct |
34 ms |
111448 KB |
Output is correct |
23 |
Correct |
35 ms |
111448 KB |
Output is correct |
24 |
Correct |
34 ms |
111444 KB |
Output is correct |
25 |
Correct |
35 ms |
111452 KB |
Output is correct |
26 |
Correct |
34 ms |
111336 KB |
Output is correct |
27 |
Correct |
33 ms |
111352 KB |
Output is correct |
28 |
Correct |
35 ms |
111552 KB |
Output is correct |
29 |
Correct |
33 ms |
111452 KB |
Output is correct |
30 |
Correct |
33 ms |
111364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
111196 KB |
Output is correct |
2 |
Correct |
32 ms |
111448 KB |
Output is correct |
3 |
Correct |
33 ms |
111416 KB |
Output is correct |
4 |
Correct |
34 ms |
111196 KB |
Output is correct |
5 |
Correct |
34 ms |
111448 KB |
Output is correct |
6 |
Correct |
35 ms |
111452 KB |
Output is correct |
7 |
Correct |
35 ms |
111708 KB |
Output is correct |
8 |
Correct |
33 ms |
111452 KB |
Output is correct |
9 |
Correct |
33 ms |
111452 KB |
Output is correct |
10 |
Correct |
35 ms |
111452 KB |
Output is correct |
11 |
Correct |
37 ms |
111560 KB |
Output is correct |
12 |
Correct |
34 ms |
111532 KB |
Output is correct |
13 |
Correct |
35 ms |
111448 KB |
Output is correct |
14 |
Correct |
35 ms |
111452 KB |
Output is correct |
15 |
Correct |
36 ms |
111448 KB |
Output is correct |
16 |
Correct |
33 ms |
111448 KB |
Output is correct |
17 |
Correct |
35 ms |
111572 KB |
Output is correct |
18 |
Correct |
34 ms |
111692 KB |
Output is correct |
19 |
Correct |
37 ms |
111544 KB |
Output is correct |
20 |
Correct |
35 ms |
111452 KB |
Output is correct |
21 |
Correct |
33 ms |
111400 KB |
Output is correct |
22 |
Correct |
34 ms |
111448 KB |
Output is correct |
23 |
Correct |
35 ms |
111448 KB |
Output is correct |
24 |
Correct |
34 ms |
111444 KB |
Output is correct |
25 |
Correct |
35 ms |
111452 KB |
Output is correct |
26 |
Correct |
34 ms |
111336 KB |
Output is correct |
27 |
Correct |
33 ms |
111352 KB |
Output is correct |
28 |
Correct |
35 ms |
111552 KB |
Output is correct |
29 |
Correct |
33 ms |
111452 KB |
Output is correct |
30 |
Correct |
33 ms |
111364 KB |
Output is correct |
31 |
Correct |
573 ms |
141768 KB |
Output is correct |
32 |
Correct |
109 ms |
118212 KB |
Output is correct |
33 |
Correct |
467 ms |
131508 KB |
Output is correct |
34 |
Correct |
606 ms |
141780 KB |
Output is correct |
35 |
Correct |
610 ms |
138468 KB |
Output is correct |
36 |
Correct |
518 ms |
132308 KB |
Output is correct |
37 |
Correct |
321 ms |
132556 KB |
Output is correct |
38 |
Correct |
290 ms |
127948 KB |
Output is correct |
39 |
Correct |
268 ms |
129280 KB |
Output is correct |
40 |
Correct |
250 ms |
127960 KB |
Output is correct |
41 |
Correct |
430 ms |
137688 KB |
Output is correct |
42 |
Correct |
426 ms |
137844 KB |
Output is correct |
43 |
Correct |
85 ms |
118084 KB |
Output is correct |
44 |
Correct |
401 ms |
137164 KB |
Output is correct |
45 |
Correct |
375 ms |
134824 KB |
Output is correct |
46 |
Correct |
397 ms |
128964 KB |
Output is correct |
47 |
Correct |
243 ms |
134092 KB |
Output is correct |
48 |
Correct |
219 ms |
132820 KB |
Output is correct |
49 |
Correct |
275 ms |
134708 KB |
Output is correct |
50 |
Correct |
298 ms |
137124 KB |
Output is correct |
51 |
Correct |
264 ms |
133072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3328 ms |
299848 KB |
Output is correct |
2 |
Correct |
3366 ms |
192316 KB |
Output is correct |
3 |
Correct |
2629 ms |
341812 KB |
Output is correct |
4 |
Correct |
3423 ms |
304980 KB |
Output is correct |
5 |
Correct |
2635 ms |
164692 KB |
Output is correct |
6 |
Correct |
3269 ms |
183444 KB |
Output is correct |
7 |
Correct |
2226 ms |
318532 KB |
Output is correct |
8 |
Correct |
2456 ms |
292152 KB |
Output is correct |
9 |
Correct |
3061 ms |
291072 KB |
Output is correct |
10 |
Correct |
3455 ms |
238948 KB |
Output is correct |
11 |
Correct |
1300 ms |
198916 KB |
Output is correct |
12 |
Correct |
1498 ms |
231160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5033 ms |
282240 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
111196 KB |
Output is correct |
2 |
Correct |
32 ms |
111448 KB |
Output is correct |
3 |
Correct |
33 ms |
111416 KB |
Output is correct |
4 |
Correct |
34 ms |
111196 KB |
Output is correct |
5 |
Correct |
34 ms |
111448 KB |
Output is correct |
6 |
Correct |
35 ms |
111452 KB |
Output is correct |
7 |
Correct |
35 ms |
111708 KB |
Output is correct |
8 |
Correct |
33 ms |
111452 KB |
Output is correct |
9 |
Correct |
33 ms |
111452 KB |
Output is correct |
10 |
Correct |
35 ms |
111452 KB |
Output is correct |
11 |
Correct |
37 ms |
111560 KB |
Output is correct |
12 |
Correct |
34 ms |
111532 KB |
Output is correct |
13 |
Correct |
35 ms |
111448 KB |
Output is correct |
14 |
Correct |
35 ms |
111452 KB |
Output is correct |
15 |
Correct |
36 ms |
111448 KB |
Output is correct |
16 |
Correct |
33 ms |
111448 KB |
Output is correct |
17 |
Correct |
35 ms |
111572 KB |
Output is correct |
18 |
Correct |
34 ms |
111692 KB |
Output is correct |
19 |
Correct |
37 ms |
111544 KB |
Output is correct |
20 |
Correct |
35 ms |
111452 KB |
Output is correct |
21 |
Correct |
33 ms |
111400 KB |
Output is correct |
22 |
Correct |
34 ms |
111448 KB |
Output is correct |
23 |
Correct |
35 ms |
111448 KB |
Output is correct |
24 |
Correct |
34 ms |
111444 KB |
Output is correct |
25 |
Correct |
35 ms |
111452 KB |
Output is correct |
26 |
Correct |
34 ms |
111336 KB |
Output is correct |
27 |
Correct |
33 ms |
111352 KB |
Output is correct |
28 |
Correct |
35 ms |
111552 KB |
Output is correct |
29 |
Correct |
33 ms |
111452 KB |
Output is correct |
30 |
Correct |
33 ms |
111364 KB |
Output is correct |
31 |
Correct |
573 ms |
141768 KB |
Output is correct |
32 |
Correct |
109 ms |
118212 KB |
Output is correct |
33 |
Correct |
467 ms |
131508 KB |
Output is correct |
34 |
Correct |
606 ms |
141780 KB |
Output is correct |
35 |
Correct |
610 ms |
138468 KB |
Output is correct |
36 |
Correct |
518 ms |
132308 KB |
Output is correct |
37 |
Correct |
321 ms |
132556 KB |
Output is correct |
38 |
Correct |
290 ms |
127948 KB |
Output is correct |
39 |
Correct |
268 ms |
129280 KB |
Output is correct |
40 |
Correct |
250 ms |
127960 KB |
Output is correct |
41 |
Correct |
430 ms |
137688 KB |
Output is correct |
42 |
Correct |
426 ms |
137844 KB |
Output is correct |
43 |
Correct |
85 ms |
118084 KB |
Output is correct |
44 |
Correct |
401 ms |
137164 KB |
Output is correct |
45 |
Correct |
375 ms |
134824 KB |
Output is correct |
46 |
Correct |
397 ms |
128964 KB |
Output is correct |
47 |
Correct |
243 ms |
134092 KB |
Output is correct |
48 |
Correct |
219 ms |
132820 KB |
Output is correct |
49 |
Correct |
275 ms |
134708 KB |
Output is correct |
50 |
Correct |
298 ms |
137124 KB |
Output is correct |
51 |
Correct |
264 ms |
133072 KB |
Output is correct |
52 |
Correct |
445 ms |
158660 KB |
Output is correct |
53 |
Correct |
433 ms |
157700 KB |
Output is correct |
54 |
Correct |
561 ms |
150948 KB |
Output is correct |
55 |
Correct |
426 ms |
144632 KB |
Output is correct |
56 |
Correct |
418 ms |
145592 KB |
Output is correct |
57 |
Correct |
404 ms |
138096 KB |
Output is correct |
58 |
Correct |
421 ms |
144264 KB |
Output is correct |
59 |
Correct |
483 ms |
145972 KB |
Output is correct |
60 |
Correct |
444 ms |
139212 KB |
Output is correct |
61 |
Correct |
86 ms |
126676 KB |
Output is correct |
62 |
Correct |
512 ms |
150732 KB |
Output is correct |
63 |
Correct |
541 ms |
150300 KB |
Output is correct |
64 |
Correct |
574 ms |
148348 KB |
Output is correct |
65 |
Correct |
531 ms |
141896 KB |
Output is correct |
66 |
Correct |
413 ms |
138916 KB |
Output is correct |
67 |
Correct |
144 ms |
124112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
111196 KB |
Output is correct |
2 |
Correct |
32 ms |
111448 KB |
Output is correct |
3 |
Correct |
33 ms |
111416 KB |
Output is correct |
4 |
Correct |
34 ms |
111196 KB |
Output is correct |
5 |
Correct |
34 ms |
111448 KB |
Output is correct |
6 |
Correct |
35 ms |
111452 KB |
Output is correct |
7 |
Correct |
35 ms |
111708 KB |
Output is correct |
8 |
Correct |
33 ms |
111452 KB |
Output is correct |
9 |
Correct |
33 ms |
111452 KB |
Output is correct |
10 |
Correct |
35 ms |
111452 KB |
Output is correct |
11 |
Correct |
37 ms |
111560 KB |
Output is correct |
12 |
Correct |
34 ms |
111532 KB |
Output is correct |
13 |
Correct |
35 ms |
111448 KB |
Output is correct |
14 |
Correct |
35 ms |
111452 KB |
Output is correct |
15 |
Correct |
36 ms |
111448 KB |
Output is correct |
16 |
Correct |
33 ms |
111448 KB |
Output is correct |
17 |
Correct |
35 ms |
111572 KB |
Output is correct |
18 |
Correct |
34 ms |
111692 KB |
Output is correct |
19 |
Correct |
37 ms |
111544 KB |
Output is correct |
20 |
Correct |
35 ms |
111452 KB |
Output is correct |
21 |
Correct |
33 ms |
111400 KB |
Output is correct |
22 |
Correct |
34 ms |
111448 KB |
Output is correct |
23 |
Correct |
35 ms |
111448 KB |
Output is correct |
24 |
Correct |
34 ms |
111444 KB |
Output is correct |
25 |
Correct |
35 ms |
111452 KB |
Output is correct |
26 |
Correct |
34 ms |
111336 KB |
Output is correct |
27 |
Correct |
33 ms |
111352 KB |
Output is correct |
28 |
Correct |
35 ms |
111552 KB |
Output is correct |
29 |
Correct |
33 ms |
111452 KB |
Output is correct |
30 |
Correct |
33 ms |
111364 KB |
Output is correct |
31 |
Correct |
573 ms |
141768 KB |
Output is correct |
32 |
Correct |
109 ms |
118212 KB |
Output is correct |
33 |
Correct |
467 ms |
131508 KB |
Output is correct |
34 |
Correct |
606 ms |
141780 KB |
Output is correct |
35 |
Correct |
610 ms |
138468 KB |
Output is correct |
36 |
Correct |
518 ms |
132308 KB |
Output is correct |
37 |
Correct |
321 ms |
132556 KB |
Output is correct |
38 |
Correct |
290 ms |
127948 KB |
Output is correct |
39 |
Correct |
268 ms |
129280 KB |
Output is correct |
40 |
Correct |
250 ms |
127960 KB |
Output is correct |
41 |
Correct |
430 ms |
137688 KB |
Output is correct |
42 |
Correct |
426 ms |
137844 KB |
Output is correct |
43 |
Correct |
85 ms |
118084 KB |
Output is correct |
44 |
Correct |
401 ms |
137164 KB |
Output is correct |
45 |
Correct |
375 ms |
134824 KB |
Output is correct |
46 |
Correct |
397 ms |
128964 KB |
Output is correct |
47 |
Correct |
243 ms |
134092 KB |
Output is correct |
48 |
Correct |
219 ms |
132820 KB |
Output is correct |
49 |
Correct |
275 ms |
134708 KB |
Output is correct |
50 |
Correct |
298 ms |
137124 KB |
Output is correct |
51 |
Correct |
264 ms |
133072 KB |
Output is correct |
52 |
Correct |
3328 ms |
299848 KB |
Output is correct |
53 |
Correct |
3366 ms |
192316 KB |
Output is correct |
54 |
Correct |
2629 ms |
341812 KB |
Output is correct |
55 |
Correct |
3423 ms |
304980 KB |
Output is correct |
56 |
Correct |
2635 ms |
164692 KB |
Output is correct |
57 |
Correct |
3269 ms |
183444 KB |
Output is correct |
58 |
Correct |
2226 ms |
318532 KB |
Output is correct |
59 |
Correct |
2456 ms |
292152 KB |
Output is correct |
60 |
Correct |
3061 ms |
291072 KB |
Output is correct |
61 |
Correct |
3455 ms |
238948 KB |
Output is correct |
62 |
Correct |
1300 ms |
198916 KB |
Output is correct |
63 |
Correct |
1498 ms |
231160 KB |
Output is correct |
64 |
Execution timed out |
5033 ms |
282240 KB |
Time limit exceeded |
65 |
Halted |
0 ms |
0 KB |
- |