답안 #1040099

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1040099 2024-07-31T16:15:35 Z Marco_Escandon Event Hopping (BOI22_events) C++11
0 / 100
249 ms 105004 KB
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
const string ny[2] = {"No", "Yes"};
#define x first
#define y second
vector<vector<ll>> G(200000);
vector<vector<ll>> P(24,vector<ll>(200000,0));
vector<ll> v(200000,0);
void dfs(ll node, ll p,ll d)
{
    if(v[node]!=0) return;
    v[node]=d;
    P[0][node]=p;
    for(int i=1; i<24; i++)
        P[i][node]=P[i-1][P[i-1][node]];
    for(auto i:G[node])
        dfs(i,node,d+1);
}
ll Padre_A_Distancia_D(ll a, ll d)
{
    for(int i=19; i>=0; i--)
        if((d&(1<<i))!=0)
            a=P[i][a];
    return a;
}
ll lca(ll a, ll b)
{
    ll cont=0;
    if(v[a]<=v[b])
        swap(a,b);
    else return 1e9;
    cont+=v[a]-v[b];
    a=Padre_A_Distancia_D(a,v[a]-v[b]);
    ll c2=0;
    for(int i=23; i>=0; i--)
    {
        if(P[i][a]!=P[i][b])
        {
            a=P[i][a];
            b=P[i][b];
            c2+=(1<<i);
        }
    }
    if(a!=b)
    {
        c2++;
        a=P[0][a];
    }
    if(a==0||b==0) return 1e9;
    return c2*2+cont;
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    ll n,q;
    cin>>n>>q;
    v.resize(n+2);
    vector<pair<ll,ll>> cad;
    vector<pair<ll,ll>> input;input.push_back({0,0});
    for(int i=1; i<=n; i++)
    {
        ll a,b;
        cin>>a>>b;
        input.push_back({a,b});
        cad.push_back({a,i});
        cad.push_back({b,i});
    }
    sort(cad.begin(),cad.end());
    set<ll> s;
    for(int i=0; i<cad.size(); i++)
    {
        vector<ll> temp,temp2;
        ll a=i;
        while(i<cad.size()&&cad[a].x==cad[i].x){
            temp.push_back(cad[i].y);
            i++;
        }
        i--;
        for(auto j:temp)
        {
            if(s.find(j)!=s.end())
                temp2.push_back(j);
            else s.insert(j);
        }
        for(auto j:temp2) for(auto k:s) G[j].push_back(k);
        for(auto j:temp2) if(s.find(j)!=s.end()) s.erase(j);
    }
    for(int i=0; i<cad.size(); i++)
        dfs(cad[i].y,0,1);
    for(int i=0; i<q; i++)
    {
        ll a,b;
        cin>>a>>b;
        a=lca(a,b);
        if(a==1e9) cout<<"impossible"<<"\n";
        else cout<<a<<"\n";
    }
    return 0;
}

Compilation message

events.cpp: In function 'int main()':
events.cpp:73:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   73 |     for(int i=0; i<cad.size(); i++)
      |                  ~^~~~~~~~~~~
events.cpp:77:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   77 |         while(i<cad.size()&&cad[a].x==cad[i].x){
      |               ~^~~~~~~~~~~
events.cpp:91:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   91 |     for(int i=0; i<cad.size(); i++)
      |                  ~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 44124 KB Output is correct
2 Correct 134 ms 59880 KB Output is correct
3 Correct 147 ms 59756 KB Output is correct
4 Correct 142 ms 60980 KB Output is correct
5 Incorrect 141 ms 60468 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 20 ms 44308 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 20 ms 44308 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 20 ms 44308 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 146 ms 59956 KB Output is correct
2 Correct 158 ms 59772 KB Output is correct
3 Correct 201 ms 60984 KB Output is correct
4 Correct 105 ms 56632 KB Output is correct
5 Correct 159 ms 59052 KB Output is correct
6 Incorrect 249 ms 105004 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 44124 KB Output is correct
2 Correct 134 ms 59880 KB Output is correct
3 Correct 147 ms 59756 KB Output is correct
4 Correct 142 ms 60980 KB Output is correct
5 Incorrect 141 ms 60468 KB Output isn't correct
6 Halted 0 ms 0 KB -