Submission #499021

# Submission time Handle Problem Language Result Execution time Memory
499021 2021-12-27T04:58:40 Z blue Horses (IOI15_horses) C++17
100 / 100
1054 ms 174280 KB
#include "horses.h"
#include <iostream>
#include <string>
#include <set>
using namespace std;

long long mod = 1e9 + 7;

long long ll(int a)
{
    return (long long)(a);
}

int* currv;

struct segtree
{
    int l;
    int r;

    long long p_mod = 1;
    long long p_actual = 1;
    long long mx = 1;

    segtree* left = NULL;
    segtree* right = NULL;

    segtree()
    {
        ;
    }

    segtree(int L, int R)
    {
        l = L;
        r = R;
        if(l == r)
        {
            p_mod = p_actual = mx = currv[l];
        }
        else
        {
            int m = (l+r)/2;
            left = new segtree(l, m);
            right = new segtree(m+1, r);

            p_mod = (left->p_mod * right->p_mod) % mod;
            p_actual = left->p_actual * right->p_actual;
            mx = max(left->mx, right->mx);
        }
    }

    long long rangeprod_mod(int L, int R)
    {
        if(L > R) return 1;
        if(r < L || R < l) return 1;
        if(L <= l && r <= R) return p_mod;
        return (left->rangeprod_mod(L, R) * right->rangeprod_mod(L, R)) % mod;
    }

    long long rangeprod_actual(int L, int R)
    {
        if(L > R) return 1;
        if(r < L || R < l) return 1;
        if(L <= l && r <= R) return p_actual;
        return left->rangeprod_actual(L, R) * right->rangeprod_actual(L, R);
    }

    long long rangemax(int L, int R)
    {
        if(r < L || R < l) return 0;
        if(L <= l && r <= R) return mx;
        return max(left->rangemax(L, R), right->rangemax(L, R));
    }

    void update(int I, long long V)
    {
        if(I < l || r < I) return;
        if(l == r)
        {
            p_mod = p_actual = mx = V;
        }
        else
        {
            left->update(I, V);
            right->update(I, V);
            p_mod = (left->p_mod * right->p_mod) % mod;
            p_actual = left->p_actual * right->p_actual;
            mx = max(left->mx, right->mx);
        }
    }
};

struct growth
{
    int i;
    long long x;
};

bool operator < (growth A, growth B)
{
    return A.i > B.i;
}

int N;
segtree X;
segtree Y;
set<growth> Xset;

int compute()
{
   // if(Xset.size() == 0) return Y.rangemax(0, N-1);

    long long maxprod = 1, maxprod_pos = 0;
    for(growth g: Xset)
    {
        maxprod *= g.x;
        if(maxprod > ll(1e9))
        {
            maxprod_pos = g.i + 1;
            break;
        }
    } //maxprod_pos is the last position with a suffix product <= 1e9

    long long res = 0, res_pos = -1;

    // for(int i = 0; i < N; i++) cout << X.rangeprod_mod(i, i) << ' ';
    // cout << '\n';
    // for(int i = 0; i < N; i++) cout << X.rangeprod_actual(i, i) << ' ';
    // cout << '\n';
    // for(int i = 0; i < N; i++) cout << Y.rangemax(i, i) << ' ';
    // cout << '\n';

    for(growth g: Xset)
    {
        if(g.i < maxprod_pos - 1) break;
        if(X.rangeprod_actual(maxprod_pos, g.i) * Y.rangemax(g.i, N-1) >= res)
        {
            res = X.rangeprod_actual(maxprod_pos, g.i) * Y.rangemax(g.i, N-1);
            res_pos = g.i;
        }
        // if(g.i > 0 && X.rangeprod_actual(maxprod_pos, g.i-1) * Y.rangemax(g.i-1, N-1) > res)
        // {
        //     res = X.rangeprod_actual(maxprod_pos, g.i-1) * Y.rangemax(g.i-1, N-1);
        //     res_pos = g.i-1;
        // }
    }

    return int( (X.rangeprod_mod(0, res_pos) * Y.rangemax(res_pos, N-1)) % mod );
}

int init(int n, int x[], int y[])
{
    N = n;

    currv = x;
    X = segtree(0, N-1);

    currv = y;
    Y = segtree(0, N-1);

    Xset.insert(growth{-1, 1});

    for(int i = 0; i < N; i++)
    {
        if(x[i] != 1) Xset.insert(growth{i, ll(x[i])});
    }

    return compute();
}

int updateX(int pos, int val)
{
    int curr_val = X.rangemax(pos, pos);
    if(curr_val != 1) Xset.erase(growth{pos, curr_val});

    X.update(pos, val);
    if(val != 1) Xset.insert(growth{pos, val});
    return compute();
}

