結果

問題 No.1788 Same Set
ユーザー ぷらぷら
提出日時 2022-04-05 01:04:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,032 ms / 4,000 ms
コード長 3,972 bytes
コンパイル時間 2,619 ms
コンパイル使用メモリ 212,872 KB
実行使用メモリ 56,320 KB
最終ジャッジ日時 2024-05-04 09:58:14
合計ジャッジ時間 25,165 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 20 ms
22,144 KB
testcase_01 AC 19 ms
22,144 KB
testcase_02 AC 20 ms
22,144 KB
testcase_03 AC 19 ms
22,144 KB
testcase_04 AC 20 ms
22,144 KB
testcase_05 AC 20 ms
22,272 KB
testcase_06 AC 21 ms
22,272 KB
testcase_07 AC 20 ms
22,272 KB
testcase_08 AC 20 ms
22,144 KB
testcase_09 AC 20 ms
22,144 KB
testcase_10 AC 20 ms
22,144 KB
testcase_11 AC 264 ms
31,396 KB
testcase_12 AC 317 ms
31,600 KB
testcase_13 AC 343 ms
31,724 KB
testcase_14 AC 408 ms
32,128 KB
testcase_15 AC 506 ms
31,872 KB
testcase_16 AC 597 ms
31,872 KB
testcase_17 AC 971 ms
48,512 KB
testcase_18 AC 939 ms
39,552 KB
testcase_19 AC 951 ms
56,320 KB
testcase_20 AC 308 ms
32,512 KB
testcase_21 AC 531 ms
32,640 KB
testcase_22 AC 596 ms
34,816 KB
testcase_23 AC 546 ms
34,816 KB
testcase_24 AC 729 ms
36,224 KB
testcase_25 AC 855 ms
36,224 KB
testcase_26 AC 786 ms
36,224 KB
testcase_27 AC 498 ms
38,016 KB
testcase_28 AC 996 ms
37,888 KB
testcase_29 AC 835 ms
37,888 KB
testcase_30 AC 515 ms
37,888 KB
testcase_31 AC 908 ms
38,016 KB
testcase_32 AC 869 ms
40,448 KB
testcase_33 AC 930 ms
40,448 KB
testcase_34 AC 1,032 ms
40,576 KB
testcase_35 AC 1,002 ms
40,960 KB
testcase_36 AC 992 ms
40,832 KB
testcase_37 AC 984 ms
40,832 KB
testcase_38 AC 964 ms
40,960 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

template <typename T>
struct LazySegmentTree{
    int n;
    vector<pair<int,int>>dat;
    vector<T>lazy;
    LazySegmentTree() {};
    LazySegmentTree(int N) {
        n = 1;
        while(n < N) {
            n *= 2;
        }
        dat.resize(n*2);
        lazy.resize(n*2,0);
        for(int i = n*2-1; i >= n; i--) {
            dat[i].second = 1;
        }
        for(int i = n-1; i >= 1; i--) {
            dat[i].second = dat[i*2].second+dat[i*2+1].second;
        }
    }
    void eval(int k,int l,int r) {
        if(lazy[k] == 0) return;
        if(k < n) {
            lazy[k*2] += lazy[k];
            lazy[k*2+1] += lazy[k];
        }
        dat[k].first += lazy[k];
        lazy[k] = 0;
    }
    void update(int a, int b, T x, int k, int l, int r) {
        eval(k,l,r);
        if(a <= l && r <= b) {
            lazy[k] += x;
            eval(k,l,r);
        }
        else if(a < r && l < b) {
            update(a, b, x, k*2, l, (l+r)/2);
            update(a, b, x, k*2+1, (l+r)/2, r);
            dat[k].first = min(dat[k*2].first,dat[k*2+1].first);
            if(dat[k*2].first == dat[k*2+1].first) {
                dat[k].second = dat[k*2].second+dat[k*2+1].second;
            }
            else if(dat[k*2].first < dat[k*2+1].first) {
                dat[k].second = dat[k*2].second;
            }
            else {
                dat[k].second = dat[k*2+1].second;
            }
        }
    }
    void update(int a, int b, T x) {//[a,b)
        update(a, b, x, 1, 0, n);
    }
    pair<int,int> query_sub(int a, int b, int k, int l, int r) {
        eval(k,l,r);
        if (r <= a || b <= l) {
            return {1001001001,0};
        }
        else if (a <= l && r <= b) {
            return dat[k];
        }
        else {
            pair<int,int> vl = query_sub(a, b, k*2, l, (l+r)/2);
            pair<int,int> vr = query_sub(a, b, k*2+1, (l+r)/2, r);
            pair<int,int> vs = {min(vl.first,vr.first),0};
            if(vl.first == vr.first) {
                vs.second = vl.second+vr.second;
            }
            else if(vl.first < vr.first) {
                vs.second = vl.second;
            }
            else {
                vs.second = vr.second;
            }
            return vs;
        }
    }
    pair<int,int> query(int a, int b) {//[a,b)
        return query_sub(a, b, 1, 0, n);
    }
};

vector<int>mp1[400005],mp2[400005];
int idx1[400005],idx2[400005];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N;
    cin >> N;
    vector<int>A(N),B(N);
    for(int i = 0; i < N; i++) {
        cin >> A[i];
    }
    for(int i = 0; i < N; i++) {
        cin >> B[i];
    }
    for(int i = 0; i < N; i++) {
        mp1[A[i]].push_back(i);
        mp2[B[i]].push_back(i);
    }
    for(int i = 1; i <= 400000; i++) {
        if(mp1[i].empty() && mp2[i].empty()) {
            continue;
        }
        mp1[i].push_back(N);
        mp2[i].push_back(N);
    }
    LazySegmentTree<int>Seg(N);
    for(int i = 1; i <= 400000; i++) {
        if(mp1[i].empty() && mp2[i].empty()) {
            continue;
        }
        pair<int,int> p = minmax(mp1[i].front(),mp2[i].front());
        Seg.update(p.first,p.second,1);
    }
    long long ans = 0;
    for(int i = 0; i < N; i++) {
        pair<int,int> a = Seg.query(i,N);
        if(a.first == 0) {
            ans += a.second;
        }
        a = minmax(mp1[A[i]][idx1[A[i]]],mp2[A[i]][idx2[A[i]]]);
        Seg.update(a.first,a.second,-1);
        a = minmax(mp1[B[i]][idx1[B[i]]],mp2[B[i]][idx2[B[i]]]);
        Seg.update(a.first,a.second,-1);
        idx1[A[i]]++;
        idx2[B[i]]++;
        a = minmax(mp1[A[i]][idx1[A[i]]],mp2[A[i]][idx2[A[i]]]);
        Seg.update(a.first,a.second,1);
        if(A[i] != B[i]) {
            a = minmax(mp1[B[i]][idx1[B[i]]],mp2[B[i]][idx2[B[i]]]);
            Seg.update(a.first,a.second,1);
        }
    }
    cout << ans << endl;
}
0