結果

問題 No.255 Splarrraaay スプラーレェーーイ
ユーザー rpy3cpprpy3cpp
提出日時 2017-10-04 01:09:18
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,119 ms / 10,000 ms
コード長 6,698 bytes
コンパイル時間 2,597 ms
コンパイル使用メモリ 192,664 KB
実行使用メモリ 130,508 KB
最終ジャッジ日時 2024-11-16 06:22:15
合計ジャッジ時間 14,006 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,119 ms
129,940 KB
testcase_01 AC 1,113 ms
130,484 KB
testcase_02 AC 1,098 ms
129,708 KB
testcase_03 AC 925 ms
129,904 KB
testcase_04 AC 2 ms
6,816 KB
testcase_05 AC 1,100 ms
129,588 KB
testcase_06 AC 1,111 ms
129,604 KB
testcase_07 AC 1,105 ms
129,476 KB
testcase_08 AC 1,114 ms
130,508 KB
testcase_09 AC 1,083 ms
129,632 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//
// 2017/Oct/03.
// Yukicoder No.255 Splarrraaay

// ******************************************************************
// Segment Tree with Lazy Propagation
// Add and Assignment modification on range,
// Queries for weighted sum on range,
// with coordinate compression
// ******************************************************************

#include <bits/stdc++.h>
using namespace std;

constexpr long long mod = 1'000'000'000'000'000'009ull;

template<typename T>
struct LazyData{
    T add;
    T assign;
    bool has_assign;
    LazyData(T v = 0, T w = 0, bool tf = false): add(v), assign(w), has_assign(tf) {}
};

template<typename T>
class SegTree{
    size_t n;
    int h;
    vector<T> data;           // data[i]: 区間i の重みづけ合計
    vector<T> weight;         // weight[i]: 区間i の重み
    vector<LazyData<T>> lazy; // lazy[i]: 区間i の未伝搬の遅延評価データ
    static T combine(T L, T R){
        return L + R;
    }
    void apply(size_t i, T v, int command_type){
        if (command_type == 0){
            apply_assign(i, v);
        }else{
            apply_add(i, v);
        }
    }
    void apply_assign(size_t i, T v) {  // v: value to assign
        data[i] = weight[i] * v;
        if (i < n) {
            lazy[i].add = 0;
            lazy[i].assign = v;
            lazy[i].has_assign = true;
        }
    }
    void apply_add(size_t i, T v) {
        data[i] += weight[i] * v;
        if (i < n) (lazy[i].has_assign ? lazy[i].assign : lazy[i].add) += v;
    }
    void build(size_t i){      // update all the parents of node i.
        while (i >>= 1) if (not lazy[i].has_assign) data[i] = combine(data[i*2], data[i*2+1]) + lazy[i].add * weight[i];
    }
    void push(size_t p){       // propagates the changes from the root to node p.
        for (int s = h; s > 0; --s){
            size_t i = p >> s;
            if (lazy[i].has_assign){
                apply_assign(i*2,   lazy[i].assign);
                apply_assign(i*2+1, lazy[i].assign);
                lazy[i].has_assign = false;
            }else if (lazy[i].add != 0){
                apply_add(i*2,   lazy[i].add);
                apply_add(i*2+1, lazy[i].add);
                lazy[i].add = 0;
            }
        }
    }
    int calc_h(size_t nn){
        int hh = 1;
        for (; nn > 1; ++hh, nn >>= 1);
        return hh;
    }
    void process_command(size_t L, size_t R, T v, int command_type){
        L += n;
        R += n;
        size_t L0 = L;
        size_t R0 = R;
        push(L);
        push(R - 1);
        for (; L < R; L >>= 1, R >>= 1){
            if (L & 1) apply(L++, v, command_type);
            if (R & 1) apply(--R, v, command_type);
        }
        build(L0);
        build(R0 - 1);
    }
    void fill_weight(const vector<T> & ws){
        copy(begin(ws), end(ws), begin(weight) + n);
        for (int i = n - 1; i > 0; --i) weight[i] = combine(weight[i*2], weight[i*2+1]);
    }
public:
    SegTree(): n(0), h(1), data(), weight(), lazy() {}
    explicit SegTree(const vector<T> &_weight): n(_weight.size()), h(calc_h(n)), data(2 * n, 0), weight(2 * n, 0), lazy(n) {
        fill_weight(_weight);
    }
    SegTree(const vector<T> &src, const vector<T> &_weight): n(src.size()), h(calc_h(n)), data(2 * n, 0), weight(2 * n, 0), lazy(n){
        fill_weight(_weight);
        for (int i = 0; i < n; ++i) data[n + i] = src[i] * weight[n + i];
        for (int i = n - 1; i > 0; --i) data[i] = combine(data[i*2], data[i*2+1]);
    }
    void init(const vector<T> &_weight){
        n = _weight.size();
        h = calc_h(n);
        data.assign(2 * n, 0);
        weight.assign(2 * n, 0);
        lazy.resize(n);
        fill_weight(_weight);
    }
    void modify(size_t L, size_t R, T w) {process_command(L, R, w, 0);} // assign w to range [L, R)
    void add(size_t L, size_t R, T v)    {process_command(L, R, v, 1);} // add v to range [L, R)
    T query(size_t L, size_t R){
        L += n;
        R += n;
        push(L);
        push(R - 1);
        T ret = 0;
        for (; L < R; L >>= 1, R >>= 1){
            if (L & 1) ret = combine(ret, data[L++]);
            if (R & 1) ret = combine(data[--R], ret);
        }
        return ret;
    }
};


