#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define MAXN (1000005)
ll parent[MAXN];
ll SIZE[MAXN];
long long find_set(ll a){
if(parent[a] == a) return a;
parent[a] = find_set(parent[a]);
return parent[a];
}
bool same_set(ll x,ll y){
return find_set(x) == find_set(y);
}
void merge_set(ll x,ll y){
parent[find_set(x)] = find_set(y);
}
int main() {
ios_base::sync_with_stdio(false);cin.tie(0);
ll N,Q;
cin>>N>>Q;
ll sum = 0;
ll arr[N];
for(ll i = 0;i < N;i++){
cin>>arr[i];
}
pair<ll,ll> planes[Q];
for(ll i = 0;i < Q;i++){
cin>>planes[i].first;
planes[i].second = i;
}
ll prefix[N + 1];
ll cur = 0;
for(ll i = 0;i <= N;i++){
cur += i;
prefix[i] = cur;
}
deque<pair<ll,ll> > dq;
for(ll i = 0;i < N;i++){
dq.push_back(make_pair(arr[i],i));
}
ll ans[Q];
ll can[N];
memset(can,0,sizeof(can));
memset(ans,0,sizeof(ans));
for(ll i = 0;i < N;i++){
parent[i] = i;
}
for(ll i = 0;i < N;i++){
SIZE[i] = 1;
}
sort(planes,planes + Q);
sort(dq.begin(),dq.end());
queue<ll> q;
for(ll i = 0;i < Q;i++){
while(dq.size() != 0 && dq.front().first <= planes[i].first){
ll a = dq.front().first;
ll index = dq.front().second;
can[index] = i + 1;
q.push(index);
dq.pop_front();
if(N == 1){
}else if(index == 0){
ll b = 0;
ll c = 0;
if(can[index + 1] >= 1){
b = find_set(index + 1);
sum -= prefix[SIZE[b]];
c += SIZE[b];
merge_set(index,index + 1);
b = find_set(index + 1);
SIZE[b] = c + 1;
sum += prefix[SIZE[b]];
}else{
sum++;
}
}else if(index == N - 1){
ll b = 0;
ll c = 0;
if(can[index - 1] >= 1){
b = find_set(index - 1);
sum -= prefix[SIZE[b]];
c += SIZE[b];
merge_set(index,index - 1);
b = find_set(index - 1);
SIZE[b] = c + 1;
sum += prefix[SIZE[b]];
}else{
sum++;
}
}else{
ll b = 0;
ll c = 0;
if(can[index - 1] >= 1){
b = find_set(index - 1);
sum -= prefix[SIZE[b]];
c += SIZE[b];
}
if(can[index + 1] >= 1){
b = find_set(index + 1);
sum -= prefix[SIZE[b]];
c += SIZE[b];
}
if(can[index - 1] >= 1) merge_set(index,index - 1);
if(can[index + 1] >= 1) merge_set(index,index + 1);
b = find_set(index);
SIZE[b] = c + 1;
sum += prefix[SIZE[b]];
}
}
ans[planes[i].second] = sum;
}
for(ll i = 0;i < Q;i++){
cout<<ans[i]<<'\n';
}
}
Compilation message
pilot.cpp: In function 'int main()':
pilot.cpp:57:7: warning: unused variable 'a' [-Wunused-variable]
57 | ll a = dq.front().first;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
316 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
316 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
316 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 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 |
1 ms |
324 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
316 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 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 |
1 ms |
324 KB |
Output is correct |
20 |
Correct |
1 ms |
332 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 |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
6220 KB |
Output is correct |
2 |
Correct |
24 ms |
6468 KB |
Output is correct |
3 |
Correct |
22 ms |
6028 KB |
Output is correct |
4 |
Correct |
24 ms |
5956 KB |
Output is correct |
5 |
Correct |
22 ms |
6008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
9852 KB |
Output is correct |
2 |
Correct |
46 ms |
9924 KB |
Output is correct |
3 |
Correct |
42 ms |
9820 KB |
Output is correct |
4 |
Correct |
39 ms |
9992 KB |
Output is correct |
5 |
Correct |
39 ms |
9668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
9876 KB |
Output is correct |
2 |
Correct |
39 ms |
9904 KB |
Output is correct |
3 |
Correct |
38 ms |
9828 KB |
Output is correct |
4 |
Correct |
43 ms |
10156 KB |
Output is correct |
5 |
Correct |
51 ms |
10296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
316 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
23 ms |
6220 KB |
Output is correct |
12 |
Correct |
24 ms |
6468 KB |
Output is correct |
13 |
Correct |
22 ms |
6028 KB |
Output is correct |
14 |
Correct |
24 ms |
5956 KB |
Output is correct |
15 |
Correct |
22 ms |
6008 KB |
Output is correct |
16 |
Correct |
24 ms |
5920 KB |
Output is correct |
17 |
Correct |
21 ms |
6340 KB |
Output is correct |
18 |
Correct |
21 ms |
6440 KB |
Output is correct |
19 |
Correct |
21 ms |
6000 KB |
Output is correct |
20 |
Correct |
24 ms |
6296 KB |
Output is correct |
21 |
Correct |
26 ms |
5908 KB |
Output is correct |
22 |
Correct |
22 ms |
6116 KB |
Output is correct |
23 |
Correct |
22 ms |
6404 KB |
Output is correct |
24 |
Correct |
21 ms |
6012 KB |
Output is correct |
25 |
Correct |
22 ms |
6232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
316 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 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 |
1 ms |
324 KB |
Output is correct |
20 |
Correct |
1 ms |
332 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 |
332 KB |
Output is correct |
26 |
Correct |
23 ms |
6220 KB |
Output is correct |
27 |
Correct |
24 ms |
6468 KB |
Output is correct |
28 |
Correct |
22 ms |
6028 KB |
Output is correct |
29 |
Correct |
24 ms |
5956 KB |
Output is correct |
30 |
Correct |
22 ms |
6008 KB |
Output is correct |
31 |
Correct |
42 ms |
9852 KB |
Output is correct |
32 |
Correct |
46 ms |
9924 KB |
Output is correct |
33 |
Correct |
42 ms |
9820 KB |
Output is correct |
34 |
Correct |
39 ms |
9992 KB |
Output is correct |
35 |
Correct |
39 ms |
9668 KB |
Output is correct |
36 |
Correct |
41 ms |
9876 KB |
Output is correct |
37 |
Correct |
39 ms |
9904 KB |
Output is correct |
38 |
Correct |
38 ms |
9828 KB |
Output is correct |
39 |
Correct |
43 ms |
10156 KB |
Output is correct |
40 |
Correct |
51 ms |
10296 KB |
Output is correct |
41 |
Correct |
24 ms |
5920 KB |
Output is correct |
42 |
Correct |
21 ms |
6340 KB |
Output is correct |
43 |
Correct |
21 ms |
6440 KB |
Output is correct |
44 |
Correct |
21 ms |
6000 KB |
Output is correct |
45 |
Correct |
24 ms |
6296 KB |
Output is correct |
46 |
Correct |
26 ms |
5908 KB |
Output is correct |
47 |
Correct |
22 ms |
6116 KB |
Output is correct |
48 |
Correct |
22 ms |
6404 KB |
Output is correct |
49 |
Correct |
21 ms |
6012 KB |
Output is correct |
50 |
Correct |
22 ms |
6232 KB |
Output is correct |
51 |
Correct |
56 ms |
9864 KB |
Output is correct |
52 |
Correct |
51 ms |
9796 KB |
Output is correct |
53 |
Correct |
47 ms |
9952 KB |
Output is correct |
54 |
Correct |
48 ms |
9736 KB |
Output is correct |
55 |
Correct |
49 ms |
9924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
316 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 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 |
1 ms |
324 KB |
Output is correct |
20 |
Correct |
1 ms |
332 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 |
332 KB |
Output is correct |
26 |
Correct |
23 ms |
6220 KB |
Output is correct |
27 |
Correct |
24 ms |
6468 KB |
Output is correct |
28 |
Correct |
22 ms |
6028 KB |
Output is correct |
29 |
Correct |
24 ms |
5956 KB |
Output is correct |
30 |
Correct |
22 ms |
6008 KB |
Output is correct |
31 |
Correct |
42 ms |
9852 KB |
Output is correct |
32 |
Correct |
46 ms |
9924 KB |
Output is correct |
33 |
Correct |
42 ms |
9820 KB |
Output is correct |
34 |
Correct |
39 ms |
9992 KB |
Output is correct |
35 |
Correct |
39 ms |
9668 KB |
Output is correct |
36 |
Correct |
41 ms |
9876 KB |
Output is correct |
37 |
Correct |
39 ms |
9904 KB |
Output is correct |
38 |
Correct |
38 ms |
9828 KB |
Output is correct |
39 |
Correct |
43 ms |
10156 KB |
Output is correct |
40 |
Correct |
51 ms |
10296 KB |
Output is correct |
41 |
Correct |
24 ms |
5920 KB |
Output is correct |
42 |
Correct |
21 ms |
6340 KB |
Output is correct |
43 |
Correct |
21 ms |
6440 KB |
Output is correct |
44 |
Correct |
21 ms |
6000 KB |
Output is correct |
45 |
Correct |
24 ms |
6296 KB |
Output is correct |
46 |
Correct |
26 ms |
5908 KB |
Output is correct |
47 |
Correct |
22 ms |
6116 KB |
Output is correct |
48 |
Correct |
22 ms |
6404 KB |
Output is correct |
49 |
Correct |
21 ms |
6012 KB |
Output is correct |
50 |
Correct |
22 ms |
6232 KB |
Output is correct |
51 |
Correct |
56 ms |
9864 KB |
Output is correct |
52 |
Correct |
51 ms |
9796 KB |
Output is correct |
53 |
Correct |
47 ms |
9952 KB |
Output is correct |
54 |
Correct |
48 ms |
9736 KB |
Output is correct |
55 |
Correct |
49 ms |
9924 KB |
Output is correct |
56 |
Correct |
602 ms |
96516 KB |
Output is correct |
57 |
Correct |
608 ms |
97428 KB |
Output is correct |
58 |
Correct |
558 ms |
91360 KB |
Output is correct |
59 |
Correct |
571 ms |
93596 KB |
Output is correct |
60 |
Correct |
599 ms |
99580 KB |
Output is correct |