/*
بسم الله الرحمن الرحيم
Author:
(:Muhammad Aneeq:)
*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
int const N=1e6+10,LG=22;
int h[N];
int n,q;
long long ANS[N]={};
int pa[N]={};
long long sz[N]={};
int root(int n)
{
if (pa[n]==n)
return n;
return (pa[n]=root(pa[n]));
}
long long ans=0;
void merge(int u,int v)
{
u=root(u);
v=root(v);
if (u==v)
return;
ans+=(sz[u]*sz[v]);
sz[u]+=sz[v];
pa[v]=u;
}
inline void solve()
{
cin>>n>>q;
for (int i=0;i<n;i++)
{
cin>>h[i];
}
for (int i=0;i<N;i++)
pa[i]=i,sz[i]=1;
vector<pair<int,int>>d;
for (int i=0;i<n;i++)
d.push_back({h[i],i});
sort(begin(d),end(d));
int i=0;
for (int mx=1;mx<N;mx++)
{
while (i<n&&d[i].first==mx)
{
if (d[i].second>0&&h[d[i].second-1]<=mx)
merge(d[i].second,d[i].second-1);
if (d[i].second+1<n&&h[d[i].second+1]<=mx)
merge(d[i].second,d[i].second+1);
i++;
ans++;
}
ANS[mx]=ans;
}
while (q--)
{
int x;
cin>>x;
cout<<ANS[x]<<endl;
}
}
signed main()
{
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
19804 KB |
Output is correct |
2 |
Correct |
9 ms |
19824 KB |
Output is correct |
3 |
Correct |
9 ms |
19804 KB |
Output is correct |
4 |
Correct |
9 ms |
19936 KB |
Output is correct |
5 |
Correct |
9 ms |
19804 KB |
Output is correct |
6 |
Correct |
8 ms |
19804 KB |
Output is correct |
7 |
Correct |
8 ms |
19804 KB |
Output is correct |
8 |
Correct |
9 ms |
19804 KB |
Output is correct |
9 |
Correct |
8 ms |
19804 KB |
Output is correct |
10 |
Correct |
8 ms |
19804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
19804 KB |
Output is correct |
2 |
Correct |
9 ms |
19824 KB |
Output is correct |
3 |
Correct |
9 ms |
19804 KB |
Output is correct |
4 |
Correct |
9 ms |
19936 KB |
Output is correct |
5 |
Correct |
9 ms |
19804 KB |
Output is correct |
6 |
Correct |
8 ms |
19804 KB |
Output is correct |
7 |
Correct |
8 ms |
19804 KB |
Output is correct |
8 |
Correct |
9 ms |
19804 KB |
Output is correct |
9 |
Correct |
8 ms |
19804 KB |
Output is correct |
10 |
Correct |
8 ms |
19804 KB |
Output is correct |
11 |
Correct |
9 ms |
19804 KB |
Output is correct |
12 |
Correct |
8 ms |
19968 KB |
Output is correct |
13 |
Correct |
8 ms |
19804 KB |
Output is correct |
14 |
Correct |
8 ms |
19804 KB |
Output is correct |
15 |
Correct |
9 ms |
19940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
19804 KB |
Output is correct |
2 |
Correct |
9 ms |
19824 KB |
Output is correct |
3 |
Correct |
9 ms |
19804 KB |
Output is correct |
4 |
Correct |
9 ms |
19936 KB |
Output is correct |
5 |
Correct |
9 ms |
19804 KB |
Output is correct |
6 |
Correct |
8 ms |
19804 KB |
Output is correct |
7 |
Correct |
8 ms |
19804 KB |
Output is correct |
8 |
Correct |
9 ms |
19804 KB |
Output is correct |
9 |
Correct |
8 ms |
19804 KB |
Output is correct |
10 |
Correct |
8 ms |
19804 KB |
Output is correct |
11 |
Correct |
9 ms |
19804 KB |
Output is correct |
12 |
Correct |
8 ms |
19968 KB |
Output is correct |
13 |
Correct |
8 ms |
19804 KB |
Output is correct |
14 |
Correct |
8 ms |
19804 KB |
Output is correct |
15 |
Correct |
9 ms |
19940 KB |
Output is correct |
16 |
Correct |
10 ms |
19804 KB |
Output is correct |
17 |
Correct |
9 ms |
19968 KB |
Output is correct |
18 |
Correct |
9 ms |
19840 KB |
Output is correct |
19 |
Correct |
9 ms |
19804 KB |
Output is correct |
20 |
Correct |
9 ms |
19804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
19804 KB |
Output is correct |
2 |
Correct |
9 ms |
19824 KB |
Output is correct |
3 |
Correct |
9 ms |
19804 KB |
Output is correct |
4 |
Correct |
9 ms |
19936 KB |
Output is correct |
5 |
Correct |
9 ms |
19804 KB |
Output is correct |
6 |
Correct |
8 ms |
19804 KB |
Output is correct |
7 |
Correct |
8 ms |
19804 KB |
Output is correct |
8 |
Correct |
9 ms |
19804 KB |
Output is correct |
9 |
Correct |
8 ms |
19804 KB |
Output is correct |
10 |
Correct |
8 ms |
19804 KB |
Output is correct |
11 |
Correct |
9 ms |
19804 KB |
Output is correct |
12 |
Correct |
8 ms |
19968 KB |
Output is correct |
13 |
Correct |
8 ms |
19804 KB |
Output is correct |
14 |
Correct |
8 ms |
19804 KB |
Output is correct |
15 |
Correct |
9 ms |
19940 KB |
Output is correct |
16 |
Correct |
10 ms |
19804 KB |
Output is correct |
17 |
Correct |
9 ms |
19968 KB |
Output is correct |
18 |
Correct |
9 ms |
19840 KB |
Output is correct |
19 |
Correct |
9 ms |
19804 KB |
Output is correct |
20 |
Correct |
9 ms |
19804 KB |
Output is correct |
21 |
Correct |
10 ms |
20060 KB |
Output is correct |
22 |
Correct |
10 ms |
20060 KB |
Output is correct |
23 |
Correct |
11 ms |
20060 KB |
Output is correct |
24 |
Correct |
10 ms |
20056 KB |
Output is correct |
25 |
Correct |
9 ms |
20068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
21200 KB |
Output is correct |
2 |
Correct |
25 ms |
21200 KB |
Output is correct |
3 |
Correct |
23 ms |
21208 KB |
Output is correct |
4 |
Correct |
24 ms |
21108 KB |
Output is correct |
5 |
Correct |
24 ms |
21196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
125 ms |
22216 KB |
Output is correct |
2 |
Correct |
125 ms |
22224 KB |
Output is correct |
3 |
Correct |
127 ms |
22224 KB |
Output is correct |
4 |
Correct |
121 ms |
22216 KB |
Output is correct |
5 |
Correct |
119 ms |
22220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
120 ms |
22068 KB |
Output is correct |
2 |
Correct |
134 ms |
22084 KB |
Output is correct |
3 |
Correct |
122 ms |
22220 KB |
Output is correct |
4 |
Correct |
125 ms |
22220 KB |
Output is correct |
5 |
Correct |
134 ms |
22224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
19804 KB |
Output is correct |
2 |
Correct |
9 ms |
19824 KB |
Output is correct |
3 |
Correct |
9 ms |
19804 KB |
Output is correct |
4 |
Correct |
9 ms |
19936 KB |
Output is correct |
5 |
Correct |
9 ms |
19804 KB |
Output is correct |
6 |
Correct |
8 ms |
19804 KB |
Output is correct |
7 |
Correct |
8 ms |
19804 KB |
Output is correct |
8 |
Correct |
9 ms |
19804 KB |
Output is correct |
9 |
Correct |
8 ms |
19804 KB |
Output is correct |
10 |
Correct |
8 ms |
19804 KB |
Output is correct |
11 |
Correct |
24 ms |
21200 KB |
Output is correct |
12 |
Correct |
25 ms |
21200 KB |
Output is correct |
13 |
Correct |
23 ms |
21208 KB |
Output is correct |
14 |
Correct |
24 ms |
21108 KB |
Output is correct |
15 |
Correct |
24 ms |
21196 KB |
Output is correct |
16 |
Correct |
23 ms |
21136 KB |
Output is correct |
17 |
Correct |
26 ms |
21200 KB |
Output is correct |
18 |
Correct |
25 ms |
21292 KB |
Output is correct |
19 |
Correct |
23 ms |
21108 KB |
Output is correct |
20 |
Correct |
25 ms |
21200 KB |
Output is correct |
21 |
Correct |
24 ms |
21120 KB |
Output is correct |
22 |
Correct |
28 ms |
21196 KB |
Output is correct |
23 |
Correct |
24 ms |
21228 KB |
Output is correct |
24 |
Correct |
25 ms |
21208 KB |
Output is correct |
25 |
Correct |
24 ms |
21208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
19804 KB |
Output is correct |
2 |
Correct |
9 ms |
19824 KB |
Output is correct |
3 |
Correct |
9 ms |
19804 KB |
Output is correct |
4 |
Correct |
9 ms |
19936 KB |
Output is correct |
5 |
Correct |
9 ms |
19804 KB |
Output is correct |
6 |
Correct |
8 ms |
19804 KB |
Output is correct |
7 |
Correct |
8 ms |
19804 KB |
Output is correct |
8 |
Correct |
9 ms |
19804 KB |
Output is correct |
9 |
Correct |
8 ms |
19804 KB |
Output is correct |
10 |
Correct |
8 ms |
19804 KB |
Output is correct |
11 |
Correct |
9 ms |
19804 KB |
Output is correct |
12 |
Correct |
8 ms |
19968 KB |
Output is correct |
13 |
Correct |
8 ms |
19804 KB |
Output is correct |
14 |
Correct |
8 ms |
19804 KB |
Output is correct |
15 |
Correct |
9 ms |
19940 KB |
Output is correct |
16 |
Correct |
10 ms |
19804 KB |
Output is correct |
17 |
Correct |
9 ms |
19968 KB |
Output is correct |
18 |
Correct |
9 ms |
19840 KB |
Output is correct |
19 |
Correct |
9 ms |
19804 KB |
Output is correct |
20 |
Correct |
9 ms |
19804 KB |
Output is correct |
21 |
Correct |
10 ms |
20060 KB |
Output is correct |
22 |
Correct |
10 ms |
20060 KB |
Output is correct |
23 |
Correct |
11 ms |
20060 KB |
Output is correct |
24 |
Correct |
10 ms |
20056 KB |
Output is correct |
25 |
Correct |
9 ms |
20068 KB |
Output is correct |
26 |
Correct |
24 ms |
21200 KB |
Output is correct |
27 |
Correct |
25 ms |
21200 KB |
Output is correct |
28 |
Correct |
23 ms |
21208 KB |
Output is correct |
29 |
Correct |
24 ms |
21108 KB |
Output is correct |
30 |
Correct |
24 ms |
21196 KB |
Output is correct |
31 |
Correct |
125 ms |
22216 KB |
Output is correct |
32 |
Correct |
125 ms |
22224 KB |
Output is correct |
33 |
Correct |
127 ms |
22224 KB |
Output is correct |
34 |
Correct |
121 ms |
22216 KB |
Output is correct |
35 |
Correct |
119 ms |
22220 KB |
Output is correct |
36 |
Correct |
120 ms |
22068 KB |
Output is correct |
37 |
Correct |
134 ms |
22084 KB |
Output is correct |
38 |
Correct |
122 ms |
22220 KB |
Output is correct |
39 |
Correct |
125 ms |
22220 KB |
Output is correct |
40 |
Correct |
134 ms |
22224 KB |
Output is correct |
41 |
Correct |
23 ms |
21136 KB |
Output is correct |
42 |
Correct |
26 ms |
21200 KB |
Output is correct |
43 |
Correct |
25 ms |
21292 KB |
Output is correct |
44 |
Correct |
23 ms |
21108 KB |
Output is correct |
45 |
Correct |
25 ms |
21200 KB |
Output is correct |
46 |
Correct |
24 ms |
21120 KB |
Output is correct |
47 |
Correct |
28 ms |
21196 KB |
Output is correct |
48 |
Correct |
24 ms |
21228 KB |
Output is correct |
49 |
Correct |
25 ms |
21208 KB |
Output is correct |
50 |
Correct |
24 ms |
21208 KB |
Output is correct |
51 |
Correct |
136 ms |
21984 KB |
Output is correct |
52 |
Correct |
170 ms |
21704 KB |
Output is correct |
53 |
Correct |
143 ms |
21900 KB |
Output is correct |
54 |
Correct |
139 ms |
21712 KB |
Output is correct |
55 |
Correct |
128 ms |
21964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
19804 KB |
Output is correct |
2 |
Correct |
9 ms |
19824 KB |
Output is correct |
3 |
Correct |
9 ms |
19804 KB |
Output is correct |
4 |
Correct |
9 ms |
19936 KB |
Output is correct |
5 |
Correct |
9 ms |
19804 KB |
Output is correct |
6 |
Correct |
8 ms |
19804 KB |
Output is correct |
7 |
Correct |
8 ms |
19804 KB |
Output is correct |
8 |
Correct |
9 ms |
19804 KB |
Output is correct |
9 |
Correct |
8 ms |
19804 KB |
Output is correct |
10 |
Correct |
8 ms |
19804 KB |
Output is correct |
11 |
Correct |
9 ms |
19804 KB |
Output is correct |
12 |
Correct |
8 ms |
19968 KB |
Output is correct |
13 |
Correct |
8 ms |
19804 KB |
Output is correct |
14 |
Correct |
8 ms |
19804 KB |
Output is correct |
15 |
Correct |
9 ms |
19940 KB |
Output is correct |
16 |
Correct |
10 ms |
19804 KB |
Output is correct |
17 |
Correct |
9 ms |
19968 KB |
Output is correct |
18 |
Correct |
9 ms |
19840 KB |
Output is correct |
19 |
Correct |
9 ms |
19804 KB |
Output is correct |
20 |
Correct |
9 ms |
19804 KB |
Output is correct |
21 |
Correct |
10 ms |
20060 KB |
Output is correct |
22 |
Correct |
10 ms |
20060 KB |
Output is correct |
23 |
Correct |
11 ms |
20060 KB |
Output is correct |
24 |
Correct |
10 ms |
20056 KB |
Output is correct |
25 |
Correct |
9 ms |
20068 KB |
Output is correct |
26 |
Correct |
24 ms |
21200 KB |
Output is correct |
27 |
Correct |
25 ms |
21200 KB |
Output is correct |
28 |
Correct |
23 ms |
21208 KB |
Output is correct |
29 |
Correct |
24 ms |
21108 KB |
Output is correct |
30 |
Correct |
24 ms |
21196 KB |
Output is correct |
31 |
Correct |
125 ms |
22216 KB |
Output is correct |
32 |
Correct |
125 ms |
22224 KB |
Output is correct |
33 |
Correct |
127 ms |
22224 KB |
Output is correct |
34 |
Correct |
121 ms |
22216 KB |
Output is correct |
35 |
Correct |
119 ms |
22220 KB |
Output is correct |
36 |
Correct |
120 ms |
22068 KB |
Output is correct |
37 |
Correct |
134 ms |
22084 KB |
Output is correct |
38 |
Correct |
122 ms |
22220 KB |
Output is correct |
39 |
Correct |
125 ms |
22220 KB |
Output is correct |
40 |
Correct |
134 ms |
22224 KB |
Output is correct |
41 |
Correct |
23 ms |
21136 KB |
Output is correct |
42 |
Correct |
26 ms |
21200 KB |
Output is correct |
43 |
Correct |
25 ms |
21292 KB |
Output is correct |
44 |
Correct |
23 ms |
21108 KB |
Output is correct |
45 |
Correct |
25 ms |
21200 KB |
Output is correct |
46 |
Correct |
24 ms |
21120 KB |
Output is correct |
47 |
Correct |
28 ms |
21196 KB |
Output is correct |
48 |
Correct |
24 ms |
21228 KB |
Output is correct |
49 |
Correct |
25 ms |
21208 KB |
Output is correct |
50 |
Correct |
24 ms |
21208 KB |
Output is correct |
51 |
Correct |
136 ms |
21984 KB |
Output is correct |
52 |
Correct |
170 ms |
21704 KB |
Output is correct |
53 |
Correct |
143 ms |
21900 KB |
Output is correct |
54 |
Correct |
139 ms |
21712 KB |
Output is correct |
55 |
Correct |
128 ms |
21964 KB |
Output is correct |
56 |
Execution timed out |
1043 ms |
48560 KB |
Time limit exceeded |
57 |
Halted |
0 ms |
0 KB |
- |