Submission #831057

# Submission time Handle Problem Language Result Execution time Memory
831057 2023-08-19T16:24:49 Z skittles1412 Horses (IOI15_horses) C++17
100 / 100
537 ms 66056 KB
#include "bits/extc++.h"

using namespace std;

template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
    cerr << t;
    ((cerr << " | " << u), ...);
    cerr << endl;
}

#ifdef DEBUG
#define dbg(...)                                              \
    cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]: "; \
    dbgh(__VA_ARGS__)
#else
#define dbg(...)
#define cerr   \
    if (false) \
    cerr
#endif

using i128 = __int128_t;

#define endl "\n"
#define long int64_t
#define sz(x) int(std::size(x))

template <typename T, typename Op = plus<T>>
struct ST {
    int n;
    vector<T> v, arr;
    Op op;

    ST() {}
    ST(const vector<T>& arr, const Op& op = {})
        : n(sz(arr)), v(4 * n), arr(arr), op(op) {
        build(1, 0, n - 1);
    }

    void build(int o, int l, int r) {
        if (l == r) {
            v[o] = arr[l];
            return;
        }

        int mid = (l + r) / 2, lc = o * 2, rc = lc + 1;
        build(lc, l, mid);
        build(rc, mid + 1, r);
        v[o] = op(v[lc], v[rc]);
    }

    void update(int o, int l, int r, int ind, const T& x) {
        if (l == r) {
            v[o] = x;
            return;
        }

        int mid = (l + r) / 2, lc = o * 2, rc = lc + 1;
        if (ind <= mid) {
            update(lc, l, mid, ind, x);
        } else {
            update(rc, mid + 1, r, ind, x);
        }
        v[o] = op(v[lc], v[rc]);
    }

    void update(int ind, const T& x) {
        update(1, 0, n - 1, ind, x);
    }

    T query(int o, int l, int r, int ql, int qr) const {
        if (ql <= l && r <= qr) {
            return v[o];
        }

        int mid = (l + r) / 2, lc = o * 2, rc = lc + 1;
        if (ql <= mid) {
            if (mid < qr) {
                return op(query(lc, l, mid, ql, qr),
                          query(rc, mid + 1, r, ql, qr));
            }
            return query(lc, l, mid, ql, qr);
        }
        return query(rc, mid + 1, r, ql, qr);
    }

    T query(int l, int r) const {
        dbg(l, r);
        return query(1, 0, n - 1, l, r);
    }
};

struct mint {
    static constexpr int MOD = 1e9 + 7;

    static constexpr int norm(long x) {
        x %= MOD;
        if (x < 0) {
            x += MOD;
        }
        return int(x);
    }

    int x;

    constexpr mint() : x(0) {}
    constexpr mint(long x) : x(norm(x)) {}

    static mint from_unchecked(int x) {
        mint m;
        m.x = x;
        return m;
    }

    friend ostream& operator<<(ostream& out, const mint& m) {
        return out << m.x;
    }

    constexpr mint operator-() const {
        if (!x) {
            return {};
        }
        return mint::from_unchecked(MOD - x);
    }

    constexpr mint& operator+=(const mint& m) {
        x += m.x;
        if (x >= MOD) {
            x -= MOD;
        }
        return *this;
    }
    constexpr mint& operator-=(const mint& m) {
        return *this += -m;
    }

    constexpr mint& operator*=(const mint& m) {
        x = int(long(x) * m.x % MOD);
        return *this;
    }

    friend constexpr mint operator+(mint a, const mint& b) {
        return a += b;
    }
    friend constexpr mint operator-(mint a, const mint& b) {
        return a -= b;
    }
    friend constexpr mint operator*(mint a, const mint& b) {
        return a *= b;
    }
};

template <typename T>
struct MaxOp {
    T operator()(const T& a, const T& b) const {
        return max(a, b);
    }
};

struct Solver {
    int n;
    vector<int> arr_x, arr_y;
    set<int> pos_x;
    ST<mint, multiplies<mint>> st_x;
    ST<int, MaxOp<int>> st_y;

    Solver() {}
    Solver(const vector<int>& arr_x, const vector<int>& arr_y)
        : n(sz(arr_x)),
          arr_x(arr_x),
          arr_y(arr_y),
          st_x(({
              vector<mint> v(n);
              for (int i = 0; i < n; i++) {
                  v[i] = arr_x[i];
              }
              v;
          })),
          st_y(arr_y) {
        for (int i = 0; i < n; i++) {
            if (arr_x[i] > 1) {
                pos_x.insert(i);
            }
        }
    }

