Submission #1098765

#TimeUsernameProblemLanguageResultExecution timeMemory
1098765TrinhKhanhDungRailway Trip 2 (JOI22_ho_t4)C++14
0 / 100
203 ms33156 KiB
#include <bits/stdc++.h> #define ll long long #define fi first #define se second #define sz(x) (int)x.size() #define ALL(v) v.begin(),v.end() #define MASK(k) (1LL << (k)) #define BIT(x, i) (((x) >> (i)) & 1) #define oo (ll)1e18 #define INF (ll)1e9 #define MOD (ll)(1e9 + 7) using namespace std; template<class T1, class T2> bool maximize(T1 &a, T2 b){if(a < b){a = b; return true;} return false;} template<class T1, class T2> bool minimize(T1 &a, T2 b){if(a > b){a = b; return true;} return false;} template<class T1, class T2> void add(T1 &a, T2 b){a += b; if(a >= MOD) a -= MOD;} template<class T1, class T2> void sub(T1 &a, T2 b){a -= b; if(a < 0) a += MOD;} template<class T> void cps(T &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());} struct seg1{ vector<int> val; int n; void build(int i, int l, int r){ if(l == r){ val[i] = l; return; } int m = (l + r) >> 1; build(i * 2, l, m); build(i * 2 + 1, m + 1, r); } seg1(int _n = 0){ n = _n; val.resize(n * 4 + 3, INF); build(1, 1, n); } void push(int i){ minimize(val[i * 2], val[i]); minimize(val[i * 2 + 1], val[i]); } void upd(int i, int l, int r, int u, int v, int c){ if(r < u || v < l) return; if(u <= l && r <= v){ minimize(val[i], c); return; } push(i); int m = (l + r) >> 1; upd(i * 2, l, m, u, v, c); upd(i * 2 + 1, m + 1, r, u, v, c); } void upd(int u, int v, int c){ upd(1, 1, n, u, v, c); } int get(int p){ int i = 1, l = 1, r = n; while(l < r){ push(i); int m = (l + r) >> 1; if(p <= m){ i = i * 2; r = m; } else{ i = i * 2 + 1; l = m + 1; } } return val[i]; } }; struct seg2{ vector<int> val; int n; void build(int i, int l, int r){ if(l == r){ val[i] = l; return; } int m = (l + r) >> 1; build(i * 2, l, m); build(i * 2 + 1, m + 1, r); } seg2(int _n = 0){ n = _n; val.resize(n * 4 + 3, 0); build(1, 1, n); } void push(int i){ maximize(val[i * 2], val[i]); maximize(val[i * 2 + 1], val[i]); } void upd(int i, int l, int r, int u, int v, int c){ if(r < u || v < l) return; if(u <= l && r <= v){ maximize(val[i], c); return; } push(i); int m = (l + r) >> 1; upd(i * 2, l, m, u, v, c); upd(i * 2 + 1, m + 1, r, u, v, c); } void upd(int u, int v, int c){ upd(1, 1, n, u, v, c); } int get(int p){ int i = 1, l = 1, r = n; while(l < r){ push(i); int m = (l + r) >> 1; if(p <= m){ i = i * 2; r = m; } else{ i = i * 2 + 1; l = m + 1; } } return val[i]; } }; const int maxN = 1e5 + 10, LOG = 17; int N, K, M, Q; int L[maxN][LOG], R[maxN][LOG]; pair<int, int> travel[maxN][LOG]; int getL(int l, int r){ int k = 31 - __builtin_clz(r - l + 1); return min(L[l][k], R[r - MASK(k) + 1][k]); } int getR(int l, int r){ int k = 31 - __builtin_clz(r - l + 1); return max(R[l][k], R[r - MASK(k) + 1][k]); } void solve(){ cin >> N >> K; cin >> M; seg1 it1(N); seg2 it2(N); for(int i = 1; i <= M; i++){ int u, v; cin >> u >> v; if(u <= v){ it2.upd(u, min(v, u + K - 1), v); } else{ it1.upd(max(v, u - K + 1), u, v); } } for(int i = 1; i <= N; i++){ L[i][0] = it1.get(i); R[i][0] = it2.get(i); travel[i][0] = {L[i][0], R[i][0]}; } for(int t = 1; t < LOG; t++){ for(int j = 1; j < LOG; j++){ for(int i = 1; i + MASK(j) <= N + 1; i++){ L[i][j] = min(L[i][j - 1], L[i + MASK(j - 1)][j - 1]); R[i][j] = max(R[i][j - 1], R[i + MASK(j - 1)][j - 1]); } } for(int i = 1; i <= N; i++){ int l = travel[i][t - 1].fi, r = travel[i][t - 1].se; travel[i][t] = {getL(l, r), getR(l, r)}; } for(int i = 1; i <= N; i++){ L[i][0] = travel[i][t].fi; R[i][0] = travel[i][t].se; } } cin >> Q; while(Q--){ int s, t; cin >> s >> t; if(t < travel[s][LOG - 1].fi || t > travel[s][LOG - 1].se){ cout << -1 << '\n'; } else{ int ans = 1; for(int i = LOG - 1; i >= 0; i--){ if(t < travel[s][i].fi || t > travel[s][i].se){ ans += MASK(i); } } cout << ans << '\n'; } } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); // freopen("partner.inp","r",stdin); // freopen("partner.out","w",stdout); int t = 1; // cin >> t; while(t--){ solve(); } 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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...