Submission #987194

# Submission time Handle Problem Language Result Execution time Memory
987194 2024-05-22T10:01:01 Z alexdd Event Hopping (BOI22_events) C++17
0 / 100
1500 ms 5576 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;
            }
        }

        if(!dq.empty() && e[x].second == e[dq.back()].second && e[x].first > e[x].second) dq.pop_back();
    }
    for(int i=1;i<=q;i++)
    {
        if(rez[i]==-1) cout<<"impossible\n";
        else cout<<rez[i]<<"\n";
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2648 KB Output is correct
2 Execution timed out 1535 ms 5576 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2652 KB Output is correct
2 Correct 2 ms 2652 KB Output is correct
3 Correct 2 ms 2760 KB Output is correct
4 Correct 2 ms 2652 KB Output is correct
5 Correct 3 ms 2652 KB Output is correct
6 Correct 2 ms 2652 KB Output is correct
7 Incorrect 2 ms 2652 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2652 KB Output is correct
2 Correct 2 ms 2652 KB Output is correct
3 Correct 2 ms 2760 KB Output is correct
4 Correct 2 ms 2652 KB Output is correct
5 Correct 3 ms 2652 KB Output is correct
6 Correct 2 ms 2652 KB Output is correct
7 Incorrect 2 ms 2652 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2652 KB Output is correct
2 Correct 2 ms 2652 KB Output is correct
3 Correct 2 ms 2760 KB Output is correct
4 Correct 2 ms 2652 KB Output is correct
5 Correct 3 ms 2652 KB Output is correct
6 Correct 2 ms 2652 KB Output is correct
7 Incorrect 2 ms 2652 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1535 ms 5180 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2648 KB Output is correct
2 Execution timed out 1535 ms 5576 KB Time limit exceeded
3 Halted 0 ms 0 KB -