template<typename T>
void compress(const vector<T> &src, unordered_map<T, int> &zip, vector<T> &unzip){
    unzip = src;
    sort(unzip.begin(), unzip.end());
    unzip.erase(unique(unzip.begin(), unzip.end()), unzip.end());
    int i = 0;
    for (auto u : unzip) zip[u] = i++;
};


vector<unsigned long long> calc_spans(const vector<unsigned long long> & src){
    vector<unsigned long long> spans;
    for (int i = 1; i < src.size(); ++i) spans.push_back(src[i] - src[i - 1]);
    spans.push_back(0);
    return spans;
}

struct Query{
    int x;
    unsigned long long L;
    unsigned long long R;
    Query(int _x=0, unsigned long long _L=0, unsigned long long _R=0):x(_x), L(_L), R(_R){}
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    unsigned long long N;
    int Q;
    cin >> N;
    cin >> Q;
    vector<Query> Qs(Q);
    for (auto & q : Qs) cin >> q.x >> q.L >> q.R;
    vector<unsigned long long> LRs(2*Q);
    for (int i = 0; i < Q; ++i){
        LRs[2*i] = Qs[i].L;
        LRs[2*i + 1] = Qs[i].R + 1;
    }
    LRs.push_back(N);
    LRs.push_back(0);
    vector<unsigned long long> unzip;
    unordered_map<unsigned long long, int> zip;
    compress(LRs, zip, unzip);
    auto spans= calc_spans(unzip);

    vector<SegTree<unsigned long long>> segs(5, SegTree<unsigned long long>(spans));
    vector<unsigned long long> scores(5, 0);
    for (const auto & q : Qs){
        int cL = zip[q.L];
        int cR = zip[q.R + 1];
        if (q.x == 0){
            unsigned long long bonus = 0;
            int winner = -1;
            for (int i = 0; i < 5; ++i){
                unsigned long long score = segs[i].query(cL, cR);
                if (score > bonus){
                    bonus = score;
                    winner = i;
                }else if (score == bonus){
                    winner = -1;
                }
            }
            if (winner == -1) continue;
            scores[winner] += (bonus % mod);
            scores[winner] %= mod;
        }else{
            for (int i = 0; i < 5; ++i){
                if (i + 1 == q.x){
                    segs[i].add(cL, cR, 1);
                }else{
                    segs[i].modify(cL, cR, 0);
                }
            }
        }
    }
    for (int i = 0; i < 5; ++i){
        scores[i] += (segs[i].query(0, zip[N]) % mod);
        scores[i] %= mod;
    }
    cout << scores[0];
    for (int i = 1; i < 5; ++i) cout << ' ' << scores[i];
    cout << endl;
    return 0;
}
0