結果

問題 No.1788 Same Set
ユーザー chineristACchineristAC
提出日時 2022-09-24 04:36:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 560 ms / 4,000 ms
コード長 3,044 bytes
コンパイル時間 6,400 ms
コンパイル使用メモリ 225,908 KB
実行使用メモリ 14,620 KB
最終ジャッジ日時 2023-08-23 21:15:56
合計ジャッジ時間 23,186 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,504 KB
testcase_01 AC 4 ms
6,300 KB
testcase_02 AC 4 ms
6,280 KB
testcase_03 AC 3 ms
6,296 KB
testcase_04 AC 4 ms
6,256 KB
testcase_05 AC 4 ms
6,220 KB
testcase_06 AC 4 ms
6,324 KB
testcase_07 AC 4 ms
6,292 KB
testcase_08 AC 4 ms
6,252 KB
testcase_09 AC 4 ms
6,304 KB
testcase_10 AC 3 ms
6,244 KB
testcase_11 AC 197 ms
14,404 KB
testcase_12 AC 281 ms
14,360 KB
testcase_13 AC 318 ms
14,440 KB
testcase_14 AC 409 ms
14,372 KB
testcase_15 AC 467 ms
14,472 KB
testcase_16 AC 485 ms
14,336 KB
testcase_17 AC 364 ms
14,620 KB
testcase_18 AC 444 ms
14,404 KB
testcase_19 AC 270 ms
14,376 KB
testcase_20 AC 257 ms
14,368 KB
testcase_21 AC 483 ms
14,364 KB
testcase_22 AC 510 ms
14,476 KB
testcase_23 AC 437 ms
14,340 KB
testcase_24 AC 485 ms
14,336 KB
testcase_25 AC 549 ms
14,472 KB
testcase_26 AC 560 ms
14,396 KB
testcase_27 AC 313 ms
14,380 KB
testcase_28 AC 534 ms
14,376 KB
testcase_29 AC 545 ms
14,420 KB
testcase_30 AC 313 ms
14,400 KB
testcase_31 AC 534 ms
14,348 KB
testcase_32 AC 484 ms
14,420 KB
testcase_33 AC 474 ms
14,476 KB
testcase_34 AC 495 ms
14,364 KB
testcase_35 AC 471 ms
14,340 KB
testcase_36 AC 470 ms
14,528 KB
testcase_37 AC 467 ms
14,336 KB
testcase_38 AC 470 ms
14,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <random>
#include <stdio.h>
#include <fstream>
#include <functional>
#include <cassert>
#include <atcoder/all>


using namespace std;
using namespace atcoder;


#define rep(i,n) for (int i=0;i<n;i+=1)
#define append push_back
#define all(x) (x).begin(), (x).end()

template<class T>
using vec = vector<T>;
template<class T>
using vvec = vec<vec<T>>;
template<class T>
using vvvec = vec<vvec<T>>;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;


template<class T>
bool chmin(T &a, T b){
  if (a>b){
    a = b;
    return true;
  }
  return false;
}

template<class T>
bool chmax(T &a, T b){
  if (a<b){
    a = b;
    return true;
  }
  return false;
}

template<class T>
T sum(vec<T> x){
  T res=0;
  for (auto e:x){
    res += e;
  }
  return res;
}

template<class T>
void printv(vec<T> x){
  for (auto e:x){
    cout<<e<<" ";
  }
  cout<<"\n";
}

template<class T>
ostream& operator<<(ostream& os, const vec<T>& A){
  os << "[";
  rep(i,A.size()){
    os << A[i];
    if (i!=A.size()-1){
      os << ", ";
    }
  }
  os << "]" ;
  return os;
}

template<class T>
ostream& operator<<(ostream& os, const deque<T>& A){
  os << "deque{[";
  rep(i,A.size()){
    os << A[i];
    if (i!=A.size()-1){
      os << ", ";
    }
  }
  os << "]}" ;
  return os;
}

template<class T>
ostream& operator<<(ostream& os, const pair<T,T>& A){
  os << "(";
  os << A.first ;
  os << ", ";
  os << A.second;
  os << ")";
  return os;
}

struct S{
  int val,cnt;
};

S op(S a,S b){
  if (a.val < b.val){
    return a;
  }
  if (b.val < a.val){
    return b;
  }
  return {a.val,a.cnt+b.cnt};
}

S e(){
  return {(int)1e9,0};
}

using F = int;

S mapping(F f,S x){
  return {x.val+f,x.cnt};
}

F composition(F f,F g){
  return f+g;
}

F id(){
  return 0;
}



int main() {

  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);

  int N;
  cin>>N;
  vec<int> A(N),B(N);
  rep(i,N){cin>>A[i];}
  rep(i,N){cin>>B[i];}

  int M = 4e5;

  vec<int> first_A(M+1,N);
  vec<int> first_B(M+1,N);

  vec<S> init(N,{0,1});
  lazy_segtree<S,op,e,F,mapping,composition,id> seg(init);

  ll res = 0;
  int l,r;
  for (int i=N-1;0<=i;i--){
    l = min(first_A[A[i]],first_B[A[i]]);
    r = max(first_A[A[i]],first_B[A[i]]);
    seg.apply(l,r,-1);

    first_A[A[i]] = i;
    l = min(first_A[A[i]],first_B[A[i]]);
    r = max(first_A[A[i]],first_B[A[i]]);
    seg.apply(l,r,1);

    l = min(first_A[B[i]],first_B[B[i]]);
    r = max(first_A[B[i]],first_B[B[i]]);
    seg.apply(l,r,-1);

    first_B[B[i]] = i;
    l = min(first_A[B[i]],first_B[B[i]]);
    r = max(first_A[B[i]],first_B[B[i]]);
    seg.apply(l,r,1);

    S tmp = seg.prod(i,N);
    //cout << tmp.val << " " << tmp.cnt << endl;
    if (tmp.val==0){
      res += (ll)tmp.cnt;
    }
  }

  cout << res << endl;




       

}
0