結果

問題 No.1827 最長部分スーパーリッチ門松列列
ユーザー SSRSSSRS
提出日時 2022-01-28 21:54:10
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 582 ms / 2,000 ms
コード長 2,024 bytes
コンパイル時間 2,061 ms
コンパイル使用メモリ 203,040 KB
実行使用メモリ 48,064 KB
最終ジャッジ日時 2023-08-28 19:49:40
合計ジャッジ時間 12,113 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 18 ms
4,380 KB
testcase_04 AC 100 ms
4,380 KB
testcase_05 AC 100 ms
4,376 KB
testcase_06 AC 101 ms
4,376 KB
testcase_07 AC 423 ms
47,920 KB
testcase_08 AC 423 ms
47,916 KB
testcase_09 AC 424 ms
48,000 KB
testcase_10 AC 424 ms
47,896 KB
testcase_11 AC 424 ms
47,848 KB
testcase_12 AC 424 ms
47,956 KB
testcase_13 AC 421 ms
47,852 KB
testcase_14 AC 423 ms
47,940 KB
testcase_15 AC 422 ms
47,932 KB
testcase_16 AC 424 ms
48,064 KB
testcase_17 AC 582 ms
47,900 KB
testcase_18 AC 507 ms
47,896 KB
testcase_19 AC 422 ms
47,940 KB
testcase_20 AC 422 ms
47,936 KB
testcase_21 AC 421 ms
47,992 KB
testcase_22 AC 422 ms
47,988 KB
testcase_23 AC 422 ms
47,880 KB
testcase_24 AC 423 ms
47,880 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const long long INF = 1000000;
struct monoid{
  bool e;
  long long aa, ab, ba, bb;
  monoid(){
    e = true;
  }
  monoid(int x){
    e = false;
    if (x == 1){
      aa = 1, ab = -INF, ba = -INF, bb = -INF;
    } else {
      aa = -INF, ab = -INF, ba = -INF, bb = 1;
    }
  }
};
monoid f(monoid L, monoid R){
  if (L.e){
    return R;
  }
  if (R.e){
    return L;
  }
  monoid ans;
  ans.e = false;
  ans.aa = max({L.aa, R.aa, L.aa + R.ba, L.ab + R.aa});
  ans.ab = max({L.ab, R.ab, L.aa + R.bb, L.ab + R.ab});
  ans.ba = max({L.ba, R.ba, L.ba + R.ba, L.bb + R.aa});
  ans.bb = max({L.bb, R.bb, L.ba + R.bb, L.bb + R.ab});
  return ans;
}
struct segment_tree{
  int N;
  vector<monoid> ST;
  segment_tree(int N2){
    N = 1;
    while (N < N2){
      N *= 2;
    }
    ST = vector<monoid>(N * 2 - 1);
  }
  void update(int i, int x){
    i += N - 1;
    ST[i] = monoid(x);
    while (i > 0){
      i = (i - 1) / 2;
      ST[i] = f(ST[i * 2 + 1], ST[i * 2 + 2]);
    }
  }
  monoid all(){
    return ST[0];
  }
  monoid range_fold(int L, int R, int i, int l, int r){
    if (r <= L || R <= l){
      return monoid();
    } else if (L <= l && r <= R){
      return ST[i];
    } else {
      int m = (l + r) / 2;
      return f(range_fold(L, R, i * 2 + 1, l, m), range_fold(L, R, i * 2 + 2, m, r));
    }
  }
  monoid range_fold(int L, int R){
    return range_fold(L, R, 0, 0, N);
  }
};
int main(){
  int T;
  cin >> T;
  for (int i = 0; i < T; i++){
    int N;
    cin >> N;
    vector<int> P(N);
    for (int j = 0; j < N; j++){
      cin >> P[j];
      P[j]--;
    }
    vector<int> Q(N);
    for (int j = 0; j < N; j++){
      Q[P[j]] = j;
    }
    long long ans = 0;
    segment_tree ST(N);
    for (int j = 0; j < N; j++){
      ST.update(j, -1);
    }
    for (int j = 0; j <= N; j++){
      monoid tmp = ST.all();
      ans = max(ans, max({tmp.aa, tmp.ab, tmp.ba, tmp.bb}));
      if (j < N){
        ST.update(Q[j], 1);
      }
    }
    cout << ans << endl;
  }
}
0