#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;
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 |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
456 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 |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
456 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 |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 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 |
1 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
456 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 |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 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 |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
456 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 |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 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 |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
600 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
472 KB |
Output is correct |
23 |
Correct |
1 ms |
344 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 |
34 ms |
10588 KB |
Output is correct |
2 |
Correct |
35 ms |
11224 KB |
Output is correct |
3 |
Correct |
35 ms |
10328 KB |
Output is correct |
4 |
Correct |
34 ms |
10076 KB |
Output is correct |
5 |
Correct |
32 ms |
10332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
13144 KB |
Output is correct |
2 |
Correct |
41 ms |
13140 KB |
Output is correct |
3 |
Correct |
47 ms |
12928 KB |
Output is correct |
4 |
Correct |
44 ms |
13392 KB |
Output is correct |
5 |
Correct |
42 ms |
12880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
13092 KB |
Output is correct |
2 |
Correct |
41 ms |
12908 KB |
Output is correct |
3 |
Correct |
53 ms |
12912 KB |
Output is correct |
4 |
Correct |
44 ms |
13768 KB |
Output is correct |
5 |
Correct |
43 ms |
13396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
456 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 |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
34 ms |
10588 KB |
Output is correct |
12 |
Correct |
35 ms |
11224 KB |
Output is correct |
13 |
Correct |
35 ms |
10328 KB |
Output is correct |
14 |
Correct |
34 ms |
10076 KB |
Output is correct |
15 |
Correct |
32 ms |
10332 KB |
Output is correct |
16 |
Correct |
30 ms |
10332 KB |
Output is correct |
17 |
Correct |
23 ms |
10844 KB |
Output is correct |
18 |
Correct |
19 ms |
10964 KB |
Output is correct |
19 |
Correct |
21 ms |
10332 KB |
Output is correct |
20 |
Correct |
25 ms |
10588 KB |
Output is correct |
21 |
Correct |
32 ms |
10212 KB |
Output is correct |
22 |
Correct |
26 ms |
10332 KB |
Output is correct |
23 |
Correct |
22 ms |
10980 KB |
Output is correct |
24 |
Correct |
18 ms |
10076 KB |
Output is correct |
25 |
Correct |
26 ms |
10588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
456 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 |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 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 |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
600 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
472 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
34 ms |
10588 KB |
Output is correct |
27 |
Correct |
35 ms |
11224 KB |
Output is correct |
28 |
Correct |
35 ms |
10328 KB |
Output is correct |
29 |
Correct |
34 ms |
10076 KB |
Output is correct |
30 |
Correct |
32 ms |
10332 KB |
Output is correct |
31 |
Correct |
46 ms |
13144 KB |
Output is correct |
32 |
Correct |
41 ms |
13140 KB |
Output is correct |
33 |
Correct |
47 ms |
12928 KB |
Output is correct |
34 |
Correct |
44 ms |
13392 KB |
Output is correct |
35 |
Correct |
42 ms |
12880 KB |
Output is correct |
36 |
Correct |
44 ms |
13092 KB |
Output is correct |
37 |
Correct |
41 ms |
12908 KB |
Output is correct |
38 |
Correct |
53 ms |
12912 KB |
Output is correct |
39 |
Correct |
44 ms |
13768 KB |
Output is correct |
40 |
Correct |
43 ms |
13396 KB |
Output is correct |
41 |
Correct |
30 ms |
10332 KB |
Output is correct |
42 |
Correct |
23 ms |
10844 KB |
Output is correct |
43 |
Correct |
19 ms |
10964 KB |
Output is correct |
44 |
Correct |
21 ms |
10332 KB |
Output is correct |
45 |
Correct |
25 ms |
10588 KB |
Output is correct |
46 |
Correct |
32 ms |
10212 KB |
Output is correct |
47 |
Correct |
26 ms |
10332 KB |
Output is correct |
48 |
Correct |
22 ms |
10980 KB |
Output is correct |
49 |
Correct |
18 ms |
10076 KB |
Output is correct |
50 |
Correct |
26 ms |
10588 KB |
Output is correct |
51 |
Correct |
57 ms |
13280 KB |
Output is correct |
52 |
Correct |
55 ms |
13088 KB |
Output is correct |
53 |
Correct |
65 ms |
13408 KB |
Output is correct |
54 |
Correct |
57 ms |
12692 KB |
Output is correct |
55 |
Correct |
56 ms |
13436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
456 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 |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 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 |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
600 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
472 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
34 ms |
10588 KB |
Output is correct |
27 |
Correct |
35 ms |
11224 KB |
Output is correct |
28 |
Correct |
35 ms |
10328 KB |
Output is correct |
29 |
Correct |
34 ms |
10076 KB |
Output is correct |
30 |
Correct |
32 ms |
10332 KB |
Output is correct |
31 |
Correct |
46 ms |
13144 KB |
Output is correct |
32 |
Correct |
41 ms |
13140 KB |
Output is correct |
33 |
Correct |
47 ms |
12928 KB |
Output is correct |
34 |
Correct |
44 ms |
13392 KB |
Output is correct |
35 |
Correct |
42 ms |
12880 KB |
Output is correct |
36 |
Correct |
44 ms |
13092 KB |
Output is correct |
37 |
Correct |
41 ms |
12908 KB |
Output is correct |
38 |
Correct |
53 ms |
12912 KB |
Output is correct |
39 |
Correct |
44 ms |
13768 KB |
Output is correct |
40 |
Correct |
43 ms |
13396 KB |
Output is correct |
41 |
Correct |
30 ms |
10332 KB |
Output is correct |
42 |
Correct |
23 ms |
10844 KB |
Output is correct |
43 |
Correct |
19 ms |
10964 KB |
Output is correct |
44 |
Correct |
21 ms |
10332 KB |
Output is correct |
45 |
Correct |
25 ms |
10588 KB |
Output is correct |
46 |
Correct |
32 ms |
10212 KB |
Output is correct |
47 |
Correct |
26 ms |
10332 KB |
Output is correct |
48 |
Correct |
22 ms |
10980 KB |
Output is correct |
49 |
Correct |
18 ms |
10076 KB |
Output is correct |
50 |
Correct |
26 ms |
10588 KB |
Output is correct |
51 |
Correct |
57 ms |
13280 KB |
Output is correct |
52 |
Correct |
55 ms |
13088 KB |
Output is correct |
53 |
Correct |
65 ms |
13408 KB |
Output is correct |
54 |
Correct |
57 ms |
12692 KB |
Output is correct |
55 |
Correct |
56 ms |
13436 KB |
Output is correct |
56 |
Correct |
903 ms |
125712 KB |
Output is correct |
57 |
Correct |
896 ms |
129108 KB |
Output is correct |
58 |
Correct |
820 ms |
113764 KB |
Output is correct |
59 |
Correct |
835 ms |
116664 KB |
Output is correct |
60 |
Correct |
870 ms |
123556 KB |
Output is correct |