답안 #866299

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
866299 2023-10-25T19:18:22 Z nima_aryan Election (BOI18_election) C++17
28 / 100
3000 ms 6744 KB
/**
 *    author:  NimaAryan
 *    created: 2023-10-25 19:58:10  
**/
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "algo/debug.h"
#endif

using i64 = long long;

struct Info {
  int pref, suff, sum;
  int best;
};
Info operator+(const Info& a, const Info& b) {
  Info res;
  res.pref = max(a.pref, a.sum + b.pref);
  res.suff = max(b.suff, b.sum + a.suff);
  res.sum = a.sum + b.sum;
  res.best = max(a.pref + b.suff, max(a.sum + b.best, b.sum + a.best));
  return res;
}
struct SegmentTree {
  vector<Info> info;
  int n;

  SegmentTree(vector<Info> init) : n(init.size()) {
    info.assign(4 << __lg(n), Info());
    auto build = [&](auto self, int p, int l, int r) -> void {
      if (r - l == 1) {
        info[p] = init[l];
        return;
      }
      int m = (l + r) / 2;
      self(self, 2 * p, l, m);
      self(self, 2 * p + 1, m, r);
      pull(p);
    };
    build(build, 1, 0, n);
  }

  void pull(int p) {
    info[p] = info[2 * p] + info[2 * p + 1];
  }

  void modify(int p, int l, int r, int x, const Info& v) {
    if (r - l == 1) {
      info[p] = v;
      return;
    }
    int m = (l + r) / 2;
    if (x < m) {
      modify(2 * p, l, m, x, v);
    } else {
      modify(2 * p + 1, m, r, x, v);
    }
    pull(p);
  }
  void modify(int x, const Info& v) {
    modify(1, 0, n, x, v);
  }

  Info query(int p, int l, int r, int lx, int rx) {
    if (l >= rx || r <= lx) {
      return Info();
    }
    if (l >= lx && r <= rx) {
      return info[p];
    }
    int m = (l + r) / 2;
    query(2 * p, l, m, lx, rx);
    query(2 * p + 1, m, r, lx, rx);
    return query(2 * p, l, m, lx, rx) +
           query(2 * p + 1, m, r, lx, rx);
  }
  Info query(int lx, int rx) {
    return query(1, 0, n, lx, rx);
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  string S;
  cin >> S;
  vector<Info> init(N);
  for (int i = 0; i < N; ++i) {
    if (S[i] == 'C') {
      init[i] = Info{0, 0, -1, 0};
    } else {
      init[i] = Info{1, 1, +1, 1};
    }
  }
  SegmentTree seg(init);
  int Q;
  cin >> Q;
  vector<int> L(Q), R(Q);
  for (int i = 0; i < Q; ++i) {
    cin >> L[i] >> R[i];
    --L[i];
  }
  for (int i = 0; i < Q; ++i) {
    cout << seg.query(L[i], R[i]).best << "\n";
  }
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 63 ms 348 KB Output is correct
2 Correct 73 ms 564 KB Output is correct
3 Correct 62 ms 348 KB Output is correct
4 Correct 66 ms 560 KB Output is correct
5 Correct 73 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 63 ms 348 KB Output is correct
2 Correct 73 ms 564 KB Output is correct
3 Correct 62 ms 348 KB Output is correct
4 Correct 66 ms 560 KB Output is correct
5 Correct 73 ms 348 KB Output is correct
6 Execution timed out 3012 ms 6744 KB Time limit exceeded
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 63 ms 348 KB Output is correct
2 Correct 73 ms 564 KB Output is correct
3 Correct 62 ms 348 KB Output is correct
4 Correct 66 ms 560 KB Output is correct
5 Correct 73 ms 348 KB Output is correct
6 Execution timed out 3012 ms 6744 KB Time limit exceeded
7 Halted 0 ms 0 KB -