#include <bits/stdc++.h>
using namespace std;
template < typename third >
class SegmentTree {
private:
third *Stree;
third *Snum;
long long n;
third NullValue = 0;
third combine(third a, third b) {
return max(a,b);
}
void build(long long idx, long long s, long long e) {
if (s > e)
return;
if (s == e) {
Stree[idx] = Snum[s];
return ;
}
long long m = ( s + e ) / 2;
build( 2 * idx, s, m);
build( 2 * idx + 1, m + 1, e);
Stree[idx] = combine(Stree[2 * idx], Stree[2 * idx + 1] );
return;
}
third queryUtil(long long idx, long long s, long long e, long long l, long long r) {
if (l > e || r < s)
return NullValue;
if (s >= l && e <= r) {
return Stree[idx];
}
long long m = ( s + e ) / 2;
third left = queryUtil(2 * idx, s, m, l, r);
third right = queryUtil(2 * idx + 1, m + 1, e, l, r);
return combine(left, right );
}
public:
SegmentTree(vector<third> a) {
this->n = a.size();
Stree = new third[4 * n + 5];
Snum = new third[n];
for (long long i = 0; i < n; i++) {
Snum[i] = a[i];
}
build(1, 0, n - 1);
}
third query(long long l, long long r) {
return queryUtil(1, 0, n - 1, l - 1, r - 1);
}
};
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
long long n;
cin >> n;
long long m;
cin >> m;
vector<long long> a(n);
for(long long i = 0 ; i < n ; i++) {
cin >> a[i];
}
vector<long long> b(m);
for(long long i = 0 ; i < m ; i++) {
cin >> b[i];
}
vector<long long> diff(1000005,0);
SegmentTree<long long> ST(a);
map<long long,long long> last;
for(long long i = 0 ; i < a.size() ; i++) {
last[a[i]]=-1;
}
for(long long i=0; i<n; i++) {
long long ryt=0;
long long l=i;
long long r=a.size()-1;
while(l<=r) {
if(l==r) {
if(ST.query(i+1,l+1)<=a[i]) {
ryt=max(ryt,l);
}
break;
}
long long m=(l+r)/2;
if(ST.query(i+1,m+1)>a[i]) {
r=m-1;
} else {
l=m+1;
ryt=max(ryt,m);
}
}
ryt=ryt-i+1;
ryt--;
long long left=1e9;
l=last[a[i]]+1;
r=i;
while(l<=r) {
if(l==r) {
if(ST.query(l+1,i+1)<=a[i]) {
left=min(left,l);
}
break;
}
long long m=(l+r)/2;
if(ST.query(m+1,i+1)>a[i]) {
l=m+1;
} else {
r=m-1;
left=min(left,m);
}
}
left=i-left;
last[a[i]]=i;
diff[a[i]]+=((left+1)*(ryt+1));
}
for(long long i=1; i<diff.size(); i++) {
diff[i]+=diff[i-1];
}
for(long long i = 0 ; i < b.size() ; i++) {
cout << diff[b[i]] << '\n';
}
return 0;
}
Compilation message
pilot.cpp: In function 'int32_t main()':
pilot.cpp:137:29: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
137 | for(long long i = 0 ; i < a.size() ; i++) {
| ~~^~~~~~~~~~
pilot.cpp:229:25: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
229 | for(long long i=1; i<diff.size(); i++) {
| ~^~~~~~~~~~~~
pilot.cpp:235:29: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
235 | for(long long i = 0 ; i < b.size() ; i++) {
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
8176 KB |
Output is correct |
2 |
Correct |
8 ms |
8172 KB |
Output is correct |
3 |
Correct |
8 ms |
8172 KB |
Output is correct |
4 |
Correct |
8 ms |
8172 KB |
Output is correct |
5 |
Correct |
8 ms |
8172 KB |
Output is correct |
6 |
Correct |
9 ms |
8172 KB |
Output is correct |
7 |
Correct |
8 ms |
8172 KB |
Output is correct |
8 |
Correct |
8 ms |
8172 KB |
Output is correct |
9 |
Correct |
9 ms |
8172 KB |
Output is correct |
10 |
Correct |
8 ms |
8172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
8176 KB |
Output is correct |
2 |
Correct |
8 ms |
8172 KB |
Output is correct |
3 |
Correct |
8 ms |
8172 KB |
Output is correct |
4 |
Correct |
8 ms |
8172 KB |
Output is correct |
5 |
Correct |
8 ms |
8172 KB |
Output is correct |
6 |
Correct |
9 ms |
8172 KB |
Output is correct |
7 |
Correct |
8 ms |
8172 KB |
Output is correct |
8 |
Correct |
8 ms |
8172 KB |
Output is correct |
9 |
Correct |
9 ms |
8172 KB |
Output is correct |
10 |
Correct |
8 ms |
8172 KB |
Output is correct |
11 |
Correct |
8 ms |
8172 KB |
Output is correct |
12 |
Correct |
14 ms |
8172 KB |
Output is correct |
13 |
Correct |
8 ms |
8172 KB |
Output is correct |
14 |
Correct |
8 ms |
8320 KB |
Output is correct |
15 |
Correct |
8 ms |
8172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
8176 KB |
Output is correct |
2 |
Correct |
8 ms |
8172 KB |
Output is correct |
3 |
Correct |
8 ms |
8172 KB |
Output is correct |
4 |
Correct |
8 ms |
8172 KB |
Output is correct |
5 |
Correct |
8 ms |
8172 KB |
Output is correct |
6 |
Correct |
9 ms |
8172 KB |
Output is correct |
7 |
Correct |
8 ms |
8172 KB |
Output is correct |
8 |
Correct |
8 ms |
8172 KB |
Output is correct |
9 |
Correct |
9 ms |
8172 KB |
Output is correct |
10 |
Correct |
8 ms |
8172 KB |
Output is correct |
11 |
Correct |
8 ms |
8172 KB |
Output is correct |
12 |
Correct |
14 ms |
8172 KB |
Output is correct |
13 |
Correct |
8 ms |
8172 KB |
Output is correct |
14 |
Correct |
8 ms |
8320 KB |
Output is correct |
15 |
Correct |
8 ms |
8172 KB |
Output is correct |
16 |
Correct |
8 ms |
8172 KB |
Output is correct |
17 |
Correct |
8 ms |
8320 KB |
Output is correct |
18 |
Correct |
14 ms |
8188 KB |
Output is correct |
19 |
Correct |
8 ms |
8300 KB |
Output is correct |
20 |
Correct |
8 ms |
8172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
8176 KB |
Output is correct |
2 |
Correct |
8 ms |
8172 KB |
Output is correct |
3 |
Correct |
8 ms |
8172 KB |
Output is correct |
4 |
Correct |
8 ms |
8172 KB |
Output is correct |
5 |
Correct |
8 ms |
8172 KB |
Output is correct |
6 |
Correct |
9 ms |
8172 KB |
Output is correct |
7 |
Correct |
8 ms |
8172 KB |
Output is correct |
8 |
Correct |
8 ms |
8172 KB |
Output is correct |
9 |
Correct |
9 ms |
8172 KB |
Output is correct |
10 |
Correct |
8 ms |
8172 KB |
Output is correct |
11 |
Correct |
8 ms |
8172 KB |
Output is correct |
12 |
Correct |
14 ms |
8172 KB |
Output is correct |
13 |
Correct |
8 ms |
8172 KB |
Output is correct |
14 |
Correct |
8 ms |
8320 KB |
Output is correct |
15 |
Correct |
8 ms |
8172 KB |
Output is correct |
16 |
Correct |
8 ms |
8172 KB |
Output is correct |
17 |
Correct |
8 ms |
8320 KB |
Output is correct |
18 |
Correct |
14 ms |
8188 KB |
Output is correct |
19 |
Correct |
8 ms |
8300 KB |
Output is correct |
20 |
Correct |
8 ms |
8172 KB |
Output is correct |
21 |
Correct |
13 ms |
8300 KB |
Output is correct |
22 |
Correct |
11 ms |
8300 KB |
Output is correct |
23 |
Correct |
12 ms |
8300 KB |
Output is correct |
24 |
Correct |
11 ms |
8300 KB |
Output is correct |
25 |
Correct |
13 ms |
8300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
785 ms |
16188 KB |
Output is correct |
2 |
Correct |
701 ms |
18404 KB |
Output is correct |
3 |
Correct |
645 ms |
15876 KB |
Output is correct |
4 |
Correct |
633 ms |
17700 KB |
Output is correct |
5 |
Correct |
668 ms |
16016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
587 ms |
20360 KB |
Output is correct |
2 |
Correct |
597 ms |
20428 KB |
Output is correct |
3 |
Correct |
564 ms |
20304 KB |
Output is correct |
4 |
Correct |
632 ms |
20728 KB |
Output is correct |
5 |
Correct |
574 ms |
20152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
610 ms |
20732 KB |
Output is correct |
2 |
Correct |
576 ms |
20496 KB |
Output is correct |
3 |
Correct |
566 ms |
20512 KB |
Output is correct |
4 |
Correct |
623 ms |
21208 KB |
Output is correct |
5 |
Correct |
586 ms |
20860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
8176 KB |
Output is correct |
2 |
Correct |
8 ms |
8172 KB |
Output is correct |
3 |
Correct |
8 ms |
8172 KB |
Output is correct |
4 |
Correct |
8 ms |
8172 KB |
Output is correct |
5 |
Correct |
8 ms |
8172 KB |
Output is correct |
6 |
Correct |
9 ms |
8172 KB |
Output is correct |
7 |
Correct |
8 ms |
8172 KB |
Output is correct |
8 |
Correct |
8 ms |
8172 KB |
Output is correct |
9 |
Correct |
9 ms |
8172 KB |
Output is correct |
10 |
Correct |
8 ms |
8172 KB |
Output is correct |
11 |
Correct |
785 ms |
16188 KB |
Output is correct |
12 |
Correct |
701 ms |
18404 KB |
Output is correct |
13 |
Correct |
645 ms |
15876 KB |
Output is correct |
14 |
Correct |
633 ms |
17700 KB |
Output is correct |
15 |
Correct |
668 ms |
16016 KB |
Output is correct |
16 |
Correct |
638 ms |
16016 KB |
Output is correct |
17 |
Correct |
703 ms |
18276 KB |
Output is correct |
18 |
Correct |
740 ms |
18396 KB |
Output is correct |
19 |
Correct |
636 ms |
15768 KB |
Output is correct |
20 |
Correct |
711 ms |
18164 KB |
Output is correct |
21 |
Correct |
639 ms |
15896 KB |
Output is correct |
22 |
Correct |
673 ms |
17940 KB |
Output is correct |
23 |
Correct |
698 ms |
16356 KB |
Output is correct |
24 |
Correct |
661 ms |
17688 KB |
Output is correct |
25 |
Correct |
662 ms |
16116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
8176 KB |
Output is correct |
2 |
Correct |
8 ms |
8172 KB |
Output is correct |
3 |
Correct |
8 ms |
8172 KB |
Output is correct |
4 |
Correct |
8 ms |
8172 KB |
Output is correct |
5 |
Correct |
8 ms |
8172 KB |
Output is correct |
6 |
Correct |
9 ms |
8172 KB |
Output is correct |
7 |
Correct |
8 ms |
8172 KB |
Output is correct |
8 |
Correct |
8 ms |
8172 KB |
Output is correct |
9 |
Correct |
9 ms |
8172 KB |
Output is correct |
10 |
Correct |
8 ms |
8172 KB |
Output is correct |
11 |
Correct |
8 ms |
8172 KB |
Output is correct |
12 |
Correct |
14 ms |
8172 KB |
Output is correct |
13 |
Correct |
8 ms |
8172 KB |
Output is correct |
14 |
Correct |
8 ms |
8320 KB |
Output is correct |
15 |
Correct |
8 ms |
8172 KB |
Output is correct |
16 |
Correct |
8 ms |
8172 KB |
Output is correct |
17 |
Correct |
8 ms |
8320 KB |
Output is correct |
18 |
Correct |
14 ms |
8188 KB |
Output is correct |
19 |
Correct |
8 ms |
8300 KB |
Output is correct |
20 |
Correct |
8 ms |
8172 KB |
Output is correct |
21 |
Correct |
13 ms |
8300 KB |
Output is correct |
22 |
Correct |
11 ms |
8300 KB |
Output is correct |
23 |
Correct |
12 ms |
8300 KB |
Output is correct |
24 |
Correct |
11 ms |
8300 KB |
Output is correct |
25 |
Correct |
13 ms |
8300 KB |
Output is correct |
26 |
Correct |
785 ms |
16188 KB |
Output is correct |
27 |
Correct |
701 ms |
18404 KB |
Output is correct |
28 |
Correct |
645 ms |
15876 KB |
Output is correct |
29 |
Correct |
633 ms |
17700 KB |
Output is correct |
30 |
Correct |
668 ms |
16016 KB |
Output is correct |
31 |
Correct |
587 ms |
20360 KB |
Output is correct |
32 |
Correct |
597 ms |
20428 KB |
Output is correct |
33 |
Correct |
564 ms |
20304 KB |
Output is correct |
34 |
Correct |
632 ms |
20728 KB |
Output is correct |
35 |
Correct |
574 ms |
20152 KB |
Output is correct |
36 |
Correct |
610 ms |
20732 KB |
Output is correct |
37 |
Correct |
576 ms |
20496 KB |
Output is correct |
38 |
Correct |
566 ms |
20512 KB |
Output is correct |
39 |
Correct |
623 ms |
21208 KB |
Output is correct |
40 |
Correct |
586 ms |
20860 KB |
Output is correct |
41 |
Correct |
638 ms |
16016 KB |
Output is correct |
42 |
Correct |
703 ms |
18276 KB |
Output is correct |
43 |
Correct |
740 ms |
18396 KB |
Output is correct |
44 |
Correct |
636 ms |
15768 KB |
Output is correct |
45 |
Correct |
711 ms |
18164 KB |
Output is correct |
46 |
Correct |
639 ms |
15896 KB |
Output is correct |
47 |
Correct |
673 ms |
17940 KB |
Output is correct |
48 |
Correct |
698 ms |
16356 KB |
Output is correct |
49 |
Correct |
661 ms |
17688 KB |
Output is correct |
50 |
Correct |
662 ms |
16116 KB |
Output is correct |
51 |
Correct |
686 ms |
18296 KB |
Output is correct |
52 |
Correct |
704 ms |
20080 KB |
Output is correct |
53 |
Correct |
713 ms |
18440 KB |
Output is correct |
54 |
Correct |
700 ms |
20096 KB |
Output is correct |
55 |
Correct |
706 ms |
18500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
8176 KB |
Output is correct |
2 |
Correct |
8 ms |
8172 KB |
Output is correct |
3 |
Correct |
8 ms |
8172 KB |
Output is correct |
4 |
Correct |
8 ms |
8172 KB |
Output is correct |
5 |
Correct |
8 ms |
8172 KB |
Output is correct |
6 |
Correct |
9 ms |
8172 KB |
Output is correct |
7 |
Correct |
8 ms |
8172 KB |
Output is correct |
8 |
Correct |
8 ms |
8172 KB |
Output is correct |
9 |
Correct |
9 ms |
8172 KB |
Output is correct |
10 |
Correct |
8 ms |
8172 KB |
Output is correct |
11 |
Correct |
8 ms |
8172 KB |
Output is correct |
12 |
Correct |
14 ms |
8172 KB |
Output is correct |
13 |
Correct |
8 ms |
8172 KB |
Output is correct |
14 |
Correct |
8 ms |
8320 KB |
Output is correct |
15 |
Correct |
8 ms |
8172 KB |
Output is correct |
16 |
Correct |
8 ms |
8172 KB |
Output is correct |
17 |
Correct |
8 ms |
8320 KB |
Output is correct |
18 |
Correct |
14 ms |
8188 KB |
Output is correct |
19 |
Correct |
8 ms |
8300 KB |
Output is correct |
20 |
Correct |
8 ms |
8172 KB |
Output is correct |
21 |
Correct |
13 ms |
8300 KB |
Output is correct |
22 |
Correct |
11 ms |
8300 KB |
Output is correct |
23 |
Correct |
12 ms |
8300 KB |
Output is correct |
24 |
Correct |
11 ms |
8300 KB |
Output is correct |
25 |
Correct |
13 ms |
8300 KB |
Output is correct |
26 |
Correct |
785 ms |
16188 KB |
Output is correct |
27 |
Correct |
701 ms |
18404 KB |
Output is correct |
28 |
Correct |
645 ms |
15876 KB |
Output is correct |
29 |
Correct |
633 ms |
17700 KB |
Output is correct |
30 |
Correct |
668 ms |
16016 KB |
Output is correct |
31 |
Correct |
587 ms |
20360 KB |
Output is correct |
32 |
Correct |
597 ms |
20428 KB |
Output is correct |
33 |
Correct |
564 ms |
20304 KB |
Output is correct |
34 |
Correct |
632 ms |
20728 KB |
Output is correct |
35 |
Correct |
574 ms |
20152 KB |
Output is correct |
36 |
Correct |
610 ms |
20732 KB |
Output is correct |
37 |
Correct |
576 ms |
20496 KB |
Output is correct |
38 |
Correct |
566 ms |
20512 KB |
Output is correct |
39 |
Correct |
623 ms |
21208 KB |
Output is correct |
40 |
Correct |
586 ms |
20860 KB |
Output is correct |
41 |
Correct |
638 ms |
16016 KB |
Output is correct |
42 |
Correct |
703 ms |
18276 KB |
Output is correct |
43 |
Correct |
740 ms |
18396 KB |
Output is correct |
44 |
Correct |
636 ms |
15768 KB |
Output is correct |
45 |
Correct |
711 ms |
18164 KB |
Output is correct |
46 |
Correct |
639 ms |
15896 KB |
Output is correct |
47 |
Correct |
673 ms |
17940 KB |
Output is correct |
48 |
Correct |
698 ms |
16356 KB |
Output is correct |
49 |
Correct |
661 ms |
17688 KB |
Output is correct |
50 |
Correct |
662 ms |
16116 KB |
Output is correct |
51 |
Correct |
686 ms |
18296 KB |
Output is correct |
52 |
Correct |
704 ms |
20080 KB |
Output is correct |
53 |
Correct |
713 ms |
18440 KB |
Output is correct |
54 |
Correct |
700 ms |
20096 KB |
Output is correct |
55 |
Correct |
706 ms |
18500 KB |
Output is correct |
56 |
Execution timed out |
1099 ms |
97344 KB |
Time limit exceeded |
57 |
Halted |
0 ms |
0 KB |
- |