Submission #868972

# Submission time Handle Problem Language Result Execution time Memory
868972 2023-11-02T16:52:08 Z evenvalue Tortoise (CEOI21_tortoise) C++17
0 / 100
0 ms 348 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long

#ifdef evenvalue
  #include "debug.h"
  #define debug(...) print(#__VA_ARGS__, __VA_ARGS__)
#else
  #define debug(...)
#endif

using int64 = long long;
using ld = long double;

template<typename T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;
template<typename T>
using max_heap = priority_queue<T, vector<T>, less<T>>;

namespace read {
int Int() {
  int x;
  cin >> x;
  return x;
}
int64 Int64() {
  int64 x;
  cin >> x;
  return x;
}
char Char() {
  char c;
  cin >> c;
  return c;
}
string String() {
  string s;
  cin >> s;
  return s;
}
double Double() {
  return stod(String());
}
ld LongDouble() {
  return stold(String());
}
template<typename T1, typename T2>
pair<T1, T2> Pair() {
  pair<T1, T2> p;
  cin >> p.first >> p.second;
  return p;
}
template<typename T>
vector<T> Vec(const int n) {
  vector<T> v(n);
  for (T &x : v) {
    cin >> x;
  }
  return v;
}
template<typename T>
vector<vector<T>> VecVec(const int n, const int m) {
  vector<vector<T>> v(n);
  for (vector<T> &vec : v) {
    vec = Vec<T>(m);
  }
  return v;
}
}//namespace read

constexpr int kInf = 1e9 + 10;
constexpr int64 kInf64 = 1e15 + 10;
constexpr int kMod = 1e9 + 7;
constexpr int kMaxN = 2e5 + 10;

template<class Node>
class LazySegTree {
  using Unite_t = std::function<Node(const Node &, const Node &)>;
  using Apply_t = std::function<void(Node &, const Node &)>;
  using Unlazy = std::function<void(Node &)>;

  size_t n = 0;
  std::vector<Node> t;

  Unite_t unite;
  Apply_t apply;
  Unlazy activate;

  void push(const size_t x, const size_t l, const size_t r) {
    const size_t mid = (l + r) / 2;
    const size_t y = 2 * (mid - l + 1) + x;
    apply(t[x + 1], t[x]);
    apply(t[y], t[x]);
    activate(t[x]);
  }

  void update(const size_t x, const size_t l, const size_t r, const size_t ql, const size_t qr, const Node upd) {
    if (ql <= l and r <= qr) {
      apply(t[x], upd);
      return;
    }
    push(x, l, r);
    const size_t mid = (l + r) / 2;
    const size_t y = 2 * (mid - l + 1) + x;
    if (ql <= mid) {
      update(x + 1, l, mid, ql, qr, upd);
    }
    if (mid < qr) {
      update(y, mid + 1, r, ql, qr, upd);
    }
    t[x] = unite(t[x + 1], t[y]);
  }

  Node query(const size_t x, const size_t l, const size_t r, const size_t ql, const size_t qr) {
    if (ql <= l and r <= qr) {
      return t[x];
    }
    push(x, l, r);
    const size_t mid = (l + r) / 2;
    const size_t y = 2 * (mid - l + 1) + x;
    if (qr <= mid) {
      return query(x + 1, l, mid, ql, qr);
    } else if (mid < ql) {
      return query(y, mid + 1, r, ql, qr);
    } else {
      return unite(query(x + 1, l, mid, ql, qr), query(y, mid + 1, r, ql, qr));
    }
  }

  void build(const int x, const int l, const int r, const std::vector<Node> &a) {
    if (l == r) {
      t[x] = a[l];
      return;
    }
    const int mid = (l + r) / 2;
    const int y = 2 * (mid - l + 1) + x;
    build(x + 1, l, mid, a);
    build(y, mid + 1, r, a);
    t[x] = unite(t[x + 1], t[y]);
  }

public:
  LazySegTree(const size_t n, const Node e, const Unite_t unite, const Apply_t apply, const Unlazy activate)
      : n(n), t(2 * n - 1, e), unite(unite), apply(apply), activate(activate) {}

  LazySegTree(const std::vector<Node> &a, const Unite_t unite, const Apply_t apply, const Unlazy activate)
      : n(a.size()), t(2 * n - 1), unite(unite), apply(apply), activate(activate) {
    build(0, 0, n - 1, a);
  }

  void update(const int ql, const int qr, const Node upd) {
    assert(0 <= ql and ql <= qr and qr < n);
    update(0, 0, n - 1, ql, qr, upd);
  }

  Node query(const int ql, const int qr) {
    assert(0 <= ql and ql <= qr and qr < n);
    return query(0, 0, n - 1, ql, qr);
  }

  LazySegTree<Node> &operator=(LazySegTree<Node> other) {
    swap(n, other.n);
    swap(t, other.t);
    swap(unite, other.unite);
    swap(apply, other.apply);
    swap(activate, other.activate);
    return *this;
  }
};

