Submission #876327

#TimeUsernameProblemLanguageResultExecution timeMemory
876327marvinthangNew Home (APIO18_new_home)C++17
100 / 100
3420 ms81632 KiB
/************************************* * author: marvinthang * * created: 21.11.2023 14:25:44 * *************************************/ #include <bits/stdc++.h> using namespace std; #define fi first #define se second #define left ___left #define right ___right #define TIME (1.0 * clock() / CLOCKS_PER_SEC) #define MASK(i) (1LL << (i)) #define BIT(x, i) ((x) >> (i) & 1) #define __builtin_popcount __builtin_popcountll #define ALL(v) (v).begin(), (v).end() #define REP(i, n) for (int i = 0, _n = (n); i < _n; ++i) #define REPD(i, n) for (int i = (n); i-- > 0; ) #define FOR(i, a, b) for (int i = (a), _b = (b); i < _b; ++i) #define FORD(i, b, a) for (int i = (b), _a = (a); --i >= _a; ) #define FORE(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i) #define FORDE(i, b, a) for (int i = (b), _a = (a); i >= _a; --i) #define scan_op(...) istream & operator >> (istream &in, __VA_ARGS__ &u) #define print_op(...) ostream & operator << (ostream &out, const __VA_ARGS__ &u) #ifdef LOCAL #include "debug.h" #else #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); } #define DB(...) 23 #define db(...) 23 #define debug(...) 23 #endif template <class U, class V> scan_op(pair <U, V>) { return in >> u.first >> u.second; } template <class T> scan_op(vector <T>) { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; } template <class U, class V> print_op(pair <U, V>) { return out << '(' << u.first << ", " << u.second << ')'; } template <size_t i, class T> ostream & print_tuple_utils(ostream &out, const T &tup) { if constexpr(i == tuple_size<T>::value) return out << ")"; else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); } template <class ...U> print_op(tuple<U...>) { return print_tuple_utils<0, tuple<U...>>(out, u); } template <class Con, class = decltype(begin(declval<Con>()))> typename enable_if <!is_same<Con, string>::value, ostream&>::type operator << (ostream &out, const Con &con) { out << '{'; for (__typeof(con.begin()) it = con.begin(); it != con.end(); ++it) out << (it == con.begin() ? "" : ", ") << *it; return out << '}'; } // end of template const int INF = 1e8 + 23; struct Store { int x, t, a, b; Store(int x, int t, int a, int b): x(x), t(t), a(a), b(b) {} bool operator < (const Store &ot) const { return x < ot.x; } }; void process(void) { int n, q, k; cin >> n >> k >> q; vector <Store> stores; vector <int> cp; vector <set <int>> pos(k); REP(i, n) { int x, t, a, b; cin >> x >> t >> a >> b; --t; stores.emplace_back(x, t, a, b); } sort(ALL(stores)); vector <pair <int, int>> queries(q); cin >> queries; vector <pair <int, int>> events; REP(i, n) { events.emplace_back(stores[i].a, i); events.emplace_back(stores[i].b + 1, i); } REP(i, q) events.emplace_back(queries[i].se, i + n); sort(ALL(events)); multiset <int> last; REP(i, k) last.insert(-1); auto cmp = [&] (const Store &a, const int &b) { return a.x < b; }; vector <int> st(n * 4 + 23, INF); function <void(int, int, int, int, int)> update = [&] (int i, int l, int r, int p, int v) { if (r - l == 1) { st[i] = v; return; } int m = l + r >> 1; if (p < m) update(i << 1, l, m, p, v); else update(i << 1 | 1, m, r, p, v); st[i] = min(st[i << 1], st[i << 1 | 1]); }; function <int(int, int, int, int)> get = [&] (int i, int l, int r, int p) { if (r - l == 1) return st[i]; int m = l + r >> 1; if (p >= m) return get(i << 1 | 1, m, r, p); return min(get(i << 1, l, m, p), st[i << 1 | 1]); }; update(1, 0, n + 1, n, -1); vector <int> res(q); for (auto [y, i]: events) { if (i < n) { auto [x, t, a, b] = stores[i]; if (y == a) { auto it = pos[t].insert(i).fi; int prv = it == pos[t].begin() ? -1 : *prev(it); int nxt = next(it) == pos[t].end() ? n : *next(it); if (nxt == n) { last.erase(last.find(prv)); last.insert(i); update(1, 0, n + 1, nxt, *last.begin()); } else update(1, 0, n + 1, nxt, i); update(1, 0, n + 1, i, prv); } else { auto it = pos[t].find(i); int prv = it == pos[t].begin() ? -1 : *prev(it); int nxt = next(it) == pos[t].end() ? n : *next(it); if (nxt == n) { last.erase(i); last.insert(prv); update(1, 0, n + 1, nxt, *last.begin()); } else update(1, 0, n + 1, nxt, prv); update(1, 0, n + 1, i, INF); pos[t].erase(it); } } else { i -= n; int l = 0, r = INF; while (l <= r) { int m = (l + r) >> 1; int ll = lower_bound(ALL(stores), queries[i].fi - m, cmp) - stores.begin(); int rr = lower_bound(ALL(stores), queries[i].fi + m + 1, cmp) - stores.begin(); if (ll < rr && get(1, 0, n + 1, rr) >= ll) r = m - 1; else l = m + 1; } res[i] = l > INF ? -1 : l; } } REP(i, q) cout << res[i] << '\n'; } int main(void) { ios_base::sync_with_stdio(false); cin.tie(nullptr); // cout.tie(nullptr); file("new_home"); // int t; cin >> t; while (t--) process(); // cerr << "Time elapsed: " << TIME << " s.\n"; return (0^0); }

Compilation message (stderr)

new_home.cpp: In lambda function:
new_home.cpp:86:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   86 |   int m = l + r >> 1;
      |           ~~^~~
new_home.cpp: In lambda function:
new_home.cpp:94:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   94 |   int m = l + r >> 1;
      |           ~~^~~
new_home.cpp: In function 'int main()':
new_home.cpp:30:61: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   30 |     #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
new_home.cpp:144:2: note: in expansion of macro 'file'
  144 |  file("new_home");
      |  ^~~~
new_home.cpp:30:94: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   30 |     #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                                                       ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
new_home.cpp:144:2: note: in expansion of macro 'file'
  144 |  file("new_home");
      |  ^~~~
#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...