結果

問題 No.1226 I hate Robot Arms
ユーザー finefine
提出日時 2020-09-11 23:53:30
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 284 ms / 2,000 ms
コード長 3,952 bytes
コンパイル時間 3,140 ms
コンパイル使用メモリ 175,876 KB
実行使用メモリ 11,812 KB
最終ジャッジ日時 2023-08-30 10:45:29
合計ジャッジ時間 16,547 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 77 ms
4,516 KB
testcase_03 AC 87 ms
5,244 KB
testcase_04 AC 95 ms
10,956 KB
testcase_05 AC 85 ms
11,436 KB
testcase_06 AC 220 ms
11,428 KB
testcase_07 AC 69 ms
4,548 KB
testcase_08 AC 26 ms
11,220 KB
testcase_09 AC 160 ms
5,280 KB
testcase_10 AC 20 ms
4,380 KB
testcase_11 AC 106 ms
10,844 KB
testcase_12 AC 59 ms
7,008 KB
testcase_13 AC 44 ms
11,608 KB
testcase_14 AC 194 ms
5,380 KB
testcase_15 AC 17 ms
11,076 KB
testcase_16 AC 224 ms
11,400 KB
testcase_17 AC 62 ms
5,424 KB
testcase_18 AC 42 ms
5,708 KB
testcase_19 AC 111 ms
11,192 KB
testcase_20 AC 102 ms
10,872 KB
testcase_21 AC 138 ms
11,024 KB
testcase_22 AC 226 ms
11,772 KB
testcase_23 AC 225 ms
11,748 KB
testcase_24 AC 223 ms
11,720 KB
testcase_25 AC 224 ms
11,744 KB
testcase_26 AC 224 ms
11,772 KB
testcase_27 AC 284 ms
11,812 KB
testcase_28 AC 276 ms
11,752 KB
testcase_29 AC 277 ms
11,776 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

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

constexpr char newl = '\n';
constexpr double PI = 3.14159265358979;

// [Related]: monoid
template <class Monoid>
struct SegmentTree {
    using T = typename Monoid::T;

    int n;
    vector<T> data;
    vector<double> ang;

    SegmentTree() {}

    SegmentTree(int size, T initial_value = Monoid::unit()) {
        n = 1;
        while (n < size) n <<= 1;
        data.assign(2 * n - 1, initial_value);
        ang.assign(2 * n - 1, 0);

        if (initial_value != Monoid::unit()) {
            for (int i = n - 2; i >= 0; i--) data[i] = Monoid::merge(data[i * 2 + 1], data[i * 2 + 2], ang[i * 2 + 2]);
        }
    }

    SegmentTree(const vector<T>& v) {
        int size = v.size();
        n = 1;
        while (n < size) n <<= 1;
        data.assign(2 * n - 1, Monoid::unit());
        ang.assign(2 * n - 1, 0);

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

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

    void change(int s, T x, int v, int l, int r) {
        if (s <= l) return;
        else if (s < r) {
            int chl = (v << 1) + 1, chr = (v << 1) + 2;
            int m = (l + r) >> 1;
            change(s, x, chl, l, m);
            change(s, x, chr, m, r);
            if (s <= m) ang[v] += x.second;
            data[v] = Monoid::merge(data[chl], data[chr], ang[v]);
        }
    }

    void update(int k, T x) {
        if (x.first == 0) {
            change(k, x, 0, 0, n);
            return;
        }
        k += n - 1;
        Monoid::update(data[k], x);
        while (k > 0) {
            k = (k - 1) / 2;
            data[k] = Monoid::merge(data[2 * k + 1], data[2 * k + 2], ang[k]);
        }
    }

    //区間[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 Monoid::unit();
        //[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 Monoid::merge(vl, vr, ang[k]);
        }
    }

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

template <class U = P>
struct Arm {
    using T = U;
    static T merge(const T& x, const T& y, double ang) {
        double s = sin(ang), c = cos(ang);
        return T(
            x.first + (c * y.first - s * y.second),
            x.second + (s * y.first + c * y.second)
        );
    }
    static void update(T& target, const T& x) { target.first += x.first; }
    static constexpr T unit() { return T(0, 0); }
};

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

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

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

    return 0;
}
0