Submission #640804

# Submission time Handle Problem Language Result Execution time Memory
640804 2022-09-15T09:31:23 Z piOOE Horses (IOI15_horses) C++17
100 / 100
550 ms 48936 KB
#include <bits/stdc++.h>
#include "horses.h"

using namespace std;
using ll = long long;

constexpr int MOD = 1e9 + 7;

int add(int a, int b) {
    return a + b < MOD ? a + b : a + b - MOD;
}

int mul(int a, int b) {
    return (long long) a * b % MOD;
}

int sub(int a, int b) {
    return a >= b ? a - b : a - b + MOD;
}

int power(int a, int b) {
    int ans = 1;
    for (; b > 0; b >>= 1, a = mul(a, a)) if (b & 1) ans = mul(ans, a);
    return ans;
}

int inv(int a) {
    return power(a, MOD - 2);
}

vector<int> t;
int sz = 1;

void init(vector<int> a) {
    sz = 1;
    while (sz < a.size()) sz <<= 1;
    t.assign(sz << 1, 0);
    for (int i = 0; i < a.size(); ++i) {
        t[i + sz] = a[i];
    }
    for (int i = sz - 1; i > 0; --i) {
        t[i] = max(t[i << 1], t[i << 1 | 1]);
    }
}

void pull(int x) {
    t[x] = max(t[x << 1], t[x << 1 | 1]);
}

void modify(int i, int val, int x = 1, int lx = 0, int rx = sz) {
    if (lx + 1 == rx) {
        t[x] = val;
        return;
    }
    int mid = (lx + rx) >> 1;
    if (i < mid) modify(i, val, x << 1, lx, mid);
    else modify(i, val, x << 1 | 1, mid, rx);
    pull(x);
}

int query(int l, int r, int x = 1, int lx = 0, int rx = sz) {
    if (l >= rx ||  lx >= r) return 0;
    if (l <= lx && rx <= r) return t[x];
    int mid = (lx + rx) >> 1;
    return max(query(l, r, x << 1, lx, mid), query(l, r, x << 1 | 1, mid, rx));
}

int n;

vector<int> x, y;

int prodX = 1;

set<int> alive;

int answer() {
    int maxY = t[1];
    if (alive.empty()) {
        return maxY;
    }
    vector<int> pos;
    ll m = 1;
    auto it = alive.end();
    do {
        it = prev(it);
        m *= x[*it];
    } while (m <= maxY && it != alive.begin());
    if (m <= maxY) {
        vector<int> consider;
        for (int i : alive) consider.push_back(i);
        if (consider[0] != 0) {
            consider.insert(consider.begin(), 0);
        }
        ll prod = 1;
        ll ans = 1;
        consider.push_back(n);
        for (int i = 0; i < (int) consider.size() - 1; ++i) {
            int j = consider[i];
            prod *= x[j];
            ans = max(ans, prod * query(j, consider[i + 1]));
        }
        return ans % MOD;
    }
    ll ans = 1;
    auto itt = it;
    int newProd = mul(prodX, inv(m / x[*it]));
    for (m = 1; it != alive.end(); it = next(it)) {
        if (it != itt) {
            m *= x[*it];
        }
        ans = max(ans, m * query(*it, n));
    }
    ans %= MOD;
    return mul(newProd, ans);
}

int init(int N, int X[], int Y[]) {
    n = N;
    x.resize(n), y.resize(n);
    for (int i = 0; i < n; ++i) {
        x[i] = X[i], y[i] = Y[i];
        if (x[i] != 1) {
            prodX = mul(prodX, x[i]);
            alive.insert(alive.end(), i);
        }
    }
    init(y);
    return answer();
}

int updateX(int pos, int val) {
    prodX = mul(prodX, inv(x[pos]));
    x[pos] = val;
    prodX = mul(prodX, val);
    if (val == 1) {
        alive.erase(pos);
    } else {
        alive.insert(pos);
    }
    return answer();
}


int updateY(int pos, int val) {
    y[pos] = val;
    modify(pos, val);
    return answer();
}

Compilation message

