結果

問題 No.619 CardShuffle
ユーザー finefine
提出日時 2019-05-02 17:15:34
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 204 ms / 3,000 ms
コード長 5,041 bytes
コンパイル時間 1,849 ms
コンパイル使用メモリ 175,084 KB
実行使用メモリ 7,988 KB
最終ジャッジ日時 2023-08-30 04:50:12
合計ジャッジ時間 6,756 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 140 ms
7,740 KB
testcase_17 AC 133 ms
7,740 KB
testcase_18 AC 140 ms
7,812 KB
testcase_19 AC 137 ms
7,800 KB
testcase_20 AC 129 ms
7,792 KB
testcase_21 AC 70 ms
7,744 KB
testcase_22 AC 61 ms
7,796 KB
testcase_23 AC 70 ms
7,800 KB
testcase_24 AC 61 ms
7,744 KB
testcase_25 AC 57 ms
7,796 KB
testcase_26 AC 204 ms
7,784 KB
testcase_27 AC 202 ms
7,740 KB
testcase_28 AC 204 ms
7,800 KB
testcase_29 AC 202 ms
7,988 KB
testcase_30 AC 193 ms
7,804 KB
testcase_31 AC 2 ms
4,384 KB
testcase_32 AC 131 ms
7,800 KB
testcase_33 AC 140 ms
7,804 KB
testcase_34 AC 194 ms
7,852 KB
testcase_35 AC 129 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

const ll MOD = 1000000007;

struct Monoid {
    ll a;
    ll b;
    ll c;
    bool f;
    Monoid() : a(0), b(0), c(1), f(true) {}
    Monoid(ll a_, ll b_, ll c_, bool f_) : a(a_), b(b_), c(c_), f(f_) {}

    ll get(bool f2) {
        return (a + b + c * f2) % MOD;
    }
};

Monoid operator+(const Monoid& m1, const Monoid& m2) {
    if (m2.f) return Monoid(m1.a, m1.b, m1.c * m2.c % MOD, m1.f);
    if (m1.f) return Monoid(m1.c * m2.a % MOD, m2.b, m2.c, false);
    return Monoid(m1.a, (m1.b + m1.c * m2.a % MOD + m2.b) % MOD, m2.c, false);
}

template <typename T>
struct SegmentTree {
    int n;
    vector<T> data;
    T INITIAL_VALUE;

    //使うときは、この2つを適宜変更する
    static T merge(T x, T y);
    void updateNode(int k, T x);

    SegmentTree(int size, T initial_value) {
        n = 1;
        INITIAL_VALUE = initial_value;
        while (n < size) n *= 2;
        data.resize(2 * n - 1, INITIAL_VALUE);
    }

    T getLeaf(int k) {
        return data[k + n - 1];
    }

    void update(int k, T x) {
        k += n - 1; //葉の節点
        updateNode(k, x);
        while (k > 0) {
            k = (k - 1) / 2;
            data[k] = merge(data[k * 2 + 1], data[k * 2 + 2]);
        }
    }

    //区間[a, b)に対するクエリに答える
    //k:節点番号, [l, r):節点に対応する区間
    T query(int a, int b, int k, int l, int r) {
        //[a, b)と[l, r)が交差しない場合
        if (r <= a || b <= l) return INITIAL_VALUE;
        //[a, b)が[l, r)を含む場合、節点の値
        if (a <= l && r <= b) return data[k];
        else {
            //二つの子をマージ
            T vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
            T vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
            return merge(vl, vr);
        }
    }

    //外から呼ぶ用
    T query(int a, int b) {
        return query(a, b, 0, 0, n);
    }

    //非再帰版: バグってるかもしれないので定数倍高速化する時以外使わないで
    //区間[a, b)に対するクエリに答える
    T query_fast(int a, int b) {
        T vl = INITIAL_VALUE, vr = INITIAL_VALUE;
        for (int l = a + n, r = b + n; l != r; l >>= 1, r >>= 1) {
            if (l & 1) vl = merge(vl, data[l++ - 1]);
            if (r & 1) vr = merge(data[--r - 1], vr);
        }
        return merge(vl, vr);
    }
};

//使うときは以下2つを変更
//非可換の場合は順序に注意!!!
template <typename T>
T SegmentTree<T>::merge(T x, T y) {
    return x + y;
}

template <typename T>
void SegmentTree<T>::updateNode(int k, T x) {
    data[k] = x;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    n = (n + 1) / 2;
    string s = "";
    vector<ll> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        if (i < n - 1) {
            char c;
            cin >> c;
            s += c;
        }
    }

    s += '+';

    SegmentTree<Monoid> st(n, Monoid());
    for (int i = 0; i < n; i++) {
        if (s[i] == '*') st.update(i, Monoid(0, 0, a[i], true));
        else st.update(i, Monoid(a[i], 0, 1, false));
    }

    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        char t;
        int x, y;
        cin >> t >> x >> y;
        x--; y--;
        bool f = (x % 2 == 0);
        x /= 2;
        y /= 2;
        if (t == '!') {
            Monoid tmp1 = st.getLeaf(x);
            Monoid tmp2 = st.getLeaf(y);
            if (f) {
                ll v1 = (tmp1.f ? tmp1.c : tmp1.a);
                ll v2 = (tmp2.f ? tmp2.c : tmp2.a);
                (tmp1.f ? tmp1.c : tmp1.a) = v2;
                (tmp2.f ? tmp2.c : tmp2.a) = v1;

                st.update(x, tmp1);
                st.update(y, tmp2);
            } else {
                char c1 = (tmp1.f ? '*' : '+');
                char c2 = (tmp2.f ? '*' : '+');
                if (c1 == c2) continue;

                if (c1 == '*') {
                    s[x] = '+';
                    tmp1.a = tmp1.c;
                    tmp1.c = 1;
                    tmp1.f = false;
                } else {
                    s[x] = '*';
                    tmp1.c = tmp1.a;
                    tmp1.a = 0;
                    tmp1.f = true;
                }
                st.update(x, tmp1);

                if (c2 == '*') {
                    s[y] = '+';
                    tmp2.a = tmp2.c;
                    tmp2.c = 1;
                    tmp2.f = false;
                } else {
                    s[y] = '*';
                    tmp2.c = tmp2.a;
                    tmp2.a = 0;
                    tmp2.f = true;
                }
                st.update(y, tmp2);               
            }
        } else {
            Monoid m = st.query_fast(x, y + 1);
            ll ans = m.get(s[y] == '*');
            //cerr << m.a << ", " << m.b << ", " << m.c << "," << m.f << endl;
            cout << ans << endl;
        }
    }
    return 0;
}
0