Submission #593530

# Submission time Handle Problem Language Result Execution time Memory
593530 2022-07-11T10:55:30 Z slime Event Hopping (BOI22_events) C++14
25 / 100
1500 ms 75060 KB
#include "bits/stdc++.h"
using namespace std;
#define int long long
const int MAXN = 3e5 + 10;
const int MOD = 1e9 + 7;
#define ll __int128
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
int rnd(int x, int y) {
  int u = uniform_int_distribution<int>(x, y)(rng); return u;
}
ll read() { int x; cin >> x; return (ll)x; }
long long bm(long long b, long long p) {
  if(p==0) return 1 % MOD;
  long long r = bm(b, p >> 1);
  if(p&1) return (((r*r) % MOD) * b) % MOD;
  return (r*r) % MOD;
}
long long inv(long long b) { 
  return bm(b, MOD-2);
}
long long f[MAXN];
long long nCr(int n, int r) { 
  long long ans = f[n]; ans *= inv(f[r]); ans %= MOD;
  ans *= inv(f[n-r]); ans %= MOD; return ans;
}
long long fib[MAXN], lucas[MAXN];
void precomp() { 
  for(int i=0; i<MAXN; i++) f[i] = (i == 0 ? 1 % MOD : (f[i-1] * i) % MOD); 
  lucas[0] = 2;
  lucas[1] = 1;
  for(int i=2; i<MAXN; i++) lucas[i] = (lucas[i-2] + lucas[i-1]) % MOD;
  fib[0] = 0;
  fib[1] = 1;
  for(int i=2; i<MAXN; i++) fib[i] = (fib[i-2] + fib[i-1]) % MOD;
}
int fastlog(int x) {
  return (x == 0 ? -1 : 64 - __builtin_clzll(x) - 1);
}
void gay(int i) { cout << "Case #" << i << ": "; }
int csb(int l, int r, int k) { // count number of [l, r] such that i & 2^k > 0
  if(l > r) return 0;
  if(l == 0) {
    int s = r / (1ll << (k+1)); // number of complete cycles
    int t = r % (1ll << (k+1));
    int ans = s * (1ll << k);
    ans += (t >= (1ll << k) ? t - (1ll << k) + 1 : 0);
    return ans;
  }
  else return csb(0, r, k) - csb(0, l - 1, k);
}
int lis(vector<int> a) {
  int n = a.size();
  int bucket[n+1];
  for(int i=1; i<=n; i++) bucket[i] = 1e18;
  int ans = 1;
  for(int x: a) {
    auto it = lower_bound(bucket + 1, bucket +n +1, x);
    int d = distance(bucket, it);
    ans = max(ans, d);
    bucket[d] = min(bucket[d], x);
  }
  return ans;
}
struct event {
  int s, e;
  int id;
};
bool cmp_s(event x, event y) {
  return x.s < y.s;
}
bool cmp_e(event x, event y) {
  return x.e < y.e;
}
struct segtree_basic {
  struct node {
    int lazyval, mi, ma, sum; char lazytag;
    int len; // not changing
  };
  int stok;
  vector<node> st;
  void bu(int l, int r, int idx) {
    st[idx].lazyval = st[idx].mi = st[idx].ma = st[idx].sum = 0;
    st[idx].lazytag = '?';
    st[idx].len = r - l + 1;
    if(l == r) {
      return;
    }
    int mid = (l + r) >> 1;
    bu(l, mid, 2*idx+1);
    bu(mid+1, r, 2*idx+2);
  }
  void push_down(int idx) {
    if(st[idx].lazytag == '?') return;
    if(st[idx].lazytag == ':') {
      st[2*idx+1].lazyval = st[idx].lazyval;
      st[2*idx+1].mi = st[idx].lazyval;
      st[2*idx+1].ma = st[idx].lazyval;
      st[2*idx+1].sum = st[idx].lazyval * st[2*idx+1].len;
      st[2*idx+1].lazytag = ':';
 
      st[2*idx+2].lazyval = st[idx].lazyval;
      st[2*idx+2].mi = st[idx].lazyval;
      st[2*idx+2].ma = st[idx].lazyval;
      st[2*idx+2].sum = st[idx].lazyval * st[2*idx+2].len;
      st[2*idx+2].lazytag = ':';
 
    }
    else {
      st[2*idx+1].lazyval += st[idx].lazyval;
      st[2*idx+1].mi += st[idx].lazyval;
      st[2*idx+1].ma += st[idx].lazyval;
      st[2*idx+1].sum += st[idx].lazyval * st[2*idx+1].len;
      st[2*idx+1].lazytag = (st[2*idx+1].lazytag == ':' ? ':' : '+');
 
      st[2*idx+2].lazyval += st[idx].lazyval;
      st[2*idx+2].mi += st[idx].lazyval;
      st[2*idx+2].ma += st[idx].lazyval;
      st[2*idx+2].sum += st[idx].lazyval * st[2*idx+2].len;
      st[2*idx+2].lazytag = (st[2*idx+2].lazytag == ':' ? ':' : '+');
    }
    st[idx].lazytag = '?';
    st[idx].lazyval = 0;
  }
  void push_up(int idx) {
    st[idx].mi = min(st[2*idx+1].mi, st[2*idx+2].mi);
    st[idx].ma = max(st[2*idx+1].ma, st[2*idx+2].ma);
    st[idx].sum = st[2*idx+1].sum + st[2*idx+2].sum;
  }
  void u1(int l, int r, int constl, int constr, int idx, int val) { // range := v
    if(l <= constl && constr <= r) {
      st[idx].lazyval = val;
      st[idx].mi = val;
      st[idx].ma = val;
      st[idx].sum = val * st[idx].len;
      st[idx].lazytag = ':';
      return;
    }
    int mid = (constl + constr) >> 1;
    push_down(idx);
    if(mid < l || r < constl) u1(l, r, mid+1, constr, 2*idx+2, val);
    else if(constr < l || r < mid + 1) u1(l, r, constl, mid, 2*idx+1, val);
    else {
      u1(l, r, constl, mid, 2*idx+1, val);
      u1(l, r, mid+1, constr, 2*idx+2, val);
    }
    push_up(idx);
  }
 
