Submission #519110

# Submission time Handle Problem Language Result Execution time Memory
519110 2022-01-25T16:40:27 Z tabr Horses (IOI15_horses) C++17
100 / 100
378 ms 62952 KB
#include <bits/stdc++.h>
using namespace std;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif

template <long long mod>
struct modular {
    long long value;
    modular(long long x = 0) {
        value = x % mod;
        if (value < 0) value += mod;
    }
    modular& operator+=(const modular& other) {
        if ((value += other.value) >= mod) value -= mod;
        return *this;
    }
    modular& operator-=(const modular& other) {
        if ((value -= other.value) < 0) value += mod;
        return *this;
    }
    modular& operator*=(const modular& other) {
        value = value * other.value % mod;
        return *this;
    }
    modular& operator/=(const modular& other) {
        long long a = 0, b = 1, c = other.value, m = mod;
        while (c != 0) {
            long long t = m / c;
            m -= t * c;
            swap(c, m);
            a -= t * b;
            swap(a, b);
        }
        a %= mod;
        if (a < 0) a += mod;
        value = value * a % mod;
        return *this;
    }
    friend modular operator+(const modular& lhs, const modular& rhs) { return modular(lhs) += rhs; }
    friend modular operator-(const modular& lhs, const modular& rhs) { return modular(lhs) -= rhs; }
    friend modular operator*(const modular& lhs, const modular& rhs) { return modular(lhs) *= rhs; }
    friend modular operator/(const modular& lhs, const modular& rhs) { return modular(lhs) /= rhs; }
    modular& operator++() { return *this += 1; }
    modular& operator--() { return *this -= 1; }
    modular operator++(int) {
        modular res(*this);
        *this += 1;
        return res;
    }
    modular operator--(int) {
        modular res(*this);
        *this -= 1;
        return res;
    }
    modular operator-() const { return modular(-value); }
    bool operator==(const modular& rhs) const { return value == rhs.value; }
    bool operator!=(const modular& rhs) const { return value != rhs.value; }
    bool operator<(const modular& rhs) const { return value < rhs.value; }
};
template <long long mod>
string to_string(const modular<mod>& x) {
    return to_string(x.value);
}
template <long long mod>
ostream& operator<<(ostream& stream, const modular<mod>& x) {
    return stream << x.value;
}
template <long long mod>
istream& operator>>(istream& stream, modular<mod>& x) {
    stream >> x.value;
    x.value %= mod;
    if (x.value < 0) x.value += mod;
    return stream;
}

constexpr long long mod = (long long) 1e9 + 7;
using mint = modular<mod>;

mint power(mint a, long long n) {
    mint res = 1;
    while (n > 0) {
        if (n & 1) {
            res *= a;
        }
        a *= a;
        n >>= 1;
    }
    return res;
}

vector<mint> fact(1, 1);
vector<mint> finv(1, 1);

mint C(int n, int k) {
    if (n < k || k < 0) {
        return mint(0);
    }
    while ((int) fact.size() < n + 1) {
        fact.emplace_back(fact.back() * (int) fact.size());
        finv.emplace_back(mint(1) / fact.back());
    }
    return fact[n] * finv[k] * finv[n - k];
}

struct segtree {
    struct T {
        int mx;
        mint prod;
    };
    int n;
    int size;
    vector<T> node;

    T e() {
        return T{-1, mint(1)};
    }

    T op(T x, T y) {
        return T{max(x.mx, y.mx), x.prod * y.prod};
    }

    segtree() : segtree(0) {}
    segtree(int _n) {
        build(vector<T>(_n, e()));
    }
    segtree(const vector<T>& v) {
        build(v);
    }

    void build(const vector<T>& v) {
        n = (int) v.size();
        if (n <= 1) {
            size = n;
        } else {
            size = 1 << (32 - __builtin_clz(n - 1));
        }
        node.resize(2 * size, e());
        for (int i = 0; i < n; i++) {
            node[i + size] = v[i];
        }
        for (int i = size - 1; i > 0; i--) {
            node[i] = op(node[2 * i], node[2 * i + 1]);
        }
    }

    void set(int p, T v) {
        assert(0 <= p && p < n);
        p += size;
        node[p] = v;  // update
        // node[p] += v;  // add
        while (p > 1) {
            p >>= 1;
            node[p] = op(node[2 * p], node[2 * p + 1]);
        }
    }

    T get(int l, int r) {
        assert(0 <= l && l <= r && r <= n);
        T vl = e();
        T vr = e();
        l += size;
        r += size;
        while (l < r) {
            if (l & 1) {
                vl = op(vl, node[l++]);
            }
            if (r & 1) {
                vr = op(node[--r], vr);
            }
            l >>= 1;
            r >>= 1;
        }
        return op(vl, vr);
    }

    T get(int p) {
        assert(0 <= p && p < n);
        return node[p + size];
    }
};

int n;
int* x;
int* y;
set<int> st;
segtree seg;

int f() {
    __int128_t prod = 1;
    vector<int> c;
    auto iter = st.rbegin();
    while (prod < (int) 1e9 && iter != st.rend()) {
        c.emplace_back(*iter);
        prod *= x[*iter];
        iter++;
    }
    __int128_t t = 1;
    for (int i : c) {
        int s = seg.get(i, n).mx;
        if (t < prod * s) {
            t = prod * s;
        }
        prod /= x[i];
    }
    mint ans = (long long) (t % mod);
    ans *= seg.get(0, c.back()).prod;
    return (int) ans.value;
}

int init(int _n, int _x[], int _y[]) {
    n = _n;
    x = _x;
    y = _y;
    st.emplace(0);
    for (int i = 0; i < n; i++) {
        if (x[i] > 1) {
            st.emplace(i);
        }
    }
    seg = segtree(n);
    for (int i = 0; i < n; i++) {
        seg.set(i, segtree::T{y[i], x[i]});
    }
    return f();
}

int updateX(int pos, int val) {
    x[pos] = val;
    if (x[pos] > 1) {
        st.emplace(pos);
    } else if (pos != 0) {
        st.erase(pos);
    }
    seg.set(pos, segtree::T{y[pos], x[pos]});
    return f();
}

int updateY(int pos, int val) {
    y[pos] = val;
    seg.set(pos, segtree::T{y[pos], x[pos]});
    return f();
}

#ifdef tabr
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int _n = 3;
    int _x[] = {2, 1, 3};
    int _y[] = {3, 4, 1};
    cout << init(_n, _x, _y) << '\n';
    cout << updateY(1, 2) << '\n';
    return 0;
}
#endif

Compilation message

horses.cpp: In instantiation of 'modular<mod>::modular(long long int) [with long long int mod = 1000000007]':
horses.cpp:83:16:   required from here
horses.cpp:12:23: warning: declaration of 'x' shadows a global declaration [-Wshadow]
   12 |     modular(long long x = 0) {
      |             ~~~~~~~~~~^~~~~
horses.cpp:186:6: note: shadowed declaration is here
  186 | int* x;
      |      ^
horses.cpp:12:23: warning: declaration of 'x' shadows a global declaration [-Wshadow]
   12 |     modular(long long x = 0) {
      |             ~~~~~~~~~~^~~~~
horses.cpp:186:6: note: shadowed declaration is here
  186 | int* x;
      |      ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 0 ms 204 KB Output is correct
21 Correct 0 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 296 KB Output is correct
25 Correct 2 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 2 ms 332 KB Output is correct
28 Correct 2 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 2 ms 332 KB Output is correct
31 Correct 1 ms 300 KB Output is correct
32 Correct 2 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 322 ms 51928 KB Output is correct
2 Correct 334 ms 51908 KB Output is correct
3 Correct 327 ms 51912 KB Output is correct
4 Correct 372 ms 51844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 0 ms 204 KB Output is correct
21 Correct 0 ms 204 KB Output is correct
22 Correct 0 ms 204 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 452 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 2 ms 332 KB Output is correct
28 Correct 1 ms 284 KB Output is correct
29 Correct 1 ms 300 KB Output is correct
30 Correct 1 ms 324 KB Output is correct
31 Correct 2 ms 332 KB Output is correct
32 Correct 2 ms 304 KB Output is correct
33 Correct 97 ms 32328 KB Output is correct
34 Correct 98 ms 32324 KB Output is correct
35 Correct 236 ms 62812 KB Output is correct
36 Correct 232 ms 62896 KB Output is correct
37 Correct 99 ms 30532 KB Output is correct
38 Correct 148 ms 43248 KB Output is correct
39 Correct 86 ms 30404 KB Output is correct
40 Correct 219 ms 57760 KB Output is correct
41 Correct 88 ms 30448 KB Output is correct
42 Correct 93 ms 30572 KB Output is correct
43 Correct 215 ms 58296 KB Output is correct
44 Correct 215 ms 58276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Correct 0 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 0 ms 204 KB Output is correct
21 Correct 0 ms 204 KB Output is correct
22 Correct 0 ms 204 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 2 ms 332 KB Output is correct
28 Correct 1 ms 296 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 2 ms 284 KB Output is correct
33 Correct 302 ms 54076 KB Output is correct
34 Correct 336 ms 62912 KB Output is correct
35 Correct 310 ms 54000 KB Output is correct
36 Correct 378 ms 57904 KB Output is correct
37 Correct 95 ms 32316 KB Output is correct
38 Correct 94 ms 32292 KB Output is correct
39 Correct 255 ms 62792 KB Output is correct
40 Correct 227 ms 62748 KB Output is correct
41 Correct 99 ms 30444 KB Output is correct
42 Correct 155 ms 43204 KB Output is correct
43 Correct 99 ms 30448 KB Output is correct
44 Correct 222 ms 57740 KB Output is correct
45 Correct 92 ms 30448 KB Output is correct
46 Correct 95 ms 30516 KB Output is correct
47 Correct 210 ms 58308 KB Output is correct
48 Correct 215 ms 58272 KB Output is correct
49 Correct 167 ms 32464 KB Output is correct
50 Correct 166 ms 32376 KB Output is correct
51 Correct 323 ms 62904 KB Output is correct
52 Correct 277 ms 62952 KB Output is correct
53 Correct 278 ms 30568 KB Output is correct
54 Correct 223 ms 43212 KB Output is correct
55 Correct 132 ms 30576 KB Output is correct
56 Correct 283 ms 57820 KB Output is correct
57 Correct 164 ms 30532 KB Output is correct
58 Correct 220 ms 30580 KB Output is correct
59 Correct 216 ms 58268 KB Output is correct