horses.cpp: In function 'int mul(int, int)':
horses.cpp:14:30: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
   14 |     return (long long) a * b % MOD;
      |            ~~~~~~~~~~~~~~~~~~^~~~~
horses.cpp: In function 'void init(std::vector<int>)':
horses.cpp:36:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   36 |     while (sz < a.size()) sz <<= 1;
      |            ~~~^~~~~~~~~~
horses.cpp:38:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   38 |     for (int i = 0; i < a.size(); ++i) {
      |                     ~~^~~~~~~~~~
horses.cpp: In function 'int answer()':
horses.cpp:102:20: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  102 |         return ans % MOD;
      |                ~~~~^~~~~
horses.cpp:106:36: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  106 |     int newProd = mul(prodX, inv(m / x[*it]));
horses.cpp:114:25: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  114 |     return mul(newProd, ans);
      |                         ^~~
# 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 296 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 300 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 1 ms 296 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 300 KB Output is correct
17 Correct 0 ms 300 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 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 296 KB Output is correct
4 Correct 1 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 1 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 1 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 1 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 2 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 2 ms 340 KB Output is correct
26 Correct 1 ms 308 KB Output is correct
27 Correct 4 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 308 KB Output is correct
31 Correct 2 ms 312 KB Output is correct
32 Correct 4 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 473 ms 40464 KB Output is correct
2 Correct 249 ms 48740 KB Output is correct
3 Correct 238 ms 40484 KB Output is correct
4 Correct 293 ms 44264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 304 KB Output is correct
2 Correct 0 ms 340 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 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 1 ms 292 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 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 340 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 2 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 312 KB Output is correct
29 Correct 1 ms 212 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
33 Correct 38 ms 18036 KB Output is correct
34 Correct 42 ms 17996 KB Output is correct
35 Correct 110 ms 48288 KB Output is correct
36 Correct 104 ms 48412 KB Output is correct
37 Correct 81 ms 16252 KB Output is correct
38 Correct 54 ms 28904 KB Output is correct
39 Correct 25 ms 16124 KB Output is correct
40 Correct 86 ms 43520 KB Output is correct
41 Correct 45 ms 16204 KB Output is correct
42 Correct 59 ms 16224 KB Output is correct
43 Correct 77 ms 43972 KB Output is correct
44 Correct 79 ms 43976 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 212 KB Output is correct
6 Correct 1 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 1 ms 212 KB Output is correct
11 Correct 0 ms 296 KB Output is correct
12 Correct 0 ms 300 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 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 1 ms 296 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 300 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 316 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 212 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 340 KB Output is correct
33 Correct 506 ms 40528 KB Output is correct
34 Correct 230 ms 48936 KB Output is correct
35 Correct 226 ms 40620 KB Output is correct
36 Correct 301 ms 44328 KB Output is correct
37 Correct 40 ms 18060 KB Output is correct
38 Correct 37 ms 18076 KB Output is correct
39 Correct 110 ms 48412 KB Output is correct
40 Correct 104 ms 48308 KB Output is correct
41 Correct 74 ms 16208 KB Output is correct
42 Correct 62 ms 28892 KB Output is correct
43 Correct 26 ms 16132 KB Output is correct
44 Correct 82 ms 43516 KB Output is correct
45 Correct 44 ms 16204 KB Output is correct
46 Correct 60 ms 16336 KB Output is correct
47 Correct 77 ms 44016 KB Output is correct
48 Correct 75 ms 43980 KB Output is correct
49 Correct 132 ms 19392 KB Output is correct
50 Correct 114 ms 19368 KB Output is correct
51 Correct 243 ms 48552 KB Output is correct
52 Correct 185 ms 48512 KB Output is correct
53 Correct 550 ms 17568 KB Output is correct
54 Correct 168 ms 31144 KB Output is correct
55 Correct 82 ms 16324 KB Output is correct
56 Correct 156 ms 43576 KB Output is correct
57 Correct 281 ms 16252 KB Output is correct
58 Correct 442 ms 16424 KB Output is correct
59 Correct 75 ms 43936 KB Output is correct