  void u2(int l, int r, int constl, int constr, int idx, int val) { // range += v
    if(l <= constl && constr <= r) {
      st[idx].lazyval += val;
      st[idx].mi += val;
      st[idx].ma += val;
      st[idx].sum += val * st[idx].len;
      st[idx].lazytag = (st[idx].lazytag == ':' ? ':' : '+');
      return;
    }
    int mid = (constl + constr) >> 1;
    push_down(idx);
    if(mid < l || r < constl) u2(l, r, mid+1, constr, 2*idx+2, val);
    else if(constr < l || r < mid + 1) u2(l, r, constl, mid, 2*idx+1, val);
    else {
      u2(l, r, constl, mid, 2*idx+1, val);
      u2(l, r, mid+1, constr, 2*idx+2, val);
    }
    push_up(idx);
  }
 
  int qu1(int l, int r, int constl, int constr, int idx) { // range min
    if(l <= constl && constr <= r) return st[idx].mi;
    int mid = (constl + constr) >> 1;
    push_down(idx);
    if(mid < l || r < constl) return qu1(l, r, mid+1, constr, 2*idx+2);
    else if(constr < l || r < mid + 1) return qu1(l, r, constl, mid, 2*idx+1);
    else {
      return min(qu1(l, r, constl, mid, 2*idx+1), qu1(l, r, mid+1, constr, 2*idx+2));
    }
  }
 
