제출 #1274591

#제출 시각아이디문제언어결과실행 시간메모리
1274591DanielPr8Alternating Current (BOI18_alternating)C++20
0 / 100
20 ms10724 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using pll = pair<ll,ll>;
using vpl = vector<pll>;
using vvl =vector<vll>;
#define f first
#define s second
#define pb push_back
#define all(v) v.begin(),v.end()
void fuck(){
    cout << "impossible";
    exit(0);
}
vll op, dp;
struct seg{
    ll n;
    vll bl;
    ll mx(ll a, ll b){
        if(dp[a])return a;
        return b;
    }
    seg(ll n):n(n){
        bl = vll(n*2);
    }
    void upd(ll i, ll x){
        for(i+=n; i>0; i/=2){
            bl[i] = mx(bl[i],x);
        }
    }
    ll qu(ll a, ll b){
        ll ans=bl[a+n];
        for(a+=n,b+=n;a<=b; a/=2, b/=2){
            if(a%2==1)ans = mx(ans, bl[a++]);
            if(b%2==0)ans = mx(ans, bl[b--]);
        }
        return ans;
    }
};
int main(){
    ios_base::sync_with_stdio(0);cin.tie(NULL);
    ll n, m;
    cin >> n >> m;
    vpl rl(m);
    vll po(n+2);
    ll st=0;
    for(ll j = 0; j < m; ++j){
        pll& i = rl[j];
        cin >> i.f >> i.s;
        i.f--;
        i.s--;
        if(i.f==0)st=j;
        if(i.f<=i.s){
            po[i.f]++;
            po[i.s+1]--;
        }
        else{
            st=j;
            po[0]++;
            po[i.s+1]--;
            po[i.f]++;
            i.s+=n;
        }
    }
    vll am(n);
    am[0]=po[0];
    vll one={-1};
    if(am[0]==1)fuck();
    else if(am[0]==2)one.pb(0);
    for(ll i = 1; i < n; ++i){
        am[i]=am[i-1]+po[i];
        if(am[i]==1)fuck();
        else if(am[i]==2)one.pb(i);
    }
    if(one.size()>1)one.pb(one[1]+n);
    one.pb(1e9);
    op = dp = vll(m);
    vll ord(m);
    ll N = 1<<(32-__builtin_clz(n*2-1));
    seg *S = new seg(N);
    iota(all(ord),0);
    sort(all(ord), [&](ll a, ll b){return rl[a].f>rl[b].f;});

    ll lm = one[lower_bound(all(one), rl[st].f)-one.begin()];
    for(ll i:ord){
        if(i==st)continue;
        if(rl[i].s>=lm)continue;
        if(rl[i].s>=rl[st].f-1){dp[i]=1;op[i]=st;}
        else{
            ll bt = max(rl[i].f,one[upper_bound(all(one), rl[i].s)-one.begin()-1]+1);
            if(bt>rl[i].s+1)continue;
            ll nx = S->qu(bt, rl[i].s+1);
            dp[i]=dp[nx];
            op[i]=nx;
        }
        S->upd(rl[i].f, i);
    }
    if(rl[st].s==rl[st].f+n-1){dp[st]=1;op[st]=st;}
    else{
        ll bt = max(0ll,one[upper_bound(all(one), rl[st].s-n)-one.begin()-1]+1);
        if(bt<=rl[st].s-n+1){
            ll nx = S->qu(bt, rl[st].s-n+1);
            if(dp[nx]==1){
                dp[st]=dp[nx];
                op[st]=nx;
            }
        }
        if(bt==0){
            bt = max(rl[st].f,one[upper_bound(all(one), n-1)-one.begin()-1]+1);
            if(bt<=n-1){
                ll nx = S->qu(bt, n-1);
                if(dp[nx]==1){
                    dp[st]=dp[nx];
                    op[st]=nx;
                }
            }
        }

    }
    vll ans(m);
    if(!dp[st])fuck();
    ans[st]=1;
    ll cur = op[st];
    while(cur!=st){
        ans[cur]=1;
        cur=op[cur];
    }
    for(ll i:ans)cout << i;
}
#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...