#include <bits/stdc++.h>
#define Loop(x,l,r) for (ll x = (l); x < (ll)(r); ++x)
#define LoopR(x,l,r) for (ll x = (r)-1; x >= (ll)(l); --x)
typedef long long ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll , ll > pll;
using namespace std;
#define int ll
#define pii pll
enum EventType {Rem, Add, Query};
struct Event {
int time;
EventType type;
int store;
int pos;
int query_idx;
};
bool operator<(const Event &a, const Event &b) {
return a.time < b.time || a.time == b.time && a.type < b.type;
}
vector<Event> evs;
vector<set<pii>> type_list;
struct Seg {
int *t;
int begin, end;
void init(int b, int e) {
t = new int[4 * (e-b)];
fill(t, t + 4*(e-b), INT_MIN);
begin = b;
end = e;
}
void up_impl(int p, int x, int i, int b, int e) {
if (e-b==1) {
t[i] = x;
return;
}
if (p < (b+e)/2)
up_impl(p, x, 2*i+1, b, (b+e)/2);
else
up_impl(p, x, 2*i+2, (b+e)/2, e);
t[i] = max(t[2*i+1], t[2*i+2]);
}
int get_impl(int l, int r, int i, int b, int e) {
if (l <= b && e <= r)
return t[i];
if (r <= b || e <= l)
return INT_MIN;
return max(get_impl(l, r, 2*i+1, b, (b+e)/2),
get_impl(l, r, 2*i+2, (b+e)/2, e));
}
void up(int p, int x) { return up_impl(p, x, 0, begin, end); }
int get(int l, int r) { return get_impl(l, r, 0, begin, end); }
} next_right;
int cnt_types, cnt_stores, cnt_queries;
vector<int> store_poses;
struct Store {
int type;
int pos;
int b, e;
};
bool operator<(const Store &a, const Store &b) { return a.pos < b.pos; }
vector<Store> stores;
vector<int> ans;
void handle_add(Event ev)
{
Store s = stores[ev.store];
auto &list = type_list[s.type];
auto it = list.upper_bound({s.pos, ev.store});
int p = it == list.end()? INT_MAX: it->first;
--it;
next_right.up(it->second, s.pos);
next_right.up(ev.store, p);
list.insert({s.pos, ev.store});
}
void handle_rem(Event ev)
{
Store s = stores[ev.store];
auto &list = type_list[s.type];
auto it = list.upper_bound({s.pos, ev.store});
int p = it == list.end()? INT_MAX: it->first;
--it; --it;
next_right.up(it->second, p);
next_right.up(ev.store, INT_MIN);
list.erase({s.pos, ev.store});
}
void handle_query(Event ev)
{
if (next_right.get(-cnt_types, 0) == INT_MAX) {
ans[ev.query_idx] = -1;
return;
}
int l = 0, r = store_poses.back();
auto get = [&](int e) {
e = lower_bound(store_poses.begin(), store_poses.end(), e) - store_poses.begin();
return next_right.get(-cnt_stores, e);
};
while (l < r) {
int m = (l + r)/2;
int b = ev.pos - m, e = ev.pos + m + 1;
if (get(b) < e)
r = m;
else
l = m+1;
}
ans[ev.query_idx] = l;
}
signed main()
{
cin.tie(0) -> sync_with_stdio(false);
cin >> cnt_stores >> cnt_types >> cnt_queries;
Loop (i,0,cnt_stores) {
Store s;
cin >> s.pos >> s.type >> s.b >> s.e;
--s.pos; --s.b;
stores.push_back(s);
store_poses.push_back(s.pos);
}
sort(stores.begin(), stores.end());
sort(store_poses.begin(), store_poses.end());
Loop (i,0,cnt_stores) {
evs.push_back({
.time = stores[i].b,
.type = Add,
.store = i,
.pos = stores[i].pos,
.query_idx = -1,
});
evs.push_back({
.time = stores[i].e,
.type = Rem,
.store = i,
.pos = stores[i].pos,
.query_idx = -1,
});
}
Loop (i,0,cnt_queries) {
int l, y;
cin >> l >> y;
--l; --y;
evs.push_back({
.time = y,
.type = Query,
.store = -1,
.pos = l,
.query_idx = i,
});
}
sort(evs.begin(), evs.end());
ans.resize(cnt_queries);
type_list.resize(cnt_types+1);
next_right.init(-cnt_types, cnt_stores);
Loop (i,-cnt_types,0) {
type_list[-i].insert({i, i});
next_right.up(i, INT_MAX);
}
for (Event ev : evs) {
switch (ev.type) {
case Add: handle_add(ev); break;
case Rem: handle_rem(ev); break;
case Query: handle_query(ev); break;
}
}
Loop (i,0,cnt_queries)
cout << ans[i] << '\n';
}
Compilation message
new_home.cpp: In function 'bool operator<(const Event&, const Event&)':
new_home.cpp:23:45: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
23 | return a.time < b.time || a.time == b.time && a.type < b.type;
| ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
2 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
2 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
368 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
2 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
2 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
368 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
422 ms |
16268 KB |
Output is correct |
32 |
Correct |
166 ms |
14360 KB |
Output is correct |
33 |
Correct |
424 ms |
14324 KB |
Output is correct |
34 |
Correct |
411 ms |
14300 KB |
Output is correct |
35 |
Correct |
434 ms |
16292 KB |
Output is correct |
36 |
Correct |
427 ms |
16196 KB |
Output is correct |
37 |
Correct |
370 ms |
14324 KB |
Output is correct |
38 |
Correct |
400 ms |
14332 KB |
Output is correct |
39 |
Correct |
337 ms |
14324 KB |
Output is correct |
40 |
Correct |
356 ms |
14352 KB |
Output is correct |
41 |
Correct |
285 ms |
14324 KB |
Output is correct |
42 |
Correct |
225 ms |
14324 KB |
Output is correct |
43 |
Correct |
234 ms |
16408 KB |
Output is correct |
44 |
Correct |
285 ms |
14392 KB |
Output is correct |
45 |
Correct |
315 ms |
14324 KB |
Output is correct |
46 |
Correct |
326 ms |
14348 KB |
Output is correct |
47 |
Correct |
206 ms |
14196 KB |
Output is correct |
48 |
Correct |
243 ms |
14416 KB |
Output is correct |
49 |
Correct |
244 ms |
14316 KB |
Output is correct |
50 |
Correct |
219 ms |
14412 KB |
Output is correct |
51 |
Correct |
278 ms |
14348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1709 ms |
88924 KB |
Output is correct |
2 |
Correct |
2439 ms |
79236 KB |
Output is correct |
3 |
Correct |
1303 ms |
122812 KB |
Output is correct |
4 |
Correct |
1451 ms |
94616 KB |
Output is correct |
5 |
Correct |
2440 ms |
78864 KB |
Output is correct |
6 |
Correct |
2481 ms |
79256 KB |
Output is correct |
7 |
Correct |
1345 ms |
122812 KB |
Output is correct |
8 |
Correct |
1844 ms |
94640 KB |
Output is correct |
9 |
Correct |
2015 ms |
84756 KB |
Output is correct |
10 |
Correct |
2471 ms |
79924 KB |
Output is correct |
11 |
Correct |
1994 ms |
78160 KB |
Output is correct |
12 |
Correct |
2079 ms |
79512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2234 ms |
80964 KB |
Output is correct |
2 |
Correct |
915 ms |
78344 KB |
Output is correct |
3 |
Correct |
3042 ms |
79548 KB |
Output is correct |
4 |
Correct |
927 ms |
121044 KB |
Output is correct |
5 |
Correct |
952 ms |
87128 KB |
Output is correct |
6 |
Correct |
982 ms |
92744 KB |
Output is correct |
7 |
Correct |
2866 ms |
78980 KB |
Output is correct |
8 |
Correct |
2896 ms |
79376 KB |
Output is correct |
9 |
Correct |
1248 ms |
122156 KB |
Output is correct |
10 |
Correct |
1740 ms |
90568 KB |
Output is correct |
11 |
Correct |
2169 ms |
82504 KB |
Output is correct |
12 |
Correct |
3135 ms |
79924 KB |
Output is correct |
13 |
Correct |
1886 ms |
77112 KB |
Output is correct |
14 |
Correct |
1848 ms |
76200 KB |
Output is correct |
15 |
Correct |
2090 ms |
78164 KB |
Output is correct |
16 |
Correct |
2195 ms |
79400 KB |
Output is correct |
17 |
Correct |
2107 ms |
77732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
2 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
2 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
368 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
422 ms |
16268 KB |
Output is correct |
32 |
Correct |
166 ms |
14360 KB |
Output is correct |
33 |
Correct |
424 ms |
14324 KB |
Output is correct |
34 |
Correct |
411 ms |
14300 KB |
Output is correct |
35 |
Correct |
434 ms |
16292 KB |
Output is correct |
36 |
Correct |
427 ms |
16196 KB |
Output is correct |
37 |
Correct |
370 ms |
14324 KB |
Output is correct |
38 |
Correct |
400 ms |
14332 KB |
Output is correct |
39 |
Correct |
337 ms |
14324 KB |
Output is correct |
40 |
Correct |
356 ms |
14352 KB |
Output is correct |
41 |
Correct |
285 ms |
14324 KB |
Output is correct |
42 |
Correct |
225 ms |
14324 KB |
Output is correct |
43 |
Correct |
234 ms |
16408 KB |
Output is correct |
44 |
Correct |
285 ms |
14392 KB |
Output is correct |
45 |
Correct |
315 ms |
14324 KB |
Output is correct |
46 |
Correct |
326 ms |
14348 KB |
Output is correct |
47 |
Correct |
206 ms |
14196 KB |
Output is correct |
48 |
Correct |
243 ms |
14416 KB |
Output is correct |
49 |
Correct |
244 ms |
14316 KB |
Output is correct |
50 |
Correct |
219 ms |
14412 KB |
Output is correct |
51 |
Correct |
278 ms |
14348 KB |
Output is correct |
52 |
Correct |
158 ms |
24412 KB |
Output is correct |
53 |
Correct |
151 ms |
22072 KB |
Output is correct |
54 |
Correct |
147 ms |
18700 KB |
Output is correct |
55 |
Correct |
311 ms |
16640 KB |
Output is correct |
56 |
Correct |
290 ms |
18732 KB |
Output is correct |
57 |
Correct |
293 ms |
14288 KB |
Output is correct |
58 |
Correct |
228 ms |
16608 KB |
Output is correct |
59 |
Correct |
211 ms |
18528 KB |
Output is correct |
60 |
Correct |
221 ms |
14380 KB |
Output is correct |
61 |
Correct |
117 ms |
24800 KB |
Output is correct |
62 |
Incorrect |
182 ms |
24528 KB |
Output isn't correct |
63 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
2 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
2 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
368 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
422 ms |
16268 KB |
Output is correct |
32 |
Correct |
166 ms |
14360 KB |
Output is correct |
33 |
Correct |
424 ms |
14324 KB |
Output is correct |
34 |
Correct |
411 ms |
14300 KB |
Output is correct |
35 |
Correct |
434 ms |
16292 KB |
Output is correct |
36 |
Correct |
427 ms |
16196 KB |
Output is correct |
37 |
Correct |
370 ms |
14324 KB |
Output is correct |
38 |
Correct |
400 ms |
14332 KB |
Output is correct |
39 |
Correct |
337 ms |
14324 KB |
Output is correct |
40 |
Correct |
356 ms |
14352 KB |
Output is correct |
41 |
Correct |
285 ms |
14324 KB |
Output is correct |
42 |
Correct |
225 ms |
14324 KB |
Output is correct |
43 |
Correct |
234 ms |
16408 KB |
Output is correct |
44 |
Correct |
285 ms |
14392 KB |
Output is correct |
45 |
Correct |
315 ms |
14324 KB |
Output is correct |
46 |
Correct |
326 ms |
14348 KB |
Output is correct |
47 |
Correct |
206 ms |
14196 KB |
Output is correct |
48 |
Correct |
243 ms |
14416 KB |
Output is correct |
49 |
Correct |
244 ms |
14316 KB |
Output is correct |
50 |
Correct |
219 ms |
14412 KB |
Output is correct |
51 |
Correct |
278 ms |
14348 KB |
Output is correct |
52 |
Correct |
1709 ms |
88924 KB |
Output is correct |
53 |
Correct |
2439 ms |
79236 KB |
Output is correct |
54 |
Correct |
1303 ms |
122812 KB |
Output is correct |
55 |
Correct |
1451 ms |
94616 KB |
Output is correct |
56 |
Correct |
2440 ms |
78864 KB |
Output is correct |
57 |
Correct |
2481 ms |
79256 KB |
Output is correct |
58 |
Correct |
1345 ms |
122812 KB |
Output is correct |
59 |
Correct |
1844 ms |
94640 KB |
Output is correct |
60 |
Correct |
2015 ms |
84756 KB |
Output is correct |
61 |
Correct |
2471 ms |
79924 KB |
Output is correct |
62 |
Correct |
1994 ms |
78160 KB |
Output is correct |
63 |
Correct |
2079 ms |
79512 KB |
Output is correct |
64 |
Correct |
2234 ms |
80964 KB |
Output is correct |
65 |
Correct |
915 ms |
78344 KB |
Output is correct |
66 |
Correct |
3042 ms |
79548 KB |
Output is correct |
67 |
Correct |
927 ms |
121044 KB |
Output is correct |
68 |
Correct |
952 ms |
87128 KB |
Output is correct |
69 |
Correct |
982 ms |
92744 KB |
Output is correct |
70 |
Correct |
2866 ms |
78980 KB |
Output is correct |
71 |
Correct |
2896 ms |
79376 KB |
Output is correct |
72 |
Correct |
1248 ms |
122156 KB |
Output is correct |
73 |
Correct |
1740 ms |
90568 KB |
Output is correct |
74 |
Correct |
2169 ms |
82504 KB |
Output is correct |
75 |
Correct |
3135 ms |
79924 KB |
Output is correct |
76 |
Correct |
1886 ms |
77112 KB |
Output is correct |
77 |
Correct |
1848 ms |
76200 KB |
Output is correct |
78 |
Correct |
2090 ms |
78164 KB |
Output is correct |
79 |
Correct |
2195 ms |
79400 KB |
Output is correct |
80 |
Correct |
2107 ms |
77732 KB |
Output is correct |
81 |
Correct |
158 ms |
24412 KB |
Output is correct |
82 |
Correct |
151 ms |
22072 KB |
Output is correct |
83 |
Correct |
147 ms |
18700 KB |
Output is correct |
84 |
Correct |
311 ms |
16640 KB |
Output is correct |
85 |
Correct |
290 ms |
18732 KB |
Output is correct |
86 |
Correct |
293 ms |
14288 KB |
Output is correct |
87 |
Correct |
228 ms |
16608 KB |
Output is correct |
88 |
Correct |
211 ms |
18528 KB |
Output is correct |
89 |
Correct |
221 ms |
14380 KB |
Output is correct |
90 |
Correct |
117 ms |
24800 KB |
Output is correct |
91 |
Incorrect |
182 ms |
24528 KB |
Output isn't correct |
92 |
Halted |
0 ms |
0 KB |
- |