結果
問題 | No.2992 Range ABCD String Query |
ユーザー |
![]() |
提出日時 | 2024-12-18 13:24:28 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 467 ms / 6,000 ms |
コード長 | 2,268 bytes |
コンパイル時間 | 792 ms |
コンパイル使用メモリ | 58,980 KB |
最終ジャッジ日時 | 2025-02-26 15:18:52 |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 41 |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:97:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 97 | scanf("%d%d%s", &n, &qn, s); | ~~~~~^~~~~~~~~~~~~~~~~~~~~~ main.cpp:105:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 105 | scanf("%d", &op); | ~~~~~^~~~~~~~~~~ main.cpp:110:12: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 110 | scanf("%d%s", &x, c), x--; | ~~~~~^~~~~~~~~~~~~~~ main.cpp:116:12: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 116 | scanf("%d%d", &l, &r), l--; | ~~~~~^~~~~~~~~~~~~~~~
ソースコード
/* -*- coding: utf-8 -*-** 2992.cc: No.2992 Range ABCD String Query - yukicoder*/#include<cstdio>#include<vector>#include<algorithm>using namespace std;/* constant */const int MAX_N = 200000;const int INF = 1 << 30;/* typedef */struct Elm {int ss[4][4];Elm(): ss() {}Elm(int d) {for (int i = 0; i < 4; i++)for (int j = i; j < 4; j++)ss[i][j] = (d < i || j < d) ? 1 : 0;}Elm operator+(const Elm &e) const {Elm r;for (int i = 0; i <= 4; i++)for (int j = i; j < 4; j++) {r.ss[i][j] = INF;for (int k = i; k <= j; k++)r.ss[i][j] = min(r.ss[i][j], ss[i][k] + e.ss[k][j]);}return r;}int val() const { return ss[0][3]; }};template <typename T>struct SegTreeSum {int e2;vector<T> nodes;T defv;SegTreeSum() {}void init(int n, T _defv) {defv = _defv;for (e2 = 1; e2 < n; e2 <<= 1);nodes.assign(e2 * 2, defv);}T &geti(int i) { return nodes[e2 - 1 + i]; }void seti(int i, T v) { geti(i) = v; }void setall() {for (int j = e2 - 2; j >= 0; j--)nodes[j] = nodes[j * 2 + 1] + nodes[j * 2 + 2];}void set(int i, T v) {int j = e2 - 1 + i;nodes[j] = v;while (j > 0) {j = (j - 1) / 2;nodes[j] = nodes[j * 2 + 1] + nodes[j * 2 + 2];}}T sum_range(int r0, int r1, int k, int i0, int i1) {if (r1 <= i0 || i1 <= r0) return defv;if (r0 <= i0 && i1 <= r1) return nodes[k];int im = (i0 + i1) / 2;T v0 = sum_range(r0, r1, k * 2 + 1, i0, im);T v1 = sum_range(r0, r1, k * 2 + 2, im, i1);return v0 + v1;}T sum_range(int r0, int r1) { return sum_range(r0, r1, 0, 0, e2); }};/* global variables */char s[MAX_N + 4];SegTreeSum<Elm> st;/* subroutines *//* main */int main() {int n, qn;scanf("%d%d%s", &n, &qn, s);st.init(n, Elm());for (int i = 0; i < n; i++) st.seti(i, Elm(s[i] - 'A'));st.setall();while (qn--) {int op;scanf("%d", &op);if (op == 1) {int x;char c[4];scanf("%d%s", &x, c), x--;st.set(x, Elm(c[0] - 'A'));}else {int l, r;scanf("%d%d", &l, &r), l--;auto e = st.sum_range(l, r);printf("%d\n", e.val());}}return 0;}