結果
問題 | No.1788 Same Set |
ユーザー | 👑 Nachia |
提出日時 | 2021-12-17 00:49:53 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 1,265 ms / 4,000 ms |
コード長 | 2,903 bytes |
コンパイル時間 | 1,260 ms |
コンパイル使用メモリ | 93,572 KB |
実行使用メモリ | 116,096 KB |
最終ジャッジ日時 | 2024-09-14 00:49:42 |
合計ジャッジ時間 | 27,362 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 11 ms
21,964 KB |
testcase_01 | AC | 11 ms
21,836 KB |
testcase_02 | AC | 11 ms
21,892 KB |
testcase_03 | AC | 11 ms
21,832 KB |
testcase_04 | AC | 10 ms
21,864 KB |
testcase_05 | AC | 10 ms
21,832 KB |
testcase_06 | AC | 10 ms
22,052 KB |
testcase_07 | AC | 10 ms
22,220 KB |
testcase_08 | AC | 11 ms
21,952 KB |
testcase_09 | AC | 11 ms
22,000 KB |
testcase_10 | AC | 11 ms
21,992 KB |
testcase_11 | AC | 56 ms
52,432 KB |
testcase_12 | AC | 229 ms
57,060 KB |
testcase_13 | AC | 325 ms
60,276 KB |
testcase_14 | AC | 572 ms
68,732 KB |
testcase_15 | AC | 838 ms
81,848 KB |
testcase_16 | AC | 1,021 ms
93,980 KB |
testcase_17 | AC | 1,036 ms
105,580 KB |
testcase_18 | AC | 1,136 ms
108,680 KB |
testcase_19 | AC | 852 ms
102,772 KB |
testcase_20 | AC | 72 ms
52,144 KB |
testcase_21 | AC | 851 ms
81,844 KB |
testcase_22 | AC | 973 ms
84,616 KB |
testcase_23 | AC | 728 ms
80,668 KB |
testcase_24 | AC | 839 ms
89,132 KB |
testcase_25 | AC | 1,170 ms
101,980 KB |
testcase_26 | AC | 1,162 ms
94,880 KB |
testcase_27 | AC | 113 ms
54,296 KB |
testcase_28 | AC | 1,255 ms
110,592 KB |
testcase_29 | AC | 1,184 ms
98,812 KB |
testcase_30 | AC | 117 ms
54,456 KB |
testcase_31 | AC | 1,201 ms
103,948 KB |
testcase_32 | AC | 1,097 ms
103,240 KB |
testcase_33 | AC | 1,181 ms
106,396 KB |
testcase_34 | AC | 1,265 ms
111,532 KB |
testcase_35 | AC | 1,232 ms
114,028 KB |
testcase_36 | AC | 1,230 ms
114,744 KB |
testcase_37 | AC | 1,234 ms
114,072 KB |
testcase_38 | AC | 1,235 ms
116,096 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){ for(auto [ql, qr] : segtree_queries[t]) segtree_query(ql, qr, t); if(l+1 == r){ if(t - segtree_N <= N){ //cout << "t = " << (t-segtree_N) << ", dans = " << (segtree[1] - (t-segtree_N)) << endl; 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]; //cout << "tl = " << tl << ", tr = " << tr << ", ql = " << ql << ", qr = " << qr << endl; 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;