結果

問題 No.728 ギブ and テイク
ユーザー hashiryohashiryo
提出日時 2023-11-02 23:03:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 822 ms / 3,000 ms
コード長 8,304 bytes
コンパイル時間 2,852 ms
コンパイル使用メモリ 222,728 KB
実行使用メモリ 92,252 KB
最終ジャッジ日時 2024-09-25 18:23:32
合計ジャッジ時間 10,108 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 3 ms
5,376 KB
testcase_13 AC 26 ms
7,424 KB
testcase_14 AC 44 ms
10,240 KB
testcase_15 AC 19 ms
6,144 KB
testcase_16 AC 36 ms
9,596 KB
testcase_17 AC 33 ms
8,916 KB
testcase_18 AC 542 ms
61,952 KB
testcase_19 AC 587 ms
65,672 KB
testcase_20 AC 727 ms
82,304 KB
testcase_21 AC 623 ms
68,608 KB
testcase_22 AC 175 ms
28,588 KB
testcase_23 AC 105 ms
18,516 KB
testcase_24 AC 398 ms
52,948 KB
testcase_25 AC 401 ms
52,632 KB
testcase_26 AC 167 ms
27,572 KB
testcase_27 AC 822 ms
92,252 KB
testcase_28 AC 369 ms
92,116 KB
testcase_29 AC 2 ms
6,940 KB
testcase_30 AC 2 ms
6,940 KB
testcase_31 AC 2 ms
6,940 KB
testcase_32 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
// clang-format off
std::ostream&operator<<(std::ostream&os,std::int8_t x){return os<<(int)x;}
std::ostream&operator<<(std::ostream&os,std::uint8_t x){return os<<(int)x;}
std::ostream&operator<<(std::ostream&os,const __int128_t &v){if(!v)os<<"0";__int128_t tmp=v<0?(os<<"-",-v):v;std::string s;while(tmp)s+='0'+(tmp%10),tmp/=10;return std::reverse(s.begin(),s.end()),os<<s;}
std::ostream&operator<<(std::ostream&os,const __uint128_t &v){if(!v)os<<"0";__uint128_t tmp=v;std::string s;while(tmp)s+='0'+(tmp%10),tmp/=10;return std::reverse(s.begin(),s.end()),os<<s;}
#define checkpoint() (void(0))
#define debug(...) (void(0))
#define debugArray(x,n) (void(0))
#define debugMatrix(x,h,w) (void(0))
// clang-format on
#include <type_traits>
template <class T> static constexpr bool tuple_like_v= false;
template <class... Args> static constexpr bool tuple_like_v<std::tuple<Args...>> = true;
template <class T, class U> static constexpr bool tuple_like_v<std::pair<T, U>> = true;
template <class T, size_t K> static constexpr bool tuple_like_v<std::array<T, K>> = true;
template <class T> auto to_tuple(const T &t) {
 if constexpr (tuple_like_v<T>) return std::apply([](auto &&...x) { return std::make_tuple(x...); }, t);
}
template <class T> auto forward_tuple(const T &t) {
 if constexpr (tuple_like_v<T>) return std::apply([](auto &&...x) { return std::forward_as_tuple(x...); }, t);
}
template <class T> static constexpr bool array_like_v= false;
template <class T, size_t K> static constexpr bool array_like_v<std::array<T, K>> = true;
template <class T, class U> static constexpr bool array_like_v<std::pair<T, U>> = std::is_convertible_v<T, U>;
template <class T> static constexpr bool array_like_v<std::tuple<T>> = true;
template <class T, class U, class... Args> static constexpr bool array_like_v<std::tuple<T, U, Args...>> = array_like_v<std::tuple<T, Args...>> && std::is_convertible_v<U, T>;
template <class T> auto to_array(const T &t) {
 if constexpr (array_like_v<T>) return std::apply([](auto &&...x) { return std::array{x...}; }, t);
}
template <class T> using to_tuple_t= decltype(to_tuple(T()));
template <class T> using to_array_t= decltype(to_array(T()));
template <class pos_t, class M> class SegmentTree_2D {
public:
 using T= typename M::T;
 using Pos= std::array<pos_t, 2>;
 std::vector<pos_t> xs;
 std::vector<Pos> yxs;
 std::vector<int> id, tol;
 std::vector<T> val;
 template <class P> using canbe_Pos= std::is_convertible<to_tuple_t<P>, std::tuple<pos_t, pos_t>>;
 template <class P> using canbe_PosV= std::is_convertible<to_tuple_t<P>, std::tuple<pos_t, pos_t, T>>;
 template <class P, class U> static constexpr bool canbe_Pos_and_T_v= std::conjunction_v<canbe_Pos<P>, std::is_convertible<U, T>>;
 int sz;
 inline int x2i(pos_t x) const { return std::lower_bound(xs.begin(), xs.end(), x) - xs.begin(); }
 inline int y2i(pos_t y) const {
  return std::lower_bound(yxs.begin(), yxs.end(), Pos{y, 0}, [](const Pos &a, const Pos &b) { return a[0] < b[0]; }) - yxs.begin();
 }
 inline int xy2i(pos_t x, pos_t y) const {
  Pos p{y, x};
  auto it= std::lower_bound(yxs.begin(), yxs.end(), p);
  return assert(p == *it), it - yxs.begin();
 }
 template <bool z, size_t k, class P> inline auto get_(const P &p) {
  if constexpr (z == 0) return std::get<k>(p);
  else return std::get<k>(p.first);
 }
 template <bool z, class XYW> inline void build(const XYW *xyw, int n, const T &v= M::ti()) {
  xs.resize(n), yxs.resize(n);
  for (int i= n; i--;) xs[i]= get_<z, 0>(xyw[i]);
  std::sort(xs.begin(), xs.end()), xs.erase(std::unique(xs.begin(), xs.end()), xs.end()), id.resize((sz= 1 << (32 - __builtin_clz(xs.size()))) * 2 + 1);
  std::vector<int> ix(n), ord(n);
  for (int i= n; i--;) ix[i]= x2i(get_<z, 0>(xyw[i]));
  for (int i: ix)
   for (i+= sz; i; i>>= 1) ++id[i + 1];
  for (int i= 1, e= sz * 2; i < e; ++i) id[i + 1]+= id[i];
  val.assign(id.back() * 2, M::ti()), tol.resize(id[sz] + 1), std::iota(ord.begin(), ord.end(), 0), std::sort(ord.begin(), ord.end(), [&](int i, int j) { return get_<z, 1>(xyw[i]) == get_<z, 1>(xyw[j]) ? get_<z, 0>(xyw[i]) < get_<z, 0>(xyw[j]) : get_<z, 1>(xyw[i]) < get_<z, 1>(xyw[j]); });
  for (int i= n; i--;) yxs[i]= {get_<z, 1>(xyw[ord[i]]), get_<z, 0>(xyw[ord[i]])};
  std::vector<int> ptr= id;
  for (int r: ord)
   for (int i= ix[r] + sz, j= -1; i; j= i, i>>= 1) {
    int p= ptr[i]++;
    if constexpr (z == 0) {
     if constexpr (std::tuple_size_v<XYW> == 3) val[id[i + 1] + p]= std::get<2>(xyw[r]);
     else val[id[i + 1] + p]= v;
    } else val[id[i + 1] + p]= xyw[r].second;
    if (j != -1) tol[p + 1]= !(j & 1);
   }
  for (int i= 1, e= id[sz]; i < e; ++i) tol[i + 1]+= tol[i];
  for (int i= 0, e= sz * 2; i < e; ++i) {
   auto dat= val.begin() + id[i] * 2;
   for (int j= id[i + 1] - id[i]; --j > 0;) dat[j]= M::op(dat[j * 2], dat[j * 2 + 1]);
  }
 }
 inline T fold(int i, int a, int b) const {
  int n= id[i + 1] - id[i];
  T ret= M::ti();
  auto dat= val.begin() + id[i] * 2;
  for (a+= n, b+= n; a < b; a>>= 1, b>>= 1) {
   if (a & 1) ret= M::op(ret, dat[a++]);
   if (b & 1) ret= M::op(dat[--b], ret);
  }
  return ret;
 }
 inline void seti(int i, int j, T v) {
  auto dat= val.begin() + id[i] * 2;
  for (dat[j+= id[i + 1] - id[i]]= v; j;) j>>= 1, dat[j]= M::op(dat[2 * j], dat[2 * j + 1]);
 }
public:
 template <class P, typename= std::enable_if_t<std::disjunction_v<canbe_Pos<P>, canbe_PosV<P>>>> SegmentTree_2D(const P *p, size_t n) { build<0>(p, n); }
 template <class P, typename= std::enable_if_t<std::disjunction_v<canbe_Pos<P>, canbe_PosV<P>>>> SegmentTree_2D(const std::vector<P> &p): SegmentTree_2D(p.data(), p.size()) {}
 template <class P, typename= std::enable_if_t<canbe_Pos<P>::value>> SegmentTree_2D(const std::set<P> &p): SegmentTree_2D(std::vector(p.begin(), p.end())) {}
 template <class P, class U, typename= std::enable_if_t<canbe_Pos_and_T_v<P, U>>> SegmentTree_2D(const P *p, size_t n, const U &v) { build<0>(p, n, v); }
 template <class P, class U, typename= std::enable_if_t<canbe_Pos_and_T_v<P, U>>> SegmentTree_2D(const std::vector<P> &p, const U &v): SegmentTree_2D(p.data(), p.size(), v) {}
 template <class P, class U, typename= std::enable_if_t<canbe_Pos_and_T_v<P, U>>> SegmentTree_2D(const std::set<P> &p, const U &v): SegmentTree_2D(std::vector(p.begin(), p.end()), v) {}
 template <class P, class U, typename= std::enable_if_t<canbe_Pos_and_T_v<P, U>>> SegmentTree_2D(const std::pair<P, U> *p, size_t n) { build<1>(p, n); }
 template <class P, class U, typename= std::enable_if_t<canbe_Pos_and_T_v<P, U>>> SegmentTree_2D(const std::vector<std::pair<P, U>> &p): SegmentTree_2D(p.data(), p.size()) {}
 template <class P, class U, typename= std::enable_if_t<canbe_Pos_and_T_v<P, U>>> SegmentTree_2D(const std::map<P, U> &p): SegmentTree_2D(std::vector(p.begin(), p.end())) {}
 // [l,r) x [u,d)
 T fold(pos_t l, pos_t r, pos_t u, pos_t d) const {
  T ret= M::ti();
  int L= x2i(l), R= x2i(r);
  auto dfs= [&](auto &dfs, int i, int a, int b, int c, int d) -> void {
   if (c == d || R <= a || b <= L) return;
   if (L <= a && b <= R) return ret= M::op(ret, fold(i, c, d)), void();
   int m= (a + b) / 2, ac= tol[id[i] + c] - tol[id[i]], bc= c - ac, ad= tol[id[i] + d] - tol[id[i]], bd= d - ad;
   dfs(dfs, i * 2, a, m, ac, ad), dfs(dfs, i * 2 + 1, m, b, bc, bd);
  };
  return dfs(dfs, 1, 0, sz, y2i(u), y2i(d)), ret;
 }
 void set(pos_t x, pos_t y, T v) {
  for (int i= 1, p= xy2i(x, y);;) {
   if (seti(i, p - id[i], v); i >= sz) break;
   if (int lc= tol[p] - tol[id[i]], rc= (p - id[i]) - lc; tol[p + 1] - tol[p]) p= id[2 * i] + lc, i= 2 * i;
   else p= id[2 * i + 1] + rc, i= 2 * i + 1;
  }
 }
 T get(pos_t x, pos_t y) const { return val[xy2i(x, y) + id[2]]; }
};
using namespace std;
struct RSQ {
 using T= int;
 static T ti() { return 0; }
 static T op(T l, T r) { return l + r; }
};
signed main() {
 cin.tie(0);
 ios::sync_with_stdio(0);
 int N;
 cin >> N;
 int A[N], L[N], R[N];
 for (int i= 0; i < N; ++i) cin >> A[i];
 for (int i= 0; i < N; ++i) cin >> L[i] >> R[i];
 vector<array<int, 2>> v(N);
 for (int i= 0; i < N; i++) v[i]= {A[i], A[i] + R[i]};
 SegmentTree_2D<int, RSQ> seg(v, 1);
 long long ans= 0;
 for (int i= 0; i < N; ++i)
  if (L[i] > 0) ans+= seg.fold(A[i] - L[i], A[i], A[i], 0x7fffffff);
 cout << ans << "\n";
 return 0;
}
0