結果

問題 No.1788 Same Set
ユーザー KudeKude
提出日時 2021-12-17 08:16:21
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 426 ms / 4,000 ms
コード長 1,798 bytes
コンパイル時間 2,224 ms
コンパイル使用メモリ 222,680 KB
最終ジャッジ日時 2025-01-27 01:51:18
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic pop
using namespace std;
using namespace atcoder;
#define rep(i,n)for (int i = 0; i < int(n); ++i)
#define rrep(i,n)for (int i = int(n)-1; i >= 0; --i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template<class T> void chmax(T& a, const T& b) { a = max(a, b); }
template<class T> void chmin(T& a, const T& b) { a = min(a, b); }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;

constexpr int INF = 1001001001;
struct S { int min = INF, cnt = 0; };
S op(S x, S y) {
  if (x.min == y.min) return {x.min, x.cnt + y.cnt};
  else if (x.min < y.min) return x;
  else return y;
}
S e() { return S(); }
int composition(int f, int g) { return f + g; }
int id() { return 0; }
S mapping(int f, S x) { return {x.min + f, x.cnt}; }

} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  VI a(n), b(n);
  rep(i, n) cin >> a[i];
  rep(i, n) cin >> b[i];
  lazy_segtree<S, op, e, int, mapping, composition, id> seg(vector<S>(n, S{0, 1}));
  constexpr int MX = 400000;
  VI last_a(MX + 1, -1), last_b(MX + 1, -1);
  ll ans = 0;
  rep(i, n) {
    int x = a[i], y = b[i];
    if (last_a[x] < last_b[x]) seg.apply(last_a[x] + 1, last_b[x] + 1, -1);
    if (last_b[y] < last_a[y]) seg.apply(last_b[y] + 1, last_a[y] + 1, -1);
    if (x != y) {
      seg.apply(max(last_a[x], last_b[x]) + 1, i + 1, 1);
      seg.apply(max(last_a[y], last_b[y]) + 1, i + 1, 1);
    }
    last_a[x] = i;
    last_b[y] = i;
    auto [mn, cnt] = seg.prod(0, i + 1);
    if (mn == 0) ans += cnt;
  }
  cout << ans << '\n';
}
0