Submission #1000686

# Submission time Handle Problem Language Result Execution time Memory
1000686 2024-06-18T07:14:43 Z ProtonDecay314 Horses (IOI15_horses) C++17
100 / 100
905 ms 136656 KB
/*
IOI 2015: Horses
(AC)

Learnings:
1. Combine after recursing in a segtree
2. Don't forget to combine after recursing in a segtree
3. Don't forget to combine after recursing in a segtree

Proof of correctness:
Suppose that X[i + 1]X[i + 2]...X[j]Y[j] > Y[i]

Thus, in this case, it makes sense to keep the horse in year i and sell it in year j

But the cost of selling a horse is independent of the number of horses you sell

Thus, if it makes sense to keep one horse, it makes sense to keep ALL horses since
every horse kept would yield the same return

Finally, notice that we can multiply both sides of the inequality above by X[0]X[1]...X[i]:

X[0]X[1]...X[i]X[i + 1]X[i + 2]...X[j]Y[j] > X[0]X[1]...X[i]Y[i]

Thus, it is indeed optimal to pick the year i with the largest X[0]X[1]...X[i]Y[i]
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vll;

const ll MOD = 1'000'000'007ll;
const ll INF = 1'000'000'008ll;
vll y_arr;

class TreeX {
    public:
        ll l, r, v, vmod;
        TreeX* lt;
        TreeX* rt;

        TreeX(ll a_l, ll a_r): l(a_l), r(a_r), v(1), vmod(1), lt(nullptr), rt(nullptr) {};

        static inline ll v_combine(ll a, ll b) {
            return min(a * b, INF);
        }

        static inline ll vmod_combine(ll a, ll b) {
            return (a * b) % MOD;
        }

        void combine() {
            v = TreeX::v_combine(lt->v, rt->v);
            vmod = TreeX::vmod_combine(lt->vmod, rt->vmod);
        }

        void build(const vll& a_v) {
            if(l == r) {
                v = vmod = a_v[l];
                return;
            }

            ll m = (l + r) >> 1;

            lt = new TreeX(l, m);
            rt = new TreeX(m + 1, r);
            lt->build(a_v);
            rt->build(a_v);
            combine(); // ! You forgot to combine!!
        }

        void upd(ll i, ll nv) {
            if(l == r && r == i) {
                v = vmod = nv;
                return;
            }
            ll m = (l + r) >> 1;
            if(i <= m) lt->upd(i, nv);
            else rt->upd(i, nv);
            combine(); // ! BRO YOU FORGOT TO COMBINE AGAIN
        }

        ll query_v(ll ql, ll qr) {
            if(ql > r || qr < l) return 1ll;
            if(ql == l && qr == r) return v;

            ll m = (l + r) >> 1;

            return TreeX::v_combine(lt->query_v(ql, min(m, qr)), rt->query_v(max(m + 1, ql), qr));
        }

        ll query_vmod(ll ql, ll qr) {
            if(ql > r || qr < l) return 1ll;
            if(ql == l && qr == r) return vmod;
            ll m = (l + r) >> 1;
            return TreeX::vmod_combine(lt->query_vmod(ql, min(m, qr)), rt->query_vmod(max(m + 1, ql), qr));
        }
};

TreeX* tx;

class TreeY {
    public:
        ll l, r, ind;
        TreeY* lt;
        TreeY* rt;

        TreeY(ll a_l, ll a_r): l(a_l), r(a_r), ind(0), lt(nullptr), rt(nullptr) {};

        void combine() {
            ll lv = y_arr[lt->ind];
            ll rv = TreeX::v_combine(tx->query_v(lt->ind + 1, rt->ind), y_arr[rt->ind]);

            if(lv > rv) ind = lt->ind;
            else ind = rt->ind;
        }

        void build() {
            if(l == r) {
                ind = l;
                return;
            }

            ll m = (l + r) >> 1;

            lt = new TreeY(l, m);
            rt = new TreeY(m + 1, r);
            lt->build();
            rt->build();

            combine();
        }

        void upd(ll i) {
            if(l == r && r == i) {
                ind = l;
                return;
            }

            ll m = (l + r) >> 1;
            if(i <= m) lt->upd(i);
            else rt->upd(i);
            combine();
        }
};

TreeY* ty;

ll cur_maxv() {
    // Rich people have hundreds of horses
    // I only have JUAN
    ll max_ind = ty->ind;
    return (tx->query_vmod(0, max_ind) * y_arr[max_ind]) % MOD;
}

ll ll_init(ll n, const vll& x, const vll& y) {
    y_arr = y; // Copy y array into new array

    tx = new TreeX(0, n - 1);
    tx->build(x);
    ty = new TreeY(0, n - 1);
    ty->build();

    return cur_maxv();
}

ll ll_updateX(ll pos, ll nv) {
    tx->upd(pos, nv);
    ty->upd(pos);
    return cur_maxv();
}

ll ll_updateY(ll pos, ll nv) {
    y_arr[pos] = nv;
    ty->upd(pos);
    return cur_maxv();
}

// IOI functions
int init(int N, int X[], int Y[]) {
    ll nll = N;
    vll x(N, 0);
    vll y(N, 0);

    for(ll i = 0; i < nll; i++) {
        x[i] = X[i];
        y[i] = Y[i];
    }

    return ll_init(nll, x, y);
}

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

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

Compilation message

horses.cpp: In function 'int init(int, int*, int*)':
horses.cpp:189:19: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  189 |     return ll_init(nll, x, y);
      |            ~~~~~~~^~~~~~~~~~~
horses.cpp: In function 'int updateX(int, int)':
horses.cpp:193:22: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  193 |     return ll_updateX(pos, val);
      |            ~~~~~~~~~~^~~~~~~~~~
horses.cpp: In function 'int updateY(int, int)':
horses.cpp:197:22: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  197 |     return ll_updateY(pos, val);
      |            ~~~~~~~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 432 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 436 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 440 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 2 ms 604 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 1 ms 608 KB Output is correct
26 Correct 1 ms 604 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
28 Correct 1 ms 604 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
31 Correct 1 ms 608 KB Output is correct
32 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 265 ms 127636 KB Output is correct
2 Correct 436 ms 136532 KB Output is correct
3 Correct 563 ms 127828 KB Output is correct
4 Correct 522 ms 131596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 2 ms 600 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 1 ms 604 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
28 Correct 1 ms 604 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
31 Correct 1 ms 604 KB Output is correct
32 Correct 1 ms 600 KB Output is correct
33 Correct 202 ms 129348 KB Output is correct
34 Correct 208 ms 129360 KB Output is correct
35 Correct 163 ms 136472 KB Output is correct
36 Correct 163 ms 136272 KB Output is correct
37 Correct 167 ms 127596 KB Output is correct
38 Correct 145 ms 128628 KB Output is correct
39 Correct 127 ms 127572 KB Output is correct
40 Correct 142 ms 131408 KB Output is correct
41 Correct 136 ms 127568 KB Output is correct
42 Correct 139 ms 127572 KB Output is correct
43 Correct 134 ms 131944 KB Output is correct
44 Correct 140 ms 132004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 404 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 604 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 2 ms 604 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 1 ms 604 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
28 Correct 1 ms 604 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
31 Correct 1 ms 604 KB Output is correct
32 Correct 1 ms 604 KB Output is correct
33 Correct 262 ms 127768 KB Output is correct
34 Correct 409 ms 136528 KB Output is correct
35 Correct 545 ms 127752 KB Output is correct
36 Correct 514 ms 131564 KB Output is correct
37 Correct 195 ms 129364 KB Output is correct
38 Correct 191 ms 129360 KB Output is correct
39 Correct 174 ms 136436 KB Output is correct
40 Correct 166 ms 136276 KB Output is correct
41 Correct 175 ms 127756 KB Output is correct
42 Correct 148 ms 128592 KB Output is correct
43 Correct 130 ms 127572 KB Output is correct
44 Correct 142 ms 131412 KB Output is correct
45 Correct 156 ms 127572 KB Output is correct
46 Correct 134 ms 127572 KB Output is correct
47 Correct 139 ms 131872 KB Output is correct
48 Correct 150 ms 131940 KB Output is correct
49 Correct 905 ms 129552 KB Output is correct
50 Correct 808 ms 129444 KB Output is correct
51 Correct 343 ms 136620 KB Output is correct
52 Correct 246 ms 136656 KB Output is correct
53 Correct 566 ms 127828 KB Output is correct
54 Correct 289 ms 128612 KB Output is correct
55 Correct 225 ms 127684 KB Output is correct
56 Correct 227 ms 131664 KB Output is correct
57 Correct 271 ms 127828 KB Output is correct
58 Correct 249 ms 127824 KB Output is correct
59 Correct 137 ms 131860 KB Output is correct