Submission #936518

# Submission time Handle Problem Language Result Execution time Memory
936518 2024-03-02T04:32:54 Z rxlfd314 New Home (APIO18_new_home) C++17
47 / 100
5000 ms 515508 KB
#include <bits/stdc++.h>
using namespace std;
using ari2 = array<int, 2>;
using ari3 = array<int, 3>;
using ari4 = array<int, 4>;

#define vt vector
#define size(x) (int((x).size()))
#define all(x) begin(x), end(x)

#define REP(a, b, c, d) for (int a = (b); (d) > 0 ? a <= (c) : a >= (c); a += (d))
#define FOR(a, b, c) REP(a, b, c, 1)
#define ROF(a, b, c) REP(a, b, c, -1)

constexpr int mxN = 300000;
int N, K, Q;
vt<int> cmp;

struct ST {
  multiset<int> st[mxN<<2];
  #define lc i << 1
  #define rc lc | 1
  void insert(int ql, int qr, int v, int i = 1, int tl = 0, int tr = size(cmp)-1) {
    if (tl > qr || tr < ql)
      return;
    if (ql <= tl && tr <= qr)
      st[i].insert(v);
    else {
      int tm = tl + tr >> 1;
      insert(ql, qr, v, lc, tl, tm);
      insert(ql, qr, v, rc, tm+1, tr);
    }
  }
  void erase(int ql, int qr, int v, int i = 1, int tl = 0, int tr = size(cmp)-1) {
    if (tl > qr || tr < ql)
      return;
    if (ql <= tl && tr <= qr)
      st[i].erase(st[i].find(v));
    else {
      int tm = tl + tr >> 1;
      erase(ql, qr, v, lc, tl, tm);
      erase(ql, qr, v, rc, tm+1, tr);
    }
  }
  int qry(int p, bool last, int i = 1, int tl = 0, int tr = size(cmp)-1) {
    int ret = last ? -1 : INT_MAX;
    while (tl < tr) {
      if (size(st[i])) {
        if (last)
          ret = max(ret, *prev(end(st[i])));
        else
          ret = min(ret, *begin(st[i]));
      }
      int tm = tl + tr >> 1;
      if (p <= tm)
        i = lc, tr = tm;
      else
        i = rc, tl = tm + 1;
    }
    if (size(st[i])) {
      if (last)
        ret = max(ret, *prev(end(st[i])));
      else
        ret = min(ret, *begin(st[i]));
    }
    return ret;
  }
  #undef lc
  #undef rc
} st_left, st_right;

signed main() {
#ifndef DEBUG
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
#endif
  cin >> N >> K >> Q;
  vt<ari4> events;
  FOR(i, 0, N-1) {
    int x, t, a, b;
    cin >> x >> t >> a >> b, t--;
    events.push_back({a, 0, x, t}); // time, (0 = insert, 1 = remove, 2 = is year), location, type
    events.push_back({b+1, 1, x, t});
  }
  FOR(i, 0, Q-1) {
    int year, x;
    cin >> x >> year;
    events.push_back({year, 2, x, i}); // time, is year, location, query index
    cmp.push_back(x);
  }
  sort(all(cmp));
  cmp.erase(unique(all(cmp)), end(cmp));
  sort(all(events));

  auto ind = [&](int v) {
    return lower_bound(all(cmp), v) - begin(cmp);
  };
  auto erase = [&](int l, int r) {
    assert(l + r + 1 >= 0);
    int m = l + r + 1 >> 1;
    int li = ind(l), ri = ind(r), mi = ind(m);
    ri -= cmp[ri] > r;
    if (mi - 1 >= li)
      st_left.erase(li, mi-1, l); 
    if (ri >= mi)
      st_right.erase(mi, ri, r);
  };
  auto insert = [&](int l, int r) {
    assert(l + r + 1 >= 0);
    int m = l + r + 1 >> 1;
    int li = ind(l), ri = ind(r), mi = ind(m);
    ri -= cmp[ri] > r;
    if (mi - 1 >= li)
      st_left.insert(li, mi-1, l);
    if (ri >= mi)
      st_right.insert(mi, ri, r);
  };
  
  vt<int> ans(Q), present(K);
  vt<map<int, int>> positions(K);
  int present_cnt = 0;
  for (auto &[t, is_query, x, type] : events) {
    if (is_query == 2) {
      if (present_cnt < K) {
        ans[type] = -1;
        continue;
      }
      int i = ind(x);
      int l = min(x, st_left.qry(i, false));
      int r = max(x, st_right.qry(i, true));
      ans[type] = max(r - x, x - l);
      #ifdef DEBUG
      cout << "at time " << t << " answer for " << x << " is:\n";
      cout << "L: " << l << '\n';
      cout << "R: " << r << '\n';
      #endif
    } else if (is_query) {
      auto &xs = positions[type];
      if (xs[x] == 1) {
        auto it = xs.find(x);
        int l = it != begin(xs) ? prev(it)->first : -1;
        int r = next(it) != end(xs) ? next(it)->first : INT_MAX;
        xs.erase(it);
        if (l >= 0)
          erase(l, x);
        else
          erase(-x, x);
        if (r < INT_MAX)
          erase(x, r); 
        else
          erase(x, INT_MAX - x - 1);
        
        if (l >= 0 && r < INT_MAX)
          insert(l, r);
        else if (l >= 0)
          insert(l, INT_MAX - l - 1);
        else if (r < INT_MAX)
          insert(-r, r);
      } else {
        xs[x]--;
      }
      present_cnt -= --present[type] == 0;
    } else {
      auto &xs = positions[type];
      if (xs[x])
        xs[x]++;
      else {
        xs[x] = 1;
        auto it = xs.find(x);
        int l = it != begin(xs) ? prev(it)->first : -1;
        int r = next(it) != end(xs) ? next(it)->first : INT_MAX;
        if (l >= 0 && r < INT_MAX)
          erase(l, r);
        else if (l >= 0)
          erase(l, INT_MAX - l - 1);
        else if (r < INT_MAX)
          erase(-r, r);
        
        if (l >= 0)
          insert(l, x);
        else
          insert(-x, x);
        if (r < INT_MAX)
          insert(x, r);
        else
          insert(x, INT_MAX - x - 1);
      }
      present_cnt += ++present[type] == 1;
    }
  }
  
  for (int &i : ans)
    cout << i << '\n';
}

