This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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++) {
| ~~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |