Submission #390644

# Submission time Handle Problem Language Result Execution time Memory
390644 2021-04-16T12:53:50 Z WLZ Horses (IOI15_horses) C++14
100 / 100
1112 ms 92100 KB
#include "horses.h"
#include <bits/stdc++.h>
using namespace std;

const int MOD = (int) 1e9 + 7;

int add(int a, int b) {
  if (a + b >= MOD) return a + b - MOD;
  return a + b;
}

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

int n;
vector<int> x, y;
set<int> st;

struct node {
  int l, r, mx, pr;
  node *left, *right;
} *root;

node *build(int l, int r) {
  if (l == r) return new node{l, r, y[l], x[l], nullptr, nullptr};
  node *left = build(l, (l + r) / 2);
  node *right = build((l + r) / 2 + 1, r);
  return new node{l, r, max(left->mx, right->mx), mul(left->pr, right->pr), left, right};
}

void update(node *cur, int pos) {
  if (cur->l == cur->r) {
    cur->mx = y[pos], cur->pr = x[pos];
    return;
  }
  if (pos <= cur->left->r) update(cur->left, pos);
  else update(cur->right, pos);
  cur->mx = max(cur->left->mx, cur->right->mx);
  cur->pr = mul(cur->left->pr, cur->right->pr);
}

int query_mx(node *cur, int l, int r) {
  if (cur->l > r || cur->r < l) return 0;
  if (l <= cur->l && cur->r <= r) return cur->mx;
  return max(query_mx(cur->left, l, r), query_mx(cur->right, l, r));
}

int query_pr(node *cur, int l, int r) {
  if (cur->l > r || cur->r < l) return 1;
  if (l <= cur->l && cur->r <= r) return cur->pr;
  return mul(query_pr(cur->left, l, r), query_pr(cur->right, l, r));
}

int calc_ans() {
  int i = 0;
  st.insert(0);
  vector<int> v;
  for (auto it = st.rbegin(); it != st.rend() && i < 30; it++, i++) {
    v.push_back(*it);
  }
  reverse(v.begin(), v.end());
  int best = 0;
  long long cur = 1, mx = query_mx(root, v[0], (int) v.size() > 1 ? v[1] - 1 : n - 1);
  for (i = 1; i < (int) v.size(); i++) {
    long long tmp = query_mx(root, v[i], i < (int) v.size() - 1 ? v[i + 1] - 1 : n - 1);
    if (cur * x[v[i]] > mx) {
      cur = 1, mx = tmp, best = i;
      continue;
    }
    cur *= x[v[i]];
    if (cur * tmp > mx) {
      cur = 1, mx = tmp, best = i;
    }
  }
  return mul(query_pr(root, 0, v[best]), query_mx(root, v[best], best < (int) v.size() - 1 ? v[best + 1] - 1 : n - 1));
}

int init(int N, int X[], int Y[]) {
  n = N;
  x.assign(X, X + n);
  y.assign(Y, Y + n);
  for (int i = 0; i < n; i++) if (x[i] != 1) st.insert(i);
  root = build(0, n - 1);
  return calc_ans();
}

int updateX(int pos, int val) {	
  if (x[pos] != val) {
    if (val != 1) st.insert(pos);
    else if (st.count(pos)) st.erase(pos);
    x[pos] = val;
    update(root, pos);
  }
  return calc_ans();
}

int updateY(int pos, int val) {
  y[pos] = val;
  update(root, pos);
  return calc_ans();
}

Compilation message

horses.cpp: In function 'int mul(int, int)':
horses.cpp:13:28: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
   13 |   return (long long) a * b % MOD;
      |          ~~~~~~~~~~~~~~~~~~^~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 5 ms 444 KB Output is correct
24 Correct 5 ms 332 KB Output is correct
25 Correct 5 ms 440 KB Output is correct
26 Correct 5 ms 440 KB Output is correct
27 Correct 5 ms 460 KB Output is correct
28 Correct 5 ms 436 KB Output is correct
29 Correct 2 ms 332 KB Output is correct
30 Correct 5 ms 460 KB Output is correct
31 Correct 5 ms 332 KB Output is correct
32 Correct 5 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 820 ms 79540 KB Output is correct
2 Correct 1016 ms 92100 KB Output is correct
3 Correct 999 ms 83396 KB Output is correct
4 Correct 1094 ms 87368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 208 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 208 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 0 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 6 ms 332 KB Output is correct
24 Correct 5 ms 332 KB Output is correct
25 Correct 5 ms 460 KB Output is correct
26 Correct 5 ms 460 KB Output is correct
27 Correct 5 ms 332 KB Output is correct
28 Correct 6 ms 460 KB Output is correct
29 Correct 2 ms 332 KB Output is correct
30 Correct 5 ms 464 KB Output is correct
31 Correct 4 ms 332 KB Output is correct
32 Correct 5 ms 332 KB Output is correct
33 Correct 187 ms 55636 KB Output is correct
34 Correct 153 ms 55704 KB Output is correct
35 Correct 280 ms 78968 KB Output is correct
36 Correct 275 ms 79052 KB Output is correct
37 Correct 188 ms 55596 KB Output is correct
38 Correct 197 ms 67504 KB Output is correct
39 Correct 87 ms 55584 KB Output is correct
40 Correct 286 ms 78980 KB Output is correct
41 Correct 115 ms 55528 KB Output is correct
42 Correct 142 ms 55540 KB Output is correct
43 Correct 204 ms 79096 KB Output is correct
44 Correct 210 ms 78988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 5 ms 332 KB Output is correct
24 Correct 6 ms 312 KB Output is correct
25 Correct 5 ms 440 KB Output is correct
26 Correct 5 ms 436 KB Output is correct
27 Correct 6 ms 332 KB Output is correct
28 Correct 6 ms 448 KB Output is correct
29 Correct 2 ms 332 KB Output is correct
30 Correct 6 ms 480 KB Output is correct
31 Correct 5 ms 424 KB Output is correct
32 Correct 5 ms 416 KB Output is correct
33 Correct 856 ms 79896 KB Output is correct
34 Correct 1112 ms 92048 KB Output is correct
35 Correct 1025 ms 83424 KB Output is correct
36 Correct 1095 ms 87332 KB Output is correct
37 Correct 198 ms 59216 KB Output is correct
38 Correct 147 ms 59172 KB Output is correct
39 Correct 281 ms 89540 KB Output is correct
40 Correct 271 ms 89412 KB Output is correct
41 Correct 176 ms 57340 KB Output is correct
42 Correct 195 ms 70152 KB Output is correct
43 Correct 81 ms 57284 KB Output is correct
44 Correct 266 ms 84532 KB Output is correct
45 Correct 108 ms 57156 KB Output is correct
46 Correct 136 ms 57208 KB Output is correct
47 Correct 197 ms 84984 KB Output is correct
48 Correct 200 ms 84880 KB Output is correct
49 Correct 1065 ms 62276 KB Output is correct
50 Correct 805 ms 62212 KB Output is correct
51 Correct 857 ms 91324 KB Output is correct
52 Correct 831 ms 90884 KB Output is correct
53 Correct 1004 ms 60532 KB Output is correct
54 Correct 798 ms 74100 KB Output is correct
55 Correct 235 ms 58180 KB Output is correct
56 Correct 978 ms 86276 KB Output is correct
57 Correct 534 ms 58916 KB Output is correct
58 Correct 751 ms 59448 KB Output is correct
59 Correct 196 ms 84932 KB Output is correct