Submission #70665

# Submission time Handle Problem Language Result Execution time Memory
70665 2018-08-23T08:22:09 Z 강태규(#2184) Fibonacci representations (CEOI18_fib) C++11
65 / 100
4000 ms 59200 KB
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <unordered_map>
#include <functional>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cstdlib>

using namespace std;
typedef long long llong;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<llong, llong> pll;

int n;
int a[100000];

int cp[100001];

const int mod = 1e9 + 7;

struct matrix {
    int x[2][2];
    matrix() {
        for (int i = 0; i < 2; ++i) {
            for (int j = 0; j < 2; ++j) {
                x[i][j] = (i == j);
            }
        }
    }
    matrix operator*(const matrix &p) const {
        matrix ret;
        for (int i = 0; i < 2; ++i) {
            for (int j = 0; j < 2; ++j) {
                ret.x[i][j] = 0;
                for (int k = 0; k < 2; ++k) {
                    ret.x[i][j] += (llong)x[i][k] * p.x[k][j] % mod;
                    ret.x[i][j] %= mod;
                }
            }
        }
        return ret;
    }
};

struct node {
    matrix dp;
    int s, e, a;

    void init(int x, int c = 1) {
        s = e = x; a = c;
    }

    node operator+(const node &p) const {
        if (a == 0) return p;
        if (p.a == 0) return *this;

        node ret;
        ret.a = 1;
        ret.s = s;
        ret.e = p.e;
        int d = p.s - e;
        matrix m;
        m.x[0][0] = 1;
        m.x[0][1] = 1;
        m.x[1][0] = (d - 1) / 2;
        m.x[1][1] = d / 2;
        ret.dp = p.dp * m * dp;
        return ret;
    }
} seg[1 << 18];

void active(int i, int s, int e, int x) {
    if (s == e) {
        seg[i].init(cp[s]);
        return;
    }
    int m = (s + e) / 2;
    if (x <= cp[m]) active(i << 1, s, m, x);
    else active(i << 1 | 1, m + 1, e, x);
    seg[i] = seg[i << 1] + seg[i << 1 | 1];
}

void solve0() {
    for (int i = 0; i < n; ++i) cp[i + 1] = a[i];
    sort(cp + 1, cp + (n + 1));
    active(1, 0, n, 0);
    for (int i = 0; i < n; ++i) {
        active(1, 0, n, a[i]);
        printf("%d\n", (seg[1].dp.x[0][0] + seg[1].dp.x[1][0]) % mod);
    }
}

const int inf = 2e9;
struct dsnode {
    node x;
    dsnode *l, *r;
    dsnode() : l(0), r(0) {}
} * rt;

void update(dsnode * &i, int s, int e, int x, int a) {
    if (i == 0) i = new dsnode();
    if (s == e) {
        i->x.init(s, a);
        return;
    }
    int m = ((llong)s + e) / 2;
    if (x <= m) update(i->l, s, m, x, a);
    else update(i->r, m + 1, e, x, a);
    i->x.init(0, 0);
    if (i->l) i->x = i->x + i->l->x;
    if (i->r) i->x = i->x + i->r->x;
}

void addSet(set<int> &mp, int x) {
    if (x == 0) x = 1;
    if (x == -1) return;
    if (mp.count(x)) {
        update(rt, 0, inf, x, 0);
        mp.erase(x);
        addSet(mp, x - 2);
        addSet(mp, x + 1);
        return;
    }
    if (mp.count(x + 1)) {
        update(rt, 0, inf, x + 1, 0);
        mp.erase(x + 1);
        addSet(mp, x + 2);
        return;
    }
    if (mp.count(x - 1)) {
        update(rt, 0, inf, x - 1, 0);
        mp.erase(x - 1);
        addSet(mp, x + 1);
        return;
    }
    update(rt, 0, inf, x, 1);
    mp.insert(x);
}

void solve1() {
    rt = new dsnode();
    update(rt, 0, inf, 0, 1);
    set<int> mp;
    for (int i = 0; i < n; ++i) {
        addSet(mp, a[i]);
        printf("%d\n", (rt->x.dp.x[0][0] + rt->x.dp.x[1][0]) % mod);
    }
}

int main() {
    scanf("%d", &n);
    int md = 0;
    {
        set<int> mp;
        for (int i = 0; i < n; ++i) {
            scanf("%d", a + i);
            mp.insert(a[i]);
        }
        if (mp.size() == n) {
            md = 1;
            int pr = -1;
            for (int i : mp) {
                if (i - pr == 1) {
                    md = 0;
                    break;
                }
                pr = i;
            }
        }
    }
    if (md) solve0();
    else solve1();
    return 0;
}

Compilation message

fib.cpp: In function 'int main()':
fib.cpp:166:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         if (mp.size() == n) {
             ~~~~~~~~~~^~~~
fib.cpp:158:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &n);
     ~~~~~^~~~~~~~~~
fib.cpp:163:18: 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 8 ms 7544 KB Output is correct
2 Correct 7 ms 7656 KB Output is correct
3 Correct 7 ms 7656 KB Output is correct
4 Correct 7 ms 7680 KB Output is correct
5 Correct 7 ms 7800 KB Output is correct
6 Correct 8 ms 7852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 7544 KB Output is correct
2 Correct 7 ms 7656 KB Output is correct
3 Correct 7 ms 7656 KB Output is correct
4 Correct 7 ms 7680 KB Output is correct
5 Correct 7 ms 7800 KB Output is correct
6 Correct 8 ms 7852 KB Output is correct
7 Correct 8 ms 7852 KB Output is correct
8 Correct 8 ms 7852 KB Output is correct
9 Correct 8 ms 7852 KB Output is correct
10 Correct 7 ms 7852 KB Output is correct
11 Correct 8 ms 7852 KB Output is correct
12 Correct 8 ms 7852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 7852 KB Output is correct
2 Correct 8 ms 7852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 7544 KB Output is correct
2 Correct 7 ms 7656 KB Output is correct
3 Correct 7 ms 7656 KB Output is correct
4 Correct 7 ms 7680 KB Output is correct
5 Correct 7 ms 7800 KB Output is correct
6 Correct 8 ms 7852 KB Output is correct
7 Correct 8 ms 7852 KB Output is correct
8 Correct 8 ms 7852 KB Output is correct
9 Correct 8 ms 7852 KB Output is correct
10 Correct 7 ms 7852 KB Output is correct
11 Correct 8 ms 7852 KB Output is correct
12 Correct 8 ms 7852 KB Output is correct
13 Correct 8 ms 7852 KB Output is correct
14 Correct 8 ms 7852 KB Output is correct
15 Correct 8 ms 7852 KB Output is correct
16 Correct 8 ms 7852 KB Output is correct
17 Correct 8 ms 7852 KB Output is correct
18 Correct 7 ms 7852 KB Output is correct
19 Correct 7 ms 7852 KB Output is correct
20 Correct 13 ms 7852 KB Output is correct
21 Correct 11 ms 7852 KB Output is correct
22 Correct 10 ms 7852 KB Output is correct
23 Correct 10 ms 7916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 7916 KB Output is correct
2 Correct 410 ms 14188 KB Output is correct
3 Correct 290 ms 14272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 7544 KB Output is correct
2 Correct 7 ms 7656 KB Output is correct
3 Correct 7 ms 7656 KB Output is correct
4 Correct 7 ms 7680 KB Output is correct
5 Correct 7 ms 7800 KB Output is correct
6 Correct 8 ms 7852 KB Output is correct
7 Correct 8 ms 7852 KB Output is correct
8 Correct 8 ms 7852 KB Output is correct
9 Correct 8 ms 7852 KB Output is correct
10 Correct 7 ms 7852 KB Output is correct
11 Correct 8 ms 7852 KB Output is correct
12 Correct 8 ms 7852 KB Output is correct
13 Correct 8 ms 7852 KB Output is correct
14 Correct 8 ms 7852 KB Output is correct
15 Correct 8 ms 7852 KB Output is correct
16 Correct 8 ms 7852 KB Output is correct
17 Correct 8 ms 7852 KB Output is correct
18 Correct 7 ms 7852 KB Output is correct
19 Correct 7 ms 7852 KB Output is correct
20 Correct 13 ms 7852 KB Output is correct
21 Correct 11 ms 7852 KB Output is correct
22 Correct 10 ms 7852 KB Output is correct
23 Correct 10 ms 7916 KB Output is correct
24 Correct 9 ms 7916 KB Output is correct
25 Correct 410 ms 14188 KB Output is correct
26 Correct 290 ms 14272 KB Output is correct
27 Correct 181 ms 40172 KB Output is correct
28 Correct 322 ms 59200 KB Output is correct
29 Correct 124 ms 59200 KB Output is correct
30 Correct 272 ms 59200 KB Output is correct
31 Execution timed out 4027 ms 59200 KB Time limit exceeded
32 Halted 0 ms 0 KB -