  int qu2(int l, int r, int constl, int constr, int idx) { // range max
    if(l <= constl && constr <= r) return st[idx].ma;
    int mid = (constl + constr) >> 1;
    push_down(idx);
    if(mid < l || r < constl) return qu2(l, r, mid+1, constr, 2*idx+2); 
    else if(constr < l || r < mid + 1) return qu2(l, r, constl, mid, 2*idx+1);
    else {
      return max(qu2(l, r, constl, mid, 2*idx+1), qu2(l, r, mid+1, constr, 2*idx+2));
    }
  }
  int qu3(int l, int r, int constl, int constr, int idx) { // range sum
    if(l <= constl && constr <= r) return st[idx].sum;
    int mid = (constl + constr) >> 1;
    push_down(idx);
    if(mid < l || r < constl) return qu3(l, r, mid+1, constr, 2*idx+2);
    else if(constr < l || r < mid + 1) return qu3(l, r, constl, mid, 2*idx+1);
    else {
      return qu3(l, r, constl, mid, 2*idx+1) + qu3(l, r, mid+1, constr, 2*idx+2);
    }
  }
  int qu4(int l, int r, int constl, int constr, int idx, int val) { // first at least v
    if(l <= constl && constr <= r) {
      if(st[idx].ma < val) return -1;
      while(constl < constr) {
        int mid = (constl + constr) >> 1;
        push_down(idx);
        if(st[2*idx+1].ma >= val) constr = mid, idx = 2*idx + 1;
        else constl = mid+1, idx = 2*idx + 2;
      }
      return constl;
    }
    int mid = (constl + constr) >> 1;
    push_down(idx);
    if(mid < l || r < constl) return qu4(l, r, mid+1, constr, 2*idx+2, val);
    else if(constr < l || r < mid + 1) return qu4(l, r, constl, mid, 2*idx+1, val);
    else {
      int lchild = qu4(l, r, constl, mid, 2*idx+1, val);
      if(lchild != -1) return lchild;
      return qu4(l, r, mid+1, constr, 2*idx+2, val);
    }
  }
  int qu5(int l, int r, int constl, int constr, int idx, int val) { // first at most v
    if(l <= constl && constr <= r) {
      if(st[idx].mi > val) return -1;
      while(constl < constr) {
        int mid = (constl + constr) >> 1;
        push_down(idx);
        if(st[2*idx+1].mi <= val) constr = mid, idx = 2*idx + 1;
        else constl = mid+1, idx = 2*idx + 2;
      }
      return constl;
    }
    int mid = (constl + constr) >> 1;
    push_down(idx);
    if(mid < l || r < constl) return qu5(l, r, mid+1, constr, 2*idx+2, val);
    else if(constr < l || r < mid + 1) return qu5(l, r, constl, mid, 2*idx+1, val);
    else {
      int lchild = qu5(l, r, constl, mid, 2*idx+1, val);
      if(lchild != -1) return lchild;
      return qu5(l, r, mid+1, constr, 2*idx+2, val);
    }
  }
  int qu6(int l, int r, int constl, int constr, int idx, int val) { // last at least v
    if(l <= constl && constr <= r) {
      if(st[idx].ma < val) return -1;
      while(constl < constr) {
        int mid = (constl + constr) >> 1;
        push_down(idx);
        if(st[2*idx+2].ma >= val) constl = mid+1, idx = 2*idx + 2;
        else constr = mid, idx = 2*idx + 1;
      }
      return constl;
    }
    int mid = (constl + constr) >> 1;
    push_down(idx);
    if(mid < l || r < constl) return qu6(l, r, mid+1, constr, 2*idx+2, val);
    else if(constr < l || r < mid + 1) return qu6(l, r, constl, mid, 2*idx+1, val);
    else {
      int rchild = qu6(l, r, mid+1, constr, 2*idx+2, val);
      if(rchild != -1) return rchild;
      return qu6(l, r, constl, mid, 2*idx+1, val);
    }
  }
  int qu7(int l, int r, int constl, int constr, int idx, int val) { // last at most v
    if(l <= constl && constr <= r) {
      if(st[idx].mi > val) return -1;
      while(constl < constr) {
        int mid = (constl + constr) >> 1;
        push_down(idx);
        if(st[2*idx+2].mi <= val) constl = mid+1, idx = 2*idx + 2;
        else constr = mid, idx = 2*idx + 1;
      }
      return constl;
    }
    int mid = (constl + constr) >> 1;
    push_down(idx);
    if(mid < l || r < constl) return qu7(l, r, mid+1, constr, 2*idx+2, val);
    else if(constr < l || r < mid + 1) return qu7(l, r, constl, mid, 2*idx+1, val);
    else {
      int rchild = qu7(l, r, mid+1, constr, 2*idx+2, val);
      if(rchild != -1) return rchild;
      return qu7(l, r, constl, mid, 2*idx+1, val);
    }
  }
  public:
  void resize(int k) {
    st.resize(4*k + 10);
    stok = k;
    bu(0, k, 0);
  }
  void range_assign(int l, int r, int v) { u1(l, r, 0, stok, 0, v); }
 
