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;
const int N=1e6+5;
int n,q;
vector<pair<int,int>> a;
vector<pair<int,long long>> ans;
set<pair<int,int>> s;
void calc(int v){
long long sum=0;
for(auto [l,r]:s){
long long cnt=r-l+1;
sum+=(cnt)*(cnt+1)/2;
}
ans.emplace_back(v,sum);
}
int main(){
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> q;
for(int i=1;i<=n;++i){
int x;
cin >> x;
a.emplace_back(x,i);
}
sort(a.begin(),a.end());
int idx=0;
int pre=a[0].first;
ans.emplace_back(0,0);
for(auto [v,i]:a){
if(pre!=v){
calc(pre);
pre=v;
}
int l=i,r=i;
auto it=s.lower_bound({i,i});
if(it!=s.begin()){
it--;
if(it->second==i-1){
l=it->first;
s.erase(it);
};
}
it=s.upper_bound({i,i});
if(it!=s.end() && it->first==i+1){
r=it->second;
s.erase(it);
}
s.insert({l,r});
}
calc(pre);
for(int i=1;i<=q;++i){
int x;
cin >> x;
auto it=upper_bound(ans.begin(),ans.end(),make_pair(x,(long long)1e18));
it--;
cout << it->second << '\n';
}
}
Compilation message (stderr)
pilot.cpp: In function 'void calc(int)':
pilot.cpp:15:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
15 | for(auto [l,r]:s){
| ^
pilot.cpp: In function 'int main()':
pilot.cpp:34:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
34 | for(auto [v,i]:a){
| ^
pilot.cpp:31:9: warning: unused variable 'idx' [-Wunused-variable]
31 | int idx=0;
| ^~~
# | 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... |