結果

問題 No.1788 Same Set
ユーザー SSRSSSRS
提出日時 2021-12-17 00:24:06
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 829 ms / 4,000 ms
コード長 2,515 bytes
コンパイル時間 1,473 ms
コンパイル使用メモリ 172,176 KB
実行使用メモリ 14,048 KB
最終ジャッジ日時 2023-10-12 00:29:48
合計ジャッジ時間 22,207 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,188 KB
testcase_01 AC 3 ms
6,188 KB
testcase_02 AC 4 ms
6,308 KB
testcase_03 AC 3 ms
6,144 KB
testcase_04 AC 3 ms
6,188 KB
testcase_05 AC 3 ms
6,192 KB
testcase_06 AC 4 ms
6,252 KB
testcase_07 AC 3 ms
6,372 KB
testcase_08 AC 4 ms
6,244 KB
testcase_09 AC 3 ms
6,156 KB
testcase_10 AC 3 ms
6,192 KB
testcase_11 AC 442 ms
13,776 KB
testcase_12 AC 459 ms
13,800 KB
testcase_13 AC 470 ms
13,780 KB
testcase_14 AC 511 ms
13,828 KB
testcase_15 AC 582 ms
13,892 KB
testcase_16 AC 655 ms
13,748 KB
testcase_17 AC 601 ms
13,848 KB
testcase_18 AC 682 ms
13,852 KB
testcase_19 AC 523 ms
13,856 KB
testcase_20 AC 574 ms
13,732 KB
testcase_21 AC 644 ms
13,760 KB
testcase_22 AC 670 ms
13,748 KB
testcase_23 AC 674 ms
13,752 KB
testcase_24 AC 742 ms
13,780 KB
testcase_25 AC 775 ms
13,868 KB
testcase_26 AC 744 ms
13,792 KB
testcase_27 AC 675 ms
13,748 KB
testcase_28 AC 829 ms
13,848 KB
testcase_29 AC 764 ms
13,736 KB
testcase_30 AC 701 ms
13,896 KB
testcase_31 AC 771 ms
13,896 KB
testcase_32 AC 728 ms
13,792 KB
testcase_33 AC 705 ms
13,776 KB
testcase_34 AC 759 ms
13,852 KB
testcase_35 AC 702 ms
13,860 KB
testcase_36 AC 729 ms
14,048 KB
testcase_37 AC 725 ms
13,748 KB
testcase_38 AC 716 ms
13,736 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000;
struct monoid{
  int mn, cnt;
  monoid(){
    mn = INF, cnt = 0;
  }
  monoid(int x){
    mn = x, cnt = 1;
  }
};
monoid f(monoid A, monoid B){
  monoid C;
  C.mn = min(A.mn, B.mn);
  if (A.mn == C.mn){
    C.cnt += A.cnt;
  }
  if (B.mn == C.mn){
    C.cnt += B.cnt;
  }
  return C;
}
struct lazy_segment_tree{
  int N;
  vector<monoid> ST;
  vector<int> lazy;
  lazy_segment_tree(int N2){
    N = 1;
    while (N < N2){
      N *= 2;
    }
    ST = vector<monoid>(N * 2 - 1, monoid(0));
    for (int i = N - 2; i >= 0; i--){
      ST[i] = f(ST[i * 2 + 1], ST[i * 2 + 2]);
    }
    lazy = vector<int>(N * 2 - 1, 0);
  }
  void eval(int i){
    if (i < N - 1){
      lazy[i * 2 + 1] += lazy[i];
      lazy[i * 2 + 2] += lazy[i];
    }
    ST[i].mn += lazy[i];
    lazy[i] = 0;
  }
  void range_add(int L, int R, int x, int i, int l, int r){
    eval(i);
    if (r <= L || R <= l){
      return;
    } else if (L <= l && r <= R){
      lazy[i] += x;
      eval(i);
    } else {
      int m = (l + r) / 2;
      range_add(L, R, x, i * 2 + 1, l, m);
      range_add(L, R, x, i * 2 + 2, m, r);
      ST[i] = f(ST[i * 2 + 1], ST[i * 2 + 2]);
    }
  }
  void range_add(int L, int R, int x){
    range_add(L, R, x, 0, 0, N);
  }
  monoid range_fold(int L, int R, int i, int l, int r){
    eval(i);
    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 N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++){
    cin >> A[i];
    A[i]--;
  }
  vector<int> B(N);
  for (int i = 0; i < N; i++){
    cin >> B[i];
    B[i]--;
  }
  long long ans = 0;
  vector<int> Af(400000, N), Bf(400000, N);
  lazy_segment_tree ST(N);
  for (int i = N - 1; i >= 0; i--){
    //[Af,Bf) or [Bf,Af) は NG
    //range add x, rane-min-cnt
    ST.range_add(min(Af[A[i]], Bf[A[i]]), max(Af[A[i]], Bf[A[i]]), -1);
    Af[A[i]] = i;
    ST.range_add(min(Af[A[i]], Bf[A[i]]), max(Af[A[i]], Bf[A[i]]), 1);
    ST.range_add(min(Af[B[i]], Bf[B[i]]), max(Af[B[i]], Bf[B[i]]), -1);
    Bf[B[i]] = i;
    ST.range_add(min(Af[B[i]], Bf[B[i]]), max(Af[B[i]], Bf[B[i]]), 1);
    monoid res = ST.range_fold(i, N);
    if (res.mn == 0){
      ans += res.cnt;
    }
  }
  cout << ans << endl;
}
0