  void range_add(int l, int r, int v) { u2(l, r, 0, stok, 0, v); }
 
  int query_min(int l, int r) { return qu1(l, r, 0, stok, 0); }
 
  int query_max(int l, int r) { return qu2(l, r, 0, stok, 0); }
 
  int query_sum(int l, int r) { return qu3(l, r, 0, stok, 0); }
 
  int query_firstAtLeast(int l, int r, int v) { return qu4(l, r, 0, stok, 0, v); }
 
  int query_firstAtMost(int l, int r, int v) { return qu5(l, r, 0, stok, 0, v); }
 
  int query_lastAtLeast(int l, int r, int v) { return qu6(l, r, 0, stok, 0, v); } 
 
  int query_lastAtMost(int l, int r, int v) { return qu7(l, r, 0, stok, 0, v); }
};
bool normal_order(event x, event y) {
  return x.id < y.id;
}

struct query {
  int start, end, id;
  int ans;
};
event a[MAXN], b[MAXN];
bool by_endtime(query x, query y) {
  return a[x.end].e < a[y.end].e;
}

bool natural_order(query x, query y) {
  return x.id < y.id;
}
void solve(int tc) {
  int n, q;
  cin >> n >> q;
  set<int> discrete;
  for(int i=1; i<=n; i++) {
    cin >> a[i].s >> a[i].e;
    discrete.insert(a[i].s);
    discrete.insert(a[i].e);
    a[i].id = i;
    b[i].s = a[i].s;
    b[i].e = a[i].e;
    b[i].id = a[i].id;
  }
  int owo = 0;
  map<int, int > is;
  for(int x: discrete) {
    owo++;
    is[x] = owo;
  }
  for(int i=1; i<=n; i++) {
    a[i].s = is[a[i].s];
    b[i].s = is[b[i].s];
    a[i].e = is[a[i].e];
    b[i].e = is[b[i].e];
  }
  query c[q+1];
  for(int i=1; i<=q; i++) {
    cin >> c[i].start >> c[i].end;
    c[i].id = i;
  }
  sort(c+1, c+q+1, by_endtime);
  sort(b+1, b+n+1, cmp_e);
  int j = 0;
  segtree_basic opt;
  opt.resize(2*n);
  int mi[2*n + 1]; // for all events that end at i, min strat time
  for(int i=1; i<=2*n; i++) mi[i] = 1e9;
  for(int i=1; i<=n; i++) mi[a[i].e] = min(mi[a[i].e], a[i].s);
  for(int i=1; i<=q; i++) {
    if(a[c[i].start].e > a[c[i].end].e) {
      c[i].ans = -1;
      continue;
    }
    if(c[i].start == c[i].end) {
      c[i].ans = 0;
      continue;
    }
    int et = a[c[i].end].e;
    while(j+1 <= n && b[j+1].e <= et) {
      j++;
      opt.range_assign(b[j].s, b[j].e, b[j].id);
    }
//    for(int j=1; j<=2*n; j++) cout << opt.query_max(j, j) << " \n"[j == 2*n];
    int st = c[i].start;
    int turns = n;
    int ans = 0;
    while(turns--) {
      int oo = opt.query_max(a[st].e, a[st].e);
      //cout << oo << " ";
      if(a[oo].e < a[c[i].end].e) {
        st = oo;
        ans++;
      }
      else {
        break;
      }
    }
    if(a[c[i].end].s <= a[st].e && a[st].e <= a[c[i].end].e) c[i].ans = ans + 1;
    else if(mi[a[c[i].end].e] <= a[st].e && a[st].e <= a[c[i].end].e) c[i].ans = ans + 2;
    else c[i].ans = -1;

    //else if(mi[b[se].e] <= b[fi].e && b[fi].e <= b[se].e) cout << ans + 2 << "\n";
  }
  sort(c+1, c+1+q, natural_order);
  for(int i=1; i<=q; i++) {
    if(c[i].ans == -1) cout << "impossible\n";
    else cout << c[i].ans << "\n";
  }

}
int32_t main() {
  precomp();
  ios::sync_with_stdio(0); cin.tie(0);
  int t = 1; //cin >> t;
  for(int i=1; i<=t; i++) solve(i);
}
// I don't know geometry.
// Teaming is unfair.

