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;
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>st1;
tree<int,null_type,greater<int>,rb_tree_tag,tree_order_statistics_node_update>st2;
#include "books.h"
struct BIT{
vector<long long>Tree;
int n;
void init(int N){
n=N;
Tree.assign(n+3,0);
}
void update(int i,int val){
i++;
for(;i<=n;i+=i&-i)Tree[i]+=val;
}
long long get(int i){
long long res=0;
for(;i;i-=i&-i)res+=Tree[i];
return res;
}
long long gte(int i){
i++;
return get(n)-get(i);
}
long long lte(int i){
i++;
return get(i);
}
}Tree;
long long minimum_walk(vector<int> p, int s) {
int n=p.size();
Tree.init(n+5);
vector<int>seg1(n),seg2(n+1),mx1(n+1),mx2(n+1);
long long res=0;
for(int i=0;i<n;i++){
Tree.update(p[i],1);
seg1[i]=Tree.gte(i);
}
for(int i=0;i<n;i++){
Tree.update(p[i],-1);
seg2[i]=Tree.lte(i);
}
for(int i=n-1;i>=0;i--)mx1[i]=max(mx1[i+1],seg1[i]);
for(int i=0;i<n;i++)mx2[i]=max((i?mx2[i-1]:0),seg1[i]);
for(int i=0;i<n;i++){
if(seg1[i]==0&&seg2[i]==0){
if(i>=s&&mx1[i])res+=2;
else if(i<s&&mx2[i])res+=2;
}
else res+=seg1[i]+seg2[i];
}
return res;
}
# | 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... |