結果
問題 | No.1788 Same Set |
ユーザー | 👑 hos.lyric |
提出日時 | 2021-12-17 00:26:34 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 1,212 ms / 4,000 ms |
コード長 | 4,347 bytes |
コンパイル時間 | 1,237 ms |
コンパイル使用メモリ | 111,964 KB |
実行使用メモリ | 22,068 KB |
最終ジャッジ日時 | 2024-09-14 00:27:16 |
合計ジャッジ時間 | 23,542 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 3 ms
9,808 KB |
testcase_01 | AC | 3 ms
7,888 KB |
testcase_02 | AC | 4 ms
7,888 KB |
testcase_03 | AC | 3 ms
7,888 KB |
testcase_04 | AC | 3 ms
7,756 KB |
testcase_05 | AC | 3 ms
7,888 KB |
testcase_06 | AC | 3 ms
7,892 KB |
testcase_07 | AC | 3 ms
9,808 KB |
testcase_08 | AC | 3 ms
7,772 KB |
testcase_09 | AC | 2 ms
6,944 KB |
testcase_10 | AC | 2 ms
6,944 KB |
testcase_11 | AC | 177 ms
13,428 KB |
testcase_12 | AC | 208 ms
13,304 KB |
testcase_13 | AC | 229 ms
13,340 KB |
testcase_14 | AC | 273 ms
15,348 KB |
testcase_15 | AC | 378 ms
13,568 KB |
testcase_16 | AC | 510 ms
13,332 KB |
testcase_17 | AC | 1,194 ms
19,128 KB |
testcase_18 | AC | 1,040 ms
17,872 KB |
testcase_19 | AC | 1,212 ms
22,068 KB |
testcase_20 | AC | 260 ms
14,548 KB |
testcase_21 | AC | 410 ms
15,600 KB |
testcase_22 | AC | 524 ms
16,476 KB |
testcase_23 | AC | 568 ms
16,156 KB |
testcase_24 | AC | 799 ms
15,996 KB |
testcase_25 | AC | 840 ms
15,996 KB |
testcase_26 | AC | 733 ms
16,316 KB |
testcase_27 | AC | 426 ms
16,348 KB |
testcase_28 | AC | 1,074 ms
16,944 KB |
testcase_29 | AC | 743 ms
16,092 KB |
testcase_30 | AC | 419 ms
15,996 KB |
testcase_31 | AC | 886 ms
16,036 KB |
testcase_32 | AC | 774 ms
15,964 KB |
testcase_33 | AC | 913 ms
16,148 KB |
testcase_34 | AC | 1,092 ms
16,784 KB |
testcase_35 | AC | 1,184 ms
18,244 KB |
testcase_36 | AC | 1,155 ms
17,296 KB |
testcase_37 | AC | 1,146 ms
17,192 KB |
testcase_38 | AC | 1,055 ms
16,508 KB |
ソースコード
#include <cassert> #include <cmath> #include <cstdint> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <bitset> #include <complex> #include <deque> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <string> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; using Int = long long; template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; }; template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; } template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; } template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; } template <class T> void bAdd(vector<T> &bit, int pos, const T &val) { const int bitN = bit.size(); for (int x = pos; x < bitN; x |= x + 1) bit[x] += val; } template <class T> T bSum(const vector<T> &bit, int pos) { T ret = 0; for (int x = pos - 1; x >= 0; x = (x & (x + 1)) - 1) ret += bit[x]; return ret; } template <class T> T bSum(const vector<T> &bit, int pos0, int pos1) { return bSum(bit, pos1) - bSum(bit, pos0); } constexpr int MAX = 400'010; int N; vector<int> A, B; Int ans; int lsA[MAX], lsB[MAX], rsA[MAX], rsB[MAX]; bool onA[MAX], onB[MAX]; void solve(int l, int r) { if (r - l == 1) { if (A[l] == B[l]) { ++ans; } } else { const int mid = (l + r) / 2; solve(l, mid); solve(mid, r); // cerr<<"solve "<<l<<" "<<mid<<" "<<r<<endl; const int X = mid - l, Y = r - mid; for (int i = l; i < r; ++i) { lsA[A[i]] = lsA[B[i]] = lsB[A[i]] = lsB[B[i]] = X + 1; rsA[A[i]] = rsA[B[i]] = rsB[A[i]] = rsB[B[i]] = Y + 1; } for (int i = l; i < mid; ++i) { lsA[A[i]] = lsB[B[i]] = mid - i; } for (int i = r; --i >= mid; ) { rsA[A[i]] = rsB[B[i]] = i + 1 - mid; } // cerr<<" lsA = ";pv(lsA,lsA+10); // cerr<<" lsB = ";pv(lsB,lsB+10); // cerr<<" rsA = ";pv(rsA,rsA+10); // cerr<<" rsB = ";pv(rsB,rsB+10); vector<int> ps(X + 1, 0), qs(Y + 1, 0); { for (int i = l; i < mid; ++i) { onA[A[i]] = onA[B[i]] = onB[A[i]] = onB[B[i]] = false; } multiset<int> mxs; mxs.insert(1); for (int i = mid; --i >= l; ) { if (!onA[A[i]]) { if (onB[A[i]]) { mxs.erase(mxs.find(rsA[A[i]])); } else { mxs.insert(rsB[A[i]]); } onA[A[i]] = true; } if (!onB[B[i]]) { if (onA[B[i]]) { mxs.erase(mxs.find(rsB[B[i]])); } else { mxs.insert(rsA[B[i]]); } onB[B[i]] = true; } ps[mid - i] = *mxs.rbegin(); } } { for (int i = mid; i < r; ++i) { onA[A[i]] = onA[B[i]] = onB[A[i]] = onB[B[i]] = false; } multiset<int> mxs; mxs.insert(1); for (int i = mid; i < r; ++i) { if (!onA[A[i]]) { if (onB[A[i]]) { mxs.erase(mxs.find(lsA[A[i]])); } else { mxs.insert(lsB[A[i]]); } onA[A[i]] = true; } if (!onB[B[i]]) { if (onA[B[i]]) { mxs.erase(mxs.find(lsB[B[i]])); } else { mxs.insert(lsA[B[i]]); } onB[B[i]] = true; } qs[i - mid + 1] = *mxs.rbegin(); } } // cerr<<" ps = ";pv(ps.begin(),ps.end()); // cerr<<" qs = ";pv(qs.begin(),qs.end()); /* (x, y) y >= ps[x] x >= qs[y] */ vector<vector<int>> xss(Y + 1); for (int x = 1; x <= X; ++x) { if (ps[x] <= Y) { xss[ps[x]].push_back(x); } } vector<int> bit(X + 1, 0); for (int y = Y; y >= 1; --y) { if (qs[y] <= X) { bAdd(bit, qs[y], 1); } for (const int x : xss[y]) { ans += bSum(bit, x + 1); } } } } int main() { for (; ~scanf("%d", &N); ) { A.resize(N); for (int i = 0; i < N; ++i) scanf("%d", &A[i]); B.resize(N); for (int i = 0; i < N; ++i) scanf("%d", &B[i]); ans = 0; solve(0, N); printf("%lld\n", ans); } return 0; }