/*
3 1

1 6
5 5
6 6

2 3
*/
# Verdict Execution time Memory Grader output
1 Correct 8 ms 7380 KB Output is correct
2 Execution timed out 1586 ms 68300 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 7380 KB Output is correct
2 Correct 8 ms 7268 KB Output is correct
3 Correct 16 ms 7892 KB Output is correct
4 Correct 12 ms 7892 KB Output is correct
5 Correct 11 ms 7888 KB Output is correct
6 Correct 11 ms 7892 KB Output is correct
7 Correct 11 ms 8020 KB Output is correct
8 Correct 9 ms 8020 KB Output is correct
9 Correct 8 ms 7760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 7380 KB Output is correct
2 Correct 8 ms 7268 KB Output is correct
3 Correct 16 ms 7892 KB Output is correct
4 Correct 12 ms 7892 KB Output is correct
5 Correct 11 ms 7888 KB Output is correct
6 Correct 11 ms 7892 KB Output is correct
7 Correct 11 ms 8020 KB Output is correct
8 Correct 9 ms 8020 KB Output is correct
9 Correct 8 ms 7760 KB Output is correct
10 Correct 8 ms 7380 KB Output is correct
11 Correct 8 ms 7380 KB Output is correct
12 Correct 16 ms 7916 KB Output is correct
13 Correct 12 ms 7904 KB Output is correct
14 Correct 10 ms 7932 KB Output is correct
15 Correct 10 ms 7856 KB Output is correct
16 Correct 9 ms 8020 KB Output is correct
17 Correct 11 ms 8136 KB Output is correct
18 Correct 8 ms 7748 KB Output is correct
19 Execution timed out 1579 ms 14264 KB Time limit exceeded
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 7380 KB Output is correct
2 Correct 8 ms 7268 KB Output is correct
3 Correct 16 ms 7892 KB Output is correct
4 Correct 12 ms 7892 KB Output is correct
5 Correct 11 ms 7888 KB Output is correct
6 Correct 11 ms 7892 KB Output is correct
7 Correct 11 ms 8020 KB Output is correct
8 Correct 9 ms 8020 KB Output is correct
9 Correct 8 ms 7760 KB Output is correct
10 Correct 8 ms 7380 KB Output is correct
11 Correct 8 ms 7288 KB Output is correct
12 Correct 16 ms 7820 KB Output is correct
13 Correct 11 ms 7892 KB Output is correct
14 Correct 10 ms 7892 KB Output is correct
15 Correct 10 ms 7892 KB Output is correct
16 Correct 9 ms 8020 KB Output is correct
17 Correct 10 ms 8020 KB Output is correct
18 Correct 9 ms 7764 KB Output is correct
19 Correct 1441 ms 63940 KB Output is correct
20 Correct 404 ms 63308 KB Output is correct
21 Correct 339 ms 63884 KB Output is correct
22 Correct 540 ms 68048 KB Output is correct
23 Correct 381 ms 74892 KB Output is correct
24 Correct 379 ms 75060 KB Output is correct
25 Correct 73 ms 52392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1583 ms 68224 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 7380 KB Output is correct
2 Execution timed out 1586 ms 68300 KB Time limit exceeded
3 Halted 0 ms 0 KB -