Submission #84168

#TimeUsernameProblemLanguageResultExecution timeMemory
84168radoslav11New Home (APIO18_new_home)C++14
47 / 100
5104 ms155948 KiB
/* 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; int read_int(); random_device rd; mt19937 mt(rd() ^ 231231); struct treap_l { struct node { pair<int, int> v; int mx; int prior; node *l, *r; node() { v = {0, 0}; prior = mx = 0; l = r = nullptr; } node(int L, int R) { v = {L, R}; mx = R; prior = mt(); l = r = nullptr; } }; using pnode = node*; 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); } 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); } pnode root; treap_l() { root = nullptr; } pnode rotate_left(pnode x) { pnode y = x->r, T2 = y->l; x->r = T2; y->l = x; return y; } pnode rotate_right(pnode y) { pnode x = y->l, T2 = x->r; y->l = T2; x->r = y; return x; } pnode lazy_add(pnode t, pair<int, int> x) { if(!t) return new node(x.first, x.second); if(x < t->v) { t->l = lazy_add(t->l, x); if(t->l->prior > t->prior) t = rotate_right(t); pull(t->r); } else { t->r = lazy_add(t->r, x); if(t->r->prior > t->prior) t = rotate_left(t); pull(t->l); } pull(t); return t; } pnode lazy_remove(pnode t, pair<int, int> x) { if(t->v == x) { merge(t, t->l, t->r); return t; } if(t->v < x) t->r = lazy_remove(t->r, x); else t->l = lazy_remove(t->l, x); pull(t); return t; } void add_interval(int l, int r) { root = lazy_add(root, {l, r}); } void rem_interval(int l, int r) { root = lazy_remove(root, {l, r}); } int query(pnode t, int x) { if(!t) return 0; int ret = 0; if(t->v.first <= x && 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) { return query(root, x); } } L; struct treap_r { struct node { pair<int, int> v; int mn; int prior; node *l, *r; node() { v = {0, 0}; prior = mn = 0; l = r = nullptr; } node(int R, int L) { v = {R, L}; mn = L; prior = mt(); l = r = nullptr; } }; using pnode = node*; 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); } 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); } pnode root; treap_r() { root = nullptr; } pnode rotate_left(pnode x) { pnode y = x->r, T2 = y->l; x->r = T2; y->l = x; return y; } pnode rotate_right(pnode y) { pnode x = y->l, T2 = x->r; y->l = T2; x->r = y; return x; } pnode lazy_add(pnode t, pair<int, int> x) { if(!t) return new node(x.first, x.second); if(x < t->v) { t->l = lazy_add(t->l, x); if(t->l->prior > t->prior) t = rotate_right(t); pull(t->r); } else { t->r = lazy_add(t->r, x); if(t->r->prior > t->prior) t = rotate_left(t); pull(t->l); } pull(t); return t; } pnode lazy_remove(pnode t, pair<int, int> x) { if(t->v == x) { merge(t, t->l, t->r); return t; } if(t->v < x) t->r = lazy_remove(t->r, x); else t->l = lazy_remove(t->l, x); pull(t); return t; } void add_interval(int l, int r) { root = lazy_add(root, {r, l}); } void rem_interval(int l, int r) { root = lazy_remove(root, {r, l}); } int query(pnode t, int x) { if(!t) return 0; int ret = 0; if(t->v.second <= x && x <= t->v.first) 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) { return query(root, x); } } 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() { n = read_int(); k = read_int(); q = read_int(); for(int i = 0; i < n; i++) { int x, t, a, b; x = read_int(); t = read_int(); a = read_int(); b = read_int(); 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; x = read_int(); t = read_int(); 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; } const int maxl = 100000; char buff[maxl]; int ret_int, pos_buff = 0; void next_char() { if(++pos_buff == maxl) fread(buff, 1, maxl, stdin), pos_buff = 0; } int read_int() { ret_int = 0; for(; buff[pos_buff] < '0' || buff[pos_buff] > '9'; next_char()); for(; buff[pos_buff] >= '0' && buff[pos_buff] <= '9'; next_char()) ret_int = ret_int * 10 + buff[pos_buff] - '0'; return ret_int; }

Compilation message (stderr)

new_home.cpp: In function 'void next_char()':
new_home.cpp:479:70: warning: ignoring return value of 'size_t fread(void*, size_t, size_t, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
 void next_char() { if(++pos_buff == maxl) fread(buff, 1, maxl, stdin), pos_buff = 0; }
                                           ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...