Submission #66023

# Submission time Handle Problem Language Result Execution time Memory
66023 2018-08-09T12:06:09 Z funcsr New Home (APIO18_new_home) C++17
80 / 100
5000 ms 623308 KB
#pragma GCC optimize ("O3")
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <cassert>
using namespace std;

typedef pair<int, int> P;
#define rep(i, n) for (int i=0; i<(n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define index(xs, x) (int)(lower_bound(all(xs), x) - xs.begin())
#define _1 first
#define _2 second
#define pb push_back
#define MOD 1000000007
#define MAX_N (1<<19)
struct PQ {
  int size = 0;
  priority_queue<int> pq_max, erase_max;
  priority_queue<int, vector<int>, greater<int> > pq_min, erase_min;

  void add(int x) {
    size++;
    pq_min.push(x);
    pq_max.push(x);
  }
  void erase(int x) {
    size--;
    erase_min.push(x);
    erase_max.push(x);
  }
  bool empty() {
    return size==0;
  }
  int min() {
    while (!erase_min.empty() && pq_min.top() == erase_min.top()) pq_min.pop(), erase_min.pop();
    return pq_min.top();
  }
  int max() {
    while (!erase_max.empty() && pq_max.top() == erase_max.top()) pq_max.pop(), erase_max.pop();
    return pq_max.top();
  }
};

inline int dist(PQ &s, int d) {
  if (s.empty()) return 0;
  //else return max(abs(d-*s.begin()), abs(d-*s.rbegin()));
  else return max(d-s.min(), s.max()-d);
}

bool all1, do_not_add_it_yet;
struct SegTree {
  PQ seg[MAX_N*2-1];
  int emin[MAX_N*2-1], emax[MAX_N*2-1]; // for a=1
  SegTree() {
    rep(i, MAX_N*2-1) emin[i] = 1e9, emax[i] = -1e9;
  }

  void add(int a, int b, int x, int k=0, int l=0, int r=MAX_N) {
    if (b <= l || r <= a) return;
    if (a <= l && r <= b) {
      if (all1) {
        emin[k] = min(emin[k], x);
        emax[k] = max(emax[k], x);
      }
      else {
        seg[k].add(x);
      }
      return;
    }
    add(a, b, x, k*2+1, l, (l+r)/2);
    add(a, b, x, k*2+2, (l+r)/2, r);
  }
  void remove(int a, int b, int x, int k=0, int l=0, int r=MAX_N) {
    if (b <= l || r <= a) return;
    if (a <= l && r <= b) {
      if (!all1) seg[k].erase(x);
      return;
    }
    remove(a, b, x, k*2+1, l, (l+r)/2);
    remove(a, b, x, k*2+2, (l+r)/2, r);
  }
  int query(int k, int x) {
    k += MAX_N-1;
    int m = 0;
    if (all1) {
      m = max(m, max(x-emin[k], emax[k]-x));
    }
    else {
      m = dist(seg[k], x);
    }
    while (k > 0) {
      k = (k-1)/2;
      if (all1) m = max(m, max(x-emin[k], emax[k]-x));
      else m = max(m, dist(seg[k], x));
    }
    return m;
  }
} seg;

int N, K, Q;
int X[300000], T[300000], A[300000], B[300000];
int L[300000], Y[300000];
#define MAX_Y 300000
vector<int> Gopen[MAX_Y];
vector<int> Gclose[MAX_Y];
vector<int> Gquery[MAX_Y];
int rangeL[300000], rangeR[300000];
int ans[300000];
vector<int> years, xs;

inline void updateRangeL(int id, int l) {
  rangeL[id] = lower_bound(all(xs), l) - xs.begin();
}
inline void updateRangeR(int id, int r) {
  rangeR[id] = upper_bound(all(xs), r) - xs.begin() - 1;
}
inline void addRange(int id) {
  if (do_not_add_it_yet) return;
  int l = rangeL[id], r = rangeR[id];
  if (l <= r) seg.add(l, r+1, X[id]);
}
inline void removeRange(int id) {
  if (all1) return;
  int l = rangeL[id], r = rangeR[id];
  if (l <= r) seg.remove(l, r+1, X[id]);
}

