結果

問題 No.1493 隣接xor
ユーザー ぷらぷら
提出日時 2021-06-11 19:48:13
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 406 ms / 2,000 ms
コード長 2,397 bytes
コンパイル時間 2,381 ms
コンパイル使用メモリ 211,996 KB
実行使用メモリ 13,796 KB
最終ジャッジ日時 2023-08-21 07:54:05
合計ジャッジ時間 10,481 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 406 ms
13,728 KB
testcase_04 AC 380 ms
13,660 KB
testcase_05 AC 400 ms
13,636 KB
testcase_06 AC 394 ms
13,656 KB
testcase_07 AC 393 ms
13,788 KB
testcase_08 AC 388 ms
13,796 KB
testcase_09 AC 391 ms
13,792 KB
testcase_10 AC 399 ms
13,728 KB
testcase_11 AC 394 ms
13,744 KB
testcase_12 AC 401 ms
13,680 KB
testcase_13 AC 92 ms
5,564 KB
testcase_14 AC 38 ms
5,592 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 189 ms
8,612 KB
testcase_21 AC 220 ms
8,952 KB
testcase_22 AC 149 ms
8,108 KB
testcase_23 AC 188 ms
8,512 KB
testcase_24 AC 374 ms
13,724 KB
testcase_25 AC 144 ms
8,136 KB
testcase_26 AC 227 ms
8,864 KB
testcase_27 AC 86 ms
6,052 KB
testcase_28 AC 358 ms
13,572 KB
testcase_29 AC 239 ms
8,780 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

const int mod = 1000000007;

template <typename T>
struct LazySegmentTree{
    int n;
    vector<T> dat;
    vector<T>lazy;
    LazySegmentTree() {};
    LazySegmentTree(int N) {
        n = 1;
        while(n < N) {
            n *= 2;
        }
        dat.resize(n*2-1,0);
        lazy.resize(n*2-1,0);
    }
    void eval(int k,int l,int r) {
        if(lazy[k] == 0) return;
        if(k < n-1) {
            lazy[k*2+1] += lazy[k];
            lazy[k*2+1] %= mod;
            lazy[k*2+2] += lazy[k];
            lazy[k*2+2] %= mod;
        }
        dat[k] += lazy[k];
        dat[k] %= mod;
        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;
            lazy[k] %= mod;
            eval(k,l,r);
        }
        else if(a < r && l < b) {
            update(a, b, x, k*2+1, l, (l+r)/2);
            update(a, b, x, k*2+2, (l+r)/2, r);
            dat[k] = dat[k*2+1]+dat[k*2+2];
            dat[k] %= mod;
        }
    }
    void update(int a, int b, T x) {//[a,b)
        update(a, b, x, 0, 0, n);
    }
    T query_sub(int a, int b, int k, int l, int r) {
        eval(k,l,r);
        if (r <= a || b <= l) {
            return 0;
        }
        else if (a <= l && r <= b) {
            return dat[k];
        }
        else {
            T vl = query_sub(a, b, k*2+1, l, (l+r)/2);
            T vr = query_sub(a, b, k*2+2, (l+r)/2, r);
            return (vl+vr)%mod;
        }
    }
    T query(int a, int b) {//[a,b)
        return query_sub(a, b, 0, 0, n);
    }
};

int main() {
    int N;
    cin >> N;
    vector<int>A(N),B,C(N);
    int sum = 0;
    for(int i = 0; i < N; i++) {
        cin >> A[i];
        sum ^= A[i];
        C[i] = sum;
        B.push_back(sum);
    }
    sort(B.begin(),B.end());
    B.erase(unique(B.begin(),B.end()),B.end());
    LazySegmentTree<long long>tree(B.size());
    tree.update(0,B.size(),1);
    sum = 0;
    long long ans = 0;
    for(int i = 0; i < N; i++) {
        sum ^= A[i];
        int it = lower_bound(B.begin(),B.end(),sum)-B.begin();
        long long tmp = tree.query(it,it+1);
        tree.update(it,it+1,-tmp);
        tree.update(0,B.size(),tmp);
        if(C[N-1] == C[i]) {
            ans += tmp;
            ans %= mod;
        }
    }
    cout << ans << endl;
}
0