結果
問題 | No.1433 Two color sequence |
ユーザー | rxoxixyxd |
提出日時 | 2021-03-19 23:03:24 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 218 ms / 2,000 ms |
コード長 | 3,855 bytes |
コンパイル時間 | 4,384 ms |
コンパイル使用メモリ | 202,820 KB |
最終ジャッジ日時 | 2025-01-19 19:19:06 |
ジャッジサーバーID (参考情報) |
judge5 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 21 |
ソースコード
#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;}