Submission #49858

# Submission time Handle Problem Language Result Execution time Memory
49858 2018-06-03T21:04:50 Z zemen New Home (APIO18_new_home) C++17
5 / 100
5000 ms 261584 KB
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using oset = __gnu_pbds::tree<int, __gnu_pbds::null_type,
        std::less<int>,
        __gnu_pbds::rb_tree_tag,
        __gnu_pbds::tree_order_statistics_node_update>;
 
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
 
const int base = 1 << 19;
const int inf = 1e9;
oset t[base * 2];
vector<pair<int, int>> xc;
int k;
 
int tget(int l, int r, int v = 1, int cl = 0, int cr = base) {
  if (l <= cl && cr <= r) {
    return t[v].size() - t[v].order_of_key(r);
  }
  if (r <= cl || cr <= l) {
    return 0;
  }
  int cc = (cl + cr) / 2;
  return tget(l, r, v * 2, cl, cc) + tget(l, r, v * 2 + 1, cc, cr);
}
 
int get(int x) {
  int L = -1, R = inf / 2;
  map<pair<int, int>, int> bscache;
  bool ok = false;
  while (L + 1 < R) {
    int C = (L + R) / 2;
    int lb = lower_bound(xc.begin(), xc.end(), pair<int, int>{x - C, -1}) - xc.begin();
    int rb = upper_bound(xc.begin(), xc.end(), pair<int, int>{x + C, inf}) - xc.begin();
 
    auto q = pair<int, int>{lb, rb};
    int cnt;
    if (bscache.count(q)) {
      cnt = bscache[q];
    } else {
      bscache[q] = cnt = tget(lb, rb);
    }
 
    assert(cnt <= k);
    if (cnt >= k) {
      R = C;
      ok = true;
    } else {
      L = C;
    }
  }
  if (!ok) {
    return -1;
  } else {
    return R;
  }
}
 
void tput(int v, int val) {
  v += base;
  while (v >= 1) {
    int ss = -t[v].size();
    t[v].insert(val);
    ss += t[v].size();
    assert(ss == 1);
    v /= 2;
  }
}
 
void tdel(int v, int val) {
  v += base;
  while (v >= 1) {
    int ss = t[v].size();
    t[v].erase(val);
    ss -= t[v].size();
    assert(ss == 1);
    v /= 2;
  }
}
 
set<int> pos[base];
void put(int x, int t, int delta) {
  if (delta == -1) {
    auto it = pos[t].upper_bound(x);
    int val = it == pos[t].end() ? inf + t : *it;
    if (it != pos[t].begin()) {
      --it;
      tdel(*it, val);
      tput(*it, x);
    }
    pos[t].insert(x);
    tput(x, val);
  } else if (delta == 1) {
    assert(pos[t].count(x));
    auto it = pos[t].upper_bound(x);
    int val = it == pos[t].end() ? inf + t : *it;
    tdel(x, val);
    pos[t].erase(x);
    if (it != pos[t].begin()) {
      --it;
      tdel(*it, x);
      tput(*it, val);
    }
  } else {
    assert(false);
  }
}
 
