Submission #1361294

#TimeUsernameProblemLanguageResultExecution timeMemory
1361294SmuggingSpunEvent Hopping (BOI22_events)C++20
100 / 100
98 ms24560 KiB
#include<bits/stdc++.h>
#define taskname "B"
using namespace std;
template<class T>void maximize(T& a, T b){
  if(a < b){
    a = b;
  }
}
const int lim = 1e5 + 5;
int n, q, l[lim], r[lim];
namespace sub2{
  void solve(){
    for(int _ = 0; _ < q; _++){
      int s, e;
      cin >> s >> e;
      queue<int>Q;
      Q.push(s);
      vector<int>f(n + 1, -1);
      f[s] = 0;
      while(!Q.empty()){
        int u = Q.front();
        Q.pop();
        for(int v = 1; v <= n; v++){
          if(f[v] == -1 && l[v] <= r[u] && r[v] >= r[u]){
            f[v] = f[u] + 1;
            Q.push(v);
          }
        }
      }
      if(f[e] == -1){
        cout << "impossible\n";
        continue;
      }
      cout << f[e] << "\n";
    }
  }
}
namespace sub13456{
  const int LG = 17;
  vector<int>comp(1, -1);
  int get_id(int x){
    return lower_bound(comp.begin(), comp.end(), x) - comp.begin();
  }
  int get_min_pos(int i, int j){
    return l[i] < l[j] ? i : j;
  }
  int lgv[lim << 1], up[LG][lim], spt[LG + 1][lim << 1];
  int get_spt(int l, int r){
    int k = lgv[r - l + 1];
    return get_min_pos(spt[k][l], spt[k][r - (1 << k) + 1]);
  }
  void solve(){
    lgv[0] = -1;
    for(int i = 1; i < (lim << 1); i++){
      lgv[i] = lgv[i >> 1] + 1;
    }
    comp.insert(comp.end(), l + 1, l + n + 1);
    comp.insert(comp.end(), r + 1, r + n + 1);
    sort(comp.begin(), comp.end());
    comp.resize(unique(comp.begin(), comp.end()) - comp.begin());
    int N = (l[0] = comp.size()) - 1;
    memset(spt, up[0][0] = 0, sizeof(spt));
    for(int i = 1; i <= n; i++){
      l[i] = get_id(l[i]);
      r[i] = get_id(r[i]);
      spt[0][r[i]] = get_min_pos(spt[0][r[i]], i);
    }
    for(int i = 1; i <= LG; i++){
      for(int j = 1; j + (1 << i) - 1 <= N; j++){
        spt[i][j] = get_min_pos(spt[i - 1][j], spt[i - 1][j + (1 << (i - 1))]);
      }
    }
    for(int i = 1; i <= n; i++){
      up[0][i] = get_spt(l[i], r[i]);
    }
    for(int i = 1; i < LG; i++){
      for(int j = 0; j <= n; j++){
        up[i][j] = up[i - 1][up[i - 1][j]];
      }
    }
    for(int _ = 0; _ < q; _++){
      int s, e, ans = 0;
      cin >> s >> e;
      if(s == e){
        cout << "0\n";
        continue;
      }
      if(l[e] <= r[s] && r[e] >= r[s]){
        cout << "1\n";
        continue;
      }
      if(r[s] > r[e]){
        cout << "impossible\n";
        continue;
      }
      for(int bit = LG - 1; bit > -1; bit--){
        int ne = up[bit][e];
        if(ne != 0 && l[ne] > r[s]){
          ans |= 1 << bit;
          e = ne;
        }
      }
      if(l[up[0][e]] > r[s]){
        cout << "impossible\n";
        continue;
      }
      cout << ans + 2 << "\n";
    }
  }
}
int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	if(fopen(taskname".inp", "r")){
		freopen(taskname".inp", "r", stdin);
	}
  cin >> n >> q;
  for(int i = 1; i <= n; i++){
    cin >> l[i] >> r[i];
  }
  if(n <= 1000 && q <= 100){
    sub2::solve();
  }
  else{
    sub13456::solve();
  }
}

Compilation message (stderr)

events.cpp: In function 'int main()':
events.cpp:114:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  114 |                 freopen(taskname".inp", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...