Submission #778268

# Submission time Handle Problem Language Result Execution time Memory
778268 2023-07-10T07:59:09 Z danikoynov Horses (IOI15_horses) C++14
100 / 100
493 ms 59176 KB
#include "horses.h"
#include<bits/stdc++.h>

using namespace std;
typedef long long ll;

const int maxn = 5e5 + 10;
const ll mod = 1e9 + 7;
ll x[maxn], y[maxn], n;

ll prod_tree[4 * maxn];

void build_prod_tree(int root, int left, int right)
{
    if (left == right)
    {
        prod_tree[root] = x[left];
        return;
    }

    int mid = (left + right) / 2;
    build_prod_tree(root * 2, left, mid);
    build_prod_tree(root * 2 + 1, mid + 1, right);

    prod_tree[root] = (prod_tree[root * 2] * prod_tree[root * 2 + 1]) % mod;
}

void update_prod(int root, int left, int right, int pos)
{
    if (left == right)
    {
        prod_tree[root] = x[left];
        return;
    }

    int mid = (left + right) / 2;
    if (pos <= mid)
        update_prod(root * 2, left, mid, pos);
    else
        update_prod(root * 2 + 1, mid + 1, right, pos);

    prod_tree[root] = (prod_tree[root * 2] * prod_tree[root * 2 + 1]) % mod;
}

ll prod_query(int root, int left, int right, int qleft, int qright)
{
    if (left > qright || right < qleft)
        return 1;
    if (left >= qleft && right <= qright)
        return prod_tree[root];

    int mid = (left + right) / 2;
    return (prod_query(root * 2, left, mid, qleft, qright) *
            prod_query(root * 2 + 1, mid + 1, right, qleft, qright)) % mod;
}

ll breed_tree[4 * maxn];

void build_breed_tree(int root, int left, int right)
{
    if (left == right)
    {
        breed_tree[root] = y[left];
        return;
    }

    int mid = (left + right) / 2;
    build_breed_tree(root * 2, left, mid);
    build_breed_tree(root * 2 + 1, mid + 1, right);

    breed_tree[root] = max(breed_tree[root * 2], breed_tree[root * 2 + 1]);
}

void update_breed(int root, int left, int right, int pos)
{
    if (left == right)
    {
        breed_tree[root] = y[left];
        return;
    }

    int mid = (left + right) / 2;
    if (pos <= mid)
        update_breed(root * 2, left, mid, pos);
    else
        update_breed(root * 2 + 1, mid + 1, right, pos);

    breed_tree[root] = max(breed_tree[root * 2], breed_tree[root * 2 + 1]);
}

ll breed_query(int root, int left, int right, int qleft, int qright)
{
    if (left > qright || right < qleft)
        return 0;

    if (left >= qleft && right <= qright)
        return breed_tree[root];

    int mid = (left + right) / 2;
    return max(breed_query(root * 2, left, mid, qleft, qright),
               breed_query(root * 2 + 1, mid + 1, right, qleft, qright));
}

set < int > active;

ll action()
{
    ll up = y[n - 1], dw = 1, pos = n - 1;
    ll div = 1;
    set < int > :: reverse_iterator it;
    if (active.empty() || *active.begin() != 0)
    {
        active.insert(0);
    }
    for (it = active.rbegin(); it != active.rend(); it ++)
    {
        ll cur = breed_query(1, 0, n - 1, *it, n - 1);
        if (div > 1e9)
            break;
            ///cout << *it << " " << cur << " " << div << endl;
        if (cur * dw >= up * div)
        {
            up = cur;
            dw = div;
            pos = *it;
        }
        div = div * x[*it];
    }

    /**for (int i = n - 2; i >= 0; i --)
    {
        div = div * x[i + 1];
        if (div > 1e9)
            break;
        ///cout << i << " : " << y[i] << " " << div << endl;
        /// y[i] / div >? up / dw
        if (y[i] * dw >= up * div)
        {

            ///cout << " " << up << " : " << dw << endl;
            up = y[i];
            dw = div;
            pos = i;
        }
    }*/

    ///cout << pos << endl;
    ll ans = prod_query(1, 0, n - 1, 0, pos);
    //for (int i = 0; i <= pos; i ++)
    //  ans = (ans * x[i]) % mod;
    ans = (ans * breed_query(1, 0, n - 1, pos, n - 1)) % mod;
    return ans;
}
int init(int N, int X[], int Y[])
{
    n = N;
    for (int i = 0; i < n; i ++)
    {
        if (X[i] > 1)
            active.insert(i);
        x[i] = X[i];
        y[i] = Y[i];
    }
    build_prod_tree(1, 0, n - 1);
    build_breed_tree(1, 0, n - 1);
    return action();
}

int updateX(int pos, int val)
{
    if (x[pos] > 1 && pos != 0)
        active.erase(pos);
    x[pos] = val;
    if (x[pos] > 1 && pos != 0)
        active.insert(pos);
    update_prod(1, 0, n - 1, pos);
    return action();
}

int updateY(int pos, int val)
{
    y[pos] = val;
    update_breed(1, 0, n - 1, pos);
    return action();
}

Compilation message

horses.cpp: In function 'll action()':
horses.cpp:117:38: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  117 |         ll cur = breed_query(1, 0, n - 1, *it, n - 1);
      |                                    ~~^~~
horses.cpp:117:50: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  117 |         ll cur = breed_query(1, 0, n - 1, *it, n - 1);
      |                                                ~~^~~
horses.cpp:118:13: warning: conversion from 'll' {aka 'long long int'} to 'double' may change value [-Wconversion]
  118 |         if (div > 1e9)
      |             ^~~
horses.cpp:148:33: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  148 |     ll ans = prod_query(1, 0, n - 1, 0, pos);
      |                               ~~^~~
horses.cpp:148:41: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  148 |     ll ans = prod_query(1, 0, n - 1, 0, pos);
      |                                         ^~~
horses.cpp:151:38: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  151 |     ans = (ans * breed_query(1, 0, n - 1, pos, n - 1)) % mod;
      |                                    ~~^~~
horses.cpp:151:43: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  151 |     ans = (ans * breed_query(1, 0, n - 1, pos, n - 1)) % mod;
      |                                           ^~~
horses.cpp:151:50: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  151 |     ans = (ans * breed_query(1, 0, n - 1, pos, n - 1)) % mod;
      |                                                ~~^~~
horses.cpp: In function 'int init(int, int*, int*)':
horses.cpp:164:29: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  164 |     build_prod_tree(1, 0, n - 1);
      |                           ~~^~~
horses.cpp:165:30: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  165 |     build_breed_tree(1, 0, n - 1);
      |                            ~~^~~
horses.cpp:166:18: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  166 |     return action();
      |            ~~~~~~^~
horses.cpp: In function 'int updateX(int, int)':
horses.cpp:176:25: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  176 |     update_prod(1, 0, n - 1, pos);
      |                       ~~^~~
horses.cpp:177:18: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  177 |     return action();
      |            ~~~~~~^~
horses.cpp: In function 'int updateY(int, int)':
horses.cpp:183:26: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  183 |     update_breed(1, 0, n - 1, pos);
      |                        ~~^~~
horses.cpp:184:18: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  184 |     return action();
      |            ~~~~~~^~
# 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 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 1 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
# 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 1 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 1 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 1 ms 212 KB Output is correct
17 Correct 1 ms 340 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 0 ms 212 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 2 ms 396 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 2 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 2 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 488 ms 52848 KB Output is correct
2 Correct 306 ms 52936 KB Output is correct
3 Correct 309 ms 52932 KB Output is correct
4 Correct 347 ms 52828 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 1 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 1 ms 212 KB Output is correct
9 Correct 1 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 1 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 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 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 340 KB Output is correct
31 Correct 2 ms 340 KB Output is correct
32 Correct 2 ms 388 KB Output is correct
33 Correct 45 ms 28620 KB Output is correct
34 Correct 46 ms 28588 KB Output is correct
35 Correct 170 ms 52048 KB Output is correct
36 Correct 155 ms 52016 KB Output is correct
37 Correct 66 ms 28608 KB Output is correct
38 Correct 94 ms 40476 KB Output is correct
39 Correct 38 ms 28492 KB Output is correct
40 Correct 141 ms 57900 KB Output is correct
41 Correct 53 ms 30492 KB Output is correct
42 Correct 60 ms 30544 KB Output is correct
43 Correct 138 ms 58152 KB Output is correct
44 Correct 129 ms 58216 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 340 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 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 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 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 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 1 ms 340 KB Output is correct
31 Correct 2 ms 340 KB Output is correct
32 Correct 2 ms 340 KB Output is correct
33 Correct 493 ms 52920 KB Output is correct
34 Correct 292 ms 53008 KB Output is correct
35 Correct 275 ms 52880 KB Output is correct
36 Correct 382 ms 52900 KB Output is correct
37 Correct 43 ms 28620 KB Output is correct
38 Correct 40 ms 28684 KB Output is correct
39 Correct 156 ms 52004 KB Output is correct
40 Correct 146 ms 52040 KB Output is correct
41 Correct 63 ms 28648 KB Output is correct
42 Correct 88 ms 40492 KB Output is correct
43 Correct 31 ms 28396 KB Output is correct
44 Correct 138 ms 57952 KB Output is correct
45 Correct 48 ms 30584 KB Output is correct
46 Correct 60 ms 30640 KB Output is correct
47 Correct 130 ms 58216 KB Output is correct
48 Correct 129 ms 58232 KB Output is correct
49 Correct 152 ms 35576 KB Output is correct
50 Correct 139 ms 35660 KB Output is correct
51 Correct 281 ms 59148 KB Output is correct
52 Correct 213 ms 59176 KB Output is correct
53 Correct 404 ms 33832 KB Output is correct
54 Correct 239 ms 47408 KB Output is correct
55 Correct 97 ms 31596 KB Output is correct
56 Correct 232 ms 58528 KB Output is correct
57 Correct 271 ms 32292 KB Output is correct
58 Correct 403 ms 32720 KB Output is correct
59 Correct 131 ms 57264 KB Output is correct