結果

問題 No.2065 Sum of Min
ユーザー hashiryohashiryo
提出日時 2023-11-05 22:28:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 759 ms / 2,000 ms
コード長 8,726 bytes
コンパイル時間 3,055 ms
コンパイル使用メモリ 231,808 KB
実行使用メモリ 81,108 KB
最終ジャッジ日時 2024-09-25 22:46:28
合計ジャッジ時間 15,284 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 311 ms
81,064 KB
testcase_05 AC 275 ms
81,016 KB
testcase_06 AC 401 ms
80,888 KB
testcase_07 AC 177 ms
80,964 KB
testcase_08 AC 387 ms
80,880 KB
testcase_09 AC 482 ms
80,884 KB
testcase_10 AC 319 ms
81,108 KB
testcase_11 AC 318 ms
80,888 KB
testcase_12 AC 720 ms
81,008 KB
testcase_13 AC 724 ms
81,088 KB
testcase_14 AC 712 ms
80,900 KB
testcase_15 AC 714 ms
80,936 KB
testcase_16 AC 719 ms
81,108 KB
testcase_17 AC 759 ms
80,880 KB
testcase_18 AC 707 ms
81,012 KB
testcase_19 AC 706 ms
81,012 KB
testcase_20 AC 717 ms
80,916 KB
testcase_21 AC 733 ms
81,012 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) 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) {
     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;
 }
 template <bool z> inline void seti(int i, int j, T v) {
  auto dat= val.begin() + id[i] * 2;
  j+= id[i + 1] - id[i];
  if constexpr (z) dat[j]= v;
  else dat[j]= M::op(dat[j], v);
  for (; j;) j>>= 1, dat[j]= M::op(dat[2 * j], dat[2 * j + 1]);
 }
 template <bool z> inline void set_(pos_t x, pos_t y, T v) {
  for (int i= 1, p= xy2i(x, y);;) {
   if (seti<z>(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;
  }
 }
 template <class F> void dfs(const F &f, int i, int a, int b, int c, int d, int L, int R) const {
  if (c == d || R <= a || b <= L) return;
  if (L <= a && b <= R) return f(i, c, d);
  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(f, i * 2, a, m, ac, ad, L, R), dfs(f, i * 2 + 1, m, b, bc, bd, L, R);
 }
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<1>(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<1>(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<0>(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();
  auto f= [&](int i, int c, int d) { ret= M::op(ret, fold(i, c, d)); };
  return dfs(f, 1, 0, sz, y2i(u), y2i(d), x2i(l), x2i(r)), ret;
 }
 // [l,r) x [u,d)
 int count(pos_t l, pos_t r, pos_t u, pos_t d) const {
  int ret= 0;
  auto f= [&](int i, int c, int d) { ret+= d - c; };
  return dfs(f, 1, 0, sz, y2i(u), y2i(d), x2i(l), x2i(r)), ret;
 }
 void set(pos_t x, pos_t y, T v) { set_<1>(x, y, v); }
 void mul(pos_t x, pos_t y, T v) { set_<0>(x, y, v); }
 T get(pos_t x, pos_t y) const { return val[xy2i(x, y) + id[2]]; }
};
using namespace std;
struct RSQ {
 using T= long long;
 static T ti() { return 0; }
 static T op(const T &l, const T &r) { return l + r; }
};
signed main() {
 cin.tie(0);
 ios::sync_with_stdio(0);
 int N, Q;
 cin >> N >> Q;
 vector<array<int, 3>> xyv(N);
 for (int i= 0; i < N; ++i) {
  int A;
  cin >> A;
  xyv.push_back({i, A, A});
 }
 SegmentTree_2D<int, RSQ> seg(xyv);
 while (Q--) {
  int L, R, X;
  cin >> L >> R >> X, --L;
  cout << seg.fold(L, R, 0, X) + (long long)seg.count(L, R, X, 1e9 + 10) * X << '\n';
 }
 return 0;
}
0