# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
209790 | Alexa2001 | Pilot (NOI19_pilot) | C++17 | 317 ms | 27512 KiB |
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;
/// 16:02
typedef long long ll;
const int Nmax = 1e6 + 5;
int n, q, h[Nmax], st[Nmax], nxt[Nmax], prv[Nmax];
ll ans[Nmax];
void solve()
{
int i, nr = 0;
for(i=1; i<=n; ++i)
{
while(nr && h[st[nr]] < h[i]) --nr;
prv[i] = st[nr];
st[++nr] = i;
}
nr = 0;
st[0] = n+1;
for(i=n; i; --i)
{
while(nr && h[st[nr]] <= h[i]) --nr;
nxt[i] = st[nr];
st[++nr] = i;
}
for(i=1; i<=n; ++i)
ans[h[i]] += (ll) (i - prv[i]) * (nxt[i] - i);
}
const int bsize = 1 << 18;
char buffer[bsize+2]; int cursor;
static inline void init_read()
{
fread(buffer, 1, bsize, stdin), cursor = 0;
}
template<typename T>
void read(T &x)
{
x = 0;
while(!isdigit(buffer[cursor]))
{
++cursor;
if(cursor == bsize) init_read();
}
while(isdigit(buffer[cursor]))
{
x = x * 10 + buffer[cursor] - '0';
++cursor;
if(cursor == bsize) init_read();
}
}
int main()
{
// freopen("input", "r", stdin);
cin.sync_with_stdio(false); cin.tie(0);
init_read();
read(n); read(q);
int i;
for(i=1; i<=n; ++i) read(h[i]);
solve();
for(i=1; i<=1000*1000; ++i)
ans[i+1] += ans[i];
for(i=1; i<=q; ++i)
{
int x;
read(x);
printf("%lld\n", ans[x]);
}
return 0;
}
Compilation message (stderr)
# | 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... |