#include <bits/stdc++.h>
#include "horses.h"
#define MAX 1000001
#define INF LLONG_MAX
#define MOD 1000000007
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define ins insert
#define ff first
#define ss second
#define gett(x,m) get<m>(x)
#define all(a) a.begin(),a.end()
#define lb(a,b) lower_bound(all(a),b)
#define ub(a,b) upper_bound(all(a),b)
#define sortv(a) sort(all(a))
#define sorta(a,sz) sort(a,a+sz)
#define inputar(a,b){\
for(int i=0;i<b;i++){\
cin >> a[i];\
}\
}
#define inputvec(a,b){\
for(int i=0;i<b;i++){\
ll num;\
cin >> num;\
a.pb(num);\
}\
}
#define outputar(a,b){\
for(int i=0;i<b;i++){\
cout << a[i] << " ";\
}\
cout << "\n";\
}
#define outputvec(a){\
for(auto x:a){\
cout << x << " ";\
}\
cout << "\n";\
}
#define reset(a,n,v){\
for(int i=0;i<n;i++){\
a[i]=v;\
}\
}
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef tuple<ll,ll,ll> tll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
typedef double db;
typedef long double ldb;
int n;
vector<ll> X1,Y1;
bool ok=false;
struct segtree_mul{
int size;
vector<ll> muls;
void build(vector<ll> &a,int x,int lx,int rx){
if(rx-lx==1){
if(lx<(int)a.size()){
muls[x]=a[lx];
}
return;
}
int m=(lx+rx)/2;
build(a,2*x+1,lx,m);
build(a,2*x+2,m,rx);
muls[x]=muls[2*x+1]*muls[2*x+2];
muls[x]%=MOD;
}
void build(vector<ll> &a){
build(a,0,0,size);
}
void init(int n){
size=1;
while(size<n){
size*=2;
}
muls.assign(2*size,1);
}
ll mul(int l,int r,int x,int lx,int rx){
if(lx>=r || rx<=l){
return 1;
}
if(lx>=l && rx<=r){
return muls[x];
}
ll s1,s2;
int m=(lx+rx)/2;
s1=mul(l,r,2*x+1,lx,m);
s2=mul(l,r,2*x+2,m,rx);
return (s1*s2)%MOD;
}
ll mul(int l,int r){
return mul(l,r,0,0,size);
}
void set(int i,int v,int x,int lx,int rx){
if(rx-lx==1){
muls[x]=v;
return;
}
int m=(lx+rx)/2;
if(i<m){
set(i,v,2*x+1,lx,m);
}
else{
set(i,v,2*x+2,m,rx);
}
muls[x]=muls[2*x+1]*muls[2*x+2];
muls[x]%=MOD;
}
void set(int i,int v){
set(i,v,0,0,size);
}
};
struct segtree_max{
int size;
vector<pll> maxs;
void build(vector<ll> &a,int x,int lx,int rx){
if(rx-lx==1){
if(lx<(int)a.size()){
maxs[x]=mp(a[lx],lx);
}
return;
}
int m=(lx+rx)/2;
build(a,2*x+1,lx,m);
build(a,2*x+2,m,rx);
maxs[x]=max(maxs[2*x+1],maxs[2*x+2]);
}
void build(vector<ll> &a){
build(a,0,0,size);
}
void init(int n){
size=1;
while(size<n){
size*=2;
}
maxs.assign(2*size,mp(0,0));
}
pll get_max(int l,int r,int x,int lx,int rx){
if(lx>=r || rx<=l){
return mp(0,0);
}
if(lx>=l && rx<=r){
return maxs[x];
}
pll s1,s2;
int m=(lx+rx)/2;
s1=get_max(l,r,2*x+1,lx,m);
s2=get_max(l,r,2*x+2,m,rx);
return max(s1,s2);
}
pll get_max(int l,int r){
return get_max(l,r,0,0,size);
}
void set(int i,int v,int x,int lx,int rx){
if(rx-lx==1){
maxs[x]=mp(v,i);
return;
}
int m=(lx+rx)/2;
if(i<m){
set(i,v,2*x+1,lx,m);
}
else{
set(i,v,2*x+2,m,rx);
}
maxs[x]=max(maxs[2*x+1],maxs[2*x+2]);
}
void set(int i,int v){
set(i,v,0,0,size);
}
};
segtree_mul st_mul;
segtree_max st_max;
set<ll> b;
int init(int N, int X[], int Y[]) {
n=N;
X1.resize(n);
Y1.resize(n);
b.ins(-1);
for(int i=0;i<N;i++){
if(X[i]!=1){
b.ins(i);
}
X1[i]=X[i];
Y1[i]=Y[i];
}
st_mul.init(n);
st_mul.build(X1);
st_max.init(n);
st_max.build(Y1);
ll h=n-1;
ll cnt=0;
vector<ll> a;
while(h>=0){
if(X1[h]==1){
auto it=b.upper_bound(h);
--it;
pll p=st_max.get_max(*it+1,h+1);
a.pb(p.ss);
h=*it+1;
}
else{
a.pb(h);
}
h--;
cnt++;
if(cnt==60){
break;
}
}
reverse(all(a));
ll sz=(ll)a.size();
ll h1=1;
ll ind=0;
for(int i=1;i<sz;i++){
if(h1>MOD/X1[a[i]]){
h1=MOD;
}
else{
h1*=X1[a[i]];
}
if(h1>Y1[a[ind]]/Y1[a[i]]){
ind=i;
h1=1;
}
}
ll res1=st_mul.mul(0,a[ind]+1)*Y1[a[ind]];
res1%=MOD;
return res1;
}
int updateX(int pos, int val) {
st_mul.set(pos,val);
if(X1[pos]==1 && val!=1){
b.ins(pos);
}
else if(val==1){
b.erase(pos);
}
X1[pos]=val;
ll h=n-1;
ll cnt=0;
vector<ll> a;
while(h>=0){
if(X1[h]==1){
auto it=b.upper_bound(h);
--it;
pll p=st_max.get_max(*it+1,h+1);
a.pb(p.ss);
h=*it+1;
}
else{
a.pb(h);
}
h--;
cnt++;
if(cnt==60){
break;
}
}
reverse(all(a));
ll sz=(ll)a.size();
ll h1=1;
ll ind=0;
for(int i=1;i<sz;i++){
if(h1>MOD/X1[a[i]]){
h1=MOD;
}
else{
h1*=X1[a[i]];
}
if(h1>Y1[a[ind]]/Y1[a[i]]){
ind=i;
h1=1;
}
}
ll res1=st_mul.mul(0,a[ind]+1)*Y1[a[ind]];
res1%=MOD;
return res1;
}
int updateY(int pos, int val){
st_max.set(pos,val);
Y1[pos]=val;
ll h=n-1;
ll cnt=0;
vector<ll> a;
while(h>=0){
if(X1[h]==1){
auto it=b.upper_bound(h);
--it;
pll p=st_max.get_max(*it+1,h+1);
a.pb(p.ss);
h=*it+1;
}
else{
a.pb(h);
}
h--;
cnt++;
if(cnt==60){
break;
}
}
reverse(all(a));
ll sz=(ll)a.size();
ll h1=1;
ll ind=0;
for(int i=1;i<sz;i++){
if(h1>MOD/X1[a[i]]){
h1=MOD;
}
else{
h1*=X1[a[i]];
}
if(h1>Y1[a[ind]]/Y1[a[i]]){
ind=i;
h1=1;
}
}
ll res1=st_mul.mul(0,a[ind]+1)*Y1[a[ind]];
res1%=MOD;
return res1;
}
/*
int X[MAX],Y[MAX],m,x,y,z;
int main(){
cin >> n;
for(int i=0;i<n;i++){
cin >> X[i];
}
for(int i=0;i<n;i++){
cin >> Y[i];
}
cout << init(n,X,Y) << "\n";
cin >> m;
for(int i=0;i<m;i++){
if(i==1){
ok=true;
}
else{
ok=false;
}
cin >> x >> y >> z;
if(x==1){
cout << updateX(y,z) << "\n";
}
else{
cout << updateY(y,z) << "\n";
}
}
}
*/
Compilation message
horses.cpp: In member function 'void segtree_mul::init(int)':
horses.cpp:77:19: warning: declaration of 'n' shadows a global declaration [-Wshadow]
77 | void init(int n){
| ~~~~^
horses.cpp:55:5: note: shadowed declaration is here
55 | int n;
| ^
horses.cpp: In member function 'void segtree_max::init(int)':
horses.cpp:137:19: warning: declaration of 'n' shadows a global declaration [-Wshadow]
137 | void init(int n){
| ~~~~^
horses.cpp:55:5: note: shadowed declaration is here
55 | int n;
| ^
horses.cpp: In function 'int init(int, int*, int*)':
horses.cpp:204:28: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
204 | pll p=st_max.get_max(*it+1,h+1);
| ~~~^~
horses.cpp:204:32: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
204 | pll p=st_max.get_max(*it+1,h+1);
| ~^~
horses.cpp:233:29: warning: conversion from '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} to 'int' may change value [-Wconversion]
233 | ll res1=st_mul.mul(0,a[ind]+1)*Y1[a[ind]];
horses.cpp:235:9: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
235 | return res1;
| ^~~~
horses.cpp: In function 'int updateX(int, int)':
horses.cpp:253:28: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
253 | pll p=st_max.get_max(*it+1,h+1);
| ~~~^~
horses.cpp:253:32: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
253 | pll p=st_max.get_max(*it+1,h+1);
| ~^~
horses.cpp:282:29: warning: conversion from '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} to 'int' may change value [-Wconversion]
282 | ll res1=st_mul.mul(0,a[ind]+1)*Y1[a[ind]];
horses.cpp:284:9: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
284 | return res1;
| ^~~~
horses.cpp: In function 'int updateY(int, int)':
horses.cpp:297:28: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
297 | pll p=st_max.get_max(*it+1,h+1);
| ~~~^~
horses.cpp:297:32: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
297 | pll p=st_max.get_max(*it+1,h+1);
| ~^~
horses.cpp:326:29: warning: conversion from '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} to 'int' may change value [-Wconversion]
326 | ll res1=st_mul.mul(0,a[ind]+1)*Y1[a[ind]];
horses.cpp:328:9: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
328 | return res1;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 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 |
344 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 |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 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 |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
4 ms |
348 KB |
Output is correct |
24 |
Correct |
2 ms |
348 KB |
Output is correct |
25 |
Correct |
2 ms |
348 KB |
Output is correct |
26 |
Correct |
2 ms |
348 KB |
Output is correct |
27 |
Correct |
3 ms |
348 KB |
Output is correct |
28 |
Correct |
3 ms |
600 KB |
Output is correct |
29 |
Correct |
1 ms |
524 KB |
Output is correct |
30 |
Correct |
2 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
4 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
254 ms |
61456 KB |
Output is correct |
2 |
Correct |
267 ms |
61264 KB |
Output is correct |
3 |
Correct |
280 ms |
61200 KB |
Output is correct |
4 |
Correct |
274 ms |
61264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 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 |
1 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 |
1 ms |
344 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 |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
344 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 |
2 ms |
348 KB |
Output is correct |
25 |
Correct |
2 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
5 ms |
348 KB |
Output is correct |
28 |
Correct |
4 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
2 ms |
452 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
4 ms |
344 KB |
Output is correct |
33 |
Correct |
92 ms |
40784 KB |
Output is correct |
34 |
Correct |
56 ms |
40784 KB |
Output is correct |
35 |
Correct |
158 ms |
71268 KB |
Output is correct |
36 |
Correct |
153 ms |
71180 KB |
Output is correct |
37 |
Correct |
86 ms |
38996 KB |
Output is correct |
38 |
Correct |
111 ms |
51788 KB |
Output is correct |
39 |
Correct |
32 ms |
38928 KB |
Output is correct |
40 |
Correct |
137 ms |
66192 KB |
Output is correct |
41 |
Correct |
29 ms |
38736 KB |
Output is correct |
42 |
Correct |
64 ms |
38800 KB |
Output is correct |
43 |
Correct |
124 ms |
66640 KB |
Output is correct |
44 |
Correct |
124 ms |
66640 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 |
600 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 |
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 |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
4 ms |
516 KB |
Output is correct |
24 |
Correct |
2 ms |
348 KB |
Output is correct |
25 |
Correct |
2 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
600 KB |
Output is correct |
27 |
Correct |
4 ms |
348 KB |
Output is correct |
28 |
Correct |
3 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
2 ms |
348 KB |
Output is correct |
31 |
Correct |
2 ms |
348 KB |
Output is correct |
32 |
Correct |
4 ms |
348 KB |
Output is correct |
33 |
Correct |
254 ms |
64892 KB |
Output is correct |
34 |
Correct |
270 ms |
73812 KB |
Output is correct |
35 |
Correct |
285 ms |
65072 KB |
Output is correct |
36 |
Correct |
274 ms |
68772 KB |
Output is correct |
37 |
Correct |
91 ms |
40876 KB |
Output is correct |
38 |
Correct |
45 ms |
40788 KB |
Output is correct |
39 |
Correct |
157 ms |
71100 KB |
Output is correct |
40 |
Correct |
154 ms |
70988 KB |
Output is correct |
41 |
Correct |
83 ms |
39012 KB |
Output is correct |
42 |
Correct |
110 ms |
51852 KB |
Output is correct |
43 |
Correct |
35 ms |
38740 KB |
Output is correct |
44 |
Correct |
137 ms |
66128 KB |
Output is correct |
45 |
Correct |
31 ms |
38924 KB |
Output is correct |
46 |
Correct |
65 ms |
38992 KB |
Output is correct |
47 |
Correct |
128 ms |
66600 KB |
Output is correct |
48 |
Correct |
126 ms |
66660 KB |
Output is correct |
49 |
Correct |
597 ms |
44116 KB |
Output is correct |
50 |
Correct |
193 ms |
43840 KB |
Output is correct |
51 |
Correct |
320 ms |
72984 KB |
Output is correct |
52 |
Correct |
272 ms |
72452 KB |
Output is correct |
53 |
Correct |
587 ms |
42064 KB |
Output is correct |
54 |
Correct |
494 ms |
55588 KB |
Output is correct |
55 |
Correct |
122 ms |
39764 KB |
Output is correct |
56 |
Correct |
284 ms |
68176 KB |
Output is correct |
57 |
Correct |
107 ms |
40536 KB |
Output is correct |
58 |
Correct |
450 ms |
41028 KB |
Output is correct |
59 |
Correct |
128 ms |
66424 KB |
Output is correct |