#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=1000001, MOD=1e9+7, BLOCK=330, INF=1e9+7;
const ld ERR=1e-7, pi=3.14159265358979323846;
ll N, Q, ptr=0;
vector<ll> H, ans;
ll tot=0;
vector<pii> A, Y;
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;
scanf("%lld", &N);
scanf("%lld", &Q);
H.resize(N);
A.resize(N);
Y.resize(Q);
ans.resize(Q);
for(ll i=0; i<N; i++){
scanf("%lld", &H[i]);
A[i]={H[i], i};
}
for(ll i=0; i<Q; i++){
scanf("%lld", &Y[i].fi);
Y[i].se=i;
}
sort(Y.begin(), Y.end());
sort(A.begin(), A.end());
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++){
printf("%lld\n", ans[i]);
}
}
Compilation message
pilot.cpp: In function 'int main()':
pilot.cpp:58:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
58 | scanf("%lld", &N);
| ~~~~~^~~~~~~~~~~~
pilot.cpp:59:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
59 | scanf("%lld", &Q);
| ~~~~~^~~~~~~~~~~~
pilot.cpp:65:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
65 | scanf("%lld", &H[i]);
| ~~~~~^~~~~~~~~~~~~~~
pilot.cpp:69:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
69 | scanf("%lld", &Y[i].fi);
| ~~~~~^~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
15948 KB |
Output is correct |
2 |
Correct |
6 ms |
15948 KB |
Output is correct |
3 |
Correct |
6 ms |
15948 KB |
Output is correct |
4 |
Correct |
7 ms |
15888 KB |
Output is correct |
5 |
Correct |
9 ms |
15968 KB |
Output is correct |
6 |
Correct |
6 ms |
15948 KB |
Output is correct |
7 |
Correct |
7 ms |
15948 KB |
Output is correct |
8 |
Correct |
7 ms |
15948 KB |
Output is correct |
9 |
Correct |
6 ms |
15948 KB |
Output is correct |
10 |
Correct |
6 ms |
15976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
15948 KB |
Output is correct |
2 |
Correct |
6 ms |
15948 KB |
Output is correct |
3 |
Correct |
6 ms |
15948 KB |
Output is correct |
4 |
Correct |
7 ms |
15888 KB |
Output is correct |
5 |
Correct |
9 ms |
15968 KB |
Output is correct |
6 |
Correct |
6 ms |
15948 KB |
Output is correct |
7 |
Correct |
7 ms |
15948 KB |
Output is correct |
8 |
Correct |
7 ms |
15948 KB |
Output is correct |
9 |
Correct |
6 ms |
15948 KB |
Output is correct |
10 |
Correct |
6 ms |
15976 KB |
Output is correct |
11 |
Correct |
6 ms |
15948 KB |
Output is correct |
12 |
Correct |
6 ms |
15948 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
15948 KB |
Output is correct |
2 |
Correct |
6 ms |
15948 KB |
Output is correct |
3 |
Correct |
6 ms |
15948 KB |
Output is correct |
4 |
Correct |
7 ms |
15888 KB |
Output is correct |
5 |
Correct |
9 ms |
15968 KB |
Output is correct |
6 |
Correct |
6 ms |
15948 KB |
Output is correct |
7 |
Correct |
7 ms |
15948 KB |
Output is correct |
8 |
Correct |
7 ms |
15948 KB |
Output is correct |
9 |
Correct |
6 ms |
15948 KB |
Output is correct |
10 |
Correct |
6 ms |
15976 KB |
Output is correct |
11 |
Correct |
6 ms |
15948 KB |
Output is correct |
12 |
Correct |
6 ms |
15948 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 |
15948 KB |
Output is correct |
17 |
Correct |
6 ms |
15948 KB |
Output is correct |
18 |
Correct |
6 ms |
15980 KB |
Output is correct |
19 |
Correct |
6 ms |
15980 KB |
Output is correct |
20 |
Correct |
6 ms |
15948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
15948 KB |
Output is correct |
2 |
Correct |
6 ms |
15948 KB |
Output is correct |
3 |
Correct |
6 ms |
15948 KB |
Output is correct |
4 |
Correct |
7 ms |
15888 KB |
Output is correct |
5 |
Correct |
9 ms |
15968 KB |
Output is correct |
6 |
Correct |
6 ms |
15948 KB |
Output is correct |
7 |
Correct |
7 ms |
15948 KB |
Output is correct |
8 |
Correct |
7 ms |
15948 KB |
Output is correct |
9 |
Correct |
6 ms |
15948 KB |
Output is correct |
10 |
Correct |
6 ms |
15976 KB |
Output is correct |
11 |
Correct |
6 ms |
15948 KB |
Output is correct |
12 |
Correct |
6 ms |
15948 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 |
15948 KB |
Output is correct |
17 |
Correct |
6 ms |
15948 KB |
Output is correct |
18 |
Correct |
6 ms |
15980 KB |
Output is correct |
19 |
Correct |
6 ms |
15980 KB |
Output is correct |
20 |
Correct |
6 ms |
15948 KB |
Output is correct |
21 |
Correct |
7 ms |
15948 KB |
Output is correct |
22 |
Correct |
6 ms |
15948 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 |
15944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
18256 KB |
Output is correct |
2 |
Correct |
26 ms |
18292 KB |
Output is correct |
3 |
Correct |
26 ms |
18156 KB |
Output is correct |
4 |
Correct |
25 ms |
18092 KB |
Output is correct |
5 |
Correct |
26 ms |
18140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
21164 KB |
Output is correct |
2 |
Correct |
47 ms |
21316 KB |
Output is correct |
3 |
Correct |
44 ms |
21316 KB |
Output is correct |
4 |
Correct |
43 ms |
21452 KB |
Output is correct |
5 |
Correct |
43 ms |
21216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
21188 KB |
Output is correct |
2 |
Correct |
42 ms |
21248 KB |
Output is correct |
3 |
Correct |
49 ms |
21276 KB |
Output is correct |
4 |
Correct |
44 ms |
21316 KB |
Output is correct |
5 |
Correct |
46 ms |
21448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
15948 KB |
Output is correct |
2 |
Correct |
6 ms |
15948 KB |
Output is correct |
3 |
Correct |
6 ms |
15948 KB |
Output is correct |
4 |
Correct |
7 ms |
15888 KB |
Output is correct |
5 |
Correct |
9 ms |
15968 KB |
Output is correct |
6 |
Correct |
6 ms |
15948 KB |
Output is correct |
7 |
Correct |
7 ms |
15948 KB |
Output is correct |
8 |
Correct |
7 ms |
15948 KB |
Output is correct |
9 |
Correct |
6 ms |
15948 KB |
Output is correct |
10 |
Correct |
6 ms |
15976 KB |
Output is correct |
11 |
Correct |
26 ms |
18256 KB |
Output is correct |
12 |
Correct |
26 ms |
18292 KB |
Output is correct |
13 |
Correct |
26 ms |
18156 KB |
Output is correct |
14 |
Correct |
25 ms |
18092 KB |
Output is correct |
15 |
Correct |
26 ms |
18140 KB |
Output is correct |
16 |
Correct |
25 ms |
18148 KB |
Output is correct |
17 |
Correct |
24 ms |
18284 KB |
Output is correct |
18 |
Correct |
23 ms |
18380 KB |
Output is correct |
19 |
Correct |
24 ms |
18124 KB |
Output is correct |
20 |
Correct |
24 ms |
18200 KB |
Output is correct |
21 |
Correct |
26 ms |
18120 KB |
Output is correct |
22 |
Correct |
23 ms |
18156 KB |
Output is correct |
23 |
Correct |
24 ms |
18204 KB |
Output is correct |
24 |
Correct |
24 ms |
18128 KB |
Output is correct |
25 |
Correct |
24 ms |
18124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
15948 KB |
Output is correct |
2 |
Correct |
6 ms |
15948 KB |
Output is correct |
3 |
Correct |
6 ms |
15948 KB |
Output is correct |
4 |
Correct |
7 ms |
15888 KB |
Output is correct |
5 |
Correct |
9 ms |
15968 KB |
Output is correct |
6 |
Correct |
6 ms |
15948 KB |
Output is correct |
7 |
Correct |
7 ms |
15948 KB |
Output is correct |
8 |
Correct |
7 ms |
15948 KB |
Output is correct |
9 |
Correct |
6 ms |
15948 KB |
Output is correct |
10 |
Correct |
6 ms |
15976 KB |
Output is correct |
11 |
Correct |
6 ms |
15948 KB |
Output is correct |
12 |
Correct |
6 ms |
15948 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 |
15948 KB |
Output is correct |
17 |
Correct |
6 ms |
15948 KB |
Output is correct |
18 |
Correct |
6 ms |
15980 KB |
Output is correct |
19 |
Correct |
6 ms |
15980 KB |
Output is correct |
20 |
Correct |
6 ms |
15948 KB |
Output is correct |
21 |
Correct |
7 ms |
15948 KB |
Output is correct |
22 |
Correct |
6 ms |
15948 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 |
15944 KB |
Output is correct |
26 |
Correct |
26 ms |
18256 KB |
Output is correct |
27 |
Correct |
26 ms |
18292 KB |
Output is correct |
28 |
Correct |
26 ms |
18156 KB |
Output is correct |
29 |
Correct |
25 ms |
18092 KB |
Output is correct |
30 |
Correct |
26 ms |
18140 KB |
Output is correct |
31 |
Correct |
42 ms |
21164 KB |
Output is correct |
32 |
Correct |
47 ms |
21316 KB |
Output is correct |
33 |
Correct |
44 ms |
21316 KB |
Output is correct |
34 |
Correct |
43 ms |
21452 KB |
Output is correct |
35 |
Correct |
43 ms |
21216 KB |
Output is correct |
36 |
Correct |
42 ms |
21188 KB |
Output is correct |
37 |
Correct |
42 ms |
21248 KB |
Output is correct |
38 |
Correct |
49 ms |
21276 KB |
Output is correct |
39 |
Correct |
44 ms |
21316 KB |
Output is correct |
40 |
Correct |
46 ms |
21448 KB |
Output is correct |
41 |
Correct |
25 ms |
18148 KB |
Output is correct |
42 |
Correct |
24 ms |
18284 KB |
Output is correct |
43 |
Correct |
23 ms |
18380 KB |
Output is correct |
44 |
Correct |
24 ms |
18124 KB |
Output is correct |
45 |
Correct |
24 ms |
18200 KB |
Output is correct |
46 |
Correct |
26 ms |
18120 KB |
Output is correct |
47 |
Correct |
23 ms |
18156 KB |
Output is correct |
48 |
Correct |
24 ms |
18204 KB |
Output is correct |
49 |
Correct |
24 ms |
18128 KB |
Output is correct |
50 |
Correct |
24 ms |
18124 KB |
Output is correct |
51 |
Correct |
53 ms |
21248 KB |
Output is correct |
52 |
Correct |
52 ms |
21016 KB |
Output is correct |
53 |
Correct |
52 ms |
21188 KB |
Output is correct |
54 |
Correct |
51 ms |
20952 KB |
Output is correct |
55 |
Correct |
53 ms |
21256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
15948 KB |
Output is correct |
2 |
Correct |
6 ms |
15948 KB |
Output is correct |
3 |
Correct |
6 ms |
15948 KB |
Output is correct |
4 |
Correct |
7 ms |
15888 KB |
Output is correct |
5 |
Correct |
9 ms |
15968 KB |
Output is correct |
6 |
Correct |
6 ms |
15948 KB |
Output is correct |
7 |
Correct |
7 ms |
15948 KB |
Output is correct |
8 |
Correct |
7 ms |
15948 KB |
Output is correct |
9 |
Correct |
6 ms |
15948 KB |
Output is correct |
10 |
Correct |
6 ms |
15976 KB |
Output is correct |
11 |
Correct |
6 ms |
15948 KB |
Output is correct |
12 |
Correct |
6 ms |
15948 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 |
15948 KB |
Output is correct |
17 |
Correct |
6 ms |
15948 KB |
Output is correct |
18 |
Correct |
6 ms |
15980 KB |
Output is correct |
19 |
Correct |
6 ms |
15980 KB |
Output is correct |
20 |
Correct |
6 ms |
15948 KB |
Output is correct |
21 |
Correct |
7 ms |
15948 KB |
Output is correct |
22 |
Correct |
6 ms |
15948 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 |
15944 KB |
Output is correct |
26 |
Correct |
26 ms |
18256 KB |
Output is correct |
27 |
Correct |
26 ms |
18292 KB |
Output is correct |
28 |
Correct |
26 ms |
18156 KB |
Output is correct |
29 |
Correct |
25 ms |
18092 KB |
Output is correct |
30 |
Correct |
26 ms |
18140 KB |
Output is correct |
31 |
Correct |
42 ms |
21164 KB |
Output is correct |
32 |
Correct |
47 ms |
21316 KB |
Output is correct |
33 |
Correct |
44 ms |
21316 KB |
Output is correct |
34 |
Correct |
43 ms |
21452 KB |
Output is correct |
35 |
Correct |
43 ms |
21216 KB |
Output is correct |
36 |
Correct |
42 ms |
21188 KB |
Output is correct |
37 |
Correct |
42 ms |
21248 KB |
Output is correct |
38 |
Correct |
49 ms |
21276 KB |
Output is correct |
39 |
Correct |
44 ms |
21316 KB |
Output is correct |
40 |
Correct |
46 ms |
21448 KB |
Output is correct |
41 |
Correct |
25 ms |
18148 KB |
Output is correct |
42 |
Correct |
24 ms |
18284 KB |
Output is correct |
43 |
Correct |
23 ms |
18380 KB |
Output is correct |
44 |
Correct |
24 ms |
18124 KB |
Output is correct |
45 |
Correct |
24 ms |
18200 KB |
Output is correct |
46 |
Correct |
26 ms |
18120 KB |
Output is correct |
47 |
Correct |
23 ms |
18156 KB |
Output is correct |
48 |
Correct |
24 ms |
18204 KB |
Output is correct |
49 |
Correct |
24 ms |
18128 KB |
Output is correct |
50 |
Correct |
24 ms |
18124 KB |
Output is correct |
51 |
Correct |
53 ms |
21248 KB |
Output is correct |
52 |
Correct |
52 ms |
21016 KB |
Output is correct |
53 |
Correct |
52 ms |
21188 KB |
Output is correct |
54 |
Correct |
51 ms |
20952 KB |
Output is correct |
55 |
Correct |
53 ms |
21256 KB |
Output is correct |
56 |
Correct |
565 ms |
68812 KB |
Output is correct |
57 |
Correct |
579 ms |
68144 KB |
Output is correct |
58 |
Correct |
534 ms |
65420 KB |
Output is correct |
59 |
Correct |
543 ms |
66684 KB |
Output is correct |
60 |
Correct |
579 ms |
70004 KB |
Output is correct |