Submission #733267

# Submission time Handle Problem Language Result Execution time Memory
733267 2023-04-30T10:22:28 Z That_Salamander Horses (IOI15_horses) C++17
100 / 100
1194 ms 68696 KB
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#include <cstring>
#include <queue>
#include <unordered_set>
#include <unordered_map>

#define FOR(var,bound) for(int var = 0; var < bound; var++)
#define FORB(var,lb,ub) for (int var = lb; var < ub; var++)
#define FORR(var,bound) for(int var = bound-1; var >= 0; var--)
#define MAX 1000000007ll

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef pair<int, int> pii;

int n;
vector<int> x, y;

vector<ll> t(2000000, 1);
vector<int> tMax(2000000, 0);
set<int> nonOneX;

void update(int n, int tl, int tr, int idx, ll val) {
    if (tl == tr) {
        t[n] = val;

        if (val == 1) {
            nonOneX.erase(idx);
        } else {
            nonOneX.insert(idx);
        }

        return;
    }

    int m = (tl + tr) / 2;
    if (idx <= m) update(n * 2, tl, m, idx, val);
    else update(n*2+1, m + 1, tr, idx, val);

    t[n]  = (t[n*2] * t[n*2+1]) % MAX;
}

ll query(int n, int tl, int tr, int ql, int qr) {
    if (qr < tl || tr < ql) {
        return 1;
    } else if (ql <= tl && tr <= qr) {
        return t[n];
    } else {
        int m = (tl + tr) / 2;
        return (
            query(n*2  , tl, m, ql, qr) *
            query(n*2+1, m+1, tr, ql, qr)
        ) % MAX;
    }
}

void updateY(int n, int tl, int tr, int idx, int val) {
    if (tl == tr) {
        tMax[n] = val;

        return;
    }

    int m = (tl + tr) / 2;
    if (idx <= m) updateY(n * 2, tl, m, idx, val);
    else updateY(n*2+1, m + 1, tr, idx, val);

    tMax[n]  = max(tMax[n*2], tMax[n*2+1]);
}

int queryY(int n, int tl, int tr, int ql, int qr) {
    if (qr < tl || tr < ql) {
        return 1;
    } else if (ql <= tl && tr <= qr) {
        return tMax[n];
    } else {
        int m = (tl + tr) / 2;
        return max(
            queryY(n*2  , tl, m, ql, qr),
            queryY(n*2+1, m+1, tr, ql, qr)
        );
    }
}

int solve() {
    ll best = 0;

    ll horses = 1;
    ll mul = 1;
    ll mulToBeat = 0;

    if (nonOneX.size() == 0) {
        return queryY(1, 0, n-1, 0, n-1);
    }


    auto it = nonOneX.begin();

    if (*it != 0) {
        best = mulToBeat = queryY(1, 0, n - 1, 0, *it - 1);
    }

    if (nonOneX.size() > 35) {
        it = nonOneX.end();

        for (int i = 0; i < 33; i++) {
            it--;
        }

        horses = query(1, 0, n - 1, 0, (*it) - 1);
    }

    while (it != nonOneX.end()) {
        int xVal = x[*it];
        int yVal;
        auto nit = it; nit++;
        if (nit == nonOneX.end()) {
            yVal = queryY(1, 0, n - 1, *it, n - 1);
        } else {
            yVal = queryY(1, 0, n - 1, *it, *nit - 1);
        }

        horses = (horses * xVal) % MAX;
        ll price = (horses * yVal) % MAX;

        mul *= xVal;

        if (mul >= mulToBeat || mul * yVal >= mulToBeat) {
            mulToBeat = yVal;
            best = price;
            mul = 1;
        }

        it++;
    }

    return best % MAX;
}

int init(int N, int X[], int Y[]) {
    n = N;
    x.resize(N); y.resize(N);

    FOR(i, N) {
        x[i] = X[i];
        y[i] = Y[i];
        update(1, 0, n - 1, i, X[i]);
        updateY(1, 0, n - 1, i, Y[i]);
    }

    return solve();
}

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

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


#ifdef LOCAL_TEST
static char buffer[1024];
static int currentChar = 0;
static int charsNumber = 0;

static inline int readInt() {
  int x;
  cin >> x;
  return x;
}

int main() {
  int N;
  N = readInt();

  int *X = (int *)malloc(sizeof(int) * (unsigned int)N);
  int *Y = (int *)malloc(sizeof(int) * (unsigned int)N);

  for (int i = 0; i < N; i++) {
    X[i] = readInt();
  }

  for (int i = 0; i < N; i++) {
    Y[i] = readInt();
  }

  printf("%d\n", init(N, X, Y));

  int M;
  M = readInt();

  for (int i = 0; i < M; i++) {
    int type;
    type = readInt();
    int pos;
    pos = readInt();
    int val;
    val = readInt();

    if (type == 1) {
      printf("%d\n", updateX(pos, val));
    } else if (type == 2) {
      printf("%d\n", updateY(pos, val));
    }
  }

  return 0;
}

#endif

Compilation message

