Submission #1091994

# Submission time Handle Problem Language Result Execution time Memory
1091994 2024-09-22T19:27:23 Z triplem5ds Horses (IOI15_horses) C++14
0 / 100
579 ms 48340 KB
#include "horses.h"
#include "bits/stdc++.h"
using namespace std;


const int MOD = 1e9 + 7;
const int N = 5e5 + 5;

struct segTreeNode
{
    int mulX;
    int maxY;
    segTreeNode(int _mulX = 0, int _maxY = 0) : mulX(_mulX), maxY(_maxY) {}
    friend segTreeNode operator + (segTreeNode a, segTreeNode b) {
        return segTreeNode(
            a.mulX * b.mulX % MOD,
            max(a.maxY, b.maxY)
        );
    }
};

int x[N], y[N], n;
segTreeNode tree[N << 2];
set<int> hotPoints;

void build(int node, int s, int e) {
    if (s == e) {
        tree[node] = segTreeNode(x[s], y[s]);
        return;
    }
    int md = (s + e) >> 1;
    build(node << 1, s, md);
    build(node << 1 | 1, md + 1, e);
    tree[node] = tree[node << 1] + tree[node << 1 | 1];
}

segTreeNode query(int node, int s, int e, int l, int r) {
    if (r < s || e < l)  return segTreeNode(1, 0);
    if (l <= s && e <= r)
        return tree[node];
    int md = (s + e) >> 1;
    return query(node << 1, s, md, l, r) + query(node << 1 | 1, md + 1, e, l, r);
}

void update(int node, int s, int e, int index) {
    if (s == e) {
        tree[node] = segTreeNode(x[s], y[s]);
        return;
    }
    int md = (s + e) >> 1;
    if (index <= md)
        update(node << 1, s, md, index);
    else
        update(node << 1 | 1, md + 1, e, index);
    tree[node] = tree[node << 1] + tree[node << 1 | 1];
}

int calcAnswer() {

    vector<array<int, 3>> vec; ///(x, y)
    int prv = n;

    ///Extract last 30 elements
    for (auto it = hotPoints.rbegin(); it != hotPoints.rend() && vec.size() < 30; it++) {
        vec.push_back({
            x[*it],
            query(1, 0, n - 1, *it, prv - 1).maxY,
            *it
            });
        prv = *it;
    }

    reverse(begin(vec), end(vec));  ///reorder to go from first to last

    ///I want to get the best Y
    int choosen = 0;
    long long cur = 1;
    for (int i = 1; i < vec.size(); i++) {
        /*
            I want to get the first j > i such that
            x[i + 1] * ... * x[j] * y[j] > y[i]
        */
        cur = cur * vec[i][0];
        if (cur > vec[choosen][1] || cur * vec[i][1] > vec[choosen][1]) { ///to avoid overflow
            choosen = i;
            cur = 1;
        }
    }

    int nxt = n;
    if (choosen + 1 != vec.size()) nxt = vec[choosen + 1][2];
    auto val = query(1, 0, n - 1, 0, nxt - 1);
    return val.maxY * val.mulX % MOD;
}

int init(int N, int X[], int Y[]) {
    copy(X, X + N, x);
    copy(Y, Y + N, y);
    n = N;
    build(1, 0, N - 1);
    for (int i = 0; i < N; i++) {
        if (X[i] > 1) hotPoints.insert(i);
    }
    return calcAnswer();
}

int updateX(int pos, int val) {
    x[pos] = val;
    update(1, 0, n - 1, pos);
    return calcAnswer();
}

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


// int main() {
//     int n = 3;
//     int x[] = { 2,1,3 };
//     int y[] = { 3,4,1 };
//     cout << init(n, x, y) << "\n";
//     cout << updateY(1, 2) << "\n";

// }

Compilation message

horses.cpp: In function 'int calcAnswer()':
horses.cpp:78:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   78 |     for (int i = 1; i < vec.size(); i++) {
      |                     ~~^~~~~~~~~~~~
horses.cpp:91:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   91 |     if (choosen + 1 != vec.size()) nxt = vec[choosen + 1][2];
      |         ~~~~~~~~~~~~^~~~~~~~~~~~~
horses.cpp: In function 'int init(int, int*, int*)':
horses.cpp:96:14: warning: declaration of 'N' shadows a global declaration [-Wshadow]
   96 | int init(int N, int X[], int Y[]) {
      |          ~~~~^
horses.cpp:7:11: note: shadowed declaration is here
    7 | const int N = 5e5 + 5;
      |           ^
# Verdict Execution time Memory Grader output
1 Correct 10 ms 15960 KB Output is correct
2 Correct 6 ms 15964 KB Output is correct
3 Correct 6 ms 15960 KB Output is correct
4 Correct 6 ms 15880 KB Output is correct
5 Runtime error 16 ms 32156 KB Execution killed with signal 11
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 15964 KB Output is correct
2 Correct 6 ms 15964 KB Output is correct
3 Correct 6 ms 15920 KB Output is correct
4 Correct 6 ms 15964 KB Output is correct
5 Runtime error 17 ms 32148 KB Execution killed with signal 11
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 579 ms 48340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 9 ms 15964 KB Output is correct
2 Correct 7 ms 16092 KB Output is correct
3 Correct 6 ms 15964 KB Output is correct
4 Correct 6 ms 15964 KB Output is correct
5 Runtime error 17 ms 32092 KB Execution killed with signal 11
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 15960 KB Output is correct
2 Correct 9 ms 15964 KB Output is correct
3 Correct 5 ms 15964 KB Output is correct
4 Correct 7 ms 15964 KB Output is correct
5 Runtime error 16 ms 32092 KB Execution killed with signal 11
6 Halted 0 ms 0 KB -