結果

問題 No.833 かっこいい電車
ユーザー finefine
提出日時 2019-05-24 21:50:50
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 139 ms / 2,000 ms
コード長 4,998 bytes
コンパイル時間 1,855 ms
コンパイル使用メモリ 181,500 KB
実行使用メモリ 9,828 KB
最終ジャッジ日時 2023-09-14 20:50:23
合計ジャッジ時間 5,068 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 65 ms
6,676 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,384 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 93 ms
6,776 KB
testcase_11 AC 131 ms
8,768 KB
testcase_12 AC 39 ms
5,628 KB
testcase_13 AC 23 ms
4,376 KB
testcase_14 AC 110 ms
8,856 KB
testcase_15 AC 53 ms
6,128 KB
testcase_16 AC 48 ms
7,064 KB
testcase_17 AC 29 ms
4,376 KB
testcase_18 AC 109 ms
6,816 KB
testcase_19 AC 46 ms
6,776 KB
testcase_20 AC 12 ms
4,768 KB
testcase_21 AC 82 ms
4,956 KB
testcase_22 AC 82 ms
9,388 KB
testcase_23 AC 50 ms
6,928 KB
testcase_24 AC 79 ms
9,124 KB
testcase_25 AC 103 ms
6,484 KB
testcase_26 AC 55 ms
8,388 KB
testcase_27 AC 77 ms
6,396 KB
testcase_28 AC 51 ms
4,728 KB
testcase_29 AC 64 ms
5,952 KB
testcase_30 AC 139 ms
9,828 KB
testcase_31 AC 63 ms
6,532 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

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

// Description: 区間をsetで管理するデータ構造(なお実装はmap).各クエリO(log区間数).

// #### attention! : [l, r] ( include r, not [l, r) )
class SegmentMap : public std::map<signed, signed> {
private:
	bool flagToMergeAdjacentSegment;
public:
	// if merge [l, c] and [c+1, r], set flagToMergeAdjacentSegment to true
	SegmentMap(bool flagToMergeAdjacentSegment) :
		flagToMergeAdjacentSegment(flagToMergeAdjacentSegment) {}
	// __exist -> iterator pair(l, r) (contain p)
	// noexist -> map.end()
	auto get(signed p) const {
		auto it = upper_bound(p);
		if (it == begin() || (--it)->second < p) return end();
		return it;
	}
	// insert segment [l, r]
	void insert(signed l, signed r) {
		auto itl = upper_bound(l), itr = upper_bound(r + flagToMergeAdjacentSegment);
		if (itl != begin()) {
			if ((--itl)->second < l - flagToMergeAdjacentSegment) ++itl;
		}
		if (itl != itr) {
			l = std::min(l, itl->first);
			r = std::max(r, std::prev(itr)->second);
			erase(itl, itr);
		}
		(*this)[l] = r;
	}
	// remove segment [l, r]
	void remove(signed l, signed r) {
		auto itl = upper_bound(l), itr = upper_bound(r);
		if (itl != begin()) {
			if ((--itl)->second < l) ++itl;
		}
		if (itl == itr) return;
		int tl = std::min(l, itl->first), tr = std::max(r, std::prev(itr)->second);
		erase(itl, itr);
		if (tl < l) (*this)[tl] = l - 1;
		if (r < tr) (*this)[r + 1] = tr;
	}
	// Is p and q in same segment?
	bool same(signed p, signed q) const {
		const auto&& it = get(p);
		return it != end() && it->first <= q && q <= it->second;
	}
};

template <typename T>
struct SegmentTree {
    int n;
    vector<T> data;
    T INITIAL_VALUE;

    //使うときは、この2つを適宜変更する
    static T merge(T x, T y);
    void updateNode(int k, T x);

    SegmentTree(int size, T initial_value) {
        n = 1;
        INITIAL_VALUE = initial_value;
        while (n < size) n *= 2;
        data.resize(2 * n - 1, INITIAL_VALUE);
    }

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

    void update(int k, T x) {
        k += n - 1; //葉の節点
        updateNode(k, x);
        while (k > 0) {
            k = (k - 1) / 2;
            data[k] = merge(data[k * 2 + 1], data[k * 2 + 2]);
        }
    }

    //区間[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 INITIAL_VALUE;
        //[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 merge(vl, vr);
        }
    }

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

    //非再帰版: バグってるかもしれないので定数倍高速化する時以外使わないで
    //区間[a, b)に対するクエリに答える
    T query_fast(int a, int b) {
        T vl = INITIAL_VALUE, vr = INITIAL_VALUE;
        for (int l = a + n, r = b + n; l != r; l >>= 1, r >>= 1) {
            if (l & 1) vl = merge(vl, data[l++ - 1]);
            if (r & 1) vr = merge(data[--r - 1], vr);
        }
        return merge(vl, vr);
    }
};

//使うときは以下2つを変更
//非可換の場合は順序に注意!!!
template <typename T>
T SegmentTree<T>::merge(T x, T y) {
    return x + y;
}

template <typename T>
void SegmentTree<T>::updateNode(int k, T x) {
    data[k] += x;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, q;
    cin >> n >> q;
    SegmentMap sm(false);
    SegmentTree<ll> st(n, 0);
    for (int i = 0; i < n; i++) {
        ll a;
        cin >> a;
        sm.insert(i, i);
        st.update(i, a);
    }

    for (int i = 0; i < q; i++) {
        int que, x;
        cin >> que >> x;
        x--;
        if (que == 1) {
            if (!sm.same(x, x + 1)) {
                auto it1 = sm.get(x);
                auto it2 = sm.get(x + 1);
                P p(it1->first, it2->second);
                sm.remove(it1->first, it1->second);
                sm.remove(it2->first, it2->second);
                sm.insert(p.first, p.second);
            }
        } else if (que == 2) {
            if (sm.same(x, x + 1)) {
                auto it = sm.get(x);
                P p1(it->first, x), p2(x + 1, it->second);
                sm.remove(it->first, it->second);
                sm.insert(p1.first, p1.second);
                sm.insert(p2.first, p2.second);
            }
        } else if (que == 3) {
            st.update(x, 1);
        } else {
            auto it = sm.get(x);
            cout << st.query(it->first, it->second + 1) << "\n";
        }
    }
    return 0;
}
0