This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
const int lim = 5e6;
struct Node{
int inc = -1e9, dec = -1e9;
Node *l = nullptr, *r = nullptr;
Node(){}
Node(Node *_l, Node *_r): l(_l), r(_r){}
void update(){
inc = -1e9;
if(l) inc = max(inc, l->inc);
if(r) inc = max(inc, r->inc);
dec = -1e9;
if(l) dec = max(dec, l->dec);
if(r) dec = max(dec, r->dec);
}
}Nodes[lim];
int ls_node = 0;
Node* newNode(){
return &Nodes[ls_node++];
}
struct sparse_segtree{
Node* root = newNode();
int n = 1;
sparse_segtree(int _n){
while(n < _n) n<<=1;
}
void update(int i, Node *nd, int a, int b, int x, bool inc){
if(a == b){
if(inc) nd->inc = x+n-1-i;
else nd->dec = x+i;
return;
}
int m = (a+b)>>1;
if(i <= m){
if(nd->l == nullptr) nd->l = newNode();
update(i, nd->l, a, m, x, inc);
}
else{
if(nd->r == nullptr) nd->r = newNode();
update(i, nd->r, m+1, b, x, inc);
}
nd->update();
}
void update(int i, int x, bool inc){
update(i, root, 0, n-1, x, inc);
}
int get(int l, int r, Node* nd, int a, int b, bool inc){
if(!nd) return -1e9;
if(a > r || b < l) return -1e9;
if(a >= l && b <= r){
if(inc) return nd->inc;
else return nd->dec;
}
int m = (a+b)>>1;
return max(get(l, r, nd->l, a, m, inc), get(l, r, nd->r, m+1, b, inc));
}
int get(int l, int r, bool inc){
return get(l, r, root, 0, n-1, inc);
}
};
const int L = 1e8+5;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, k, q; cin >> n >> k >> q;
vector<tuple<int, int, int, int>> queries;
for(int i = 0; i < n; i++){
int x, t, a, b; cin >> x >> t >> a >> b; t--;
queries.push_back({a, 0, x, t});
queries.push_back({b, 2, x, t});
}
for(int i = 0; i < q; i++){
int l, y; cin >> l >> y;
queries.push_back({y, 1, l, i});
}
sort(queries.begin(), queries.end());
vector<set<int>> stores(k);
vector<map<int, int>> cnt(k);
map<int, map<int, int>> mx[2];
sparse_segtree S(L);
auto add_to_mx = [&](int i, int x, bool inc){
if(mx[inc][i].empty() || x > mx[inc][i].rbegin()->first) S.update(i, x, inc);
mx[inc][i][x]++;
};
auto remove_from_mx = [&](int i, int x, bool inc){
mx[inc][i][x]--;
if(mx[inc][i][x] == 0) mx[inc][i].erase(x);
if(mx[inc][i].empty()) S.update(i, -1e9, inc);
else if(x > mx[inc][i].rbegin()->first) S.update(i, mx[inc][i].rbegin()->first, inc);
};
auto add = [&](int t, int x){
cnt[t][x]++;
if(cnt[t][x] > 1) return;
auto it = stores[t].insert(x).first;
if(stores[t].size() == 1){
add_to_mx(0, x, false);
add_to_mx(L-1, L-1-x, true);
}
else if(it == stores[t].begin()){
remove_from_mx(0, *next(it), false);
add_to_mx(0, x, false);
int md = (x+*next(it))>>1;
if(md > x) add_to_mx(md, md-x, true);
add_to_mx(md+1, *next(it)-md-1, false);
}
else if(next(it) == stores[t].end()){
remove_from_mx(L-1, L-1-*prev(it), true);
int md = (*prev(it)+x)>>1;
if(md > *prev(it)) add_to_mx(md, md-*prev(it), true);
add_to_mx(md+1, x-md-1, false);
add_to_mx(L-1, L-1-x, true);
}
else{
int md = (*prev(it)+*next(it))>>1;
if(md > *prev(it)) remove_from_mx(md, md-*prev(it), true);
remove_from_mx(md+1, *next(it)-md-1, false);
md = (*prev(it)+x)>>1;
if(md > *prev(it)) add_to_mx(md, md-*prev(it), true);
add_to_mx(md+1, x-md-1, false);
md = (x+*next(it))>>1;
if(md > x) add_to_mx(md, md-x, true);
add_to_mx(md+1, *next(it)-md-1, false);
}
};
auto remove = [&](int t, int x){
cnt[t][x]--;
if(cnt[t][x] > 0) return;
auto it = stores[t].find(x);
if(stores[t].size() == 1){
remove_from_mx(0, x, false);
remove_from_mx(L-1, L-1-x, true);
}
else if(it == stores[t].begin()){
add_to_mx(0, *next(it), false);
remove_from_mx(0, x, false);
int md = (x+*next(it))>>1;
if(md > x) remove_from_mx(md, md-x, true);
remove_from_mx(md+1, *next(it)-md-1, false);
}
else if(next(it) == stores[t].end()){
add_to_mx(L-1, L-1-*prev(it), true);
int md = (*prev(it)+x)>>1;
if(md > *prev(it)) remove_from_mx(md, md-*prev(it), true);
remove_from_mx(md+1, x-md-1, false);
remove_from_mx(L-1, L-1-x, true);
}
else{
int md = (*prev(it)+*next(it))>>1;
if(md > *prev(it)) add_to_mx(md, md-*prev(it), true);
add_to_mx(md+1, *next(it)-md-1, false);
md = (*prev(it)+x)>>1;
if(md > *prev(it)) remove_from_mx(md, md-*prev(it), true);
remove_from_mx(md+1, x-md-1, false);
md = (x+*next(it))>>1;
if(md > x) remove_from_mx(md, md-x, true);
remove_from_mx(md+1, *next(it)-md-1, false);
}
stores[t].erase(it);
};
vector<int> cnt_t(k, 0);
int nonzero = 0;
vector<int> ans(q);
for(auto [_, type, x, i]: queries){
if(type == 0){
cnt_t[i]++;
if(cnt_t[i] == 1) nonzero++;
add(i, x);
}
else if(type == 2){
cnt_t[i]--;
if(cnt_t[i] == 0) nonzero--;
remove(i, x);
}
else{
if(nonzero < k) ans[i] = -1;
else ans[i] = max(S.get(0, x, 0)-x, S.get(x, S.n-1, 1)-(S.n-1)+x);
}
}
for(int x: ans) cout << x << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |