#include<bits/stdc++.h>
using namespace std;
struct Node{
long long num_of_intervals = 0;
int pref = 0, suff = 0;
};
struct SegmentTree{
vector<Node>sTree;
SegmentTree(int n){
sTree.resize(4 * n);
}
Node Merge(Node &left, Node &right, int &l, int &r){
Node res;
int mid = (l + r) >> 1;
res.pref = left.pref;
if(left.pref == mid - l + 1) res.pref += right.pref;
res.suff = right.suff;
if(right.suff == r - mid) res.suff += left.suff;
res.num_of_intervals = left.num_of_intervals + right.num_of_intervals;
res.num_of_intervals -= (long long)right.pref * (right.pref + 1) / 2 + (long long)left.suff * (left.suff + 1) / 2;
res.num_of_intervals += ((long long)right.pref + left.suff) * (right.pref + left.suff + 1) / 2;
return res;
}
void Update(int node, int l, int r, int pos){
if(l == r){
sTree[node] = {1, 1, 1};
}
else{
int mid = (l + r) >> 1;
if(pos <= mid){
Update(node * 2, l, mid, pos);
}
else{
Update(node * 2 + 1, mid + 1, r, pos);
}
sTree[node] = Merge(sTree[node * 2], sTree[node * 2 + 1], l, r);
}
}
};
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, q;
cin >> n >> q;
vector<pair<int, int>>v(n + 1);
for(int i = 1;i <= n;++i){
cin >> v[i].first;
v[i].second = i;
}
vector<pair<int, int>>queries(q);
vector<long long> answers(q);
for(int i = 0;i < q;++i) {
cin >> queries[i].first;
queries[i].second = i;
}
sort(v.begin(), v.end());
sort(queries.begin(), queries.end());
SegmentTree tree(n);
int p_v = 1, p_queries = 0;
while(p_queries != q){
if(p_v <= n && v[p_v].first <= queries[p_queries].first){
tree.Update(1, 1, n, v[p_v].second);
p_v++;
}
else{
answers[queries[p_queries].second] = tree.sTree[1].num_of_intervals;
p_queries++;
}
}
for(int i = 0;i < q;++i){
cout << answers[i] << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
352 KB |
Output is correct |
2 |
Correct |
1 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 |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
352 KB |
Output is correct |
2 |
Correct |
1 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 |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 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 |
1 ms |
352 KB |
Output is correct |
2 |
Correct |
1 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 |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 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 |
596 KB |
Output is correct |
17 |
Correct |
1 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 |
1 ms |
352 KB |
Output is correct |
2 |
Correct |
1 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 |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 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 |
596 KB |
Output is correct |
17 |
Correct |
1 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 |
344 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 |
496 KB |
Output is correct |
25 |
Correct |
1 ms |
352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
7840 KB |
Output is correct |
2 |
Correct |
35 ms |
7788 KB |
Output is correct |
3 |
Correct |
34 ms |
7620 KB |
Output is correct |
4 |
Correct |
32 ms |
7264 KB |
Output is correct |
5 |
Correct |
35 ms |
7516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
10132 KB |
Output is correct |
2 |
Correct |
43 ms |
10320 KB |
Output is correct |
3 |
Correct |
48 ms |
10064 KB |
Output is correct |
4 |
Correct |
43 ms |
10580 KB |
Output is correct |
5 |
Correct |
41 ms |
9968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
10324 KB |
Output is correct |
2 |
Correct |
43 ms |
10328 KB |
Output is correct |
3 |
Correct |
45 ms |
10116 KB |
Output is correct |
4 |
Correct |
50 ms |
10700 KB |
Output is correct |
5 |
Correct |
44 ms |
10608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
352 KB |
Output is correct |
2 |
Correct |
1 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 |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
35 ms |
7840 KB |
Output is correct |
12 |
Correct |
35 ms |
7788 KB |
Output is correct |
13 |
Correct |
34 ms |
7620 KB |
Output is correct |
14 |
Correct |
32 ms |
7264 KB |
Output is correct |
15 |
Correct |
35 ms |
7516 KB |
Output is correct |
16 |
Correct |
28 ms |
7364 KB |
Output is correct |
17 |
Correct |
22 ms |
7768 KB |
Output is correct |
18 |
Correct |
17 ms |
8028 KB |
Output is correct |
19 |
Correct |
16 ms |
7260 KB |
Output is correct |
20 |
Correct |
25 ms |
7772 KB |
Output is correct |
21 |
Correct |
32 ms |
7260 KB |
Output is correct |
22 |
Correct |
24 ms |
7516 KB |
Output is correct |
23 |
Correct |
21 ms |
8028 KB |
Output is correct |
24 |
Correct |
17 ms |
7516 KB |
Output is correct |
25 |
Correct |
26 ms |
7896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
352 KB |
Output is correct |
2 |
Correct |
1 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 |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 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 |
596 KB |
Output is correct |
17 |
Correct |
1 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 |
344 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 |
496 KB |
Output is correct |
25 |
Correct |
1 ms |
352 KB |
Output is correct |
26 |
Correct |
35 ms |
7840 KB |
Output is correct |
27 |
Correct |
35 ms |
7788 KB |
Output is correct |
28 |
Correct |
34 ms |
7620 KB |
Output is correct |
29 |
Correct |
32 ms |
7264 KB |
Output is correct |
30 |
Correct |
35 ms |
7516 KB |
Output is correct |
31 |
Correct |
46 ms |
10132 KB |
Output is correct |
32 |
Correct |
43 ms |
10320 KB |
Output is correct |
33 |
Correct |
48 ms |
10064 KB |
Output is correct |
34 |
Correct |
43 ms |
10580 KB |
Output is correct |
35 |
Correct |
41 ms |
9968 KB |
Output is correct |
36 |
Correct |
43 ms |
10324 KB |
Output is correct |
37 |
Correct |
43 ms |
10328 KB |
Output is correct |
38 |
Correct |
45 ms |
10116 KB |
Output is correct |
39 |
Correct |
50 ms |
10700 KB |
Output is correct |
40 |
Correct |
44 ms |
10608 KB |
Output is correct |
41 |
Correct |
28 ms |
7364 KB |
Output is correct |
42 |
Correct |
22 ms |
7768 KB |
Output is correct |
43 |
Correct |
17 ms |
8028 KB |
Output is correct |
44 |
Correct |
16 ms |
7260 KB |
Output is correct |
45 |
Correct |
25 ms |
7772 KB |
Output is correct |
46 |
Correct |
32 ms |
7260 KB |
Output is correct |
47 |
Correct |
24 ms |
7516 KB |
Output is correct |
48 |
Correct |
21 ms |
8028 KB |
Output is correct |
49 |
Correct |
17 ms |
7516 KB |
Output is correct |
50 |
Correct |
26 ms |
7896 KB |
Output is correct |
51 |
Correct |
56 ms |
10320 KB |
Output is correct |
52 |
Correct |
59 ms |
10064 KB |
Output is correct |
53 |
Correct |
56 ms |
10324 KB |
Output is correct |
54 |
Correct |
60 ms |
10132 KB |
Output is correct |
55 |
Correct |
57 ms |
10512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
352 KB |
Output is correct |
2 |
Correct |
1 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 |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 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 |
596 KB |
Output is correct |
17 |
Correct |
1 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 |
344 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 |
496 KB |
Output is correct |
25 |
Correct |
1 ms |
352 KB |
Output is correct |
26 |
Correct |
35 ms |
7840 KB |
Output is correct |
27 |
Correct |
35 ms |
7788 KB |
Output is correct |
28 |
Correct |
34 ms |
7620 KB |
Output is correct |
29 |
Correct |
32 ms |
7264 KB |
Output is correct |
30 |
Correct |
35 ms |
7516 KB |
Output is correct |
31 |
Correct |
46 ms |
10132 KB |
Output is correct |
32 |
Correct |
43 ms |
10320 KB |
Output is correct |
33 |
Correct |
48 ms |
10064 KB |
Output is correct |
34 |
Correct |
43 ms |
10580 KB |
Output is correct |
35 |
Correct |
41 ms |
9968 KB |
Output is correct |
36 |
Correct |
43 ms |
10324 KB |
Output is correct |
37 |
Correct |
43 ms |
10328 KB |
Output is correct |
38 |
Correct |
45 ms |
10116 KB |
Output is correct |
39 |
Correct |
50 ms |
10700 KB |
Output is correct |
40 |
Correct |
44 ms |
10608 KB |
Output is correct |
41 |
Correct |
28 ms |
7364 KB |
Output is correct |
42 |
Correct |
22 ms |
7768 KB |
Output is correct |
43 |
Correct |
17 ms |
8028 KB |
Output is correct |
44 |
Correct |
16 ms |
7260 KB |
Output is correct |
45 |
Correct |
25 ms |
7772 KB |
Output is correct |
46 |
Correct |
32 ms |
7260 KB |
Output is correct |
47 |
Correct |
24 ms |
7516 KB |
Output is correct |
48 |
Correct |
21 ms |
8028 KB |
Output is correct |
49 |
Correct |
17 ms |
7516 KB |
Output is correct |
50 |
Correct |
26 ms |
7896 KB |
Output is correct |
51 |
Correct |
56 ms |
10320 KB |
Output is correct |
52 |
Correct |
59 ms |
10064 KB |
Output is correct |
53 |
Correct |
56 ms |
10324 KB |
Output is correct |
54 |
Correct |
60 ms |
10132 KB |
Output is correct |
55 |
Correct |
57 ms |
10512 KB |
Output is correct |
56 |
Correct |
816 ms |
96356 KB |
Output is correct |
57 |
Correct |
814 ms |
98344 KB |
Output is correct |
58 |
Correct |
828 ms |
91600 KB |
Output is correct |
59 |
Correct |
838 ms |
93904 KB |
Output is correct |
60 |
Correct |
871 ms |
99764 KB |
Output is correct |