    int query() {
        long opt = 0;
        mint opt_m = 0;

        auto upd = [&](long co, mint cm) -> void {
            if (co > opt) {
                opt = co;
                opt_m = cm;
            }
        };

        int prv = n;
        auto go = [&](int ind) -> void {
            mint pref_x = st_x.query(0, ind);
            int max_y = st_y.query(ind, prv - 1);
            upd(max_y, pref_x * max_y);
            dbg(ind, max_y);

            prv = ind;
            opt *= arr_x[ind];
        };

        for (auto it = pos_x.rbegin(); it != pos_x.rend() && opt < long(1e9);
             ++it) {
            go(*it);
        }

        if (prv && opt < long(1e9)) {
            assert((!sz(pos_x) && prv == n) ||
                   (sz(pos_x) && prv == *pos_x.begin()));

            mint pref_x = 1;
            int max_y = st_y.query(0, prv - 1);
            upd(max_y, pref_x * max_y);
        }

        return opt_m.x;
    }

    void update_x(int ind, int x) {
        if (arr_x[ind] > 1) {
            pos_x.erase(ind);
        }

        arr_x[ind] = x;
        st_x.update(ind, x);

        if (arr_x[ind] > 1) {
            pos_x.insert(ind);
        }
    }

    void update_y(int ind, int x) {
        arr_y[ind] = x;
        st_y.update(ind, x);
    }
} solver;

int init(int n, int arr_x[], int arr_y[]) {
    solver =
        Solver(vector<int>(arr_x, arr_x + n), vector<int>(arr_y, arr_y + n));
    return solver.query();
}

int updateX(int ind, int x) {
    solver.update_x(ind, x);
    return solver.query();
}

int updateY(int ind, int x) {
    solver.update_y(ind, x);
    return solver.query();
}

Compilation message

horses.cpp: In constructor 'ST<T, Op>::ST(const std::vector<_Tp>&, const Op&)':
horses.cpp:36:40: warning: declaration of 'op' shadows a member of 'ST<T, Op>' [-Wshadow]
   36 |     ST(const vector<T>& arr, const Op& op = {})
      |                              ~~~~~~~~~~^~~~~~~
horses.cpp:33:8: note: shadowed declaration is here
   33 |     Op op;
      |        ^~
horses.cpp:36:25: warning: declaration of 'arr' shadows a member of 'ST<T, Op>' [-Wshadow]
   36 |     ST(const vector<T>& arr, const Op& op = {})
      |        ~~~~~~~~~~~~~~~~~^~~
horses.cpp:32:18: note: shadowed declaration is here
   32 |     vector<T> v, arr;
      |                  ^~~
horses.cpp: In constructor 'constexpr mint::mint(int64_t)':
horses.cpp:108:25: warning: declaration of 'x' shadows a member of 'mint' [-Wshadow]
  108 |     constexpr mint(long x) : x(norm(x)) {}
      |                         ^
horses.cpp:105:9: note: shadowed declaration is here
  105 |     int x;
      |         ^
horses.cpp: In constructor 'constexpr mint::mint(int64_t)':
horses.cpp:108:25: warning: declaration of 'x' shadows a member of 'mint' [-Wshadow]
  108 |     constexpr mint(long x) : x(norm(x)) {}
      |                         ^
horses.cpp:105:9: note: shadowed declaration is here
  105 |     int x;
      |         ^
horses.cpp: In constructor 'constexpr mint::mint(int64_t)':
horses.cpp:108:25: warning: declaration of 'x' shadows a member of 'mint' [-Wshadow]
  108 |     constexpr mint(long x) : x(norm(x)) {}
      |                         ^
horses.cpp:105:9: note: shadowed declaration is here
  105 |     int x;
      |         ^
horses.cpp: In constructor 'Solver::Solver(const std::vector<int>&, const std::vector<int>&)':
horses.cpp:169:57: warning: declaration of 'arr_y' shadows a member of 'Solver' [-Wshadow]
  169 |     Solver(const vector<int>& arr_x, const vector<int>& arr_y)
      |                                      ~~~~~~~~~~~~~~~~~~~^~~~~
horses.cpp:163:24: note: shadowed declaration is here
  163 |     vector<int> arr_x, arr_y;
      |                        ^~~~~
horses.cpp:169:31: warning: declaration of 'arr_x' shadows a member of 'Solver' [-Wshadow]
  169 |     Solver(const vector<int>& arr_x, const vector<int>& arr_y)
      |            ~~~~~~~~~~~~~~~~~~~^~~~~
horses.cpp:163:17: note: shadowed declaration is here
  163 |     vector<int> arr_x, arr_y;
      |                 ^~~~~
