Submission #366344

# Submission time Handle Problem Language Result Execution time Memory
366344 2021-02-14T03:33:55 Z arujbansal Po (COCI21_po) C++17
70 / 70
62 ms 24172 KB
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <array>
#include <stack>
#include <queue>
#include <random>
#include <numeric>
#include <chrono>
#include <utility>
#include <iomanip>
#include <assert.h>

using namespace std;

void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)

#define rng_init mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define rng_seed(x) mt19937 rng(x)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int) (x).size()
// #define int long long

template<bool returnMax, bool returnIndex>
struct sparse_table {
    int n, k;
    vector<int> a;
    vector<vector<int>> st;

    void init(const vector<int> &a) {
        this->a = a;
        n = (int) a.size();
        k = lg(n);
        st.assign(n + 1, vector<int>(k + 1));
        build();
    }

    int lg(int x) { return 31 - __builtin_clz(x); }

    int merge(int x, int y) {
        if (returnMax) return a[x] >= a[y] ? x : y;
        return a[x] < a[y] ? x : y;
    }

    void build() {
        for (int i = 0; i < n; i++) st[i][0] = i;

        for (int j = 1; j <= k; j++)
            for (int i = 0; i + (1 << j) - 1 < n; i++)
                st[i][j] = merge(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
    }

    int query(int l, int r) {
        int j = lg(r - l + 1);

        int idx = merge(st[l][j], st[r - (1 << j) + 1][j]);
        return (returnIndex ? idx : a[idx]);
    }
};

const int MXN = 1e5 + 5, INF = 1e9 + 1;
int N;
vector<int> A;
sparse_table<false, false> rmq;
vector<int> pos[MXN];

int f(int i, int j) {    
    if (i > j)
        return 0;

    // dbg(i, j);

    if (i == j)
        return 1;

    int mn = rmq.query(i, j);

    int l = lower_bound(all(pos[mn]), i) - pos[mn].begin();
    int r = upper_bound(all(pos[mn]), j) - pos[mn].begin() - 1;

    // dbg(l, r, mn);
    // dbg(pos[mn][l], pos[mn][r]);

    int ans = 1;

    for (int k = l + 1; k <= r; k++) {
        ans += f(pos[mn][k - 1] + 1, pos[mn][k] - 1);
    }

    if (i < pos[mn][l])
        ans += f(i, pos[mn][l] - 1);

    if (j > pos[mn][r]) 
        ans += f(pos[mn][r] + 1, j);

    return ans;
}

void solve() {
    cin >> N;

    vector<int> compress;

    A.resize(N + 2);
    for (int i = 1; i <= N; i++) {
        cin >> A[i];

        if (A[i] == 0) continue;
        compress.push_back(A[i]);
    }

    sort(all(compress));
    compress.resize(unique(all(compress)) - compress.begin());

    for (int i = 1; i <= N; i++) {
        if (A[i] == 0) continue;

        A[i] = lower_bound(all(compress), A[i]) - compress.begin() + 1;
        pos[A[i]].push_back(i);
    }

    rmq.init(A);

    int ans = 0;

    int left = 1;
    for (int j = 1; j <= N; j++) {
        if (A[j] == 0) {
            if (j > left)
                ans += f(left, j - 1);

            left = j + 1;
        }
    }

    if (left <= N)
        ans += f(left, N);

    cout << ans;
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int TC = 1;
    // cin >> TC;
    while (TC--) solve();
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2668 KB Output is correct
2 Correct 2 ms 2668 KB Output is correct
3 Correct 2 ms 2796 KB Output is correct
4 Correct 23 ms 8688 KB Output is correct
5 Correct 36 ms 11628 KB Output is correct
6 Correct 62 ms 20332 KB Output is correct
7 Correct 59 ms 24172 KB Output is correct