Submission #702793

# Submission time Handle Problem Language Result Execution time Memory
702793 2023-02-25T07:31:36 Z veehz Strange Device (APIO19_strange_device) C++17
20 / 100
1757 ms 524288 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef __int128_t i128;

/* Lazy Sparse Segment Tree (Unsure of correctness) */
template <class S, S (*op)(S, S, long long), S (*e)()>
struct lazysparseSegtree {
  /* S op(S a, S b, long long len) {} -> Combine values, op(a,b,len) =
   * op(op(a,b,len-1),b,1) */
  /* S e() {} -> Initial value (0) */
  typedef long long ll;
  ll l, r;
  S val;
  bool marked;
  S flag;
  lazysparseSegtree<S, op, e>*left, *right;
  lazysparseSegtree(ll _l, ll _r) : lazysparseSegtree(_l, _r, e()) {}
  lazysparseSegtree(ll _l, ll _r, S _val)
      : lazysparseSegtree(_l, _r, _val, false, e()) {}
  lazysparseSegtree(ll _l, ll _r, S _val, bool _marked, S _flag)
      : l(_l),
        r(_r),
        val(_val),
        marked(_marked),
        flag(_flag),
        left(nullptr),
        right(nullptr) {}

  void push() {
    if (marked) {
      if (left == nullptr)
        left = new lazysparseSegtree<S, op, e>(l, (l + r) / 2, e());
      if (right == nullptr)
        right = new lazysparseSegtree<S, op, e>((l + r) / 2 + 1, r, e());
      left->set(flag);
      right->set(flag);
      marked = false;
    }
  }

  void set(ll L, ll R, S x) {
    if (R < l || r < L) return;
    if (L <= l && r <= R) {
      set(x);
      return;
    }
    push();
    if (left == nullptr)
      left = new lazysparseSegtree<S, op, e>(l, (l + r) / 2, e());
    if (right == nullptr)
      right = new lazysparseSegtree<S, op, e>((l + r) / 2 + 1, r, e());
    left->set(L, R, x);
    right->set(L, R, x);
    val = op(left->val, right->val, 1);
  }

  void set(S x) {
    if (marked) {
      flag = x;
      val = x * (r-l+1);
    } else {
      marked = true;
      flag = x;
      val = x * (r-l+1);
    }
  }

  S prod(ll ql, ll qr) {
    if (qr < l || r < ql) return e();
    if (ql <= l && r <= qr) return val;
    push();
    S sml = e(), smr = e();
    if (left != nullptr) sml = left->prod(ql, qr);
    if (right != nullptr) smr = right->prod(ql, qr);
    return op(sml, smr, 1);
  }

  S prod(ll q) { return prod(q, q); }
};
/* End: Lazy Sparse Segment Tree */

ll op(ll a, ll b, ll len) { return a + b * len; }
ll e() { return 0; }

const int NINF = -1e9;
int main() {
  ll n, a, b;
  cin >> n >> a >> b;
  vector<pair<ll, ll>> v(n);
  for (int i = 0; i < n; i++) cin >> v[i].first >> v[i].second;

  lazysparseSegtree<ll, op, e> root(0, 1e18 + 1);
  ll gc = __gcd(a, b + 1);
  bool smallEnough = (i128(a) / gc * i128(b) <= LONG_LONG_MAX);
  ll cy = smallEnough ? a / gc * b : NINF;

  for (int i = 0; i < n; i++) {
    auto& p = v[i];
    ll l = p.first, r = p.second;
    if (smallEnough && r - l + 1 >= cy) {
      cerr << "FULL" << endl;
      cout << cy << endl;
      return 0;
    }

    if (smallEnough) {
      l %= cy;
      r %= cy;
      if (l > r) {
        root.set(l, cy - 1, 1);
        root.set(0, r, 1);
      } else {
        root.set(l, r, 1);
      }
    } else {
      root.set(l, r, 1);
    }
  }
  cout << root.prod(0, 1e18 + 1) << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 22 ms 5556 KB Output is correct
3 Correct 22 ms 6976 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 300 KB Output is correct
8 Correct 1 ms 300 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 21 ms 5320 KB Output is correct
17 Correct 172 ms 17072 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 3 ms 1748 KB Output is correct
3 Correct 3 ms 1848 KB Output is correct
4 Correct 3 ms 1748 KB Output is correct
5 Correct 1243 ms 18092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1688 ms 141252 KB Output is correct
3 Runtime error 1757 ms 524288 KB Execution killed with signal 9
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1688 ms 141252 KB Output is correct
3 Runtime error 1757 ms 524288 KB Execution killed with signal 9
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1688 ms 141252 KB Output is correct
3 Runtime error 1757 ms 524288 KB Execution killed with signal 9
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 248 ms 86440 KB Output is correct
3 Correct 343 ms 202172 KB Output is correct
4 Runtime error 1709 ms 524288 KB Execution killed with signal 9
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 22 ms 5556 KB Output is correct
3 Correct 22 ms 6976 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 300 KB Output is correct
8 Correct 1 ms 300 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 21 ms 5320 KB Output is correct
17 Correct 172 ms 17072 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 3 ms 1748 KB Output is correct
26 Correct 3 ms 1848 KB Output is correct
27 Correct 3 ms 1748 KB Output is correct
28 Correct 1243 ms 18092 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 1688 ms 141252 KB Output is correct
31 Runtime error 1757 ms 524288 KB Execution killed with signal 9
32 Halted 0 ms 0 KB -