結果
問題 | No.1788 Same Set |
ユーザー | 👑 Nachia |
提出日時 | 2021-12-17 23:45:35 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 1,581 ms / 4,000 ms |
コード長 | 2,869 bytes |
コンパイル時間 | 1,381 ms |
コンパイル使用メモリ | 95,904 KB |
実行使用メモリ | 114,756 KB |
最終ジャッジ日時 | 2024-09-15 00:45:49 |
合計ジャッジ時間 | 32,205 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 15 ms
22,016 KB |
testcase_01 | AC | 16 ms
22,016 KB |
testcase_02 | AC | 18 ms
22,016 KB |
testcase_03 | AC | 17 ms
22,016 KB |
testcase_04 | AC | 17 ms
22,016 KB |
testcase_05 | AC | 16 ms
21,888 KB |
testcase_06 | AC | 17 ms
22,016 KB |
testcase_07 | AC | 17 ms
22,016 KB |
testcase_08 | AC | 16 ms
22,016 KB |
testcase_09 | AC | 16 ms
22,016 KB |
testcase_10 | AC | 16 ms
22,016 KB |
testcase_11 | AC | 70 ms
52,432 KB |
testcase_12 | AC | 248 ms
57,064 KB |
testcase_13 | AC | 353 ms
60,272 KB |
testcase_14 | AC | 617 ms
68,736 KB |
testcase_15 | AC | 963 ms
82,048 KB |
testcase_16 | AC | 1,236 ms
94,080 KB |
testcase_17 | AC | 1,273 ms
105,704 KB |
testcase_18 | AC | 1,379 ms
108,668 KB |
testcase_19 | AC | 1,075 ms
102,244 KB |
testcase_20 | AC | 88 ms
52,224 KB |
testcase_21 | AC | 972 ms
81,920 KB |
testcase_22 | AC | 1,092 ms
84,608 KB |
testcase_23 | AC | 856 ms
80,768 KB |
testcase_24 | AC | 1,002 ms
89,224 KB |
testcase_25 | AC | 1,416 ms
101,664 KB |
testcase_26 | AC | 1,325 ms
94,748 KB |
testcase_27 | AC | 126 ms
54,272 KB |
testcase_28 | AC | 1,581 ms
110,876 KB |
testcase_29 | AC | 1,338 ms
97,632 KB |
testcase_30 | AC | 134 ms
54,528 KB |
testcase_31 | AC | 1,429 ms
103,828 KB |
testcase_32 | AC | 1,216 ms
100,160 KB |
testcase_33 | AC | 1,384 ms
105,660 KB |
testcase_34 | AC | 1,534 ms
112,008 KB |
testcase_35 | AC | 1,520 ms
113,904 KB |
testcase_36 | AC | 1,517 ms
114,756 KB |
testcase_37 | AC | 1,513 ms
114,328 KB |
testcase_38 | AC | 1,507 ms
114,356 KB |
ソースコード
#include <iostream> #include <vector> #include <algorithm> using namespace std; #define rep(i,n) for(int i=0; i<(n); i++) int segtree_N = 1 << 18; vector<int> segtree; vector<vector<pair<int,int>>> segtree_queries; vector<vector<pair<int,int>>> segtree_rollback; long long ans = 0; int N; vector<int> A; vector<int> B; vector<vector<int>> occurA; vector<vector<int>> occurB; void segtree_query(int l, int r, int t, int a=0, int b=0, int i=0){ if(i == 0){ a=0; b=segtree_N; i=1; } if(segtree[i] == 0) return; if(r <= a || b <= l) return; if(l <= a && b <= r){ segtree_rollback[t].push_back(make_pair(i,segtree[i])); segtree[i] = 0; return; } segtree_query(l, r, t, a, (a+b)/2, i*2); segtree_query(l, r, t, (a+b)/2, b, i*2+1); segtree_rollback[t].push_back(make_pair(i,segtree[i])); segtree[i] = segtree[i*2] + segtree[i*2+1]; } void run(int l, int r, int t){ sort(segtree_queries[t].begin(), segtree_queries[t].end(), [](pair<int,int> l, pair<int,int> r){ return l.second - l.first > r.second - r.first; }); for(auto [ql, qr] : segtree_queries[t]) segtree_query(ql, qr, t); if(l+1 == r){ if(t - segtree_N <= N){ ans += segtree[1] - (t-segtree_N); } } else{ run(l, (l+r)/2, t*2); run((l+r)/2, r, t*2+1); } reverse(segtree_rollback[t].begin(), segtree_rollback[t].end()); for(auto [i, v] : segtree_rollback[t]) segtree[i] = v; segtree_rollback[t].clear(); segtree_rollback[t].shrink_to_fit(); } void segtree_init(int n){ segtree_N = 1; while(segtree_N < n) segtree_N *= 2; segtree.assign(segtree_N * 2, 0); segtree_queries.assign(segtree_N * 2, {}); segtree_rollback.assign(segtree_N * 2, {}); for(int i=0; i<n; i++) segtree[segtree_N + i] = 1; for(int i=segtree_N-1; i>=1; i--) segtree[i] = segtree[i*2] + segtree[i*2+1]; } void segtree_dynamic_query(int l, int r, int tl, int tr){ tl += segtree_N; tr += segtree_N; while(tl < tr){ if(tl & 1) segtree_queries[tl++].push_back(make_pair(l,r)); if(tr & 1) segtree_queries[--tr].push_back(make_pair(l,r)); tl /= 2; tr /= 2; } } int main(){ cin >> N; A.resize(N); rep(i,N){ cin >> A[i]; A[i]--; } B.resize(N); rep(i,N){ cin >> B[i]; B[i]--; } segtree_init(N); occurA.resize(400000); occurB.resize(400000); rep(i,N) occurA[A[i]].push_back(i); rep(i,N) occurB[B[i]].push_back(i); rep(t,2){ swap(A, B); swap(occurA, occurB); rep(i,N){ int a = A[i]; if(B[i] == a) continue; auto r = lower_bound(occurB[a].begin(), occurB[a].end(), i) - occurB[a].begin(); int tl = 0; if(r != 0) tl = occurB[a][r-1] + 1; int tr = i+1; int ql = i; int qr = N; if(r != occurB[a].size()) qr = occurB[a][r]; segtree_dynamic_query(ql, qr, tl, tr); } } run(0, segtree_N, 1); cout << ans << endl; return 0; } struct ios_do_not_sync{ ios_do_not_sync(){ ios::sync_with_stdio(false); cin.tie(nullptr); } } ios_do_not_sync_instance;