Submission #265283

# Submission time Handle Problem Language Result Execution time Memory
265283 2020-08-14T14:48:41 Z extraterrestrial New Home (APIO18_new_home) C++14
45 / 100
2310 ms 133624 KB
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,avx2")
#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
using namespace std;
#define F first
#define S second
#define pb push_back  
#define all(x) (x).begin(), (x).end()
#define SZ(x) (int)(x).size()
//#define int ll    

#define FAST_ALLOCATOR_MEMORY 3e8 
#ifdef FAST_ALLOCATOR_MEMORY
int allocator_pos = 0;
char allocator_memory[(int)FAST_ALLOCATOR_MEMORY];
inline void * operator new ( size_t n ) {
  char *res = allocator_memory + allocator_pos;
  allocator_pos += n;
  assert(allocator_pos <= (int)FAST_ALLOCATOR_MEMORY);
  return (void *)res;
}
inline void operator delete ( void * ) noexcept { }
//inline void * operator new [] ( size_t ) { assert(0); }
//inline void operator delete [] ( void * ) { assert(0); }
#endif
 
const int N = 3e5 + 10;
vector<pair<int, int>> add[2 * N], del[2 * N], que[2 * N];
multiset<int> flex[N], have[N];
int ans[N];
 
const int M = 5e6 + 10;
int mn[M], mx[M];
void build_min(int v, int l, int r) {
  mn[v] = 1e9;
  if (l == r) {
    return;
  }
  int mid = (l + r) / 2;
  build_min(2 * v, l, mid);
  build_min(2 * v + 1, mid + 1, r);
}
 
void build_max(int v, int l, int r) {
  mx[v] = 0;
  if (l == r) {
    return;
  }
  int mid = (l + r) / 2;
  build_max(2 * v, l, mid);
  build_max(2 * v + 1, mid + 1, r);
}
 
void update_min(int v, int l, int r, int a, int b, int vl) {
  if (l > b || r < a) {
    return;
  }
  if (l >= a && r <= b) {
    mn[v] = min(mn[v], vl);
    return;
  }
  int mid = (l + r) / 2;
  update_min(2 * v, l, mid, a, b, vl);
  update_min(2 * v + 1, mid + 1, r, a, b, vl);
}
 
void update_max(int v, int l, int r, int a, int b, int vl) {
  if (l > b || r < a) {
    return;
  }
  if (l >= a && r <= b) {
    mx[v] = max(mx[v], vl);
    return;
  }
  int mid = (l + r) / 2;
  update_max(2 * v, l, mid, a, b, vl);
  update_max(2 * v + 1, mid + 1, r, a, b, vl); 
}
 
int get_min(int v, int l, int r, int need) {
  if (l == r) {
    return mn[v];
  }
  int mid = (l + r) / 2;
  if (need <= mid) {
    return min(mn[v], get_min(2 * v, l, mid, need));
  }
  return min(mn[v], get_min(2 * v + 1, mid + 1, r, need));
}
 
