#include<bits/stdc++.h>
using namespace std;
#define int long long
const int mxn =1000005;
int n,q,h[mxn],a[mxn],dp[mxn],L[mxn],R[mxn];
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
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> s;
h[0]=h[n+1]=INT_MAX;
s.push(0);
for(int i=1;i<=n;i++)
{
while(h[s.top()]<h[i])
s.pop();
L[i]=s.top();
s.push(i);
}
while(!s.empty())s.pop();
s.push(n+1);
for(int i=n;i>=1;i--)
{
while(h[s.top()]<=h[i])
s.pop();
R[i]=s.top();
s.push(i);
}
for(int i=1;i<=n;i++)
dp[h[i]]+=(i-L[i])*(R[i]-i);
for(int i=1;i<=1000000;i++)
dp[i]+=dp[i-1];
for(int i=1;i<=q;i++)
cout<<dp[a[i]]<<"\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
14684 KB |
Output is correct |
2 |
Correct |
4 ms |
13660 KB |
Output is correct |
3 |
Correct |
4 ms |
13660 KB |
Output is correct |
4 |
Correct |
4 ms |
14684 KB |
Output is correct |
5 |
Correct |
4 ms |
13660 KB |
Output is correct |
6 |
Correct |
4 ms |
14684 KB |
Output is correct |
7 |
Correct |
4 ms |
13660 KB |
Output is correct |
8 |
Correct |
4 ms |
14684 KB |
Output is correct |
9 |
Correct |
4 ms |
13660 KB |
Output is correct |
10 |
Correct |
4 ms |
14684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
14684 KB |
Output is correct |
2 |
Correct |
4 ms |
13660 KB |
Output is correct |
3 |
Correct |
4 ms |
13660 KB |
Output is correct |
4 |
Correct |
4 ms |
14684 KB |
Output is correct |
5 |
Correct |
4 ms |
13660 KB |
Output is correct |
6 |
Correct |
4 ms |
14684 KB |
Output is correct |
7 |
Correct |
4 ms |
13660 KB |
Output is correct |
8 |
Correct |
4 ms |
14684 KB |
Output is correct |
9 |
Correct |
4 ms |
13660 KB |
Output is correct |
10 |
Correct |
4 ms |
14684 KB |
Output is correct |
11 |
Correct |
4 ms |
14684 KB |
Output is correct |
12 |
Correct |
4 ms |
14684 KB |
Output is correct |
13 |
Correct |
4 ms |
14824 KB |
Output is correct |
14 |
Correct |
4 ms |
14684 KB |
Output is correct |
15 |
Correct |
4 ms |
14684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
14684 KB |
Output is correct |
2 |
Correct |
4 ms |
13660 KB |
Output is correct |
3 |
Correct |
4 ms |
13660 KB |
Output is correct |
4 |
Correct |
4 ms |
14684 KB |
Output is correct |
5 |
Correct |
4 ms |
13660 KB |
Output is correct |
6 |
Correct |
4 ms |
14684 KB |
Output is correct |
7 |
Correct |
4 ms |
13660 KB |
Output is correct |
8 |
Correct |
4 ms |
14684 KB |
Output is correct |
9 |
Correct |
4 ms |
13660 KB |
Output is correct |
10 |
Correct |
4 ms |
14684 KB |
Output is correct |
11 |
Correct |
4 ms |
14684 KB |
Output is correct |
12 |
Correct |
4 ms |
14684 KB |
Output is correct |
13 |
Correct |
4 ms |
14824 KB |
Output is correct |
14 |
Correct |
4 ms |
14684 KB |
Output is correct |
15 |
Correct |
4 ms |
14684 KB |
Output is correct |
16 |
Correct |
4 ms |
14828 KB |
Output is correct |
17 |
Correct |
4 ms |
14824 KB |
Output is correct |
18 |
Correct |
4 ms |
14684 KB |
Output is correct |
19 |
Correct |
4 ms |
14684 KB |
Output is correct |
20 |
Correct |
4 ms |
14684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
14684 KB |
Output is correct |
2 |
Correct |
4 ms |
13660 KB |
Output is correct |
3 |
Correct |
4 ms |
13660 KB |
Output is correct |
4 |
Correct |
4 ms |
14684 KB |
Output is correct |
5 |
Correct |
4 ms |
13660 KB |
Output is correct |
6 |
Correct |
4 ms |
14684 KB |
Output is correct |
7 |
Correct |
4 ms |
13660 KB |
Output is correct |
8 |
Correct |
4 ms |
14684 KB |
Output is correct |
9 |
Correct |
4 ms |
13660 KB |
Output is correct |
10 |
Correct |
4 ms |
14684 KB |
Output is correct |
11 |
Correct |
4 ms |
14684 KB |
Output is correct |
12 |
Correct |
4 ms |
14684 KB |
Output is correct |
13 |
Correct |
4 ms |
14824 KB |
Output is correct |
14 |
Correct |
4 ms |
14684 KB |
Output is correct |
15 |
Correct |
4 ms |
14684 KB |
Output is correct |
16 |
Correct |
4 ms |
14828 KB |
Output is correct |
17 |
Correct |
4 ms |
14824 KB |
Output is correct |
18 |
Correct |
4 ms |
14684 KB |
Output is correct |
19 |
Correct |
4 ms |
14684 KB |
Output is correct |
20 |
Correct |
4 ms |
14684 KB |
Output is correct |
21 |
Correct |
4 ms |
14684 KB |
Output is correct |
22 |
Correct |
4 ms |
14684 KB |
Output is correct |
23 |
Correct |
4 ms |
14684 KB |
Output is correct |
24 |
Correct |
4 ms |
14684 KB |
Output is correct |
25 |
Correct |
4 ms |
14684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
19036 KB |
Output is correct |
2 |
Correct |
13 ms |
19652 KB |
Output is correct |
3 |
Correct |
12 ms |
19548 KB |
Output is correct |
4 |
Correct |
12 ms |
19716 KB |
Output is correct |
5 |
Correct |
11 ms |
19544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
20820 KB |
Output is correct |
2 |
Correct |
24 ms |
21828 KB |
Output is correct |
3 |
Correct |
23 ms |
21844 KB |
Output is correct |
4 |
Correct |
22 ms |
21840 KB |
Output is correct |
5 |
Correct |
22 ms |
21860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
20828 KB |
Output is correct |
2 |
Correct |
23 ms |
21924 KB |
Output is correct |
3 |
Correct |
23 ms |
22108 KB |
Output is correct |
4 |
Correct |
23 ms |
22104 KB |
Output is correct |
5 |
Correct |
23 ms |
22096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
14684 KB |
Output is correct |
2 |
Correct |
4 ms |
13660 KB |
Output is correct |
3 |
Correct |
4 ms |
13660 KB |
Output is correct |
4 |
Correct |
4 ms |
14684 KB |
Output is correct |
5 |
Correct |
4 ms |
13660 KB |
Output is correct |
6 |
Correct |
4 ms |
14684 KB |
Output is correct |
7 |
Correct |
4 ms |
13660 KB |
Output is correct |
8 |
Correct |
4 ms |
14684 KB |
Output is correct |
9 |
Correct |
4 ms |
13660 KB |
Output is correct |
10 |
Correct |
4 ms |
14684 KB |
Output is correct |
11 |
Correct |
12 ms |
19036 KB |
Output is correct |
12 |
Correct |
13 ms |
19652 KB |
Output is correct |
13 |
Correct |
12 ms |
19548 KB |
Output is correct |
14 |
Correct |
12 ms |
19716 KB |
Output is correct |
15 |
Correct |
11 ms |
19544 KB |
Output is correct |
16 |
Correct |
11 ms |
19548 KB |
Output is correct |
17 |
Correct |
12 ms |
19548 KB |
Output is correct |
18 |
Correct |
13 ms |
19772 KB |
Output is correct |
19 |
Correct |
12 ms |
19548 KB |
Output is correct |
20 |
Correct |
12 ms |
19676 KB |
Output is correct |
21 |
Correct |
12 ms |
19544 KB |
Output is correct |
22 |
Correct |
13 ms |
19544 KB |
Output is correct |
23 |
Correct |
12 ms |
19548 KB |
Output is correct |
24 |
Correct |
13 ms |
19544 KB |
Output is correct |
25 |
Correct |
12 ms |
19544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
14684 KB |
Output is correct |
2 |
Correct |
4 ms |
13660 KB |
Output is correct |
3 |
Correct |
4 ms |
13660 KB |
Output is correct |
4 |
Correct |
4 ms |
14684 KB |
Output is correct |
5 |
Correct |
4 ms |
13660 KB |
Output is correct |
6 |
Correct |
4 ms |
14684 KB |
Output is correct |
7 |
Correct |
4 ms |
13660 KB |
Output is correct |
8 |
Correct |
4 ms |
14684 KB |
Output is correct |
9 |
Correct |
4 ms |
13660 KB |
Output is correct |
10 |
Correct |
4 ms |
14684 KB |
Output is correct |
11 |
Correct |
4 ms |
14684 KB |
Output is correct |
12 |
Correct |
4 ms |
14684 KB |
Output is correct |
13 |
Correct |
4 ms |
14824 KB |
Output is correct |
14 |
Correct |
4 ms |
14684 KB |
Output is correct |
15 |
Correct |
4 ms |
14684 KB |
Output is correct |
16 |
Correct |
4 ms |
14828 KB |
Output is correct |
17 |
Correct |
4 ms |
14824 KB |
Output is correct |
18 |
Correct |
4 ms |
14684 KB |
Output is correct |
19 |
Correct |
4 ms |
14684 KB |
Output is correct |
20 |
Correct |
4 ms |
14684 KB |
Output is correct |
21 |
Correct |
4 ms |
14684 KB |
Output is correct |
22 |
Correct |
4 ms |
14684 KB |
Output is correct |
23 |
Correct |
4 ms |
14684 KB |
Output is correct |
24 |
Correct |
4 ms |
14684 KB |
Output is correct |
25 |
Correct |
4 ms |
14684 KB |
Output is correct |
26 |
Correct |
12 ms |
19036 KB |
Output is correct |
27 |
Correct |
13 ms |
19652 KB |
Output is correct |
28 |
Correct |
12 ms |
19548 KB |
Output is correct |
29 |
Correct |
12 ms |
19716 KB |
Output is correct |
30 |
Correct |
11 ms |
19544 KB |
Output is correct |
31 |
Correct |
21 ms |
20820 KB |
Output is correct |
32 |
Correct |
24 ms |
21828 KB |
Output is correct |
33 |
Correct |
23 ms |
21844 KB |
Output is correct |
34 |
Correct |
22 ms |
21840 KB |
Output is correct |
35 |
Correct |
22 ms |
21860 KB |
Output is correct |
36 |
Correct |
22 ms |
20828 KB |
Output is correct |
37 |
Correct |
23 ms |
21924 KB |
Output is correct |
38 |
Correct |
23 ms |
22108 KB |
Output is correct |
39 |
Correct |
23 ms |
22104 KB |
Output is correct |
40 |
Correct |
23 ms |
22096 KB |
Output is correct |
41 |
Correct |
11 ms |
19548 KB |
Output is correct |
42 |
Correct |
12 ms |
19548 KB |
Output is correct |
43 |
Correct |
13 ms |
19772 KB |
Output is correct |
44 |
Correct |
12 ms |
19548 KB |
Output is correct |
45 |
Correct |
12 ms |
19676 KB |
Output is correct |
46 |
Correct |
12 ms |
19544 KB |
Output is correct |
47 |
Correct |
13 ms |
19544 KB |
Output is correct |
48 |
Correct |
12 ms |
19548 KB |
Output is correct |
49 |
Correct |
13 ms |
19544 KB |
Output is correct |
50 |
Correct |
12 ms |
19544 KB |
Output is correct |
51 |
Correct |
24 ms |
21080 KB |
Output is correct |
52 |
Correct |
24 ms |
20964 KB |
Output is correct |
53 |
Correct |
24 ms |
21016 KB |
Output is correct |
54 |
Correct |
24 ms |
20956 KB |
Output is correct |
55 |
Correct |
24 ms |
20820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
14684 KB |
Output is correct |
2 |
Correct |
4 ms |
13660 KB |
Output is correct |
3 |
Correct |
4 ms |
13660 KB |
Output is correct |
4 |
Correct |
4 ms |
14684 KB |
Output is correct |
5 |
Correct |
4 ms |
13660 KB |
Output is correct |
6 |
Correct |
4 ms |
14684 KB |
Output is correct |
7 |
Correct |
4 ms |
13660 KB |
Output is correct |
8 |
Correct |
4 ms |
14684 KB |
Output is correct |
9 |
Correct |
4 ms |
13660 KB |
Output is correct |
10 |
Correct |
4 ms |
14684 KB |
Output is correct |
11 |
Correct |
4 ms |
14684 KB |
Output is correct |
12 |
Correct |
4 ms |
14684 KB |
Output is correct |
13 |
Correct |
4 ms |
14824 KB |
Output is correct |
14 |
Correct |
4 ms |
14684 KB |
Output is correct |
15 |
Correct |
4 ms |
14684 KB |
Output is correct |
16 |
Correct |
4 ms |
14828 KB |
Output is correct |
17 |
Correct |
4 ms |
14824 KB |
Output is correct |
18 |
Correct |
4 ms |
14684 KB |
Output is correct |
19 |
Correct |
4 ms |
14684 KB |
Output is correct |
20 |
Correct |
4 ms |
14684 KB |
Output is correct |
21 |
Correct |
4 ms |
14684 KB |
Output is correct |
22 |
Correct |
4 ms |
14684 KB |
Output is correct |
23 |
Correct |
4 ms |
14684 KB |
Output is correct |
24 |
Correct |
4 ms |
14684 KB |
Output is correct |
25 |
Correct |
4 ms |
14684 KB |
Output is correct |
26 |
Correct |
12 ms |
19036 KB |
Output is correct |
27 |
Correct |
13 ms |
19652 KB |
Output is correct |
28 |
Correct |
12 ms |
19548 KB |
Output is correct |
29 |
Correct |
12 ms |
19716 KB |
Output is correct |
30 |
Correct |
11 ms |
19544 KB |
Output is correct |
31 |
Correct |
21 ms |
20820 KB |
Output is correct |
32 |
Correct |
24 ms |
21828 KB |
Output is correct |
33 |
Correct |
23 ms |
21844 KB |
Output is correct |
34 |
Correct |
22 ms |
21840 KB |
Output is correct |
35 |
Correct |
22 ms |
21860 KB |
Output is correct |
36 |
Correct |
22 ms |
20828 KB |
Output is correct |
37 |
Correct |
23 ms |
21924 KB |
Output is correct |
38 |
Correct |
23 ms |
22108 KB |
Output is correct |
39 |
Correct |
23 ms |
22104 KB |
Output is correct |
40 |
Correct |
23 ms |
22096 KB |
Output is correct |
41 |
Correct |
11 ms |
19548 KB |
Output is correct |
42 |
Correct |
12 ms |
19548 KB |
Output is correct |
43 |
Correct |
13 ms |
19772 KB |
Output is correct |
44 |
Correct |
12 ms |
19548 KB |
Output is correct |
45 |
Correct |
12 ms |
19676 KB |
Output is correct |
46 |
Correct |
12 ms |
19544 KB |
Output is correct |
47 |
Correct |
13 ms |
19544 KB |
Output is correct |
48 |
Correct |
12 ms |
19548 KB |
Output is correct |
49 |
Correct |
13 ms |
19544 KB |
Output is correct |
50 |
Correct |
12 ms |
19544 KB |
Output is correct |
51 |
Correct |
24 ms |
21080 KB |
Output is correct |
52 |
Correct |
24 ms |
20964 KB |
Output is correct |
53 |
Correct |
24 ms |
21016 KB |
Output is correct |
54 |
Correct |
24 ms |
20956 KB |
Output is correct |
55 |
Correct |
24 ms |
20820 KB |
Output is correct |
56 |
Correct |
219 ms |
59708 KB |
Output is correct |
57 |
Correct |
209 ms |
59476 KB |
Output is correct |
58 |
Correct |
200 ms |
58044 KB |
Output is correct |
59 |
Correct |
207 ms |
58448 KB |
Output is correct |
60 |
Correct |
216 ms |
60128 KB |
Output is correct |