//Pantyhose(black) + glasses = infinity
#include <bits/stdc++.h>
using namespace std;
#define debug(x) cerr << #x << " = " << x << '\n';
#define BP() cerr << "OK!\n";
#define PR(A, n) {cerr << #A << " = "; for (int _=1; _<=n; ++_) cerr << A[_] << ' '; cerr << '\n';}
#define PR0(A, n) {cerr << #A << " = "; for (int _=0; _<n; ++_) cerr << A[_] << ' '; cerr << '\n';}
#define FILE_NAME "new_home"
const int MAX_N = 300002;
const int INF = 1e9+100;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
struct treap {
struct item {
int L, R, max_r, prior;
item *l, *r;
item(int L=0, int R=0): L(L), R(R) {
max_r = R;
prior = uniform_int_distribution<int>(1, INF)(rng);
l = r = nullptr;
}
};
typedef item * pitem;
pitem root = nullptr;
int get_max_r(pitem t) {
return t ? t->max_r : -INF;
}
void upd(pitem t) {
if (t)
t->max_r = max(t->R, max(get_max_r(t->l), get_max_r(t->r)));
}
void split(pitem t, pitem &l, pitem &r, int key) {
if (!t) {
l = r = nullptr;
}
else {
if (key>t->L)
split(t->r, t->r, r, key), l = t;
else
split(t->l, l, t->l, key), r = t;
}
upd(t);
}
void merge(pitem &t, pitem l, pitem r) {
if (!l || !r) {
t = l ? l : r;
}
else {
if (l->prior>r->prior)
merge(l->r, l->r, r), t = l;
else
merge(r->l, l, r->l), t = r;
}
upd(t);
}
void insert(pitem &t, pitem it) {
if (!t) {
t = it;
}
else {
if (it->prior>t->prior)
split(t, it->l, it->r, it->L), t = it;
else
insert(it->L<t->L ? t->l : t->r, it);
}
upd(t);
}
void erase(pitem &t, int L, int R) {
if (!t)
return;
if (t->L==L && t->R==R)
merge(t, t->l, t->r);
else
erase(L<t->L ? t->l : t->r, L, R);
upd(t);
}
bool get(pitem t, int L, int R) {
if (get_max_r(t)<=R)
return true;
if (L<t->L)
return get(t->l, L, R);
return t->R<=R && get_max_r(t->l)<=R && get(t->r, L, R);
}
void insert(int L, int R) {
insert(root, new item(L, R));
}
void erase(int L, int R) {
erase(root, L, R);
}
bool get(int L, int R) {
return get(root, L, R);
}
};
struct store {
int x, t, a, b;
};
struct query {
int l, y;
};
int n, k, nQueries, res[MAX_N];
vector<int> T, store_event[MAX_N*3], q[MAX_N*3];
store s[MAX_N];
query Q[MAX_N];
map<int, int> pos[MAX_N*3];
treap interval_set;
int read_int() {
char c;
for (c = getchar(); c<'0' || c>'9'; c = getchar());
int res = c - '0';
for (c = getchar(); '0'<=c && c<='9'; c = getchar())
res = res * 10 + c - '0';
return res;
}
void read_input() {
n = read_int();
k = read_int();
nQueries = read_int();
for (int i=1; i<=n; ++i) {
s[i].x = read_int();
s[i].t = read_int();
s[i].a = read_int();
s[i].b = read_int();
}
for (int i=1; i<=nQueries; ++i) {
Q[i].l = read_int();
Q[i].y = read_int();
}
}
void compress() {
for (int i=1; i<=n; ++i) {
T.push_back(s[i].a);
T.push_back(s[i].b);
}
for (int i=1; i<=nQueries; ++i)
T.push_back(Q[i].y);
sort(T.begin(), T.end());
T.resize(unique(T.begin(), T.end()) - T.begin());
#define get_pos(P, x) lower_bound(P.begin(), P.end(), x) - P.begin() + 1
for (int i=1; i<=n; ++i) {
s[i].a = get_pos(T, s[i].a);
s[i].b = get_pos(T, s[i].b);
}
for (int i=1; i<=nQueries; ++i)
Q[i].y = get_pos(T, Q[i].y);
#undef uniq
}
void init() {
for (int i=1; i<=n; ++i) {
store_event[s[i].a].push_back(i);
store_event[s[i].b+1].push_back(-i);
}
for (int i=1; i<=nQueries; ++i) {
q[Q[i].y].push_back(i);
}
for (int i=1; i<=k; ++i) {
++pos[i][-INF];
++pos[i][INF];
interval_set.insert(-INF, INF);
}
}
void add_store(int idx) {
int tmp = ++pos[s[idx].t][s[idx].x];
if (tmp==1) {
auto u = pos[s[idx].t].lower_bound(s[idx].x);
--u;
auto v = pos[s[idx].t].upper_bound(s[idx].x);
// debug(u->first);
// debug(v->first);
interval_set.erase(u->first, v->first);
interval_set.insert(u->first, s[idx].x);
interval_set.insert(s[idx].x, v->first);
}
}
void remove_store(int idx) {
int tmp = --pos[s[idx].t][s[idx].x];
if (!tmp) {
auto u = pos[s[idx].t].lower_bound(s[idx].x);
--u;
auto v = pos[s[idx].t].upper_bound(s[idx].x);
// debug(u->first);
// debug(v->first);
interval_set.erase(u->first, s[idx].x);
interval_set.erase(s[idx].x, v->first);
interval_set.insert(u->first, v->first);
pos[s[idx].t].erase(s[idx].x);
}
}
int bisect(int x) {
int l = 0, r = 2e8;
for (int mid=(l+r)/2; mid!=l && r!=mid; mid=(l+r)/2) {
if (interval_set.get(x-mid-1, x+mid))
r = mid;
else
l = mid;
}
for (int i=l; i<=r; ++i) {
if (interval_set.get(x-i-1, x+i))
return i;
}
return -1;
}
void solve() {
for (int i=1; i<=T.size(); ++i) {
for (auto x : store_event[i]) {
if (x>0)
add_store(x);
else
remove_store(-x);
}
for (auto x : q[i])
res[x] = bisect(Q[x].l);
}
for (int i=1; i<=nQueries; ++i)
cout << res[i] << '\n';
}
int main() {
#ifdef GLASSES_GIRL
freopen(FILE_NAME".in", "r", stdin);
freopen(FILE_NAME".out", "w", stdout);
#endif
ios::sync_with_stdio(0); cin.tie(0);
read_input();
compress();
init();
solve();
}
Compilation message
new_home.cpp: In function 'void solve()':
new_home.cpp:259:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=1; i<=T.size(); ++i) {
~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
89 ms |
84984 KB |
Output is correct |
2 |
Correct |
81 ms |
84984 KB |
Output is correct |
3 |
Correct |
78 ms |
84992 KB |
Output is correct |
4 |
Correct |
85 ms |
84856 KB |
Output is correct |
5 |
Correct |
80 ms |
84984 KB |
Output is correct |
6 |
Correct |
81 ms |
84984 KB |
Output is correct |
7 |
Correct |
91 ms |
85044 KB |
Output is correct |
8 |
Correct |
79 ms |
85112 KB |
Output is correct |
9 |
Correct |
87 ms |
85112 KB |
Output is correct |
10 |
Correct |
87 ms |
84960 KB |
Output is correct |
11 |
Correct |
93 ms |
85060 KB |
Output is correct |
12 |
Correct |
81 ms |
85112 KB |
Output is correct |
13 |
Correct |
89 ms |
84988 KB |
Output is correct |
14 |
Correct |
98 ms |
85096 KB |
Output is correct |
15 |
Correct |
82 ms |
84984 KB |
Output is correct |
16 |
Correct |
93 ms |
85108 KB |
Output is correct |
17 |
Correct |
100 ms |
84992 KB |
Output is correct |
18 |
Correct |
86 ms |
85088 KB |
Output is correct |
19 |
Correct |
92 ms |
85112 KB |
Output is correct |
20 |
Correct |
86 ms |
84984 KB |
Output is correct |
21 |
Correct |
92 ms |
85116 KB |
Output is correct |
22 |
Correct |
87 ms |
85112 KB |
Output is correct |
23 |
Correct |
89 ms |
85112 KB |
Output is correct |
24 |
Correct |
91 ms |
84984 KB |
Output is correct |
25 |
Correct |
85 ms |
84984 KB |
Output is correct |
26 |
Correct |
82 ms |
84984 KB |
Output is correct |
27 |
Correct |
95 ms |
84984 KB |
Output is correct |
28 |
Correct |
85 ms |
85104 KB |
Output is correct |
29 |
Correct |
83 ms |
84984 KB |
Output is correct |
30 |
Correct |
86 ms |
85084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
89 ms |
84984 KB |
Output is correct |
2 |
Correct |
81 ms |
84984 KB |
Output is correct |
3 |
Correct |
78 ms |
84992 KB |
Output is correct |
4 |
Correct |
85 ms |
84856 KB |
Output is correct |
5 |
Correct |
80 ms |
84984 KB |
Output is correct |
6 |
Correct |
81 ms |
84984 KB |
Output is correct |
7 |
Correct |
91 ms |
85044 KB |
Output is correct |
8 |
Correct |
79 ms |
85112 KB |
Output is correct |
9 |
Correct |
87 ms |
85112 KB |
Output is correct |
10 |
Correct |
87 ms |
84960 KB |
Output is correct |
11 |
Correct |
93 ms |
85060 KB |
Output is correct |
12 |
Correct |
81 ms |
85112 KB |
Output is correct |
13 |
Correct |
89 ms |
84988 KB |
Output is correct |
14 |
Correct |
98 ms |
85096 KB |
Output is correct |
15 |
Correct |
82 ms |
84984 KB |
Output is correct |
16 |
Correct |
93 ms |
85108 KB |
Output is correct |
17 |
Correct |
100 ms |
84992 KB |
Output is correct |
18 |
Correct |
86 ms |
85088 KB |
Output is correct |
19 |
Correct |
92 ms |
85112 KB |
Output is correct |
20 |
Correct |
86 ms |
84984 KB |
Output is correct |
21 |
Correct |
92 ms |
85116 KB |
Output is correct |
22 |
Correct |
87 ms |
85112 KB |
Output is correct |
23 |
Correct |
89 ms |
85112 KB |
Output is correct |
24 |
Correct |
91 ms |
84984 KB |
Output is correct |
25 |
Correct |
85 ms |
84984 KB |
Output is correct |
26 |
Correct |
82 ms |
84984 KB |
Output is correct |
27 |
Correct |
95 ms |
84984 KB |
Output is correct |
28 |
Correct |
85 ms |
85104 KB |
Output is correct |
29 |
Correct |
83 ms |
84984 KB |
Output is correct |
30 |
Correct |
86 ms |
85084 KB |
Output is correct |
31 |
Correct |
981 ms |
102012 KB |
Output is correct |
32 |
Correct |
127 ms |
88304 KB |
Output is correct |
33 |
Correct |
840 ms |
101484 KB |
Output is correct |
34 |
Correct |
912 ms |
101372 KB |
Output is correct |
35 |
Correct |
778 ms |
101840 KB |
Output is correct |
36 |
Correct |
812 ms |
101820 KB |
Output is correct |
37 |
Correct |
546 ms |
101344 KB |
Output is correct |
38 |
Correct |
491 ms |
101200 KB |
Output is correct |
39 |
Correct |
393 ms |
101476 KB |
Output is correct |
40 |
Correct |
443 ms |
101332 KB |
Output is correct |
41 |
Correct |
782 ms |
101316 KB |
Output is correct |
42 |
Correct |
1836 ms |
101304 KB |
Output is correct |
43 |
Correct |
156 ms |
89072 KB |
Output is correct |
44 |
Correct |
747 ms |
101488 KB |
Output is correct |
45 |
Correct |
486 ms |
101360 KB |
Output is correct |
46 |
Correct |
338 ms |
101260 KB |
Output is correct |
47 |
Correct |
279 ms |
100588 KB |
Output is correct |
48 |
Correct |
281 ms |
100336 KB |
Output is correct |
49 |
Correct |
340 ms |
100720 KB |
Output is correct |
50 |
Correct |
1022 ms |
101084 KB |
Output is correct |
51 |
Correct |
308 ms |
100860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5085 ms |
112828 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5072 ms |
130780 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
89 ms |
84984 KB |
Output is correct |
2 |
Correct |
81 ms |
84984 KB |
Output is correct |
3 |
Correct |
78 ms |
84992 KB |
Output is correct |
4 |
Correct |
85 ms |
84856 KB |
Output is correct |
5 |
Correct |
80 ms |
84984 KB |
Output is correct |
6 |
Correct |
81 ms |
84984 KB |
Output is correct |
7 |
Correct |
91 ms |
85044 KB |
Output is correct |
8 |
Correct |
79 ms |
85112 KB |
Output is correct |
9 |
Correct |
87 ms |
85112 KB |
Output is correct |
10 |
Correct |
87 ms |
84960 KB |
Output is correct |
11 |
Correct |
93 ms |
85060 KB |
Output is correct |
12 |
Correct |
81 ms |
85112 KB |
Output is correct |
13 |
Correct |
89 ms |
84988 KB |
Output is correct |
14 |
Correct |
98 ms |
85096 KB |
Output is correct |
15 |
Correct |
82 ms |
84984 KB |
Output is correct |
16 |
Correct |
93 ms |
85108 KB |
Output is correct |
17 |
Correct |
100 ms |
84992 KB |
Output is correct |
18 |
Correct |
86 ms |
85088 KB |
Output is correct |
19 |
Correct |
92 ms |
85112 KB |
Output is correct |
20 |
Correct |
86 ms |
84984 KB |
Output is correct |
21 |
Correct |
92 ms |
85116 KB |
Output is correct |
22 |
Correct |
87 ms |
85112 KB |
Output is correct |
23 |
Correct |
89 ms |
85112 KB |
Output is correct |
24 |
Correct |
91 ms |
84984 KB |
Output is correct |
25 |
Correct |
85 ms |
84984 KB |
Output is correct |
26 |
Correct |
82 ms |
84984 KB |
Output is correct |
27 |
Correct |
95 ms |
84984 KB |
Output is correct |
28 |
Correct |
85 ms |
85104 KB |
Output is correct |
29 |
Correct |
83 ms |
84984 KB |
Output is correct |
30 |
Correct |
86 ms |
85084 KB |
Output is correct |
31 |
Correct |
981 ms |
102012 KB |
Output is correct |
32 |
Correct |
127 ms |
88304 KB |
Output is correct |
33 |
Correct |
840 ms |
101484 KB |
Output is correct |
34 |
Correct |
912 ms |
101372 KB |
Output is correct |
35 |
Correct |
778 ms |
101840 KB |
Output is correct |
36 |
Correct |
812 ms |
101820 KB |
Output is correct |
37 |
Correct |
546 ms |
101344 KB |
Output is correct |
38 |
Correct |
491 ms |
101200 KB |
Output is correct |
39 |
Correct |
393 ms |
101476 KB |
Output is correct |
40 |
Correct |
443 ms |
101332 KB |
Output is correct |
41 |
Correct |
782 ms |
101316 KB |
Output is correct |
42 |
Correct |
1836 ms |
101304 KB |
Output is correct |
43 |
Correct |
156 ms |
89072 KB |
Output is correct |
44 |
Correct |
747 ms |
101488 KB |
Output is correct |
45 |
Correct |
486 ms |
101360 KB |
Output is correct |
46 |
Correct |
338 ms |
101260 KB |
Output is correct |
47 |
Correct |
279 ms |
100588 KB |
Output is correct |
48 |
Correct |
281 ms |
100336 KB |
Output is correct |
49 |
Correct |
340 ms |
100720 KB |
Output is correct |
50 |
Correct |
1022 ms |
101084 KB |
Output is correct |
51 |
Correct |
308 ms |
100860 KB |
Output is correct |
52 |
Execution timed out |
5010 ms |
97632 KB |
Time limit exceeded |
53 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
89 ms |
84984 KB |
Output is correct |
2 |
Correct |
81 ms |
84984 KB |
Output is correct |
3 |
Correct |
78 ms |
84992 KB |
Output is correct |
4 |
Correct |
85 ms |
84856 KB |
Output is correct |
5 |
Correct |
80 ms |
84984 KB |
Output is correct |
6 |
Correct |
81 ms |
84984 KB |
Output is correct |
7 |
Correct |
91 ms |
85044 KB |
Output is correct |
8 |
Correct |
79 ms |
85112 KB |
Output is correct |
9 |
Correct |
87 ms |
85112 KB |
Output is correct |
10 |
Correct |
87 ms |
84960 KB |
Output is correct |
11 |
Correct |
93 ms |
85060 KB |
Output is correct |
12 |
Correct |
81 ms |
85112 KB |
Output is correct |
13 |
Correct |
89 ms |
84988 KB |
Output is correct |
14 |
Correct |
98 ms |
85096 KB |
Output is correct |
15 |
Correct |
82 ms |
84984 KB |
Output is correct |
16 |
Correct |
93 ms |
85108 KB |
Output is correct |
17 |
Correct |
100 ms |
84992 KB |
Output is correct |
18 |
Correct |
86 ms |
85088 KB |
Output is correct |
19 |
Correct |
92 ms |
85112 KB |
Output is correct |
20 |
Correct |
86 ms |
84984 KB |
Output is correct |
21 |
Correct |
92 ms |
85116 KB |
Output is correct |
22 |
Correct |
87 ms |
85112 KB |
Output is correct |
23 |
Correct |
89 ms |
85112 KB |
Output is correct |
24 |
Correct |
91 ms |
84984 KB |
Output is correct |
25 |
Correct |
85 ms |
84984 KB |
Output is correct |
26 |
Correct |
82 ms |
84984 KB |
Output is correct |
27 |
Correct |
95 ms |
84984 KB |
Output is correct |
28 |
Correct |
85 ms |
85104 KB |
Output is correct |
29 |
Correct |
83 ms |
84984 KB |
Output is correct |
30 |
Correct |
86 ms |
85084 KB |
Output is correct |
31 |
Correct |
981 ms |
102012 KB |
Output is correct |
32 |
Correct |
127 ms |
88304 KB |
Output is correct |
33 |
Correct |
840 ms |
101484 KB |
Output is correct |
34 |
Correct |
912 ms |
101372 KB |
Output is correct |
35 |
Correct |
778 ms |
101840 KB |
Output is correct |
36 |
Correct |
812 ms |
101820 KB |
Output is correct |
37 |
Correct |
546 ms |
101344 KB |
Output is correct |
38 |
Correct |
491 ms |
101200 KB |
Output is correct |
39 |
Correct |
393 ms |
101476 KB |
Output is correct |
40 |
Correct |
443 ms |
101332 KB |
Output is correct |
41 |
Correct |
782 ms |
101316 KB |
Output is correct |
42 |
Correct |
1836 ms |
101304 KB |
Output is correct |
43 |
Correct |
156 ms |
89072 KB |
Output is correct |
44 |
Correct |
747 ms |
101488 KB |
Output is correct |
45 |
Correct |
486 ms |
101360 KB |
Output is correct |
46 |
Correct |
338 ms |
101260 KB |
Output is correct |
47 |
Correct |
279 ms |
100588 KB |
Output is correct |
48 |
Correct |
281 ms |
100336 KB |
Output is correct |
49 |
Correct |
340 ms |
100720 KB |
Output is correct |
50 |
Correct |
1022 ms |
101084 KB |
Output is correct |
51 |
Correct |
308 ms |
100860 KB |
Output is correct |
52 |
Execution timed out |
5085 ms |
112828 KB |
Time limit exceeded |
53 |
Halted |
0 ms |
0 KB |
- |