이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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... |