제출 #736153

#제출 시각아이디문제언어결과실행 시간메모리
736153ksu2009enEvent Hopping (BOI22_events)C++17
10 / 100
1584 ms13456 KiB
#include <iostream>
#include <vector>
#include <string>
#include <math.h>
#include <cmath>
#include <iomanip>
#include <cstdio>
#include <algorithm>
#include <numeric>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <deque>
#include <bitset>
#include <cstring>
#include <unordered_map>

using namespace std;
typedef long long ll;

struct one{
    ll first, second, ind;
};

bool cmp(one p1, one p2){
    if(p1.first == p2.first)
        return p1.second < p2.second;
    return p1.first < p2.first;
}

void compress(vector<one> &a){
    vector<ll>b;
    for(auto i: a){
        b.push_back(i.first);
        b.push_back(i.second);
    }
    
    sort(b.begin(), b.end());
    b.resize(unique(b.begin(), b.end()) - b.begin());
    
    ll step = 1;
    map<ll, ll> mp;
    for(auto i: b)
        mp[i] = step++;
    
    for(auto &i: a){
        i.first = mp[i.first];
        i.second = mp[i.second];
    }
}

ll dp[5007][5007];
vector<ll>end_here[5007];

vector<one>a;

void count_dp(one start){
    for(int i = 0; i < a.size(); i++)
        dp[start.ind][i] = 1e9;
    
    dp[start.ind][start.ind] = 0;
    
    bool ok = false;
    
    multiset<ll>st;
    
    for(int ind = 0; ind < a.size(); ind++){
        auto i = a[ind];
        
        if(i.ind == start.ind){
            ok = true;
            st.insert(0);
        }
        else if(ok){
            if(st.empty()){
                dp[start.ind][i.ind] = 1e9;
                st.insert(1e9);
            }
            else{
                dp[start.ind][i.ind] = *st.begin();
                if(*st.begin() != (ll)(1e9))
                    dp[start.ind][i.ind]++;
                
                st.insert(dp[start.ind][i.ind]);
            }
        }
        else
            st.insert(1e9);
        
        if(ind + 1 < a.size()){
            for(int del = i.first; del < a[ind + 1].first; del++)
                for(auto j: end_here[del]){
                    st.erase(st.find(dp[start.ind][j]));
                }
        }
    }
}

vector<ll>count_dp2(ll start){
    vector<ll>dp2(a.size(), 1e9);
    
    dp2[start] = 0;
    
    for(auto i: a){
        for(auto j: a){
            if(i.first <= j.second && j.second <= i.second){
                dp2[i.ind] = min(dp2[i.ind], dp2[j.ind] + 1);
            }
        }
    }
    
    return dp2;
}

int main(){
    ll n, q;
    cin >> n >> q;
    
    a.resize(n);
    for(int i = 0; i < n; i++){
        cin >> a[i].first >> a[i].second;
        a[i].ind = i;
    }
    
    compress(a);
    sort(a.begin(), a.end(), cmp);
    
    for(auto i: a)
        end_here[i.second].push_back(i.ind);
   
    while(q--){
        ll l, r;
        cin >> l >> r;
        
        l--, r--;
        
        auto dp2 = count_dp2(l);
        
        if(dp2[r] == (ll)(1e9))
            cout << "impossible" << endl;
        else
            cout << dp2[r] << endl;
    }
     
    return 0;
}
/*
 8 1
 1 2
 3 4
 1 5
 6 7
 5 10
 10 20
 15 20
 999999999 1000000000
 3 3
 */

컴파일 시 표준 에러 (stderr) 메시지

events.cpp: In function 'void count_dp(one)':
events.cpp:59:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<one>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |     for(int i = 0; i < a.size(); i++)
      |                    ~~^~~~~~~~~~
events.cpp:68:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<one>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   68 |     for(int ind = 0; ind < a.size(); ind++){
      |                      ~~~~^~~~~~~~~~
events.cpp:91:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<one>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   91 |         if(ind + 1 < a.size()){
      |            ~~~~~~~~^~~~~~~~~~
#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...