Submission #1368365

#TimeUsernameProblemLanguageResultExecution timeMemory
1368365chithanhnguyenSimple game (IZhO17_game)C++20
100 / 100
35 ms9184 KiB
/*
Author: Nguyen Chi Thanh - High School for the Gifted - VNU.HCM (i2528)
*/
#include <bits/stdc++.h>
using namespace std;

/* START OF TEMPALTE */

// #define int long long
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define fi first
#define se second
#define popcount __builtin_popcountll
#define all(x) (x).begin(), (x).end()
#define BIT(x, i) (((x) >> (i)) & 1)
#define MASK(x) (1ll << (x))
#define SZ(a) ((int32_t)a.size())

#define debug(a, l, r) {for (int _i = (l); _i <= (r); ++_i) cout << (a)[_i] << ' '; cout << '\n';}

template<class X, class Y>
bool minimize(X &x, const Y &y) {
    if (x > y) {
        x = y;
        return true;
    } else return false;
}

template<class X, class Y>
bool maximize(X &x, const Y &y) {
    if (x < y) {
        x = y;
        return true;
    } else return false;
}

/* END OF TEMPALTE */

struct FenwickTree {
    int n;
    vector<ll> fen;

    FenwickTree (int _n) : n(_n), fen(n + 5, 0ll) {}

    void update(int idx, ll v) {
        for (int i = idx; i <= n; i += i & -i)
            fen[i] += v;
    }

    void updateRange(int l, int r, ll v) {
        if (l > r) swap(l, r);
        update(l, v); update(r + 1, -v);
    }

    ll get(int idx) {
        ll sum = 0;
        for (int i = idx; i; i -= i & -i)
            sum += fen[i];
        return sum;
    }
};

const int MAXN = (int)1e5 + 5;
const int MAXH = (int)1e6 + 5;
int n, q, a[MAXN];

void init() {
    cin >> n >> q;
    for (int i = 1; i <= n; ++i) cin >> a[i];
}

void solve() {
    FenwickTree fen(MAXH);
    for (int i = 1; i < n; ++i)
        fen.updateRange(a[i], a[i + 1], 1);

    while (q--) {
        int type; cin >> type;
        if (type == 1) {
            int pos, val; cin >> pos >> val;
            
            if (pos > 1) {
                fen.updateRange(a[pos - 1], a[pos], -1);
                fen.updateRange(a[pos - 1], val, 1);
            }

            if (pos < n) {
                fen.updateRange(a[pos + 1], a[pos], -1);
                fen.updateRange(a[pos + 1], val, 1);
            }

            a[pos] = val;
        } else {
            int h; cin >> h;
            cout << fen.get(h) << '\n';
        }
    }
}

signed main() {
    #ifdef NCTHANH
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr); cout.tie(nullptr);

    init();
    solve();

    return 0;
}
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...