Submission #656584

#TimeUsernameProblemLanguageResultExecution timeMemory
656584LoboEvent Hopping (BOI22_events)C++17
0 / 100
6 ms748 KiB
#include<bits/stdc++.h> using namespace std; const long long inf = (long long) 1e18 + 10; const int inf1 = (int) 1e9 + 10; #define int long long #define dbl long double #define endl '\n' #define sc second #define fr first #define mp make_pair #define pb push_back #define all(x) x.begin(), x.end() const int maxn = 2e5+10; int n, q, idord[maxn], lf[maxn], rg[maxn], nx[maxn][25], pv[maxn][25]; pair<int,int> trmn[4*maxn],trmx[4*maxn]; void buildmx(int no, int l, int r) { trmx[no] = mp(-inf,0); if(l == r) return; int lc=2*no,rc=2*no+1,mid=(l+r)/2; buildmx(lc,l,mid); buildmx(rc,mid+1,r); } void updmx(int no, int l, int r, int pos, pair<int,int> val) { if(l > pos || r < pos) return; if(l == r) { trmx[no] = max(trmx[no],val); return; } int lc=2*no,rc=2*no+1,mid=(l+r)/2; updmx(lc,l,mid,pos,val); updmx(rc,mid+1,r,pos,val); trmx[no] = max(trmx[lc],trmx[rc]); } pair<int,int> qrymx(int no, int l, int r, int L, int R) { if(l > R || r < L) return mp(-inf,0); if(l >= L && r <= R) return trmx[no]; int lc=2*no,rc=2*no+1,mid=(l+r)/2; return max(qrymx(lc,l,mid,L,R),qrymx(rc,mid+1,r,L,R)); } void buildmn(int no, int l, int r) { trmn[no] = mp(inf,0); if(l == r) return; int lc=2*no,rc=2*no+1,mid=(l+r)/2; buildmn(lc,l,mid); buildmn(rc,mid+1,r); } void updmn(int no, int l, int r, int pos, pair<int,int> val) { if(l > pos || r < pos) return; if(l == r) { trmn[no] = min(trmn[no],val); return; } int lc=2*no,rc=2*no+1,mid=(l+r)/2; updmn(lc,l,mid,pos,val); updmn(rc,mid+1,r,pos,val); trmn[no] = min(trmn[lc],trmn[rc]); } pair<int,int> qrymn(int no, int l, int r, int L, int R) { if(l > R || r < L) return mp(inf,0); if(l >= L && r <= R) return trmn[no]; int lc=2*no,rc=2*no+1,mid=(l+r)/2; return min(qrymn(lc,l,mid,L,R),qrymn(rc,mid+1,r,L,R)); } void solve() { cin >> n >> q; vector<pair<pair<int,int>,int>> ord; vector<int> cc; for(int i = 1; i <= n; i++) { int l, r; cin >> l >> r; ord.pb(mp(mp(r,l),i)); lf[i] = l; rg[i] = r; cc.pb(lf[i]); cc.pb(rg[i]); } sort(all(cc)); cc.erase(unique(all(cc)),cc.end()); int mxv = cc.size(); for(int i = 1; i <= n; i++) { lf[i] = upper_bound(all(cc),lf[i])-cc.begin(); rg[i] = upper_bound(all(cc),rg[i])-cc.begin(); } // sort(all(ord)); buildmx(1,1,mxv); for(int i = 1; i <= n; i++) { updmx(1,1,mxv,lf[i],mp(rg[i],i)); } for(int i = 1; i <= n; i++) { nx[i][0] = 0; // for(int j = 1; j <= n; j++) { // if(lf[j] <= rg[i] && rg[j] > rg[i]) { // if(nx[i][0] == 0 || rg[j] > rg[nx[i][0]]) nx[i][0] = j; // } // } auto valnx = qrymx(1,1,mxv,1,rg[i]); if(valnx.fr == rg[i]) nx[i][0] = 0; else nx[i][0] = valnx.sc; pv[i][0] = 0; for(int j = 1; j <= n; j++) { if(j == i || !(lf[i] <= rg[j] && rg[j] <= rg[i]) || !(lf[j] < lf[i])) continue; if(pv[i][0] == 0 || lf[j] < lf[pv[i][0]]) pv[i][0] = j; } // cout << i << " " << nx[i][0] << " " << pv[i][0] << endl; } for(int j = 1; j <= 20; j++) { for(int i = 1; i <= n; i++) { nx[i][j] = nx[nx[i][j-1]][j-1]; pv[i][j] = pv[pv[i][j-1]][j-1]; } } while(q--) { int s,e; cin >> s >> e; int ans = 0; for(int j = 20; j >= 0; j--) { if(nx[s][j] != 0 && rg[nx[s][j]] < rg[e]) { ans+= (1<<j); s = nx[s][j]; } } for(int j = 20; j >= 0; j--) { if(pv[e][j] != 0 && lf[pv[e][j]] > lf[s]) { ans+= (1<<j); e = pv[e][j]; } } if(s == e) { cout << ans << endl; } else if(lf[e] <= rg[s] && rg[s] <= rg[e]) { cout << ans+1 << endl; } else if(nx[s][0] != 0 && lf[e] <= rg[nx[s][0]] && rg[nx[s][0]] <= rg[e]) { cout << ans+2 << endl; } else if(pv[e][0] != 0 && lf[pv[e][0]] <= rg[s] && rg[s] <= rg[pv[e][0]]) { cout << ans+2 << endl; } else { cout << "impossible" << endl; } } } int32_t main() { ios::sync_with_stdio(false); cin.tie(0); freopen("in.in", "r", stdin); // freopen("out.out", "w", stdout); int tt = 1; // cin >> tt; while(tt--) { solve(); } }

Compilation message (stderr)

events.cpp: In function 'int32_t main()':
events.cpp:160:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  160 |     freopen("in.in", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
#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...