#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 readll(){
bool minus=false;
ll result=0;
char ch;
ch=getchar();
while(true){
if(ch=='-')
break;
if(ch>='0' && ch<='9')
break;
ch=getchar();
}
if(ch=='-')
minus=true;
else
result = ch-'0';
while(true){
ch=getchar();
if(ch<'0' || ch>'9')
break;
result=result*10+(ch-'0');
}
if(minus)
return -result;
else
return result;
}
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(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
N=readll();
Q=readll();
for(ll i=0; i<N; i++){
H[i]=readll();
A[i]={H[i], i};
}
for(ll i=0; i<Q; i++){
Y[i].fi=readll();
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] << '\n';
}
}
Compilation message
pilot.cpp: In function 'long long int readll()':
pilot.cpp:39:5: warning: this 'else' clause does not guard... [-Wmisleading-indentation]
39 | else
| ^~~~
pilot.cpp:41:2: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'else'
41 | while(true){
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
15948 KB |
Output is correct |
2 |
Correct |
6 ms |
15908 KB |
Output is correct |
3 |
Correct |
6 ms |
15948 KB |
Output is correct |
4 |
Correct |
6 ms |
15948 KB |
Output is correct |
5 |
Correct |
6 ms |
15936 KB |
Output is correct |
6 |
Correct |
7 ms |
15948 KB |
Output is correct |
7 |
Correct |
6 ms |
15948 KB |
Output is correct |
8 |
Correct |
7 ms |
15968 KB |
Output is correct |
9 |
Correct |
6 ms |
15948 KB |
Output is correct |
10 |
Correct |
7 ms |
15948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
15948 KB |
Output is correct |
2 |
Correct |
6 ms |
15908 KB |
Output is correct |
3 |
Correct |
6 ms |
15948 KB |
Output is correct |
4 |
Correct |
6 ms |
15948 KB |
Output is correct |
5 |
Correct |
6 ms |
15936 KB |
Output is correct |
6 |
Correct |
7 ms |
15948 KB |
Output is correct |
7 |
Correct |
6 ms |
15948 KB |
Output is correct |
8 |
Correct |
7 ms |
15968 KB |
Output is correct |
9 |
Correct |
6 ms |
15948 KB |
Output is correct |
10 |
Correct |
7 ms |
15948 KB |
Output is correct |
11 |
Correct |
7 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 |
6 ms |
15948 KB |
Output is correct |
2 |
Correct |
6 ms |
15908 KB |
Output is correct |
3 |
Correct |
6 ms |
15948 KB |
Output is correct |
4 |
Correct |
6 ms |
15948 KB |
Output is correct |
5 |
Correct |
6 ms |
15936 KB |
Output is correct |
6 |
Correct |
7 ms |
15948 KB |
Output is correct |
7 |
Correct |
6 ms |
15948 KB |
Output is correct |
8 |
Correct |
7 ms |
15968 KB |
Output is correct |
9 |
Correct |
6 ms |
15948 KB |
Output is correct |
10 |
Correct |
7 ms |
15948 KB |
Output is correct |
11 |
Correct |
7 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 |
15908 KB |
Output is correct |
17 |
Correct |
6 ms |
15948 KB |
Output is correct |
18 |
Correct |
7 ms |
16076 KB |
Output is correct |
19 |
Correct |
7 ms |
15948 KB |
Output is correct |
20 |
Correct |
6 ms |
15900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
15948 KB |
Output is correct |
2 |
Correct |
6 ms |
15908 KB |
Output is correct |
3 |
Correct |
6 ms |
15948 KB |
Output is correct |
4 |
Correct |
6 ms |
15948 KB |
Output is correct |
5 |
Correct |
6 ms |
15936 KB |
Output is correct |
6 |
Correct |
7 ms |
15948 KB |
Output is correct |
7 |
Correct |
6 ms |
15948 KB |
Output is correct |
8 |
Correct |
7 ms |
15968 KB |
Output is correct |
9 |
Correct |
6 ms |
15948 KB |
Output is correct |
10 |
Correct |
7 ms |
15948 KB |
Output is correct |
11 |
Correct |
7 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 |
15908 KB |
Output is correct |
17 |
Correct |
6 ms |
15948 KB |
Output is correct |
18 |
Correct |
7 ms |
16076 KB |
Output is correct |
19 |
Correct |
7 ms |
15948 KB |
Output is correct |
20 |
Correct |
6 ms |
15900 KB |
Output is correct |
21 |
Correct |
6 ms |
15948 KB |
Output is correct |
22 |
Correct |
6 ms |
15948 KB |
Output is correct |
23 |
Correct |
6 ms |
15948 KB |
Output is correct |
24 |
Correct |
7 ms |
15948 KB |
Output is correct |
25 |
Correct |
7 ms |
15948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
18164 KB |
Output is correct |
2 |
Correct |
20 ms |
18252 KB |
Output is correct |
3 |
Correct |
19 ms |
18188 KB |
Output is correct |
4 |
Correct |
19 ms |
17976 KB |
Output is correct |
5 |
Correct |
20 ms |
18168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
21220 KB |
Output is correct |
2 |
Correct |
29 ms |
21420 KB |
Output is correct |
3 |
Correct |
29 ms |
21284 KB |
Output is correct |
4 |
Correct |
31 ms |
21372 KB |
Output is correct |
5 |
Correct |
29 ms |
21236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
21276 KB |
Output is correct |
2 |
Correct |
36 ms |
21272 KB |
Output is correct |
3 |
Correct |
34 ms |
21284 KB |
Output is correct |
4 |
Correct |
32 ms |
21408 KB |
Output is correct |
5 |
Correct |
31 ms |
21444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
15948 KB |
Output is correct |
2 |
Correct |
6 ms |
15908 KB |
Output is correct |
3 |
Correct |
6 ms |
15948 KB |
Output is correct |
4 |
Correct |
6 ms |
15948 KB |
Output is correct |
5 |
Correct |
6 ms |
15936 KB |
Output is correct |
6 |
Correct |
7 ms |
15948 KB |
Output is correct |
7 |
Correct |
6 ms |
15948 KB |
Output is correct |
8 |
Correct |
7 ms |
15968 KB |
Output is correct |
9 |
Correct |
6 ms |
15948 KB |
Output is correct |
10 |
Correct |
7 ms |
15948 KB |
Output is correct |
11 |
Correct |
21 ms |
18164 KB |
Output is correct |
12 |
Correct |
20 ms |
18252 KB |
Output is correct |
13 |
Correct |
19 ms |
18188 KB |
Output is correct |
14 |
Correct |
19 ms |
17976 KB |
Output is correct |
15 |
Correct |
20 ms |
18168 KB |
Output is correct |
16 |
Correct |
18 ms |
18160 KB |
Output is correct |
17 |
Correct |
19 ms |
18284 KB |
Output is correct |
18 |
Correct |
17 ms |
18252 KB |
Output is correct |
19 |
Correct |
17 ms |
18124 KB |
Output is correct |
20 |
Correct |
22 ms |
18244 KB |
Output is correct |
21 |
Correct |
20 ms |
18124 KB |
Output is correct |
22 |
Correct |
17 ms |
18124 KB |
Output is correct |
23 |
Correct |
18 ms |
18328 KB |
Output is correct |
24 |
Correct |
16 ms |
18124 KB |
Output is correct |
25 |
Correct |
18 ms |
18248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
15948 KB |
Output is correct |
2 |
Correct |
6 ms |
15908 KB |
Output is correct |
3 |
Correct |
6 ms |
15948 KB |
Output is correct |
4 |
Correct |
6 ms |
15948 KB |
Output is correct |
5 |
Correct |
6 ms |
15936 KB |
Output is correct |
6 |
Correct |
7 ms |
15948 KB |
Output is correct |
7 |
Correct |
6 ms |
15948 KB |
Output is correct |
8 |
Correct |
7 ms |
15968 KB |
Output is correct |
9 |
Correct |
6 ms |
15948 KB |
Output is correct |
10 |
Correct |
7 ms |
15948 KB |
Output is correct |
11 |
Correct |
7 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 |
15908 KB |
Output is correct |
17 |
Correct |
6 ms |
15948 KB |
Output is correct |
18 |
Correct |
7 ms |
16076 KB |
Output is correct |
19 |
Correct |
7 ms |
15948 KB |
Output is correct |
20 |
Correct |
6 ms |
15900 KB |
Output is correct |
21 |
Correct |
6 ms |
15948 KB |
Output is correct |
22 |
Correct |
6 ms |
15948 KB |
Output is correct |
23 |
Correct |
6 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 |
21 ms |
18164 KB |
Output is correct |
27 |
Correct |
20 ms |
18252 KB |
Output is correct |
28 |
Correct |
19 ms |
18188 KB |
Output is correct |
29 |
Correct |
19 ms |
17976 KB |
Output is correct |
30 |
Correct |
20 ms |
18168 KB |
Output is correct |
31 |
Correct |
30 ms |
21220 KB |
Output is correct |
32 |
Correct |
29 ms |
21420 KB |
Output is correct |
33 |
Correct |
29 ms |
21284 KB |
Output is correct |
34 |
Correct |
31 ms |
21372 KB |
Output is correct |
35 |
Correct |
29 ms |
21236 KB |
Output is correct |
36 |
Correct |
29 ms |
21276 KB |
Output is correct |
37 |
Correct |
36 ms |
21272 KB |
Output is correct |
38 |
Correct |
34 ms |
21284 KB |
Output is correct |
39 |
Correct |
32 ms |
21408 KB |
Output is correct |
40 |
Correct |
31 ms |
21444 KB |
Output is correct |
41 |
Correct |
18 ms |
18160 KB |
Output is correct |
42 |
Correct |
19 ms |
18284 KB |
Output is correct |
43 |
Correct |
17 ms |
18252 KB |
Output is correct |
44 |
Correct |
17 ms |
18124 KB |
Output is correct |
45 |
Correct |
22 ms |
18244 KB |
Output is correct |
46 |
Correct |
20 ms |
18124 KB |
Output is correct |
47 |
Correct |
17 ms |
18124 KB |
Output is correct |
48 |
Correct |
18 ms |
18328 KB |
Output is correct |
49 |
Correct |
16 ms |
18124 KB |
Output is correct |
50 |
Correct |
18 ms |
18248 KB |
Output is correct |
51 |
Correct |
39 ms |
21280 KB |
Output is correct |
52 |
Correct |
39 ms |
21004 KB |
Output is correct |
53 |
Correct |
42 ms |
21316 KB |
Output is correct |
54 |
Correct |
38 ms |
21056 KB |
Output is correct |
55 |
Correct |
39 ms |
21236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
15948 KB |
Output is correct |
2 |
Correct |
6 ms |
15908 KB |
Output is correct |
3 |
Correct |
6 ms |
15948 KB |
Output is correct |
4 |
Correct |
6 ms |
15948 KB |
Output is correct |
5 |
Correct |
6 ms |
15936 KB |
Output is correct |
6 |
Correct |
7 ms |
15948 KB |
Output is correct |
7 |
Correct |
6 ms |
15948 KB |
Output is correct |
8 |
Correct |
7 ms |
15968 KB |
Output is correct |
9 |
Correct |
6 ms |
15948 KB |
Output is correct |
10 |
Correct |
7 ms |
15948 KB |
Output is correct |
11 |
Correct |
7 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 |
15908 KB |
Output is correct |
17 |
Correct |
6 ms |
15948 KB |
Output is correct |
18 |
Correct |
7 ms |
16076 KB |
Output is correct |
19 |
Correct |
7 ms |
15948 KB |
Output is correct |
20 |
Correct |
6 ms |
15900 KB |
Output is correct |
21 |
Correct |
6 ms |
15948 KB |
Output is correct |
22 |
Correct |
6 ms |
15948 KB |
Output is correct |
23 |
Correct |
6 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 |
21 ms |
18164 KB |
Output is correct |
27 |
Correct |
20 ms |
18252 KB |
Output is correct |
28 |
Correct |
19 ms |
18188 KB |
Output is correct |
29 |
Correct |
19 ms |
17976 KB |
Output is correct |
30 |
Correct |
20 ms |
18168 KB |
Output is correct |
31 |
Correct |
30 ms |
21220 KB |
Output is correct |
32 |
Correct |
29 ms |
21420 KB |
Output is correct |
33 |
Correct |
29 ms |
21284 KB |
Output is correct |
34 |
Correct |
31 ms |
21372 KB |
Output is correct |
35 |
Correct |
29 ms |
21236 KB |
Output is correct |
36 |
Correct |
29 ms |
21276 KB |
Output is correct |
37 |
Correct |
36 ms |
21272 KB |
Output is correct |
38 |
Correct |
34 ms |
21284 KB |
Output is correct |
39 |
Correct |
32 ms |
21408 KB |
Output is correct |
40 |
Correct |
31 ms |
21444 KB |
Output is correct |
41 |
Correct |
18 ms |
18160 KB |
Output is correct |
42 |
Correct |
19 ms |
18284 KB |
Output is correct |
43 |
Correct |
17 ms |
18252 KB |
Output is correct |
44 |
Correct |
17 ms |
18124 KB |
Output is correct |
45 |
Correct |
22 ms |
18244 KB |
Output is correct |
46 |
Correct |
20 ms |
18124 KB |
Output is correct |
47 |
Correct |
17 ms |
18124 KB |
Output is correct |
48 |
Correct |
18 ms |
18328 KB |
Output is correct |
49 |
Correct |
16 ms |
18124 KB |
Output is correct |
50 |
Correct |
18 ms |
18248 KB |
Output is correct |
51 |
Correct |
39 ms |
21280 KB |
Output is correct |
52 |
Correct |
39 ms |
21004 KB |
Output is correct |
53 |
Correct |
42 ms |
21316 KB |
Output is correct |
54 |
Correct |
38 ms |
21056 KB |
Output is correct |
55 |
Correct |
39 ms |
21236 KB |
Output is correct |
56 |
Correct |
437 ms |
68848 KB |
Output is correct |
57 |
Correct |
431 ms |
68232 KB |
Output is correct |
58 |
Correct |
401 ms |
65528 KB |
Output is correct |
59 |
Correct |
410 ms |
66604 KB |
Output is correct |
60 |
Correct |
443 ms |
69956 KB |
Output is correct |