Submission #1337812

#TimeUsernameProblemLanguageResultExecution timeMemory
1337812nguyenkhangninh99Simple game (IZhO17_game)C++20
100 / 100
42 ms9556 KiB

#include<bits/stdc++.h>
using namespace std;

#define int long long
const int maxn = 1e6 + 5;
int bit[maxn];
void update(int p, int val){
    for(; p < maxn; p += p & -p) bit[p] += val;
}
signed main(){
    ios_base::sync_with_stdio(0); 
    cin.tie(0); cout.tie(0);

    int n, q; cin >> n >> q;
    vector<int> a(n + 1);
    for(int i = 1; i <= n; i++) cin >> a[i];
    for(int i = 2; i <= n; i++) update(min(a[i - 1], a[i]), 1), update(max(a[i - 1], a[i]) + 1, -1);
    while(q--){
        int type; cin >> type;
        if(type == 1){
            int i, vall; cin >> i >> vall;
            if(i > 1){
                update(min(a[i - 1], a[i]), -1);
                update(max(a[i - 1], a[i]) + 1, 1);
                update(min(a[i - 1], vall), 1);
                update(max(a[i - 1], vall) + 1, -1);                
            }
            if(i < n){
                update(min(a[i], a[i + 1]), -1);
                update(max(a[i], a[i + 1]) + 1, 1);
                update(min(vall, a[i + 1]), 1);
                update(max(vall, a[i + 1]) + 1, -1);       
            }
            a[i] = vall;
        }else{
            int x; cin >> x;
            int res = 0;
            for(; x; x -= x & -x) res += bit[x];
            cout << res << "\n";
        }
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...