제출 #376954

#제출 시각아이디문제언어결과실행 시간메모리
376954YJURailway Trip (JOI17_railway_trip)C++14
100 / 100
366 ms51564 KiB
#include<bits/stdc++.h> #pragma GCC optimize("unroll-loops,no-stack-protector,Ofast") using namespace std; typedef long long ll; typedef long double ld; typedef pair<ll,ll> pll; const ll MOD=1e9+7; const ll MOD2=998244353; const ll N=4e5+5; const ld pi=acos(-1); const ll INF=(1LL<<60); #define SQ(i) ((i)*(i)) #define REP(i,n) for(ll i=0;i<n;i++) #define REP1(i,n) for(ll i=1;i<=n;i++) #define pb push_back #define mp make_pair #define X first #define Y second #define setp setprecision #define lwb lower_bound #define SZ(_a) (ll)_a.size() set<ll> s; ll n,m,q,u[N]; vector<ll> in[N]; ll r[N][20],l[N][20]; int main(){ ios_base::sync_with_stdio(0);cin.tie(0); cin>>n>>m>>q; REP1(i,n){cin>>u[i];in[u[i]].pb(i);} REP(j,20)r[n][j]=n,l[1][j]=1; for(int c=m;c>=1;c--){ for(auto j:in[c]){ s.insert(j); } for(auto j:in[c]){ r[j][0]=(s.lwb(j+1)==s.end()?n:*s.lwb(j+1)); l[j][0]=(s.lwb(j)==s.begin()?1:*prev(s.lwb(j))); } } for(int j=1;j<20;j++){ REP1(i,n){ r[i][j]=max(r[r[i][j-1]][j-1],r[l[i][j-1]][j-1]); l[i][j]=min(l[l[i][j-1]][j-1],l[r[i][j-1]][j-1]); } } while(q--){ ll ans=0,a,b; cin>>a>>b; if(a>b)swap(a,b); ll L,R; L=R=a; for(int p=18;p>=0;p--){ if(max(r[L][p],r[R][p])<b){ ll TL=L,TR=R; ans+=(1<<p); L=min(l[TL][p],l[TR][p]),R=max(r[TR][p],r[TL][p]); } } ll mid=R; L=R=b; for(int p=18;p>=0;p--){ if(min(l[L][p],l[R][p])>mid){ ll TL=L,TR=R; ans+=(1<<p); L=min(l[TL][p],l[TR][p]),R=max(r[TR][p],r[TL][p]); } } cout<<ans<<"\n"; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...