#include "horses.h"
#include <bits/stdc++.h>
using namespace std;
#define scd(t) scanf("%d", &t)
#define sclld(t) scanf("%lld", &t)
#define forr(i, j, k) for (int i = j; i < k; i++)
#define frange(i, j) forr(i, 0, j)
#define all(cont) cont.begin(), cont.end()
#define mp make_pair
#define pb push_back
#define f first
#define s second
typedef long long int lli;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<lli> vll;
typedef vector<string> vs;
typedef vector<pii> vii;
typedef vector<vi> vvi;
typedef map<int, int> mpii;
typedef set<int> seti;
typedef multiset<int> mseti;
typedef long double ld;
template <class T>
struct SegTree
{
int size = 1, n;
vector<T> segtree;
vector<T> vec;
T def; // Assign a value
void init(int l, T defv)
{
n = l;
def = defv;
while (size < n)
size *= 2;
segtree.assign(2 * size, def);
vec.assign(size, def);
}
T operation(T x, T y)
{
return max(x, y); // Any required operation
}
void recalculate(int x)
{
segtree[x] = operation(segtree[2 * x + 1], segtree[2 * x + 2]);
}
void build(int x, int l, int r)
{
if (l == r)
{
segtree[x] = vec[l];
return;
}
int mid = (l + r) / 2;
build(2 * x + 1, l, mid);
build(2 * x + 2, mid + 1, r);
recalculate(x);
}
void build()
{
build(0, 0, size - 1);
}
void set(int id, T val)
{
vec[id] = val;
}
void update(int x, int l, int r, int id, T val)
{
if (l == r)
{
segtree[x] = val;
return;
}
int mid = (l + r) / 2;
if (id <= mid)
{
update(2 * x + 1, l, mid, id, val);
}
else
{
update(2 * x + 2, mid + 1, r, id, val);
}
recalculate(x);
}
void update(int id, T val)
{
update(0, 0, size - 1, id, val);
}
T query(int x, int l, int r, int lx, int rx)
{
if (lx > r || rx < l)
{
return def;
}
if (lx <= l && r <= rx)
{
return segtree[x];
}
int mid = (l + r) / 2;
T v1 = query(2 * x + 1, l, mid, lx, rx);
T v2 = query(2 * x + 2, mid + 1, r, lx, rx);
return operation(v1, v2);
}
T query(int lx, int rx)
{
return query(0, 0, size - 1, lx, rx);
}
};
const lli mod = 1e9 + 7;
template <class T>
struct SegTreeMul
{
int size = 1, n;
vector<T> segtree;
vector<T> vec;
T def; // Assign a value
void init(int l, T defv)
{
n = l;
def = defv;
while (size < n)
size *= 2;
segtree.assign(2 * size, def);
vec.assign(size, def);
}
T operation(T x, T y)
{
return (x*y)%mod; // Any required operation
}
void recalculate(int x)
{
segtree[x] = operation(segtree[2 * x + 1], segtree[2 * x + 2]);
}
void build(int x, int l, int r)
{
if (l == r)
{
segtree[x] = vec[l];
return;
}
int mid = (l + r) / 2;
build(2 * x + 1, l, mid);
build(2 * x + 2, mid + 1, r);
recalculate(x);
}
void build()
{
build(0, 0, size - 1);
}
void set(int id, T val)
{
vec[id] = val;
}
void update(int x, int l, int r, int id, T val)
{
if (l == r)
{
segtree[x] = val;
return;
}
int mid = (l + r) / 2;
if (id <= mid)
{
update(2 * x + 1, l, mid, id, val);
}
else
{
update(2 * x + 2, mid + 1, r, id, val);
}
recalculate(x);
}
void update(int id, T val)
{
update(0, 0, size - 1, id, val);
}
T query(int x, int l, int r, int lx, int rx)
{
if (lx > r || rx < l)
{
return def;
}
if (lx <= l && r <= rx)
{
return segtree[x];
}
int mid = (l + r) / 2;
T v1 = query(2 * x + 1, l, mid, lx, rx);
T v2 = query(2 * x + 2, mid + 1, r, lx, rx);
return operation(v1, v2);
}
T query(int lx, int rx)
{
return query(0, 0, size - 1, lx, rx);
}
};
SegTree<lli> segtree;
SegTreeMul<lli> segmul;
seti act;
vll v1, v2;
int n;
int init(int N, int X[], int Y[]) {
n = N;
segtree.init(n, 0LL);
segmul.init(n, 1);
v1 = v2 = vll(n);
frange(i, n) {
if(X[i] != 1) act.insert(i);
v1[i] = X[i];
v2[i] = Y[i];
segtree.set(i, v2[i]);
segmul.set(i, v1[i]);
}
act.insert(0);
segtree.build();
segmul.build();
auto it = act.end();
frange(i, 30) {
it--;
if(it == act.begin()) break;
}
int ma = *it;
lli v = segtree.query(ma, n-1);
it++;
lli curr = 1;
while(it != act.end()) {
int id = *it;
curr *= v1[id];
lli val = segtree.query(id, n-1);
if(curr >= v || curr * val >= v) {
ma = id;
v = val;
curr = 1;
}
it++;
}
lli out = segmul.query(0, ma) * segtree.query(ma, n-1) % mod;
return out;
}
int updateX(int pos, int val) {
segmul.update(pos, val);
if(v1[pos] != 1 && val == 1) act.erase(pos);
if(val != 1) act.insert(pos);
act.insert(0);
v1[pos] = val;
auto it = act.end();
frange(i, 30) {
it--;
if(it == act.begin()) break;
}
int ma = *it;
lli v = segtree.query(ma, n-1);
it++;
lli curr = 1;
while(it != act.end()) {
int id = *it;
curr *= v1[id];
lli val = segtree.query(id, n-1);
if(curr >= v || curr * val >= v) {
ma = id;
v = val;
curr = 1;
}
it++;
}
lli out = segmul.query(0, ma) * segtree.query(ma, n-1) % mod;
return out;
}
int updateY(int pos, int val) {
segtree.update(pos, val);
v2[pos] = val;
auto it = act.end();
frange(i, 30) {
it--;
if(it == act.begin()) break;
}
int ma = *it;
lli v = segtree.query(ma, n-1);
it++;
lli curr = 1;
while(it != act.end()) {
int id = *it;
curr *= v1[id];
lli val = segtree.query(id, n-1);
if(curr >= v || curr * val >= v) {
ma = id;
v = val;
curr = 1;
}
it++;
}
lli out = segmul.query(0, ma) * segtree.query(ma, n-1) % mod;
return out;
}
Compilation message
horses.cpp: In function 'int init(int, int*, int*)':
horses.cpp:277:9: warning: conversion from 'lli' {aka 'long long int'} to 'int' may change value [-Wconversion]
277 | return out;
| ^~~
horses.cpp: In function 'int updateX(int, int)':
horses.cpp:299:7: warning: declaration of 'lli val' shadows a parameter [-Wshadow]
299 | lli val = segtree.query(id, n-1);
| ^~~
horses.cpp:280:26: note: shadowed declaration is here
280 | int updateX(int pos, int val) {
| ~~~~^~~
horses.cpp:310:9: warning: conversion from 'lli' {aka 'long long int'} to 'int' may change value [-Wconversion]
310 | return out;
| ^~~
horses.cpp: In function 'int updateY(int, int)':
horses.cpp:329:7: warning: declaration of 'lli val' shadows a parameter [-Wshadow]
329 | lli val = segtree.query(id, n-1);
| ^~~
horses.cpp:313:26: note: shadowed declaration is here
313 | int updateY(int pos, int val) {
| ~~~~^~~
horses.cpp:340:9: warning: conversion from 'lli' {aka 'long long int'} to 'int' may change value [-Wconversion]
340 | return out;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
344 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 |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
17 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
496 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 |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 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 |
4 ms |
348 KB |
Output is correct |
24 |
Correct |
3 ms |
348 KB |
Output is correct |
25 |
Correct |
3 ms |
348 KB |
Output is correct |
26 |
Correct |
2 ms |
348 KB |
Output is correct |
27 |
Correct |
4 ms |
344 KB |
Output is correct |
28 |
Correct |
3 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
3 ms |
348 KB |
Output is correct |
31 |
Correct |
2 ms |
344 KB |
Output is correct |
32 |
Correct |
2 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
404 ms |
61264 KB |
Output is correct |
2 |
Correct |
498 ms |
61264 KB |
Output is correct |
3 |
Correct |
476 ms |
61144 KB |
Output is correct |
4 |
Correct |
544 ms |
61188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
356 KB |
Output is correct |
13 |
Correct |
0 ms |
360 KB |
Output is correct |
14 |
Correct |
0 ms |
352 KB |
Output is correct |
15 |
Correct |
0 ms |
352 KB |
Output is correct |
16 |
Correct |
0 ms |
360 KB |
Output is correct |
17 |
Correct |
0 ms |
360 KB |
Output is correct |
18 |
Correct |
0 ms |
360 KB |
Output is correct |
19 |
Correct |
0 ms |
360 KB |
Output is correct |
20 |
Correct |
0 ms |
360 KB |
Output is correct |
21 |
Correct |
0 ms |
360 KB |
Output is correct |
22 |
Correct |
0 ms |
360 KB |
Output is correct |
23 |
Correct |
3 ms |
360 KB |
Output is correct |
24 |
Correct |
2 ms |
360 KB |
Output is correct |
25 |
Correct |
2 ms |
360 KB |
Output is correct |
26 |
Correct |
2 ms |
360 KB |
Output is correct |
27 |
Correct |
3 ms |
360 KB |
Output is correct |
28 |
Correct |
3 ms |
360 KB |
Output is correct |
29 |
Correct |
1 ms |
360 KB |
Output is correct |
30 |
Correct |
3 ms |
464 KB |
Output is correct |
31 |
Correct |
2 ms |
360 KB |
Output is correct |
32 |
Correct |
2 ms |
360 KB |
Output is correct |
33 |
Correct |
77 ms |
40884 KB |
Output is correct |
34 |
Correct |
62 ms |
40788 KB |
Output is correct |
35 |
Correct |
178 ms |
71452 KB |
Output is correct |
36 |
Correct |
171 ms |
71044 KB |
Output is correct |
37 |
Correct |
68 ms |
38996 KB |
Output is correct |
38 |
Correct |
109 ms |
51796 KB |
Output is correct |
39 |
Correct |
32 ms |
38748 KB |
Output is correct |
40 |
Correct |
173 ms |
66128 KB |
Output is correct |
41 |
Correct |
45 ms |
38736 KB |
Output is correct |
42 |
Correct |
53 ms |
38920 KB |
Output is correct |
43 |
Correct |
130 ms |
66680 KB |
Output is correct |
44 |
Correct |
137 ms |
66640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
600 KB |
Output is correct |
5 |
Correct |
0 ms |
600 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
600 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
596 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 |
3 ms |
360 KB |
Output is correct |
24 |
Correct |
2 ms |
360 KB |
Output is correct |
25 |
Correct |
3 ms |
360 KB |
Output is correct |
26 |
Correct |
2 ms |
360 KB |
Output is correct |
27 |
Correct |
3 ms |
360 KB |
Output is correct |
28 |
Correct |
3 ms |
360 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
3 ms |
348 KB |
Output is correct |
31 |
Correct |
2 ms |
348 KB |
Output is correct |
32 |
Correct |
2 ms |
536 KB |
Output is correct |
33 |
Correct |
421 ms |
65256 KB |
Output is correct |
34 |
Correct |
493 ms |
73808 KB |
Output is correct |
35 |
Correct |
466 ms |
64932 KB |
Output is correct |
36 |
Correct |
523 ms |
69376 KB |
Output is correct |
37 |
Correct |
77 ms |
40924 KB |
Output is correct |
38 |
Correct |
63 ms |
40788 KB |
Output is correct |
39 |
Correct |
178 ms |
71060 KB |
Output is correct |
40 |
Correct |
177 ms |
70996 KB |
Output is correct |
41 |
Correct |
67 ms |
39092 KB |
Output is correct |
42 |
Correct |
109 ms |
51796 KB |
Output is correct |
43 |
Correct |
31 ms |
38744 KB |
Output is correct |
44 |
Correct |
173 ms |
66540 KB |
Output is correct |
45 |
Correct |
44 ms |
38748 KB |
Output is correct |
46 |
Correct |
52 ms |
38804 KB |
Output is correct |
47 |
Correct |
131 ms |
66640 KB |
Output is correct |
48 |
Correct |
141 ms |
66540 KB |
Output is correct |
49 |
Correct |
463 ms |
43860 KB |
Output is correct |
50 |
Correct |
353 ms |
43884 KB |
Output is correct |
51 |
Correct |
452 ms |
73128 KB |
Output is correct |
52 |
Correct |
444 ms |
72532 KB |
Output is correct |
53 |
Correct |
443 ms |
42364 KB |
Output is correct |
54 |
Correct |
463 ms |
55632 KB |
Output is correct |
55 |
Correct |
109 ms |
39748 KB |
Output is correct |
56 |
Correct |
614 ms |
67880 KB |
Output is correct |
57 |
Correct |
227 ms |
40528 KB |
Output is correct |
58 |
Correct |
306 ms |
41044 KB |
Output is correct |
59 |
Correct |
138 ms |
66644 KB |
Output is correct |