Submission #794691

#TimeUsernameProblemLanguageResultExecution timeMemory
794691AntekbRailway Trip (JOI17_railway_trip)C++17
100 / 100
1010 ms44524 KiB
#include<bits/stdc++.h> #pragma GCC optimize("Ofast") //#pragma GCC optimize("trapv") #define st first #define nd second #define pb push_back #define eb emplace_back #define pp(x) pop_back(x) #define mp(a, b) make_pair(a, b) #define all(x) (x).begin(), (x).end() #define rev(x) reverse(all(x)) #define sor(x) sort(all(x)) #define sz(x) (int)(x).size() #define rsz(x) resize(x) using namespace std; ///~~~~~~~~~~~~~~~~~~~~~~~~~~ template <typename H, typename T> ostream& operator<<(ostream& os, pair<H, T> m){ return os <<"("<< m.st<<", "<<m.nd<<")"; } template <typename H> ostream& operator<<(ostream& os, vector<H> V){ os<<"{"; for(int i=0; i<V.size(); i++){ if(i)os<<" "; os<<V[i]; } os<<"}"; return os; } void debug(){cerr<<"\n";} template <typename H, typename... T> void debug(H h, T... t) {cerr<<h; if (sizeof...(t)) cerr << ", "; debug(t...);} #define deb(x...) cerr<<#x<<" = ";debug(x); //#define deb(x...) ; ///~~~~~~~~~~~~~~~~~~~~~~~~~ typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<pii > vii; typedef vector<ll> vl; typedef vector<pll> vll; typedef string str; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #define BOOST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); const int K=17, N=1<<K, INF=1e9+5, mod2=1e9+7, mod=998244353; struct modint{ int n=0; modint(){} modint(ll x){ n=x%mod; if(n<0)n+=mod; } operator int(){ return n; } modint operator+(modint a){a.n = n+a.n; if(a.n>=mod)a.n-=mod;return a;} modint operator+=(modint a){return (*this)=(*this)+a;} modint operator-(modint a){a.n = n-a.n; if(a.n<0)a.n+=mod;return a;} modint operator-=(modint a){return (*this)=(*this)-a;} modint operator*(modint a){a.n = (n*1ll*a.n)%mod; return a;} modint operator*=(modint a){return (*this)=(*this)*a;} modint operator^(const ll &m)const{ modint a(1); if(m==0)return a; if(m==1)return (*this); a=(*this)^(m/2); a*=a; return a*((*this)^(m&1)); } modint odw(){ return (*this)^((ll)mod-2); } modint operator/(modint a){return (*this)*a.odw();} modint operator/=(modint a){return (*this)=(*this)/a;} bool operator==(modint a){return a.n==n;} friend ostream& operator<<(ostream& os, modint m) { return os << m.n; } }; /*modint fact[N], fact2[N]; typedef vector<modint> vm; void factinit(){ fact[0]=1; for(int i=1; i<N; i++){ fact[i]=(fact[i-1]*modint(i))%mod; } fact2[N-1]=fact[N-1].odw(); for(int i=N-2; i>=0; i--){ fact2[i]=(fact2[i+1]*modint(i+1))%mod; } } modint npok(int _n, int _k){ if(_n<_k)return 0; return fact[_n]*fact2[_k]*fact2[_n-_k]; }*/ int los(int a, int b){ return a+(rng())%(b-a+1); } vector<int> typ[N+N], tab[N+N];//permutcja, stale int maks[N+N]; vector<int> gdzie[N]; int get(int l, int r){ int a=0; for(l+=N, r+=N; l<r; l>>=1, r>>=1){ if(l&1)a=max(a, maks[l++]); if(r&1)a=max(a, maks[--r]); } return a; } pair<vi, vi> zloz(vi &A, vi& c1, vi &B, vi& c2){ //A(B(i)) - B glebiej vi C(3), c3(3); for(int i=0; i<3; i++){ C[i]=A[B[i]]; c3[i]=c2[i]+c1[B[i]]; } return mp(C, c3); } void prop(int v){ if(v>=N)return; int l=v+v, r=l+1; auto a=zloz(typ[v], tab[v], typ[l], tab[l]); typ[l]=a.st; tab[l]=a.nd; a=zloz(typ[v], tab[v], typ[r], tab[r]); typ[r]=a.st; tab[r]=a.nd; typ[v]={0, 1, 2}; tab[v]={0, 0, 0}; } void update(int v, int L, int R, int l, int r, vi &A, vi &c){ if(L==l && r==R){ auto a=zloz(A, c, typ[v], tab[v]); typ[v]=a.st; tab[v]=a.nd; return; } int M=(L+R)>>1; prop(v); if(l<=M)update(v+v, L, M, l, min(M, r), A, c); if(r>M)update(v+v+1, M+1, R, max(M+1, l), r, A, c); } void upd(int l, int r, int a, int b){ if(l>r)return; vi A(3); vi c(3); for(int i=0; i<3; i++){ int L=a, R=b+i-1; if(L>R+1)L=R+1; if(L+1<R)R=L+1; c[i]=L; A[i]=R-L+1; } update(1, 0, N-1, l, r, A, c); } pii val(int v){ int c=0, t=1; v+=N; for(;v>0; v>>=1){ c+=tab[v][t]; t=typ[v][t]; } return mp(c, c+t-1); } int main(){ //factinit(); //BOOST int n, k, q; cin>>n>>k>>q; set<int> S; for(int i=0; i<n; i++){ S.insert(i); cin>>maks[N+i]; gdzie[maks[N+i]].pb(i); } for(int i=N-1; i>0; i--){ maks[i]=max(maks[i+i], maks[i+i+1]); } for(int i=0; i<N+N; i++){ typ[i]={0, 1, 2}; tab[i]={0, 0, 0}; } vector<pair<pii, pii>> Q; for(int i=0; i<q; i++){ int a, b; cin>>a>>b; a--; b--; if(a>b)swap(a, b); int m=get(a, b+1); //deb(a, b, m); Q.eb(mp(m, i), mp(a, b)); } sor(Q); int wskq=0, wskq2=0; vi ans(q); for(int i=1; i<=k; i++){ //deb(i); while(wskq<q && Q[wskq].st.st==i){ int a=Q[wskq].nd.st, b=Q[wskq].nd.nd; ans[Q[wskq].st.nd]=val(a).nd+val(b).st+upper_bound(all(gdzie[i]), b)-lower_bound(all(gdzie[i]), a)-1; //deb(Q[wskq]); wskq++; } if(i<k){ vector<int> co; for(int j=0; j<gdzie[i].size(); j++){ co.pb(gdzie[i][j]); S.erase(gdzie[i][j]); if(j==gdzie[i].size()-1 || *S.lower_bound(gdzie[i][j])!=gdzie[i][j+1]){ int L=*(--S.lower_bound(gdzie[i][j])); int R=*(S.lower_bound(gdzie[i][j])); upd(L+1, co[0]-1, 0, co.size()); for(int l=0; l<co.size()-1; l++){ upd(co[l]+1, co[l+1]-1, l+1, co.size()-l-1); } upd(co.back()+1, R-1, co.size(), 0); for(int l=0; l<co.size(); l++){ upd(co[l], co[l], l+1, co.size()-l); } co.clear(); } } while(wskq2<q && Q[wskq2].st.st==i){ int a=Q[wskq2].nd.st, b=Q[wskq2].nd.nd; ans[Q[wskq2].st.nd]=min(ans[Q[wskq2].st.nd], val(a).st+val(b).nd+1); wskq2++; } } /*for(int j=0; j<n; j++){ if(maks[j+N]<=i){ deb(val(j)); } }*/ } for(int i=0; i<q; i++){ cout<<ans[i]-1<<"\n"; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...