horses.cpp: In function 'void update(int, int, int, int, ll)':
horses.cpp:32:17: warning: declaration of 'n' shadows a global declaration [-Wshadow]
   32 | void update(int n, int tl, int tr, int idx, ll val) {
      |             ~~~~^
horses.cpp:25:5: note: shadowed declaration is here
   25 | int n;
      |     ^
horses.cpp: In function 'll query(int, int, int, int, int)':
horses.cpp:52:14: warning: declaration of 'n' shadows a global declaration [-Wshadow]
   52 | ll query(int n, int tl, int tr, int ql, int qr) {
      |          ~~~~^
horses.cpp:25:5: note: shadowed declaration is here
   25 | int n;
      |     ^
horses.cpp: In function 'void updateY(int, int, int, int, int)':
horses.cpp:66:18: warning: declaration of 'n' shadows a global declaration [-Wshadow]
   66 | void updateY(int n, int tl, int tr, int idx, int val) {
      |              ~~~~^
horses.cpp:25:5: note: shadowed declaration is here
   25 | int n;
      |     ^
horses.cpp: In function 'int queryY(int, int, int, int, int)':
horses.cpp:80:16: warning: declaration of 'n' shadows a global declaration [-Wshadow]
   80 | int queryY(int n, int tl, int tr, int ql, int qr) {
      |            ~~~~^
horses.cpp:25:5: note: shadowed declaration is here
   25 | int n;
      |     ^
horses.cpp: In function 'int solve()':
horses.cpp:146:17: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  146 |     return best % MAX;
      |                 ^
# Verdict Execution time Memory Grader output
1 Correct 11 ms 23764 KB Output is correct
2 Correct 13 ms 23764 KB Output is correct
3 Correct 14 ms 23764 KB Output is correct
4 Correct 16 ms 23764 KB Output is correct
5 Correct 13 ms 23804 KB Output is correct
6 Correct 14 ms 23764 KB Output is correct
7 Correct 12 ms 23764 KB Output is correct
8 Correct 15 ms 23704 KB Output is correct
9 Correct 11 ms 23804 KB Output is correct
10 Correct 12 ms 23688 KB Output is correct
11 Correct 11 ms 23676 KB Output is correct
12 Correct 13 ms 23764 KB Output is correct
13 Correct 15 ms 23804 KB Output is correct
14 Correct 11 ms 23700 KB Output is correct
15 Correct 12 ms 23692 KB Output is correct
16 Correct 11 ms 23720 KB Output is correct
17 Correct 12 ms 23764 KB Output is correct
18 Correct 12 ms 23716 KB Output is correct
19 Correct 12 ms 23764 KB Output is correct
20 Correct 11 ms 23756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23680 KB Output is correct
2 Correct 11 ms 23764 KB Output is correct
3 Correct 10 ms 23800 KB Output is correct
4 Correct 17 ms 23704 KB Output is correct
5 Correct 11 ms 23804 KB Output is correct
6 Correct 13 ms 23752 KB Output is correct
7 Correct 17 ms 23776 KB Output is correct
8 Correct 16 ms 23788 KB Output is correct
9 Correct 10 ms 23800 KB Output is correct
10 Correct 11 ms 23764 KB Output is correct
11 Correct 11 ms 23764 KB Output is correct
12 Correct 11 ms 23764 KB Output is correct
13 Correct 14 ms 23688 KB Output is correct
14 Correct 11 ms 23764 KB Output is correct
15 Correct 12 ms 23804 KB Output is correct
16 Correct 12 ms 23784 KB Output is correct
17 Correct 17 ms 23764 KB Output is correct
18 Correct 11 ms 23764 KB Output is correct
19 Correct 10 ms 23764 KB Output is correct
20 Correct 11 ms 23808 KB Output is correct
21 Correct 11 ms 23800 KB Output is correct
22 Correct 11 ms 23696 KB Output is correct
23 Correct 15 ms 23764 KB Output is correct
24 Correct 24 ms 23856 KB Output is correct
25 Correct 16 ms 23816 KB Output is correct
26 Correct 17 ms 23896 KB Output is correct
27 Correct 13 ms 23764 KB Output is correct
28 Correct 14 ms 23892 KB Output is correct
29 Correct 12 ms 23764 KB Output is correct
30 Correct 19 ms 23884 KB Output is correct
31 Correct 21 ms 23812 KB Output is correct
32 Correct 22 ms 23764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 903 ms 59880 KB Output is correct
2 Correct 1194 ms 68576 KB Output is correct
3 Correct 1028 ms 59940 KB Output is correct
4 Correct 1069 ms 63784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23808 KB Output is correct
2 Correct 11 ms 23784 KB Output is correct
3 Correct 11 ms 23800 KB Output is correct
4 Correct 12 ms 23736 KB Output is correct
5 Correct 14 ms 23792 KB Output is correct
6 Correct 19 ms 23776 KB Output is correct
7 Correct 14 ms 23704 KB Output is correct
8 Correct 21 ms 23760 KB Output is correct
9 Correct 12 ms 23724 KB Output is correct
10 Correct 20 ms 23764 KB Output is correct
11 Correct 20 ms 23768 KB Output is correct
12 Correct 12 ms 23764 KB Output is correct
13 Correct 12 ms 23764 KB Output is correct
14 Correct 12 ms 23724 KB Output is correct
15 Correct 11 ms 23764 KB Output is correct
16 Correct 12 ms 23740 KB Output is correct
17 Correct 12 ms 23800 KB Output is correct
18 Correct 13 ms 23764 KB Output is correct
19 Correct 13 ms 23764 KB Output is correct
20 Correct 13 ms 23764 KB Output is correct
21 Correct 13 ms 23804 KB Output is correct
22 Correct 12 ms 23792 KB Output is correct
23 Correct 17 ms 23820 KB Output is correct
24 Correct 17 ms 23832 KB Output is correct
25 Correct 24 ms 23816 KB Output is correct
26 Correct 17 ms 23892 KB Output is correct
27 Correct 22 ms 23804 KB Output is correct
28 Correct 15 ms 23764 KB Output is correct
29 Correct 13 ms 23724 KB Output is correct
30 Correct 14 ms 23892 KB Output is correct
31 Correct 14 ms 23764 KB Output is correct
32 Correct 15 ms 23764 KB Output is correct
33 Correct 274 ms 35704 KB Output is correct
34 Correct 264 ms 35668 KB Output is correct
35 Correct 430 ms 65988 KB Output is correct
36 Correct 420 ms 65936 KB Output is correct
37 Correct 380 ms 33940 KB Output is correct
38 Correct 365 ms 46668 KB Output is correct
39 Correct 243 ms 33668 KB Output is correct
40 Correct 381 ms 61100 KB Output is correct
41 Correct 267 ms 33660 KB Output is correct
42 Correct 253 ms 33700 KB Output is correct
43 Correct 340 ms 61364 KB Output is correct
44 Correct 328 ms 61476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 23788 KB Output is correct
2 Correct 13 ms 23764 KB Output is correct
3 Correct 12 ms 23764 KB Output is correct
4 Correct 17 ms 23764 KB Output is correct
5 Correct 14 ms 23788 KB Output is correct
6 Correct 11 ms 23764 KB Output is correct
7 Correct 12 ms 23764 KB Output is correct
8 Correct 11 ms 23800 KB Output is correct
9 Correct 15 ms 23800 KB Output is correct
10 Correct 21 ms 23776 KB Output is correct
11 Correct 20 ms 23764 KB Output is correct
12 Correct 12 ms 23764 KB Output is correct
13 Correct 14 ms 23720 KB Output is correct
14 Correct 12 ms 23800 KB Output is correct
15 Correct 11 ms 23800 KB Output is correct
16 Correct 12 ms 23800 KB Output is correct
17 Correct 12 ms 23764 KB Output is correct
18 Correct 12 ms 23776 KB Output is correct
19 Correct 12 ms 23764 KB Output is correct
20 Correct 14 ms 23780 KB Output is correct
21 Correct 14 ms 23764 KB Output is correct
22 Correct 12 ms 23756 KB Output is correct
23 Correct 17 ms 23844 KB Output is correct
24 Correct 17 ms 23820 KB Output is correct
25 Correct 20 ms 23800 KB Output is correct
26 Correct 22 ms 23816 KB Output is correct
27 Correct 24 ms 23816 KB Output is correct
28 Correct 19 ms 23892 KB Output is correct
29 Correct 13 ms 23816 KB Output is correct
30 Correct 16 ms 23892 KB Output is correct
31 Correct 14 ms 23788 KB Output is correct
32 Correct 25 ms 23828 KB Output is correct
33 Correct 899 ms 59856 KB Output is correct
34 Correct 1075 ms 68696 KB Output is correct
35 Correct 1008 ms 59948 KB Output is correct
36 Correct 1095 ms 63704 KB Output is correct
37 Correct 291 ms 35652 KB Output is correct
38 Correct 263 ms 35716 KB Output is correct
39 Correct 471 ms 66156 KB Output is correct
40 Correct 433 ms 65964 KB Output is correct
41 Correct 276 ms 33860 KB Output is correct
42 Correct 360 ms 46576 KB Output is correct
43 Correct 208 ms 33664 KB Output is correct
44 Correct 435 ms 61052 KB Output is correct
45 Correct 232 ms 33724 KB Output is correct
46 Correct 245 ms 33828 KB Output is correct
47 Correct 391 ms 61388 KB Output is correct
48 Correct 328 ms 61484 KB Output is correct
49 Correct 894 ms 38712 KB Output is correct
50 Correct 784 ms 38644 KB Output is correct
51 Correct 880 ms 67884 KB Output is correct
52 Correct 908 ms 67512 KB Output is correct
53 Correct 891 ms 37120 KB Output is correct
54 Correct 888 ms 50528 KB Output is correct
55 Correct 287 ms 34708 KB Output is correct
56 Correct 916 ms 62844 KB Output is correct
57 Correct 497 ms 35352 KB Output is correct
58 Correct 702 ms 35808 KB Output is correct
59 Correct 345 ms 61412 KB Output is correct