// Online C++ compiler to run C++ program online
#include <bits/stdc++.h>
using namespace std;
pair<int, int> a[2100000];
int h[1000001];
long long d[1000001];
priority_queue<pair<int, int>> q;
void L(int x, int y, bool z){
int x_ = x, y_ = y;
if(!z){
x_ = 0;
y_ = 0;
}
while(x < y){
if(x % 2 == 1){
a[x].first = x_;
a[x].second = y_;
x++;
}
if(y % 2 == 0){
a[y].first = x_;
a[y].second = y_;
y--;
}
x /= 2;
y /= 2;
}
if(x == y){
a[x].first = x_;
a[y].second = y_;
}
}
int g(int x){
while(!a[x].first){
x /= 2;
}
return x;
}
int main() {
// Write C++ code here
ios_base::sync_with_stdio(NULL);
cin.tie(NULL);
int n, m;
cin >> n >> m;
int k = 1, o = 0;
while(k < n){
k *= 2;
}
for(int i = 0; i < n; i++){
cin >> h[i];
q.push({h[i], i});
o = max(o, h[i]);
}
L(k, k+n-1, true);
while(!q.empty()){
int w = q.top().first;
int z = q.top().second;
q.pop();
int t = g(k+z);
int x = a[t].first, y = a[t].second;
L(a[t].first, a[t].second, false);
d[w] += 1ll * (k+z-x+1) * (y-k-z+1);
if(k+z == x && k+z == y){
continue;
}
if(k+z > x){
L(x, k+z-1, true);
}
if(k+z < y){
L(k+z+1, y, true);
}
}
for(int i = o; i; i--){
d[i-1] += d[i];
}
for(int i = 0; i < m; i++){
int w;
cin >> w;
cout << 1ll * n * (n+1) / 2 - d[w+1] << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
3 ms |
4052 KB |
Output is correct |
3 |
Correct |
5 ms |
7244 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
5 ms |
7252 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
5 ms |
7764 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
4 ms |
5972 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
3 ms |
4052 KB |
Output is correct |
3 |
Correct |
5 ms |
7244 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
5 ms |
7252 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
5 ms |
7764 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
4 ms |
5972 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
5 ms |
7752 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
5 ms |
8020 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
3 ms |
4052 KB |
Output is correct |
3 |
Correct |
5 ms |
7244 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
5 ms |
7252 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
5 ms |
7764 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
4 ms |
5972 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
5 ms |
7752 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
5 ms |
8020 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
6 ms |
8020 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
6 ms |
8020 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
3 ms |
4052 KB |
Output is correct |
3 |
Correct |
5 ms |
7244 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
5 ms |
7252 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
5 ms |
7764 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
4 ms |
5972 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
5 ms |
7752 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
5 ms |
8020 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
6 ms |
8020 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
6 ms |
8020 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
5 ms |
8148 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
6 ms |
8196 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
3828 KB |
Output is correct |
2 |
Correct |
39 ms |
11020 KB |
Output is correct |
3 |
Correct |
38 ms |
3892 KB |
Output is correct |
4 |
Correct |
32 ms |
10868 KB |
Output is correct |
5 |
Correct |
34 ms |
3824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
4676 KB |
Output is correct |
2 |
Correct |
43 ms |
4728 KB |
Output is correct |
3 |
Correct |
48 ms |
4700 KB |
Output is correct |
4 |
Correct |
48 ms |
4808 KB |
Output is correct |
5 |
Correct |
42 ms |
4632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
11732 KB |
Output is correct |
2 |
Correct |
53 ms |
11680 KB |
Output is correct |
3 |
Correct |
71 ms |
11776 KB |
Output is correct |
4 |
Correct |
52 ms |
11908 KB |
Output is correct |
5 |
Correct |
49 ms |
11844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
3 ms |
4052 KB |
Output is correct |
3 |
Correct |
5 ms |
7244 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
5 ms |
7252 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
5 ms |
7764 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
4 ms |
5972 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
27 ms |
3828 KB |
Output is correct |
12 |
Correct |
39 ms |
11020 KB |
Output is correct |
13 |
Correct |
38 ms |
3892 KB |
Output is correct |
14 |
Correct |
32 ms |
10868 KB |
Output is correct |
15 |
Correct |
34 ms |
3824 KB |
Output is correct |
16 |
Correct |
27 ms |
3804 KB |
Output is correct |
17 |
Correct |
32 ms |
11036 KB |
Output is correct |
18 |
Correct |
34 ms |
11096 KB |
Output is correct |
19 |
Correct |
30 ms |
3816 KB |
Output is correct |
20 |
Correct |
32 ms |
11044 KB |
Output is correct |
21 |
Correct |
26 ms |
3836 KB |
Output is correct |
22 |
Correct |
31 ms |
10860 KB |
Output is correct |
23 |
Correct |
30 ms |
4008 KB |
Output is correct |
24 |
Correct |
31 ms |
10832 KB |
Output is correct |
25 |
Correct |
27 ms |
3892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
3 ms |
4052 KB |
Output is correct |
3 |
Correct |
5 ms |
7244 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
5 ms |
7252 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
5 ms |
7764 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
4 ms |
5972 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
5 ms |
7752 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
5 ms |
8020 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
6 ms |
8020 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
6 ms |
8020 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
5 ms |
8148 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
6 ms |
8196 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
27 ms |
3828 KB |
Output is correct |
27 |
Correct |
39 ms |
11020 KB |
Output is correct |
28 |
Correct |
38 ms |
3892 KB |
Output is correct |
29 |
Correct |
32 ms |
10868 KB |
Output is correct |
30 |
Correct |
34 ms |
3824 KB |
Output is correct |
31 |
Correct |
41 ms |
4676 KB |
Output is correct |
32 |
Correct |
43 ms |
4728 KB |
Output is correct |
33 |
Correct |
48 ms |
4700 KB |
Output is correct |
34 |
Correct |
48 ms |
4808 KB |
Output is correct |
35 |
Correct |
42 ms |
4632 KB |
Output is correct |
36 |
Correct |
52 ms |
11732 KB |
Output is correct |
37 |
Correct |
53 ms |
11680 KB |
Output is correct |
38 |
Correct |
71 ms |
11776 KB |
Output is correct |
39 |
Correct |
52 ms |
11908 KB |
Output is correct |
40 |
Correct |
49 ms |
11844 KB |
Output is correct |
41 |
Correct |
27 ms |
3804 KB |
Output is correct |
42 |
Correct |
32 ms |
11036 KB |
Output is correct |
43 |
Correct |
34 ms |
11096 KB |
Output is correct |
44 |
Correct |
30 ms |
3816 KB |
Output is correct |
45 |
Correct |
32 ms |
11044 KB |
Output is correct |
46 |
Correct |
26 ms |
3836 KB |
Output is correct |
47 |
Correct |
31 ms |
10860 KB |
Output is correct |
48 |
Correct |
30 ms |
4008 KB |
Output is correct |
49 |
Correct |
31 ms |
10832 KB |
Output is correct |
50 |
Correct |
27 ms |
3892 KB |
Output is correct |
51 |
Correct |
46 ms |
4812 KB |
Output is correct |
52 |
Correct |
50 ms |
11588 KB |
Output is correct |
53 |
Correct |
49 ms |
4800 KB |
Output is correct |
54 |
Correct |
53 ms |
11540 KB |
Output is correct |
55 |
Correct |
50 ms |
4920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
3 ms |
4052 KB |
Output is correct |
3 |
Correct |
5 ms |
7244 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
5 ms |
7252 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
5 ms |
7764 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
4 ms |
5972 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
5 ms |
7752 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
5 ms |
8020 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
6 ms |
8020 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
6 ms |
8020 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
5 ms |
8148 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
6 ms |
8196 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
27 ms |
3828 KB |
Output is correct |
27 |
Correct |
39 ms |
11020 KB |
Output is correct |
28 |
Correct |
38 ms |
3892 KB |
Output is correct |
29 |
Correct |
32 ms |
10868 KB |
Output is correct |
30 |
Correct |
34 ms |
3824 KB |
Output is correct |
31 |
Correct |
41 ms |
4676 KB |
Output is correct |
32 |
Correct |
43 ms |
4728 KB |
Output is correct |
33 |
Correct |
48 ms |
4700 KB |
Output is correct |
34 |
Correct |
48 ms |
4808 KB |
Output is correct |
35 |
Correct |
42 ms |
4632 KB |
Output is correct |
36 |
Correct |
52 ms |
11732 KB |
Output is correct |
37 |
Correct |
53 ms |
11680 KB |
Output is correct |
38 |
Correct |
71 ms |
11776 KB |
Output is correct |
39 |
Correct |
52 ms |
11908 KB |
Output is correct |
40 |
Correct |
49 ms |
11844 KB |
Output is correct |
41 |
Correct |
27 ms |
3804 KB |
Output is correct |
42 |
Correct |
32 ms |
11036 KB |
Output is correct |
43 |
Correct |
34 ms |
11096 KB |
Output is correct |
44 |
Correct |
30 ms |
3816 KB |
Output is correct |
45 |
Correct |
32 ms |
11044 KB |
Output is correct |
46 |
Correct |
26 ms |
3836 KB |
Output is correct |
47 |
Correct |
31 ms |
10860 KB |
Output is correct |
48 |
Correct |
30 ms |
4008 KB |
Output is correct |
49 |
Correct |
31 ms |
10832 KB |
Output is correct |
50 |
Correct |
27 ms |
3892 KB |
Output is correct |
51 |
Correct |
46 ms |
4812 KB |
Output is correct |
52 |
Correct |
50 ms |
11588 KB |
Output is correct |
53 |
Correct |
49 ms |
4800 KB |
Output is correct |
54 |
Correct |
53 ms |
11540 KB |
Output is correct |
55 |
Correct |
50 ms |
4920 KB |
Output is correct |
56 |
Correct |
638 ms |
45968 KB |
Output is correct |
57 |
Correct |
667 ms |
55304 KB |
Output is correct |
58 |
Correct |
613 ms |
51648 KB |
Output is correct |
59 |
Correct |
597 ms |
53332 KB |
Output is correct |
60 |
Correct |
647 ms |
56216 KB |
Output is correct |