int updateY(int pos, int val)
{
    Y.update(pos, val);
    return compute();
}

Compilation message

horses.cpp: In function 'int compute()':
horses.cpp:137:31: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
  137 |         if(X.rangeprod_actual(maxprod_pos, g.i) * Y.rangemax(g.i, N-1) >= res)
      |                               ^~~~~~~~~~~
horses.cpp:139:38: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
  139 |             res = X.rangeprod_actual(maxprod_pos, g.i) * Y.rangemax(g.i, N-1);
      |                                      ^~~~~~~~~~~
horses.cpp:149:37: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
  149 |     return int( (X.rangeprod_mod(0, res_pos) * Y.rangemax(res_pos, N-1)) % mod );
      |                                     ^~~~~~~
horses.cpp:149:59: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
  149 |     return int( (X.rangeprod_mod(0, res_pos) * Y.rangemax(res_pos, N-1)) % mod );
      |                                                           ^~~~~~~
horses.cpp: In function 'int updateX(int, int)':
horses.cpp:174:30: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
  174 |     int curr_val = X.rangemax(pos, pos);
      |                    ~~~~~~~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 1 ms 304 KB Output is correct
3 Correct 1 ms 296 KB Output is correct
4 Correct 1 ms 304 KB Output is correct
5 Correct 0 ms 208 KB Output is correct
6 Correct 0 ms 208 KB Output is correct
7 Correct 0 ms 304 KB Output is correct
8 Correct 0 ms 208 KB Output is correct
9 Correct 1 ms 208 KB Output is correct
10 Correct 1 ms 208 KB Output is correct
11 Correct 1 ms 208 KB Output is correct
12 Correct 0 ms 208 KB Output is correct
13 Correct 0 ms 208 KB Output is correct
14 Correct 0 ms 208 KB Output is correct
15 Correct 0 ms 296 KB Output is correct
16 Correct 1 ms 304 KB Output is correct
17 Correct 0 ms 208 KB Output is correct
18 Correct 1 ms 208 KB Output is correct
19 Correct 0 ms 296 KB Output is correct
20 Correct 0 ms 208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 0 ms 208 KB Output is correct
3 Correct 0 ms 208 KB Output is correct
4 Correct 0 ms 300 KB Output is correct
5 Correct 0 ms 208 KB Output is correct
6 Correct 1 ms 208 KB Output is correct
7 Correct 1 ms 280 KB Output is correct
8 Correct 1 ms 300 KB Output is correct
9 Correct 1 ms 208 KB Output is correct
10 Correct 1 ms 296 KB Output is correct
11 Correct 1 ms 208 KB Output is correct
12 Correct 0 ms 208 KB Output is correct
13 Correct 0 ms 208 KB Output is correct
14 Correct 0 ms 208 KB Output is correct
15 Correct 0 ms 208 KB Output is correct
16 Correct 1 ms 208 KB Output is correct
17 Correct 1 ms 208 KB Output is correct
18 Correct 1 ms 296 KB Output is correct
19 Correct 0 ms 300 KB Output is correct
20 Correct 2 ms 208 KB Output is correct
21 Correct 1 ms 296 KB Output is correct
22 Correct 0 ms 208 KB Output is correct
23 Correct 1 ms 564 KB Output is correct
24 Correct 1 ms 592 KB Output is correct
25 Correct 2 ms 592 KB Output is correct
26 Correct 1 ms 592 KB Output is correct
27 Correct 5 ms 592 KB Output is correct
28 Correct 2 ms 568 KB Output is correct
29 Correct 1 ms 464 KB Output is correct
30 Correct 2 ms 572 KB Output is correct
31 Correct 3 ms 464 KB Output is correct
32 Correct 5 ms 584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 872 ms 165512 KB Output is correct
2 Correct 508 ms 174280 KB Output is correct
3 Correct 531 ms 165480 KB Output is correct
4 Correct 620 ms 169444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 296 KB Output is correct
2 Correct 1 ms 296 KB Output is correct
3 Correct 1 ms 208 KB Output is correct
4 Correct 0 ms 208 KB Output is correct
5 Correct 0 ms 208 KB Output is correct
6 Correct 0 ms 208 KB Output is correct
7 Correct 0 ms 208 KB Output is correct
8 Correct 1 ms 304 KB Output is correct
9 Correct 0 ms 208 KB Output is correct
10 Correct 0 ms 208 KB Output is correct
11 Correct 1 ms 208 KB Output is correct
12 Correct 0 ms 300 KB Output is correct
13 Correct 0 ms 208 KB Output is correct
14 Correct 0 ms 208 KB Output is correct
15 Correct 0 ms 300 KB Output is correct
16 Correct 0 ms 208 KB Output is correct
17 Correct 1 ms 208 KB Output is correct
18 Correct 1 ms 256 KB Output is correct
19 Correct 0 ms 208 KB Output is correct
20 Correct 1 ms 208 KB Output is correct
21 Correct 1 ms 208 KB Output is correct
22 Correct 1 ms 208 KB Output is correct
23 Correct 2 ms 592 KB Output is correct
24 Correct 1 ms 592 KB Output is correct
25 Correct 2 ms 592 KB Output is correct
26 Correct 1 ms 592 KB Output is correct
27 Correct 8 ms 592 KB Output is correct
28 Correct 2 ms 592 KB Output is correct
29 Correct 1 ms 560 KB Output is correct
30 Correct 2 ms 592 KB Output is correct
31 Correct 3 ms 568 KB Output is correct
32 Correct 4 ms 464 KB Output is correct
33 Correct 155 ms 133632 KB Output is correct
34 Correct 173 ms 133628 KB Output is correct
35 Correct 302 ms 171728 KB Output is correct
36 Correct 286 ms 171576 KB Output is correct
37 Correct 189 ms 131748 KB Output is correct
38 Correct 190 ms 148488 KB Output is correct
39 Correct 121 ms 131444 KB Output is correct
40 Correct 261 ms 166780 KB Output is correct
41 Correct 173 ms 131584 KB Output is correct
42 Correct 160 ms 131580 KB Output is correct
43 Correct 302 ms 167112 KB Output is correct
44 Correct 257 ms 167036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 296 KB Output is correct
2 Correct 0 ms 296 KB Output is correct
3 Correct 1 ms 208 KB Output is correct
4 Correct 1 ms 296 KB Output is correct
5 Correct 0 ms 208 KB Output is correct
6 Correct 0 ms 208 KB Output is correct
7 Correct 0 ms 208 KB Output is correct
8 Correct 1 ms 208 KB Output is correct
9 Correct 0 ms 208 KB Output is correct
10 Correct 0 ms 208 KB Output is correct
11 Correct 1 ms 208 KB Output is correct
12 Correct 0 ms 208 KB Output is correct
13 Correct 1 ms 208 KB Output is correct
14 Correct 0 ms 208 KB Output is correct
15 Correct 1 ms 208 KB Output is correct
16 Correct 1 ms 300 KB Output is correct
17 Correct 0 ms 208 KB Output is correct
18 Correct 0 ms 208 KB Output is correct
19 Correct 0 ms 304 KB Output is correct
20 Correct 0 ms 208 KB Output is correct
21 Correct 1 ms 208 KB Output is correct
22 Correct 1 ms 296 KB Output is correct
23 Correct 1 ms 564 KB Output is correct
24 Correct 2 ms 592 KB Output is correct
25 Correct 2 ms 592 KB Output is correct
26 Correct 1 ms 592 KB Output is correct
27 Correct 7 ms 592 KB Output is correct
28 Correct 2 ms 592 KB Output is correct
29 Correct 2 ms 464 KB Output is correct
30 Correct 2 ms 592 KB Output is correct
31 Correct 3 ms 464 KB Output is correct
32 Correct 4 ms 464 KB Output is correct
33 Correct 886 ms 165572 KB Output is correct
34 Correct 548 ms 174228 KB Output is correct
35 Correct 566 ms 165508 KB Output is correct
36 Correct 723 ms 169304 KB Output is correct
37 Correct 128 ms 133576 KB Output is correct
38 Correct 141 ms 133596 KB Output is correct
39 Correct 353 ms 171720 KB Output is correct
40 Correct 334 ms 171612 KB Output is correct
41 Correct 194 ms 131816 KB Output is correct
42 Correct 231 ms 148592 KB Output is correct
43 Correct 111 ms 131448 KB Output is correct
44 Correct 308 ms 166784 KB Output is correct
45 Correct 133 ms 131576 KB Output is correct
46 Correct 157 ms 131644 KB Output is correct
47 Correct 278 ms 167028 KB Output is correct
48 Correct 288 ms 167068 KB Output is correct
49 Correct 353 ms 136932 KB Output is correct
50 Correct 291 ms 136932 KB Output is correct
51 Correct 556 ms 173572 KB Output is correct
52 Correct 389 ms 173000 KB Output is correct
53 Correct 1054 ms 135172 KB Output is correct
54 Correct 492 ms 153156 KB Output is correct
55 Correct 235 ms 132556 KB Output is correct
56 Correct 431 ms 168532 KB Output is correct
57 Correct 499 ms 133164 KB Output is correct
58 Correct 727 ms 133652 KB Output is correct
59 Correct 319 ms 167048 KB Output is correct