horses.cpp: In constructor 'Solver::Solver(const std::vector<int>&, const std::vector<int>&)':
horses.cpp:169:57: warning: declaration of 'arr_y' shadows a member of 'Solver' [-Wshadow]
  169 |     Solver(const vector<int>& arr_x, const vector<int>& arr_y)
      |                                      ~~~~~~~~~~~~~~~~~~~^~~~~
horses.cpp:163:24: note: shadowed declaration is here
  163 |     vector<int> arr_x, arr_y;
      |                        ^~~~~
horses.cpp:169:31: warning: declaration of 'arr_x' shadows a member of 'Solver' [-Wshadow]
  169 |     Solver(const vector<int>& arr_x, const vector<int>& arr_y)
      |            ~~~~~~~~~~~~~~~~~~~^~~~~
horses.cpp:163:17: note: shadowed declaration is here
  163 |     vector<int> arr_x, arr_y;
      |                 ^~~~~
horses.cpp: In constructor 'Solver::Solver(const std::vector<int>&, const std::vector<int>&)':
horses.cpp:169:57: warning: declaration of 'arr_y' shadows a member of 'Solver' [-Wshadow]
  169 |     Solver(const vector<int>& arr_x, const vector<int>& arr_y)
      |                                      ~~~~~~~~~~~~~~~~~~~^~~~~
horses.cpp:163:24: note: shadowed declaration is here
  163 |     vector<int> arr_x, arr_y;
      |                        ^~~~~
horses.cpp:169:31: warning: declaration of 'arr_x' shadows a member of 'Solver' [-Wshadow]
  169 |     Solver(const vector<int>& arr_x, const vector<int>& arr_y)
      |            ~~~~~~~~~~~~~~~~~~~^~~~~
horses.cpp:163:17: note: shadowed declaration is here
  163 |     vector<int> arr_x, arr_y;
      |                 ^~~~~
horses.cpp: In instantiation of 'ST<T, Op>::ST(const std::vector<_Tp>&, const Op&) [with T = mint; Op = std::multiplies<mint>]':
horses.cpp:180:21:   required from here
horses.cpp:36:40: warning: declaration of 'op' shadows a member of 'ST<mint, std::multiplies<mint> >' [-Wshadow]
   36 |     ST(const vector<T>& arr, const Op& op = {})
      |                              ~~~~~~~~~~^~~~~~~
horses.cpp:33:8: note: shadowed declaration is here
   33 |     Op op;
      |        ^~
horses.cpp:36:25: warning: declaration of 'arr' shadows a member of 'ST<mint, std::multiplies<mint> >' [-Wshadow]
   36 |     ST(const vector<T>& arr, const Op& op = {})
      |        ~~~~~~~~~~~~~~~~~^~~
horses.cpp:32:18: note: shadowed declaration is here
   32 |     vector<T> v, arr;
      |                  ^~~
horses.cpp:36:40: warning: declaration of 'op' shadows a member of 'ST<mint, std::multiplies<mint> >' [-Wshadow]
   36 |     ST(const vector<T>& arr, const Op& op = {})
      |                              ~~~~~~~~~~^~~~~~~
horses.cpp:33:8: note: shadowed declaration is here
   33 |     Op op;
      |        ^~
horses.cpp:36:25: warning: declaration of 'arr' shadows a member of 'ST<mint, std::multiplies<mint> >' [-Wshadow]
   36 |     ST(const vector<T>& arr, const Op& op = {})
      |        ~~~~~~~~~~~~~~~~~^~~
horses.cpp:32:18: note: shadowed declaration is here
   32 |     vector<T> v, arr;
      |                  ^~~
horses.cpp:36:40: warning: declaration of 'op' shadows a member of 'ST<mint, std::multiplies<mint> >' [-Wshadow]
   36 |     ST(const vector<T>& arr, const Op& op = {})
      |                              ~~~~~~~~~~^~~~~~~
horses.cpp:33:8: note: shadowed declaration is here
   33 |     Op op;
      |        ^~
horses.cpp:36:25: warning: declaration of 'arr' shadows a member of 'ST<mint, std::multiplies<mint> >' [-Wshadow]
   36 |     ST(const vector<T>& arr, const Op& op = {})
      |        ~~~~~~~~~~~~~~~~~^~~
horses.cpp:32:18: note: shadowed declaration is here
   32 |     vector<T> v, arr;
      |                  ^~~
horses.cpp: In instantiation of 'ST<T, Op>::ST(const std::vector<_Tp>&, const Op&) [with T = int; Op = MaxOp<int>]':
horses.cpp:180:21:   required from here
horses.cpp:36:40: warning: declaration of 'op' shadows a member of 'ST<int, MaxOp<int> >' [-Wshadow]
   36 |     ST(const vector<T>& arr, const Op& op = {})
      |                              ~~~~~~~~~~^~~~~~~
