#include <bits/stdc++.h>
#define eb emplace_back
using namespace std;
const int sz = 1 << 22;
struct event{
int t, x, k;
event(int t, int x, int k) : t(t), x(x), k(k) {}
};
struct line{
int t, k, x, f;
line(int t, int k, int x, int f) : t(t), k(k), x(x), f(f) {}
};
struct seg{
multiset <int> L[sz + 100];
int T[sz + sz + 100];
seg()
{
int i;
for(i=0; i<sz; i++){
L[i].insert(-1e9);
}
for(i=0; i<sz+sz; i++){
T[i] = -1e9;
}
}
void insert(int p, int v)
{
L[p].insert(v);
p += sz;
for(; p; p>>=1){
if(p >= sz) T[p] = *prev(L[p - sz].end());
else T[p] = max(T[p << 1], T[p << 1 | 1]);
}
}
void erase(int p, int v)
{
L[p].erase(L[p].find(v));
p += sz;
for(; p; p>>=1){
if(p >= sz) T[p] = *prev(L[p - sz].end());
else T[p] = max(T[p << 1], T[p << 1 | 1]);
}
}
int get_max(int l, int r)
{
int ret = -1e9;
l += sz, r += sz;
for(; l<r; ){
if(l & 1) ret = max(ret, T[l]);
if(~r & 1) ret = max(ret, T[r]);
l = l + 1 >> 1;
r = r - 1 >> 1;
}
if(l == r) ret = max(ret, T[l]);
return ret;
}
};
seg TX, TY;
vector <line> LX, LY;
vector <event> E, Q;
multiset <int> S[303030];
vector <int> X;
int A[303030];
int n, g, q;
void update_line(int t, int k, int x, int f)
{
if(S[k].empty()){
LX.eb(t, 0, 1e9, !f);
LY.eb(t, 2e8, 1e9, !f);
LX.eb(t, 0, x, f);
LY.eb(t, 2e8, -x, f);
}
else{
auto it = S[k].lower_bound(x);
if(it == S[k].begin()){
LX.eb(t, 0, *it, !f);
LX.eb(t, 0, x, f);
LX.eb(t, (x + *it) >> 1, *it, f);
LY.eb(t, (x + *it) >> 1, -x, f);
}
else if(it == S[k].end()){
LY.eb(t, 2e8, -(*prev(it)), !f);
LY.eb(t, 2e8, -x, f);
LY.eb(t, (x + *prev(it)) >> 1, -(*prev(it)), f);
LX.eb(t, (x + *prev(it)) >> 1, x, f);
}
else{
LX.eb(t, (*it + *prev(it)) >> 1, *it, !f);
LY.eb(t, (*it + *prev(it)) >> 1, -(*prev(it)), !f);
LX.eb(t, (x + *it) >> 1, *it, f);
LY.eb(t, (x + *it) >> 1, -x, f);
LY.eb(t, (x + *prev(it)) >> 1, -(*prev(it)), f);
LX.eb(t, (x + *prev(it)) >> 1, x, f);
}
}
}
int main()
{
int x, t, a, b, i, j, k, i_, j_, k_;
scanf("%d%d%d", &n, &g, &q);
for(i=1; i<=n; i++){
scanf("%d%d%d%d", &x, &t, &a, &b);
E.eb(a, x << 1, t);
E.eb(b + 1, x << 1, -t);
}
sort(E.begin(), E.end(), [&](event &ea, event &eb){
return ea.t < eb.t;
});
for(i=1; i<=g; i++){
LX.eb(0, 0, 1e9, 0);
LY.eb(0, 2e8, 1e9, 0);
}
for(i=0; i<E.size(); i=i_){
t = E[i].t;
for(i_=i; i_<E.size() && E[i_].t == t; i_++){
k = E[i_].k; x = E[i_].x;
if(k > 0){
update_line(t, k, x, 0);
S[k].insert(x);
}
else{
S[-k].erase(S[-k].find(x));
update_line(t, -k, x, 1);
}
}
}
for(i=1; i<=q; i++){
scanf("%d%d", &x, &t);
x <<= 1; Q.eb(t, x, i);
X.push_back(x);
}
sort(Q.begin(), Q.end(), [&](event &ea, event &eb){
return ea.t < eb.t;
});
for(line &l: LX) X.push_back(l.k);
for(line &l: LY) X.push_back(l.k);
sort(X.begin(), X.end());
X.erase(unique(X.begin(), X.end()), X.end());
for(line &l: LX) l.k = lower_bound(X.begin(), X.end(), l.k) - X.begin();
for(line &l: LY) l.k = lower_bound(X.begin(), X.end(), l.k) - X.begin();
for(i=0, j=0, k=0; i<LX.size() || j<LY.size(); i=i_, j=j_, k=k_){
if(i == LX.size()) t = LY[j].t;
else if(j == LY.size()) t = LX[i].t;
else t = min(LX[i].t, LY[j].t);
for(k_=k; k_<Q.size() && Q[k_].t < t; k_++){
x = lower_bound(X.begin(), X.end(), Q[k_].x) - X.begin();
A[Q[k_].k] = max(TX.get_max(0, x) - Q[k_].x, Q[k_].x + TY.get_max(x, sz - 1));
}
for(i_=i; i_<LX.size() && LX[i_].t == t; i_++){
if(!LX[i_].f) TX.insert(LX[i_].k, LX[i_].x);
else TX.erase(LX[i_].k, LX[i_].x);
}
for(j_=j; j_<LY.size() && LY[j_].t == t; j_++){
if(!LY[j_].f) TY.insert(LY[j_].k, LY[j_].x);
else TY.erase(LY[j_].k, LY[j_].x);
}
}
for(; k<Q.size(); k++){
x = lower_bound(X.begin(), X.end(), Q[k].x) - X.begin();
A[Q[k].k] = max(TX.get_max(0, x) - Q[k].x, Q[k].x + TY.get_max(x, sz - 1));
}
for(i=1; i<=q; i++){
if(A[i] >= 2e8) printf("-1\n");
else printf("%d\n", A[i] >> 1);
}
return 0;
}
Compilation message
new_home.cpp: In member function 'int seg::get_max(int, int)':
new_home.cpp:66:10: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
l = l + 1 >> 1;
~~^~~
new_home.cpp:67:10: warning: suggest parentheses around '-' inside '>>' [-Wparentheses]
r = r - 1 >> 1;
~~^~~
new_home.cpp: In function 'int main()':
new_home.cpp:138:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0; i<E.size(); i=i_){
~^~~~~~~~~
new_home.cpp:140:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i_=i; i_<E.size() && E[i_].t == t; i_++){
~~^~~~~~~~~
new_home.cpp:172:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0, j=0, k=0; i<LX.size() || j<LY.size(); i=i_, j=j_, k=k_){
~^~~~~~~~~~
new_home.cpp:172:37: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0, j=0, k=0; i<LX.size() || j<LY.size(); i=i_, j=j_, k=k_){
~^~~~~~~~~~
new_home.cpp:173:8: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(i == LX.size()) t = LY[j].t;
~~^~~~~~~~~~~~
new_home.cpp:174:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
else if(j == LY.size()) t = LX[i].t;
~~^~~~~~~~~~~~
new_home.cpp:177:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(k_=k; k_<Q.size() && Q[k_].t < t; k_++){
~~^~~~~~~~~
new_home.cpp:182:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i_=i; i_<LX.size() && LX[i_].t == t; i_++){
~~^~~~~~~~~~
new_home.cpp:187:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(j_=j; j_<LY.size() && LY[j_].t == t; j_++){
~~^~~~~~~~~~
new_home.cpp:193:9: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(; k<Q.size(); k++){
~^~~~~~~~~
new_home.cpp:121:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d", &n, &g, &q);
~~~~~^~~~~~~~~~~~~~~~~~~~~~
new_home.cpp:124:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d%d", &x, &t, &a, &b);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
new_home.cpp:154:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &x, &t);
~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1118 ms |
868340 KB |
Output is correct |
2 |
Correct |
1091 ms |
868344 KB |
Output is correct |
3 |
Correct |
1103 ms |
868236 KB |
Output is correct |
4 |
Correct |
1103 ms |
868232 KB |
Output is correct |
5 |
Correct |
1108 ms |
868544 KB |
Output is correct |
6 |
Correct |
1092 ms |
868552 KB |
Output is correct |
7 |
Correct |
1097 ms |
868472 KB |
Output is correct |
8 |
Correct |
1099 ms |
868600 KB |
Output is correct |
9 |
Correct |
1090 ms |
868420 KB |
Output is correct |
10 |
Correct |
1091 ms |
868516 KB |
Output is correct |
11 |
Correct |
1151 ms |
868472 KB |
Output is correct |
12 |
Correct |
1097 ms |
868536 KB |
Output is correct |
13 |
Correct |
1090 ms |
868472 KB |
Output is correct |
14 |
Correct |
1092 ms |
868472 KB |
Output is correct |
15 |
Correct |
1092 ms |
868492 KB |
Output is correct |
16 |
Correct |
1108 ms |
868560 KB |
Output is correct |
17 |
Correct |
1096 ms |
868600 KB |
Output is correct |
18 |
Correct |
1122 ms |
868600 KB |
Output is correct |
19 |
Correct |
1094 ms |
868472 KB |
Output is correct |
20 |
Correct |
1097 ms |
868344 KB |
Output is correct |
21 |
Correct |
1101 ms |
868468 KB |
Output is correct |
22 |
Correct |
1114 ms |
868600 KB |
Output is correct |
23 |
Correct |
1097 ms |
868472 KB |
Output is correct |
24 |
Correct |
1090 ms |
868352 KB |
Output is correct |
25 |
Correct |
1099 ms |
868400 KB |
Output is correct |
26 |
Correct |
1094 ms |
868520 KB |
Output is correct |
27 |
Correct |
1100 ms |
868348 KB |
Output is correct |
28 |
Correct |
1101 ms |
868600 KB |
Output is correct |
29 |
Correct |
1103 ms |
868472 KB |
Output is correct |
30 |
Correct |
1096 ms |
868452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1118 ms |
868340 KB |
Output is correct |
2 |
Correct |
1091 ms |
868344 KB |
Output is correct |
3 |
Correct |
1103 ms |
868236 KB |
Output is correct |
4 |
Correct |
1103 ms |
868232 KB |
Output is correct |
5 |
Correct |
1108 ms |
868544 KB |
Output is correct |
6 |
Correct |
1092 ms |
868552 KB |
Output is correct |
7 |
Correct |
1097 ms |
868472 KB |
Output is correct |
8 |
Correct |
1099 ms |
868600 KB |
Output is correct |
9 |
Correct |
1090 ms |
868420 KB |
Output is correct |
10 |
Correct |
1091 ms |
868516 KB |
Output is correct |
11 |
Correct |
1151 ms |
868472 KB |
Output is correct |
12 |
Correct |
1097 ms |
868536 KB |
Output is correct |
13 |
Correct |
1090 ms |
868472 KB |
Output is correct |
14 |
Correct |
1092 ms |
868472 KB |
Output is correct |
15 |
Correct |
1092 ms |
868492 KB |
Output is correct |
16 |
Correct |
1108 ms |
868560 KB |
Output is correct |
17 |
Correct |
1096 ms |
868600 KB |
Output is correct |
18 |
Correct |
1122 ms |
868600 KB |
Output is correct |
19 |
Correct |
1094 ms |
868472 KB |
Output is correct |
20 |
Correct |
1097 ms |
868344 KB |
Output is correct |
21 |
Correct |
1101 ms |
868468 KB |
Output is correct |
22 |
Correct |
1114 ms |
868600 KB |
Output is correct |
23 |
Correct |
1097 ms |
868472 KB |
Output is correct |
24 |
Correct |
1090 ms |
868352 KB |
Output is correct |
25 |
Correct |
1099 ms |
868400 KB |
Output is correct |
26 |
Correct |
1094 ms |
868520 KB |
Output is correct |
27 |
Correct |
1100 ms |
868348 KB |
Output is correct |
28 |
Correct |
1101 ms |
868600 KB |
Output is correct |
29 |
Correct |
1103 ms |
868472 KB |
Output is correct |
30 |
Correct |
1096 ms |
868452 KB |
Output is correct |
31 |
Correct |
1845 ms |
894016 KB |
Output is correct |
32 |
Correct |
1392 ms |
890056 KB |
Output is correct |
33 |
Correct |
1785 ms |
892652 KB |
Output is correct |
34 |
Correct |
1778 ms |
892468 KB |
Output is correct |
35 |
Correct |
1822 ms |
893824 KB |
Output is correct |
36 |
Correct |
1800 ms |
893928 KB |
Output is correct |
37 |
Correct |
1599 ms |
892580 KB |
Output is correct |
38 |
Correct |
1587 ms |
892412 KB |
Output is correct |
39 |
Correct |
1484 ms |
892340 KB |
Output is correct |
40 |
Correct |
1525 ms |
892604 KB |
Output is correct |
41 |
Correct |
1501 ms |
888916 KB |
Output is correct |
42 |
Correct |
1509 ms |
888844 KB |
Output is correct |
43 |
Correct |
1289 ms |
888944 KB |
Output is correct |
44 |
Correct |
1481 ms |
888952 KB |
Output is correct |
45 |
Correct |
1452 ms |
889016 KB |
Output is correct |
46 |
Correct |
1443 ms |
888816 KB |
Output is correct |
47 |
Correct |
1320 ms |
887796 KB |
Output is correct |
48 |
Correct |
1315 ms |
887684 KB |
Output is correct |
49 |
Correct |
1367 ms |
888116 KB |
Output is correct |
50 |
Correct |
1388 ms |
888424 KB |
Output is correct |
51 |
Correct |
1362 ms |
888132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4427 ms |
983560 KB |
Output is correct |
2 |
Execution timed out |
5134 ms |
996400 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5151 ms |
992072 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1118 ms |
868340 KB |
Output is correct |
2 |
Correct |
1091 ms |
868344 KB |
Output is correct |
3 |
Correct |
1103 ms |
868236 KB |
Output is correct |
4 |
Correct |
1103 ms |
868232 KB |
Output is correct |
5 |
Correct |
1108 ms |
868544 KB |
Output is correct |
6 |
Correct |
1092 ms |
868552 KB |
Output is correct |
7 |
Correct |
1097 ms |
868472 KB |
Output is correct |
8 |
Correct |
1099 ms |
868600 KB |
Output is correct |
9 |
Correct |
1090 ms |
868420 KB |
Output is correct |
10 |
Correct |
1091 ms |
868516 KB |
Output is correct |
11 |
Correct |
1151 ms |
868472 KB |
Output is correct |
12 |
Correct |
1097 ms |
868536 KB |
Output is correct |
13 |
Correct |
1090 ms |
868472 KB |
Output is correct |
14 |
Correct |
1092 ms |
868472 KB |
Output is correct |
15 |
Correct |
1092 ms |
868492 KB |
Output is correct |
16 |
Correct |
1108 ms |
868560 KB |
Output is correct |
17 |
Correct |
1096 ms |
868600 KB |
Output is correct |
18 |
Correct |
1122 ms |
868600 KB |
Output is correct |
19 |
Correct |
1094 ms |
868472 KB |
Output is correct |
20 |
Correct |
1097 ms |
868344 KB |
Output is correct |
21 |
Correct |
1101 ms |
868468 KB |
Output is correct |
22 |
Correct |
1114 ms |
868600 KB |
Output is correct |
23 |
Correct |
1097 ms |
868472 KB |
Output is correct |
24 |
Correct |
1090 ms |
868352 KB |
Output is correct |
25 |
Correct |
1099 ms |
868400 KB |
Output is correct |
26 |
Correct |
1094 ms |
868520 KB |
Output is correct |
27 |
Correct |
1100 ms |
868348 KB |
Output is correct |
28 |
Correct |
1101 ms |
868600 KB |
Output is correct |
29 |
Correct |
1103 ms |
868472 KB |
Output is correct |
30 |
Correct |
1096 ms |
868452 KB |
Output is correct |
31 |
Correct |
1845 ms |
894016 KB |
Output is correct |
32 |
Correct |
1392 ms |
890056 KB |
Output is correct |
33 |
Correct |
1785 ms |
892652 KB |
Output is correct |
34 |
Correct |
1778 ms |
892468 KB |
Output is correct |
35 |
Correct |
1822 ms |
893824 KB |
Output is correct |
36 |
Correct |
1800 ms |
893928 KB |
Output is correct |
37 |
Correct |
1599 ms |
892580 KB |
Output is correct |
38 |
Correct |
1587 ms |
892412 KB |
Output is correct |
39 |
Correct |
1484 ms |
892340 KB |
Output is correct |
40 |
Correct |
1525 ms |
892604 KB |
Output is correct |
41 |
Correct |
1501 ms |
888916 KB |
Output is correct |
42 |
Correct |
1509 ms |
888844 KB |
Output is correct |
43 |
Correct |
1289 ms |
888944 KB |
Output is correct |
44 |
Correct |
1481 ms |
888952 KB |
Output is correct |
45 |
Correct |
1452 ms |
889016 KB |
Output is correct |
46 |
Correct |
1443 ms |
888816 KB |
Output is correct |
47 |
Correct |
1320 ms |
887796 KB |
Output is correct |
48 |
Correct |
1315 ms |
887684 KB |
Output is correct |
49 |
Correct |
1367 ms |
888116 KB |
Output is correct |
50 |
Correct |
1388 ms |
888424 KB |
Output is correct |
51 |
Correct |
1362 ms |
888132 KB |
Output is correct |
52 |
Correct |
1560 ms |
891492 KB |
Output is correct |
53 |
Correct |
1548 ms |
891604 KB |
Output is correct |
54 |
Correct |
1678 ms |
890772 KB |
Output is correct |
55 |
Correct |
1525 ms |
889488 KB |
Output is correct |
56 |
Correct |
1506 ms |
889692 KB |
Output is correct |
57 |
Correct |
1502 ms |
888996 KB |
Output is correct |
58 |
Correct |
1519 ms |
889372 KB |
Output is correct |
59 |
Correct |
1513 ms |
889556 KB |
Output is correct |
60 |
Correct |
1523 ms |
888908 KB |
Output is correct |
61 |
Correct |
1366 ms |
891252 KB |
Output is correct |
62 |
Correct |
1583 ms |
891728 KB |
Output is correct |
63 |
Correct |
1608 ms |
889692 KB |
Output is correct |
64 |
Correct |
1563 ms |
889120 KB |
Output is correct |
65 |
Correct |
1556 ms |
888976 KB |
Output is correct |
66 |
Correct |
1503 ms |
888888 KB |
Output is correct |
67 |
Correct |
1408 ms |
887276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1118 ms |
868340 KB |
Output is correct |
2 |
Correct |
1091 ms |
868344 KB |
Output is correct |
3 |
Correct |
1103 ms |
868236 KB |
Output is correct |
4 |
Correct |
1103 ms |
868232 KB |
Output is correct |
5 |
Correct |
1108 ms |
868544 KB |
Output is correct |
6 |
Correct |
1092 ms |
868552 KB |
Output is correct |
7 |
Correct |
1097 ms |
868472 KB |
Output is correct |
8 |
Correct |
1099 ms |
868600 KB |
Output is correct |
9 |
Correct |
1090 ms |
868420 KB |
Output is correct |
10 |
Correct |
1091 ms |
868516 KB |
Output is correct |
11 |
Correct |
1151 ms |
868472 KB |
Output is correct |
12 |
Correct |
1097 ms |
868536 KB |
Output is correct |
13 |
Correct |
1090 ms |
868472 KB |
Output is correct |
14 |
Correct |
1092 ms |
868472 KB |
Output is correct |
15 |
Correct |
1092 ms |
868492 KB |
Output is correct |
16 |
Correct |
1108 ms |
868560 KB |
Output is correct |
17 |
Correct |
1096 ms |
868600 KB |
Output is correct |
18 |
Correct |
1122 ms |
868600 KB |
Output is correct |
19 |
Correct |
1094 ms |
868472 KB |
Output is correct |
20 |
Correct |
1097 ms |
868344 KB |
Output is correct |
21 |
Correct |
1101 ms |
868468 KB |
Output is correct |
22 |
Correct |
1114 ms |
868600 KB |
Output is correct |
23 |
Correct |
1097 ms |
868472 KB |
Output is correct |
24 |
Correct |
1090 ms |
868352 KB |
Output is correct |
25 |
Correct |
1099 ms |
868400 KB |
Output is correct |
26 |
Correct |
1094 ms |
868520 KB |
Output is correct |
27 |
Correct |
1100 ms |
868348 KB |
Output is correct |
28 |
Correct |
1101 ms |
868600 KB |
Output is correct |
29 |
Correct |
1103 ms |
868472 KB |
Output is correct |
30 |
Correct |
1096 ms |
868452 KB |
Output is correct |
31 |
Correct |
1845 ms |
894016 KB |
Output is correct |
32 |
Correct |
1392 ms |
890056 KB |
Output is correct |
33 |
Correct |
1785 ms |
892652 KB |
Output is correct |
34 |
Correct |
1778 ms |
892468 KB |
Output is correct |
35 |
Correct |
1822 ms |
893824 KB |
Output is correct |
36 |
Correct |
1800 ms |
893928 KB |
Output is correct |
37 |
Correct |
1599 ms |
892580 KB |
Output is correct |
38 |
Correct |
1587 ms |
892412 KB |
Output is correct |
39 |
Correct |
1484 ms |
892340 KB |
Output is correct |
40 |
Correct |
1525 ms |
892604 KB |
Output is correct |
41 |
Correct |
1501 ms |
888916 KB |
Output is correct |
42 |
Correct |
1509 ms |
888844 KB |
Output is correct |
43 |
Correct |
1289 ms |
888944 KB |
Output is correct |
44 |
Correct |
1481 ms |
888952 KB |
Output is correct |
45 |
Correct |
1452 ms |
889016 KB |
Output is correct |
46 |
Correct |
1443 ms |
888816 KB |
Output is correct |
47 |
Correct |
1320 ms |
887796 KB |
Output is correct |
48 |
Correct |
1315 ms |
887684 KB |
Output is correct |
49 |
Correct |
1367 ms |
888116 KB |
Output is correct |
50 |
Correct |
1388 ms |
888424 KB |
Output is correct |
51 |
Correct |
1362 ms |
888132 KB |
Output is correct |
52 |
Correct |
4427 ms |
983560 KB |
Output is correct |
53 |
Execution timed out |
5134 ms |
996400 KB |
Time limit exceeded |
54 |
Halted |
0 ms |
0 KB |
- |