Submission #909428

#TimeUsernameProblemLanguageResultExecution timeMemory
909428LOLOLOPilot (NOI19_pilot)C++17
100 / 100
666 ms58228 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

#define           f     first
#define           s     second
#define           pb    push_back
#define           ep    emplace
#define           eb    emplace_back
#define           lb    lower_bound
#define           ub    upper_bound
#define       all(x)    x.begin(), x.end()
#define      rall(x)    x.rbegin(), x.rend()
#define   uniquev(v)    sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define     mem(f,x)    memset(f , x , sizeof(f))
#define        sz(x)    (int)(x).size()
#define  __lcm(a, b)    (1ll * ((a) / __gcd((a), (b))) * (b))
#define          mxx    *max_element
#define          mnn    *min_element
#define    cntbit(x)    __builtin_popcountll(x)
#define       len(x)    (int)(x.length())

const int N = 1e6 + 100;
ll pr[N], l[N], r[N];
int a[N], b[N], n, c[N], d[N], sz = 0;

void compress() {
    int lst = 0;
    for (int i = 1; i <= n; i++) {
        if (b[i] != lst) {
            lst = b[i];
            sz++;
            b[sz] = lst;
        }
    }

    for (int i = 1; i <= n; i++) {
        c[i] = lower_bound(b, b + 1 + sz, a[i]) - b;
    }
}

ll solve() {
    int m;
    cin >> n >> m;

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

    sort(b + 1, b + 1 + n);
    compress();

    a[0] = a[n + 1] = 1e9 + 1;
    stack <int> st;

    for (int i = 0; i <= n; i++) {
        while (sz(st) && a[i] > a[st.top()]) {
            st.pop();
        }

        if (sz(st)) {
            l[i] = st.top() + 1;
        }

        st.push(i);
    }

    while (sz(st))
        st.pop();

    for (int i = n + 1; i >= 1; i--) {
        while (sz(st) && a[i] >= a[st.top()]) {
            st.pop();
        }

        if (sz(st)) {
            r[i] = st.top() - 1;
        }

        st.push(i);
    }

    for (ll i = 1; i <= n; i++) {
        pr[c[i]] += (i - l[i] + 1) * (r[i] - i + 1);
    }

    for (int i = 1; i <= n; i++) {
        pr[i] += pr[i - 1];
    }

    while (m--) {
        int x;
        cin >> x;
        x = upper_bound(b, b + 1 + sz, x) - b - 1;
        cout << pr[x] << '\n';
    }

    return 0;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int t = 1;
    //cin >> t;

    while (t--) {
        solve();
        //cout << solve() << '\n';
    }
}
#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...