제출 #1155674

#제출 시각아이디문제언어결과실행 시간메모리
1155674adiyerFire (BOI24_fire)C++20
0 / 100
855 ms12976 KiB
#include <bits/stdc++.h>

#define all(x) x.begin(), x.end()
#define len(s) (ll) s.size()
#define pb push_back
#define F first 
#define S second 

using namespace std;

typedef long long ll;

const int N = 1e5 + 17; 
const int P = 31;
const int mod = 1e9 + 7;
const ll inf = 1e18;

ll n, m, sz, res;
ll l[N], r[N], ord[N], was[N], d[N], mx[N], val[N];

map < ll, ll > id;

vector < pair < ll, ll > > vec;
vector < pair < ll, ll > > g[N];

signed main(){
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  cin >> n >> m, res = inf;
  for(ll i = 1; i <= n; i++){ 
    cin >> l[i] >> r[i], ord[i] = i;
    if(r[i] < l[i]) r[i] = r[i] + m;
    vec.pb({l[i], r[i]});
    vec.pb({r[i], inf});
    vec.pb({l[i] + m - 1, inf});
  }
  sort(all(vec));
  for(ll i = 0; i < len(vec); i++){
    if(!id[vec[i].F]) id[vec[i].F] = ++sz, val[sz] = vec[i].F;
    if(vec[i].S != inf) mx[id[vec[i].F]] = max(mx[id[vec[i].F]], vec[i].S);
  }
  for(ll i = 1; i <= sz; i++){
    if(mx[i]) g[i].pb({id[mx[i]], 1});
    if(i > 1) g[i].pb({i - 1, 0});
  }
  for(ll i = 1; i <= sz; i++){
    if(!mx[i]) continue;
    deque < ll > q;
    q.push_front(i), was[i] = 1;
    while(len(q)){
      ll v = q.front(); q.pop_front();
      for(auto [u, w] : g[v]){
        if(!was[u]){
          d[u] = d[v] + w, was[u] = 1;
          if(!w) q.push_front(u);
          else q.push_back(u);
        }
      }
    }
    for(ll j = 1; j <= sz; j++)
      if(val[i] + m - 1 <= val[j] && was[j]) 
        res = min(res, d[j]);
    for(ll j = 1; j <= sz; j++) was[j] = 0;
  }
  if(res == inf) res = -1;
  cout << res;
}
#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...