#include <bits/stdc++.h>
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define debug(x) cout << "Line " << __LINE__ << ", " << #x << " is " << x << endl
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ll long long
#define pll pair<ll, ll>
#define ull unsigned long long
#define ld long double
#define pii pair<ll, ll>
#define nl '\n'
#define tb '\t'
#define sp ' '
using namespace std;
const ll MX=1000007, MOD=1e9+7, BLOCK=330, INF=1e9+7;
const ld ERR=1e-7, pi=3.14159265358979323846;
ll N, Q, H[MX], ptr=0;
ll tot=0, ans[MX];
pii A[MX], Y[MX];
ll calc(ll x){
return x*(1ll+x)/2ll;
}
struct dsu{
ll par[MX], sz[MX];
dsu(){
for(ll i=0; i<MX; i++){
par[i]=i;
sz[i]=1;
}
}
ll f(ll u){
if(par[u]!=u)
par[u]=f(par[u]);
return par[u];
}
void join(ll u, ll v){
u=f(u);
v=f(v);
if(u==v)
return;
if(sz[u]>sz[v])
swap(u, v);
tot-=calc(sz[u])+calc(sz[v]);
par[u]=v;
sz[v]+=sz[u];
tot+=calc(sz[v]);
}
}DSU;
int main(){
fastio;
cin >> N >> Q;
for(ll i=0; i<N; i++){
cin >> H[i];
A[i]={H[i], i};
}
for(ll i=0; i<Q; i++){
cin >> Y[i].fi;
Y[i].se=i;
}
sort(Y, Y+Q);
sort(A, A+N);
for(ll i=0; ptr<Q && i<N; i++){
ll h=A[i].fi, idx=A[i].se;
while(ptr<Q && Y[ptr].fi<h)
ans[Y[ptr++].se]=tot;
if(ptr>=Q)
break;
tot++;
if(idx && H[idx-1]<=h)
DSU.join(idx-1, idx);
if(idx+1<N && H[idx+1]<=h)
DSU.join(idx+1, idx);
}
while(ptr<Q)
ans[Y[ptr++].se]=tot;
for(ll i=0; i<Q; i++){
cout << ans[i] << nl;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
15948 KB |
Output is correct |
2 |
Correct |
6 ms |
15948 KB |
Output is correct |
3 |
Correct |
6 ms |
15912 KB |
Output is correct |
4 |
Correct |
7 ms |
15948 KB |
Output is correct |
5 |
Correct |
6 ms |
15948 KB |
Output is correct |
6 |
Correct |
6 ms |
15948 KB |
Output is correct |
7 |
Correct |
6 ms |
15948 KB |
Output is correct |
8 |
Correct |
7 ms |
15948 KB |
Output is correct |
9 |
Correct |
7 ms |
15948 KB |
Output is correct |
10 |
Correct |
7 ms |
15948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
15948 KB |
Output is correct |
2 |
Correct |
6 ms |
15948 KB |
Output is correct |
3 |
Correct |
6 ms |
15912 KB |
Output is correct |
4 |
Correct |
7 ms |
15948 KB |
Output is correct |
5 |
Correct |
6 ms |
15948 KB |
Output is correct |
6 |
Correct |
6 ms |
15948 KB |
Output is correct |
7 |
Correct |
6 ms |
15948 KB |
Output is correct |
8 |
Correct |
7 ms |
15948 KB |
Output is correct |
9 |
Correct |
7 ms |
15948 KB |
Output is correct |
10 |
Correct |
7 ms |
15948 KB |
Output is correct |
11 |
Correct |
6 ms |
15948 KB |
Output is correct |
12 |
Correct |
6 ms |
15936 KB |
Output is correct |
13 |
Correct |
6 ms |
15948 KB |
Output is correct |
14 |
Correct |
6 ms |
15948 KB |
Output is correct |
15 |
Correct |
6 ms |
15948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
15948 KB |
Output is correct |
2 |
Correct |
6 ms |
15948 KB |
Output is correct |
3 |
Correct |
6 ms |
15912 KB |
Output is correct |
4 |
Correct |
7 ms |
15948 KB |
Output is correct |
5 |
Correct |
6 ms |
15948 KB |
Output is correct |
6 |
Correct |
6 ms |
15948 KB |
Output is correct |
7 |
Correct |
6 ms |
15948 KB |
Output is correct |
8 |
Correct |
7 ms |
15948 KB |
Output is correct |
9 |
Correct |
7 ms |
15948 KB |
Output is correct |
10 |
Correct |
7 ms |
15948 KB |
Output is correct |
11 |
Correct |
6 ms |
15948 KB |
Output is correct |
12 |
Correct |
6 ms |
15936 KB |
Output is correct |
13 |
Correct |
6 ms |
15948 KB |
Output is correct |
14 |
Correct |
6 ms |
15948 KB |
Output is correct |
15 |
Correct |
6 ms |
15948 KB |
Output is correct |
16 |
Correct |
6 ms |
16004 KB |
Output is correct |
17 |
Correct |
7 ms |
15948 KB |
Output is correct |
18 |
Correct |
7 ms |
15948 KB |
Output is correct |
19 |
Correct |
7 ms |
15948 KB |
Output is correct |
20 |
Correct |
8 ms |
15928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
15948 KB |
Output is correct |
2 |
Correct |
6 ms |
15948 KB |
Output is correct |
3 |
Correct |
6 ms |
15912 KB |
Output is correct |
4 |
Correct |
7 ms |
15948 KB |
Output is correct |
5 |
Correct |
6 ms |
15948 KB |
Output is correct |
6 |
Correct |
6 ms |
15948 KB |
Output is correct |
7 |
Correct |
6 ms |
15948 KB |
Output is correct |
8 |
Correct |
7 ms |
15948 KB |
Output is correct |
9 |
Correct |
7 ms |
15948 KB |
Output is correct |
10 |
Correct |
7 ms |
15948 KB |
Output is correct |
11 |
Correct |
6 ms |
15948 KB |
Output is correct |
12 |
Correct |
6 ms |
15936 KB |
Output is correct |
13 |
Correct |
6 ms |
15948 KB |
Output is correct |
14 |
Correct |
6 ms |
15948 KB |
Output is correct |
15 |
Correct |
6 ms |
15948 KB |
Output is correct |
16 |
Correct |
6 ms |
16004 KB |
Output is correct |
17 |
Correct |
7 ms |
15948 KB |
Output is correct |
18 |
Correct |
7 ms |
15948 KB |
Output is correct |
19 |
Correct |
7 ms |
15948 KB |
Output is correct |
20 |
Correct |
8 ms |
15928 KB |
Output is correct |
21 |
Correct |
7 ms |
15948 KB |
Output is correct |
22 |
Correct |
6 ms |
16016 KB |
Output is correct |
23 |
Correct |
7 ms |
15948 KB |
Output is correct |
24 |
Correct |
7 ms |
15948 KB |
Output is correct |
25 |
Correct |
7 ms |
15948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
18252 KB |
Output is correct |
2 |
Correct |
25 ms |
18252 KB |
Output is correct |
3 |
Correct |
24 ms |
18124 KB |
Output is correct |
4 |
Correct |
24 ms |
18080 KB |
Output is correct |
5 |
Correct |
24 ms |
18080 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
21188 KB |
Output is correct |
2 |
Correct |
38 ms |
21444 KB |
Output is correct |
3 |
Correct |
44 ms |
21376 KB |
Output is correct |
4 |
Correct |
38 ms |
21444 KB |
Output is correct |
5 |
Correct |
37 ms |
21228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
21180 KB |
Output is correct |
2 |
Correct |
37 ms |
21292 KB |
Output is correct |
3 |
Correct |
39 ms |
21360 KB |
Output is correct |
4 |
Correct |
38 ms |
21388 KB |
Output is correct |
5 |
Correct |
40 ms |
21580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
15948 KB |
Output is correct |
2 |
Correct |
6 ms |
15948 KB |
Output is correct |
3 |
Correct |
6 ms |
15912 KB |
Output is correct |
4 |
Correct |
7 ms |
15948 KB |
Output is correct |
5 |
Correct |
6 ms |
15948 KB |
Output is correct |
6 |
Correct |
6 ms |
15948 KB |
Output is correct |
7 |
Correct |
6 ms |
15948 KB |
Output is correct |
8 |
Correct |
7 ms |
15948 KB |
Output is correct |
9 |
Correct |
7 ms |
15948 KB |
Output is correct |
10 |
Correct |
7 ms |
15948 KB |
Output is correct |
11 |
Correct |
24 ms |
18252 KB |
Output is correct |
12 |
Correct |
25 ms |
18252 KB |
Output is correct |
13 |
Correct |
24 ms |
18124 KB |
Output is correct |
14 |
Correct |
24 ms |
18080 KB |
Output is correct |
15 |
Correct |
24 ms |
18080 KB |
Output is correct |
16 |
Correct |
23 ms |
18152 KB |
Output is correct |
17 |
Correct |
21 ms |
18228 KB |
Output is correct |
18 |
Correct |
21 ms |
18252 KB |
Output is correct |
19 |
Correct |
21 ms |
18124 KB |
Output is correct |
20 |
Correct |
23 ms |
18124 KB |
Output is correct |
21 |
Correct |
23 ms |
18132 KB |
Output is correct |
22 |
Correct |
22 ms |
18056 KB |
Output is correct |
23 |
Correct |
23 ms |
18312 KB |
Output is correct |
24 |
Correct |
21 ms |
18132 KB |
Output is correct |
25 |
Correct |
24 ms |
18232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
15948 KB |
Output is correct |
2 |
Correct |
6 ms |
15948 KB |
Output is correct |
3 |
Correct |
6 ms |
15912 KB |
Output is correct |
4 |
Correct |
7 ms |
15948 KB |
Output is correct |
5 |
Correct |
6 ms |
15948 KB |
Output is correct |
6 |
Correct |
6 ms |
15948 KB |
Output is correct |
7 |
Correct |
6 ms |
15948 KB |
Output is correct |
8 |
Correct |
7 ms |
15948 KB |
Output is correct |
9 |
Correct |
7 ms |
15948 KB |
Output is correct |
10 |
Correct |
7 ms |
15948 KB |
Output is correct |
11 |
Correct |
6 ms |
15948 KB |
Output is correct |
12 |
Correct |
6 ms |
15936 KB |
Output is correct |
13 |
Correct |
6 ms |
15948 KB |
Output is correct |
14 |
Correct |
6 ms |
15948 KB |
Output is correct |
15 |
Correct |
6 ms |
15948 KB |
Output is correct |
16 |
Correct |
6 ms |
16004 KB |
Output is correct |
17 |
Correct |
7 ms |
15948 KB |
Output is correct |
18 |
Correct |
7 ms |
15948 KB |
Output is correct |
19 |
Correct |
7 ms |
15948 KB |
Output is correct |
20 |
Correct |
8 ms |
15928 KB |
Output is correct |
21 |
Correct |
7 ms |
15948 KB |
Output is correct |
22 |
Correct |
6 ms |
16016 KB |
Output is correct |
23 |
Correct |
7 ms |
15948 KB |
Output is correct |
24 |
Correct |
7 ms |
15948 KB |
Output is correct |
25 |
Correct |
7 ms |
15948 KB |
Output is correct |
26 |
Correct |
24 ms |
18252 KB |
Output is correct |
27 |
Correct |
25 ms |
18252 KB |
Output is correct |
28 |
Correct |
24 ms |
18124 KB |
Output is correct |
29 |
Correct |
24 ms |
18080 KB |
Output is correct |
30 |
Correct |
24 ms |
18080 KB |
Output is correct |
31 |
Correct |
37 ms |
21188 KB |
Output is correct |
32 |
Correct |
38 ms |
21444 KB |
Output is correct |
33 |
Correct |
44 ms |
21376 KB |
Output is correct |
34 |
Correct |
38 ms |
21444 KB |
Output is correct |
35 |
Correct |
37 ms |
21228 KB |
Output is correct |
36 |
Correct |
37 ms |
21180 KB |
Output is correct |
37 |
Correct |
37 ms |
21292 KB |
Output is correct |
38 |
Correct |
39 ms |
21360 KB |
Output is correct |
39 |
Correct |
38 ms |
21388 KB |
Output is correct |
40 |
Correct |
40 ms |
21580 KB |
Output is correct |
41 |
Correct |
23 ms |
18152 KB |
Output is correct |
42 |
Correct |
21 ms |
18228 KB |
Output is correct |
43 |
Correct |
21 ms |
18252 KB |
Output is correct |
44 |
Correct |
21 ms |
18124 KB |
Output is correct |
45 |
Correct |
23 ms |
18124 KB |
Output is correct |
46 |
Correct |
23 ms |
18132 KB |
Output is correct |
47 |
Correct |
22 ms |
18056 KB |
Output is correct |
48 |
Correct |
23 ms |
18312 KB |
Output is correct |
49 |
Correct |
21 ms |
18132 KB |
Output is correct |
50 |
Correct |
24 ms |
18232 KB |
Output is correct |
51 |
Correct |
47 ms |
21284 KB |
Output is correct |
52 |
Correct |
46 ms |
21072 KB |
Output is correct |
53 |
Correct |
46 ms |
21316 KB |
Output is correct |
54 |
Correct |
48 ms |
21060 KB |
Output is correct |
55 |
Correct |
49 ms |
21320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
15948 KB |
Output is correct |
2 |
Correct |
6 ms |
15948 KB |
Output is correct |
3 |
Correct |
6 ms |
15912 KB |
Output is correct |
4 |
Correct |
7 ms |
15948 KB |
Output is correct |
5 |
Correct |
6 ms |
15948 KB |
Output is correct |
6 |
Correct |
6 ms |
15948 KB |
Output is correct |
7 |
Correct |
6 ms |
15948 KB |
Output is correct |
8 |
Correct |
7 ms |
15948 KB |
Output is correct |
9 |
Correct |
7 ms |
15948 KB |
Output is correct |
10 |
Correct |
7 ms |
15948 KB |
Output is correct |
11 |
Correct |
6 ms |
15948 KB |
Output is correct |
12 |
Correct |
6 ms |
15936 KB |
Output is correct |
13 |
Correct |
6 ms |
15948 KB |
Output is correct |
14 |
Correct |
6 ms |
15948 KB |
Output is correct |
15 |
Correct |
6 ms |
15948 KB |
Output is correct |
16 |
Correct |
6 ms |
16004 KB |
Output is correct |
17 |
Correct |
7 ms |
15948 KB |
Output is correct |
18 |
Correct |
7 ms |
15948 KB |
Output is correct |
19 |
Correct |
7 ms |
15948 KB |
Output is correct |
20 |
Correct |
8 ms |
15928 KB |
Output is correct |
21 |
Correct |
7 ms |
15948 KB |
Output is correct |
22 |
Correct |
6 ms |
16016 KB |
Output is correct |
23 |
Correct |
7 ms |
15948 KB |
Output is correct |
24 |
Correct |
7 ms |
15948 KB |
Output is correct |
25 |
Correct |
7 ms |
15948 KB |
Output is correct |
26 |
Correct |
24 ms |
18252 KB |
Output is correct |
27 |
Correct |
25 ms |
18252 KB |
Output is correct |
28 |
Correct |
24 ms |
18124 KB |
Output is correct |
29 |
Correct |
24 ms |
18080 KB |
Output is correct |
30 |
Correct |
24 ms |
18080 KB |
Output is correct |
31 |
Correct |
37 ms |
21188 KB |
Output is correct |
32 |
Correct |
38 ms |
21444 KB |
Output is correct |
33 |
Correct |
44 ms |
21376 KB |
Output is correct |
34 |
Correct |
38 ms |
21444 KB |
Output is correct |
35 |
Correct |
37 ms |
21228 KB |
Output is correct |
36 |
Correct |
37 ms |
21180 KB |
Output is correct |
37 |
Correct |
37 ms |
21292 KB |
Output is correct |
38 |
Correct |
39 ms |
21360 KB |
Output is correct |
39 |
Correct |
38 ms |
21388 KB |
Output is correct |
40 |
Correct |
40 ms |
21580 KB |
Output is correct |
41 |
Correct |
23 ms |
18152 KB |
Output is correct |
42 |
Correct |
21 ms |
18228 KB |
Output is correct |
43 |
Correct |
21 ms |
18252 KB |
Output is correct |
44 |
Correct |
21 ms |
18124 KB |
Output is correct |
45 |
Correct |
23 ms |
18124 KB |
Output is correct |
46 |
Correct |
23 ms |
18132 KB |
Output is correct |
47 |
Correct |
22 ms |
18056 KB |
Output is correct |
48 |
Correct |
23 ms |
18312 KB |
Output is correct |
49 |
Correct |
21 ms |
18132 KB |
Output is correct |
50 |
Correct |
24 ms |
18232 KB |
Output is correct |
51 |
Correct |
47 ms |
21284 KB |
Output is correct |
52 |
Correct |
46 ms |
21072 KB |
Output is correct |
53 |
Correct |
46 ms |
21316 KB |
Output is correct |
54 |
Correct |
48 ms |
21060 KB |
Output is correct |
55 |
Correct |
49 ms |
21320 KB |
Output is correct |
56 |
Correct |
510 ms |
68824 KB |
Output is correct |
57 |
Correct |
512 ms |
68032 KB |
Output is correct |
58 |
Correct |
480 ms |
65596 KB |
Output is correct |
59 |
Correct |
487 ms |
66692 KB |
Output is correct |
60 |
Correct |
522 ms |
69956 KB |
Output is correct |