結果

問題 No.151 セグメントフィッシング
ユーザー izuru_matsuuraizuru_matsuura
提出日時 2016-09-09 12:05:18
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 132 ms / 5,000 ms
コード長 4,966 bytes
コンパイル時間 1,754 ms
コンパイル使用メモリ 174,212 KB
実行使用メモリ 6,224 KB
最終ジャッジ日時 2023-08-10 05:09:10
合計ジャッジ時間 4,380 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 2 ms
4,384 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 21 ms
4,384 KB
testcase_09 AC 20 ms
4,380 KB
testcase_10 AC 21 ms
4,380 KB
testcase_11 AC 21 ms
4,380 KB
testcase_12 AC 98 ms
5,880 KB
testcase_13 AC 99 ms
5,872 KB
testcase_14 AC 97 ms
5,832 KB
testcase_15 AC 98 ms
6,108 KB
testcase_16 AC 97 ms
5,988 KB
testcase_17 AC 49 ms
6,148 KB
testcase_18 AC 53 ms
6,224 KB
testcase_19 AC 129 ms
5,264 KB
testcase_20 AC 132 ms
5,360 KB
testcase_21 AC 77 ms
6,136 KB
testcase_22 AC 76 ms
6,188 KB
testcase_23 AC 50 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

namespace {

    typedef double real;
    typedef long long ll;

    template<class T> ostream& operator<<(ostream& os, const vector<T>& vs) {
        if (vs.empty()) return os << "[]";
        os << "[" << vs[0];
        for (int i = 1; i < vs.size(); i++) os << " " << vs[i];
        return os << "]";
    }
    template<class T> istream& operator>>(istream& is, vector<T>& vs) {
        for (auto it = vs.begin(); it != vs.end(); it++) is >> *it;
        return is;
    }

    template<class V> struct Treap {
        mt19937 mt;
        Treap() : root(NULL) {
            random_device rnd;
            mt.seed(rnd());
        }

        struct Node {
            V value;
            Node *left, *right;
            int priority;
            int size;
            V sum; /**/
            Node(V value, int priority) : value(value), priority(priority), size(1), sum(value)/**/ {
                left = NULL;
                right = NULL;
            }
        };
        
        Node *root;
        int size(Node *t) {
            if (t == NULL) return 0;
            return t->size;
        }

        V sum(Node *t) { /**/
            if (t == NULL) return 0;
            V l = (t->left == NULL ? 0 : t->left->sum);
            V r = (t->right == NULL ? 0 : t->right->sum);
            return l + r + t->value;
        }

        Node* update(Node *v) {
            v->size = size(v->left) + size(v->right) + 1;
            v->sum = sum(v->left) + sum(v->right) + v->value; /**/
            return v;
        }

        Node* merge(Node *l, Node *r) {
            if (l == NULL) return r;
            if (r == NULL) return l;
            assert(l != NULL && r != NULL);

            if (l->priority > r->priority) {
                l->right = merge(l->right, r);
                return update(l);
            } else {
                r->left = merge(l, r->left);
                return update(r);
            }
        }

        pair<Node*, Node*> split(Node *t, int k) { // [0, k) と [k, n)に分割
            if (t == NULL) return make_pair((Node*)NULL, (Node*)NULL);
            if (k <= size(t->left)) {
                auto s = split(t->left, k);
                t->left = s.second;
                return make_pair(s.first, update(t));
            } else {
                auto s = split(t->right, k - size(t->left) - 1); // この-1は今見てる部分木のroot(つまりt)の分
                t->right = s.first;
                return make_pair(update(t), s.second);
            }
        }
        pair<Node*, Node*> split(int k) { return split(root, k); }

        Node* insert(Node *t, int k, V value) {
            auto x = split(t, k);
            auto y = split(x.second, 1);
            return merge(x.first, merge(new Node(value, mt()), y.second));
        }
        Node* insert(int k, V value) { return root = insert(root, k, value); }

        Node* erase(Node *t, int k) {
            auto x = split(t, k);
            auto y = split(x.second, 1);
            return merge(x.first, y.second);
        }
        Node* erase(int k) { return root = erase(root, k); }

        Node* find(Node *t, int k) {
            auto x = split(t, k);
            auto y = split(x.second, 1);
            Node *ret = y.first;
            merge(x.first, merge(y.first, y.second));
            return ret;
        }
        Node* find(int k) { return find(root, k); }

    };

    void solve() {
        int N, Q; cin >> N >> Q;
        Treap<ll> l, r;
        for (int i = 0; i < N; i++) {
            l.insert(i, 0);
            r.insert(i, 0);
        }

        for (int q = 0; q < Q; q++) {
            string x; int y, z; 
            cin >> x >> y >> z;
            //cout << x << " " << y << " " << z << endl;

            auto ls = l.split(1);
            auto rs = r.split(N - 1);
            l.root = l.merge(ls.second, rs.second);
            r.root = r.merge(ls.first, rs.first);
            //cout << "L:"; for (int i = 0; i < N; i++) cout << " " << l.find(i)->value; cout << endl;
            //cout << "R:"; for (int i = 0; i < N; i++) cout << " " << r.find(i)->value; cout << endl;

            if (x == "L") {
                auto v = l.find(y);
                if (v == NULL) l.insert(y, z);
                else l.insert(y, v->value + z);
            } else if (x == "R") {
                auto v = r.find(y);
                if (v == NULL) r.insert(y, z);
                else r.insert(y, v->value + z);
            } else {
                auto ls = l.split(y);
                auto lt = l.split(ls.second, z - y);
                auto rs = r.split(y);
                auto rt = r.split(rs.second, z - y);
                cout << l.sum(lt.first) + r.sum(rt.first) << endl;
                l.merge(ls.first, l.merge(lt.first, lt.second));
                r.merge(rs.first, l.merge(rt.first, rt.second));
            }
        }
    }
}

int main() {
    solve();
    return 0;
}
0