#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ff first
#define ss second
int pairs(int l, int r){
int len = r - l + 1;
return len * (len + 1) / 2;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
int n, q; cin >> n >> q;
vector<pair<int, int> > qries(q), h(n);
int ans[q];
for(int i = 0; i < n; i++) {
cin >> h[i].ff; h[i].ss = i;
}
for(int i = 0; i < q; i++){
cin >> qries[i].ff; qries[i].ss = i;
}
sort(qries.rbegin(), qries.rend());
sort(h.begin(), h.end());
set<int> s({-1, n});
int curr = n - 1, ans_curr = n * (n + 1) / 2;
for(auto qry : qries){
int val = qry.ff, idx = qry.ss;
while(curr > -1 && h[curr].ff > val){
auto it = s.lower_bound(h[curr].ss);
int r = *it, l = *(--it);
r--; l++;
ans_curr += pairs(l, h[curr].ss - 1) +
pairs(h[curr].ss + 1, r) -
pairs(l, r);
s.insert(h[curr].ss); curr--;
}
ans[idx] = ans_curr;
}
for(auto a : ans) cout << a << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
2392 KB |
Output is correct |
2 |
Correct |
13 ms |
2652 KB |
Output is correct |
3 |
Correct |
13 ms |
2396 KB |
Output is correct |
4 |
Correct |
12 ms |
2396 KB |
Output is correct |
5 |
Correct |
13 ms |
2280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
10460 KB |
Output is correct |
2 |
Correct |
50 ms |
10496 KB |
Output is correct |
3 |
Correct |
48 ms |
10380 KB |
Output is correct |
4 |
Correct |
47 ms |
10660 KB |
Output is correct |
5 |
Correct |
43 ms |
10324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
10576 KB |
Output is correct |
2 |
Correct |
51 ms |
10580 KB |
Output is correct |
3 |
Correct |
45 ms |
10580 KB |
Output is correct |
4 |
Correct |
49 ms |
10836 KB |
Output is correct |
5 |
Correct |
48 ms |
10952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
13 ms |
2392 KB |
Output is correct |
12 |
Correct |
13 ms |
2652 KB |
Output is correct |
13 |
Correct |
13 ms |
2396 KB |
Output is correct |
14 |
Correct |
12 ms |
2396 KB |
Output is correct |
15 |
Correct |
13 ms |
2280 KB |
Output is correct |
16 |
Correct |
21 ms |
3672 KB |
Output is correct |
17 |
Correct |
34 ms |
5460 KB |
Output is correct |
18 |
Correct |
44 ms |
6736 KB |
Output is correct |
19 |
Correct |
40 ms |
6100 KB |
Output is correct |
20 |
Correct |
28 ms |
4688 KB |
Output is correct |
21 |
Correct |
13 ms |
2392 KB |
Output is correct |
22 |
Correct |
27 ms |
4432 KB |
Output is correct |
23 |
Correct |
37 ms |
5716 KB |
Output is correct |
24 |
Correct |
37 ms |
5956 KB |
Output is correct |
25 |
Correct |
27 ms |
4704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
13 ms |
2392 KB |
Output is correct |
27 |
Correct |
13 ms |
2652 KB |
Output is correct |
28 |
Correct |
13 ms |
2396 KB |
Output is correct |
29 |
Correct |
12 ms |
2396 KB |
Output is correct |
30 |
Correct |
13 ms |
2280 KB |
Output is correct |
31 |
Correct |
49 ms |
10460 KB |
Output is correct |
32 |
Correct |
50 ms |
10496 KB |
Output is correct |
33 |
Correct |
48 ms |
10380 KB |
Output is correct |
34 |
Correct |
47 ms |
10660 KB |
Output is correct |
35 |
Correct |
43 ms |
10324 KB |
Output is correct |
36 |
Correct |
47 ms |
10576 KB |
Output is correct |
37 |
Correct |
51 ms |
10580 KB |
Output is correct |
38 |
Correct |
45 ms |
10580 KB |
Output is correct |
39 |
Correct |
49 ms |
10836 KB |
Output is correct |
40 |
Correct |
48 ms |
10952 KB |
Output is correct |
41 |
Correct |
21 ms |
3672 KB |
Output is correct |
42 |
Correct |
34 ms |
5460 KB |
Output is correct |
43 |
Correct |
44 ms |
6736 KB |
Output is correct |
44 |
Correct |
40 ms |
6100 KB |
Output is correct |
45 |
Correct |
28 ms |
4688 KB |
Output is correct |
46 |
Correct |
13 ms |
2392 KB |
Output is correct |
47 |
Correct |
27 ms |
4432 KB |
Output is correct |
48 |
Correct |
37 ms |
5716 KB |
Output is correct |
49 |
Correct |
37 ms |
5956 KB |
Output is correct |
50 |
Correct |
27 ms |
4704 KB |
Output is correct |
51 |
Correct |
65 ms |
10580 KB |
Output is correct |
52 |
Correct |
62 ms |
10364 KB |
Output is correct |
53 |
Correct |
71 ms |
10516 KB |
Output is correct |
54 |
Correct |
64 ms |
10316 KB |
Output is correct |
55 |
Correct |
67 ms |
10580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
13 ms |
2392 KB |
Output is correct |
27 |
Correct |
13 ms |
2652 KB |
Output is correct |
28 |
Correct |
13 ms |
2396 KB |
Output is correct |
29 |
Correct |
12 ms |
2396 KB |
Output is correct |
30 |
Correct |
13 ms |
2280 KB |
Output is correct |
31 |
Correct |
49 ms |
10460 KB |
Output is correct |
32 |
Correct |
50 ms |
10496 KB |
Output is correct |
33 |
Correct |
48 ms |
10380 KB |
Output is correct |
34 |
Correct |
47 ms |
10660 KB |
Output is correct |
35 |
Correct |
43 ms |
10324 KB |
Output is correct |
36 |
Correct |
47 ms |
10576 KB |
Output is correct |
37 |
Correct |
51 ms |
10580 KB |
Output is correct |
38 |
Correct |
45 ms |
10580 KB |
Output is correct |
39 |
Correct |
49 ms |
10836 KB |
Output is correct |
40 |
Correct |
48 ms |
10952 KB |
Output is correct |
41 |
Correct |
21 ms |
3672 KB |
Output is correct |
42 |
Correct |
34 ms |
5460 KB |
Output is correct |
43 |
Correct |
44 ms |
6736 KB |
Output is correct |
44 |
Correct |
40 ms |
6100 KB |
Output is correct |
45 |
Correct |
28 ms |
4688 KB |
Output is correct |
46 |
Correct |
13 ms |
2392 KB |
Output is correct |
47 |
Correct |
27 ms |
4432 KB |
Output is correct |
48 |
Correct |
37 ms |
5716 KB |
Output is correct |
49 |
Correct |
37 ms |
5956 KB |
Output is correct |
50 |
Correct |
27 ms |
4704 KB |
Output is correct |
51 |
Correct |
65 ms |
10580 KB |
Output is correct |
52 |
Correct |
62 ms |
10364 KB |
Output is correct |
53 |
Correct |
71 ms |
10516 KB |
Output is correct |
54 |
Correct |
64 ms |
10316 KB |
Output is correct |
55 |
Correct |
67 ms |
10580 KB |
Output is correct |
56 |
Execution timed out |
1014 ms |
102804 KB |
Time limit exceeded |
57 |
Halted |
0 ms |
0 KB |
- |