結果

問題 No.2281 K → K-1 01 Flip
ユーザー magstamagsta
提出日時 2023-04-20 10:00:59
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 2,685 bytes
コンパイル時間 2,000 ms
コンパイル使用メモリ 174,996 KB
実行使用メモリ 14,504 KB
最終ジャッジ日時 2024-04-23 10:44:47
合計ジャッジ時間 23,827 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,376 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 AC 442 ms
8,320 KB
testcase_52 AC 452 ms
8,320 KB
testcase_53 AC 426 ms
8,320 KB
testcase_54 AC 437 ms
8,448 KB
testcase_55 AC 447 ms
8,192 KB
testcase_56 AC 427 ms
8,448 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template <typename T>
struct SegTree {
	const T INF = numeric_limits<T>::max();
	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