結果

問題 No.2281 K → K-1 01 Flip
ユーザー 👑 emthrmemthrm
提出日時 2023-04-22 02:29:57
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 112 ms / 2,000 ms
コード長 3,012 bytes
コンパイル時間 4,364 ms
コンパイル使用メモリ 285,820 KB
実行使用メモリ 21,760 KB
最終ジャッジ日時 2024-04-24 11:17:43
合計ジャッジ時間 12,874 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 20 ms
14,080 KB
testcase_02 AC 66 ms
11,392 KB
testcase_03 AC 85 ms
18,944 KB
testcase_04 AC 21 ms
7,168 KB
testcase_05 AC 40 ms
13,824 KB
testcase_06 AC 61 ms
11,392 KB
testcase_07 AC 35 ms
18,816 KB
testcase_08 AC 58 ms
19,072 KB
testcase_09 AC 66 ms
7,936 KB
testcase_10 AC 52 ms
8,448 KB
testcase_11 AC 25 ms
9,216 KB
testcase_12 AC 33 ms
17,408 KB
testcase_13 AC 65 ms
8,192 KB
testcase_14 AC 46 ms
14,208 KB
testcase_15 AC 17 ms
5,504 KB
testcase_16 AC 72 ms
12,160 KB
testcase_17 AC 82 ms
14,336 KB
testcase_18 AC 73 ms
9,984 KB
testcase_19 AC 45 ms
16,896 KB
testcase_20 AC 24 ms
7,168 KB
testcase_21 AC 42 ms
17,408 KB
testcase_22 AC 67 ms
7,936 KB
testcase_23 AC 6 ms
5,376 KB
testcase_24 AC 37 ms
5,376 KB
testcase_25 AC 28 ms
5,376 KB
testcase_26 AC 38 ms
15,360 KB
testcase_27 AC 64 ms
18,560 KB
testcase_28 AC 38 ms
17,664 KB
testcase_29 AC 4 ms
5,376 KB
testcase_30 AC 41 ms
5,376 KB
testcase_31 AC 54 ms
13,696 KB
testcase_32 AC 72 ms
16,000 KB
testcase_33 AC 44 ms
12,288 KB
testcase_34 AC 52 ms
5,376 KB
testcase_35 AC 56 ms
19,584 KB
testcase_36 AC 59 ms
5,376 KB
testcase_37 AC 65 ms
10,624 KB
testcase_38 AC 68 ms
5,760 KB
testcase_39 AC 36 ms
6,784 KB
testcase_40 AC 20 ms
12,288 KB
testcase_41 AC 107 ms
21,632 KB
testcase_42 AC 102 ms
21,504 KB
testcase_43 AC 107 ms
21,632 KB
testcase_44 AC 101 ms
21,504 KB
testcase_45 AC 105 ms
21,632 KB
testcase_46 AC 105 ms
21,504 KB
testcase_47 AC 104 ms
21,504 KB
testcase_48 AC 112 ms
21,504 KB
testcase_49 AC 101 ms
21,632 KB
testcase_50 AC 105 ms
21,632 KB
testcase_51 AC 99 ms
21,632 KB
testcase_52 AC 107 ms
21,760 KB
testcase_53 AC 110 ms
21,504 KB
testcase_54 AC 105 ms
21,632 KB
testcase_55 AC 106 ms
21,632 KB
testcase_56 AC 105 ms
21,504 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 998244353;
// constexpr int MOD = 1000000007;
constexpr int DY4[]{1, 0, -1, 0}, DX4[]{0, -1, 0, 1};
constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1};
constexpr int DX8[]{0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U>
inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U>
inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

template <typename Band>
struct SparseTable {
  using BinOp = std::function<Band(Band, Band)>;

  SparseTable() = default;

  explicit SparseTable(const std::vector<Band>& a, const BinOp bin_op) {
    init(a, bin_op);
  }

  void init(const std::vector<Band>& a, const BinOp bin_op_) {
    bin_op = bin_op_;
    const int n = a.size();
    assert(n > 0);
    lg.assign(n + 1, 0);
    for (int i = 2; i <= n; ++i) {
      lg[i] = lg[i >> 1] + 1;
    }
    const int table_h = std::countr_zero(std::bit_floor(a.size())) + 1;
    data.assign(table_h, std::vector<Band>(n));
    std::copy(a.begin(), a.end(), data.front().begin());
    for (int i = 1; i < table_h; ++i) {
      for (int j = 0; j + (1 << i) <= n; ++j) {
        data[i][j] = bin_op(data[i - 1][j], data[i - 1][j + (1 << (i - 1))]);
      }
    }
  }

  Band query(const int left, const int right) const {
    assert(left < right);
    const int h = lg[right - left];
    return bin_op(data[h][left], data[h][right - (1 << h)]);
  }

 private:
  BinOp bin_op;
  std::vector<int> lg;
  std::vector<std::vector<Band>> data;
};

// https://twitter.com/maspy_stars/status/1649418845071835136
int main() {
  int n, q; string s; cin >> n >> q >> s;
  array<vector<int>, 2> num{};
  REP(ch, 2) num[ch].assign(n + 1, 0);
  REP(i, n) ++num[s[i] - '0'][i + 1];
  REP(ch, 2) REP(i, n) num[ch][i + 1] += num[ch][i];
  vector<int> a(n, 0);
  for (int i = 0; i < n;) {
    int j = i + 1;
    while (j < n && s[j] == s[i]) ++j;
    for (; i < j; ++i) {
      a[i] = j - i;
    }
  }
  // REP(i, n) cout << a[i] << " \n"[i + 1 == n];
  SparseTable<int> sparse_table(a, [](const int l, const int r) -> int { return max(l, r); });
  while (q--) {
    int l, r, k; cin >> l >> r >> k; --l; --r;
    if (sparse_table.query(l, r - k + 2) < k) {
      cout << r - l + 1 << '\n';
    } else {
      const int ans = (((num[0][r + 1] - num[0][l]) - (num[1][r + 1] - num[1][l])) % (2 * k - 1) + 2 * k - 1) % (2 * k - 1);
      if (ans < k) {
        cout << k - 1 + (k - 1 - ans) << '\n';  // [0] k-1
      } else {
        cout << (ans - k) + k - 1 << '\n';  // [1] k-1
      }
    }
  }
  return 0;
}
0