답안 #599429

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
599429 2022-07-19T14:02:42 Z alexander707070 Event Hopping (BOI22_events) C++17
30 / 100
350 ms 38652 KB
#include<bits/stdc++.h>
#define MAXN 100007
using namespace std;

struct event{
    long long s;
    long long e;
    int id;

    inline friend bool operator < (event fr,event sc){
        if(fr.e!=sc.e)return fr.e<sc.e;
        return fr.s<=sc.s;
    }
};

int n,cnt,q,S,E,ans;
int st[MAXN],et[MAXN];
int pos[MAXN],parent[MAXN];
event e[MAXN];
vector<int> w;
map<int,int> mp;

pair<int,int> mins[8*MAXN];

pair<int,int> best(pair<int,int> fr,pair<int,int> sc){
    if(fr.second<=sc.second)return fr;
    return sc;
}

void update(int v,int l,int r,int pos,int val,int id){
    if(l==r){
        if(val<mins[v].second or val==10000000){
            mins[v]={id,val};
        }
    }else{
        int tt=(l+r)/2;
        if(pos<=tt)update(2*v,l,tt,pos,val,id);
        else update(2*v+1,tt+1,r,pos,val,id);
        mins[v]=best(mins[2*v],mins[2*v+1]);
    }
}

pair<int,int> getmin(int v,int l,int r,int ll,int rr){
    if(ll>rr)return {0,100000000};
    if(l==ll and r==rr){
        return mins[v];
    }else{
        int tt=(l+r)/2;
        return best( getmin(2*v,l,tt,ll,min(tt,rr)) , getmin(2*v+1,tt+1,r,max(tt+1,ll),rr) );
    }
}

int dp[MAXN][20];
bool li[MAXN][20];

int ff(int x,int p){
    if(x==0)return 0;
    if(p==0)return parent[x];

    if(li[x][p])return dp[x][p];
    li[x][p]=true;

    dp[x][p]=ff(ff(x,p-1),p-1);
    return dp[x][p];
}

int bin(int id,int id2){

    if(id==id2)return 0;
    if(et[id]<st[id2])return -1;
    if(et[id2]>et[id])return -1;
    if(et[id2]>=st[id])return 1;

    int res=0;
    for(int i=19;i>=0;i--){
        if(ff(id,i)!=0 and st[ff(id,i)]>et[id2]){
            id=ff(id,i);
            res+=(1<<i);
        }
    }

    if(parent[id]==0)return -1;
    if(parent[id]==id2)return res+1;
    return res+2;
}

int main(){

    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin>>n>>q;
    for(int i=1;i<=n;i++){
        cin>>st[i]>>et[i];
        e[i]={st[i],et[i],i};
        w.push_back(e[i].s);
        w.push_back(e[i].e);
    }

    sort(w.begin(),w.end());
    for(int i=0;i<w.size();i++){
        if(i>0 and w[i]!=w[i-1])cnt++;
        mp[w[i]]=cnt;
    }

    for(int i=1;i<=n;i++){
        e[i].s=mp[e[i].s];
        e[i].e=mp[e[i].e];
        pos[e[i].id]=i;
    }

    for(int i=0;i<=cnt;i++){
        update(1,0,cnt,i,10000000,0);
    }

    sort(e+1,e+n+1);
    for(int i=1;i<=n;i++){
        parent[e[i].id]=getmin(1,0,cnt,e[i].s,e[i].e).first;
        update(1,0,cnt,e[i].e,e[i].s,e[i].id);
    }

    for(int i=1;i<=q;i++){
        cin>>S>>E;
        ans=bin(E,S);
        if(ans==-1)cout<<"impossible\n";
        else cout<<ans<<"\n";
    }


    return 0;
}

Compilation message

events.cpp: In function 'int main()':
events.cpp:102:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  102 |     for(int i=0;i<w.size();i++){
      |                 ~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 175 ms 25180 KB Output is correct
3 Correct 276 ms 25332 KB Output is correct
4 Correct 337 ms 25128 KB Output is correct
5 Correct 278 ms 25716 KB Output is correct
6 Correct 248 ms 26172 KB Output is correct
7 Correct 248 ms 26360 KB Output is correct
8 Correct 213 ms 25544 KB Output is correct
9 Correct 197 ms 25296 KB Output is correct
10 Correct 347 ms 25576 KB Output is correct
11 Correct 245 ms 29468 KB Output is correct
12 Correct 126 ms 15176 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 2 ms 516 KB Output is correct
4 Correct 1 ms 596 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 2 ms 596 KB Output is correct
8 Correct 2 ms 468 KB Output is correct
9 Runtime error 40 ms 38652 KB Execution killed with signal 11
10 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 2 ms 516 KB Output is correct
4 Correct 1 ms 596 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 2 ms 596 KB Output is correct
8 Correct 2 ms 468 KB Output is correct
9 Runtime error 40 ms 38652 KB Execution killed with signal 11
10 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 2 ms 516 KB Output is correct
4 Correct 1 ms 596 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 2 ms 596 KB Output is correct
8 Correct 2 ms 468 KB Output is correct
9 Runtime error 40 ms 38652 KB Execution killed with signal 11
10 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 175 ms 25156 KB Output is correct
2 Correct 280 ms 25252 KB Output is correct
3 Correct 350 ms 24924 KB Output is correct
4 Correct 198 ms 25288 KB Output is correct
5 Correct 318 ms 25432 KB Output is correct
6 Correct 326 ms 31984 KB Output is correct
7 Correct 345 ms 31984 KB Output is correct
8 Correct 260 ms 32240 KB Output is correct
9 Correct 226 ms 20380 KB Output is correct
10 Correct 292 ms 31688 KB Output is correct
11 Correct 239 ms 26364 KB Output is correct
12 Correct 260 ms 31688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 175 ms 25180 KB Output is correct
3 Correct 276 ms 25332 KB Output is correct
4 Correct 337 ms 25128 KB Output is correct
5 Correct 278 ms 25716 KB Output is correct
6 Correct 248 ms 26172 KB Output is correct
7 Correct 248 ms 26360 KB Output is correct
8 Correct 213 ms 25544 KB Output is correct
9 Correct 197 ms 25296 KB Output is correct
10 Correct 347 ms 25576 KB Output is correct
11 Correct 245 ms 29468 KB Output is correct
12 Correct 126 ms 15176 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 2 ms 516 KB Output is correct
16 Correct 1 ms 596 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 2 ms 596 KB Output is correct
20 Correct 2 ms 468 KB Output is correct
21 Runtime error 40 ms 38652 KB Execution killed with signal 11
22 Halted 0 ms 0 KB -