結果

問題 No.877 Range ReLU Query
ユーザー beetbeet
提出日時 2019-08-09 16:30:21
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 823 ms / 2,000 ms
コード長 4,533 bytes
コンパイル時間 3,031 ms
コンパイル使用メモリ 213,944 KB
実行使用メモリ 129,528 KB
最終ジャッジ日時 2024-04-25 21:56:41
合計ジャッジ時間 10,915 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 3 ms
6,816 KB
testcase_02 AC 3 ms
6,944 KB
testcase_03 AC 3 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 3 ms
6,940 KB
testcase_07 AC 3 ms
6,940 KB
testcase_08 AC 4 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 3 ms
6,940 KB
testcase_11 AC 736 ms
98,896 KB
testcase_12 AC 653 ms
96,280 KB
testcase_13 AC 519 ms
79,608 KB
testcase_14 AC 492 ms
70,980 KB
testcase_15 AC 823 ms
125,980 KB
testcase_16 AC 800 ms
122,276 KB
testcase_17 AC 816 ms
125,300 KB
testcase_18 AC 804 ms
126,664 KB
testcase_19 AC 390 ms
129,400 KB
testcase_20 AC 596 ms
129,528 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using int64 = long long;
const int mod = (int) (1e9 + 7);

const int64 infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;

struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(10);
  }
} iosetup;

template< typename T1, typename T2 >
ostream &operator<<(ostream &os, const pair< T1, T2 > &p) {
  os << p.first << " " << p.second;
  return os;
}

template< typename T1, typename T2 >
istream &operator>>(istream &is, pair< T1, T2 > &p) {
  is >> p.first >> p.second;
  return is;
}

template< typename T >
ostream &operator<<(ostream &os, const vector< T > &v) {
  for(int i = 0; i < (int) v.size(); i++) {
    os << v[i] << (i + 1 != v.size() ? " " : "");
  }
  return os;
}

template< typename T >
istream &operator>>(istream &is, vector< T > &v) {
  for(T &in : v) is >> in;
  return is;
}

template< typename T1, typename T2 >
inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }

template< typename T1, typename T2 >
inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

template< typename T = int64 >
vector< T > make_v(size_t a) {
  return vector< T >(a);
}

template< typename T, typename... Ts >
auto make_v(size_t a, Ts... ts) {
  return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...));
}

template< typename T, typename V >
typename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) {
  t = v;
}

template< typename T, typename V >
typename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) {
  for(auto &e : t) fill_v(e, v);
}

template< typename F >
struct FixPoint : F {
  FixPoint(F &&f) : F(forward< F >(f)) {}

  template< typename... Args >
  decltype(auto) operator()(Args &&... args) const {
    return F::operator()(*this, forward< Args >(args)...);
  }
};

template< typename F >
inline decltype(auto) MFP(F &&f) {
  return FixPoint< F >{forward< F >(f)};
}

template< typename Monoid, typename F >
struct PersistentSegmentTree {

  struct Node {
    Monoid data;
    Node *l, *r;

    Node(const Monoid &data) : data(data), l(nullptr), r(nullptr) {}
  };


  int sz;
  const F f;
  const Monoid M1;

  PersistentSegmentTree(const F f, const Monoid &M1) : f(f), M1(M1) {}

  Node *build(const vector< Monoid > &v) {
    sz = (int) v.size();
    return build(0, (int) v.size(), v);
  }

  Node *merge(Node *l, Node *r) {
    auto t = new Node(f(l->data, r->data));
    t->l = l;
    t->r = r;
    return t;
  }

  Node *build(int l, int r, const vector< Monoid > &v) {
    if(l + 1 >= r) return new Node(v[l]);
    return merge(build(l, (l + r) >> 1, v), build((l + r) >> 1, r, v));
  }

  Node *update(int a, const Monoid &x, Node *k, int l, int r) {
    if(r <= a || a + 1 <= l) {
      return k;
    } else if(a <= l && r <= a + 1) {
      return new Node(x);
    } else {
      return merge(update(a, x, k->l, l, (l + r) >> 1), update(a, x, k->r, (l + r) >> 1, r));
    }
  }

  Node *update(Node *t, int k, const Monoid &x) {
    return update(k, x, t, 0, sz);
  }

  Monoid query(int a, int b, Node *k, int l, int r) {
    if(r <= a || b <= l) {
      return M1;
    } else if(a <= l && r <= b) {
      return k->data;
    } else {
      return f(query(a, b, k->l, l, (l + r) >> 1),
               query(a, b, k->r, (l + r) >> 1, r));
    }
  }

  Monoid query(Node *t, int a, int b) {
    return query(a, b, t, 0, sz);
  }
};

int main() {
  int N, Q;
  cin >> N >> Q;
  vector< pair< int, int > > A(N);
  for(int i = 0; i < N; i++) {
    cin >> A[i].first;
    A[i].second = i;
  }
  sort(begin(A), end(A));

  auto add = [](int64 a, int64 b) { return a + b; };
  PersistentSegmentTree< int64, decltype(add) > beet(add, 0);
  vector< PersistentSegmentTree< int64, decltype(add) >::Node * > tapi(N + 1), tapu(N + 1);
  tapi[0] = beet.build(vector< int64 >(N, 1));
  vector< int64 > dat(N);
  for(int i = 0; i < N; i++) dat[A[i].second] = A[i].first;
  tapu[0] = beet.build(dat);
  for(int i = 0; i < N; i++) {
    tapi[i + 1] = beet.update(tapi[i], A[i].second, 0);
    tapu[i + 1] = beet.update(tapu[i], A[i].second, 0);
  }
  auto type1 = [&]() {
    int l, r, x;
    cin >> l >> r >> x;
    --l;
    int pos = lower_bound(begin(A), end(A), make_pair(x, 0)) - begin(A);
    int64 add = 0;
    add -= beet.query(tapi[pos], l, r) * x;
    add += beet.query(tapu[pos], l, r);
    cout << add << "\n";
  };
  while(Q--) {
    int T;
    cin >> T;
    type1();
  }
}



0