結果

問題 No.2281 K → K-1 01 Flip
ユーザー magstamagsta
提出日時 2023-04-20 10:12:00
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 627 ms / 2,000 ms
コード長 2,662 bytes
コンパイル時間 2,050 ms
コンパイル使用メモリ 173,284 KB
実行使用メモリ 14,508 KB
最終ジャッジ日時 2024-04-23 10:43:14
合計ジャッジ時間 27,913 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 52 ms
10,384 KB
testcase_02 AC 482 ms
8,768 KB
testcase_03 AC 532 ms
13,228 KB
testcase_04 AC 127 ms
5,892 KB
testcase_05 AC 220 ms
10,152 KB
testcase_06 AC 434 ms
8,768 KB
testcase_07 AC 116 ms
13,216 KB
testcase_08 AC 319 ms
13,500 KB
testcase_09 AC 513 ms
6,480 KB
testcase_10 AC 393 ms
6,696 KB
testcase_11 AC 153 ms
7,384 KB
testcase_12 AC 125 ms
12,184 KB
testcase_13 AC 495 ms
6,904 KB
testcase_14 AC 267 ms
10,268 KB
testcase_15 AC 121 ms
5,376 KB
testcase_16 AC 502 ms
8,960 KB
testcase_17 AC 571 ms
10,268 KB
testcase_18 AC 523 ms
7,836 KB
testcase_19 AC 234 ms
11,876 KB
testcase_20 AC 147 ms
6,032 KB
testcase_21 AC 174 ms
8,604 KB
testcase_22 AC 475 ms
6,356 KB
testcase_23 AC 25 ms
5,376 KB
testcase_24 AC 277 ms
5,376 KB
testcase_25 AC 188 ms
5,376 KB
testcase_26 AC 171 ms
9,812 KB
testcase_27 AC 366 ms
12,952 KB
testcase_28 AC 157 ms
12,348 KB
testcase_29 AC 9 ms
5,376 KB
testcase_30 AC 303 ms
5,376 KB
testcase_31 AC 310 ms
7,956 KB
testcase_32 AC 418 ms
9,748 KB
testcase_33 AC 231 ms
8,844 KB
testcase_34 AC 358 ms
5,376 KB
testcase_35 AC 260 ms
12,536 KB
testcase_36 AC 425 ms
5,376 KB
testcase_37 AC 443 ms
8,156 KB
testcase_38 AC 450 ms
5,376 KB
testcase_39 AC 216 ms
5,376 KB
testcase_40 AC 74 ms
8,980 KB
testcase_41 AC 583 ms
11,564 KB
testcase_42 AC 608 ms
12,972 KB
testcase_43 AC 593 ms
11,816 KB
testcase_44 AC 623 ms
14,384 KB
testcase_45 AC 599 ms
12,972 KB
testcase_46 AC 524 ms
9,344 KB
testcase_47 AC 584 ms
11,564 KB
testcase_48 AC 576 ms
10,928 KB
testcase_49 AC 627 ms
14,508 KB
testcase_50 AC 576 ms
10,796 KB
testcase_51 AC 505 ms
8,192 KB
testcase_52 AC 516 ms
8,192 KB
testcase_53 AC 496 ms
8,320 KB
testcase_54 AC 494 ms
8,320 KB
testcase_55 AC 501 ms
8,320 KB
testcase_56 AC 495 ms
8,192 KB
権限があれば一括ダウンロードができます

ソースコード

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