This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define ll long long
#define pb push_back
#define db(val) "[" #val " = " << (val) << "] "
const ll mod = 1e9 + 7;
const int maxn = 1e6 + 4;
const int INF = 1e9;
int n, q, h[maxn], a[maxn], sum[maxn], pre[maxn], nxt[maxn];
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
//freopen(".INP", "r", stdin);
//freopen(".OUT", "w", stdout);
cin >> n >> q;
for (int i = 1; i <= n; i++)
cin >> h[i];
for (int i = 1; i <= q; i++)
cin >> a[i];
stack<int> st;
h[0] = h[n + 1] = INF;
st.push(0);
for (int i = 1; i <= n; i++) {
while (h[st.top()] < h[i])
st.pop();
pre[i] = st.top();
st.push(i);
}
stack<int>().swap(st);
st.push(n + 1);
for (int i = n; i >= 1; i--) {
while (h[st.top()] <= h[i])
st.pop();
nxt[i] = st.top();
st.push(i);
}
for (int i = 1; i <= n; i++)
sum[h[i]] += (i - pre[i]) * (nxt[i] - i);
for (int i = 1; i <= 1e6; i++)
sum[i] += sum[i - 1];
for (int i = 1; i <= q; i++)
cout << sum[a[i]] << '\n';
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |