Submission #995218

# Submission time Handle Problem Language Result Execution time Memory
995218 2024-06-08T16:15:58 Z Tuanlinh123 Xylophone (JOI18_xylophone) C++17
100 / 100
45 ms 860 KB
#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 solve(ll n)
{
//    freopen("output.txt", "w", stdout);
    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);
    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;
    }
    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;
        else
            a[i]=calc(a[i-1], a[i-2], val, query(i-2, i));
        seen[a[i]]=1;
    }
    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;
        else
            a[i]=calc(a[i-1], a[i-2], val, query(i-2, i));
        seen[a[i]]=1;
    }
//    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 time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 504 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 596 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 504 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 596 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 2 ms 344 KB Output is correct
17 Correct 4 ms 344 KB Output is correct
18 Correct 5 ms 344 KB Output is correct
19 Correct 7 ms 440 KB Output is correct
20 Correct 7 ms 428 KB Output is correct
21 Correct 6 ms 344 KB Output is correct
22 Correct 6 ms 344 KB Output is correct
23 Correct 5 ms 456 KB Output is correct
24 Correct 8 ms 344 KB Output is correct
25 Correct 5 ms 344 KB Output is correct
26 Correct 6 ms 600 KB Output is correct
27 Correct 8 ms 448 KB Output is correct
28 Correct 6 ms 452 KB Output is correct
29 Correct 6 ms 344 KB Output is correct
30 Correct 8 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 504 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 596 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 2 ms 344 KB Output is correct
17 Correct 4 ms 344 KB Output is correct
18 Correct 5 ms 344 KB Output is correct
19 Correct 7 ms 440 KB Output is correct
20 Correct 7 ms 428 KB Output is correct
21 Correct 6 ms 344 KB Output is correct
22 Correct 6 ms 344 KB Output is correct
23 Correct 5 ms 456 KB Output is correct
24 Correct 8 ms 344 KB Output is correct
25 Correct 5 ms 344 KB Output is correct
26 Correct 6 ms 600 KB Output is correct
27 Correct 8 ms 448 KB Output is correct
28 Correct 6 ms 452 KB Output is correct
29 Correct 6 ms 344 KB Output is correct
30 Correct 8 ms 344 KB Output is correct
31 Correct 16 ms 444 KB Output is correct
32 Correct 18 ms 456 KB Output is correct
33 Correct 28 ms 444 KB Output is correct
34 Correct 28 ms 344 KB Output is correct
35 Correct 24 ms 344 KB Output is correct
36 Correct 24 ms 440 KB Output is correct
37 Correct 29 ms 684 KB Output is correct
38 Correct 33 ms 448 KB Output is correct
39 Correct 28 ms 440 KB Output is correct
40 Correct 32 ms 440 KB Output is correct
41 Correct 38 ms 712 KB Output is correct
42 Correct 18 ms 452 KB Output is correct
43 Correct 27 ms 860 KB Output is correct
44 Correct 25 ms 592 KB Output is correct
45 Correct 45 ms 452 KB Output is correct
46 Correct 38 ms 600 KB Output is correct
47 Correct 28 ms 692 KB Output is correct
48 Correct 41 ms 436 KB Output is correct
49 Correct 32 ms 704 KB Output is correct
50 Correct 34 ms 592 KB Output is correct
51 Correct 33 ms 464 KB Output is correct
52 Correct 28 ms 452 KB Output is correct
53 Correct 27 ms 440 KB Output is correct
54 Correct 23 ms 456 KB Output is correct
55 Correct 22 ms 600 KB Output is correct
56 Correct 29 ms 344 KB Output is correct
57 Correct 32 ms 344 KB Output is correct
58 Correct 23 ms 848 KB Output is correct
59 Correct 36 ms 600 KB Output is correct
60 Correct 38 ms 696 KB Output is correct