답안 #667046

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
667046 2022-11-30T08:56:15 Z berr Event Hopping (BOI22_events) C++17
0 / 100
817 ms 23056 KB
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+37;
vector<int> adj[N];
int d[N], p[N], vis[N], c[N];
array<int, 2> st[N*4];
map<int, int> mp;
// 1. subtask


void update(int v, int l, int r, int pos, array<int, 2> val)
{
    if(l==r&&l==pos) st[v]=val;
    else if(l>r) return;
    else
    {
        int mid=(l+r)/2;
        if(pos<=mid) update(v*2, l, mid, pos, val);
        else update(v*2+1, mid+1, r, pos, val);
        st[v]=max(st[v*2+1], st[v*2]);
    }
}

array<int, 2> get(int v, int l, int r, int tl, int tr)
{
    if(r<l) return {0, 0};
    if(l>tr||r<tl) return {0, 0};
    if(l>=tl&&r<=tr) return st[v];
    if(l==r) return {0, 0};
    int mid=(l+r)/2;

    return max(get(v*2, l, mid, tl, tr), get(v*2+1, mid+1, r, tl, tr));  
}


signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n, q; cin>>n>>q;

    vector<array<int, 3>> a(n);
    vector<int> x;
    for(int i=0; i<n; i++)
    {
        cin>>a[i][0]>>a[i][1];
        a[i][2]=i; p[i]=i;
        x.push_back(a[i][0]);
        x.push_back(a[i][1]);
    }

    sort(x.begin(), x.end());
    int cnt=1;

    for(auto i: x)
    {
        if(mp.count(i)==0) mp[i]=cnt, cnt++;
    }


    for(int i=0; i<n; i++)
    {
        a[i][0]=mp[a[i][0]];
        a[i][1]=mp[a[i][1]];

        update(1, 0, n*2, a[i][0], {a[i][1], a[i][2]});

    }


    for(int i=0; i<n; i++)
    {
        if(get(1, 0, n*2, a[i][0], a[i][0])[0]==a[i][2])
        update(1, 0, n*2, a[i][0], {0, 0});
        auto v=get(1, 0, n*2, a[i][0], a[i][1]);

        if(v[0]>=a[i][1]&&v[1]!=a[i][2])
        {
           adj[a[i][2]].push_back(v[1]);
           c[v[1]]++;
        }
        update(1, 0, n*2, a[i][0], {a[i][1], a[i][2]});

    }

    queue<array<int, 3>> qu; 

    for(int i=0; i<n; i++)
    {
        if(c[i]==0)
        {
            qu.push({i, 0, i});
        }
    }

    while(qu.size())
    {
        int v=qu.front()[0], u=qu.front()[1], par=qu.front()[2];
        qu.pop();
        d[v]=u; p[v]=par;

        if(adj[v].size()) qu.push({adj[v][0], u+1, par});
    }

    while(q--)
    {
        int s, e; cin>>s>>e;
        s--; e--;
        if(p[s]!=p[e]||d[e]<d[s]) cout<<"impossible"<<"\n";
        else cout<<d[e]-d[s]<<"\n";
    }


}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 220 ms 16292 KB Output is correct
3 Correct 201 ms 16300 KB Output is correct
4 Correct 202 ms 16316 KB Output is correct
5 Incorrect 817 ms 17168 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 2644 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 2644 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 2644 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 215 ms 16288 KB Output is correct
2 Correct 226 ms 16296 KB Output is correct
3 Correct 209 ms 16400 KB Output is correct
4 Correct 240 ms 20688 KB Output is correct
5 Correct 214 ms 16676 KB Output is correct
6 Incorrect 296 ms 23056 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 220 ms 16292 KB Output is correct
3 Correct 201 ms 16300 KB Output is correct
4 Correct 202 ms 16316 KB Output is correct
5 Incorrect 817 ms 17168 KB Output isn't correct
6 Halted 0 ms 0 KB -