Submission #93435

# Submission time Handle Problem Language Result Execution time Memory
93435 2019-01-08T12:09:34 Z Kastanda Fibonacci representations (CEOI18_fib) C++11
65 / 100
4000 ms 195308 KB
#include<bits/stdc++.h>
#define pb push_back
#define x first
#define y second
using namespace std;
typedef pair < int , int > pii;
const int N = 100005, Mod = 1e9 + 7;
mt19937 Rnd;
struct Node
{
    int p = 0, val = 0, cz = 0;
    pii dp1, dp2;
    Node * l = NULL, * r = NULL;
    inline void Init()
    {
        p = Rnd();
        dp1 = pii(1, 1);
        dp2 = pii(cz >> 1, (cz + 1) >> 1);
    }
};
typedef Node* Pode;

/* Treap */

inline void Func(Pode A, Pode B, Pode R)
{
    vector < int > a = {A->dp1.x, A->dp1.y, A->dp2.x, A->dp2.y};
    vector < int > b = {B->dp1.x, B->dp1.y, B->dp2.x, B->dp2.y};

    R->dp1.x = (1LL * a[0] * b[0] + 1LL * a[2] * b[1]) % Mod;
    R->dp1.y = (1LL * a[1] * b[0] + 1LL * a[3] * b[1]) % Mod;
    R->dp2.x = (1LL * a[0] * b[2] + 1LL * a[2] * b[3]) % Mod;
    R->dp2.y = (1LL * a[1] * b[2] + 1LL * a[3] * b[3]) % Mod;
}
inline void Upd(Pode T)
{
    if (!T)
        return ;

    T->dp1 = pii(1, 1);
    T->dp2 = pii(T->cz >> 1, (T->cz + 1) >> 1);

    if (T->l)
        Func(T->l, T, T);
    if (T->r)
        Func(T, T->r, T);
}
void Merge(Pode &T, Pode L, Pode R)
{
    if (!L)
        T = R;
    else if (!R)
        T = L;
    else if (L->p > R->p)
        Merge(L->r, L->r, R), T = L;
    else
        Merge(R->l, L, R->l), T = R;
    if (T) Upd(T);
}
void Split_by_value(Pode T, Pode &L, Pode &R, int val)
{
    if (!T)
    {
        L = R = NULL;
        return ;
    }
    if (T->val <= val)
        Split_by_value(T->r, T->r, R, val), L = T;
    else
        Split_by_value(T->l, L, T->l, val), R = T;
    if (T) Upd(T);
}
/* End */
int n, A[N];
int MnEd, MxEd;
set < int > B;
void Add(int a)
{
    MnEd = min(MnEd, a);
    MxEd = max(MxEd, a);

    if (a < 0)
        return ;
    if (a == 0)
    {
        Add(1);
        return ;
    }
    if (B.count(a))
    {
        B.erase(a);
        Add(a + 1);
        Add(a - 2);
        return ;
    }
    if (B.count(a - 1))
    {
        B.erase(a - 1);
        Add(a + 1);
        return ;
    }
    if (B.count(a + 1))
    {
        B.erase(a + 1);
        Add(a + 2);
        return ;
    }
    B.insert(a);
}
int main()
{
    Pode T = new Node;
    Rnd.seed(time(0));

    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
    {
        scanf("%d", &A[i]);

        MnEd = INT_MAX;
        MxEd = INT_MIN;

        Add(A[i]);

        if (i == 1)
        {
            T->cz = A[i] - 1;
            T->val = A[i];
            T->Init();
            printf("%d\n", (A[i] + 1) >> 1);
            continue;
        }
        MnEd -= 5; MxEd += 5;

        int upp = MxEd;
        if (*B.rbegin() > MxEd)
            upp = *B.upper_bound(MxEd);
        Pode a = NULL, b = NULL;
        Split_by_value(T, T, a, MnEd - 1);
        Split_by_value(a, a, b, upp);

        auto it = B.lower_bound(MnEd);
        int cz = (*it) - 1, last = *it;
        if (it != B.begin())
        {
            it --;
            cz -= *it;
            it ++;
        }
        Pode K = new Node;
        K->cz = cz;
        K->val = *it;
        K->Init();

        for (it ++; it != B.end() && last <= MxEd; it ++)
        {
            cz = (*it) - 1 - last;
            Pode tobe = new Node;
            tobe->val = *it;
            tobe->cz = cz;
            tobe->Init();
            Merge(K, K, tobe);
            last = *it;
        }
        Merge(T, T, K);
        Merge(T, T, b);

        printf("%d\n", (T->dp1.x + T->dp2.x) % Mod);
    }
    return (0);
}

Compilation message

fib.cpp: In function 'int main()':
fib.cpp:115:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &n);
     ~~~~~^~~~~~~~~~
fib.cpp:118:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &A[i]);
         ~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 424 KB Output is correct
3 Correct 1 ms 376 KB Output is correct
4 Correct 2 ms 420 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 424 KB Output is correct
3 Correct 1 ms 376 KB Output is correct
4 Correct 2 ms 420 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 292 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 424 KB Output is correct
3 Correct 1 ms 376 KB Output is correct
4 Correct 2 ms 420 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 292 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 380 KB Output is correct
16 Correct 2 ms 424 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 380 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 3 ms 504 KB Output is correct
21 Correct 3 ms 376 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
23 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 1123 ms 20056 KB Output is correct
3 Correct 1109 ms 22264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 424 KB Output is correct
3 Correct 1 ms 376 KB Output is correct
4 Correct 2 ms 420 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 292 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 380 KB Output is correct
16 Correct 2 ms 424 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 380 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 3 ms 504 KB Output is correct
21 Correct 3 ms 376 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
23 Correct 2 ms 376 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 1123 ms 20056 KB Output is correct
26 Correct 1109 ms 22264 KB Output is correct
27 Correct 272 ms 6288 KB Output is correct
28 Correct 500 ms 10288 KB Output is correct
29 Correct 106 ms 12664 KB Output is correct
30 Correct 501 ms 10564 KB Output is correct
31 Execution timed out 4112 ms 195308 KB Time limit exceeded
32 Halted 0 ms 0 KB -