Submission #823868

#TimeUsernameProblemLanguageResultExecution timeMemory
823868CookieRailway Trip 2 (JOI22_ho_t4)C++14
100 / 100
665 ms45080 KiB
#include<bits/stdc++.h> #include<fstream> #pragma GCC optimize("Ofast,O3,unroll-loops") #pragma GCC target("avx2") using namespace std; //ifstream fin("FEEDING.INP"); //ofstream fout("FEEDING.OUT"); #define sz(a) (int)a.size() #define ll long long #define pb push_back #define forr(i, a, b) for(int i = a; i < b; i++) #define dorr(i, a, b) for(int i = a; i >= b; i--) #define ld long double #define vt vector #include<fstream> #define fi first #define se second #define pll pair<ll, ll> #define pii pair<int, int> const ll mod = 1e9 + 7; const int mxn = 2e5, mxr = 25e3, sq = 500, mxv = 1e6 + 5, mxvv = 130; struct Node{ int mn, mx; Node(){} Node(int _mn, int _mx){ mn = _mn; mx = _mx; } }; struct ST{ Node st[4 * mxn + 1]; Node comb(Node a, Node b){ return(Node(min(a.mn, b.mn), max(a.mx, b.mx))); } void upd(int nd, int l, int r, int id, Node v){ if(id < l || id > r)return; if(l == r){ st[nd] = v; return; } int mid = (l + r) >> 1; upd(nd << 1, l, mid, id, v); upd(nd << 1 | 1, mid + 1, r, id, v); st[nd] = comb(st[nd << 1], st[nd << 1 | 1]); } Node get(int nd, int l, int r, int ql, int qr){ if(ql <= l && qr >= r)return(st[nd]); int mid = (l + r) >> 1; if(qr <= mid)return(get(nd << 1, l, mid, ql, qr)); else if(ql > mid)return(get(nd << 1 | 1, mid + 1, r, ql, qr)); else return(comb(get(nd << 1, l, mid, ql, qr), get(nd << 1 | 1, mid + 1, r, ql, qr))); } }; int n, k, m; ST st[21]; int mx[4 * mxn + 1], mn[4 * mxn + 1]; void updmx(int nd, int l, int r, int ql, int qr, int v){ if(ql > r || qr < l)return; if(ql <= l && qr >= r){ mx[nd] = max(mx[nd], v); return; } int mid = (l + r) >> 1; updmx(nd << 1, l, mid, ql, qr, v); updmx(nd << 1 | 1, mid + 1, r, ql, qr, v); } int getmx(int nd, int l, int r, int id){ if(l == r)return(mx[nd]); int mid = (l + r) >> 1; if(id <= mid){ return(max(mx[nd], getmx(nd << 1, l, mid, id))); }else{ return(max(mx[nd], getmx(nd << 1 | 1, mid + 1, r, id))); } } void updmn(int nd, int l, int r, int ql, int qr, int v){ if(ql > r || qr < l)return; if(ql <= l && qr >= r){ mn[nd] = min(mn[nd], v); return; } int mid = (l + r) >> 1; updmn(nd << 1, l, mid, ql, qr, v); updmn(nd << 1 | 1, mid + 1, r, ql, qr, v); } int getmn(int nd, int l, int r, int id){ if(l == r)return(mn[nd]); int mid = (l + r) >> 1; if(id <= mid){ return(min(mn[nd], getmn(nd << 1, l, mid, id))); }else{ return(min(mn[nd], getmn(nd << 1 | 1, mid + 1, r, id))); } } int solve(int l, int r){ int atl = l, atr = l; Node can = st[18].get(1, 1, n, atl, atr); if(r >= can.mn && r <= can.mx){ int ans = 0; for(int i = 17; i >= 0; i--){ Node curr = st[i].get(1, 1, n, atl, atr); if(r >= curr.mn && r <= curr.mx){} else{ ans += (1 << i); atl = curr.mn; atr = curr.mx; } } return(ans + 1); }else{ return(-1); } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> k >> m; for(int i = 1; i <= 4 * n; i++)mn[i] = n; for(int i = 0; i < m; i++){ int l, r; cin >> l >> r; if(l < r){ updmx(1, 1, n, l, min(l + k - 1, r - 1), r); }else{ updmn(1, 1, n, max(l - k + 1, r + 1), l, r); } } for(int i = 1; i <= n; i++){ st[0].upd(1, 1, n, i, Node(min(getmn(1, 1, n, i), i), max(getmx(1, 1, n, i), i))); } for(int i = 1; i < 19; i++){ for(int j = 1; j <= n; j++){ Node curr = st[i - 1].get(1, 1, n, j, j); Node nw = st[i - 1].get(1, 1, n, curr.mn, curr.mx); st[i].upd(1, 1, n, j, nw); } } int q; cin >> q; while(q--){ int l, r; cin >> l >> r; cout << solve(l, r) << "\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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...