auto create_segtree(const int n) {
  struct node {
    int val = -kInf64;
    int lazy = 0;
  };

  auto unite = [&](const node &l, const node &r) {
    return node{max(l.val, r.val)};
  };

  auto apply = [&](node &x, const node y) {
    x.val += y.lazy;
    x.lazy += y.lazy;
  };

  auto activate = [&](node &x) {
    x.lazy = 0;
  };

  LazySegTree<node> lst(n, node(), unite, apply, activate);

  return lst;
}

enum direction {
  left,
  right
};

struct candidate {
  int x;
  int dist;
  direction dir;

  bool operator<(const candidate &other) const {
    return dist < other.dist;
  }
};


inline void solution() {
  const int n = read::Int();
  vector<int> a(n + 1);

  for (int i = 1; i <= n; i++) {
    a[i] = read::Int();
  }

  vector<int> left_ground(n + 1, -kInf);
  if (a[1] == -1) left_ground[1] = 1;
  for (int i = 2; i <= n; i++) {
    left_ground[i] = (a[i] == -1 ? i : left_ground[i - 1]);
  }

  vector<int> right_ground(n + 1, kInf);
  if (a[n] == -1) right_ground[n] = n;
  for (int i = n - 1; i > 0; i--) {
    right_ground[i] = (a[i] == -1 ? i : right_ground[i + 1]);
  }

  for (int i = 0; i < n; i++) {
    if (a[i] == -1) break;
    if (a[i] > 0) {
      a[i]--;
      break;
    }
  }

  vector<int> walk(n + 1, -1);
  for (int i = 1; i <= n; i++) {
    if (a[i] != -1) continue;
    if (right_ground[i] == kInf) continue;
    walk[i] = 0;
  }

  vector<candidate> candidates;
  for (int i = 1; i <= n; i++) {
    if (a[i] <= 0) continue;

    const int ld = i - left_ground[i];
    const int rd = right_ground[i] - i;

    if (ld <= rd) {
      candidates.push_back({i, ld, direction::left});
    } else {
      candidates.push_back({i, rd, direction::right});
    }
  }

  auto time = create_segtree(n + 2);

  auto increase = [&](const int l, const int r, const int x) {
    time.update(l, r, {0, x});
  };

  for (int i = 1; i <= n; i++) {
    increase(i, i, 2 * (1 - i));
  }

  auto candy_walk = [&](const candidate c) -> bool {
    const int t = -time.query(c.x, n).val;
    if (t >= 2 * walk[left_ground[c.x]]) {
      increase(c.x, n, 2 * walk[left_ground[c.x]]);
      walk[left_ground[c.x]] = c.dist;
      return true;
    }
    return false;
  };

  auto purchase = [&](const candidate c) -> int {
    increase(c.x, c.x, kInf64);

    int buy = 0;
    if (c.dir == direction::left and right_ground[c.x] != kInf) {
      buy += candy_walk(c);
    }

    int t = -time.query(c.x + 1, n + 1).val;
    int me = -time.query(c.x, c.x).val;

    int cycles = 0;
    while (a[c.x] - buy > 0 and me >= c.dist and t >= 2 * c.dist) {
      me -= 2 * c.dist;
      t -= 2 * c.dist;
      buy++;
      cycles++;
    }

    increase(c.x + 1, n + 1, cycles * 2 * c.dist);
    increase(c.x, c.x, cycles * 2 * c.dist - c.dist);

    if (buy == 0) increase(c.x, c.x, -kInf64);
    return buy;
  };

  sort(candidates.begin(), candidates.end());

  int ans = 0;
  for (const candidate c : candidates) {
    ans -= purchase(c);
  }

  for (const int x : a) {
    if (x == -1) continue;
    ans += x;
  }

  cout << ans << '\n';
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  //freopen(".in", "r", stdin);
  //freopen(".out", "w", stdout);

  cout << fixed << setprecision(10);

  int testcases = 1;
  //cin >> testcases;
  while (testcases--) {
    solution();
  }
}

Compilation message

In file included from /usr/include/c++/10/cassert:44,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
                 from tortoise.cpp:1:
tortoise.cpp: In instantiation of 'void LazySegTree<Node>::update(long long int, long long int, Node) [with Node = create_segtree(long long int)::node]':
tortoise.cpp:264:29:   required from here
tortoise.cpp:153:40: warning: comparison of integer expressions of different signedness: 'const long long int' and 'size_t' {aka 'long unsigned int'} [-Wsign-compare]
  153 |     assert(0 <= ql and ql <= qr and qr < n);
      |                                     ~~~^~~
tortoise.cpp: In instantiation of 'Node LazySegTree<Node>::query(long long int, long long int) [with Node = create_segtree(long long int)::node]':
tortoise.cpp:272:37:   required from here
tortoise.cpp:158:40: warning: comparison of integer expressions of different signedness: 'const long long int' and 'size_t' {aka 'long unsigned int'} [-Wsign-compare]
  158 |     assert(0 <= ql and ql <= qr and qr < n);
      |                                     ~~~^~~
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -