#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
using namespace std;
const int N=1e6+5;
int n,q;
ll ans,qs[N],ansarr[N];
int arr[N],qst[N],dp[N];
set<int>st;
priority_queue<pii,vector<pii>,greater<pii>>pq;
int main(){
ios_base::sync_with_stdio(0),cin.tie(0);
cin >> n >> q;
for(int i=1; i<=n; i++){
cin >> arr[i];
qs[i]=qs[i-1]+i;
pq.push({arr[i],i});
}
for(int i=1; i<=q; i++){
cin >> qst[i];
st.insert(qst[i]);
}
for(auto it=st.begin(); it!=st.end(); it++){
while(!pq.empty()&&pq.top().first<=*it){
int h=pq.top().first;
int idx=pq.top().second;
pq.pop();
dp[idx]=idx;
int l=idx,r=idx;
if(dp[idx-1]){
ans-=qs[(idx-1)-dp[idx-1]+1];
int tmp=dp[idx-1];
dp[dp[idx-1]]=idx;
dp[idx]=tmp;
l=tmp;
}
if(dp[idx+1]){
ans-=qs[dp[idx+1]-(idx+1)+1];
int tmp=dp[idx+1];
dp[dp[idx+1]]=dp[idx];
dp[dp[idx]]=tmp;
r=dp[l];
}
ans+=qs[r-l+1];
}
ansarr[*it]=ans;
}
for(int i=1; i<=q; i++){
cout << ansarr[qst[i]] << '\n';
}
return 0;
}
Compilation message
pilot.cpp: In function 'int main()':
pilot.cpp:27:17: warning: unused variable 'h' [-Wunused-variable]
27 | int h=pq.top().first;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
2 ms |
12636 KB |
Output is correct |
12 |
Correct |
2 ms |
12636 KB |
Output is correct |
13 |
Correct |
1 ms |
12888 KB |
Output is correct |
14 |
Correct |
1 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
12636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
2 ms |
12636 KB |
Output is correct |
12 |
Correct |
2 ms |
12636 KB |
Output is correct |
13 |
Correct |
1 ms |
12888 KB |
Output is correct |
14 |
Correct |
1 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
12636 KB |
Output is correct |
16 |
Correct |
2 ms |
12788 KB |
Output is correct |
17 |
Correct |
2 ms |
12636 KB |
Output is correct |
18 |
Correct |
2 ms |
12636 KB |
Output is correct |
19 |
Correct |
2 ms |
12636 KB |
Output is correct |
20 |
Correct |
2 ms |
12632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
2 ms |
12636 KB |
Output is correct |
12 |
Correct |
2 ms |
12636 KB |
Output is correct |
13 |
Correct |
1 ms |
12888 KB |
Output is correct |
14 |
Correct |
1 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
12636 KB |
Output is correct |
16 |
Correct |
2 ms |
12788 KB |
Output is correct |
17 |
Correct |
2 ms |
12636 KB |
Output is correct |
18 |
Correct |
2 ms |
12636 KB |
Output is correct |
19 |
Correct |
2 ms |
12636 KB |
Output is correct |
20 |
Correct |
2 ms |
12632 KB |
Output is correct |
21 |
Correct |
2 ms |
12888 KB |
Output is correct |
22 |
Correct |
2 ms |
13148 KB |
Output is correct |
23 |
Correct |
2 ms |
12892 KB |
Output is correct |
24 |
Correct |
2 ms |
13148 KB |
Output is correct |
25 |
Correct |
2 ms |
12892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
8404 KB |
Output is correct |
2 |
Correct |
20 ms |
8408 KB |
Output is correct |
3 |
Correct |
18 ms |
8408 KB |
Output is correct |
4 |
Correct |
18 ms |
8152 KB |
Output is correct |
5 |
Correct |
19 ms |
8152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
20176 KB |
Output is correct |
2 |
Correct |
53 ms |
22224 KB |
Output is correct |
3 |
Correct |
48 ms |
22208 KB |
Output is correct |
4 |
Correct |
47 ms |
22236 KB |
Output is correct |
5 |
Correct |
48 ms |
22224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
20168 KB |
Output is correct |
2 |
Correct |
49 ms |
22676 KB |
Output is correct |
3 |
Correct |
50 ms |
22476 KB |
Output is correct |
4 |
Correct |
52 ms |
20360 KB |
Output is correct |
5 |
Correct |
52 ms |
22928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
19 ms |
8404 KB |
Output is correct |
12 |
Correct |
20 ms |
8408 KB |
Output is correct |
13 |
Correct |
18 ms |
8408 KB |
Output is correct |
14 |
Correct |
18 ms |
8152 KB |
Output is correct |
15 |
Correct |
19 ms |
8152 KB |
Output is correct |
16 |
Correct |
16 ms |
10196 KB |
Output is correct |
17 |
Correct |
13 ms |
10456 KB |
Output is correct |
18 |
Correct |
11 ms |
10456 KB |
Output is correct |
19 |
Correct |
9 ms |
10284 KB |
Output is correct |
20 |
Correct |
15 ms |
10452 KB |
Output is correct |
21 |
Correct |
19 ms |
10416 KB |
Output is correct |
22 |
Correct |
15 ms |
10200 KB |
Output is correct |
23 |
Correct |
12 ms |
10452 KB |
Output is correct |
24 |
Correct |
11 ms |
10200 KB |
Output is correct |
25 |
Correct |
15 ms |
10456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
2 ms |
12636 KB |
Output is correct |
12 |
Correct |
2 ms |
12636 KB |
Output is correct |
13 |
Correct |
1 ms |
12888 KB |
Output is correct |
14 |
Correct |
1 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
12636 KB |
Output is correct |
16 |
Correct |
2 ms |
12788 KB |
Output is correct |
17 |
Correct |
2 ms |
12636 KB |
Output is correct |
18 |
Correct |
2 ms |
12636 KB |
Output is correct |
19 |
Correct |
2 ms |
12636 KB |
Output is correct |
20 |
Correct |
2 ms |
12632 KB |
Output is correct |
21 |
Correct |
2 ms |
12888 KB |
Output is correct |
22 |
Correct |
2 ms |
13148 KB |
Output is correct |
23 |
Correct |
2 ms |
12892 KB |
Output is correct |
24 |
Correct |
2 ms |
13148 KB |
Output is correct |
25 |
Correct |
2 ms |
12892 KB |
Output is correct |
26 |
Correct |
19 ms |
8404 KB |
Output is correct |
27 |
Correct |
20 ms |
8408 KB |
Output is correct |
28 |
Correct |
18 ms |
8408 KB |
Output is correct |
29 |
Correct |
18 ms |
8152 KB |
Output is correct |
30 |
Correct |
19 ms |
8152 KB |
Output is correct |
31 |
Correct |
58 ms |
20176 KB |
Output is correct |
32 |
Correct |
53 ms |
22224 KB |
Output is correct |
33 |
Correct |
48 ms |
22208 KB |
Output is correct |
34 |
Correct |
47 ms |
22236 KB |
Output is correct |
35 |
Correct |
48 ms |
22224 KB |
Output is correct |
36 |
Correct |
67 ms |
20168 KB |
Output is correct |
37 |
Correct |
49 ms |
22676 KB |
Output is correct |
38 |
Correct |
50 ms |
22476 KB |
Output is correct |
39 |
Correct |
52 ms |
20360 KB |
Output is correct |
40 |
Correct |
52 ms |
22928 KB |
Output is correct |
41 |
Correct |
16 ms |
10196 KB |
Output is correct |
42 |
Correct |
13 ms |
10456 KB |
Output is correct |
43 |
Correct |
11 ms |
10456 KB |
Output is correct |
44 |
Correct |
9 ms |
10284 KB |
Output is correct |
45 |
Correct |
15 ms |
10452 KB |
Output is correct |
46 |
Correct |
19 ms |
10416 KB |
Output is correct |
47 |
Correct |
15 ms |
10200 KB |
Output is correct |
48 |
Correct |
12 ms |
10452 KB |
Output is correct |
49 |
Correct |
11 ms |
10200 KB |
Output is correct |
50 |
Correct |
15 ms |
10456 KB |
Output is correct |
51 |
Correct |
56 ms |
23868 KB |
Output is correct |
52 |
Correct |
65 ms |
24184 KB |
Output is correct |
53 |
Correct |
53 ms |
24056 KB |
Output is correct |
54 |
Correct |
55 ms |
24012 KB |
Output is correct |
55 |
Correct |
53 ms |
23856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
2 ms |
12636 KB |
Output is correct |
12 |
Correct |
2 ms |
12636 KB |
Output is correct |
13 |
Correct |
1 ms |
12888 KB |
Output is correct |
14 |
Correct |
1 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
12636 KB |
Output is correct |
16 |
Correct |
2 ms |
12788 KB |
Output is correct |
17 |
Correct |
2 ms |
12636 KB |
Output is correct |
18 |
Correct |
2 ms |
12636 KB |
Output is correct |
19 |
Correct |
2 ms |
12636 KB |
Output is correct |
20 |
Correct |
2 ms |
12632 KB |
Output is correct |
21 |
Correct |
2 ms |
12888 KB |
Output is correct |
22 |
Correct |
2 ms |
13148 KB |
Output is correct |
23 |
Correct |
2 ms |
12892 KB |
Output is correct |
24 |
Correct |
2 ms |
13148 KB |
Output is correct |
25 |
Correct |
2 ms |
12892 KB |
Output is correct |
26 |
Correct |
19 ms |
8404 KB |
Output is correct |
27 |
Correct |
20 ms |
8408 KB |
Output is correct |
28 |
Correct |
18 ms |
8408 KB |
Output is correct |
29 |
Correct |
18 ms |
8152 KB |
Output is correct |
30 |
Correct |
19 ms |
8152 KB |
Output is correct |
31 |
Correct |
58 ms |
20176 KB |
Output is correct |
32 |
Correct |
53 ms |
22224 KB |
Output is correct |
33 |
Correct |
48 ms |
22208 KB |
Output is correct |
34 |
Correct |
47 ms |
22236 KB |
Output is correct |
35 |
Correct |
48 ms |
22224 KB |
Output is correct |
36 |
Correct |
67 ms |
20168 KB |
Output is correct |
37 |
Correct |
49 ms |
22676 KB |
Output is correct |
38 |
Correct |
50 ms |
22476 KB |
Output is correct |
39 |
Correct |
52 ms |
20360 KB |
Output is correct |
40 |
Correct |
52 ms |
22928 KB |
Output is correct |
41 |
Correct |
16 ms |
10196 KB |
Output is correct |
42 |
Correct |
13 ms |
10456 KB |
Output is correct |
43 |
Correct |
11 ms |
10456 KB |
Output is correct |
44 |
Correct |
9 ms |
10284 KB |
Output is correct |
45 |
Correct |
15 ms |
10452 KB |
Output is correct |
46 |
Correct |
19 ms |
10416 KB |
Output is correct |
47 |
Correct |
15 ms |
10200 KB |
Output is correct |
48 |
Correct |
12 ms |
10452 KB |
Output is correct |
49 |
Correct |
11 ms |
10200 KB |
Output is correct |
50 |
Correct |
15 ms |
10456 KB |
Output is correct |
51 |
Correct |
56 ms |
23868 KB |
Output is correct |
52 |
Correct |
65 ms |
24184 KB |
Output is correct |
53 |
Correct |
53 ms |
24056 KB |
Output is correct |
54 |
Correct |
55 ms |
24012 KB |
Output is correct |
55 |
Correct |
53 ms |
23856 KB |
Output is correct |
56 |
Correct |
904 ms |
71044 KB |
Output is correct |
57 |
Correct |
832 ms |
83312 KB |
Output is correct |
58 |
Correct |
766 ms |
80312 KB |
Output is correct |
59 |
Correct |
828 ms |
81632 KB |
Output is correct |
60 |
Correct |
905 ms |
84872 KB |
Output is correct |