Compilation message

new_home.cpp: In member function 'void ST::insert(int, int, int, int, int, int)':
new_home.cpp:29:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   29 |       int tm = tl + tr >> 1;
      |                ~~~^~~~
new_home.cpp: In member function 'void ST::erase(int, int, int, int, int, int)':
new_home.cpp:40:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   40 |       int tm = tl + tr >> 1;
      |                ~~~^~~~
new_home.cpp: In member function 'int ST::qry(int, bool, int, int, int)':
new_home.cpp:54:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   54 |       int tm = tl + tr >> 1;
      |                ~~~^~~~
new_home.cpp: In lambda function:
new_home.cpp:100:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  100 |     int m = l + r + 1 >> 1;
      |             ~~~~~~^~~
new_home.cpp: In lambda function:
new_home.cpp:110:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  110 |     int m = l + r + 1 >> 1;
      |             ~~~~~~^~~
# Verdict Execution time Memory Grader output
1 Correct 46 ms 112980 KB Output is correct
2 Correct 24 ms 112988 KB Output is correct
3 Correct 23 ms 112984 KB Output is correct
4 Correct 23 ms 112988 KB Output is correct
5 Correct 24 ms 112988 KB Output is correct
6 Correct 24 ms 113244 KB Output is correct
7 Correct 25 ms 113244 KB Output is correct
8 Correct 26 ms 113392 KB Output is correct
9 Correct 24 ms 113244 KB Output is correct
10 Correct 25 ms 113292 KB Output is correct
11 Correct 24 ms 113244 KB Output is correct
12 Correct 25 ms 113048 KB Output is correct
13 Correct 23 ms 113240 KB Output is correct
14 Correct 24 ms 113244 KB Output is correct
15 Correct 24 ms 113244 KB Output is correct
16 Correct 24 ms 113240 KB Output is correct
17 Correct 26 ms 113244 KB Output is correct
18 Correct 24 ms 113240 KB Output is correct
19 Correct 24 ms 113244 KB Output is correct
20 Correct 24 ms 113240 KB Output is correct
21 Correct 24 ms 113236 KB Output is correct
22 Correct 26 ms 113244 KB Output is correct
23 Correct 25 ms 113240 KB Output is correct
24 Correct 25 ms 113240 KB Output is correct
25 Correct 24 ms 113240 KB Output is correct
26 Correct 25 ms 113244 KB Output is correct
27 Correct 24 ms 113244 KB Output is correct
28 Correct 27 ms 113248 KB Output is correct
29 Correct 24 ms 113244 KB Output is correct
30 Correct 24 ms 113244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 112980 KB Output is correct
2 Correct 24 ms 112988 KB Output is correct
3 Correct 23 ms 112984 KB Output is correct
4 Correct 23 ms 112988 KB Output is correct
5 Correct 24 ms 112988 KB Output is correct
6 Correct 24 ms 113244 KB Output is correct
7 Correct 25 ms 113244 KB Output is correct
8 Correct 26 ms 113392 KB Output is correct
9 Correct 24 ms 113244 KB Output is correct
10 Correct 25 ms 113292 KB Output is correct
11 Correct 24 ms 113244 KB Output is correct
12 Correct 25 ms 113048 KB Output is correct
13 Correct 23 ms 113240 KB Output is correct
14 Correct 24 ms 113244 KB Output is correct
15 Correct 24 ms 113244 KB Output is correct
16 Correct 24 ms 113240 KB Output is correct
17 Correct 26 ms 113244 KB Output is correct
18 Correct 24 ms 113240 KB Output is correct
19 Correct 24 ms 113244 KB Output is correct
20 Correct 24 ms 113240 KB Output is correct
21 Correct 24 ms 113236 KB Output is correct
22 Correct 26 ms 113244 KB Output is correct
23 Correct 25 ms 113240 KB Output is correct
24 Correct 25 ms 113240 KB Output is correct
25 Correct 24 ms 113240 KB Output is correct
26 Correct 25 ms 113244 KB Output is correct
27 Correct 24 ms 113244 KB Output is correct
28 Correct 27 ms 113248 KB Output is correct
29 Correct 24 ms 113244 KB Output is correct
30 Correct 24 ms 113244 KB Output is correct
31 Correct 1798 ms 161884 KB Output is correct
32 Correct 77 ms 120012 KB Output is correct
33 Correct 580 ms 128192 KB Output is correct
34 Correct 1520 ms 137776 KB Output is correct
35 Correct 1373 ms 152556 KB Output is correct
36 Correct 652 ms 135004 KB Output is correct
37 Correct 484 ms 121732 KB Output is correct
38 Correct 364 ms 121040 KB Output is correct
39 Correct 317 ms 120264 KB Output is correct
40 Correct 338 ms 120412 KB Output is correct
41 Correct 558 ms 120520 KB Output is correct
42 Correct 566 ms 120460 KB Output is correct
43 Correct 82 ms 121292 KB Output is correct
44 Correct 537 ms 121548 KB Output is correct
45 Correct 462 ms 121032 KB Output is correct
46 Correct 383 ms 119856 KB Output is correct
47 Correct 251 ms 120776 KB Output is correct
48 Correct 227 ms 120008 KB Output is correct
49 Correct 319 ms 119736 KB Output is correct
50 Correct 434 ms 119896 KB Output is correct
51 Correct 316 ms 120524 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 5021 ms 515508 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5097 ms 457644 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 46 ms 112980 KB Output is correct
2 Correct 24 ms 112988 KB Output is correct
3 Correct 23 ms 112984 KB Output is correct
4 Correct 23 ms 112988 KB Output is correct
5 Correct 24 ms 112988 KB Output is correct
6 Correct 24 ms 113244 KB Output is correct
7 Correct 25 ms 113244 KB Output is correct
8 Correct 26 ms 113392 KB Output is correct
9 Correct 24 ms 113244 KB Output is correct
10 Correct 25 ms 113292 KB Output is correct
11 Correct 24 ms 113244 KB Output is correct
12 Correct 25 ms 113048 KB Output is correct
13 Correct 23 ms 113240 KB Output is correct
14 Correct 24 ms 113244 KB Output is correct
15 Correct 24 ms 113244 KB Output is correct
16 Correct 24 ms 113240 KB Output is correct
17 Correct 26 ms 113244 KB Output is correct
18 Correct 24 ms 113240 KB Output is correct
19 Correct 24 ms 113244 KB Output is correct
20 Correct 24 ms 113240 KB Output is correct
21 Correct 24 ms 113236 KB Output is correct
22 Correct 26 ms 113244 KB Output is correct
23 Correct 25 ms 113240 KB Output is correct
24 Correct 25 ms 113240 KB Output is correct
25 Correct 24 ms 113240 KB Output is correct
26 Correct 25 ms 113244 KB Output is correct
27 Correct 24 ms 113244 KB Output is correct
28 Correct 27 ms 113248 KB Output is correct
29 Correct 24 ms 113244 KB Output is correct
30 Correct 24 ms 113244 KB Output is correct
31 Correct 1798 ms 161884 KB Output is correct
32 Correct 77 ms 120012 KB Output is correct
33 Correct 580 ms 128192 KB Output is correct
34 Correct 1520 ms 137776 KB Output is correct
35 Correct 1373 ms 152556 KB Output is correct
36 Correct 652 ms 135004 KB Output is correct
37 Correct 484 ms 121732 KB Output is correct
38 Correct 364 ms 121040 KB Output is correct
39 Correct 317 ms 120264 KB Output is correct
40 Correct 338 ms 120412 KB Output is correct
41 Correct 558 ms 120520 KB Output is correct
42 Correct 566 ms 120460 KB Output is correct
43 Correct 82 ms 121292 KB Output is correct
44 Correct 537 ms 121548 KB Output is correct
45 Correct 462 ms 121032 KB Output is correct
46 Correct 383 ms 119856 KB Output is correct
47 Correct 251 ms 120776 KB Output is correct
48 Correct 227 ms 120008 KB Output is correct
49 Correct 319 ms 119736 KB Output is correct
50 Correct 434 ms 119896 KB Output is correct
51 Correct 316 ms 120524 KB Output is correct
52 Correct 559 ms 171460 KB Output is correct
53 Correct 504 ms 140224 KB Output is correct
54 Correct 1631 ms 182668 KB Output is correct
55 Correct 637 ms 137712 KB Output is correct
56 Correct 616 ms 145972 KB Output is correct
57 Correct 624 ms 126192 KB Output is correct
58 Correct 657 ms 138660 KB Output is correct
59 Correct 675 ms 146904 KB Output is correct
60 Correct 632 ms 126960 KB Output is correct
61 Correct 85 ms 129220 KB Output is correct
62 Correct 587 ms 170492 KB Output is correct
63 Correct 1255 ms 169080 KB Output is correct
64 Correct 1464 ms 161332 KB Output is correct
65 Correct 1228 ms 133384 KB Output is correct
66 Correct 640 ms 121468 KB Output is correct
67 Correct 325 ms 122468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 112980 KB Output is correct
2 Correct 24 ms 112988 KB Output is correct
3 Correct 23 ms 112984 KB Output is correct
4 Correct 23 ms 112988 KB Output is correct
5 Correct 24 ms 112988 KB Output is correct
6 Correct 24 ms 113244 KB Output is correct
7 Correct 25 ms 113244 KB Output is correct
8 Correct 26 ms 113392 KB Output is correct
9 Correct 24 ms 113244 KB Output is correct
10 Correct 25 ms 113292 KB Output is correct
11 Correct 24 ms 113244 KB Output is correct
12 Correct 25 ms 113048 KB Output is correct
13 Correct 23 ms 113240 KB Output is correct
14 Correct 24 ms 113244 KB Output is correct
15 Correct 24 ms 113244 KB Output is correct
16 Correct 24 ms 113240 KB Output is correct
17 Correct 26 ms 113244 KB Output is correct
18 Correct 24 ms 113240 KB Output is correct
19 Correct 24 ms 113244 KB Output is correct
20 Correct 24 ms 113240 KB Output is correct
21 Correct 24 ms 113236 KB Output is correct
22 Correct 26 ms 113244 KB Output is correct
23 Correct 25 ms 113240 KB Output is correct
24 Correct 25 ms 113240 KB Output is correct
25 Correct 24 ms 113240 KB Output is correct
26 Correct 25 ms 113244 KB Output is correct
27 Correct 24 ms 113244 KB Output is correct
28 Correct 27 ms 113248 KB Output is correct
29 Correct 24 ms 113244 KB Output is correct
30 Correct 24 ms 113244 KB Output is correct
31 Correct 1798 ms 161884 KB Output is correct
32 Correct 77 ms 120012 KB Output is correct
33 Correct 580 ms 128192 KB Output is correct
34 Correct 1520 ms 137776 KB Output is correct
35 Correct 1373 ms 152556 KB Output is correct
36 Correct 652 ms 135004 KB Output is correct
37 Correct 484 ms 121732 KB Output is correct
38 Correct 364 ms 121040 KB Output is correct
39 Correct 317 ms 120264 KB Output is correct
40 Correct 338 ms 120412 KB Output is correct
41 Correct 558 ms 120520 KB Output is correct
42 Correct 566 ms 120460 KB Output is correct
43 Correct 82 ms 121292 KB Output is correct
44 Correct 537 ms 121548 KB Output is correct
45 Correct 462 ms 121032 KB Output is correct
46 Correct 383 ms 119856 KB Output is correct
47 Correct 251 ms 120776 KB Output is correct
48 Correct 227 ms 120008 KB Output is correct
49 Correct 319 ms 119736 KB Output is correct
50 Correct 434 ms 119896 KB Output is correct
51 Correct 316 ms 120524 KB Output is correct
52 Execution timed out 5021 ms 515508 KB Time limit exceeded
53 Halted 0 ms 0 KB -