#include<bits/stdc++.h>
using namespace std;
struct Node{
long long num_of_intervals = 0;
int pref = 0, suff = 0, len = 0;
};
struct SegmentTree{
vector<Node>sTree;
SegmentTree(int n){
sTree.resize(4 * n);
}
void Build(int node, int l, int r){
if(l == r){
sTree[node] = {0, 0, 0, 1};
}
else{
int mid = (l + r) >> 1;
Build(node * 2, l, mid);
Build(node * 2 + 1, mid + 1, r);
sTree[node].len = sTree[node * 2].len + sTree[node * 2 + 1].len;
}
}
Node Merge(Node &left, Node &right){
Node res;
res.len = left.len + right.len;
res.pref = left.pref;
if(left.pref == left.len) res.pref += right.pref;
res.suff = right.suff;
if(right.suff == right.len) 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, 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]);
}
}
};
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);
tree.Build(1, 1, n);
int p_v = 1, p_queries = 0;
for(int i = 1;i <= 1e6;++i){
while(p_v <= n && v[p_v].first == i){
tree.Update(1, 1, n, v[p_v].second);
p_v++;
}
while(p_queries < q && queries[p_queries].first == i){
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 |
2 ms |
344 KB |
Output is correct |
2 |
Correct |
3 ms |
348 KB |
Output is correct |
3 |
Correct |
3 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
3 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
3 ms |
372 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
3 ms |
348 KB |
Output is correct |
10 |
Correct |
3 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
344 KB |
Output is correct |
2 |
Correct |
3 ms |
348 KB |
Output is correct |
3 |
Correct |
3 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
3 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
3 ms |
372 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
3 ms |
348 KB |
Output is correct |
10 |
Correct |
3 ms |
348 KB |
Output is correct |
11 |
Correct |
5 ms |
348 KB |
Output is correct |
12 |
Correct |
4 ms |
348 KB |
Output is correct |
13 |
Correct |
7 ms |
344 KB |
Output is correct |
14 |
Correct |
4 ms |
348 KB |
Output is correct |
15 |
Correct |
5 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
344 KB |
Output is correct |
2 |
Correct |
3 ms |
348 KB |
Output is correct |
3 |
Correct |
3 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
3 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
3 ms |
372 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
3 ms |
348 KB |
Output is correct |
10 |
Correct |
3 ms |
348 KB |
Output is correct |
11 |
Correct |
5 ms |
348 KB |
Output is correct |
12 |
Correct |
4 ms |
348 KB |
Output is correct |
13 |
Correct |
7 ms |
344 KB |
Output is correct |
14 |
Correct |
4 ms |
348 KB |
Output is correct |
15 |
Correct |
5 ms |
348 KB |
Output is correct |
16 |
Correct |
4 ms |
456 KB |
Output is correct |
17 |
Correct |
3 ms |
348 KB |
Output is correct |
18 |
Correct |
4 ms |
604 KB |
Output is correct |
19 |
Correct |
3 ms |
348 KB |
Output is correct |
20 |
Correct |
7 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
344 KB |
Output is correct |
2 |
Correct |
3 ms |
348 KB |
Output is correct |
3 |
Correct |
3 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
3 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
3 ms |
372 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
3 ms |
348 KB |
Output is correct |
10 |
Correct |
3 ms |
348 KB |
Output is correct |
11 |
Correct |
5 ms |
348 KB |
Output is correct |
12 |
Correct |
4 ms |
348 KB |
Output is correct |
13 |
Correct |
7 ms |
344 KB |
Output is correct |
14 |
Correct |
4 ms |
348 KB |
Output is correct |
15 |
Correct |
5 ms |
348 KB |
Output is correct |
16 |
Correct |
4 ms |
456 KB |
Output is correct |
17 |
Correct |
3 ms |
348 KB |
Output is correct |
18 |
Correct |
4 ms |
604 KB |
Output is correct |
19 |
Correct |
3 ms |
348 KB |
Output is correct |
20 |
Correct |
7 ms |
348 KB |
Output is correct |
21 |
Correct |
7 ms |
344 KB |
Output is correct |
22 |
Correct |
4 ms |
344 KB |
Output is correct |
23 |
Correct |
7 ms |
348 KB |
Output is correct |
24 |
Correct |
4 ms |
468 KB |
Output is correct |
25 |
Correct |
7 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
10700 KB |
Output is correct |
2 |
Correct |
38 ms |
10864 KB |
Output is correct |
3 |
Correct |
41 ms |
10320 KB |
Output is correct |
4 |
Correct |
39 ms |
10072 KB |
Output is correct |
5 |
Correct |
40 ms |
10400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
13276 KB |
Output is correct |
2 |
Correct |
46 ms |
13396 KB |
Output is correct |
3 |
Correct |
45 ms |
13140 KB |
Output is correct |
4 |
Correct |
45 ms |
13652 KB |
Output is correct |
5 |
Correct |
43 ms |
13144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
13604 KB |
Output is correct |
2 |
Correct |
44 ms |
13624 KB |
Output is correct |
3 |
Correct |
44 ms |
13436 KB |
Output is correct |
4 |
Correct |
48 ms |
14116 KB |
Output is correct |
5 |
Correct |
51 ms |
13648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
344 KB |
Output is correct |
2 |
Correct |
3 ms |
348 KB |
Output is correct |
3 |
Correct |
3 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
3 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
3 ms |
372 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
3 ms |
348 KB |
Output is correct |
10 |
Correct |
3 ms |
348 KB |
Output is correct |
11 |
Correct |
40 ms |
10700 KB |
Output is correct |
12 |
Correct |
38 ms |
10864 KB |
Output is correct |
13 |
Correct |
41 ms |
10320 KB |
Output is correct |
14 |
Correct |
39 ms |
10072 KB |
Output is correct |
15 |
Correct |
40 ms |
10400 KB |
Output is correct |
16 |
Correct |
34 ms |
10332 KB |
Output is correct |
17 |
Correct |
38 ms |
10832 KB |
Output is correct |
18 |
Correct |
38 ms |
11096 KB |
Output is correct |
19 |
Correct |
41 ms |
10156 KB |
Output is correct |
20 |
Correct |
45 ms |
10832 KB |
Output is correct |
21 |
Correct |
34 ms |
10076 KB |
Output is correct |
22 |
Correct |
35 ms |
10516 KB |
Output is correct |
23 |
Correct |
37 ms |
11100 KB |
Output is correct |
24 |
Correct |
36 ms |
10368 KB |
Output is correct |
25 |
Correct |
44 ms |
10584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
344 KB |
Output is correct |
2 |
Correct |
3 ms |
348 KB |
Output is correct |
3 |
Correct |
3 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
3 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
3 ms |
372 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
3 ms |
348 KB |
Output is correct |
10 |
Correct |
3 ms |
348 KB |
Output is correct |
11 |
Correct |
5 ms |
348 KB |
Output is correct |
12 |
Correct |
4 ms |
348 KB |
Output is correct |
13 |
Correct |
7 ms |
344 KB |
Output is correct |
14 |
Correct |
4 ms |
348 KB |
Output is correct |
15 |
Correct |
5 ms |
348 KB |
Output is correct |
16 |
Correct |
4 ms |
456 KB |
Output is correct |
17 |
Correct |
3 ms |
348 KB |
Output is correct |
18 |
Correct |
4 ms |
604 KB |
Output is correct |
19 |
Correct |
3 ms |
348 KB |
Output is correct |
20 |
Correct |
7 ms |
348 KB |
Output is correct |
21 |
Correct |
7 ms |
344 KB |
Output is correct |
22 |
Correct |
4 ms |
344 KB |
Output is correct |
23 |
Correct |
7 ms |
348 KB |
Output is correct |
24 |
Correct |
4 ms |
468 KB |
Output is correct |
25 |
Correct |
7 ms |
348 KB |
Output is correct |
26 |
Correct |
40 ms |
10700 KB |
Output is correct |
27 |
Correct |
38 ms |
10864 KB |
Output is correct |
28 |
Correct |
41 ms |
10320 KB |
Output is correct |
29 |
Correct |
39 ms |
10072 KB |
Output is correct |
30 |
Correct |
40 ms |
10400 KB |
Output is correct |
31 |
Correct |
43 ms |
13276 KB |
Output is correct |
32 |
Correct |
46 ms |
13396 KB |
Output is correct |
33 |
Correct |
45 ms |
13140 KB |
Output is correct |
34 |
Correct |
45 ms |
13652 KB |
Output is correct |
35 |
Correct |
43 ms |
13144 KB |
Output is correct |
36 |
Correct |
46 ms |
13604 KB |
Output is correct |
37 |
Correct |
44 ms |
13624 KB |
Output is correct |
38 |
Correct |
44 ms |
13436 KB |
Output is correct |
39 |
Correct |
48 ms |
14116 KB |
Output is correct |
40 |
Correct |
51 ms |
13648 KB |
Output is correct |
41 |
Correct |
34 ms |
10332 KB |
Output is correct |
42 |
Correct |
38 ms |
10832 KB |
Output is correct |
43 |
Correct |
38 ms |
11096 KB |
Output is correct |
44 |
Correct |
41 ms |
10156 KB |
Output is correct |
45 |
Correct |
45 ms |
10832 KB |
Output is correct |
46 |
Correct |
34 ms |
10076 KB |
Output is correct |
47 |
Correct |
35 ms |
10516 KB |
Output is correct |
48 |
Correct |
37 ms |
11100 KB |
Output is correct |
49 |
Correct |
36 ms |
10368 KB |
Output is correct |
50 |
Correct |
44 ms |
10584 KB |
Output is correct |
51 |
Correct |
59 ms |
13604 KB |
Output is correct |
52 |
Correct |
58 ms |
13396 KB |
Output is correct |
53 |
Correct |
68 ms |
13804 KB |
Output is correct |
54 |
Correct |
67 ms |
13432 KB |
Output is correct |
55 |
Correct |
59 ms |
13692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
344 KB |
Output is correct |
2 |
Correct |
3 ms |
348 KB |
Output is correct |
3 |
Correct |
3 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
3 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
3 ms |
372 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
3 ms |
348 KB |
Output is correct |
10 |
Correct |
3 ms |
348 KB |
Output is correct |
11 |
Correct |
5 ms |
348 KB |
Output is correct |
12 |
Correct |
4 ms |
348 KB |
Output is correct |
13 |
Correct |
7 ms |
344 KB |
Output is correct |
14 |
Correct |
4 ms |
348 KB |
Output is correct |
15 |
Correct |
5 ms |
348 KB |
Output is correct |
16 |
Correct |
4 ms |
456 KB |
Output is correct |
17 |
Correct |
3 ms |
348 KB |
Output is correct |
18 |
Correct |
4 ms |
604 KB |
Output is correct |
19 |
Correct |
3 ms |
348 KB |
Output is correct |
20 |
Correct |
7 ms |
348 KB |
Output is correct |
21 |
Correct |
7 ms |
344 KB |
Output is correct |
22 |
Correct |
4 ms |
344 KB |
Output is correct |
23 |
Correct |
7 ms |
348 KB |
Output is correct |
24 |
Correct |
4 ms |
468 KB |
Output is correct |
25 |
Correct |
7 ms |
348 KB |
Output is correct |
26 |
Correct |
40 ms |
10700 KB |
Output is correct |
27 |
Correct |
38 ms |
10864 KB |
Output is correct |
28 |
Correct |
41 ms |
10320 KB |
Output is correct |
29 |
Correct |
39 ms |
10072 KB |
Output is correct |
30 |
Correct |
40 ms |
10400 KB |
Output is correct |
31 |
Correct |
43 ms |
13276 KB |
Output is correct |
32 |
Correct |
46 ms |
13396 KB |
Output is correct |
33 |
Correct |
45 ms |
13140 KB |
Output is correct |
34 |
Correct |
45 ms |
13652 KB |
Output is correct |
35 |
Correct |
43 ms |
13144 KB |
Output is correct |
36 |
Correct |
46 ms |
13604 KB |
Output is correct |
37 |
Correct |
44 ms |
13624 KB |
Output is correct |
38 |
Correct |
44 ms |
13436 KB |
Output is correct |
39 |
Correct |
48 ms |
14116 KB |
Output is correct |
40 |
Correct |
51 ms |
13648 KB |
Output is correct |
41 |
Correct |
34 ms |
10332 KB |
Output is correct |
42 |
Correct |
38 ms |
10832 KB |
Output is correct |
43 |
Correct |
38 ms |
11096 KB |
Output is correct |
44 |
Correct |
41 ms |
10156 KB |
Output is correct |
45 |
Correct |
45 ms |
10832 KB |
Output is correct |
46 |
Correct |
34 ms |
10076 KB |
Output is correct |
47 |
Correct |
35 ms |
10516 KB |
Output is correct |
48 |
Correct |
37 ms |
11100 KB |
Output is correct |
49 |
Correct |
36 ms |
10368 KB |
Output is correct |
50 |
Correct |
44 ms |
10584 KB |
Output is correct |
51 |
Correct |
59 ms |
13604 KB |
Output is correct |
52 |
Correct |
58 ms |
13396 KB |
Output is correct |
53 |
Correct |
68 ms |
13804 KB |
Output is correct |
54 |
Correct |
67 ms |
13432 KB |
Output is correct |
55 |
Correct |
59 ms |
13692 KB |
Output is correct |
56 |
Correct |
904 ms |
131724 KB |
Output is correct |
57 |
Correct |
855 ms |
135304 KB |
Output is correct |
58 |
Correct |
840 ms |
125676 KB |
Output is correct |
59 |
Correct |
828 ms |
129024 KB |
Output is correct |
60 |
Correct |
878 ms |
137024 KB |
Output is correct |