int get_max(int v, int l, int r, int need) {
  if (l == r) {
    return mx[v];
  }
  int mid = (l + r) / 2;
  if (need <= mid) {
    return max(mx[v], get_max(2 * v, l, mid, need));
  }
  return max(mx[v], get_max(2 * v + 1, mid + 1, r, need));
}
 
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0); 
  cout.tie(0);
  int n, k, q;
  cin >> n >> k >> q;
  vector<int> x(n), type(n), l(n), r(n), interesting, coord;
  bool check = false;
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> type[i] >> l[i] >> r[i];
    if (l[i] != 1) {
      check = true;
    }
    interesting.pb(l[i]);
    interesting.pb(r[i]);
    coord.pb(x[i]);
    type[i]--;
  }
  vector<int> pos(q), tt(q);
  for (int i = 0; i < q; i++) {
    cin >> pos[i] >> tt[i];
    interesting.pb(tt[i]);
    coord.pb(pos[i]);
  }
  if (((n <= 400 && q <= 400) || k <= 400) && check) {
    sort(all(interesting));
    interesting.resize(unique(all(interesting)) - interesting.begin());
    for (int i = 0; i < n; i++) {
      l[i] = upper_bound(all(interesting), l[i]) - interesting.begin();
      r[i] = upper_bound(all(interesting), r[i]) - interesting.begin();
    }
    for (int i = 0; i < q; i++) {
      tt[i] = upper_bound(all(interesting), tt[i]) - interesting.begin();
    }
    int max_time = SZ(interesting);
   
    for (int i = 0; i < n; i++) {
      add[l[i]].pb({x[i], type[i]});
      del[r[i]].pb({x[i], type[i]});
    }
    for (int i = 0; i < q; i++) {
      que[tt[i]].pb({pos[i], i});
    }
    for (int cur_time = 1; cur_time <= max_time; cur_time++) {
      for (auto it : add[cur_time]) {
        have[it.S].insert(it.F);
      }
      for (auto it : que[cur_time]) {
        for (int i = 0; i < k; i++) {
          int rez = 1e9;
          auto kek = have[i].upper_bound(it.F);
          if (kek != have[i].end()) {
            rez = min(rez, *kek - it.F);
          }
          if (kek != have[i].begin()) {
            kek--;
            rez = min(rez, it.F - *kek);
          }
          ans[it.S] = max(ans[it.S], rez);
        }
      }
      for (auto it : del[cur_time]) {
        have[it.S].erase(have[it.S].find(it.F));
      }
    }
    for (int i = 0; i < q; i++) {
      if (ans[i] == 1e9) {
        cout << -1 << '\n';
      }
      else {
        cout << ans[i] << '\n';
      }
    }
    exit(0);
  }
 
  sort(all(coord));
  coord.resize(unique(all(coord)) - coord.begin());
  /*for (int &x : coord) {
    cerr << x << ' ';
  }
  cerr << '\n';*/
  build_min(1, 1, SZ(coord));
  build_max(1, 1, SZ(coord));
  for (int i = 0; i < n; i++) {
    x[i] = upper_bound(all(coord), x[i]) - coord.begin();
    flex[type[i]].insert(x[i]);
  }
  for (int i = 0; i < q; i++) {
    pos[i] = upper_bound(all(coord), pos[i]) - coord.begin();
  }
 
  sort(all(interesting));
  interesting.resize(unique(all(interesting)) - interesting.begin());
  for (int i = 0; i < n; i++) {
    l[i] = upper_bound(all(interesting), l[i]) - interesting.begin();
    r[i] = upper_bound(all(interesting), r[i]) - interesting.begin();
  }
  for (int i = 0; i < q; i++) {
    tt[i] = upper_bound(all(interesting), tt[i]) - interesting.begin();
  }
  int max_time = SZ(interesting);
  bool have_empty = false;
  for (int cur_type = 0; cur_type < k; cur_type++) {
    if (flex[cur_type].empty()) {
      have_empty = true;
      break;
    }
    update_max(1, 1, SZ(coord), 1, *flex[cur_type].begin(), *flex[cur_type].begin());
    update_min(1, 1, SZ(coord), *flex[cur_type].rbegin(), SZ(coord), *flex[cur_type].rbegin());
    auto it = flex[cur_type].begin();
    for (int pos = 0; pos + 1 < SZ(flex[cur_type]); pos++) {
      auto it2 = it;
      it2++;
      int l = *it, r = *it2;
      while (r - l > 1) {
        int mid = (l + r) / 2;
        if (coord[mid - 1] - coord[*it - 1] <= coord[*it2 - 1] - coord[mid - 1]) {
          l = mid;
        }
        else {
          r = mid;
        }
      }
      update_min(1, 1, SZ(coord), *it, l, *it);
      update_max(1, 1, SZ(coord), l + 1, *it2, *it2);
      it++;
    }
  }
  for (int i = 0; i < n; i++) {
    //add[l[i]].pb({x[i], type[i]});
    del[r[i]].pb({x[i], type[i]});
  }
  for (int i = 0; i < q; i++) {
    que[tt[i]].pb({pos[i], i});
  }
  for (int cur_time = 1; cur_time <= max_time; cur_time++) {
    /*for (auto it : add[cur_time]) {
      have[it.S].insert(it.F);
    }*/
    for (auto it : que[cur_time]) {
      if (have_empty) {
        ans[it.S] = 1e9;
        continue;
      }
      int lpos = get_min(1, 1, SZ(coord), it.F), rpos = get_max(1, 1, SZ(coord), it.F);
      if (lpos <= it.F) {
        ans[it.S] = max(ans[it.S], coord[it.F - 1] - coord[lpos - 1]);
      }
      if (rpos >= it.F) {
        ans[it.S] = max(ans[it.S], coord[rpos - 1] - coord[it.F - 1]);
      } 
    }
    for (auto cpar : del[cur_time]) {
      flex[cpar.S].erase(flex[cpar.S].find(cpar.F));
      if (flex[cpar.S].find(cpar.F) != flex[cpar.S].end()) {
        continue;
      }
      if (flex[cpar.S].empty()) {
        have_empty = true;
        break;
      }
      auto it2 = flex[cpar.S].upper_bound(cpar.F);
      if (it2 == flex[cpar.S].end()) {
        update_min(1, 1, SZ(coord), *flex[cpar.S].rbegin(), SZ(coord), *flex[cpar.S].rbegin());
      } 
      else if (it2 == flex[cpar.S].begin()) {
        update_max(1, 1, SZ(coord), 1, *flex[cpar.S].begin(), *flex[cpar.S].begin());
      } 
      else {
        auto it = it2;
        it--;
        int l = *it, r = *it2;
        while (r - l > 1) {
          int mid = (l + r) / 2;
          if (coord[mid - 1] - coord[*it - 1] <= coord[*it2 - 1] - coord[mid - 1]) {
            l = mid;
          }
          else {
            r = mid;
          }
        }
        update_min(1, 1, SZ(coord), *it, l, *it);
        update_max(1, 1, SZ(coord), l + 1, *it2, *it2);   
      }    
    }
  }
  for (int i = 0; i < q; i++) {
    if (ans[i] == 1e9) {
      cout << -1 << '\n';
    }
    else {
      cout << ans[i] << '\n';
    }
  }
}
# Verdict Execution time Memory Grader output
1 Correct 49 ms 70776 KB Output is correct
2 Correct 44 ms 70780 KB Output is correct
3 Correct 45 ms 70776 KB Output is correct
4 Correct 45 ms 70776 KB Output is correct
5 Correct 45 ms 71032 KB Output is correct
6 Correct 45 ms 70872 KB Output is correct
7 Correct 46 ms 70904 KB Output is correct
8 Correct 45 ms 70904 KB Output is correct
9 Correct 46 ms 70904 KB Output is correct
10 Correct 45 ms 70904 KB Output is correct
11 Correct 51 ms 70904 KB Output is correct
12 Correct 47 ms 70904 KB Output is correct
13 Correct 45 ms 70904 KB Output is correct
14 Correct 45 ms 70904 KB Output is correct
15 Correct 45 ms 70904 KB Output is correct
16 Correct 45 ms 70912 KB Output is correct
17 Correct 45 ms 70912 KB Output is correct
18 Correct 46 ms 70904 KB Output is correct
19 Correct 45 ms 70904 KB Output is correct
20 Correct 46 ms 70904 KB Output is correct
21 Correct 46 ms 70904 KB Output is correct
22 Correct 46 ms 70836 KB Output is correct
23 Correct 45 ms 70904 KB Output is correct
24 Correct 45 ms 70904 KB Output is correct
25 Correct 45 ms 70904 KB Output is correct
26 Correct 45 ms 70904 KB Output is correct
27 Correct 46 ms 70904 KB Output is correct
28 Correct 46 ms 70904 KB Output is correct
29 Correct 46 ms 70904 KB Output is correct
30 Correct 46 ms 70888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 49 ms 70776 KB Output is correct
2 Correct 44 ms 70780 KB Output is correct
3 Correct 45 ms 70776 KB Output is correct
4 Correct 45 ms 70776 KB Output is correct
5 Correct 45 ms 71032 KB Output is correct
6 Correct 45 ms 70872 KB Output is correct
7 Correct 46 ms 70904 KB Output is correct
8 Correct 45 ms 70904 KB Output is correct
9 Correct 46 ms 70904 KB Output is correct
10 Correct 45 ms 70904 KB Output is correct
11 Correct 51 ms 70904 KB Output is correct
12 Correct 47 ms 70904 KB Output is correct
13 Correct 45 ms 70904 KB Output is correct
14 Correct 45 ms 70904 KB Output is correct
15 Correct 45 ms 70904 KB Output is correct
16 Correct 45 ms 70912 KB Output is correct
17 Correct 45 ms 70912 KB Output is correct
18 Correct 46 ms 70904 KB Output is correct
19 Correct 45 ms 70904 KB Output is correct
20 Correct 46 ms 70904 KB Output is correct
21 Correct 46 ms 70904 KB Output is correct
22 Correct 46 ms 70836 KB Output is correct
23 Correct 45 ms 70904 KB Output is correct
24 Correct 45 ms 70904 KB Output is correct
25 Correct 45 ms 70904 KB Output is correct
26 Correct 45 ms 70904 KB Output is correct
27 Correct 46 ms 70904 KB Output is correct
28 Correct 46 ms 70904 KB Output is correct
29 Correct 46 ms 70904 KB Output is correct
30 Correct 46 ms 70888 KB Output is correct
31 Correct 2208 ms 79452 KB Output is correct
32 Correct 174 ms 80856 KB Output is correct
33 Correct 292 ms 79352 KB Output is correct
34 Correct 1802 ms 79588 KB Output is correct
35 Correct 1071 ms 79432 KB Output is correct
36 Correct 299 ms 79352 KB Output is correct
37 Correct 254 ms 79352 KB Output is correct
38 Correct 180 ms 79228 KB Output is correct
39 Correct 163 ms 79352 KB Output is correct
40 Correct 162 ms 79224 KB Output is correct
41 Correct 366 ms 79544 KB Output is correct
42 Correct 450 ms 79224 KB Output is correct
43 Correct 114 ms 80120 KB Output is correct
44 Correct 320 ms 79452 KB Output is correct
45 Correct 207 ms 79480 KB Output is correct
46 Correct 173 ms 79480 KB Output is correct
47 Correct 135 ms 78968 KB Output is correct
48 Correct 132 ms 78968 KB Output is correct
49 Correct 151 ms 79144 KB Output is correct
50 Correct 286 ms 79352 KB Output is correct
51 Correct 152 ms 79252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1444 ms 132856 KB Output is correct
2 Correct 1762 ms 131668 KB Output is correct
3 Correct 1129 ms 133056 KB Output is correct
4 Correct 1379 ms 132856 KB Output is correct
5 Correct 1745 ms 131240 KB Output is correct
6 Correct 1786 ms 131592 KB Output is correct
7 Correct 1049 ms 132884 KB Output is correct
8 Correct 1327 ms 133020 KB Output is correct
9 Correct 1363 ms 132984 KB Output is correct
10 Correct 1224 ms 132216 KB Output is correct
11 Correct 1048 ms 132856 KB Output is correct
12 Correct 1262 ms 133624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2310 ms 128316 KB Output is correct
2 Correct 555 ms 115556 KB Output is correct
3 Correct 2107 ms 127988 KB Output is correct
4 Correct 1204 ms 127224 KB Output is correct
5 Correct 1967 ms 127256 KB Output is correct
6 Correct 1678 ms 127136 KB Output is correct
7 Correct 2052 ms 127556 KB Output is correct
8 Correct 2105 ms 127780 KB Output is correct
9 Correct 1265 ms 128432 KB Output is correct
10 Correct 1880 ms 128444 KB Output is correct
11 Correct 2135 ms 128816 KB Output is correct
12 Correct 2121 ms 128248 KB Output is correct
13 Correct 1057 ms 127476 KB Output is correct
14 Correct 996 ms 127096 KB Output is correct
15 Correct 1186 ms 128100 KB Output is correct
16 Correct 1377 ms 128660 KB Output is correct
17 Correct 1226 ms 127860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 49 ms 70776 KB Output is correct
2 Correct 44 ms 70780 KB Output is correct
3 Correct 45 ms 70776 KB Output is correct
4 Correct 45 ms 70776 KB Output is correct
5 Correct 45 ms 71032 KB Output is correct
6 Correct 45 ms 70872 KB Output is correct
7 Correct 46 ms 70904 KB Output is correct
8 Correct 45 ms 70904 KB Output is correct
9 Correct 46 ms 70904 KB Output is correct
10 Correct 45 ms 70904 KB Output is correct
11 Correct 51 ms 70904 KB Output is correct
12 Correct 47 ms 70904 KB Output is correct
13 Correct 45 ms 70904 KB Output is correct
14 Correct 45 ms 70904 KB Output is correct
15 Correct 45 ms 70904 KB Output is correct
16 Correct 45 ms 70912 KB Output is correct
17 Correct 45 ms 70912 KB Output is correct
18 Correct 46 ms 70904 KB Output is correct
19 Correct 45 ms 70904 KB Output is correct
20 Correct 46 ms 70904 KB Output is correct
21 Correct 46 ms 70904 KB Output is correct
22 Correct 46 ms 70836 KB Output is correct
23 Correct 45 ms 70904 KB Output is correct
24 Correct 45 ms 70904 KB Output is correct
25 Correct 45 ms 70904 KB Output is correct
26 Correct 45 ms 70904 KB Output is correct
27 Correct 46 ms 70904 KB Output is correct
28 Correct 46 ms 70904 KB Output is correct
29 Correct 46 ms 70904 KB Output is correct
30 Correct 46 ms 70888 KB Output is correct
31 Correct 2208 ms 79452 KB Output is correct
32 Correct 174 ms 80856 KB Output is correct
33 Correct 292 ms 79352 KB Output is correct
34 Correct 1802 ms 79588 KB Output is correct
35 Correct 1071 ms 79432 KB Output is correct
36 Correct 299 ms 79352 KB Output is correct
37 Correct 254 ms 79352 KB Output is correct
38 Correct 180 ms 79228 KB Output is correct
39 Correct 163 ms 79352 KB Output is correct
40 Correct 162 ms 79224 KB Output is correct
41 Correct 366 ms 79544 KB Output is correct
42 Correct 450 ms 79224 KB Output is correct
43 Correct 114 ms 80120 KB Output is correct
44 Correct 320 ms 79452 KB Output is correct
45 Correct 207 ms 79480 KB Output is correct
46 Correct 173 ms 79480 KB Output is correct
47 Correct 135 ms 78968 KB Output is correct
48 Correct 132 ms 78968 KB Output is correct
49 Correct 151 ms 79144 KB Output is correct
50 Correct 286 ms 79352 KB Output is correct
51 Correct 152 ms 79252 KB Output is correct
52 Incorrect 261 ms 80900 KB Output isn't correct
53 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 49 ms 70776 KB Output is correct
2 Correct 44 ms 70780 KB Output is correct
3 Correct 45 ms 70776 KB Output is correct
4 Correct 45 ms 70776 KB Output is correct
5 Correct 45 ms 71032 KB Output is correct
6 Correct 45 ms 70872 KB Output is correct
7 Correct 46 ms 70904 KB Output is correct
8 Correct 45 ms 70904 KB Output is correct
9 Correct 46 ms 70904 KB Output is correct
10 Correct 45 ms 70904 KB Output is correct
11 Correct 51 ms 70904 KB Output is correct
12 Correct 47 ms 70904 KB Output is correct
13 Correct 45 ms 70904 KB Output is correct
14 Correct 45 ms 70904 KB Output is correct
15 Correct 45 ms 70904 KB Output is correct
16 Correct 45 ms 70912 KB Output is correct
17 Correct 45 ms 70912 KB Output is correct
18 Correct 46 ms 70904 KB Output is correct
19 Correct 45 ms 70904 KB Output is correct
20 Correct 46 ms 70904 KB Output is correct
21 Correct 46 ms 70904 KB Output is correct
22 Correct 46 ms 70836 KB Output is correct
23 Correct 45 ms 70904 KB Output is correct
24 Correct 45 ms 70904 KB Output is correct
25 Correct 45 ms 70904 KB Output is correct
26 Correct 45 ms 70904 KB Output is correct
27 Correct 46 ms 70904 KB Output is correct
28 Correct 46 ms 70904 KB Output is correct
29 Correct 46 ms 70904 KB Output is correct
30 Correct 46 ms 70888 KB Output is correct
31 Correct 2208 ms 79452 KB Output is correct
32 Correct 174 ms 80856 KB Output is correct
33 Correct 292 ms 79352 KB Output is correct
34 Correct 1802 ms 79588 KB Output is correct
35 Correct 1071 ms 79432 KB Output is correct
36 Correct 299 ms 79352 KB Output is correct
37 Correct 254 ms 79352 KB Output is correct
38 Correct 180 ms 79228 KB Output is correct
39 Correct 163 ms 79352 KB Output is correct
40 Correct 162 ms 79224 KB Output is correct
41 Correct 366 ms 79544 KB Output is correct
42 Correct 450 ms 79224 KB Output is correct
43 Correct 114 ms 80120 KB Output is correct
44 Correct 320 ms 79452 KB Output is correct
45 Correct 207 ms 79480 KB Output is correct
46 Correct 173 ms 79480 KB Output is correct
47 Correct 135 ms 78968 KB Output is correct
48 Correct 132 ms 78968 KB Output is correct
49 Correct 151 ms 79144 KB Output is correct
50 Correct 286 ms 79352 KB Output is correct
51 Correct 152 ms 79252 KB Output is correct
52 Correct 1444 ms 132856 KB Output is correct
53 Correct 1762 ms 131668 KB Output is correct
54 Correct 1129 ms 133056 KB Output is correct
55 Correct 1379 ms 132856 KB Output is correct
56 Correct 1745 ms 131240 KB Output is correct
57 Correct 1786 ms 131592 KB Output is correct
58 Correct 1049 ms 132884 KB Output is correct
59 Correct 1327 ms 133020 KB Output is correct
60 Correct 1363 ms 132984 KB Output is correct
61 Correct 1224 ms 132216 KB Output is correct
62 Correct 1048 ms 132856 KB Output is correct
63 Correct 1262 ms 133624 KB Output is correct
64 Correct 2310 ms 128316 KB Output is correct
65 Correct 555 ms 115556 KB Output is correct
66 Correct 2107 ms 127988 KB Output is correct
67 Correct 1204 ms 127224 KB Output is correct
68 Correct 1967 ms 127256 KB Output is correct
69 Correct 1678 ms 127136 KB Output is correct
70 Correct 2052 ms 127556 KB Output is correct
71 Correct 2105 ms 127780 KB Output is correct
72 Correct 1265 ms 128432 KB Output is correct
73 Correct 1880 ms 128444 KB Output is correct
74 Correct 2135 ms 128816 KB Output is correct
75 Correct 2121 ms 128248 KB Output is correct
76 Correct 1057 ms 127476 KB Output is correct
77 Correct 996 ms 127096 KB Output is correct
78 Correct 1186 ms 128100 KB Output is correct
79 Correct 1377 ms 128660 KB Output is correct
80 Correct 1226 ms 127860 KB Output is correct
81 Incorrect 261 ms 80900 KB Output isn't correct
82 Halted 0 ms 0 KB -