Submission #1348474

#TimeUsernameProblemLanguageResultExecution timeMemory
1348474chithanhnguyenPilot (NOI19_pilot)C++20
100 / 100
305 ms39840 KiB
/*
Author: Nguyen Chi Thanh - High School for the Gifted - VNU.HCM (i2528)
*/
#include <bits/stdc++.h>
using namespace std;

struct DSU{
    int n;
    vector<int> par, sz;
 
    DSU(int n) : n(n), par(n + 5), sz(n + 5) {
        iota(par.begin(), par.end(), 0);
        fill(sz.begin(), sz.end(), 1);
    }
 
    int findSet(int x) {
        while (x != par[x])
            x = par[x] = par[par[x]];
        return x;
    }
 
    bool unite(int x, int y) {
        x = findSet(x); y = findSet(y);
        if (x == y) return 0;
        if (sz[x] < sz[y]) swap(x, y);
 
        sz[x] += sz[y];
        par[y] = x;
 
        return 1;
    }
 
    int size(int u) {
        return sz[findSet(u)];
    }
 
    bool same(int u, int v) {
        return findSet(u) == findSet(v);
    }
};

/* 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 */

const int MAXN = 1e6 + 6;
int n, q;
pii h[MAXN], queries[MAXN];
bool active[MAXN];

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

    for (int i = 1; i <= q; ++i) {
        int x; cin >> x;
        queries[i] = {x, i};
    }
}

ll ans[MAXN];
ll contrib(ll x) {
    return x * (x + 1) / 2;
}

void solve() {
    sort(h + 1, h + n + 1);
    sort(queries + 1, queries + q + 1);

    DSU dsu(n); ll numPairs = 0;

    auto updatePos = [&] (int p) {
        if (p > 1 && active[p - 1]) {
            int sz = dsu.size(p - 1);
            numPairs -= contrib(sz);
            dsu.unite(p, p - 1);
        }

        if (p < n && active[p + 1]) {
            int sz = dsu.size(p + 1);
            numPairs -= contrib(sz);
            dsu.unite(p, p + 1);
        }

        int new_sz = dsu.size(p);
        numPairs += contrib(new_sz);
        active[p] = 1;
    };

    int ptr = 1;
    for (int i = 1; i <= q; ++i) {
        int x = queries[i].fi, id = queries[i].se;
        while (ptr <= n && h[ptr].fi <= x) {
            updatePos(h[ptr].se);
            ++ptr;
        }

        ans[id] = numPairs;
    }

    for (int i = 1; i <= q; ++i)
        cout << ans[i] << '\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;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...