結果

問題 No.2281 K → K-1 01 Flip
ユーザー magstamagsta
提出日時 2023-04-20 10:12:00
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 563 ms / 2,000 ms
コード長 2,662 bytes
コンパイル時間 1,869 ms
コンパイル使用メモリ 177,352 KB
実行使用メモリ 14,384 KB
最終ジャッジ日時 2024-10-15 10:37:38
合計ジャッジ時間 25,281 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 56
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

template <typename T>
struct SegTree {
	const T INF = 0;
	int n;
	vector<T> dat;
	SegTree(int n_) : dat(n_ * 4, INF) {
		int x = 1;
		while (n_ > x) {
			x *= 2;
		}
		n = x;
	}
	void init(T x) {
		for (int i = 0; i < 2 * n - 1; i++) dat[i] = x;
	}
	void update(int i, T x) {
		i += n - 1;
		dat[i] = x;
		while (i > 0) {
			i = (i - 1) / 2;
			dat[i] = max(dat[i * 2 + 1], dat[i * 2 + 2]);
		}
	}
	T query(int a, int b) {
		return query_sub(a, b, 0, 0, n);
	}
	T query_sub(int a, int b, int k, int l, int r) {
		if (r <= a || b <= l) {
			return INF;
		}
		else if (a <= l && r <= b) {
			return dat[k];
		}
		else {
			T vl = query_sub(a, b, k * 2 + 1, l, (l + r) / 2);
			T vr = query_sub(a, b, k * 2 + 2, (l + r) / 2, r);
			return max(vl, vr);
		}
	}
};

int main() {
	long long N, Q; cin >> N >> Q;
	string S; cin >> S;

	vector<pair<long long, long long>> rle;
	vector<long long> a(N);
	vector<long long> left;
	for (int i = 0; i < N; i++) {
		if (i == 0) {
			if (S[i] == '0') rle.push_back(make_pair(0, 1));
			if (S[i] == '1') rle.push_back(make_pair(1, 1));
		}
		else {
			if (rle[rle.size() - 1].first == 0 && S[i] == '0') rle[rle.size() - 1].second++;
			else if (rle[rle.size() - 1].first == 1 && S[i] == '0') rle.push_back(make_pair(0, 1));
			else if (rle[rle.size() - 1].first == 0 && S[i] == '1') rle.push_back(make_pair(1, 1));
			else if (rle[rle.size() - 1].first == 1 && S[i] == '1') rle[rle.size() - 1].second++;
		}
		a[i] = rle.size() - 1;
		if (rle[rle.size() - 1].second == 1) left.push_back(i);
	}

	SegTree<long long> seg(rle.size());
	for (int i = 0; i < rle.size(); i++) {
		seg.update(i, rle[i].second);
	}

	vector<long long> rui_l(N, 0), rui_r(N, 0);
	for (int i = 0; i < N; i++) {
		if (i != 0) rui_l[i] = rui_l[i - 1];
		if (S[i] == '0') rui_l[i] += 1;
		else rui_l[i] -= 1;
		if (i != 0) rui_r[N - i - 1] = rui_r[N - i];
		if (S[N - i - 1] == '0') rui_r[N - i - 1] += 1;
		else rui_r[N - i - 1] -= 1;
	}

	for (int i = 0; i < Q; i++) {
		long long L, R, K; cin >> L >> R >> K;
		
		long long max_ = 0;
		if (a[R - 1] > a[L - 1] + 1) max_ = seg.query(a[L - 1] + 1, a[R - 1]);
		if (a[L - 1] + 1 < rle.size()) max_ = max(max_, left[a[L - 1] + 1] - (L - 1));
		max_ = max(max_, (R - 1) - left[a[R - 1]] + 1);
		
		if (max_ < K) cout << R - L + 1 << endl;
		else {
			long long sum = rui_l[N - 1];
			if (L != 1) sum -= rui_l[L - 2];
			if (R != N) sum -= rui_r[R];
			long long amari = (sum + 1000000 * (2 * K - 1)) % (2 * K - 1);
			if (amari == 0) cout << 2 * (K - 1) << endl;
			else if (amari < K) cout << 2 * K - 2 - amari << endl;
			else cout << amari - 1 << endl;
		}
	}
}
0