結果

問題 No.833 かっこいい電車
ユーザー けーむけーむ
提出日時 2020-05-13 14:13:35
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 180 ms / 2,000 ms
コード長 5,318 bytes
コンパイル時間 2,269 ms
コンパイル使用メモリ 193,104 KB
実行使用メモリ 15,620 KB
最終ジャッジ日時 2023-10-12 16:59:43
合計ジャッジ時間 7,652 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 139 ms
9,412 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 AC 1 ms
4,356 KB
testcase_03 AC 1 ms
4,356 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 AC 1 ms
4,356 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 118 ms
9,392 KB
testcase_11 AC 144 ms
15,328 KB
testcase_12 AC 45 ms
8,932 KB
testcase_13 AC 34 ms
4,588 KB
testcase_14 AC 108 ms
15,328 KB
testcase_15 AC 53 ms
9,420 KB
testcase_16 AC 27 ms
9,672 KB
testcase_17 AC 49 ms
4,352 KB
testcase_18 AC 144 ms
9,600 KB
testcase_19 AC 30 ms
9,600 KB
testcase_20 AC 7 ms
6,168 KB
testcase_21 AC 128 ms
6,228 KB
testcase_22 AC 54 ms
15,620 KB
testcase_23 AC 37 ms
9,600 KB
testcase_24 AC 56 ms
15,324 KB
testcase_25 AC 141 ms
9,252 KB
testcase_26 AC 39 ms
14,832 KB
testcase_27 AC 89 ms
9,348 KB
testcase_28 AC 72 ms
6,228 KB
testcase_29 AC 81 ms
9,068 KB
testcase_30 AC 180 ms
15,540 KB
testcase_31 AC 132 ms
9,428 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for(ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for(ll i = (n) - 1; i >= 0; i--)
#define rreps(i, e, n) for(ll i = (n) - 1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())
#define endl "\n"

template<class T, class E>
struct LazySegmentTree {
private:
    int n;
    vector<T> node;
    vector<E> lazy;
    vector<bool> updated;
    T def;
    function<T(T,T)> out;
    function<T(T,E,int)> app;
    function<E(E,E)> lop;
    
    void propagate(int k, E x) {
        if (updated[k]) {
            lazy[k] = lop(lazy[k], x);
        }
        else {
            lazy[k] = x;
            updated[k] = true;
        }
    }
    
    void evaluate(int k, int l, int r) {
        if (updated[k]) {
            node[k] = app(node[k], lazy[k], r - l);
            if ((r - l) > 1) {
                propagate(k * 2 + 1, lazy[k]);
                propagate(k * 2 + 2, lazy[k]);
            }
            updated[k] = false;
        }
    }
    
public:
    LazySegmentTree() {}
    LazySegmentTree(const vector<T> &v, T _def, function<E(E,E)> _lop, function<T(T,E,int)> _app, function<T(T,T)> _out) {
        def = _def; out = _out; app = _app; lop = _lop;
        int vl = (int)v.size();
        n = 1; while(n < vl) n *= 2;
        node.resize(2 * n - 1, def);
        lazy.resize(2 * n - 1);
        updated.resize(2 * n - 1, false);
        for(int i = 0; i < vl; i++) node[i + n - 1] = v[i];
        for(int i = n - 2; i >= 0; i--) node[i] = out(node[i * 2 + 1], node[i * 2 + 2]);
    }
    
    void update(int a, int b, E x, int k = 0, int l = 0, int r = -1) {
        if (r < 0) r = n;
        evaluate(k, l, r);
        if((b <= l) || (r <= a)) return;
        if((a <= l) && (r <= b)) {
            propagate(k, x);
            evaluate(k, l, r);
        }
        else {
            update(a, b, x, 2 * k + 1, l, (l + r) / 2);
            update(a, b, x, 2 * k + 2, (l + r) / 2, r);
            node[k] = out(node[2 * k + 1], node[2 * k + 2]);
        }
    }
    
    T query(int a, int b, int k = 0, int l = 0, int r = -1) {
        if (r < 0) r = n;
        evaluate(k, l, r);
        if ((b <= l) || (r <= a)) return def;
        if ((a <= l) && (r <= b)) return node[k];
        T vl = query(a, b, 2 * k + 1, l, (l + r) / 2);
        T vr = query(a, b, 2 * k + 2, (l + r) / 2, r);
        return out(vl, vr);
    }
};

template<class T>
struct SegmentTree {
private:
    int n;
    vector<T> node;
    T def;
    function<T(T,T)> lop;
    function<T(T,T)> out;
    
public:
    SegmentTree() {}
    SegmentTree(const vector<T> &v, T _def, function<T(T,T)> _lop, function<T(T,T)> _out) {
        int vl = (int)v.size();
        n = 1;
        while(n < vl) n *= 2;
        def = _def; lop = _lop; out = _out;
        node = vector<T>(2 * n - 1, def);
        for(int i = 0; i < vl; i++) node[i + n - 1] = v[i];
        for(int i = n - 2; i >= 0; i--) node[i] = out(node[i * 2 + 1], node[i * 2 + 2]);
    }
    
    void update(int idx, T val) {
        idx += (n - 1);
        node[idx] = lop(node[idx], val);
        while(idx > 0) {
            idx = (idx - 1) / 2;
            node[idx] = out(node[idx * 2 + 1], node[idx * 2 + 2]);
        }
    }
    
    T query(int a, int b, int k = 0, int l = 0, int r = -1) {
        if (r < 0) r = n;
        if ((r <= a) || (b <= l)) return def;
        if ((a <= l) && (r <= b)) return node[k];
        return out(query(a, b, 2 * k + 1, l, (l + r) / 2), query(a, b, 2 * k + 2, (l + r) / 2, r));
    }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    // ifstream in("input.txt");
    // cin.rdbuf(in.rdbuf());
    ll n, q;
    cin >> n >> q;
    vector<ll> a(n);
    rep(i, n) cin >> a[i];
    vector<pair<ll, ll>> vp(n);
    rep(i, n) vp[i] = make_pair(i, i);
    auto lop1 = [](pair<ll, ll> a, pair<ll, ll> b){ return b; };
    auto app1 = [](pair<ll, ll> a, pair<ll, ll> b, int c){ return b; };
    auto out1 = [](pair<ll, ll> a, pair<ll, ll> b){ return make_pair(a.first + b.first, a.second + b.second); };
    LazySegmentTree<pair<ll, ll>, pair<ll, ll>> range(vp, make_pair(0, 0), lop1, app1, out1);
    auto lop2 = [](ll a, ll b){ return a + b; };
    auto out2 = [](ll a, ll b){ return a + b; };
    SegmentTree<ll> val(a, 0, lop2, out2);
    rep(i, q) {
        ll t, x;
        cin >> t >> x;
        x--;
        if (t == 1) {
            pair<ll, ll> p1 = range.query(x, x + 1);
            pair<ll, ll> p2 = range.query(x + 1, x + 2);
            range.update(p1.first, p2.second + 1, make_pair(p1.first, p2.second));
        }
        else if (t == 2) {
            pair<ll, ll> p1 = range.query(x, x + 1);
            pair<ll, ll> p2 = range.query(x + 1, x + 2);
            range.update(p1.first, x + 1, make_pair(p1.first, x));
            range.update(x + 1, p2.second + 1, make_pair(x + 1, p2.second));
        }
        else if (t == 3) {
            val.update(x, 1);
        }
        else {
            pair<ll, ll> p = range.query(x, x + 1);
            cout << val.query(p.first, p.second + 1) << endl;
        }
    }
    return 0;
}
0