結果

問題 No.1433 Two color sequence
ユーザー rxoxixyxdrxoxixyxd
提出日時 2021-03-19 23:03:24
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 137 ms / 2,000 ms
コード長 3,855 bytes
コンパイル時間 2,429 ms
コンパイル使用メモリ 208,456 KB
実行使用メモリ 19,560 KB
最終ジャッジ日時 2023-08-12 04:02:41
合計ジャッジ時間 6,247 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 119 ms
19,472 KB
testcase_04 AC 103 ms
19,412 KB
testcase_05 AC 1 ms
4,384 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,384 KB
testcase_08 AC 136 ms
19,412 KB
testcase_09 AC 134 ms
19,548 KB
testcase_10 AC 129 ms
18,884 KB
testcase_11 AC 126 ms
19,140 KB
testcase_12 AC 134 ms
19,412 KB
testcase_13 AC 131 ms
19,416 KB
testcase_14 AC 136 ms
19,444 KB
testcase_15 AC 136 ms
19,452 KB
testcase_16 AC 137 ms
19,436 KB
testcase_17 AC 120 ms
19,380 KB
testcase_18 AC 118 ms
19,404 KB
testcase_19 AC 117 ms
19,384 KB
testcase_20 AC 136 ms
19,560 KB
testcase_21 AC 122 ms
19,472 KB
testcase_22 AC 118 ms
19,400 KB
testcase_23 AC 120 ms
19,388 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < n; ++i)
#define ALL(c) (c).begin(), (c).end()
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())
using namespace std;

using ll = long long;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline void dump(vector<T> v) { for (auto& x : v) cerr << x << " "; cerr << endl; }
template<class T> inline void dump(vector<pair<T, T>> v) { for (auto& p : v) cerr << p.first << " " << p.second << endl; }
template<class T> inline void dump(vector<vector<T>> vv) { for (auto& v : vv) {for (auto& x : v) cerr << x << " "; cerr << endl;} }
const int INF = 1e9;
const long long INFL = 1LL<<60;

template <typename T>
class SegmentTree {
  using Fx = function<T(T, T)>;
  Fx fx;
  const T init;
  int n;             // 最下段のノード数
  vector<T> node;

 public:
  SegmentTree(const vector<T>& v, Fx f, T ini) : fx(f), init(ini) {
    n = 1;
    while (n < v.size()) n <<= 1;  // N: 最下段の横幅
    node.resize(2 * n - 1, init);  // init で初期化

    // 最下段に値を挿入
    rep(i, v.size()) node[(n - 1) + i] = v[i];

    // 最下段以外を更新していく
    for (int i = n - 2; i >= 0; i--) {
      node[i] = fx(node[i * 2 + 1], node[i * 2 + 2]);
    }
  }

  void update(int i, T x) {
    i += n - 1;  // i 番目の葉のノード番号
    node[i] = x;
    while (i > 0) {
      i = (i - 1) / 2;  // ノード i の親ノードの番号に変える
      node[i] = fx(node[i * 2 + 1], node[i * 2 + 2]);  // 左右の子の min を計算し直す
    }
  }

  void add(int i, T x) {
    i += n - 1;  // i 番目の葉のノード番号
    node[i] += x;
    while (i > 0) {
      i = (i - 1) / 2;  // ノード i の親ノードの番号に変える
      node[i] = fx(node[i * 2 + 1], node[i * 2 + 2]);
    }
  }

  // [a, b) の区間に対するクエリについてノード k (区間 [l, r) 相当) が答える
  // 注意 [a, b) は半区間なので 区間 [m, n] の最小値を求めたい場合は find(m, n+1) とする
  T find(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0) r = n;

    if (r <= a || b <= l) return init;  // 区間がかぶらない場合は init を返す
    if (a <= l && r <= b) {
      return node[k];  // ノード k の担当範囲がクエリ区間 [a, b) に完全に含まれる
    } else {
      T c1 = find(a, b, 2 * k + 1, l, (l + r) / 2);  // 左の子に値を聞く
      T c2 = find(a, b, 2 * k + 2, (l + r) / 2, r);  // 右の子に値を聞く
      return fx(c1, c2);
    }
  }

  T operator[](const int& i) const {
    return node[i + n - 1];
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n = 0;
  cin >> n;
  string s;
  cin >> s;
  vector<ll> a(n + 1);
  a[0] = 0;
  rep(i, n) cin >> a[i + 1];

  vector<ll> csum_r(n + 1, 0), csum_b(n + 1, 0);
  rep(i, n) {
    csum_r[i + 1] = csum_r[i];
    csum_b[i + 1] = csum_b[i];
    if (s[i] == 'R') csum_r[i + 1] += a[i + 1];
    else csum_b[i + 1] += a[i + 1];
  }
  vector<ll> csum_r_b(n+1), csum_b_r(n+1);
  rep(i, n+1) {
    csum_r_b[i] = csum_r[i] - csum_b[i];
    csum_b_r[i] = csum_b[i] - csum_r[i];
  }

  auto f_ma = [](ll x1, ll x2) { return max(x1, x2); };
  SegmentTree<ll> seg_r_b(csum_r_b, f_ma, -INFL), seg_b_r(csum_b_r, f_ma, -INFL);

  ll ans = 0;

  rep(i, n) {
    ll tmp = seg_r_b.find(i+1, n + 1);
    chmax(ans, tmp - csum_r_b[i]);
  }
  rep(i, n) {
    ll tmp = seg_b_r.find(i+1, n + 1);
    chmax(ans, tmp - csum_b_r[i]);
  }

  cout << ans << endl;

  return 0;
}
0