結果

問題 No.833 かっこいい電車
ユーザー toyamatoyama
提出日時 2019-07-29 22:25:27
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,986 bytes
コンパイル時間 721 ms
コンパイル使用メモリ 79,756 KB
実行使用メモリ 7,576 KB
最終ジャッジ日時 2023-09-15 12:03:33
合計ジャッジ時間 7,514 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 1 ms
4,380 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 1 ms
4,376 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 12 ms
4,380 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 70 ms
7,320 KB
testcase_31 AC 168 ms
5,052 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 + 1) >= x - mid + 1) {
                    valid = mid;
                }
                else {
                    invalid = mid;
                }
            }
            l = valid;

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