結果

問題 No.833 かっこいい電車
ユーザー toyamatoyama
提出日時 2019-07-29 22:31:58
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 321 ms / 2,000 ms
コード長 2,978 bytes
コンパイル時間 713 ms
コンパイル使用メモリ 79,664 KB
実行使用メモリ 7,376 KB
最終ジャッジ日時 2023-09-15 12:03:43
合計ジャッジ時間 5,083 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 321 ms
5,132 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 142 ms
5,444 KB
testcase_11 AC 184 ms
7,340 KB
testcase_12 AC 55 ms
5,140 KB
testcase_13 AC 41 ms
4,380 KB
testcase_14 AC 142 ms
7,376 KB
testcase_15 AC 69 ms
5,088 KB
testcase_16 AC 46 ms
5,084 KB
testcase_17 AC 56 ms
4,380 KB
testcase_18 AC 173 ms
5,084 KB
testcase_19 AC 47 ms
5,136 KB
testcase_20 AC 12 ms
4,376 KB
testcase_21 AC 152 ms
4,380 KB
testcase_22 AC 85 ms
6,988 KB
testcase_23 AC 56 ms
5,264 KB
testcase_24 AC 85 ms
6,936 KB
testcase_25 AC 168 ms
5,212 KB
testcase_26 AC 58 ms
6,984 KB
testcase_27 AC 113 ms
5,140 KB
testcase_28 AC 88 ms
4,376 KB
testcase_29 AC 99 ms
5,140 KB
testcase_30 AC 71 ms
6,964 KB
testcase_31 AC 166 ms
5,136 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <climits>
using namespace std;

template<typename Monoid>
struct SegmentTree {
    using OP = function<Monoid(Monoid, Monoid)>;
    
    vector<Monoid> tree;
    int size;
    const OP f; // bin' operation
    const Monoid e; // neutral element
                                           
    SegmentTree(int nmemb, const Monoid &e, const OP &f):
        f(f),
        e(e)
    {
        size = 1;
        while (size < nmemb) {
            size *= 2;
        }

        tree.assign(2 * size - 1, e);
    }

    void update(int k, Monoid dat)
    {
        k += size - 1;
        tree[k] = dat;
        
        while(k > 0) {
            k = (k - 1) / 2;
            tree[k] = f(tree[2 * k + 1], tree[2 * k + 2]);
        }
    }

    // [l, r)
    Monoid query(int l, int r)
    {
        l += size - 1;
        r += size - 1;

        Monoid d = e;
        while (l < r) {
            if (l % 2 == 0) {
                d = f(d, tree[l]);
                l++;
            }
            if (r % 2 == 0) {
                r--;
                d = f(d, tree[r]);
            }

            l = (l - 1) / 2;
            r = (r - 1) / 2;
        }

        return d;
    }

    Monoid operator[] (const int k)
    {
        return query(k, k + 1);
    }
};

// 車両がつながってる区間を考える
// セグ木で、二分探索 区間の長さと総和は合う?
int main()
{
    using ll = long long;

    ll n, q;
    ll a;
    ll com, x;

    cin >> n >> q;
    SegmentTree<ll> connect(n + 1, 0, [](ll l, ll r){return l + r;});
    SegmentTree<ll> sum_a(n + 1, 0, [](ll l, ll r){return l + r;});

    for (int i = 0; i < n; i++) {
        cin >> a;
        sum_a.update(i, a);
    }

    while(q--) {
        cin >> com >> x;
        --x;

        if (com == 1) {
            connect.update(x, 1);
        }
        else if (com == 2) {
            connect.update(x, 0);
        }
        else if (com == 3) {
            sum_a.update(x, sum_a[x] + 1);
        }
        else if (com == 4) {
            ll valid, invalid, mid;
            ll l, r;

            // right
            valid = x;
            invalid = n + 1;
            while(invalid - valid > 1) {
                mid = (invalid + valid) / 2;

                if (connect.query(x, mid) >= mid - x) {
                    valid = mid;
                }
                else {
                    invalid = mid;
                }
            }
            r = valid + 1;

            // left
            valid = x;
            invalid = -1;
            while (valid - invalid > 1) {
                mid = (invalid + valid) / 2;

                if (connect.query(mid, x) >= x - mid) {
                    valid = mid;
                }
                else {
                    invalid = mid;
                }
            }
            l = valid;

            cout << sum_a.query(l, r) << endl;
        }
    }
}
0