set<P> kind[300000]; // (pos, i)

void add(int k, P val) {
  int x = val._2;
  auto it = kind[k].lower_bound(val);
  int a = -1, b = -1;
  if (it != kind[k].end()) b = it->_2;
  if (it != kind[k].begin()) a = (--it)->_2;
  kind[k].insert(val);

  if (a != -1) removeRange(a);
  if (b != -1) removeRange(b);

  if (a == -1) updateRangeL(x, 0);
  else {
    updateRangeR(a, (X[a]+X[x])/2);
    updateRangeL(x, (X[a]+X[x])/2);
  }
  if (b == -1) updateRangeR(x, 2e8);
  else {
    updateRangeR(x, (X[b]+X[x])/2);
    updateRangeL(b, (X[b]+X[x])/2);
  }

  if (a != -1) addRange(a);
  if (b != -1) addRange(b);
  addRange(x);
}

void remove(int k, P val) {
  int x = val._2;
  kind[k].erase(val);
  auto it = kind[k].lower_bound(val);
  int a = -1, b = -1;
  if (it != kind[k].end()) b = it->_2;
  if (it != kind[k].begin()) a = (--it)->_2;

  if (a != -1) removeRange(a);
  if (b != -1) removeRange(b);
  removeRange(x);

  if (a != -1) updateRangeR(a, (b==-1)?2e8:((X[a]+X[b])/2));
  if (b != -1) updateRangeL(b, (a==-1)?0:((X[a]+X[b])/2));

  if (a != -1) addRange(a);
  if (b != -1) addRange(b);
}

inline int maxDist(int pos) {
  return seg.query(index(xs, pos), pos);
}

signed main() {
  scanf("%d %d %d", &N, &K, &Q);
  all1 = true;
  rep(i, N) {
    scanf("%d %d %d %d", &X[i], &T[i], &A[i], &B[i]);
    if (A[i] != 1) all1 = false;
    X[i] *= 2;
    T[i]--;
  }
  rep(i, Q) {
    scanf("%d %d", &L[i], &Y[i]);
    L[i] *= 2;
    xs.pb(L[i]);
    years.pb(Y[i]);
  }
  sort(all(years)); uniq(years);
  assert(years.size() <= MAX_Y);
  rep(i, N) Gopen[index(years, A[i])].pb(i);
  rep(i, N) Gclose[index(years, B[i]+1)].pb(i);
  rep(i, Q) Gquery[index(years, Y[i])].pb(i);

  sort(all(xs)); uniq(xs);

  int zero = K;
  if (all1) {
    do_not_add_it_yet = true;
    rep(i, N) add(T[i], P(X[i], i));
    do_not_add_it_yet = false;
    rep(i, N) addRange(i);
    zero = 0;
  }
  rep(y, years.size()) {
    for (int i : Gopen[y]) {
      if (kind[T[i]].empty()) zero--;

      if (all1) {}
      else add(T[i], P(X[i], i));
    }
    for (int i : Gclose[y]) {
      remove(T[i], P(X[i], i));
      if (kind[T[i]].empty()) zero++;
    }
    for (int q : Gquery[y]) {
      int m = -1;
      if (zero == 0) m = maxDist(L[q])/2;
      ans[q] = m;
    }
  }
  rep(i, Q) printf("%d\n", ans[i]);
  return 0;
}

Compilation message

new_home.cpp: In function 'int main()':
new_home.cpp:11:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define rep(i, n) for (int i=0; i<(n); i++)
                                  ^
