Submission #1083768

# Submission time Handle Problem Language Result Execution time Memory
1083768 2024-09-04T05:47:42 Z vjudge1 Fire (BOI24_fire) C++17
0 / 100
2 ms 348 KB
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define pb push_back
#define for1(i,x,n) for(int i=x;i<=n;i++)
#define for2(i,x,n) for(int i=x;i>=n;i--)
#define int ll

typedef long long ll;
typedef pair<int,int> pii;

const ll maxn=2e5+69;
const ll mod=1e9+7;
const ll inf=1e18;
int n,m;
pii a[maxn];
int pre[maxn][21],val[maxn][21];
int suf[maxn];
pii jump(int u)
{
    int r=a[u].fi,res=0;
    for2(i,20,0)
    {
        if (r-a[pre[u][i]].se < m)
        {
            u=pre[u][i];
            res+=val[u][i];
        }
    }
    return {pre[u][0],res+(pre[u][0]!=u)};
}
void sol()
{
    cin >> n>> m;
    for1(i,1,n)
    {
        cin >>a[i].se>> a[i].fi;
        if (a[i].fi<a[i].se) a[i].fi+=m;
//        cerr<<n<< "adsa\n";
    }
    sort(a+1,a+1+n);
    suf[n]=n;
    for2(i,n-1,1)
    {
        if (a[i].se<a[suf[i+1]].se) suf[i]=i;
        else suf[i]=suf[i+1];
    }
    for1(i,1,n)
    {
        pre[i][0]=suf[upper_bound(a+1,a+1+n,make_pair(a[i].se,-inf))-a];
        val[i][0]=(pre[i][0]!=i);
    }
    for1(j,1,20) for1(i,1,n)
    {
        pre[i][j]=pre[pre[i][j-1]][j-1];
        val[i][j]=val[i][j-1]+val[pre[i][j-1]][j-1];
    }
//    for1(i,1,n) cerr <<a[i].se<<' '<<a[i].fi <<' '<< pre[i][0]<<' '<<val[i][0]<<'\n';
    int res=inf;
    for1(i,1,n)
    {
        pii j=jump(i);
        if (a[i].fi-a[j.fi].se>=m) res=min(res,j.se);
    }
    if (res==inf) cout << "-1";
    else cout << res+1;

}

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    freopen("fire.inp","r",stdin);
    freopen("fire.out","w",stdout);
    int t=1;//cin >> t;
    while (t--)
    {
        sol();
    }
}


Compilation message

Main.cpp: In function 'int32_t main()':
Main.cpp:77:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   77 |     freopen("fire.inp","r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
Main.cpp:78:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   78 |     freopen("fire.out","w",stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -