Submission #968264

# Submission time Handle Problem Language Result Execution time Memory
968264 2024-04-23T09:07:48 Z kilkuwu New Home (APIO18_new_home) C++17
100 / 100
3113 ms 134108 KB
#include <bits/stdc++.h>

#define nl '\n'

#ifdef LOCAL
#include "template/debug.hpp"
#else
#define dbg(...) ;
#define timer(...) ;
#endif

struct SegmentTree {
  int n;
  std::vector<int> t;

  SegmentTree(int _n) : n(_n), t(2 * n, -1) {}

  void update(int p, int v) {
    for (t[p += n] = v; p > 1; p >>= 1) {
      t[p >> 1] = std::max(t[p], t[p ^ 1]);
    }
  }

  int query(int l, int r) {
    int ans = -1;
    for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
      if (l & 1) ans = std::max(ans, t[l++]);
      if (r & 1) ans = std::max(ans, t[--r]);
    }
    return ans;
  }
};

struct Store {
  int x, t, a, b;

  bool operator<(const Store& rhs) const {
    return x < rhs.x;
  }  
};

struct Query {
  int l, y;
};

struct Event {
  int time, id;
  
  bool operator<(const Event& rhs) const {
    return std::make_pair(time, id) < std::make_pair(rhs.time, rhs.id);
  }
};

bool operator<(const Store& s, const int& x) {
  return s.x < x;
}

bool operator<(const int& x, const Store& s) {
  return x < s.x;
}

