#include <bits/stdc++.h>
#define ALL(x) x.begin(),x.end()
#define pb push_back
#define lb lower_bound
using namespace std;
struct Query {
int time, pos, shopType, id;
bool isOpen, isQuery;
};
struct Node {
multiset <int> vals;
int lNode, rNode;
Node() {
lNode = rNode = -1;
}
};
int nodes = 1;
vector <Node> node(1);
Node emptyNode;
void addSide(Node& tmp, int l, int r) {
int mid = (l+r)>>1;
tmp.lNode = nodes++;
tmp.rNode = nodes++;
node.pb(emptyNode);
node.pb(emptyNode);
}
void update(int l, int r, int L, int R, int val, bool isAdd, int head) {
// cout << '\t' << L << ' ' << R << ' ' << val << ' ' << (isAdd ? "+" : "-") << '\n';
if (l > R || L > r || l > r) return;
if (L <= l && r <= R) {
if (isAdd) {
node[head].vals.insert(val);
} else {
node[head].vals.erase(node[head].vals.lb(val));
}
return;
}
int mid = (l+r)>>1;
if (node[head].lNode == -1) addSide(node[head], l, r);
update(l, mid, L, R, val, isAdd, node[head].lNode);
update(mid+1, r, L, R, val, isAdd, node[head].rNode);
}
int query(int l, int r, int pos, int head) {
if (head < 0) return 0;
int res = 0;
if (node[head].vals.size() > 0) res = max(res, abs(pos - *node[head].vals.begin()));
if (node[head].vals.size() > 1) res = max(res, abs(pos - *node[head].vals.rbegin()));
int mid = (l+r)>>1;
if (pos <= mid) res = max(res, query(l, mid, pos, node[head].lNode));
else res = max(res, query(mid+1, r, pos, node[head].rNode));
return res;
}
const int N = 3e5 + 5;
multiset <int> pos[N];
int shopSz;
int segL = 1, segR = 1e8;
void updateQuery(Query& query) {
multiset <int>& curSet = pos[query.shopType];
// cout << (query.isOpen ? "ADD" : "REM") << ' ' << query.pos << ' ' << query.shopType << ' ' << query.time << '\n';
// cout << curSet.size() << " -> " ;
if (query.isOpen) {
curSet.insert(query.pos);
auto it = curSet.lb(query.pos);
auto itL = it; itL--;
auto itR = it; itR++;
int a = *itL, b = *it, c = *itR;
int mid = (a + c) / 2;
int mid1 = (a + b) / 2;
int mid2 = (b + c) / 2;
if (a != 0 && c != 1e9) {
update(segL, segR, a, mid, a, 0, 0);
update(segL, segR, mid, c, c, 0, 0);
update(segL, segR, a, mid1, a, 1, 0);
update(segL, segR, mid1, mid2, b, 1, 0);
update(segL, segR, mid2, c, c, 1, 0);
} else if (a != 0) {
update(segL, segR, a, segR, a, 0, 0);
update(segL, segR, a, mid1, a, 1, 0);
update(segL, segR, mid1, b, b, 1, 0);
update(segL, segR, b, segR, b, 1, 0);
} else if (c != 1e9) {
update(segL, segR, segL, c, c, 0, 0);
update(segL, segR, segL, b, b, 1, 0);
update(segL, segR, b, mid2, b, 1, 0);
update(segL, segR, mid2, c, c, 1, 0);
} else {
update(segL, segR, segL, b, b, 1, 0);
update(segL, segR, b, segR, b, 1, 0);
}
} else {
auto it = curSet.lb(query.pos);
auto itL = it; itL--;
auto itR = it; itR++;
int a = *itL, b = *it, c = *itR;
curSet.erase(it);
int mid = (a + c) / 2;
int mid1 = (a + b) / 2;
int mid2 = (b + c) / 2;
if (a != 0 && c != 1e9) {
update(segL, segR, a, mid1, a, 0, 0);
update(segL, segR, mid1, mid2, b, 0, 0);
update(segL, segR, mid2, c, c, 0, 0);
update(segL, segR, a, mid, a, 1, 0);
update(segL, segR, mid, c, c, 1, 0);
} else if (a != 0) {
update(segL, segR, a, mid1, a, 0, 0);
update(segL, segR, mid1, b, b, 0, 0);
update(segL, segR, b, segR, b, 0, 0);
update(segL, segR, a, segR, a, 1, 0);
} else if (c != 1e9) {
update(segL, segR, segL, b, b, 0, 0);
update(segL, segR, b, mid2, b, 0, 0);
update(segL, segR, mid2, c, c, 0, 0);
update(segL, segR, segL, c, c, 1, 0);
} else {
update(segL, segR, segL, b, b, 0, 0);
update(segL, segR, b, segR, b, 0, 0);
}
}
// cout << curSet.size() << '\n';
shopSz += (query.isOpen && curSet.size() == 3);
shopSz -= (curSet.size() == 2);
}
int ans[N];
int n, q, k;
int getAns(Query& q) {
// cout << "QUERY: " << q.pos << ' ' << q.id << ' ' << q.time << '\n';
// cout << shopSz << ' ' << k << '\n';
if (shopSz < k) return -1;
return query(segL, segR, q.pos, 0);
}
signed main() {
// freopen("0.in", "r", stdin);
// freopen("0.out", "w", stdout);
cin >> n >> k >> q;
for (int i = 1; i <= k; i++) {
pos[i].insert(0);
pos[i].insert(1e9);
}
vector <Query> queries;
for (int i = 1; i <= n; i++) {
int x, t, a, b; cin >> x >> t >> a >> b;
queries.pb({a, x, t, 0, 1, 0});
queries.pb({b+1, x, t, 0, 0, 0});
}
for (int i = 1; i <= q; i++) {
int x, t; cin >> x >> t;
queries.pb({t, x, 0, i, 0, 1});
}
sort(ALL(queries), [&](Query&a, Query&b) {
return (a.time == b.time ? b.isQuery : a.time < b.time);
});
for (auto& query : queries) {
if (!query.isQuery) {
updateQuery(query);
} else {
ans[query.id] = getAns(query);
}
}
for (int i = 1; i <= q; i++)
cout << ans[i] << '\n';
}
Compilation message
new_home.cpp: In function 'void addSide(Node&, int, int)':
new_home.cpp:21:6: warning: unused variable 'mid' [-Wunused-variable]
21 | int mid = (l+r)>>1;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14292 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14396 KB |
Output is correct |
4 |
Execution timed out |
5047 ms |
14400 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14292 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14396 KB |
Output is correct |
4 |
Execution timed out |
5047 ms |
14400 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5045 ms |
80748 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
679 ms |
87864 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14292 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14396 KB |
Output is correct |
4 |
Execution timed out |
5047 ms |
14400 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14292 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14396 KB |
Output is correct |
4 |
Execution timed out |
5047 ms |
14400 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |