This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |