Submission #1077842

# Submission time Handle Problem Language Result Execution time Memory
1077842 2024-08-27T09:35:59 Z juicy Horses (IOI15_horses) C++17
37 / 100
383 ms 53332 KB
#include "horses.h"

#include <bits/stdc++.h>

const int N = 5e5, MD = 1e9 + 7;

int n;
int a[N], prv[N], ma[4 * N], s[4 * N];
std::set<int> st;

void upd(int i, int x, int *s, int pul(int, int), int id = 1, int l = 0, int r = n - 1) {
  if (l == r) {
    s[id] = x;
    return;
  }
  int md = (l + r) / 2;
  if (i <= md) {
    upd(i, x, s, pul, id * 2, l, md);
  } else {
    upd(i, x, s, pul, id * 2 + 1, md + 1, r);
  }
  s[id] = pul(s[id * 2], s[id * 2 + 1]);
} 

int qry(int u, int v, int *s, int pul(int, int), int id = 1, int l = 0, int r = n - 1) {
  if (u <= l && r <= v) {
    return s[id];
  }
  int md = (l + r) / 2;
  if (v <= md) {
    return qry(u, v, s, pul, id * 2, l, md);
  }
  if (md < u) {
    return qry(u, v, s, pul, id * 2 + 1, md + 1, r);
  }
  return pul(qry(u, v, s, pul, id * 2, l, md), qry(u, v, s, pul, id * 2 + 1, md + 1, r));
}

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

int max(int a, int b) {
  return std::max(a, b);
}

int get() {
  if (!st.size()) {
    return ma[1];
  }
  int p = *st.rbegin(), prod = 1;
  std::vector<int> cands;
  while (~p) {
    cands.push_back(p);
    if (a[p] > MD / prod) {
      break;
    }
    prod *= a[p];
    p = prv[p];
  } 
  reverse(cands.begin(), cands.end());
  int base = qry(0, cands[0], s, mul);
  long long best = qry(cands[0], cands.size() == 1 ? n - 1 : cands[1] - 1, ma, max);
  prod = 1;
  for (int i = 1; i < cands.size(); ++i) {
    prod *= a[cands[i]];
    int j = i + 1 == cands.size() ? n - 1 : cands[i + 1] - 1;
    best = std::max(best, (long long) qry(cands[i], j, ma, max) * prod);
  }
  return best % MD * base % MD;
}

int init(int n, int *x, int *y) {
  ::n = n;
  for (int i = 0; i < n; ++i) {
    if (x[i] > 1) {
      prv[i] = !st.size() ? -1 : *st.rbegin();
      st.insert(i);
    }
    upd(i, x[i], s, mul);
    upd(i, y[i], ma, max);
  }
  for (int i = 0; i < n; ++i) {
    a[i] = x[i];
  }
  return get();
}

void ers(int i) {
  auto it = st.find(i);
  it = st.erase(it);
  if (it != st.end()) {
    prv[*it] = it == st.begin() ? -1 : *prev(it);
  }
}

void add(int i) {
  auto it = st.insert(i).first;
  prv[*it] = it == st.begin() ? -1 : *prev(it);
  ++it;
  if (it != st.end()) {
    prv[*it] = i;
  }
}

int updateX(int i, int x) {	
  if (x == 1 && a[i] > 1) {
    ers(i);
  } 
  if (x > 1 && a[i] == 1) {
    add(i);
  }
  upd(i, a[i] = x, s, mul);
  return get();
}

int updateY(int i, int x) {
  upd(i, x, ma, max);
  return get();
}

Compilation message

horses.cpp: In function 'void upd(int, int, int*, int (*)(int, int), int, int, int)':
horses.cpp:11:29: warning: declaration of 's' shadows a global declaration [-Wshadow]
   11 | void upd(int i, int x, int *s, int pul(int, int), int id = 1, int l = 0, int r = n - 1) {
      |                        ~~~~~^
horses.cpp:8:30: note: shadowed declaration is here
    8 | int a[N], prv[N], ma[4 * N], s[4 * N];
      |                              ^
horses.cpp: In function 'int qry(int, int, int*, int (*)(int, int), int, int, int)':
horses.cpp:25:28: warning: declaration of 's' shadows a global declaration [-Wshadow]
   25 | int qry(int u, int v, int *s, int pul(int, int), int id = 1, int l = 0, int r = n - 1) {
      |                       ~~~~~^
horses.cpp:8:30: note: shadowed declaration is here
    8 | int a[N], prv[N], ma[4 * N], s[4 * N];
      |                              ^
horses.cpp: In function 'int mul(int, int)':
horses.cpp:39:13: warning: declaration of 'a' shadows a global declaration [-Wshadow]
   39 | int mul(int a, int b) {
      |         ~~~~^
horses.cpp:8:5: note: shadowed declaration is here
    8 | int a[N], prv[N], ma[4 * N], s[4 * N];
      |     ^
horses.cpp:40:28: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
   40 |   return (long long) a * b % MD;
      |          ~~~~~~~~~~~~~~~~~~^~~~
horses.cpp: In function 'int max(int, int)':
horses.cpp:43:13: warning: declaration of 'a' shadows a global declaration [-Wshadow]
   43 | int max(int a, int b) {
      |         ~~~~^
horses.cpp:8:5: note: shadowed declaration is here
    8 | int a[N], prv[N], ma[4 * N], s[4 * N];
      |     ^
horses.cpp: In function 'int get()':
horses.cpp:65:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   65 |   for (int i = 1; i < cands.size(); ++i) {
      |                   ~~^~~~~~~~~~~~~~
horses.cpp:67:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   67 |     int j = i + 1 == cands.size() ? n - 1 : cands[i + 1] - 1;
      |             ~~~~~~^~~~~~~~~~~~~~~
horses.cpp:70:27: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
   70 |   return best % MD * base % MD;
      |          ~~~~~~~~~~~~~~~~~^~~~
horses.cpp: In function 'int init(int, int*, int*)':
horses.cpp:73:14: warning: declaration of 'n' shadows a global declaration [-Wshadow]
   73 | int init(int n, int *x, int *y) {
      |          ~~~~^
horses.cpp:7:5: note: shadowed declaration is here
    7 | int n;
      |     ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 444 KB Output is correct
11 Correct 1 ms 856 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 436 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 444 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 344 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Incorrect 3 ms 348 KB Output isn't correct
28 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 383 ms 44720 KB Output is correct
2 Correct 343 ms 53332 KB Output is correct
3 Correct 338 ms 44556 KB Output is correct
4 Correct 355 ms 48468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 440 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 604 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 452 KB Output is correct
27 Incorrect 2 ms 348 KB Output isn't correct
28 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 536 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Incorrect 2 ms 348 KB Output isn't correct
28 Halted 0 ms 0 KB -