답안 #529222

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
529222 2022-02-22T13:11:19 Z Cyanmond 말 (IOI15_horses) C++17
100 / 100
364 ms 64068 KB
// clang-format off
#include <bits/stdc++.h>

using i64 = int64_t;

template <typename T> bool setmax(T &v, const T a) {
    if (v < a) {
        v = a;
        return true;
    } else {
        return false;
    }
}

int N;
std::vector<int> X, Y;

constexpr i64 mod = 1000000007;
constexpr int max_v = 1000000000;

i64 mul(i64 v, i64 d) {
    return v * d % mod;
}

template <class M>
class SegmentTree {
    using T = typename M::Type;
    int n, size;
    std::vector<T> data;

    void update(const int i) {
        assert(0 <= i and i < size);
        data[i] = M::op(data[2 * i], data[2 * i + 1]);
    }

  public:
    SegmentTree() = default;
    SegmentTree(int n_) : n(n_) {
        size = 1;
        while (size < n) size <<= 1;
        data.assign(2 * size, M::id());
    }

    void set(int i, const T v) {
        assert(0 <= i and i < n);
        i += size;
        data[i] = v;
        while (i != 1) {
            i >>= 1;
            update(i);
        }
    }

    T fold(int l, int r) {
        assert(0 <= l and l <= r and r <= n);
        T vl = M::id(), vr = M::id();
        for (l += size, r += size; l < r; l >>= 1, r >>= 1) {
            if (l & 1) vl = M::op(vl, data[l++]);
            if (r & 1) vr = M::op(data[--r], vr);
        }
        return M::op(vl, vr);
    }

    template <class F> int min_left(int r, const F& f) {
        assert(0 <= r and r <= n);
        assert(f(M::id()));
        if (r == 0) return 0;
        r += size;
        T p = M::id();
        do {
            --r;
            while (r > 1 and (r & 1)) r >>= 1;
            if (not f(M::op(data[r], p))) {
                while (r < size) {
                    r = 2 * r + 1;
                    if (f(M::op(data[r], p))) p = M::op(data[r--], p);
                }
                return r + 1 - size;
            }
            p = M::op(data[r], p);
        } while ((r & -r) != r);
        return 0;
    }
};

struct Monoid_Mul {
    using Type = i64;
    static Type op(Type a, Type b) { return mul(a, b); }
    static Type id() { return 1; }  
};

struct Monoid_Max {
    using Type = std::pair<i64, int>;
    static Type op(Type a, Type b) { return std::max(a, b); }
    static Type id() { return {0, 0}; }
};

SegmentTree<Monoid_Mul> mulseg;
SegmentTree<Monoid_Max> xseg, yseg;

int calc() {
    i64 v = 1;
    double best = (double)Y[N - 1];
    int best_pos = N - 1, r = N;
    while (v <= max_v and r != 0) {
        int nxt = xseg.min_left(r, [](const std::pair<i64, int> p) { return p.first <= 1; });
        if (nxt == 0) {
            if (setmax(best, (double)yseg.fold(nxt, r).first / (double)v)) {
                best_pos = yseg.fold(nxt, r).second;
            }
            break;
        } else {
            --nxt;
            if (setmax(best, (double)yseg.fold(nxt, r).first / (double)v)) {
                best_pos = yseg.fold(nxt, r).second;
            }
            v *= X[nxt];
            r = nxt;
        }
    }
    /*
    for (int i = N - 2; i >= 0; --i) {
        v *= X[i + 1];
        if (setmax(best, (double)Y[i] / (double)v)) {
            best_pos = i;
        }
        if (v > max_v) break;
    }
    */
    return (int)mul(mulseg.fold(0, best_pos + 1), Y[best_pos]);
}

int minit(int n, std::vector<int> x, std::vector<int> y) {
    N = n;
    X = std::move(x);
    Y = std::move(y);
    mulseg = SegmentTree<Monoid_Mul>(N);
    xseg = yseg = SegmentTree<Monoid_Max>(N);
    for (int i = 0; i < N; ++i) {
        mulseg.set(i, X[i]);
        xseg.set(i, {X[i], i});
        yseg.set(i, {Y[i], i});
    }
    return calc();
}

int mupdateX(int pos, int val) {
    X[pos] = val;
    mulseg.set(pos, val);
    xseg.set(pos, {X[pos], pos});
    return calc();
}

