#include <bits/stdc++.h>
#define long long long
using namespace std;
const int N = 3e5+1, INF = 1e9;
int n, k, q, res[N];
vector<tuple<int,int,int,int> > ops;
vector<tuple<int,int,int> > events;
// ops = initial sorting of intervals, events for compression after determining coords and calculation
map<int, int> points[N];
vector<map<int, int> > st;
// for cc
vector<int> vals;
unordered_map<int, int> rev;
// segment tree
vector<int> t;
void build(int n) { t = vector<int>(2*n, INF); }
void update(int i, int v) {
int n = t.size() >> 1;
for(t[i += n] = v; i > 1; i >>= 1) t[i >> 1] = min(t[i], t[i^1]);
}
int query(int l, int r) {
int n = t.size() >> 1;
int ret = INF;
for(l += n, r += n+1; l < r; l >>= 1, r >>= 1) {
if(l & 1) ret = min(t[l++], ret);
if(r & 1) ret = min(t[--r], ret);
}
return ret;
}
/* int query_test(int tl, int tr) { */
/* int ans = INF; */
/* cout << "list:\n"; */
/* for(int i = tl; i <= tr; i++) if(!st[i].empty()) ans = min(st[i].begin()->first, ans), cout << st[i].begin()->first << ' ' << st[i].begin()->second << endl; */
/* cout << endl; */
/* return ans; */
/* } */
inline void add_line(int l, int r) { events.emplace_back(0, l, r); }
inline void rem_line(int l, int r) { events.emplace_back(1, l, r); }
void add(int x, int ti, bool doceil) {
if(points[ti].count(x)) {
++points[ti][x];
return;
}
auto it = points[ti].upper_bound(x);
// init vals
int pre_val, curr_val;
bool set_pre = false, set_curr = false;
if(it != points[ti].begin()) {
set_pre = true;
pre_val = prev(it)->first;
}
if(it != points[ti].end()) {
set_curr = true;
curr_val = it->first;
}
// process
if(set_pre) {
if(it != points[ti].end()) rem_line(pre_val, (pre_val + curr_val - doceil) / 2);
add_line(pre_val, (pre_val + x - doceil) / 2);
}
if(it != points[ti].end()) add_line(x, (curr_val + x - doceil) / 2);
++points[ti][x];
// cout << endl;
}
void rem(int x, int ti, bool doceil) {
if(points[ti].count(x) && points[ti][x] > 1) {
--points[ti][x];
return;
}
auto it = points[ti].find(x);
// init values
int pre_val, nxt_val;
bool set_pre = false, set_nxt = false;
if(it != points[ti].begin()) {
set_pre = true;
pre_val = prev(it)->first;
}
if(next(it) != points[ti].end()) {
set_nxt = true;
nxt_val = next(it)->first;
}
// process
if(set_pre) {
if(set_nxt) add_line(pre_val, (pre_val + nxt_val - doceil) / 2);
rem_line(pre_val, (pre_val + x - doceil) / 2);
}
if(set_nxt) rem_line(x, (nxt_val + x - doceil) / 2);
points[ti].erase(x);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k >> q;
for(int i = 0, x, t, a, b; i < n; i++) {
cin >> x >> t >> a >> b;
ops.emplace_back(a, 0, x, t);
ops.emplace_back(b+1, 1, x, t);
}
for(int i = 0, l, y; i < q; i++) {
cin >> l >> y;
ops.emplace_back(y, 2, l, i);
}
sort(ops.begin(), ops.end());
for(int i = 1; i <= k; i++) {
points[i][-INF] = 1;
points[i][INF] = 1;
}
for(auto &tt: ops) {
int inst, x, ti;
tie(ignore, inst, x, ti) = tt;
if(inst == 0) add(x, ti, 0);
else if(inst == 1) rem(x, ti, 0);
else events.emplace_back(2, x, ti);
}
for(auto &tt: ops) {
int inst, x, ti;
tie(ignore, inst, x, ti) = tt;
if(inst == 0) add(-x, ti, 1);
else if(inst == 1) rem(-x, ti, 1);
else events.emplace_back(2, -x, ti);
}
vals.reserve(2 * events.size() + 10);
for(auto &tt: events) {
int inst, l, r;
tie(inst, l, r) = tt;
if(inst != 2) vals.push_back(r);
vals.push_back(l);
}
vals.push_back(-INF);
vals.push_back(0);
vals.push_back(INF);
sort(vals.begin(), vals.end());
vals.resize(unique(vals.begin(), vals.end()) - vals.begin());
for(int i = 0; i < vals.size(); i++) rev[vals[i]] = i;
st.resize(vals.size());
build(vals.size());
update(vals.size()-1, 0);
update(rev[0], -INF);
st[vals.size()-1][0] = k; // INF must be even
st[rev[0]][-INF] = k;
for(auto &tt: events) {
int inst, l, r;
tie(inst, l, r) = tt;
// cout << inst << ' ' << l << ' ' << r << endl;
if(inst == 0) {
int rc = rev[r];
++st[rc][l];
update(rc, st[rc].begin()->first);
} else if(inst == 1) {
int rc = rev[r];
--st[rc][l];
if(!st[rc][l]) st[rc].erase(l);
if(!st[rc].empty()) update(rc, st[rc].begin()->first);
else update(rc, INF);
} else {
int x = l, i = r;
int ret = query(rev[x], vals.size()-2);
// cout << endl;
res[i] = max(x-ret, res[i]);
}
}
for(int i = 0; i < q; i++) cout << (res[i] >= INF/2 ? -1 : res[i]) << '\n';
}
Compilation message
new_home.cpp: In function 'void add(int, int, bool)':
new_home.cpp:50:24: warning: variable 'set_curr' set but not used [-Wunused-but-set-variable]
bool set_pre = false, set_curr = false;
^~~~~~~~
new_home.cpp: In function 'int main()':
new_home.cpp:138:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < vals.size(); i++) rev[vals[i]] = i;
~~^~~~~~~~~~~~~
new_home.cpp: In function 'void add(int, int, bool)':
new_home.cpp:64:51: warning: 'curr_val' may be used uninitialized in this function [-Wmaybe-uninitialized]
if(it != points[ti].end()) add_line(x, (curr_val + x - doceil) / 2);
~~~~~~~~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
14456 KB |
Output is correct |
2 |
Correct |
15 ms |
14564 KB |
Output is correct |
3 |
Correct |
19 ms |
14604 KB |
Output is correct |
4 |
Correct |
17 ms |
14696 KB |
Output is correct |
5 |
Correct |
17 ms |
14728 KB |
Output is correct |
6 |
Correct |
18 ms |
15148 KB |
Output is correct |
7 |
Correct |
25 ms |
15168 KB |
Output is correct |
8 |
Correct |
18 ms |
15188 KB |
Output is correct |
9 |
Correct |
23 ms |
15256 KB |
Output is correct |
10 |
Correct |
19 ms |
15320 KB |
Output is correct |
11 |
Correct |
22 ms |
15320 KB |
Output is correct |
12 |
Correct |
23 ms |
15320 KB |
Output is correct |
13 |
Correct |
20 ms |
15332 KB |
Output is correct |
14 |
Correct |
22 ms |
15368 KB |
Output is correct |
15 |
Correct |
21 ms |
15372 KB |
Output is correct |
16 |
Correct |
21 ms |
15392 KB |
Output is correct |
17 |
Correct |
19 ms |
15412 KB |
Output is correct |
18 |
Correct |
21 ms |
15432 KB |
Output is correct |
19 |
Correct |
19 ms |
15492 KB |
Output is correct |
20 |
Correct |
18 ms |
15492 KB |
Output is correct |
21 |
Correct |
18 ms |
15492 KB |
Output is correct |
22 |
Correct |
26 ms |
15528 KB |
Output is correct |
23 |
Correct |
22 ms |
15608 KB |
Output is correct |
24 |
Correct |
22 ms |
15608 KB |
Output is correct |
25 |
Correct |
20 ms |
15612 KB |
Output is correct |
26 |
Correct |
19 ms |
15632 KB |
Output is correct |
27 |
Correct |
21 ms |
15632 KB |
Output is correct |
28 |
Correct |
18 ms |
15664 KB |
Output is correct |
29 |
Correct |
20 ms |
15700 KB |
Output is correct |
30 |
Correct |
22 ms |
15700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
14456 KB |
Output is correct |
2 |
Correct |
15 ms |
14564 KB |
Output is correct |
3 |
Correct |
19 ms |
14604 KB |
Output is correct |
4 |
Correct |
17 ms |
14696 KB |
Output is correct |
5 |
Correct |
17 ms |
14728 KB |
Output is correct |
6 |
Correct |
18 ms |
15148 KB |
Output is correct |
7 |
Correct |
25 ms |
15168 KB |
Output is correct |
8 |
Correct |
18 ms |
15188 KB |
Output is correct |
9 |
Correct |
23 ms |
15256 KB |
Output is correct |
10 |
Correct |
19 ms |
15320 KB |
Output is correct |
11 |
Correct |
22 ms |
15320 KB |
Output is correct |
12 |
Correct |
23 ms |
15320 KB |
Output is correct |
13 |
Correct |
20 ms |
15332 KB |
Output is correct |
14 |
Correct |
22 ms |
15368 KB |
Output is correct |
15 |
Correct |
21 ms |
15372 KB |
Output is correct |
16 |
Correct |
21 ms |
15392 KB |
Output is correct |
17 |
Correct |
19 ms |
15412 KB |
Output is correct |
18 |
Correct |
21 ms |
15432 KB |
Output is correct |
19 |
Correct |
19 ms |
15492 KB |
Output is correct |
20 |
Correct |
18 ms |
15492 KB |
Output is correct |
21 |
Correct |
18 ms |
15492 KB |
Output is correct |
22 |
Correct |
26 ms |
15528 KB |
Output is correct |
23 |
Correct |
22 ms |
15608 KB |
Output is correct |
24 |
Correct |
22 ms |
15608 KB |
Output is correct |
25 |
Correct |
20 ms |
15612 KB |
Output is correct |
26 |
Correct |
19 ms |
15632 KB |
Output is correct |
27 |
Correct |
21 ms |
15632 KB |
Output is correct |
28 |
Correct |
18 ms |
15664 KB |
Output is correct |
29 |
Correct |
20 ms |
15700 KB |
Output is correct |
30 |
Correct |
22 ms |
15700 KB |
Output is correct |
31 |
Correct |
1801 ms |
97472 KB |
Output is correct |
32 |
Correct |
107 ms |
97472 KB |
Output is correct |
33 |
Correct |
1777 ms |
98956 KB |
Output is correct |
34 |
Correct |
1703 ms |
102004 KB |
Output is correct |
35 |
Correct |
1762 ms |
107408 KB |
Output is correct |
36 |
Correct |
1760 ms |
110144 KB |
Output is correct |
37 |
Correct |
1495 ms |
110144 KB |
Output is correct |
38 |
Correct |
1414 ms |
112816 KB |
Output is correct |
39 |
Correct |
1089 ms |
115264 KB |
Output is correct |
40 |
Correct |
1261 ms |
118224 KB |
Output is correct |
41 |
Correct |
1461 ms |
123352 KB |
Output is correct |
42 |
Correct |
1531 ms |
126220 KB |
Output is correct |
43 |
Correct |
126 ms |
126220 KB |
Output is correct |
44 |
Correct |
1516 ms |
131708 KB |
Output is correct |
45 |
Correct |
1353 ms |
134756 KB |
Output is correct |
46 |
Correct |
1360 ms |
137536 KB |
Output is correct |
47 |
Correct |
1161 ms |
137536 KB |
Output is correct |
48 |
Correct |
1144 ms |
138792 KB |
Output is correct |
49 |
Correct |
1192 ms |
143500 KB |
Output is correct |
50 |
Correct |
1225 ms |
147180 KB |
Output is correct |
51 |
Correct |
1162 ms |
149164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5092 ms |
461024 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5026 ms |
461024 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
14456 KB |
Output is correct |
2 |
Correct |
15 ms |
14564 KB |
Output is correct |
3 |
Correct |
19 ms |
14604 KB |
Output is correct |
4 |
Correct |
17 ms |
14696 KB |
Output is correct |
5 |
Correct |
17 ms |
14728 KB |
Output is correct |
6 |
Correct |
18 ms |
15148 KB |
Output is correct |
7 |
Correct |
25 ms |
15168 KB |
Output is correct |
8 |
Correct |
18 ms |
15188 KB |
Output is correct |
9 |
Correct |
23 ms |
15256 KB |
Output is correct |
10 |
Correct |
19 ms |
15320 KB |
Output is correct |
11 |
Correct |
22 ms |
15320 KB |
Output is correct |
12 |
Correct |
23 ms |
15320 KB |
Output is correct |
13 |
Correct |
20 ms |
15332 KB |
Output is correct |
14 |
Correct |
22 ms |
15368 KB |
Output is correct |
15 |
Correct |
21 ms |
15372 KB |
Output is correct |
16 |
Correct |
21 ms |
15392 KB |
Output is correct |
17 |
Correct |
19 ms |
15412 KB |
Output is correct |
18 |
Correct |
21 ms |
15432 KB |
Output is correct |
19 |
Correct |
19 ms |
15492 KB |
Output is correct |
20 |
Correct |
18 ms |
15492 KB |
Output is correct |
21 |
Correct |
18 ms |
15492 KB |
Output is correct |
22 |
Correct |
26 ms |
15528 KB |
Output is correct |
23 |
Correct |
22 ms |
15608 KB |
Output is correct |
24 |
Correct |
22 ms |
15608 KB |
Output is correct |
25 |
Correct |
20 ms |
15612 KB |
Output is correct |
26 |
Correct |
19 ms |
15632 KB |
Output is correct |
27 |
Correct |
21 ms |
15632 KB |
Output is correct |
28 |
Correct |
18 ms |
15664 KB |
Output is correct |
29 |
Correct |
20 ms |
15700 KB |
Output is correct |
30 |
Correct |
22 ms |
15700 KB |
Output is correct |
31 |
Correct |
1801 ms |
97472 KB |
Output is correct |
32 |
Correct |
107 ms |
97472 KB |
Output is correct |
33 |
Correct |
1777 ms |
98956 KB |
Output is correct |
34 |
Correct |
1703 ms |
102004 KB |
Output is correct |
35 |
Correct |
1762 ms |
107408 KB |
Output is correct |
36 |
Correct |
1760 ms |
110144 KB |
Output is correct |
37 |
Correct |
1495 ms |
110144 KB |
Output is correct |
38 |
Correct |
1414 ms |
112816 KB |
Output is correct |
39 |
Correct |
1089 ms |
115264 KB |
Output is correct |
40 |
Correct |
1261 ms |
118224 KB |
Output is correct |
41 |
Correct |
1461 ms |
123352 KB |
Output is correct |
42 |
Correct |
1531 ms |
126220 KB |
Output is correct |
43 |
Correct |
126 ms |
126220 KB |
Output is correct |
44 |
Correct |
1516 ms |
131708 KB |
Output is correct |
45 |
Correct |
1353 ms |
134756 KB |
Output is correct |
46 |
Correct |
1360 ms |
137536 KB |
Output is correct |
47 |
Correct |
1161 ms |
137536 KB |
Output is correct |
48 |
Correct |
1144 ms |
138792 KB |
Output is correct |
49 |
Correct |
1192 ms |
143500 KB |
Output is correct |
50 |
Correct |
1225 ms |
147180 KB |
Output is correct |
51 |
Correct |
1162 ms |
149164 KB |
Output is correct |
52 |
Correct |
1312 ms |
461024 KB |
Output is correct |
53 |
Correct |
1302 ms |
461024 KB |
Output is correct |
54 |
Correct |
1496 ms |
461024 KB |
Output is correct |
55 |
Correct |
1280 ms |
461024 KB |
Output is correct |
56 |
Correct |
1411 ms |
461024 KB |
Output is correct |
57 |
Correct |
1392 ms |
461024 KB |
Output is correct |
58 |
Correct |
1508 ms |
461024 KB |
Output is correct |
59 |
Correct |
1386 ms |
461024 KB |
Output is correct |
60 |
Correct |
1357 ms |
461024 KB |
Output is correct |
61 |
Correct |
219 ms |
461024 KB |
Output is correct |
62 |
Correct |
1300 ms |
461024 KB |
Output is correct |
63 |
Correct |
1432 ms |
461024 KB |
Output is correct |
64 |
Correct |
1488 ms |
461024 KB |
Output is correct |
65 |
Correct |
1409 ms |
461024 KB |
Output is correct |
66 |
Correct |
1301 ms |
461024 KB |
Output is correct |
67 |
Correct |
307 ms |
461024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
14456 KB |
Output is correct |
2 |
Correct |
15 ms |
14564 KB |
Output is correct |
3 |
Correct |
19 ms |
14604 KB |
Output is correct |
4 |
Correct |
17 ms |
14696 KB |
Output is correct |
5 |
Correct |
17 ms |
14728 KB |
Output is correct |
6 |
Correct |
18 ms |
15148 KB |
Output is correct |
7 |
Correct |
25 ms |
15168 KB |
Output is correct |
8 |
Correct |
18 ms |
15188 KB |
Output is correct |
9 |
Correct |
23 ms |
15256 KB |
Output is correct |
10 |
Correct |
19 ms |
15320 KB |
Output is correct |
11 |
Correct |
22 ms |
15320 KB |
Output is correct |
12 |
Correct |
23 ms |
15320 KB |
Output is correct |
13 |
Correct |
20 ms |
15332 KB |
Output is correct |
14 |
Correct |
22 ms |
15368 KB |
Output is correct |
15 |
Correct |
21 ms |
15372 KB |
Output is correct |
16 |
Correct |
21 ms |
15392 KB |
Output is correct |
17 |
Correct |
19 ms |
15412 KB |
Output is correct |
18 |
Correct |
21 ms |
15432 KB |
Output is correct |
19 |
Correct |
19 ms |
15492 KB |
Output is correct |
20 |
Correct |
18 ms |
15492 KB |
Output is correct |
21 |
Correct |
18 ms |
15492 KB |
Output is correct |
22 |
Correct |
26 ms |
15528 KB |
Output is correct |
23 |
Correct |
22 ms |
15608 KB |
Output is correct |
24 |
Correct |
22 ms |
15608 KB |
Output is correct |
25 |
Correct |
20 ms |
15612 KB |
Output is correct |
26 |
Correct |
19 ms |
15632 KB |
Output is correct |
27 |
Correct |
21 ms |
15632 KB |
Output is correct |
28 |
Correct |
18 ms |
15664 KB |
Output is correct |
29 |
Correct |
20 ms |
15700 KB |
Output is correct |
30 |
Correct |
22 ms |
15700 KB |
Output is correct |
31 |
Correct |
1801 ms |
97472 KB |
Output is correct |
32 |
Correct |
107 ms |
97472 KB |
Output is correct |
33 |
Correct |
1777 ms |
98956 KB |
Output is correct |
34 |
Correct |
1703 ms |
102004 KB |
Output is correct |
35 |
Correct |
1762 ms |
107408 KB |
Output is correct |
36 |
Correct |
1760 ms |
110144 KB |
Output is correct |
37 |
Correct |
1495 ms |
110144 KB |
Output is correct |
38 |
Correct |
1414 ms |
112816 KB |
Output is correct |
39 |
Correct |
1089 ms |
115264 KB |
Output is correct |
40 |
Correct |
1261 ms |
118224 KB |
Output is correct |
41 |
Correct |
1461 ms |
123352 KB |
Output is correct |
42 |
Correct |
1531 ms |
126220 KB |
Output is correct |
43 |
Correct |
126 ms |
126220 KB |
Output is correct |
44 |
Correct |
1516 ms |
131708 KB |
Output is correct |
45 |
Correct |
1353 ms |
134756 KB |
Output is correct |
46 |
Correct |
1360 ms |
137536 KB |
Output is correct |
47 |
Correct |
1161 ms |
137536 KB |
Output is correct |
48 |
Correct |
1144 ms |
138792 KB |
Output is correct |
49 |
Correct |
1192 ms |
143500 KB |
Output is correct |
50 |
Correct |
1225 ms |
147180 KB |
Output is correct |
51 |
Correct |
1162 ms |
149164 KB |
Output is correct |
52 |
Execution timed out |
5092 ms |
461024 KB |
Time limit exceeded |
53 |
Halted |
0 ms |
0 KB |
- |