結果
問題 | No.1788 Same Set |
ユーザー |
|
提出日時 | 2022-09-24 04:36:22 |
言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 600 ms / 4,000 ms |
コード長 | 3,044 bytes |
コンパイル時間 | 21,012 ms |
コンパイル使用メモリ | 267,404 KB |
最終ジャッジ日時 | 2025-02-07 14:11:35 |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 36 |
ソースコード
#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; }