signed main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);

  int n, k, q;
  std::cin >> n >> k >> q;

  std::vector<Store> stores(n + 1);
  stores[0].x = -1;
  for (int i = 0; i < n; i++) {
    int x, t, a, b;
    std::cin >> x >> t >> a >> b;
    --t;
    stores[i + 1] = {x, t, a, b};
  }
  std::sort(stores.begin() + 1, stores.end());

  std::vector<int> ans(q);
  std::vector<Query> queries(q);
  for (int i = 0; i < q; i++) {
    std::cin >> queries[i].l >> queries[i].y;
  }

  std::vector<Event> events;
  for (int i = 1; i <= n; i++) {
    events.push_back({stores[i].a, i});
    events.push_back({stores[i].b + 1, i});
  }

  for (int i = 0; i < q; i++) {
    events.push_back({queries[i].y, i + n + 1});
  }

  std::sort(events.begin(), events.end());
  std::vector<std::set<int>> pos(k);

  std::vector<std::multiset<int>> at(n + 1);

  for (int i = 0; i < k; i++) {
    at[0].insert(n + 1);
    pos[i].insert(0);
    pos[i].insert(n + 1);
  }

  dbg("goes here");

  SegmentTree tree(n + 1);
  tree.update(0, n + 1);

  auto rem_at = [&](int id, int v) {
    at[id].erase(at[id].find(v));
    tree.update(id, at[id].empty() ? -1 : *at[id].rbegin());
  };

  auto add_at = [&](int id, int v) {
    at[id].insert(v);
    tree.update(id, at[id].empty() ? -1 : *at[id].rbegin());
  };

  for (auto [time, id] : events) {
    dbg(time, id);
    if (id <= n) {
      auto [x, t, a, b] = stores[id];
      if (time == a) {
        dbg("goes insert", time, id);
        dbg(pos[t], t);
        auto it = pos[t].insert(id).first;
        auto pre = std::prev(it);
        auto nxt = std::next(it);

        dbg(*it, *pre, *nxt);

        rem_at(*pre, *nxt);
        add_at(*pre, *it);
        add_at(*it, *nxt);
      } else {
        auto it = pos[t].find(id);
        auto pre = std::prev(it);
        auto nxt = std::next(it);
        rem_at(*it, *nxt);
        rem_at(*pre, *it);
        add_at(*pre, *nxt);
        pos[t].erase(it);
      }
    } else {
      id -= n + 1;

      int res = -1;
      int lb = 0, rb = 1e8;

      while (lb <= rb) {
        int mb = (lb + rb) / 2;
        int ql = std::lower_bound(stores.begin() + 1, stores.end(), queries[id].l - mb) - stores.begin();
        int qr = std::upper_bound(stores.begin() + 1, stores.end(), queries[id].l + mb) - stores.begin();

        if (tree.query(0, ql) < qr) {
          res = mb;
          rb = mb - 1;
        } else {
          lb = mb + 1;
        }
      }

      ans[id] = res;
    }
  }

  for (int i = 0; i < q; i++) {
    std::cout << ans[i] << nl;
  }
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 2 ms 484 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 600 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 2 ms 348 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 536 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 1 ms 600 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 1 ms 360 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 2 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 344 KB Output is correct
30 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 2 ms 484 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 600 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 2 ms 348 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 536 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 1 ms 600 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 1 ms 360 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 2 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 344 KB Output is correct
30 Correct 1 ms 468 KB Output is correct
31 Correct 383 ms 15816 KB Output is correct
32 Correct 114 ms 10188 KB Output is correct
33 Correct 409 ms 12000 KB Output is correct
34 Correct 326 ms 12232 KB Output is correct
35 Correct 389 ms 15816 KB Output is correct
36 Correct 376 ms 15556 KB Output is correct
37 Correct 351 ms 10364 KB Output is correct
38 Correct 341 ms 10320 KB Output is correct
39 Correct 314 ms 9948 KB Output is correct
40 Correct 310 ms 9932 KB Output is correct
41 Correct 224 ms 10336 KB Output is correct
42 Correct 169 ms 10212 KB Output is correct
43 Correct 100 ms 15376 KB Output is correct
44 Correct 205 ms 10344 KB Output is correct
45 Correct 214 ms 10192 KB Output is correct
46 Correct 263 ms 10168 KB Output is correct
47 Correct 189 ms 9620 KB Output is correct
48 Correct 220 ms 9420 KB Output is correct
49 Correct 236 ms 9916 KB Output is correct
50 Correct 205 ms 9932 KB Output is correct
51 Correct 277 ms 9660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1305 ms 74860 KB Output is correct
2 Correct 2193 ms 74860 KB Output is correct
3 Correct 1370 ms 133764 KB Output is correct
4 Correct 1398 ms 95744 KB Output is correct
5 Correct 2223 ms 75036 KB Output is correct
6 Correct 2179 ms 74376 KB Output is correct
7 Correct 1310 ms 134016 KB Output is correct
8 Correct 1490 ms 95524 KB Output is correct
9 Correct 1665 ms 82996 KB Output is correct
10 Correct 2290 ms 75952 KB Output is correct
11 Correct 1835 ms 73384 KB Output is correct
12 Correct 1837 ms 74868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2194 ms 64476 KB Output is correct
2 Correct 746 ms 65988 KB Output is correct
3 Correct 2759 ms 74892 KB Output is correct
4 Correct 1699 ms 131816 KB Output is correct
5 Correct 1465 ms 86716 KB Output is correct
6 Correct 1448 ms 93208 KB Output is correct
7 Correct 2796 ms 74288 KB Output is correct
8 Correct 2720 ms 74204 KB Output is correct
9 Correct 1729 ms 133136 KB Output is correct
10 Correct 1773 ms 90104 KB Output is correct
11 Correct 1992 ms 79808 KB Output is correct
12 Correct 2733 ms 74976 KB Output is correct
13 Correct 1642 ms 72120 KB Output is correct
14 Correct 1632 ms 72060 KB Output is correct
15 Correct 1937 ms 72940 KB Output is correct
16 Correct 2209 ms 75580 KB Output is correct
17 Correct 1971 ms 72396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 2 ms 484 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 600 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 2 ms 348 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 536 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 1 ms 600 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 1 ms 360 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 2 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 344 KB Output is correct
30 Correct 1 ms 468 KB Output is correct
31 Correct 383 ms 15816 KB Output is correct
32 Correct 114 ms 10188 KB Output is correct
33 Correct 409 ms 12000 KB Output is correct
34 Correct 326 ms 12232 KB Output is correct
35 Correct 389 ms 15816 KB Output is correct
36 Correct 376 ms 15556 KB Output is correct
37 Correct 351 ms 10364 KB Output is correct
38 Correct 341 ms 10320 KB Output is correct
39 Correct 314 ms 9948 KB Output is correct
40 Correct 310 ms 9932 KB Output is correct
41 Correct 224 ms 10336 KB Output is correct
42 Correct 169 ms 10212 KB Output is correct
43 Correct 100 ms 15376 KB Output is correct
44 Correct 205 ms 10344 KB Output is correct
45 Correct 214 ms 10192 KB Output is correct
46 Correct 263 ms 10168 KB Output is correct
47 Correct 189 ms 9620 KB Output is correct
48 Correct 220 ms 9420 KB Output is correct
49 Correct 236 ms 9916 KB Output is correct
50 Correct 205 ms 9932 KB Output is correct
51 Correct 277 ms 9660 KB Output is correct
52 Correct 256 ms 26780 KB Output is correct
53 Correct 282 ms 23392 KB Output is correct
54 Correct 203 ms 19248 KB Output is correct
55 Correct 219 ms 16056 KB Output is correct
56 Correct 247 ms 18708 KB Output is correct
57 Correct 210 ms 12080 KB Output is correct
58 Correct 209 ms 15816 KB Output is correct
59 Correct 205 ms 18632 KB Output is correct
60 Correct 186 ms 11976 KB Output is correct
61 Correct 131 ms 26784 KB Output is correct
62 Correct 273 ms 26828 KB Output is correct
63 Correct 221 ms 20264 KB Output is correct
64 Correct 246 ms 17668 KB Output is correct
65 Correct 191 ms 12588 KB Output is correct
66 Correct 205 ms 10444 KB Output is correct
67 Correct 126 ms 10588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 2 ms 484 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 600 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 2 ms 348 KB Output is correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 536 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 1 ms 600 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 1 ms 360 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 2 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 344 KB Output is correct
30 Correct 1 ms 468 KB Output is correct
31 Correct 383 ms 15816 KB Output is correct
32 Correct 114 ms 10188 KB Output is correct
33 Correct 409 ms 12000 KB Output is correct
34 Correct 326 ms 12232 KB Output is correct
35 Correct 389 ms 15816 KB Output is correct
36 Correct 376 ms 15556 KB Output is correct
37 Correct 351 ms 10364 KB Output is correct
38 Correct 341 ms 10320 KB Output is correct
39 Correct 314 ms 9948 KB Output is correct
40 Correct 310 ms 9932 KB Output is correct
41 Correct 224 ms 10336 KB Output is correct
42 Correct 169 ms 10212 KB Output is correct
43 Correct 100 ms 15376 KB Output is correct
44 Correct 205 ms 10344 KB Output is correct
45 Correct 214 ms 10192 KB Output is correct
46 Correct 263 ms 10168 KB Output is correct
47 Correct 189 ms 9620 KB Output is correct
48 Correct 220 ms 9420 KB Output is correct
49 Correct 236 ms 9916 KB Output is correct
50 Correct 205 ms 9932 KB Output is correct
51 Correct 277 ms 9660 KB Output is correct
52 Correct 1305 ms 74860 KB Output is correct
53 Correct 2193 ms 74860 KB Output is correct
54 Correct 1370 ms 133764 KB Output is correct
55 Correct 1398 ms 95744 KB Output is correct
56 Correct 2223 ms 75036 KB Output is correct
57 Correct 2179 ms 74376 KB Output is correct
58 Correct 1310 ms 134016 KB Output is correct
59 Correct 1490 ms 95524 KB Output is correct
60 Correct 1665 ms 82996 KB Output is correct
61 Correct 2290 ms 75952 KB Output is correct
62 Correct 1835 ms 73384 KB Output is correct
63 Correct 1837 ms 74868 KB Output is correct
64 Correct 2194 ms 64476 KB Output is correct
65 Correct 746 ms 65988 KB Output is correct
66 Correct 2759 ms 74892 KB Output is correct
67 Correct 1699 ms 131816 KB Output is correct
68 Correct 1465 ms 86716 KB Output is correct
69 Correct 1448 ms 93208 KB Output is correct
70 Correct 2796 ms 74288 KB Output is correct
71 Correct 2720 ms 74204 KB Output is correct
72 Correct 1729 ms 133136 KB Output is correct
73 Correct 1773 ms 90104 KB Output is correct
74 Correct 1992 ms 79808 KB Output is correct
75 Correct 2733 ms 74976 KB Output is correct
76 Correct 1642 ms 72120 KB Output is correct
77 Correct 1632 ms 72060 KB Output is correct
78 Correct 1937 ms 72940 KB Output is correct
79 Correct 2209 ms 75580 KB Output is correct
80 Correct 1971 ms 72396 KB Output is correct
81 Correct 256 ms 26780 KB Output is correct
82 Correct 282 ms 23392 KB Output is correct
83 Correct 203 ms 19248 KB Output is correct
84 Correct 219 ms 16056 KB Output is correct
85 Correct 247 ms 18708 KB Output is correct
86 Correct 210 ms 12080 KB Output is correct
87 Correct 209 ms 15816 KB Output is correct
88 Correct 205 ms 18632 KB Output is correct
89 Correct 186 ms 11976 KB Output is correct
90 Correct 131 ms 26784 KB Output is correct
91 Correct 273 ms 26828 KB Output is correct
92 Correct 221 ms 20264 KB Output is correct
93 Correct 246 ms 17668 KB Output is correct
94 Correct 191 ms 12588 KB Output is correct
95 Correct 205 ms 10444 KB Output is correct
96 Correct 126 ms 10588 KB Output is correct
97 Correct 2080 ms 133276 KB Output is correct
98 Correct 698 ms 49488 KB Output is correct
99 Correct 2781 ms 58668 KB Output is correct
100 Correct 1851 ms 115440 KB Output is correct
101 Correct 1773 ms 95240 KB Output is correct
102 Correct 3113 ms 76348 KB Output is correct
103 Correct 2143 ms 50860 KB Output is correct
104 Correct 2244 ms 49752 KB Output is correct
105 Correct 1943 ms 48612 KB Output is correct
106 Correct 1999 ms 48696 KB Output is correct
107 Correct 1823 ms 78400 KB Output is correct
108 Correct 1888 ms 92348 KB Output is correct
109 Correct 1467 ms 59380 KB Output is correct
110 Correct 1507 ms 78032 KB Output is correct
111 Correct 1655 ms 92200 KB Output is correct
112 Correct 1328 ms 58364 KB Output is correct
113 Correct 742 ms 131964 KB Output is correct
114 Correct 2031 ms 134108 KB Output is correct
115 Correct 1915 ms 102064 KB Output is correct
116 Correct 1906 ms 87612 KB Output is correct
117 Correct 1618 ms 62312 KB Output is correct
118 Correct 1235 ms 50796 KB Output is correct
119 Correct 1050 ms 51628 KB Output is correct
120 Correct 1048 ms 44988 KB Output is correct
121 Correct 1331 ms 47504 KB Output is correct
122 Correct 1445 ms 47472 KB Output is correct
123 Correct 1448 ms 48404 KB Output is correct
124 Correct 1321 ms 49336 KB Output is correct
125 Correct 1721 ms 47640 KB Output is correct
126 Correct 1280 ms 49864 KB Output is correct