Submission #704276

#TimeUsernameProblemLanguageResultExecution timeMemory
704276Tuanlinh123Xylophone (JOI18_xylophone)C++17
47 / 100
83 ms436 KiB
#include<bits/stdc++.h>
#include "xylophone.h"
#define ll int
using namespace std;

ll a[100005];
bool seen[100005];

ll calc(ll a, ll b, ll dis1, ll dis2)
{
    if (a<b)
    {
        if (dis1==dis2 || dis2==b-a)
            return a+dis1;
        return a-dis1;
    }
    if (dis1==dis2 || dis2==a-b)
        return a-dis1;
    return a+dis1;
}

void solve1(ll L, ll n)
{
    for (ll i=L-2; i>=1; i--)
    {
        ll val=query(i, i+1);
        if (a[i+1]-val<0 || seen[a[i+1]-val])
            a[i]=a[i+1]+val;
        else if (a[i+1]+val>n || seen[a[i+1]+val])
            a[i]=a[i+1]-val;
        else
            a[i]=calc(a[i+1], a[i+2], val, query(i, i+2));
        seen[a[i]]=1;
    }
}

void solve2(ll L, ll R, ll n)
{
    for (ll i=L+2; i<R; i++)
    {
        ll val=query(i-1, i);
        if (a[i-1]-val<0 || seen[a[i-1]-val])
            a[i]=a[i-1]+val;
        else if (a[i-1]+val>n || seen[a[i-1]+val])
            a[i]=a[i-1]-val;
        a[i]=calc(a[i-1], a[i-2], val, query(i-2, i));
        seen[a[i]]=1;
    }
}

void solve3(ll R, ll n)
{
    for (ll i=R+2; i<=n; i++)
    {
        ll val=query(i-1, i);
        if (a[i-1]-val<0 || seen[a[i-1]-val])
            a[i]=a[i-1]+val;
        else if (a[i-1]+val>n || seen[a[i-1]+val])
            a[i]=a[i-1]-val;
        a[i]=calc(a[i-1], a[i-2], val, query(i-2, i));
        seen[a[i]]=1;
    }
}

void solve(ll n)
{
//    freopen("output.txt", "w", stdout);
    srand((ll)time(0));
    ll L=1, R=n;
    ll lo=L, hi=R, val=query(lo, hi);
    while (hi-lo>1)
    {
        ll mid=(hi+lo)/2;
        if (query(L, mid)!=val)
            lo=mid;
        else hi=mid;
    }
    if (query(L, lo)!=val)
        R=hi;
    else R=lo;
    lo=L, hi=R;
    while (hi-lo>1)
    {
        ll mid=(hi+lo)/2;
        if (query(mid, R)!=val)
            hi=mid;
        else lo=mid;
    }
    if (query(hi, R)!=val)
        L=lo;
    else R=hi;
    a[L]=1; a[R]=n;
//    cout << L << " " << R << "\n";
    if (L>1) a[L-1]=a[L]+query(L-1, L);
    a[L+1]=a[L]+query(L, L+1);
    if (R<n) a[R+1]=a[R]-query(R, R+1);
    ll order[3];
    order[0]=0, order[1]=1, order[2]=2;
    for (ll i=1; i<=10; i++)
    {
        ll l=abs(rand())%3, r=abs(rand())%3;
        swap(order[l], order[r]);
    }
    for (ll i=0; i<3; i++)
    {
        ll t=order[i];
        if (t==0)
            solve1(L, n);
        else if (t==1)
            solve2(L, R, n);
        else solve3(R, n);
    }
//    for (ll i=0; i<3; i++)
//        cout << order[i] << " ";
//    cout << "\n";
//    for (ll i=1; i<=n; i++)
//        cout << a[i] << " ";
//    cout << "\n";
    for (ll i=1; i<=n; i++)
        answer(i, a[i]);
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...