#include "horses.h"
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 5e5 + 5;
const int MAX_K = 31;
const int MOD = 1e9 + 7;
int _N;
vector<int> _X(MAX_N), _Y(MAX_N);
set<int> non_one;
int mult(int a, int b) {
long long res = 1LL * a * b;
if (res >= MOD) res %= MOD;
return res;
}
class SegmentTree {
private:
struct Node {
int product;
int opt;
Node() {
product = 1;
opt = -1;
}
void merge(Node l, Node r) {
product = mult(l.product, r.product);
opt = l.opt;
if (l.opt == -1) {
opt = r.opt;
return;
}
if (r.opt == -1) {
opt = l.opt;
return;
}
auto start_location = non_one.upper_bound(opt);
long long best_multiplier = _Y[opt];
long long road = 1;
for (int i = 0; i < MAX_K && start_location != non_one.end() && *start_location <= r.opt; i++) {
int cur = _X[*start_location];
long long nxt = 1LL * road * cur;
if (nxt >= best_multiplier || (nxt * _Y[*start_location] >= best_multiplier)) {
opt = r.opt;
return;
}
start_location = next(start_location);
road = nxt;
}
if (1LL * road * _Y[r.opt] >= best_multiplier) {
opt = r.opt;
}
}
};
const int LO, HI;
vector<Node> seg;
void build(int l, int r, int node) {
if (l == r) {
seg[node].opt = l;
seg[node].product = _X[l];
} else {
const int mid = l + (r - l) / 2;
build(l, mid, 2 * node + 1);
build(mid + 1, r, 2 * node + 2);
seg[node].merge(seg[2 * node + 1], seg[2 * node + 2]);
}
}
void update(int where, int delta, int l, int r, int node) {
if (l > where || r < where) {
return;
}
if (l >= where && r <= where) {
seg[node].opt = l;
seg[node].product = delta;
return;
}
const int mid = l + (r - l) / 2;
update(where, delta, l, mid, 2 * node + 1);
update(where, delta, mid + 1, r, 2 * node + 2);
seg[node].merge(seg[2 * node + 1], seg[2 * node + 2]);
}
int query(int ql, int qr, int l, int r, int node) {
if (l > qr || r < ql) {
return 1;
}
if (l >= ql && r <= qr) {
return seg[node].product;
}
const int mid = l + (r - l) / 2;
return mult(query(ql, qr, l, mid, 2 * node + 1), query(ql, qr, mid + 1, r, 2 * node + 2));
}
public:
SegmentTree(int n) : LO(0), HI(n - 1) {
seg = vector<Node>(4 * n);
}
void build() {
build(LO, HI, 0);
}
void update(int where, int delta) {
update(where, delta, LO, HI, 0);
}
int solve() {
return mult(query(0, seg[0].opt, LO, HI, 0), _Y[seg[0].opt]);
}
};
SegmentTree seg(MAX_N);
int init(int N, int X[], int Y[]) {
for (int i = 0; i < N; i++) {
_X[i] = X[i];
_Y[i] = Y[i];
if (_X[i] > 1) {
non_one.insert(i);
}
}
seg.build();
return seg.solve();
}
int updateX(int pos, int val) {
if (_X[pos] > 1) {
non_one.erase(pos);
}
_X[pos] = val;
if (_X[pos] > 1) {
non_one.insert(pos);
}
seg.update(pos, val);
return seg.solve();
}
int updateY(int pos, int val) {
_Y[pos] = val;
seg.update(pos, _X[pos]);
return seg.solve();
}
Compilation message
horses.cpp: In function 'int mult(int, int)':
horses.cpp:16:10: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
16 | return res;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
19840 KB |
Output is correct |
2 |
Correct |
20 ms |
19840 KB |
Output is correct |
3 |
Correct |
22 ms |
19840 KB |
Output is correct |
4 |
Correct |
19 ms |
19872 KB |
Output is correct |
5 |
Correct |
19 ms |
19840 KB |
Output is correct |
6 |
Correct |
20 ms |
19840 KB |
Output is correct |
7 |
Correct |
19 ms |
19840 KB |
Output is correct |
8 |
Correct |
19 ms |
19840 KB |
Output is correct |
9 |
Correct |
19 ms |
19840 KB |
Output is correct |
10 |
Correct |
19 ms |
19840 KB |
Output is correct |
11 |
Correct |
19 ms |
19840 KB |
Output is correct |
12 |
Correct |
19 ms |
19840 KB |
Output is correct |
13 |
Correct |
20 ms |
19840 KB |
Output is correct |
14 |
Correct |
19 ms |
19840 KB |
Output is correct |
15 |
Correct |
19 ms |
19840 KB |
Output is correct |
16 |
Correct |
19 ms |
19840 KB |
Output is correct |
17 |
Correct |
19 ms |
19840 KB |
Output is correct |
18 |
Correct |
19 ms |
19840 KB |
Output is correct |
19 |
Correct |
19 ms |
19840 KB |
Output is correct |
20 |
Correct |
19 ms |
19840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
19968 KB |
Output is correct |
2 |
Correct |
21 ms |
19840 KB |
Output is correct |
3 |
Correct |
19 ms |
19840 KB |
Output is correct |
4 |
Correct |
19 ms |
19840 KB |
Output is correct |
5 |
Correct |
19 ms |
19840 KB |
Output is correct |
6 |
Correct |
19 ms |
19840 KB |
Output is correct |
7 |
Correct |
19 ms |
19840 KB |
Output is correct |
8 |
Correct |
19 ms |
19840 KB |
Output is correct |
9 |
Correct |
19 ms |
19840 KB |
Output is correct |
10 |
Correct |
18 ms |
19840 KB |
Output is correct |
11 |
Correct |
19 ms |
19840 KB |
Output is correct |
12 |
Correct |
20 ms |
19968 KB |
Output is correct |
13 |
Correct |
20 ms |
19840 KB |
Output is correct |
14 |
Correct |
19 ms |
19840 KB |
Output is correct |
15 |
Correct |
19 ms |
19840 KB |
Output is correct |
16 |
Correct |
19 ms |
19840 KB |
Output is correct |
17 |
Correct |
19 ms |
19840 KB |
Output is correct |
18 |
Correct |
19 ms |
19840 KB |
Output is correct |
19 |
Correct |
19 ms |
19840 KB |
Output is correct |
20 |
Correct |
21 ms |
19968 KB |
Output is correct |
21 |
Correct |
20 ms |
19840 KB |
Output is correct |
22 |
Correct |
21 ms |
19840 KB |
Output is correct |
23 |
Correct |
27 ms |
20096 KB |
Output is correct |
24 |
Correct |
26 ms |
19968 KB |
Output is correct |
25 |
Correct |
32 ms |
19968 KB |
Output is correct |
26 |
Correct |
32 ms |
19968 KB |
Output is correct |
27 |
Correct |
20 ms |
19968 KB |
Output is correct |
28 |
Correct |
29 ms |
19968 KB |
Output is correct |
29 |
Correct |
19 ms |
19968 KB |
Output is correct |
30 |
Correct |
31 ms |
19968 KB |
Output is correct |
31 |
Correct |
23 ms |
19968 KB |
Output is correct |
32 |
Correct |
25 ms |
19968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
517 ms |
48636 KB |
Output is correct |
2 |
Correct |
1022 ms |
49528 KB |
Output is correct |
3 |
Correct |
1261 ms |
49184 KB |
Output is correct |
4 |
Correct |
1069 ms |
49400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
19968 KB |
Output is correct |
2 |
Correct |
20 ms |
19840 KB |
Output is correct |
3 |
Correct |
19 ms |
19840 KB |
Output is correct |
4 |
Correct |
19 ms |
19840 KB |
Output is correct |
5 |
Correct |
19 ms |
19968 KB |
Output is correct |
6 |
Correct |
19 ms |
19968 KB |
Output is correct |
7 |
Correct |
21 ms |
19968 KB |
Output is correct |
8 |
Correct |
19 ms |
19840 KB |
Output is correct |
9 |
Correct |
19 ms |
19840 KB |
Output is correct |
10 |
Correct |
20 ms |
19968 KB |
Output is correct |
11 |
Correct |
19 ms |
19840 KB |
Output is correct |
12 |
Correct |
19 ms |
19840 KB |
Output is correct |
13 |
Correct |
20 ms |
19840 KB |
Output is correct |
14 |
Correct |
19 ms |
19840 KB |
Output is correct |
15 |
Correct |
19 ms |
19840 KB |
Output is correct |
16 |
Correct |
19 ms |
19968 KB |
Output is correct |
17 |
Correct |
19 ms |
19968 KB |
Output is correct |
18 |
Correct |
21 ms |
19840 KB |
Output is correct |
19 |
Correct |
19 ms |
19840 KB |
Output is correct |
20 |
Correct |
20 ms |
19968 KB |
Output is correct |
21 |
Correct |
20 ms |
19840 KB |
Output is correct |
22 |
Correct |
21 ms |
19840 KB |
Output is correct |
23 |
Correct |
27 ms |
20096 KB |
Output is correct |
24 |
Correct |
26 ms |
19968 KB |
Output is correct |
25 |
Correct |
32 ms |
19968 KB |
Output is correct |
26 |
Correct |
31 ms |
20088 KB |
Output is correct |
27 |
Correct |
21 ms |
19968 KB |
Output is correct |
28 |
Correct |
30 ms |
19968 KB |
Output is correct |
29 |
Correct |
20 ms |
19968 KB |
Output is correct |
30 |
Correct |
31 ms |
19968 KB |
Output is correct |
31 |
Correct |
23 ms |
19968 KB |
Output is correct |
32 |
Correct |
26 ms |
19968 KB |
Output is correct |
33 |
Correct |
74 ms |
24824 KB |
Output is correct |
34 |
Correct |
71 ms |
24824 KB |
Output is correct |
35 |
Correct |
275 ms |
48888 KB |
Output is correct |
36 |
Correct |
266 ms |
48888 KB |
Output is correct |
37 |
Correct |
53 ms |
24440 KB |
Output is correct |
38 |
Correct |
176 ms |
36216 KB |
Output is correct |
39 |
Correct |
39 ms |
24192 KB |
Output is correct |
40 |
Correct |
251 ms |
48248 KB |
Output is correct |
41 |
Correct |
42 ms |
24312 KB |
Output is correct |
42 |
Correct |
43 ms |
24312 KB |
Output is correct |
43 |
Correct |
228 ms |
48120 KB |
Output is correct |
44 |
Correct |
235 ms |
48124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
19840 KB |
Output is correct |
2 |
Correct |
20 ms |
19840 KB |
Output is correct |
3 |
Correct |
19 ms |
19840 KB |
Output is correct |
4 |
Correct |
19 ms |
19840 KB |
Output is correct |
5 |
Correct |
19 ms |
19840 KB |
Output is correct |
6 |
Correct |
19 ms |
19840 KB |
Output is correct |
7 |
Correct |
19 ms |
19872 KB |
Output is correct |
8 |
Correct |
19 ms |
19840 KB |
Output is correct |
9 |
Correct |
19 ms |
19840 KB |
Output is correct |
10 |
Correct |
19 ms |
19840 KB |
Output is correct |
11 |
Correct |
19 ms |
19840 KB |
Output is correct |
12 |
Correct |
19 ms |
19968 KB |
Output is correct |
13 |
Correct |
20 ms |
19840 KB |
Output is correct |
14 |
Correct |
19 ms |
19840 KB |
Output is correct |
15 |
Correct |
19 ms |
19840 KB |
Output is correct |
16 |
Correct |
22 ms |
19840 KB |
Output is correct |
17 |
Correct |
19 ms |
19960 KB |
Output is correct |
18 |
Correct |
19 ms |
19840 KB |
Output is correct |
19 |
Correct |
21 ms |
19968 KB |
Output is correct |
20 |
Correct |
20 ms |
19840 KB |
Output is correct |
21 |
Correct |
20 ms |
19840 KB |
Output is correct |
22 |
Correct |
21 ms |
19840 KB |
Output is correct |
23 |
Correct |
27 ms |
20004 KB |
Output is correct |
24 |
Correct |
26 ms |
19968 KB |
Output is correct |
25 |
Correct |
33 ms |
20088 KB |
Output is correct |
26 |
Correct |
31 ms |
19968 KB |
Output is correct |
27 |
Correct |
20 ms |
19968 KB |
Output is correct |
28 |
Correct |
29 ms |
19968 KB |
Output is correct |
29 |
Correct |
20 ms |
20096 KB |
Output is correct |
30 |
Correct |
31 ms |
19968 KB |
Output is correct |
31 |
Correct |
23 ms |
19968 KB |
Output is correct |
32 |
Correct |
25 ms |
19968 KB |
Output is correct |
33 |
Correct |
521 ms |
49016 KB |
Output is correct |
34 |
Correct |
1027 ms |
50040 KB |
Output is correct |
35 |
Correct |
1276 ms |
49016 KB |
Output is correct |
36 |
Correct |
1068 ms |
52856 KB |
Output is correct |
37 |
Correct |
76 ms |
28024 KB |
Output is correct |
38 |
Correct |
72 ms |
27896 KB |
Output is correct |
39 |
Correct |
278 ms |
55060 KB |
Output is correct |
40 |
Correct |
271 ms |
54924 KB |
Output is correct |
41 |
Correct |
53 ms |
26104 KB |
Output is correct |
42 |
Correct |
176 ms |
39032 KB |
Output is correct |
43 |
Correct |
40 ms |
25848 KB |
Output is correct |
44 |
Correct |
255 ms |
51832 KB |
Output is correct |
45 |
Correct |
43 ms |
25984 KB |
Output is correct |
46 |
Correct |
44 ms |
25976 KB |
Output is correct |
47 |
Correct |
233 ms |
51832 KB |
Output is correct |
48 |
Correct |
234 ms |
51704 KB |
Output is correct |
49 |
Correct |
345 ms |
30456 KB |
Output is correct |
50 |
Correct |
345 ms |
30456 KB |
Output is correct |
51 |
Correct |
566 ms |
56828 KB |
Output is correct |
52 |
Correct |
485 ms |
57080 KB |
Output is correct |
53 |
Correct |
288 ms |
29308 KB |
Output is correct |
54 |
Correct |
417 ms |
42872 KB |
Output is correct |
55 |
Correct |
108 ms |
27000 KB |
Output is correct |
56 |
Correct |
465 ms |
52984 KB |
Output is correct |
57 |
Correct |
115 ms |
27640 KB |
Output is correct |
58 |
Correct |
131 ms |
28280 KB |
Output is correct |
59 |
Correct |
231 ms |
51960 KB |
Output is correct |