horses.cpp:33:8: note: shadowed declaration is here
   33 |     Op op;
      |        ^~
horses.cpp:36:25: warning: declaration of 'arr' shadows a member of 'ST<int, MaxOp<int> >' [-Wshadow]
   36 |     ST(const vector<T>& arr, const Op& op = {})
      |        ~~~~~~~~~~~~~~~~~^~~
horses.cpp:32:18: note: shadowed declaration is here
   32 |     vector<T> v, arr;
      |                  ^~~
horses.cpp:36:40: warning: declaration of 'op' shadows a member of 'ST<int, MaxOp<int> >' [-Wshadow]
   36 |     ST(const vector<T>& arr, const Op& op = {})
      |                              ~~~~~~~~~~^~~~~~~
horses.cpp:33:8: note: shadowed declaration is here
   33 |     Op op;
      |        ^~
horses.cpp:36:25: warning: declaration of 'arr' shadows a member of 'ST<int, MaxOp<int> >' [-Wshadow]
   36 |     ST(const vector<T>& arr, const Op& op = {})
      |        ~~~~~~~~~~~~~~~~~^~~
horses.cpp:32:18: note: shadowed declaration is here
   32 |     vector<T> v, arr;
      |                  ^~~
horses.cpp:36:40: warning: declaration of 'op' shadows a member of 'ST<int, MaxOp<int> >' [-Wshadow]
   36 |     ST(const vector<T>& arr, const Op& op = {})
      |                              ~~~~~~~~~~^~~~~~~
horses.cpp:33:8: note: shadowed declaration is here
   33 |     Op op;
      |        ^~
horses.cpp:36:25: warning: declaration of 'arr' shadows a member of 'ST<int, MaxOp<int> >' [-Wshadow]
   36 |     ST(const vector<T>& arr, const Op& op = {})
      |        ~~~~~~~~~~~~~~~~~^~~
horses.cpp:32:18: note: shadowed declaration is here
   32 |     vector<T> v, arr;
      |                  ^~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 1 ms 304 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 3 ms 324 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 2 ms 340 KB Output is correct
32 Correct 3 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 537 ms 55100 KB Output is correct
2 Correct 250 ms 55060 KB Output is correct
3 Correct 279 ms 55068 KB Output is correct
4 Correct 261 ms 55008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 312 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 3 ms 340 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 2 ms 340 KB Output is correct
31 Correct 2 ms 340 KB Output is correct
32 Correct 3 ms 316 KB Output is correct
33 Correct 40 ms 35448 KB Output is correct
34 Correct 39 ms 35464 KB Output is correct
35 Correct 153 ms 65960 KB Output is correct
36 Correct 143 ms 65952 KB Output is correct
37 Correct 72 ms 33652 KB Output is correct
38 Correct 81 ms 46332 KB Output is correct
39 Correct 34 ms 33592 KB Output is correct
40 Correct 135 ms 60900 KB Output is correct
41 Correct 57 ms 33600 KB Output is correct
42 Correct 69 ms 33700 KB Output is correct
43 Correct 131 ms 61360 KB Output is correct
44 Correct 139 ms 61396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 228 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 316 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 3 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 2 ms 340 KB Output is correct
32 Correct 3 ms 448 KB Output is correct
33 Correct 528 ms 57272 KB Output is correct
34 Correct 243 ms 66040 KB Output is correct
35 Correct 258 ms 57144 KB Output is correct
36 Correct 255 ms 61040 KB Output is correct
37 Correct 42 ms 35512 KB Output is correct
38 Correct 37 ms 35460 KB Output is correct
39 Correct 152 ms 65948 KB Output is correct
40 Correct 142 ms 65824 KB Output is correct
41 Correct 73 ms 33668 KB Output is correct
42 Correct 86 ms 46356 KB Output is correct
43 Correct 33 ms 33668 KB Output is correct
44 Correct 129 ms 60896 KB Output is correct
45 Correct 67 ms 33584 KB Output is correct
46 Correct 78 ms 33668 KB Output is correct
47 Correct 127 ms 61508 KB Output is correct
48 Correct 128 ms 61416 KB Output is correct
49 Correct 110 ms 35552 KB Output is correct
50 Correct 85 ms 35556 KB Output is correct
51 Correct 277 ms 66048 KB Output is correct
52 Correct 175 ms 66056 KB Output is correct
53 Correct 503 ms 33732 KB Output is correct
54 Correct 211 ms 46644 KB Output is correct
55 Correct 94 ms 33760 KB Output is correct
56 Correct 204 ms 61060 KB Output is correct
57 Correct 361 ms 33724 KB Output is correct
58 Correct 481 ms 33668 KB Output is correct
59 Correct 139 ms 61380 KB Output is correct