#include "horses.h"
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
// It is always optimal to sell all horses in a single year
class SegmentTree {
private:
struct Mint {
int val = 0;
bool overflow = 0;
const Mint operator * (const Mint &x) const {
return {(int) (1ll * this->val * x.val % mod),
(bool) (this->overflow || x.overflow || (1ll * this->val * x.val >= mod))};
}
};
int sz;
vector<Mint> X;
vector<int> Y;
vector<int> optimal;
void Pull(int n, int lc, int rc) {
X[n] = X[lc] * X[rc];
Mint in_between = QueryX(1, 0, sz - 1, optimal[lc] + 1, optimal[rc]);
if (in_between.overflow) {
optimal[n] = optimal[rc];
} else if (Y[optimal[lc]] > 1ll * Y[optimal[rc]] * in_between.val) {
optimal[n] = optimal[lc];
} else {
optimal[n] = optimal[rc];
}
}
Mint QueryX(int n, int tl, int tr, int l, int r) {
if (tr < l || r < tl || tl > tr || l > r) return Mint({1, 0});
if (l <= tl && tr <= r) return X[n];
int mid = (tl + tr) / 2;
int z = n + 2 * (mid - tl + 1);
return QueryX(n + 1, tl, mid, l, r) * QueryX(z, mid + 1, tr, l, r);
}
void UpdateX(int n, int tl, int tr, int p, int x) {
if (tl == tr) {
X[n] = {x, 0};
return;
}
int mid = (tl + tr) / 2;
int z = n + 2 * (mid - tl + 1);
if (p <= mid) {
UpdateX(n + 1, tl, mid, p, x);
} else {
UpdateX(z, mid + 1, tr, p, x);
}
return Pull(n, n + 1, z);
}
void UpdateY(int n, int tl, int tr, int p, int x) {
if (tl == tr) {
Y[tl] = x;
return;
}
int mid = (tl + tr) / 2;
int z = n + 2 * (mid - tl + 1);
if (p <= mid) {
UpdateY(n + 1, tl, mid, p, x);
} else {
UpdateY(z, mid + 1, tr, p, x);
}
return Pull(n, n + 1, z);
}
void Build(int n, int tl, int tr, vector<int> &x, vector<int> &y) {
if (tl == tr) {
X[n] = {x[tl], 0};
Y[tl] = y[tl];
optimal[n] = tl;
return;
}
int mid = (tl + tr) / 2;
int z = n + 2 * (mid - tl + 1);
Build(n + 1, tl, mid, x, y);
Build(z, mid + 1, tr, x, y);
Pull(n, n + 1, z);
}
public:
SegmentTree() {}
SegmentTree(int n, vector<int> x, vector<int> y) : sz(n) {
X.assign(2 * sz, Mint({1, 0}));
Y.assign(sz, 0);
optimal.assign(2 * sz, 0);
Build(1, 0, sz - 1, x, y);
}
void UpdateX(int p, int x) {
return UpdateX(1, 0, sz - 1, p, x);
}
void UpdateY(int p, int x) {
return UpdateY(1, 0, sz - 1, p, x);
}
int Query() {
return 1ll * QueryX(1, 0, sz - 1, 0, optimal[1]).val * Y[optimal[1]] % mod;
}
};
SegmentTree segtree;
int init(int N, int X[], int Y[]) {
segtree = SegmentTree(N, vector<int>(X, X + N), vector<int>(Y, Y + N));
return segtree.Query();
}
int updateX(int pos, int val) {
segtree.UpdateX(pos, val);
return segtree.Query();
}
int updateY(int pos, int val) {
segtree.UpdateY(pos, val);
return segtree.Query();
}
Compilation message
horses.cpp: In member function 'int SegmentTree::Query()':
horses.cpp:104:74: warning: conversion to 'int' from 'long long int' may alter its value [-Wconversion]
return 1ll * QueryX(1, 0, sz - 1, 0, optimal[1]).val * Y[optimal[1]] % mod;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
256 KB |
Output is correct |
2 |
Correct |
5 ms |
256 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
256 KB |
Output is correct |
6 |
Correct |
5 ms |
256 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
256 KB |
Output is correct |
9 |
Correct |
4 ms |
256 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
4 ms |
384 KB |
Output is correct |
13 |
Correct |
4 ms |
256 KB |
Output is correct |
14 |
Correct |
4 ms |
256 KB |
Output is correct |
15 |
Correct |
4 ms |
256 KB |
Output is correct |
16 |
Correct |
4 ms |
256 KB |
Output is correct |
17 |
Correct |
5 ms |
256 KB |
Output is correct |
18 |
Correct |
4 ms |
256 KB |
Output is correct |
19 |
Correct |
4 ms |
256 KB |
Output is correct |
20 |
Correct |
5 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
256 KB |
Output is correct |
4 |
Correct |
4 ms |
256 KB |
Output is correct |
5 |
Correct |
5 ms |
256 KB |
Output is correct |
6 |
Correct |
4 ms |
256 KB |
Output is correct |
7 |
Correct |
5 ms |
256 KB |
Output is correct |
8 |
Correct |
4 ms |
256 KB |
Output is correct |
9 |
Correct |
4 ms |
256 KB |
Output is correct |
10 |
Correct |
5 ms |
256 KB |
Output is correct |
11 |
Correct |
5 ms |
256 KB |
Output is correct |
12 |
Correct |
5 ms |
256 KB |
Output is correct |
13 |
Correct |
4 ms |
256 KB |
Output is correct |
14 |
Correct |
4 ms |
256 KB |
Output is correct |
15 |
Correct |
5 ms |
256 KB |
Output is correct |
16 |
Correct |
4 ms |
256 KB |
Output is correct |
17 |
Correct |
4 ms |
256 KB |
Output is correct |
18 |
Correct |
4 ms |
256 KB |
Output is correct |
19 |
Correct |
4 ms |
256 KB |
Output is correct |
20 |
Correct |
4 ms |
256 KB |
Output is correct |
21 |
Correct |
4 ms |
256 KB |
Output is correct |
22 |
Correct |
4 ms |
256 KB |
Output is correct |
23 |
Correct |
6 ms |
384 KB |
Output is correct |
24 |
Correct |
7 ms |
384 KB |
Output is correct |
25 |
Correct |
6 ms |
308 KB |
Output is correct |
26 |
Correct |
7 ms |
384 KB |
Output is correct |
27 |
Correct |
6 ms |
384 KB |
Output is correct |
28 |
Correct |
7 ms |
384 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
5 ms |
384 KB |
Output is correct |
31 |
Correct |
6 ms |
384 KB |
Output is correct |
32 |
Correct |
6 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
523 ms |
21916 KB |
Output is correct |
2 |
Correct |
484 ms |
21880 KB |
Output is correct |
3 |
Correct |
618 ms |
21888 KB |
Output is correct |
4 |
Correct |
582 ms |
21880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
256 KB |
Output is correct |
2 |
Correct |
4 ms |
256 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
256 KB |
Output is correct |
5 |
Correct |
4 ms |
256 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
256 KB |
Output is correct |
10 |
Correct |
5 ms |
256 KB |
Output is correct |
11 |
Correct |
4 ms |
256 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
4 ms |
256 KB |
Output is correct |
14 |
Correct |
4 ms |
256 KB |
Output is correct |
15 |
Correct |
5 ms |
256 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
4 ms |
256 KB |
Output is correct |
18 |
Correct |
4 ms |
256 KB |
Output is correct |
19 |
Correct |
4 ms |
256 KB |
Output is correct |
20 |
Correct |
4 ms |
256 KB |
Output is correct |
21 |
Correct |
4 ms |
256 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
6 ms |
384 KB |
Output is correct |
24 |
Correct |
7 ms |
384 KB |
Output is correct |
25 |
Correct |
6 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
6 ms |
384 KB |
Output is correct |
28 |
Correct |
7 ms |
512 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
6 ms |
384 KB |
Output is correct |
31 |
Correct |
6 ms |
384 KB |
Output is correct |
32 |
Correct |
6 ms |
384 KB |
Output is correct |
33 |
Correct |
245 ms |
21888 KB |
Output is correct |
34 |
Correct |
240 ms |
21884 KB |
Output is correct |
35 |
Correct |
215 ms |
22008 KB |
Output is correct |
36 |
Correct |
197 ms |
21880 KB |
Output is correct |
37 |
Correct |
193 ms |
22012 KB |
Output is correct |
38 |
Correct |
186 ms |
21888 KB |
Output is correct |
39 |
Correct |
163 ms |
21888 KB |
Output is correct |
40 |
Correct |
170 ms |
21880 KB |
Output is correct |
41 |
Correct |
197 ms |
22008 KB |
Output is correct |
42 |
Correct |
173 ms |
22008 KB |
Output is correct |
43 |
Correct |
157 ms |
21880 KB |
Output is correct |
44 |
Correct |
156 ms |
21880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
256 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
256 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
256 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
256 KB |
Output is correct |
8 |
Correct |
4 ms |
256 KB |
Output is correct |
9 |
Correct |
4 ms |
256 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
256 KB |
Output is correct |
12 |
Correct |
4 ms |
384 KB |
Output is correct |
13 |
Correct |
4 ms |
256 KB |
Output is correct |
14 |
Correct |
4 ms |
256 KB |
Output is correct |
15 |
Correct |
4 ms |
384 KB |
Output is correct |
16 |
Correct |
4 ms |
384 KB |
Output is correct |
17 |
Correct |
4 ms |
256 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
4 ms |
256 KB |
Output is correct |
20 |
Correct |
5 ms |
256 KB |
Output is correct |
21 |
Correct |
4 ms |
256 KB |
Output is correct |
22 |
Correct |
4 ms |
384 KB |
Output is correct |
23 |
Correct |
8 ms |
384 KB |
Output is correct |
24 |
Correct |
7 ms |
384 KB |
Output is correct |
25 |
Correct |
6 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
6 ms |
384 KB |
Output is correct |
28 |
Correct |
6 ms |
384 KB |
Output is correct |
29 |
Correct |
7 ms |
384 KB |
Output is correct |
30 |
Correct |
6 ms |
384 KB |
Output is correct |
31 |
Correct |
6 ms |
384 KB |
Output is correct |
32 |
Correct |
6 ms |
384 KB |
Output is correct |
33 |
Correct |
522 ms |
21888 KB |
Output is correct |
34 |
Correct |
473 ms |
21880 KB |
Output is correct |
35 |
Correct |
638 ms |
22008 KB |
Output is correct |
36 |
Correct |
594 ms |
22008 KB |
Output is correct |
37 |
Correct |
246 ms |
21888 KB |
Output is correct |
38 |
Correct |
240 ms |
22008 KB |
Output is correct |
39 |
Correct |
206 ms |
21880 KB |
Output is correct |
40 |
Correct |
192 ms |
21880 KB |
Output is correct |
41 |
Correct |
197 ms |
22008 KB |
Output is correct |
42 |
Correct |
187 ms |
21900 KB |
Output is correct |
43 |
Correct |
160 ms |
21888 KB |
Output is correct |
44 |
Correct |
175 ms |
21880 KB |
Output is correct |
45 |
Correct |
181 ms |
22008 KB |
Output is correct |
46 |
Correct |
177 ms |
21888 KB |
Output is correct |
47 |
Correct |
155 ms |
21880 KB |
Output is correct |
48 |
Correct |
161 ms |
22024 KB |
Output is correct |
49 |
Correct |
962 ms |
21880 KB |
Output is correct |
50 |
Correct |
885 ms |
21880 KB |
Output is correct |
51 |
Correct |
531 ms |
21920 KB |
Output is correct |
52 |
Correct |
399 ms |
21880 KB |
Output is correct |
53 |
Correct |
720 ms |
21888 KB |
Output is correct |
54 |
Correct |
493 ms |
21880 KB |
Output is correct |
55 |
Correct |
378 ms |
21880 KB |
Output is correct |
56 |
Correct |
388 ms |
21880 KB |
Output is correct |
57 |
Correct |
605 ms |
21888 KB |
Output is correct |
58 |
Correct |
503 ms |
22008 KB |
Output is correct |
59 |
Correct |
153 ms |
21880 KB |
Output is correct |