Submission #1307480

#TimeUsernameProblemLanguageResultExecution timeMemory
1307480DangKhoizzzzEvent Hopping (BOI22_events)C++20
0 / 100
1 ms572 KiB
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair <int , int>
#define arr3 array <int , 3>
#define MASK(x) (1 << x)

using namespace std;

const int INF = 1e9;
const int maxn = 1e5 + 7;
const int mod = 998244353;

int n , q , S[maxn] , E[maxn];
pii st[maxn][20] , jump[maxn][20];

pii get(int l , int r)
{
    int k = __lg(r-l+1);
    return min(st[l][k] , st[r-MASK(k)+1][k]);
}

void build()
{
    vector <int> val;
    for(int i = 1; i <= 2*n; i++)
    {
        for(int j = 0; j < 20; j++) st[i][j] = {INF , INF};
        val.push_back(S[i]);
        val.push_back(E[i]);
    }
    sort(val.begin() , val.end());
    val.erase(unique(val.begin() , val.end()) , val.end());
    for(int i = 1; i <= n; i++)
    {
        S[i] = upper_bound(val.begin() , val.end() , S[i]) -val.begin();
        E[i] = upper_bound(val.begin() , val.end() , E[i]) -val.begin();
        st[E[i]][0] = min(st[E[i]][0] , (pii){S[i] , i});
    }
    for(int j = 1; j < 20; j++)
    {
        for(int i = 1; i + MASK(j) - 1 <= 2*n; i++)
        {
            st[i][j] = min(st[i][j-1] , st[i+MASK(j-1)][j-1]);
        }
    }
    for(int i = 1; i <= n; i++) jump[i][0] = get(S[i] , E[i]);
    for(int j = 1; j < 20; j++)
    {
        for(int i = 1; i <= n; i++) jump[i][j] = jump[jump[i][j-1].se][j-1];
    }
}

void solve()
{
    cin >> n >> q;
    for(int i = 1; i <= n; i++) cin >> S[i] >> E[i];
    build();
    while(q--)
    {
        int a , b; cin >> a >> b;
        if(a == b){ cout << 0 << '\n'; continue;}
        int ans = 1 , cur = b;
        for(int i = 18; i >= 0; i--)
        {
            if(jump[cur][i].fi > E[a])
            {
                ans += MASK(i);
                cur = jump[cur][i].se;
            }
        }
        ans++;
        cur = jump[cur][0].se;
        if(S[cur] <= E[a] && E[a] <= E[cur]) cout << ans << '\n';
        else cout << "impossible" << '\n';
    }
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    freopen("inp.txt", "r", stdin); freopen("out.txt", "w", stdout);
    solve();
    return 0;
}

Compilation message (stderr)

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