結果

問題 No.1226 I hate Robot Arms
ユーザー finefine
提出日時 2020-09-12 00:31:53
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 491 ms / 2,000 ms
コード長 5,417 bytes
コンパイル時間 2,466 ms
コンパイル使用メモリ 175,428 KB
実行使用メモリ 28,912 KB
最終ジャッジ日時 2023-08-30 11:15:35
合計ジャッジ時間 19,129 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 129 ms
6,208 KB
testcase_03 AC 156 ms
9,268 KB
testcase_04 AC 184 ms
28,284 KB
testcase_05 AC 165 ms
28,580 KB
testcase_06 AC 427 ms
28,912 KB
testcase_07 AC 119 ms
6,288 KB
testcase_08 AC 47 ms
28,244 KB
testcase_09 AC 283 ms
9,264 KB
testcase_10 AC 31 ms
4,704 KB
testcase_11 AC 205 ms
28,248 KB
testcase_12 AC 109 ms
15,792 KB
testcase_13 AC 86 ms
28,500 KB
testcase_14 AC 349 ms
9,260 KB
testcase_15 AC 31 ms
28,188 KB
testcase_16 AC 417 ms
28,576 KB
testcase_17 AC 110 ms
9,648 KB
testcase_18 AC 74 ms
9,572 KB
testcase_19 AC 210 ms
28,196 KB
testcase_20 AC 189 ms
28,296 KB
testcase_21 AC 260 ms
28,248 KB
testcase_22 AC 434 ms
28,512 KB
testcase_23 AC 431 ms
28,516 KB
testcase_24 AC 432 ms
28,452 KB
testcase_25 AC 429 ms
28,448 KB
testcase_26 AC 429 ms
28,564 KB
testcase_27 AC 485 ms
28,508 KB
testcase_28 AC 491 ms
28,576 KB
testcase_29 AC 484 ms
28,504 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;
using P = pair<ld, ld>;

constexpr char newl = '\n';
constexpr ld PI = acos((ld)-1);

// [Related]: monoid_op
// 参考: http://kazuma8128.hatenablog.com/entry/2017/12/29/081929
template <class MonoidOp>
struct LazySegmentTree {
    using Tm = typename MonoidOp::Tm;
    using To = typename MonoidOp::To;

    int n;
    int h;
    vector<Tm> data;
    vector<To> lazy;

    LazySegmentTree() {}

    LazySegmentTree(int size, Tm initial_data_value = MonoidOp::unit()) {
        n = 1;
        h = 0;
        while (n < size) {
            h++;
            n <<= 1;
        }

        data.assign((n << 1), initial_data_value);
        lazy.assign((n << 1), MonoidOp::op_unit());

        if (initial_data_value != MonoidOp::unit()) {
            for (int i = n - 1; i > 0; i--) data[i] = MonoidOp::merge(data[(i << 1)], data[(i << 1) + 1]);
        }
    }

    LazySegmentTree(const vector<Tm>& v) {
        int size = v.size();
        n = 1;
        h = 0;
        while (n < size) {
            h++;
            n <<= 1;
        }

        data.assign((n << 1), MonoidOp::unit());
        lazy.assign((n << 1), MonoidOp::op_unit());

        for (int i = 0; i < size; i++) data[i + n] = v[i];
        for (int i = n - 1; i > 0; i--) data[i] = MonoidOp::merge(data[(i << 1)], data[(i << 1) + 1]);
    }

    // https://komiyam.hatenadiary.org/entry/20131202/1385992406
    inline int getSegLen(int k) {
        return n / (1 << (31 - __builtin_clz(k)));
    }

    Tm getLeaf(int k) {
        return query(k, k + 1);
    }

    void push(int k) {
        if (lazy[k] == MonoidOp::op_unit()) return;
        int seg_len = getSegLen(k);
        data[k] = MonoidOp::apply(data[k], lazy[k], seg_len);
        if (seg_len > 1) {
            lazy[(k << 1)] = MonoidOp::op_merge(lazy[(k << 1)], lazy[k]);
            lazy[(k << 1) + 1] = MonoidOp::op_merge(lazy[(k << 1) + 1], lazy[k]);
        }
        lazy[k] = MonoidOp::op_unit();
    }

    void update(int k) {
        Tm vl = MonoidOp::apply(data[(k << 1)], lazy[(k << 1)], getSegLen(k << 1));
        Tm vr = MonoidOp::apply(data[(k << 1) + 1], lazy[(k << 1) + 1], getSegLen((k << 1) + 1));
        data[k] = MonoidOp::merge(vl, vr);
    }

    //区間[a, b)に対する更新
    void update(int a, int b, const To& x) {
        a += n, b += n - 1;
        for (int i = h; i > 0; i--) {
            push(a >> i);
            push(b >> i);
        }

        for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
            if (l & 1) lazy[l] = MonoidOp::op_merge(lazy[l], x), l++;
            if (r & 1) --r, lazy[r] = MonoidOp::op_merge(lazy[r], x);
        }

        while (a >>= 1, b >>= 1, a) {
            if (lazy[a] == MonoidOp::op_unit()) update(a);
            if (lazy[b] == MonoidOp::op_unit()) update(b);
        }
    }

    //区間[a, b)に対するクエリに答える
    Tm query(int a, int b) {
        a += n, b += n - 1;
        for (int i = h; i > 0; i--) {
            push(a >> i);
            push(b >> i);
        }

        Tm vl = MonoidOp::unit(), vr = MonoidOp::unit();
        for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
            if (l & 1) {
                vl = MonoidOp::merge(vl, MonoidOp::apply(data[l], lazy[l], getSegLen(l)));
                l++;
            }

            if (r & 1) {
                --r;
                vr = MonoidOp::merge(MonoidOp::apply(data[r], lazy[r], getSegLen(r)), vr);
            }
        }
        return MonoidOp::merge(vl, vr);
    }
};

template<class U = P, class V = array<ld, 4> >
struct Arm {
    using Tm = U;
    using To = V;
    static Tm merge(const Tm& x, const Tm& y) { 
        return Tm(x.first + y.first, x.second + y.second);
    }
    static To op_merge(const To& x, const To& y) { 
        To res{};
        for (int i = 0; i < 2; i++) {
            for (int k = 0; k < 2; k++) {
                for (int j = 0; j < 2; j++) {
                    res[i * 2 + j] += y[i * 2 + k] * x[k * 2 + j];
                }
            }
        }
        return res;
    }
    static Tm apply(const Tm& vm, const To& vo, int seg_len) { 
        return Tm(
            vo[0] * vm.first + vo[1] * vm.second,
            vo[2] * vm.first + vo[3] * vm.second
        );
    }
    static constexpr Tm unit() { return {0, 0}; }
    static constexpr To op_unit() { return {1, 0, 0, 1}; }
};

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int n, q;
    cin >> n >> q;

    vector<int> d(n, 1);
    vector<int> th(n, 0);
    LazySegmentTree< Arm<> > st(n, P(1, 0));
    for (int i = 0; i < q; i++) {
        int com;
        cin >> com;
        if (com == 0) {
            int tar, x;
            cin >> tar >> x;
            --tar;
            ld th_diff = (x - th[tar]) * PI / 180;
            st.update(tar, n, {cos(th_diff), -sin(th_diff), sin(th_diff), cos(th_diff)});
            th[tar] = x;
        } else if (com == 1) {
            int tar, x;
            cin >> tar >> x;
            --tar;
            ld len_mul = (ld)x / d[tar];
            st.update(tar, tar + 1, {len_mul, 0, 0, len_mul});
            d[tar] = x;
        } else {
            int tar;
            cin >> tar;
            P ans = st.query(0, tar);
            cout << fixed << setprecision(20) << ans.first << " " << ans.second << newl;
        }
    }

    return 0;
}
0