結果

問題 No.2281 K → K-1 01 Flip
ユーザー square1001square1001
提出日時 2023-04-21 22:27:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,548 bytes
コンパイル時間 1,223 ms
コンパイル使用メモリ 89,444 KB
実行使用メモリ 13,200 KB
最終ジャッジ日時 2024-04-24 08:34:53
合計ジャッジ時間 22,550 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 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 440 ms
12,672 KB
testcase_52 AC 422 ms
12,544 KB
testcase_53 AC 416 ms
12,544 KB
testcase_54 AC 419 ms
12,544 KB
testcase_55 AC 424 ms
12,544 KB
testcase_56 AC 406 ms
12,544 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
 
const long long INF = 3LL << 60;
 
// range add/max lazy segment tree
class lazy_segtree {
private:
	int sz;
	vector<long long> val;
	vector<long long> lazy;
	void push(int k) {
		if (lazy[k] != 0) {
			val[k] += lazy[k];
			if (k < sz) {
				lazy[k * 2 + 0] += lazy[k];
				lazy[k * 2 + 1] += lazy[k];
			}
			lazy[k] = 0;
		}
	}
	void add(int a, int b, int k, int l, int r, long long x) {
		push(k);
		if (a <= l && r <= b) {
			lazy[k] += x;
			push(k);
			return;
		}
		if (r <= a || b <= l) {
			return;
		}
		add(a, b, k * 2 + 0, l, (l + r) >> 1, x);
		add(a, b, k * 2 + 1, (l + r) >> 1, r, x);
		val[k] = max(val[k * 2 + 0], val[k * 2 + 1]);
	}
	long long rangemax(int a, int b, int k, int l, int r) {
		push(k);
		if (a <= l && r <= b) {
			return val[k];
		}
		if (r <= a || b <= l) {
			return -INF;
		}
		long long lc = rangemax(a, b, k * 2 + 0, l, (l + r) >> 1);
		long long rc = rangemax(a, b, k * 2 + 1, (l + r) >> 1, r);
		return max(lc, rc);
	}
public:
	lazy_segtree() : sz(0), val(vector<long long>()), lazy(vector<long long>()) {}
	lazy_segtree(int n) {
		sz = 1;
		while (sz < n) {
			sz *= 2;
		}
		val = vector<long long>(sz * 2, 0);
		lazy = vector<long long>(sz * 2, 0);
	}
	void add(int l, int r, long long x) {
		add(l, r, 1, 0, sz, x);
	}
	long long rangemax(int l, int r) {
		return rangemax(l, r, 1, 0, sz);
	}
};

int main() {
	int N, Q; string S;
	cin >> N >> Q >> S;
	vector<int> A = { 0 };
	for (int i = 1; i < N; i++) {
		if (S[i - 1] != S[i]) {
			A.push_back(i);
		}
	}
	A.push_back(N);
	vector<int> sum(N + 1);
	for (int i = 0; i < N; i++) {
		sum[i + 1] = sum[i] + (S[i] == '1' ? 1 : -1);
	}
	lazy_segtree seg(N);
	for (int i = 0; i < int(A.size()) - 1; i++) {
		seg.add(A[i], A[i + 1], A[i + 1] - A[i]);
	}
	for (int i = 0; i < Q; i++) {
		int L, R, K;
		cin >> L >> R >> K;
		L -= 1;
		int p1 = lower_bound(A.begin(), A.end(), L + 1) - A.begin();
		int p2 = lower_bound(A.begin(), A.end(), R) - A.begin();
		int maxwidth = 0;
		if (p1 == p2) {
			maxwidth = R - L;
		}
		else if (p2 - p1 == 1) {
			maxwidth = max(A[p1] - L, R - A[p2 - 1]);
		}
		else {
			maxwidth = max(A[p1] - L, R - A[p2 - 1]);
			maxwidth = max(maxwidth, (int)seg.rangemax(p1, p2 - 1));
		}
		if (maxwidth < K) {
			cout << R - L << '\n';
		}
		else {
			int x = sum[R] - sum[L];
			x = (x >= 0 ? x % (2 * K - 1) : ((2 * K - 1) - (-x) % (2 * K - 1)) % (2 * K - 1));
			cout << max(x - 1, (2 * K - 1) - x - 1) << '\n';
		}
	}
	return 0;
}
0