#include "horses.h"
#include <bits/stdc++.h>
using namespace std;
#define OPTM ios_base::sync_with_stdio(0); cin.tie(0);
#define INF int64_t(1e9+7)
#define ln '\n'
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define us unsigned short
#define FOR(i,s,n) for (int i = s; i < n; i++)
#define FORR(i,n,s) for (int i = n; i > s; i--)
#define FORX(u, arr) for (auto u : arr)
#define PB push_back
#define in(v,x) (v.find(x) != v.end())
#define F first
#define S second
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define UM unordered_map
#define US unordered_set
#define PQ priority_queue
#define ALL(v) v.begin(), v.end()
const ll LLINF = 1e18+1;
const int MAXN = 5e5+10, LOG = 30;
const ll MAXX = 1e9;
int n;
ll x[MAXN], y[MAXN];
set<int> a;
ll st[MAXN*4], st1[MAXN*4];
void build(int s, int tl, int tr) {
if (tl == tr) st[s] = y[tl];
else {
int tm = (tl+tr)/2;
build(s*2,tl,tm);
build(s*2+1,tm+1,tr);
st[s] = max(st[s*2], st[s*2+1]);
}
}
void update(int s, int tl, int tr, int idx, ll val) {
if (tl == tr) st[s] = val;
else {
int tm = (tl+tr)/2;
if (idx <= tm) update(s*2,tl,tm,idx,val);
else update(s*2+1,tm+1,tr,idx,val);
st[s] = max(st[s*2], st[s*2+1]);
}
}
ll query(int s, int tl, int tr, int l, int r) {
if (l <= tl && r >= tr) return st[s];
else {
int tm = (tl+tr)/2;
ll cur = -INF;
if (l <= tm) cur = max(cur, query(s*2,tl,tm,l,r));
if (r > tm) cur = max(cur, query(s*2+1,tm+1,tr,l,r));
return cur;
}
}
void build1(int s, int tl, int tr) {
if (tl == tr) st1[s] = x[tl];
else {
int tm = (tl+tr)/2;
build1(s*2,tl,tm);
build1(s*2+1,tm+1,tr);
st1[s] = (st1[s*2]*st1[s*2+1])%INF;
}
}
void update1(int s, int tl, int tr, int idx, ll val) {
if (tl == tr) st1[s] = val;
else {
int tm = (tl+tr)/2;
if (idx <= tm) update1(s*2,tl,tm,idx,val);
else update1(s*2+1,tm+1,tr,idx,val);
st1[s] = (st1[s*2]*st1[s*2+1])%INF;
}
}
ll query1(int s, int tl, int tr, int l, int r) {
if (l <= tl && r >= tr) return st1[s];
else {
int tm = (tl+tr)/2;
ll cur = 1ll;
if (l <= tm) cur = (cur*query1(s*2,tl,tm,l,r))%INF;
if (r > tm) cur = (cur*query1(s*2+1,tm+1,tr,l,r))%INF;
return cur;
}
}
int calc() {
vector<int> v;
int cnt = 0;
for (auto it = a.rbegin(); it != a.rend(); it++) {
v.PB(*it);
cnt++;
if (cnt >= LOG) break;
}
//v.PB(0);
reverse(ALL(v));
// FORX(u,v) cout << u << ' ';
// cout << ln;
int vn = v.size();
ll xval = 1, yval = 0, cur = -INF, idx = -1;
FOR(i,0,vn) {
int en = i == vn-1 ? n-1 : v[i+1]-1;
xval *= x[v[i]];
yval = query(1,0,n-1,v[i],en);
if (xval > MAXX || xval*yval > cur) {
idx = i;
cur = yval;
xval = 1;
}
}
int en = idx == vn-1 ? n-1 : v[idx+1]-1;
// cout << v[idx] << ln;
ll res = (query1(1,0,n-1,0,v[idx])*query(1,0,n-1,v[idx],en))%INF;
//ll res = 0;
return res;
}
int init(int N, int X[], int Y[]) {
OPTM;
n = N;
FOR(i,0,n) {
x[i] = X[i];
y[i] = Y[i];
}
a.insert(0);
build(1,0,n-1);
build1(1,0,n-1);
FOR(i,0,n) if (x[i] > 1) a.insert(i);
return calc();
}
int updateX(int pos, int val) {
update1(1,0,n-1,pos,val);
x[pos] = val;
if (val == 1 && a.count(pos) && pos != 0) a.erase(pos);
if (val > 1) a.insert(pos);
return calc();
}
int updateY(int pos, int val) {
update(1,0,n-1,pos,val);
y[pos] = val;
return calc();
}
Compilation message
horses.cpp: In function 'int calc()':
horses.cpp:107:20: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
107 | int vn = v.size();
| ~~~~~~^~
horses.cpp:123:12: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
123 | return res;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
3 ms |
340 KB |
Output is correct |
24 |
Correct |
4 ms |
340 KB |
Output is correct |
25 |
Correct |
3 ms |
468 KB |
Output is correct |
26 |
Correct |
3 ms |
340 KB |
Output is correct |
27 |
Correct |
4 ms |
340 KB |
Output is correct |
28 |
Correct |
3 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
3 ms |
340 KB |
Output is correct |
31 |
Correct |
2 ms |
340 KB |
Output is correct |
32 |
Correct |
3 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
474 ms |
52996 KB |
Output is correct |
2 |
Correct |
584 ms |
53028 KB |
Output is correct |
3 |
Correct |
548 ms |
52940 KB |
Output is correct |
4 |
Correct |
648 ms |
52920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
3 ms |
340 KB |
Output is correct |
24 |
Correct |
4 ms |
340 KB |
Output is correct |
25 |
Correct |
5 ms |
340 KB |
Output is correct |
26 |
Correct |
3 ms |
340 KB |
Output is correct |
27 |
Correct |
3 ms |
340 KB |
Output is correct |
28 |
Correct |
3 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
3 ms |
340 KB |
Output is correct |
31 |
Correct |
2 ms |
340 KB |
Output is correct |
32 |
Correct |
2 ms |
340 KB |
Output is correct |
33 |
Correct |
86 ms |
28668 KB |
Output is correct |
34 |
Correct |
72 ms |
28616 KB |
Output is correct |
35 |
Correct |
224 ms |
52088 KB |
Output is correct |
36 |
Correct |
209 ms |
51992 KB |
Output is correct |
37 |
Correct |
75 ms |
28620 KB |
Output is correct |
38 |
Correct |
127 ms |
40604 KB |
Output is correct |
39 |
Correct |
40 ms |
28620 KB |
Output is correct |
40 |
Correct |
197 ms |
52044 KB |
Output is correct |
41 |
Correct |
49 ms |
28544 KB |
Output is correct |
42 |
Correct |
65 ms |
30640 KB |
Output is correct |
43 |
Correct |
173 ms |
58292 KB |
Output is correct |
44 |
Correct |
169 ms |
58240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
3 ms |
340 KB |
Output is correct |
24 |
Correct |
3 ms |
340 KB |
Output is correct |
25 |
Correct |
3 ms |
460 KB |
Output is correct |
26 |
Correct |
2 ms |
340 KB |
Output is correct |
27 |
Correct |
3 ms |
340 KB |
Output is correct |
28 |
Correct |
2 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
4 ms |
464 KB |
Output is correct |
31 |
Correct |
2 ms |
340 KB |
Output is correct |
32 |
Correct |
3 ms |
340 KB |
Output is correct |
33 |
Correct |
520 ms |
52868 KB |
Output is correct |
34 |
Correct |
566 ms |
52884 KB |
Output is correct |
35 |
Correct |
559 ms |
53000 KB |
Output is correct |
36 |
Correct |
647 ms |
53056 KB |
Output is correct |
37 |
Correct |
95 ms |
28592 KB |
Output is correct |
38 |
Correct |
72 ms |
28636 KB |
Output is correct |
39 |
Correct |
203 ms |
52032 KB |
Output is correct |
40 |
Correct |
199 ms |
52012 KB |
Output is correct |
41 |
Correct |
77 ms |
28652 KB |
Output is correct |
42 |
Correct |
129 ms |
40484 KB |
Output is correct |
43 |
Correct |
37 ms |
28420 KB |
Output is correct |
44 |
Correct |
192 ms |
52084 KB |
Output is correct |
45 |
Correct |
52 ms |
28556 KB |
Output is correct |
46 |
Correct |
68 ms |
30608 KB |
Output is correct |
47 |
Correct |
167 ms |
58284 KB |
Output is correct |
48 |
Correct |
165 ms |
58400 KB |
Output is correct |
49 |
Correct |
527 ms |
35724 KB |
Output is correct |
50 |
Correct |
398 ms |
35604 KB |
Output is correct |
51 |
Correct |
543 ms |
64852 KB |
Output is correct |
52 |
Correct |
532 ms |
64224 KB |
Output is correct |
53 |
Correct |
484 ms |
33816 KB |
Output is correct |
54 |
Correct |
483 ms |
47432 KB |
Output is correct |
55 |
Correct |
134 ms |
31664 KB |
Output is correct |
56 |
Correct |
536 ms |
59732 KB |
Output is correct |
57 |
Correct |
295 ms |
32144 KB |
Output is correct |
58 |
Correct |
390 ms |
32648 KB |
Output is correct |
59 |
Correct |
169 ms |
58308 KB |
Output is correct |