結果

問題 No.426 往復漸化式
ユーザー kimiyuki
提出日時 2017-03-10 00:43:54
言語 C++14
(gcc 6.3.0)
結果
AC  
実行時間 458 ms
コード長 4098 Byte
コンパイル時間 1291 ms
使用メモリ 20824 KB

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
00_sample1.txt AC 7 ms
1528 KB
10_small1.txt AC 4 ms
1552 KB
10_small2.txt AC 4 ms
1548 KB
20_medium1.txt AC 9 ms
1552 KB
20_medium2.txt AC 9 ms
1548 KB
30_large1.txt AC 63 ms
3928 KB
30_large2.txt AC 59 ms
3924 KB
40_max1.txt AC 405 ms
20824 KB
40_max2.txt AC 399 ms
20820 KB
50_true_max1.txt AC 432 ms
20816 KB
50_true_max2.txt AC 429 ms
20820 KB
80_handmadeX1.txt AC 399 ms
20824 KB
80_handmadeX2.txt AC 437 ms
20820 KB
80_handmadeX3.txt AC 458 ms
20820 KB
80_handmadeX4.txt AC 446 ms
20820 KB
90_handmadeY1.txt AC 408 ms
20824 KB
90_handmadeY2.txt AC 439 ms
20824 KB
90_handmadeY3.txt AC 449 ms
20820 KB
90_handmadeY4.txt AC 451 ms
20824 KB
99_handmadeZ1.txt AC 424 ms
20820 KB
99_handmadeZ2.txt AC 435 ms
20824 KB
99_handmadeZ3.txt AC 444 ms
20820 KB
99_handmadeZ4.txt AC 428 ms
20824 KB
テストケース一括ダウンロード

ソースコード

diff #
#include <iostream>
#include <vector>
#include <array>
#include <functional>
#define repeat(i,n) for (int i = 0; (i) < int(n); ++(i))
using ll = long long;
using namespace std;

const int mod = 1e9+7;

template <typename T, size_t H, size_t W>
using matrix = array<array<T,W>,H>;
template <typename T, size_t A, size_t B, size_t C>
matrix<T,A,C> operator * (matrix<T,A,B> const & a, matrix<T,B,C> const & b) {
    matrix<T,A,C> c = {};
    repeat (y,A) {
        repeat (z,B) {
            repeat (x,C) {
                c[y][x] = (c[y][x] + a[y][z] *(ll) b[z][x]) % mod;
            }
        }
    }
    return c;
}
template <typename T, size_t H, size_t W>
array<T,H> operator * (matrix<T,H,W> const & a, array<T,W> const & b) {
    array<T,H> c = {};
    repeat (y,H) {
        repeat (z,W) {
            c[y] = (c[y] + a[y][z] *(ll) b[z]) % mod;
        }
    }
    return c;
}
template <typename T, size_t H, size_t W>
matrix<T,H,W> operator + (matrix<T,H,W> const & a, matrix<T,H,W> const & b) {
    matrix<T,H,W> c;
    repeat (y,H) {
        repeat (x,W) {
            c[y][x] = (a[y][x] + b[y][x]) % mod;
        }
    }
    return c;
}
template <typename T, size_t N>
array<T,N> operator + (array<T,N> const & a, array<T,N> const & b) {
    array<T,N> c;
    repeat (i,N) {
        c[i] = (a[i] + b[i]) % mod;
    }
    return c;
}
template <typename T, size_t H, size_t W>
matrix<T,H,W> matrix_zero() { return {}; }
template <typename T, size_t N>
matrix<T,N,N> matrix_unit() { matrix<T,N,N> a = {}; repeat (i,N) a[i][i] = 1; return a; }

template <typename T>
struct segment_tree { // on monoid
    int n;
    vector<T> a;
    function<T (T,T)> append; // associative
    T unit; // unit
    segment_tree() = default;
    segment_tree(int a_n, T a_unit, function<T (T,T)> a_append) {
        n = 1; while (n < a_n) n *= 2;
        a.resize(2*n-1, a_unit);
        unit = a_unit;
        append = a_append;
    }
    void point_update(int i, T z) { // 0-based
        a[i+n-1] = z;
        for (i = (i+n)/2; i > 0; i /= 2) {
            a[i-1] = append(a[2*i-1], a[2*i]);
        }
    }
    T point_get(int i) {
        return range_concat(i, i+1);
    }
    T range_concat(int l, int r) { // 0-based, [l, r)
        T lacc = unit, racc = unit;
        for (l += n, r += n; l < r; l /= 2, r /= 2) {
            if (l % 2 == 1) lacc = append(lacc, a[(l ++) - 1]);
            if (r % 2 == 1) racc = append(a[(-- r) - 1], racc);
        }
        return append(lacc, racc);
    }
};

struct range_t {
    matrix<int,3,3> A;
    matrix<int,2,2> B;
    matrix<int,2,3> S;
};

int main() {
    int n; cin >> n;
    array<int,3> a0; repeat (i,3) cin >> a0[i];
    array<int,2> bn; repeat (i,2) cin >> bn[i];
    segment_tree<range_t> segtree(n+1, (range_t) { matrix_unit<int,3>(), matrix_unit<int,2>(), matrix_zero<int,2,3>() }, [&](range_t const & l, range_t const & r) {
        range_t m;
        m.A = r.A * l.A;
        m.B = l.B * r.B;
        m.S = l.S + l.B * r.S * l.A;
        return m;
    });
    repeat (i,n+1) {
        range_t m = segtree.point_get(i);
        repeat (y,2) repeat (x,3) m.S[y][x] = 6*i + 3*y+x;
        segtree.point_update(i, m);
    }
    int query; cin >> query;
    while (query --) {
        string type; int i; cin >> type >> i;
        if (type == "a") {
            range_t m = segtree.point_get(i);
            repeat (y,3) repeat (x,3) cin >> m.A[y][x];
            segtree.point_update(i, m);
        } else if (type == "b") {
            range_t m = segtree.point_get(i);
            repeat (y,2) repeat (x,2) cin >> m.B[y][x];
            segtree.point_update(i, m);
        } else if (type == "ga") {
            range_t m = segtree.range_concat(0, i);
            array<int,3> ai = m.A * a0;
            cout << ai[0] << ' ' << ai[1] << ' ' << ai[2] << endl;
        } else if (type == "gb") {
            range_t l = segtree.range_concat(0, i+1);
            range_t r = segtree.range_concat(i+1, n+1);
            array<int,2> bi = r.B * bn + r.S * l.A * a0;
            cout << bi[0] << ' ' << bi[1] << endl;
        }
    }
    return 0;
}
0