Submission #1025792

# Submission time Handle Problem Language Result Execution time Memory
1025792 2024-07-17T10:10:10 Z phoenix Simple game (IZhO17_game) C++17
22 / 100
1000 ms 176384 KB
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;



typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag, 
tree_order_statistics_node_update> ordered_set;


const int N = 100100;
const int M = (1 << 18);

ordered_set st[2 * M];

bool count(ordered_set &st, pair<int, int> p) {
    return (st.lower_bound(p) != st.end() && *st.lower_bound(p) == p);
}

void insert(int pos, pair<int, int> p) {
    for (pos += M; pos; pos >>= 1)
        st[pos].insert(p);
}


void erase(int pos, pair<int, int> p) {
    static int iteration = 0;
    for (pos += M; pos; pos >>= 1) {
        assert(count(st[pos], p));
        st[pos].erase(p);
    }
}

int get_lower(int ql, int qr, int H) {
    int res = 0;
    for (ql += M, qr += M + 1; ql < qr; ql >>= 1, qr >>= 1) {
        if (ql & 1) {
            res += st[ql].order_of_key({H, 0});
            ql++;
        }
        if (qr & 1) {
            qr--;
            res += st[qr].order_of_key({H, 0});
        }
    }
    return res;
}
int get_upper(int ql, int qr, int H) {
    int res = 0;
    for (ql += M, qr += M + 1; ql < qr; ql >>= 1, qr >>= 1) {
        if (ql & 1) {
            res += (int)st[ql].size() - st[ql].order_of_key({H, 0});
            ql++;
        }
        if (qr & 1) {
            qr--;
            res += (int)st[qr].size() - st[qr].order_of_key({H, 0});
        }
    }
    return res;
}
 
int n, m;

int h[N];

vector<vector<int>> queries;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    #ifdef LOCAL 
    freopen("input.txt", "r", stdin);
    #endif

    cin >> n >> m;
    for (int i = 0; i < n; i++)
        cin >> h[i];

    for (int i = 0; i < m; i++) {
        int tp;
        cin >> tp;
        if (tp == 1) {
            int pos, val;
            cin >> pos >> val;
            pos--;
            queries.push_back({tp, pos, val});
        } else {
            int H;
            cin >> H;
            queries.push_back({tp, H});
        }
    }
    

    {
        map<int, int> mp;
        for (int i = 0; i < n; i++)
            mp[h[i]] = 1;
        for (int i = 0; i < m; i++) 
            mp[queries[i].back()] = 1;
        
        int k = 0;
        for (auto &c : mp)
            c.second = k++;
        for (int i = 0; i < n; i++)
            h[i] = mp[h[i]];
        for (int i = 0; i < m; i++)
            queries[i].back() = mp[queries[i].back()];
    }

    for (int i = 0; i < n - 1; i++) {
        insert(h[i], {h[i + 1], i});   
    }
    

    for (auto c : queries) {
        int tp = c[0];
        if (tp == 1) {
            int pos = c[1], val = c[2];
            if (pos) 
                erase(h[pos - 1], {h[pos], pos - 1});
            if (pos < n - 1)
                erase(h[pos], {h[pos + 1], pos});
            h[pos] = val;
            if (pos) 
                insert(h[pos - 1], {h[pos], pos - 1});
            if (pos < n - 1)
                insert(h[pos], {h[pos + 1], pos});
        } else {
            int H = c[1];
            cout << get_upper(0, H, H) + get_lower(H + 1, M - 1, H) << '\n';
        }
    }

    return 0;
}

Compilation message

game.cpp: In function 'void erase(int, std::pair<int, int>)':
game.cpp:32:16: warning: unused variable 'iteration' [-Wunused-variable]
   32 |     static int iteration = 0;
      |                ^~~~~~~~~
game.cpp: At global scope:
game.cpp:32:16: warning: 'iteration' defined but not used [-Wunused-variable]
# Verdict Execution time Memory Grader output
1 Correct 26 ms 49748 KB Output is correct
2 Correct 32 ms 50944 KB Output is correct
3 Correct 33 ms 51176 KB Output is correct
4 Correct 34 ms 51056 KB Output is correct
5 Correct 33 ms 51036 KB Output is correct
6 Correct 32 ms 51024 KB Output is correct
7 Correct 29 ms 51036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 49748 KB Output is correct
2 Correct 32 ms 50944 KB Output is correct
3 Correct 33 ms 51176 KB Output is correct
4 Correct 34 ms 51056 KB Output is correct
5 Correct 33 ms 51036 KB Output is correct
6 Correct 32 ms 51024 KB Output is correct
7 Correct 29 ms 51036 KB Output is correct
8 Correct 979 ms 174588 KB Output is correct
9 Execution timed out 1070 ms 176384 KB Time limit exceeded
10 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 26 ms 49748 KB Output is correct
2 Correct 32 ms 50944 KB Output is correct
3 Correct 33 ms 51176 KB Output is correct
4 Correct 34 ms 51056 KB Output is correct
5 Correct 33 ms 51036 KB Output is correct
6 Correct 32 ms 51024 KB Output is correct
7 Correct 29 ms 51036 KB Output is correct
8 Correct 979 ms 174588 KB Output is correct
9 Execution timed out 1070 ms 176384 KB Time limit exceeded
10 Halted 0 ms 0 KB -