# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
893337 | ReLice | Railway Trip (JOI17_railway_trip) | C++14 | 0 ms | 0 KiB |
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>
#define ll long long
#define str string
#define ins insert
#define ld long double
#define pb push_back
#define pf push_front
#define pof pop_front()
#define pob pop_back()
#define lb lower_bound
#define ub upper_bound
#define endl "\n"
#define fr first
#define sc second
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define sz size()
#define vll vector<ll>
#define bc back()
#define arr array
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <class _T>
bool chmin(_T &x, const _T &y){
bool flag=false;
if(x>=y){
x=y;flag|=true;
}
return flag;
}
template <class _T>
bool chmax(_T &x, const _T &y){
bool flag=false;
if (x<=y){
x=y;flag|=true;
}
return flag;
}
#define ordered_set tree<ll, null_type,less_equal<ll>, rb_tree_tag,tree_order_statistics_node_update>
void fre(string s){freopen((s+".in").c_str(),"r",stdin);freopen((s+".out").c_str(),"w",stdout);}
void start(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
const ll inf=1e9+7;
const ll mod=1e9+7;
const ll N=3e3+5;
const ld eps=1e-9;
ll up[N][20][2];
ll p[N][20][2];
ll t[N*4];
ll a[N];
void build(ll v,ll tl,ll tr){
if(tl==tr){
t[v]=a[tl];
return;
}
ll tm=(tl+tr)/2;
build(v*2,tl,tm);
build(v*2+1,tm+1,tr);
t[v]=max(t[v*2],t[v*2+1]);
}
ll get(ll v,ll tl,ll tr,ll l,ll r){
if(tl>r || tr<l) return 0;
if(l<=tl && tr<=r)return t[v];
ll tm=(tl+tr)/2;
return max(get(v*2,tl,tm,l,r),get(v*2+1,tm+1,tr,l,r));
}
void solve(){
ll i,j;
ll n,k,q;
cin>>n>>k>>q;
vector<vll> v(k+5);
for(i=1;i<=n;i++){
cin>>a[i];
v[a[i]].pb(i);
}
build(1,1,n);
for(i=0;i<20;i++) up[0][i][0]=up[0][i][1]=inf;
for(i=1;i<=n;i++){
if(a[i]==k) {
for(j=19;j>=0;j--){
up[i][j][0]=up[i][j][1]=inf;
}
continue;
}
ll low=lb(all(v[a[i]+1]),i)-v[a].begin()-1;
if(low>=0 && low<v[a[i]+1].sz)[
p[i][0][0]=v[a[i]+1][low];
ll low2=lb(all(v[a[i]]))
up[i][0][0]=abs(i-v[a[i]+1][low]);
}
low++;
if(low>=0 && low<v[a[i]+1].sz)[
p[i][0][1]=v[a[i]+1][low];
up[i][0][1]=i-v[a[i]+1][low];
}
}
for(ll i1=k-1;i1>0;i1--){
for(i=0;i<v[i1].sz;i++){
for(j=1;j<20;j++){
ll a=v[i1][i];
p[a][j][0]=p[p[a][j-1][0]][j-1][0];
p[a][j][1]=p[p[a][j-1][1]][j-1][1];
up[a][j][0]=min(up[a][j-1][0]+up[p[a][j-1][0]][j-1][0],up[a][j-1][1]+up[p[a][j-1][1]][j-1][0]+1);
up[a][j][1]=min(up[a][j-1][0]+up[p[a][j-1][0]][j-1][1],up[a][j-1][1]+up[p[a][j-1][1]][j-1][1]+1);
}
}
}
ll x,y;
a[0]=inf;
for(i=0;i<q;i++){
cin>>x>>y;
ll ans=inf,H=get(1,1,n,x,y);
ll l=x,r=x,suml=0,sumr=0;
for(j=20;j>=0;j--){
if(abs(H+1-a[l])>=(1ll<<j) && abs(H-a[r]>=(1ll<<j))){
suml+=up[l][j];
sumr+=up[r][j];
l=p[l][j];
r=p[r][j];
}
}
for(j=20;j>=0;j--){
if(abs(H-a[l]+1)>=(1ll<<j))
}
}
}
signed main(){
start();
ll t=1;
//cin>>t;
while(t--) solve();
return 0;
}
/*
*/