/*
We will use sweep line to solve the problem. We split the stores into 2 queries:
1) Add store i at time a[i]
2) Remove store i at time b[i] + 1
We will also have queries in the sweep line. Everything will be sorted by time in increasing order.
Now to handle queries we will maintain K sets - the available positions of j-type stores. Then if A and B are two consecutive stores, the closest elements to all positions in [A; B] are A or B.
Then let's have a two segment trees wtih sets - one for closest elements to the left and one for closest elements to the right. Now addition of store with type X will be done like that:
1) Let A <= X <= B and A and B are the closest stores of the same type.
2) We remove the interval [A; B] from the DS.
3) We add the intervals [A; X] and [X; B].
Adding or removing an interval is done by finding the middle position and then concidering the two ranges - [L; Mid] and [Mid + 1; R].
The complexity will be O(N * log N * log N).
As sets are slow, we will compress the input in each segment tree node beforehand and then use priority queue instead of sets.
Unfortunately the above data structure was too slow. So my second idea is to change the data structure to two simple treaps and do binary search on them.
The complexity will be O(N log N) this way.
*/
#include <bits/stdc++.h>
#define endl '\n'
//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")
#define SZ(x) ((int)x.size())
#define ALL(V) V.begin(), V.end()
#define L_B lower_bound
#define U_B upper_bound
using namespace std;
template<class T, class T2> inline int chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; }
template<class T, class T2> inline int chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; }
const int MAXN = (1 << 20);
const int inf = (int)1e9 + 42;
random_device rd;
mt19937 mt(rd());
struct treap_l
{
struct node
{
pair<int, int> v;
int mx, sz;
int prior;
node *l, *r;
node() { v = {0, 0}; prior = mx = sz = 0; l = r = nullptr; }
node(int L, int R)
{
v = {L, R};
mx = R;
prior = mt();
l = r = nullptr;
sz = 1;
}
};
using pnode = node*;
int size(pnode t) { return t ? t->sz : 0; }
void pull(pnode &t)
{
if(!t) return;
t->mx = t->v.second;
if(t->l) chkmax(t->mx, t->l->mx);
if(t->r) chkmax(t->mx, t->r->mx);
t->sz = size(t->l) + 1 + size(t->r);
}
void merge(pnode &t, pnode l, pnode r)
{
if(!l) { t = r; return; }
if(!r) { t = l; return; }
if(l->prior > r->prior)
merge(l->r, l->r, r), t = l;
else
merge(r->l, l, r->l), t = r;
pull(t);
}
void split(pnode t, pnode &l, pnode &r, pair<int, int> k)
{
if(!t) { l = r = nullptr; return; }
if(t->v <= k)
split(t->r, t->r, r, k), l = t;
else
split(t->l, l, t->l, k), r = t;
pull(t);
}
void split_sz(pnode t, pnode &l, pnode &r, int k, int add = 0)
{
if(!t) { l = r = nullptr; return; }
int idx = add + size(t->l);
if(idx <= k)
split_sz(t->r, t->r, r, k, idx + 1), l = t;
else
split_sz(t->l, l, t->l, k, add), r = t;
pull(t);
}
pnode root;
treap_l() { root = nullptr; }
void add_interval(int l, int r)
{
pnode R = nullptr, k = new node(l, r);
split(root, root, R, {l, r});
merge(root, root, k);
merge(root, root, R);
}
void rem_interval(int l, int r)
{
pnode L, R, dummy;
split(root, L, R, make_pair(l, r - 1));
split_sz(R, dummy, R, 0);
merge(root, L, R);
}
int query(pnode t, int x)
{
if(!t) return 0;
int ret = 0;
if(t->v.second >= x)
chkmax(ret, x - t->v.first);
if(t->l && t->l->mx >= x)
chkmax(ret, query(t->l, x));
else
chkmax(ret, query(t->r, x));
return ret;
}
int query(int x)
{
int ret;
pnode l, r;
split(root, l, r, {x + 1, -(int)2e9});
ret = query(l, x);
merge(root, l, r);
return ret;
}
} L;
struct treap_r
{
struct node
{
pair<int, int> v;
int mn, sz;
int prior;
node *l, *r;
node() { v = {0, 0}; prior = mn = sz = 0; l = r = nullptr; }
node(int R, int L)
{
v = {R, L};
mn = L;
prior = mt();
l = r = nullptr;
sz = 1;
}
};
using pnode = node*;
int size(pnode t) { return t ? t->sz : 0; }
void pull(pnode &t)
{
if(!t) return;
t->mn = t->v.second;
if(t->l) chkmin(t->mn, t->l->mn);
if(t->r) chkmin(t->mn, t->r->mn);
t->sz = size(t->l) + 1 + size(t->r);
}
void merge(pnode &t, pnode l, pnode r)
{
if(!l) { t = r; return; }
if(!r) { t = l; return; }
if(l->prior > r->prior)
merge(l->r, l->r, r), t = l;
else
merge(r->l, l, r->l), t = r;
pull(t);
}
void split(pnode t, pnode &l, pnode &r, pair<int, int> k)
{
if(!t) { l = r = nullptr; return; }
if(t->v <= k)
split(t->r, t->r, r, k), l = t;
else
split(t->l, l, t->l, k), r = t;
pull(t);
}
void split_sz(pnode t, pnode &l, pnode &r, int k, int add = 0)
{
if(!t) { l = r = nullptr; return; }
int idx = add + size(t->l);
if(idx <= k)
split_sz(t->r, t->r, r, k, idx + 1), l = t;
else
split_sz(t->l, l, t->l, k, add), r = t;
pull(t);
}
pnode root;
treap_r() { root = nullptr; }
void add_interval(int l, int r)
{
pnode R, k = new node(r, l);
split(root, root, R, {r, l});
merge(root, root, k);
merge(root, root, R);
}
void rem_interval(int l, int r)
{
pnode L, R, dummy;
split(root, L, R, make_pair(r, l - 1));
split_sz(R, dummy, R, 0);
merge(root, L, R);
}
int query(pnode t, int x)
{
if(!t) return 0;
int ret = 0;
if(t->v.second <= x)
chkmax(ret, t->v.first - x);
if(t->r && t->r->mn <= x)
chkmax(ret, query(t->r, x));
else
chkmax(ret, query(t->l, x));
return ret;
}
int query(int x)
{
int ret;
pnode l, r;
split(root, l, r, {x - 1, (int)2e9});
ret = query(r, x);
merge(root, l, r);
return ret;
}
} R;
int n, k, q;
struct event
{
int type;
int T, x, tp, idx;
event() { type = tp = T = x = 0; idx = -1; }
event(int t, int Tm, int X, int i, int pp = -1)
{
type = t;
T = Tm;
x = X;
idx = i;
tp = pp;
}
};
bool cmp(event a, event b)
{
if(a.T != b.T) return a.T < b.T;
return a.type < b.type;
}
vector<event> Ev;
int answer[MAXN];
void read()
{
cin >> n >> k >> q;
for(int i = 0; i < n; i++)
{
int x, t, a, b;
cin >> x >> t >> a >> b;
Ev.push_back(event(0, a, x, i, t));
Ev.push_back(event(1, b + 1, x, i, t));
}
for(int i = 0; i < q; i++)
{
int x, t;
cin >> x >> t;
Ev.push_back(event(2, t, x, i));
}
}
set<pair<int, int> > ST[MAXN];
void add_interval(int l, int r)
{
int mid = (l + r) / 2;
if(l <= mid) L.add_interval(l, mid);
if(mid + 1 <= r) R.add_interval(mid + 1, r);
}
void rem_interval(int l, int r)
{
int mid = (l + r) / 2;
if(l <= mid) L.rem_interval(l, mid);
if(mid + 1 <= r) R.rem_interval(mid + 1, r);
}
int query(int x) { return max(L.query(x), R.query(x)); }
void add(int y, int x, int i)
{
auto aft = ST[y].L_B({x, i});
auto bef = prev(aft);
ST[y].insert({x, i});
rem_interval(bef->first, aft->first);
add_interval(bef->first, x);
add_interval(x, aft->first);
}
void rem(int y, int x, int i)
{
ST[y].erase({x, i});
auto aft = ST[y].L_B({x, i});
auto bef = prev(aft);
rem_interval(bef->first, x);
rem_interval(x, aft->first);
add_interval(bef->first, aft->first);
}
void solve()
{
for(int i = 1; i <= k; i++)
{
ST[i].insert({-inf, -1}), ST[i].insert({inf, -1});
add_interval(-inf, inf);
}
sort(ALL(Ev), cmp);
for(auto it: Ev)
if(it.type == 0)
add(it.tp, it.x, it.idx);
else if(it.type == 1)
rem(it.tp, it.x, it.idx);
else
answer[it.idx] = query(it.x);
for(int i = 0; i < q; i++)
if(answer[i] < (int)2e8) cout << answer[i] << endl;
else cout << -1 << endl;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
read();
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
49528 KB |
Output is correct |
2 |
Correct |
47 ms |
49656 KB |
Output is correct |
3 |
Correct |
46 ms |
49656 KB |
Output is correct |
4 |
Correct |
47 ms |
49704 KB |
Output is correct |
5 |
Correct |
51 ms |
49768 KB |
Output is correct |
6 |
Correct |
51 ms |
50032 KB |
Output is correct |
7 |
Correct |
49 ms |
50044 KB |
Output is correct |
8 |
Correct |
52 ms |
50252 KB |
Output is correct |
9 |
Correct |
50 ms |
50252 KB |
Output is correct |
10 |
Correct |
51 ms |
50252 KB |
Output is correct |
11 |
Correct |
50 ms |
50264 KB |
Output is correct |
12 |
Correct |
49 ms |
50304 KB |
Output is correct |
13 |
Correct |
48 ms |
50304 KB |
Output is correct |
14 |
Correct |
48 ms |
50372 KB |
Output is correct |
15 |
Correct |
51 ms |
50372 KB |
Output is correct |
16 |
Correct |
50 ms |
50420 KB |
Output is correct |
17 |
Correct |
49 ms |
50464 KB |
Output is correct |
18 |
Correct |
51 ms |
50464 KB |
Output is correct |
19 |
Correct |
61 ms |
50588 KB |
Output is correct |
20 |
Correct |
49 ms |
50588 KB |
Output is correct |
21 |
Correct |
49 ms |
50588 KB |
Output is correct |
22 |
Correct |
49 ms |
50588 KB |
Output is correct |
23 |
Correct |
50 ms |
50588 KB |
Output is correct |
24 |
Correct |
49 ms |
50588 KB |
Output is correct |
25 |
Correct |
49 ms |
50716 KB |
Output is correct |
26 |
Correct |
48 ms |
50716 KB |
Output is correct |
27 |
Correct |
50 ms |
50744 KB |
Output is correct |
28 |
Correct |
49 ms |
50744 KB |
Output is correct |
29 |
Correct |
50 ms |
50744 KB |
Output is correct |
30 |
Correct |
47 ms |
50744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
49528 KB |
Output is correct |
2 |
Correct |
47 ms |
49656 KB |
Output is correct |
3 |
Correct |
46 ms |
49656 KB |
Output is correct |
4 |
Correct |
47 ms |
49704 KB |
Output is correct |
5 |
Correct |
51 ms |
49768 KB |
Output is correct |
6 |
Correct |
51 ms |
50032 KB |
Output is correct |
7 |
Correct |
49 ms |
50044 KB |
Output is correct |
8 |
Correct |
52 ms |
50252 KB |
Output is correct |
9 |
Correct |
50 ms |
50252 KB |
Output is correct |
10 |
Correct |
51 ms |
50252 KB |
Output is correct |
11 |
Correct |
50 ms |
50264 KB |
Output is correct |
12 |
Correct |
49 ms |
50304 KB |
Output is correct |
13 |
Correct |
48 ms |
50304 KB |
Output is correct |
14 |
Correct |
48 ms |
50372 KB |
Output is correct |
15 |
Correct |
51 ms |
50372 KB |
Output is correct |
16 |
Correct |
50 ms |
50420 KB |
Output is correct |
17 |
Correct |
49 ms |
50464 KB |
Output is correct |
18 |
Correct |
51 ms |
50464 KB |
Output is correct |
19 |
Correct |
61 ms |
50588 KB |
Output is correct |
20 |
Correct |
49 ms |
50588 KB |
Output is correct |
21 |
Correct |
49 ms |
50588 KB |
Output is correct |
22 |
Correct |
49 ms |
50588 KB |
Output is correct |
23 |
Correct |
50 ms |
50588 KB |
Output is correct |
24 |
Correct |
49 ms |
50588 KB |
Output is correct |
25 |
Correct |
49 ms |
50716 KB |
Output is correct |
26 |
Correct |
48 ms |
50716 KB |
Output is correct |
27 |
Correct |
50 ms |
50744 KB |
Output is correct |
28 |
Correct |
49 ms |
50744 KB |
Output is correct |
29 |
Correct |
50 ms |
50744 KB |
Output is correct |
30 |
Correct |
47 ms |
50744 KB |
Output is correct |
31 |
Correct |
1211 ms |
74708 KB |
Output is correct |
32 |
Correct |
411 ms |
74708 KB |
Output is correct |
33 |
Correct |
933 ms |
78448 KB |
Output is correct |
34 |
Correct |
1080 ms |
81536 KB |
Output is correct |
35 |
Correct |
1198 ms |
84200 KB |
Output is correct |
36 |
Correct |
1097 ms |
87104 KB |
Output is correct |
37 |
Correct |
775 ms |
90200 KB |
Output is correct |
38 |
Correct |
700 ms |
92848 KB |
Output is correct |
39 |
Correct |
565 ms |
95608 KB |
Output is correct |
40 |
Correct |
556 ms |
98428 KB |
Output is correct |
41 |
Correct |
613 ms |
101500 KB |
Output is correct |
42 |
Correct |
668 ms |
104408 KB |
Output is correct |
43 |
Correct |
313 ms |
104408 KB |
Output is correct |
44 |
Correct |
616 ms |
109956 KB |
Output is correct |
45 |
Correct |
517 ms |
112636 KB |
Output is correct |
46 |
Correct |
381 ms |
115480 KB |
Output is correct |
47 |
Correct |
369 ms |
117264 KB |
Output is correct |
48 |
Correct |
354 ms |
119796 KB |
Output is correct |
49 |
Correct |
430 ms |
123080 KB |
Output is correct |
50 |
Correct |
573 ms |
126652 KB |
Output is correct |
51 |
Correct |
392 ms |
128856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5013 ms |
209156 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5105 ms |
209156 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
49528 KB |
Output is correct |
2 |
Correct |
47 ms |
49656 KB |
Output is correct |
3 |
Correct |
46 ms |
49656 KB |
Output is correct |
4 |
Correct |
47 ms |
49704 KB |
Output is correct |
5 |
Correct |
51 ms |
49768 KB |
Output is correct |
6 |
Correct |
51 ms |
50032 KB |
Output is correct |
7 |
Correct |
49 ms |
50044 KB |
Output is correct |
8 |
Correct |
52 ms |
50252 KB |
Output is correct |
9 |
Correct |
50 ms |
50252 KB |
Output is correct |
10 |
Correct |
51 ms |
50252 KB |
Output is correct |
11 |
Correct |
50 ms |
50264 KB |
Output is correct |
12 |
Correct |
49 ms |
50304 KB |
Output is correct |
13 |
Correct |
48 ms |
50304 KB |
Output is correct |
14 |
Correct |
48 ms |
50372 KB |
Output is correct |
15 |
Correct |
51 ms |
50372 KB |
Output is correct |
16 |
Correct |
50 ms |
50420 KB |
Output is correct |
17 |
Correct |
49 ms |
50464 KB |
Output is correct |
18 |
Correct |
51 ms |
50464 KB |
Output is correct |
19 |
Correct |
61 ms |
50588 KB |
Output is correct |
20 |
Correct |
49 ms |
50588 KB |
Output is correct |
21 |
Correct |
49 ms |
50588 KB |
Output is correct |
22 |
Correct |
49 ms |
50588 KB |
Output is correct |
23 |
Correct |
50 ms |
50588 KB |
Output is correct |
24 |
Correct |
49 ms |
50588 KB |
Output is correct |
25 |
Correct |
49 ms |
50716 KB |
Output is correct |
26 |
Correct |
48 ms |
50716 KB |
Output is correct |
27 |
Correct |
50 ms |
50744 KB |
Output is correct |
28 |
Correct |
49 ms |
50744 KB |
Output is correct |
29 |
Correct |
50 ms |
50744 KB |
Output is correct |
30 |
Correct |
47 ms |
50744 KB |
Output is correct |
31 |
Correct |
1211 ms |
74708 KB |
Output is correct |
32 |
Correct |
411 ms |
74708 KB |
Output is correct |
33 |
Correct |
933 ms |
78448 KB |
Output is correct |
34 |
Correct |
1080 ms |
81536 KB |
Output is correct |
35 |
Correct |
1198 ms |
84200 KB |
Output is correct |
36 |
Correct |
1097 ms |
87104 KB |
Output is correct |
37 |
Correct |
775 ms |
90200 KB |
Output is correct |
38 |
Correct |
700 ms |
92848 KB |
Output is correct |
39 |
Correct |
565 ms |
95608 KB |
Output is correct |
40 |
Correct |
556 ms |
98428 KB |
Output is correct |
41 |
Correct |
613 ms |
101500 KB |
Output is correct |
42 |
Correct |
668 ms |
104408 KB |
Output is correct |
43 |
Correct |
313 ms |
104408 KB |
Output is correct |
44 |
Correct |
616 ms |
109956 KB |
Output is correct |
45 |
Correct |
517 ms |
112636 KB |
Output is correct |
46 |
Correct |
381 ms |
115480 KB |
Output is correct |
47 |
Correct |
369 ms |
117264 KB |
Output is correct |
48 |
Correct |
354 ms |
119796 KB |
Output is correct |
49 |
Correct |
430 ms |
123080 KB |
Output is correct |
50 |
Correct |
573 ms |
126652 KB |
Output is correct |
51 |
Correct |
392 ms |
128856 KB |
Output is correct |
52 |
Correct |
1203 ms |
209156 KB |
Output is correct |
53 |
Correct |
1083 ms |
209156 KB |
Output is correct |
54 |
Correct |
1350 ms |
209156 KB |
Output is correct |
55 |
Correct |
879 ms |
209156 KB |
Output is correct |
56 |
Correct |
931 ms |
209156 KB |
Output is correct |
57 |
Correct |
755 ms |
209156 KB |
Output is correct |
58 |
Correct |
952 ms |
209156 KB |
Output is correct |
59 |
Correct |
1012 ms |
209156 KB |
Output is correct |
60 |
Correct |
788 ms |
209156 KB |
Output is correct |
61 |
Correct |
551 ms |
209156 KB |
Output is correct |
62 |
Correct |
1170 ms |
209156 KB |
Output is correct |
63 |
Correct |
1280 ms |
209156 KB |
Output is correct |
64 |
Correct |
1240 ms |
209156 KB |
Output is correct |
65 |
Correct |
1135 ms |
209156 KB |
Output is correct |
66 |
Correct |
684 ms |
209156 KB |
Output is correct |
67 |
Correct |
1002 ms |
209156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
49528 KB |
Output is correct |
2 |
Correct |
47 ms |
49656 KB |
Output is correct |
3 |
Correct |
46 ms |
49656 KB |
Output is correct |
4 |
Correct |
47 ms |
49704 KB |
Output is correct |
5 |
Correct |
51 ms |
49768 KB |
Output is correct |
6 |
Correct |
51 ms |
50032 KB |
Output is correct |
7 |
Correct |
49 ms |
50044 KB |
Output is correct |
8 |
Correct |
52 ms |
50252 KB |
Output is correct |
9 |
Correct |
50 ms |
50252 KB |
Output is correct |
10 |
Correct |
51 ms |
50252 KB |
Output is correct |
11 |
Correct |
50 ms |
50264 KB |
Output is correct |
12 |
Correct |
49 ms |
50304 KB |
Output is correct |
13 |
Correct |
48 ms |
50304 KB |
Output is correct |
14 |
Correct |
48 ms |
50372 KB |
Output is correct |
15 |
Correct |
51 ms |
50372 KB |
Output is correct |
16 |
Correct |
50 ms |
50420 KB |
Output is correct |
17 |
Correct |
49 ms |
50464 KB |
Output is correct |
18 |
Correct |
51 ms |
50464 KB |
Output is correct |
19 |
Correct |
61 ms |
50588 KB |
Output is correct |
20 |
Correct |
49 ms |
50588 KB |
Output is correct |
21 |
Correct |
49 ms |
50588 KB |
Output is correct |
22 |
Correct |
49 ms |
50588 KB |
Output is correct |
23 |
Correct |
50 ms |
50588 KB |
Output is correct |
24 |
Correct |
49 ms |
50588 KB |
Output is correct |
25 |
Correct |
49 ms |
50716 KB |
Output is correct |
26 |
Correct |
48 ms |
50716 KB |
Output is correct |
27 |
Correct |
50 ms |
50744 KB |
Output is correct |
28 |
Correct |
49 ms |
50744 KB |
Output is correct |
29 |
Correct |
50 ms |
50744 KB |
Output is correct |
30 |
Correct |
47 ms |
50744 KB |
Output is correct |
31 |
Correct |
1211 ms |
74708 KB |
Output is correct |
32 |
Correct |
411 ms |
74708 KB |
Output is correct |
33 |
Correct |
933 ms |
78448 KB |
Output is correct |
34 |
Correct |
1080 ms |
81536 KB |
Output is correct |
35 |
Correct |
1198 ms |
84200 KB |
Output is correct |
36 |
Correct |
1097 ms |
87104 KB |
Output is correct |
37 |
Correct |
775 ms |
90200 KB |
Output is correct |
38 |
Correct |
700 ms |
92848 KB |
Output is correct |
39 |
Correct |
565 ms |
95608 KB |
Output is correct |
40 |
Correct |
556 ms |
98428 KB |
Output is correct |
41 |
Correct |
613 ms |
101500 KB |
Output is correct |
42 |
Correct |
668 ms |
104408 KB |
Output is correct |
43 |
Correct |
313 ms |
104408 KB |
Output is correct |
44 |
Correct |
616 ms |
109956 KB |
Output is correct |
45 |
Correct |
517 ms |
112636 KB |
Output is correct |
46 |
Correct |
381 ms |
115480 KB |
Output is correct |
47 |
Correct |
369 ms |
117264 KB |
Output is correct |
48 |
Correct |
354 ms |
119796 KB |
Output is correct |
49 |
Correct |
430 ms |
123080 KB |
Output is correct |
50 |
Correct |
573 ms |
126652 KB |
Output is correct |
51 |
Correct |
392 ms |
128856 KB |
Output is correct |
52 |
Execution timed out |
5013 ms |
209156 KB |
Time limit exceeded |
53 |
Halted |
0 ms |
0 KB |
- |