#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 par[MX], sz[MX];
ll calc(ll x){
return x*(1ll+x)/2ll;
}
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]);
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> N >> Q;
for(ll i=0; i<N; i++){
par[i]=i;
sz[i]=1;
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)
join(idx-1, idx);
if(idx+1<N && H[idx+1]<=h)
join(idx+1, idx);
}
while(ptr<Q)
ans[Y[ptr++].se]=tot;
for(ll i=0; i<Q; i++){
cout << ans[i] << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
0 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
0 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
320 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
4696 KB |
Output is correct |
2 |
Correct |
20 ms |
4860 KB |
Output is correct |
3 |
Correct |
20 ms |
4560 KB |
Output is correct |
4 |
Correct |
18 ms |
4428 KB |
Output is correct |
5 |
Correct |
20 ms |
4428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
7996 KB |
Output is correct |
2 |
Correct |
35 ms |
8096 KB |
Output is correct |
3 |
Correct |
35 ms |
8260 KB |
Output is correct |
4 |
Correct |
36 ms |
8132 KB |
Output is correct |
5 |
Correct |
33 ms |
8004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
7936 KB |
Output is correct |
2 |
Correct |
34 ms |
8004 KB |
Output is correct |
3 |
Correct |
37 ms |
7944 KB |
Output is correct |
4 |
Correct |
38 ms |
8176 KB |
Output is correct |
5 |
Correct |
35 ms |
8268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
19 ms |
4696 KB |
Output is correct |
12 |
Correct |
20 ms |
4860 KB |
Output is correct |
13 |
Correct |
20 ms |
4560 KB |
Output is correct |
14 |
Correct |
18 ms |
4428 KB |
Output is correct |
15 |
Correct |
20 ms |
4428 KB |
Output is correct |
16 |
Correct |
17 ms |
4496 KB |
Output is correct |
17 |
Correct |
18 ms |
4812 KB |
Output is correct |
18 |
Correct |
17 ms |
4908 KB |
Output is correct |
19 |
Correct |
15 ms |
4360 KB |
Output is correct |
20 |
Correct |
18 ms |
4768 KB |
Output is correct |
21 |
Correct |
19 ms |
4428 KB |
Output is correct |
22 |
Correct |
17 ms |
4556 KB |
Output is correct |
23 |
Correct |
17 ms |
4812 KB |
Output is correct |
24 |
Correct |
17 ms |
4540 KB |
Output is correct |
25 |
Correct |
18 ms |
4684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
0 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
320 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
328 KB |
Output is correct |
26 |
Correct |
19 ms |
4696 KB |
Output is correct |
27 |
Correct |
20 ms |
4860 KB |
Output is correct |
28 |
Correct |
20 ms |
4560 KB |
Output is correct |
29 |
Correct |
18 ms |
4428 KB |
Output is correct |
30 |
Correct |
20 ms |
4428 KB |
Output is correct |
31 |
Correct |
42 ms |
7996 KB |
Output is correct |
32 |
Correct |
35 ms |
8096 KB |
Output is correct |
33 |
Correct |
35 ms |
8260 KB |
Output is correct |
34 |
Correct |
36 ms |
8132 KB |
Output is correct |
35 |
Correct |
33 ms |
8004 KB |
Output is correct |
36 |
Correct |
34 ms |
7936 KB |
Output is correct |
37 |
Correct |
34 ms |
8004 KB |
Output is correct |
38 |
Correct |
37 ms |
7944 KB |
Output is correct |
39 |
Correct |
38 ms |
8176 KB |
Output is correct |
40 |
Correct |
35 ms |
8268 KB |
Output is correct |
41 |
Correct |
17 ms |
4496 KB |
Output is correct |
42 |
Correct |
18 ms |
4812 KB |
Output is correct |
43 |
Correct |
17 ms |
4908 KB |
Output is correct |
44 |
Correct |
15 ms |
4360 KB |
Output is correct |
45 |
Correct |
18 ms |
4768 KB |
Output is correct |
46 |
Correct |
19 ms |
4428 KB |
Output is correct |
47 |
Correct |
17 ms |
4556 KB |
Output is correct |
48 |
Correct |
17 ms |
4812 KB |
Output is correct |
49 |
Correct |
17 ms |
4540 KB |
Output is correct |
50 |
Correct |
18 ms |
4684 KB |
Output is correct |
51 |
Correct |
45 ms |
7976 KB |
Output is correct |
52 |
Correct |
56 ms |
7824 KB |
Output is correct |
53 |
Correct |
41 ms |
8004 KB |
Output is correct |
54 |
Correct |
41 ms |
7796 KB |
Output is correct |
55 |
Correct |
41 ms |
7992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
0 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
320 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
328 KB |
Output is correct |
26 |
Correct |
19 ms |
4696 KB |
Output is correct |
27 |
Correct |
20 ms |
4860 KB |
Output is correct |
28 |
Correct |
20 ms |
4560 KB |
Output is correct |
29 |
Correct |
18 ms |
4428 KB |
Output is correct |
30 |
Correct |
20 ms |
4428 KB |
Output is correct |
31 |
Correct |
42 ms |
7996 KB |
Output is correct |
32 |
Correct |
35 ms |
8096 KB |
Output is correct |
33 |
Correct |
35 ms |
8260 KB |
Output is correct |
34 |
Correct |
36 ms |
8132 KB |
Output is correct |
35 |
Correct |
33 ms |
8004 KB |
Output is correct |
36 |
Correct |
34 ms |
7936 KB |
Output is correct |
37 |
Correct |
34 ms |
8004 KB |
Output is correct |
38 |
Correct |
37 ms |
7944 KB |
Output is correct |
39 |
Correct |
38 ms |
8176 KB |
Output is correct |
40 |
Correct |
35 ms |
8268 KB |
Output is correct |
41 |
Correct |
17 ms |
4496 KB |
Output is correct |
42 |
Correct |
18 ms |
4812 KB |
Output is correct |
43 |
Correct |
17 ms |
4908 KB |
Output is correct |
44 |
Correct |
15 ms |
4360 KB |
Output is correct |
45 |
Correct |
18 ms |
4768 KB |
Output is correct |
46 |
Correct |
19 ms |
4428 KB |
Output is correct |
47 |
Correct |
17 ms |
4556 KB |
Output is correct |
48 |
Correct |
17 ms |
4812 KB |
Output is correct |
49 |
Correct |
17 ms |
4540 KB |
Output is correct |
50 |
Correct |
18 ms |
4684 KB |
Output is correct |
51 |
Correct |
45 ms |
7976 KB |
Output is correct |
52 |
Correct |
56 ms |
7824 KB |
Output is correct |
53 |
Correct |
41 ms |
8004 KB |
Output is correct |
54 |
Correct |
41 ms |
7796 KB |
Output is correct |
55 |
Correct |
41 ms |
7992 KB |
Output is correct |
56 |
Correct |
571 ms |
69664 KB |
Output is correct |
57 |
Correct |
524 ms |
69720 KB |
Output is correct |
58 |
Correct |
488 ms |
65800 KB |
Output is correct |
59 |
Correct |
495 ms |
67260 KB |
Output is correct |
60 |
Correct |
529 ms |
71596 KB |
Output is correct |