new_home.cpp:215:3: note: in expansion of macro 'rep'
   rep(y, years.size()) {
   ^~~
new_home.cpp:185:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d %d", &N, &K, &Q);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
new_home.cpp:188:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d %d %d", &X[i], &T[i], &A[i], &B[i]);
     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
new_home.cpp:194:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d", &L[i], &Y[i]);
     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 158 ms 183288 KB Output is correct
2 Correct 180 ms 183524 KB Output is correct
3 Correct 182 ms 183524 KB Output is correct
4 Correct 159 ms 183548 KB Output is correct
5 Correct 179 ms 183564 KB Output is correct
6 Correct 172 ms 183852 KB Output is correct
7 Correct 157 ms 183852 KB Output is correct
8 Correct 191 ms 183852 KB Output is correct
9 Correct 183 ms 183852 KB Output is correct
10 Correct 162 ms 183852 KB Output is correct
11 Correct 194 ms 183852 KB Output is correct
12 Correct 187 ms 183852 KB Output is correct
13 Correct 160 ms 183852 KB Output is correct
14 Correct 180 ms 183992 KB Output is correct
15 Correct 178 ms 183992 KB Output is correct
16 Correct 179 ms 183992 KB Output is correct
17 Correct 169 ms 183992 KB Output is correct
18 Correct 161 ms 183992 KB Output is correct
19 Correct 157 ms 183992 KB Output is correct
20 Correct 157 ms 183992 KB Output is correct
21 Correct 175 ms 183992 KB Output is correct
22 Correct 175 ms 183992 KB Output is correct
23 Correct 183 ms 183992 KB Output is correct
24 Correct 180 ms 183992 KB Output is correct
25 Correct 181 ms 183992 KB Output is correct
26 Correct 163 ms 183992 KB Output is correct
27 Correct 176 ms 183992 KB Output is correct
28 Correct 157 ms 183992 KB Output is correct
29 Correct 160 ms 183992 KB Output is correct
30 Correct 163 ms 183992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 158 ms 183288 KB Output is correct
2 Correct 180 ms 183524 KB Output is correct
3 Correct 182 ms 183524 KB Output is correct
4 Correct 159 ms 183548 KB Output is correct
5 Correct 179 ms 183564 KB Output is correct
6 Correct 172 ms 183852 KB Output is correct
7 Correct 157 ms 183852 KB Output is correct
8 Correct 191 ms 183852 KB Output is correct
9 Correct 183 ms 183852 KB Output is correct
10 Correct 162 ms 183852 KB Output is correct
11 Correct 194 ms 183852 KB Output is correct
12 Correct 187 ms 183852 KB Output is correct
13 Correct 160 ms 183852 KB Output is correct
14 Correct 180 ms 183992 KB Output is correct
15 Correct 178 ms 183992 KB Output is correct
16 Correct 179 ms 183992 KB Output is correct
17 Correct 169 ms 183992 KB Output is correct
18 Correct 161 ms 183992 KB Output is correct
19 Correct 157 ms 183992 KB Output is correct
20 Correct 157 ms 183992 KB Output is correct
21 Correct 175 ms 183992 KB Output is correct
22 Correct 175 ms 183992 KB Output is correct
23 Correct 183 ms 183992 KB Output is correct
24 Correct 180 ms 183992 KB Output is correct
25 Correct 181 ms 183992 KB Output is correct
26 Correct 163 ms 183992 KB Output is correct
27 Correct 176 ms 183992 KB Output is correct
28 Correct 157 ms 183992 KB Output is correct
29 Correct 160 ms 183992 KB Output is correct
30 Correct 163 ms 183992 KB Output is correct
31 Correct 2989 ms 253312 KB Output is correct
32 Correct 447 ms 253312 KB Output is correct
33 Correct 2235 ms 253312 KB Output is correct
34 Correct 3105 ms 255436 KB Output is correct
35 Correct 2845 ms 255436 KB Output is correct
36 Correct 2145 ms 255436 KB Output is correct
37 Correct 1885 ms 255436 KB Output is correct
38 Correct 1363 ms 255436 KB Output is correct
39 Correct 1064 ms 255436 KB Output is correct
40 Correct 1154 ms 255436 KB Output is correct
41 Correct 1711 ms 255436 KB Output is correct
42 Correct 1641 ms 255436 KB Output is correct
43 Correct 255 ms 255436 KB Output is correct
44 Correct 1589 ms 255436 KB Output is correct
45 Correct 1682 ms 255436 KB Output is correct
46 Correct 1845 ms 255436 KB Output is correct
47 Correct 942 ms 255436 KB Output is correct
48 Correct 940 ms 255436 KB Output is correct
49 Correct 1091 ms 255436 KB Output is correct
50 Correct 1123 ms 255436 KB Output is correct
51 Correct 1145 ms 255436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1487 ms 255436 KB Output is correct
2 Correct 1782 ms 255436 KB Output is correct
3 Correct 1150 ms 255436 KB Output is correct
4 Correct 1584 ms 265340 KB Output is correct
5 Correct 1659 ms 276240 KB Output is correct
6 Correct 1879 ms 289128 KB Output is correct
7 Correct 1020 ms 304208 KB Output is correct
8 Correct 1248 ms 318036 KB Output is correct
9 Correct 1357 ms 331420 KB Output is correct
10 Correct 1569 ms 343884 KB Output is correct
11 Correct 973 ms 355812 KB Output is correct
12 Correct 1079 ms 369332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3288 ms 373320 KB Output is correct
2 Correct 1134 ms 373320 KB Output is correct
3 Correct 3331 ms 388928 KB Output is correct
4 Correct 1258 ms 401928 KB Output is correct
5 Correct 2079 ms 414568 KB Output is correct
6 Correct 1977 ms 427064 KB Output is correct
7 Correct 3086 ms 439336 KB Output is correct
8 Correct 3183 ms 452160 KB Output is correct
9 Correct 1278 ms 463332 KB Output is correct
10 Correct 2013 ms 476416 KB Output is correct
11 Correct 2641 ms 488144 KB Output is correct
12 Correct 2641 ms 498256 KB Output is correct
13 Correct 1286 ms 505116 KB Output is correct
14 Correct 1377 ms 512576 KB Output is correct
15 Correct 1635 ms 521076 KB Output is correct
16 Correct 1875 ms 529696 KB Output is correct
17 Correct 1702 ms 536620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 158 ms 183288 KB Output is correct
2 Correct 180 ms 183524 KB Output is correct
3 Correct 182 ms 183524 KB Output is correct
4 Correct 159 ms 183548 KB Output is correct
5 Correct 179 ms 183564 KB Output is correct
6 Correct 172 ms 183852 KB Output is correct
7 Correct 157 ms 183852 KB Output is correct
8 Correct 191 ms 183852 KB Output is correct
9 Correct 183 ms 183852 KB Output is correct
10 Correct 162 ms 183852 KB Output is correct
11 Correct 194 ms 183852 KB Output is correct
12 Correct 187 ms 183852 KB Output is correct
13 Correct 160 ms 183852 KB Output is correct
14 Correct 180 ms 183992 KB Output is correct
15 Correct 178 ms 183992 KB Output is correct
16 Correct 179 ms 183992 KB Output is correct
17 Correct 169 ms 183992 KB Output is correct
18 Correct 161 ms 183992 KB Output is correct
19 Correct 157 ms 183992 KB Output is correct
20 Correct 157 ms 183992 KB Output is correct
21 Correct 175 ms 183992 KB Output is correct
22 Correct 175 ms 183992 KB Output is correct
23 Correct 183 ms 183992 KB Output is correct
24 Correct 180 ms 183992 KB Output is correct
25 Correct 181 ms 183992 KB Output is correct
26 Correct 163 ms 183992 KB Output is correct
27 Correct 176 ms 183992 KB Output is correct
28 Correct 157 ms 183992 KB Output is correct
29 Correct 160 ms 183992 KB Output is correct
30 Correct 163 ms 183992 KB Output is correct
31 Correct 2989 ms 253312 KB Output is correct
32 Correct 447 ms 253312 KB Output is correct
33 Correct 2235 ms 253312 KB Output is correct
34 Correct 3105 ms 255436 KB Output is correct
35 Correct 2845 ms 255436 KB Output is correct
36 Correct 2145 ms 255436 KB Output is correct
37 Correct 1885 ms 255436 KB Output is correct
38 Correct 1363 ms 255436 KB Output is correct
39 Correct 1064 ms 255436 KB Output is correct
40 Correct 1154 ms 255436 KB Output is correct
41 Correct 1711 ms 255436 KB Output is correct
42 Correct 1641 ms 255436 KB Output is correct
43 Correct 255 ms 255436 KB Output is correct
44 Correct 1589 ms 255436 KB Output is correct
45 Correct 1682 ms 255436 KB Output is correct
46 Correct 1845 ms 255436 KB Output is correct
47 Correct 942 ms 255436 KB Output is correct
48 Correct 940 ms 255436 KB Output is correct
49 Correct 1091 ms 255436 KB Output is correct
50 Correct 1123 ms 255436 KB Output is correct
51 Correct 1145 ms 255436 KB Output is correct
52 Correct 413 ms 536620 KB Output is correct
53 Correct 407 ms 536620 KB Output is correct
54 Correct 1383 ms 538936 KB Output is correct
55 Correct 1241 ms 538936 KB Output is correct
56 Correct 1014 ms 538936 KB Output is correct
57 Correct 1671 ms 538936 KB Output is correct
58 Correct 1318 ms 538936 KB Output is correct
59 Correct 1259 ms 538936 KB Output is correct
60 Correct 1748 ms 538936 KB Output is correct
61 Correct 276 ms 538936 KB Output is correct
62 Correct 463 ms 538936 KB Output is correct
63 Correct 958 ms 538936 KB Output is correct
64 Correct 1174 ms 538936 KB Output is correct
65 Correct 1499 ms 538936 KB Output is correct
66 Correct 2032 ms 538936 KB Output is correct
67 Correct 568 ms 538936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 158 ms 183288 KB Output is correct
2 Correct 180 ms 183524 KB Output is correct
3 Correct 182 ms 183524 KB Output is correct
4 Correct 159 ms 183548 KB Output is correct
5 Correct 179 ms 183564 KB Output is correct
6 Correct 172 ms 183852 KB Output is correct
7 Correct 157 ms 183852 KB Output is correct
8 Correct 191 ms 183852 KB Output is correct
9 Correct 183 ms 183852 KB Output is correct
10 Correct 162 ms 183852 KB Output is correct
11 Correct 194 ms 183852 KB Output is correct
12 Correct 187 ms 183852 KB Output is correct
13 Correct 160 ms 183852 KB Output is correct
14 Correct 180 ms 183992 KB Output is correct
15 Correct 178 ms 183992 KB Output is correct
16 Correct 179 ms 183992 KB Output is correct
17 Correct 169 ms 183992 KB Output is correct
18 Correct 161 ms 183992 KB Output is correct
19 Correct 157 ms 183992 KB Output is correct
20 Correct 157 ms 183992 KB Output is correct
21 Correct 175 ms 183992 KB Output is correct
22 Correct 175 ms 183992 KB Output is correct
23 Correct 183 ms 183992 KB Output is correct
24 Correct 180 ms 183992 KB Output is correct
25 Correct 181 ms 183992 KB Output is correct
26 Correct 163 ms 183992 KB Output is correct
27 Correct 176 ms 183992 KB Output is correct
28 Correct 157 ms 183992 KB Output is correct
29 Correct 160 ms 183992 KB Output is correct
30 Correct 163 ms 183992 KB Output is correct
31 Correct 2989 ms 253312 KB Output is correct
32 Correct 447 ms 253312 KB Output is correct
33 Correct 2235 ms 253312 KB Output is correct
34 Correct 3105 ms 255436 KB Output is correct
35 Correct 2845 ms 255436 KB Output is correct
36 Correct 2145 ms 255436 KB Output is correct
37 Correct 1885 ms 255436 KB Output is correct
38 Correct 1363 ms 255436 KB Output is correct
39 Correct 1064 ms 255436 KB Output is correct
40 Correct 1154 ms 255436 KB Output is correct
41 Correct 1711 ms 255436 KB Output is correct
42 Correct 1641 ms 255436 KB Output is correct
43 Correct 255 ms 255436 KB Output is correct
44 Correct 1589 ms 255436 KB Output is correct
45 Correct 1682 ms 255436 KB Output is correct
46 Correct 1845 ms 255436 KB Output is correct
47 Correct 942 ms 255436 KB Output is correct
48 Correct 940 ms 255436 KB Output is correct
49 Correct 1091 ms 255436 KB Output is correct
50 Correct 1123 ms 255436 KB Output is correct
51 Correct 1145 ms 255436 KB Output is correct
52 Correct 1487 ms 255436 KB Output is correct
53 Correct 1782 ms 255436 KB Output is correct
54 Correct 1150 ms 255436 KB Output is correct
55 Correct 1584 ms 265340 KB Output is correct
56 Correct 1659 ms 276240 KB Output is correct
57 Correct 1879 ms 289128 KB Output is correct
58 Correct 1020 ms 304208 KB Output is correct
59 Correct 1248 ms 318036 KB Output is correct
60 Correct 1357 ms 331420 KB Output is correct
61 Correct 1569 ms 343884 KB Output is correct
62 Correct 973 ms 355812 KB Output is correct
63 Correct 1079 ms 369332 KB Output is correct
64 Correct 3288 ms 373320 KB Output is correct
65 Correct 1134 ms 373320 KB Output is correct
66 Correct 3331 ms 388928 KB Output is correct
67 Correct 1258 ms 401928 KB Output is correct
68 Correct 2079 ms 414568 KB Output is correct
69 Correct 1977 ms 427064 KB Output is correct
70 Correct 3086 ms 439336 KB Output is correct
71 Correct 3183 ms 452160 KB Output is correct
72 Correct 1278 ms 463332 KB Output is correct
73 Correct 2013 ms 476416 KB Output is correct
74 Correct 2641 ms 488144 KB Output is correct
75 Correct 2641 ms 498256 KB Output is correct
76 Correct 1286 ms 505116 KB Output is correct
77 Correct 1377 ms 512576 KB Output is correct
78 Correct 1635 ms 521076 KB Output is correct
79 Correct 1875 ms 529696 KB Output is correct
80 Correct 1702 ms 536620 KB Output is correct
81 Correct 413 ms 536620 KB Output is correct
82 Correct 407 ms 536620 KB Output is correct
83 Correct 1383 ms 538936 KB Output is correct
84 Correct 1241 ms 538936 KB Output is correct
85 Correct 1014 ms 538936 KB Output is correct
86 Correct 1671 ms 538936 KB Output is correct
87 Correct 1318 ms 538936 KB Output is correct
88 Correct 1259 ms 538936 KB Output is correct
89 Correct 1748 ms 538936 KB Output is correct
90 Correct 276 ms 538936 KB Output is correct
91 Correct 463 ms 538936 KB Output is correct
92 Correct 958 ms 538936 KB Output is correct
93 Correct 1174 ms 538936 KB Output is correct
94 Correct 1499 ms 538936 KB Output is correct
95 Correct 2032 ms 538936 KB Output is correct
96 Correct 568 ms 538936 KB Output is correct
97 Correct 2084 ms 603108 KB Output is correct
98 Correct 1838 ms 603108 KB Output is correct
99 Execution timed out 5031 ms 623308 KB Time limit exceeded
100 Halted 0 ms 0 KB -