int mupdateY(int pos, int val) {
    Y[pos] = val;
    yseg.set(pos, {Y[pos], pos});
    return calc();
}

int init(int N, int X[], int Y[]) {
    std::vector<int> x(N), y(N);
    for (int i = 0; i < N; ++i) {
        x[i] = X[i];
        y[i] = Y[i];
    }
    return minit(N, x, y);
}

int updateX(int pos, int val) {
    return mupdateX(pos, val);
}

int updateY(int pos, int val) {
    return mupdateY(pos, val);
}

Compilation message

horses.cpp: In function 'int init(int, int*, int*)':
horses.cpp:160:30: warning: declaration of 'Y' shadows a global declaration [-Wshadow]
  160 | int init(int N, int X[], int Y[]) {
      |                          ~~~~^~~
horses.cpp:16:21: note: shadowed declaration is here
   16 | std::vector<int> X, Y;
      |                     ^
horses.cpp:160:21: warning: declaration of 'X' shadows a global declaration [-Wshadow]
  160 | int init(int N, int X[], int Y[]) {
      |                 ~~~~^~~
horses.cpp:16:18: note: shadowed declaration is here
   16 | std::vector<int> X, Y;
      |                  ^
horses.cpp:160:14: warning: declaration of 'N' shadows a global declaration [-Wshadow]
  160 | int init(int N, int X[], int Y[]) {
      |          ~~~~^
horses.cpp:15:5: note: shadowed declaration is here
   15 | int N;
      |     ^
# 결과 실행 시간 메모리 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 1 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 1 ms 204 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 1 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 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 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 1 ms 204 KB Output is correct
5 Correct 1 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 1 ms 268 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 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 1 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 0 ms 204 KB Output is correct
20 Correct 0 ms 204 KB Output is correct
21 Correct 1 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 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 396 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 388 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 2 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 293 ms 53088 KB Output is correct
2 Correct 297 ms 53144 KB Output is correct
3 Correct 299 ms 53060 KB Output is correct
4 Correct 285 ms 53092 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 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 0 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 0 ms 204 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 0 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 332 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 332 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 332 KB Output is correct
33 Correct 219 ms 52996 KB Output is correct
34 Correct 219 ms 53116 KB Output is correct
35 Correct 238 ms 53096 KB Output is correct
36 Correct 254 ms 53060 KB Output is correct
37 Correct 226 ms 53192 KB Output is correct
38 Correct 209 ms 53060 KB Output is correct
39 Correct 225 ms 53092 KB Output is correct
40 Correct 223 ms 58888 KB Output is correct
41 Correct 254 ms 55016 KB Output is correct
42 Correct 211 ms 55080 KB Output is correct
43 Correct 226 ms 59480 KB Output is correct
44 Correct 242 ms 59512 KB Output is correct
# 결과 실행 시간 메모리 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 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 1 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 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 244 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 332 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 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 2 ms 332 KB Output is correct
32 Correct 2 ms 372 KB Output is correct
33 Correct 270 ms 53100 KB Output is correct
34 Correct 301 ms 53136 KB Output is correct
35 Correct 277 ms 53060 KB Output is correct
36 Correct 312 ms 53088 KB Output is correct
37 Correct 215 ms 53060 KB Output is correct
38 Correct 222 ms 53092 KB Output is correct
39 Correct 273 ms 53084 KB Output is correct
40 Correct 239 ms 53040 KB Output is correct
41 Correct 225 ms 53188 KB Output is correct
42 Correct 214 ms 53088 KB Output is correct
43 Correct 225 ms 53092 KB Output is correct
44 Correct 221 ms 59008 KB Output is correct
45 Correct 214 ms 55024 KB Output is correct
46 Correct 222 ms 55108 KB Output is correct
47 Correct 221 ms 59384 KB Output is correct
48 Correct 221 ms 59476 KB Output is correct
49 Correct 303 ms 57052 KB Output is correct
50 Correct 298 ms 57048 KB Output is correct
51 Correct 302 ms 64068 KB Output is correct
52 Correct 279 ms 64068 KB Output is correct
53 Correct 364 ms 55236 KB Output is correct
54 Correct 267 ms 56152 KB Output is correct
55 Correct 276 ms 55160 KB Output is correct
56 Correct 290 ms 59172 KB Output is correct
57 Correct 279 ms 55236 KB Output is correct
58 Correct 302 ms 55280 KB Output is correct
59 Correct 250 ms 59472 KB Output is correct