結果
| 問題 | No.2281 K → K-1 01 Flip | 
| コンテスト | |
| ユーザー |  SSRS | 
| 提出日時 | 2023-04-21 22:19:41 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                WA
                                 
                             | 
| 実行時間 | - | 
| コード長 | 1,289 bytes | 
| コンパイル時間 | 1,960 ms | 
| コンパイル使用メモリ | 177,720 KB | 
| 実行使用メモリ | 11,648 KB | 
| 最終ジャッジ日時 | 2024-11-06 15:49:45 | 
| 合計ジャッジ時間 | 25,846 ms | 
| ジャッジサーバーID (参考情報) | judge1 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 1 | 
| other | AC * 18 WA * 38 | 
ソースコード
#include <bits/stdc++.h>
#include <atcoder/segtree>
using namespace std;
int op(int x, int y){
  return max(x, y);
}
int e(){
  return 0;
}
int main(){
  int N, Q;
  cin >> N >> Q;
  string S;
  cin >> S;
  vector<int> sum(N + 1);
  sum[0] = 0;
  for (int i = 0; i < N; i++){
    if (S[i] == '0'){
      sum[i + 1] = sum[i] + 1;
    } else {
      sum[i + 1] = sum[i] - 1;
    }
  }
  set<int> st;
  st.insert(0);
  st.insert(N);
  for (int i = 1; i < N; i++){
    if (S[i] != S[i - 1]){
      st.insert(i);
    }
  }
  vector<int> c(N, 1);
  for (int i = 1; i < N; i++){
    if (S[i] == S[i - 1]){
      c[i] = c[i - 1] + 1;
    } else {
      c[i] = 1;
    }
  }
  atcoder::segtree<int, op, e> ST(c);
  for (int i = 0; i < Q; i++){
    int L, R, K;
    cin >> L >> R >> K;
    L--;
    auto itr = st.lower_bound(L);
    if (*itr >= R){
      cout << min(R - L, K - 1) << endl;
    } else {
      bool ok = false;
      if (*itr - L >= K){
        ok = true;
      }
      if (ST.prod(*itr, R) >= K){
        ok = true;
      }
      if (!ok){
        cout << R - L << endl;
      } else {
        int s = sum[R] - sum[L];
        s %= K * 2 - 1;
        s += K * 2 - 1;
        s %= K * 2 - 1;
        s = min(s, K * 2 - 1 - s);
        cout << K * 2 - 2 - s << endl;
      }
    }
  }
}
            
            
            
        