답안 #987192

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
987192 2024-05-22T09:58:27 Z alexdd Event Hopping (BOI22_events) C++17
0 / 100
2 ms 2652 KB
#include<bits/stdc++.h>
using namespace std;

ifstream fin("events.in");
ofstream fout("events.out");
#define cin fin
#define cout fout

int n,q,cate;
pair<int,int> e[100005];
int ord[100005];
vector<pair<int,int>> qrys[100005];
int rez[100005];
bool cmp(int x, int y)
{
    if(e[x].second < e[y].second)
        return 1;
    if(e[x].second > e[y].second)
        return 0;
    return e[x].first < e[y].first;
}
signed main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    cin>>n>>q;
    int le,ri;
    for(int i=1;i<=n;i++)
    {
        cin>>le>>ri;
        e[i]={le,ri};
        ord[i]=i;
    }
    for(int i=1;i<=q;i++)
    {
        cin>>le>>ri;
        if(le==ri)
        {
            rez[i]=0;
            continue;
        }
        if(e[ri].second < e[le].second)
        {
            rez[i]=-1;
            continue;
        }
        if(e[ri].first <= e[le].second)
        {
            rez[i]=1;
            continue;
        }
        qrys[ri].push_back({le,i});
    }
    sort(ord+1,ord+1+n,cmp);
    deque<int> dq;
    for(int i=1;i<=n;i++)
    {
        int x = ord[i];
        while(!dq.empty() && e[x].first<=e[dq.back()].first)
            dq.pop_back();
        while((int)dq.size()>=2 && e[x].first<=e[dq[(int)dq.size()-2]].second)
            dq.pop_back();
        dq.push_back(x);

        //cout<<x<<"   ";for(auto y:dq) cout<<y<<" ";cout<<" dq\n";

        for(auto cv:qrys[x])
        {
            int aux = cv.first;
            rez[cv.second]=-1;
            for(int j=(int)dq.size()-1;j>=0;j--)
            {
                if(e[dq[j]].first<=e[aux].second && e[aux].second<=e[dq[j]].second)
                {
                    rez[cv.second] = (int)dq.size()-j;
                    break;
                }
                if(j>0 && e[dq[j]].first > e[dq[j-1]].second)
                    break;
            }
        }

    }
    for(int i=1;i<=q;i++)
    {
        if(rez[i]==-1) cout<<"impossible\n";
        else cout<<rez[i]<<"\n";
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 2652 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 2652 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 2652 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 2652 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 2648 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 2652 KB Output isn't correct
2 Halted 0 ms 0 KB -