#include "horses.h"
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
typedef long long llong;
const int MAXN = 500000 + 10;
const int MOD = 1e9 + 7;
struct Node
{
__int128 ans;
__int128 prodX;
bool isANSbig;
bool isXbig;
Node()
{
ans = -1;
}
inline friend Node operator + (Node left, Node right)
{
Node res;
res.prodX = left.prodX * right.prodX;
if (res.prodX >= 1LL * MOD * MOD)
{
res.isXbig = true;
res.prodX %= 1LL * MOD * MOD;
} else
{
res.isXbig = left.isXbig || right.isXbig;
}
if (left.isXbig || right.isANSbig || (left.ans < left.prodX * right.ans))
{
res.ans = left.prodX * right.ans;
if (res.ans >= 1LL * MOD * MOD || left.isXbig || right.isANSbig)
{
res.isANSbig = true;
res.ans %= 1LL * MOD * MOD;
}
} else
{
res.ans = left.ans;
res.isANSbig = false;
}
return res;
}
};
int n;
int x[MAXN];
int y[MAXN];
struct SegmentTree
{
Node tree[4 * MAXN];
void build(int l, int r, int node)
{
if (l == r)
{
tree[node].prodX = x[l];
tree[node].isXbig = false;
tree[node].isANSbig = false;
tree[node].ans = 1LL * x[l] * y[l];
return;
}
int mid = (l + r) / 2;
build(l, mid, 2*node);
build(mid + 1, r, 2*node + 1);
tree[node] = tree[2*node] + tree[2*node + 1];
}
void update(int l, int r, int node, int queryIdx, int queryVal, bool which)
{
if (l == r)
{
if (!which) x[l] = queryVal;
else y[l] = queryVal;
tree[node].ans = 1LL * x[l] * y[l];
tree[node].prodX = x[l];
return;
}
int mid = (l + r) / 2;
if (queryIdx <= mid) update(l, mid, 2*node, queryIdx, queryVal, which);
else update(mid + 1, r, 2*node + 1, queryVal, queryVal, which);
tree[node] = tree[2*node] + tree[2*node + 1];
}
inline int getANS()
{
return tree[1].ans % MOD;
}
void build()
{
build(1, n, 1);
}
void update(int idx, int val, bool which)
{
update(1, n, 1, idx, val, which);
}
};
SegmentTree tree;
int init(int N, int X[], int Y[])
{
n = N;
for (int i = 1 ; i <= n ; ++i)
{
x[i] = X[i - 1];
y[i] = Y[i - 1];
}
tree.build();
return tree.getANS();
}
int updateX(int pos, int val)
{
pos++;
tree.update(pos, val, false);
return tree.getANS();
}
int updateY(int pos, int val)
{
pos++;
tree.update(pos, val, true);
return tree.getANS();
}
Compilation message
horses.cpp: In member function 'int SegmentTree::getANS()':
horses.cpp:97:28: warning: conversion from '__int128' to 'int' may change value [-Wconversion]
97 | return tree[1].ans % MOD;
| ~~~~~~~~~~~~^~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
94200 KB |
Output is correct |
2 |
Correct |
35 ms |
94200 KB |
Output is correct |
3 |
Correct |
36 ms |
94212 KB |
Output is correct |
4 |
Correct |
35 ms |
94136 KB |
Output is correct |
5 |
Correct |
34 ms |
94244 KB |
Output is correct |
6 |
Correct |
37 ms |
94136 KB |
Output is correct |
7 |
Correct |
36 ms |
94212 KB |
Output is correct |
8 |
Correct |
40 ms |
94164 KB |
Output is correct |
9 |
Correct |
35 ms |
94156 KB |
Output is correct |
10 |
Correct |
36 ms |
94236 KB |
Output is correct |
11 |
Correct |
35 ms |
94168 KB |
Output is correct |
12 |
Correct |
35 ms |
94156 KB |
Output is correct |
13 |
Correct |
35 ms |
94156 KB |
Output is correct |
14 |
Correct |
41 ms |
94172 KB |
Output is correct |
15 |
Correct |
37 ms |
94204 KB |
Output is correct |
16 |
Correct |
36 ms |
94128 KB |
Output is correct |
17 |
Correct |
34 ms |
94160 KB |
Output is correct |
18 |
Correct |
35 ms |
94156 KB |
Output is correct |
19 |
Correct |
35 ms |
94136 KB |
Output is correct |
20 |
Correct |
37 ms |
94156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
94212 KB |
Output is correct |
2 |
Correct |
34 ms |
94160 KB |
Output is correct |
3 |
Correct |
35 ms |
94212 KB |
Output is correct |
4 |
Correct |
34 ms |
94184 KB |
Output is correct |
5 |
Correct |
35 ms |
94232 KB |
Output is correct |
6 |
Correct |
39 ms |
94188 KB |
Output is correct |
7 |
Correct |
38 ms |
94160 KB |
Output is correct |
8 |
Correct |
35 ms |
94164 KB |
Output is correct |
9 |
Correct |
37 ms |
94292 KB |
Output is correct |
10 |
Correct |
38 ms |
94152 KB |
Output is correct |
11 |
Correct |
36 ms |
94164 KB |
Output is correct |
12 |
Correct |
35 ms |
94152 KB |
Output is correct |
13 |
Correct |
35 ms |
94156 KB |
Output is correct |
14 |
Correct |
36 ms |
94244 KB |
Output is correct |
15 |
Correct |
35 ms |
94212 KB |
Output is correct |
16 |
Correct |
35 ms |
94208 KB |
Output is correct |
17 |
Correct |
36 ms |
94156 KB |
Output is correct |
18 |
Correct |
35 ms |
94292 KB |
Output is correct |
19 |
Correct |
38 ms |
94196 KB |
Output is correct |
20 |
Correct |
36 ms |
94136 KB |
Output is correct |
21 |
Incorrect |
35 ms |
94112 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
116 ms |
103156 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
94152 KB |
Output is correct |
2 |
Correct |
35 ms |
94156 KB |
Output is correct |
3 |
Correct |
36 ms |
94164 KB |
Output is correct |
4 |
Correct |
37 ms |
94116 KB |
Output is correct |
5 |
Correct |
39 ms |
94156 KB |
Output is correct |
6 |
Correct |
35 ms |
94156 KB |
Output is correct |
7 |
Correct |
35 ms |
94132 KB |
Output is correct |
8 |
Correct |
37 ms |
94148 KB |
Output is correct |
9 |
Correct |
34 ms |
94256 KB |
Output is correct |
10 |
Correct |
35 ms |
94220 KB |
Output is correct |
11 |
Correct |
36 ms |
94156 KB |
Output is correct |
12 |
Correct |
38 ms |
94256 KB |
Output is correct |
13 |
Correct |
36 ms |
94156 KB |
Output is correct |
14 |
Correct |
36 ms |
94236 KB |
Output is correct |
15 |
Correct |
36 ms |
94252 KB |
Output is correct |
16 |
Correct |
36 ms |
94204 KB |
Output is correct |
17 |
Correct |
36 ms |
94212 KB |
Output is correct |
18 |
Correct |
35 ms |
94232 KB |
Output is correct |
19 |
Correct |
37 ms |
94296 KB |
Output is correct |
20 |
Correct |
38 ms |
94156 KB |
Output is correct |
21 |
Incorrect |
36 ms |
94196 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
94156 KB |
Output is correct |
2 |
Correct |
35 ms |
94124 KB |
Output is correct |
3 |
Correct |
35 ms |
94152 KB |
Output is correct |
4 |
Correct |
35 ms |
94196 KB |
Output is correct |
5 |
Correct |
39 ms |
94132 KB |
Output is correct |
6 |
Correct |
35 ms |
94136 KB |
Output is correct |
7 |
Correct |
35 ms |
94152 KB |
Output is correct |
8 |
Correct |
38 ms |
94188 KB |
Output is correct |
9 |
Correct |
36 ms |
94188 KB |
Output is correct |
10 |
Correct |
36 ms |
94196 KB |
Output is correct |
11 |
Correct |
44 ms |
94228 KB |
Output is correct |
12 |
Correct |
38 ms |
94180 KB |
Output is correct |
13 |
Correct |
39 ms |
94212 KB |
Output is correct |
14 |
Correct |
36 ms |
94168 KB |
Output is correct |
15 |
Correct |
36 ms |
94116 KB |
Output is correct |
16 |
Correct |
38 ms |
94240 KB |
Output is correct |
17 |
Correct |
37 ms |
94128 KB |
Output is correct |
18 |
Correct |
35 ms |
94216 KB |
Output is correct |
19 |
Correct |
35 ms |
94228 KB |
Output is correct |
20 |
Correct |
38 ms |
94156 KB |
Output is correct |
21 |
Incorrect |
36 ms |
94172 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |