結果
| 問題 |
No.2281 K → K-1 01 Flip
|
| コンテスト | |
| ユーザー |
emthrm
|
| 提出日時 | 2023-04-22 02:29:57 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 170 ms / 2,000 ms |
| コード長 | 3,012 bytes |
| コンパイル時間 | 3,707 ms |
| コンパイル使用メモリ | 257,112 KB |
| 実行使用メモリ | 21,632 KB |
| 最終ジャッジ日時 | 2024-11-06 19:01:05 |
| 合計ジャッジ時間 | 12,576 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 56 |
ソースコード
#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;
}
emthrm