signed main() {
#ifdef LOCAL
  assert(freopen("a.in", "r", stdin));
#endif
  struct Event {
    int tp, tm, x, t, id;
    bool operator<(const Event& e) const {
      if (tm != e.tm) {
        return tm < e.tm;
      }
      return tp < e.tp;
    }
  };
 
  int n, q;
  cin >> n >> k >> q;
  vector<Event> ev;
  for (int i = 0; i < n; ++i) {
    int x, t, a, b;
    cin >> x >> t >> a >> b;
    ev.push_back(Event{-1, a, x, t, i});
    ev.push_back(Event{1, b, x, t, i});
    xc.emplace_back(x, i);
  }
  sort(xc.begin(), xc.end());
  for (int i = 0; i < q; ++i) {
    int l, y;
    cin >> l >> y;
    ev.push_back(Event{0, y, l, -1, i});
  }
  sort(ev.begin(), ev.end());
  vector<int> res(q);
  for (auto& e : ev) {
    if (e.tp == 0) {
      res[e.id] = get(e.x);
    } else {
      auto sx = lower_bound(xc.begin(), xc.end(), pair<int, int>{e.x, e.id}) - xc.begin();
      put(sx, e.t, e.tp);
    }
  }
  for (int i = 0; i < q; ++i) {
    cout << res[i] << '\n';
  }
}
# Verdict Execution time Memory Grader output
1 Correct 125 ms 107000 KB Output is correct
2 Correct 131 ms 107196 KB Output is correct
3 Correct 120 ms 107196 KB Output is correct
4 Correct 118 ms 107196 KB Output is correct
5 Correct 129 ms 107368 KB Output is correct
6 Correct 136 ms 107368 KB Output is correct
7 Correct 141 ms 107368 KB Output is correct
8 Correct 186 ms 107512 KB Output is correct
9 Correct 122 ms 107512 KB Output is correct
10 Correct 129 ms 107512 KB Output is correct
11 Correct 128 ms 107512 KB Output is correct
12 Correct 135 ms 107512 KB Output is correct
13 Correct 132 ms 107512 KB Output is correct
14 Correct 126 ms 107512 KB Output is correct
15 Correct 125 ms 107624 KB Output is correct
16 Correct 135 ms 107624 KB Output is correct
17 Correct 145 ms 107624 KB Output is correct
18 Correct 131 ms 107624 KB Output is correct
19 Correct 132 ms 107624 KB Output is correct
20 Correct 127 ms 107624 KB Output is correct
21 Correct 130 ms 107704 KB Output is correct
22 Correct 124 ms 107704 KB Output is correct
23 Correct 126 ms 107704 KB Output is correct
24 Correct 138 ms 107712 KB Output is correct
25 Correct 124 ms 107712 KB Output is correct
26 Correct 131 ms 107712 KB Output is correct
27 Correct 177 ms 107712 KB Output is correct
28 Correct 145 ms 107712 KB Output is correct
29 Correct 130 ms 107712 KB Output is correct
30 Correct 131 ms 107712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 125 ms 107000 KB Output is correct
2 Correct 131 ms 107196 KB Output is correct
3 Correct 120 ms 107196 KB Output is correct
4 Correct 118 ms 107196 KB Output is correct
5 Correct 129 ms 107368 KB Output is correct
6 Correct 136 ms 107368 KB Output is correct
7 Correct 141 ms 107368 KB Output is correct
8 Correct 186 ms 107512 KB Output is correct
9 Correct 122 ms 107512 KB Output is correct
10 Correct 129 ms 107512 KB Output is correct
11 Correct 128 ms 107512 KB Output is correct
12 Correct 135 ms 107512 KB Output is correct
13 Correct 132 ms 107512 KB Output is correct
14 Correct 126 ms 107512 KB Output is correct
15 Correct 125 ms 107624 KB Output is correct
16 Correct 135 ms 107624 KB Output is correct
17 Correct 145 ms 107624 KB Output is correct
18 Correct 131 ms 107624 KB Output is correct
19 Correct 132 ms 107624 KB Output is correct
20 Correct 127 ms 107624 KB Output is correct
21 Correct 130 ms 107704 KB Output is correct
22 Correct 124 ms 107704 KB Output is correct
23 Correct 126 ms 107704 KB Output is correct
24 Correct 138 ms 107712 KB Output is correct
25 Correct 124 ms 107712 KB Output is correct
26 Correct 131 ms 107712 KB Output is correct
27 Correct 177 ms 107712 KB Output is correct
28 Correct 145 ms 107712 KB Output is correct
29 Correct 130 ms 107712 KB Output is correct
30 Correct 131 ms 107712 KB Output is correct
31 Execution timed out 5026 ms 170964 KB Time limit exceeded
32 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5046 ms 261584 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5054 ms 261584 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 125 ms 107000 KB Output is correct
2 Correct 131 ms 107196 KB Output is correct
3 Correct 120 ms 107196 KB Output is correct
4 Correct 118 ms 107196 KB Output is correct
5 Correct 129 ms 107368 KB Output is correct
6 Correct 136 ms 107368 KB Output is correct
7 Correct 141 ms 107368 KB Output is correct
8 Correct 186 ms 107512 KB Output is correct
9 Correct 122 ms 107512 KB Output is correct
10 Correct 129 ms 107512 KB Output is correct
11 Correct 128 ms 107512 KB Output is correct
12 Correct 135 ms 107512 KB Output is correct
13 Correct 132 ms 107512 KB Output is correct
14 Correct 126 ms 107512 KB Output is correct
15 Correct 125 ms 107624 KB Output is correct
16 Correct 135 ms 107624 KB Output is correct
17 Correct 145 ms 107624 KB Output is correct
18 Correct 131 ms 107624 KB Output is correct
19 Correct 132 ms 107624 KB Output is correct
20 Correct 127 ms 107624 KB Output is correct
21 Correct 130 ms 107704 KB Output is correct
22 Correct 124 ms 107704 KB Output is correct
23 Correct 126 ms 107704 KB Output is correct
24 Correct 138 ms 107712 KB Output is correct
25 Correct 124 ms 107712 KB Output is correct
26 Correct 131 ms 107712 KB Output is correct
27 Correct 177 ms 107712 KB Output is correct
28 Correct 145 ms 107712 KB Output is correct
29 Correct 130 ms 107712 KB Output is correct
30 Correct 131 ms 107712 KB Output is correct
31 Execution timed out 5026 ms 170964 KB Time limit exceeded
32 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 125 ms 107000 KB Output is correct
2 Correct 131 ms 107196 KB Output is correct
3 Correct 120 ms 107196 KB Output is correct
4 Correct 118 ms 107196 KB Output is correct
5 Correct 129 ms 107368 KB Output is correct
6 Correct 136 ms 107368 KB Output is correct
7 Correct 141 ms 107368 KB Output is correct
8 Correct 186 ms 107512 KB Output is correct
9 Correct 122 ms 107512 KB Output is correct
10 Correct 129 ms 107512 KB Output is correct
11 Correct 128 ms 107512 KB Output is correct
12 Correct 135 ms 107512 KB Output is correct
13 Correct 132 ms 107512 KB Output is correct
14 Correct 126 ms 107512 KB Output is correct
15 Correct 125 ms 107624 KB Output is correct
16 Correct 135 ms 107624 KB Output is correct
17 Correct 145 ms 107624 KB Output is correct
18 Correct 131 ms 107624 KB Output is correct
19 Correct 132 ms 107624 KB Output is correct
20 Correct 127 ms 107624 KB Output is correct
21 Correct 130 ms 107704 KB Output is correct
22 Correct 124 ms 107704 KB Output is correct
23 Correct 126 ms 107704 KB Output is correct
24 Correct 138 ms 107712 KB Output is correct
25 Correct 124 ms 107712 KB Output is correct
26 Correct 131 ms 107712 KB Output is correct
27 Correct 177 ms 107712 KB Output is correct
28 Correct 145 ms 107712 KB Output is correct
29 Correct 130 ms 107712 KB Output is correct
30 Correct 131 ms 107712 KB Output is correct
31 Execution timed out 5026 ms 170964 KB Time limit exceeded
32 Halted 0 ms 0 KB -