# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
395137 |
2021-04-27T21:24:02 Z |
andremfq |
Horses (IOI15_horses) |
C++17 |
|
923 ms |
54264 KB |
//ideia Leonardo
#include <bits/stdc++.h>
#include "horses.h"
#define mp make_pair
#define mt make_tuple
#define ff first
#define ss second
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll INFL = (1LL<<62)-1;
const int INF = (1<<30)-1;
const int MAXN = 500001;
const int MOD = 1e9+7;
struct seg{
ll id, v;
};
seg tree[4*MAXN];
ll mult[4*MAXN];
int *x, *y;
int n;
set<int> s; // todos os indices de x>=2
seg merge(seg a, seg b){
if(a.v<b.v)
return b;
return a;
}
void build(int id, int l, int r){
if(l==r){
tree[id] = {l, y[l]};
mult[id] = x[l];
return;
}
int e = id*2+1;
int d = id*2+2;
int m = (l+r)>>1;
build(e,l,m);
build(d,m+1,r);
tree[id] = merge(tree[e],tree[d]);
mult[id] = (mult[e]*mult[d])%MOD;
}
void update(int id, int l, int r, int q){
if(r< q || l > q){
return;
}
if(l==q && r == q){
tree[id] = {l,y[l]};
return;
}
int e = id*2+1;
int d = id*2+2;
int m = (l+r)>>1;
update(e,l,m,q);
update(d,m+1,r,q);
tree[id] = merge(tree[e],tree[d]);
}
seg query(int id, int l, int r, int ql, int qr){
if(ql > r || qr < l || qr < ql)
return {0,-1};
if(ql <= l && r <= qr)
return tree[id];
int e = id*2+1;
int d = id*2+2;
int m = (l+r)>>1;
return merge(query(e,l,m,ql,qr),query(d,m+1,r,ql,qr));
}
void updatemult(int id, int l, int r, int q){
if(q < l || q > r)
return;
if(l==q && r ==q){
mult[id] = x[l];
return;
}
int e = id*2+1;
int d = id*2+2;
int m = (l+r)>>1;
updatemult(e,l,m,q);
updatemult(d,m+1,r,q);
mult[id] = (mult[e]*mult[d])%MOD;
}
ll querymult(int id, int l, int r, int ql, int qr){
if(ql > r || qr < l || qr < ql)
return 1;
if(ql <= l && r <= qr)
return mult[id];
int e = id*2+1;
int d = id*2+2;
int m = (l+r)>>1;
return (querymult(e,l,m,ql,qr)*querymult(d,m+1,r,ql,qr))%MOD;
}
ll calc(){
auto it = s.rbegin();
//iterador reverso: começa na ultima posição e termina 1 a menos da primeira posição
seg resp = {0,0};
int r = n-1;
while(true){
if(it == s.rend()){
resp = merge(resp, query(0,0,n-1,0,r));
// compara a resposta atual com o bloco de 1 mais à esquerda
break;
}
resp = merge(resp,query(0,0,n-1,*it,r));
// compara a resposta atual com o próximo indice de x >= 2 e com o bloco de x = 1 à direita
resp.v*= x[*it];
r = *it-1;
if(resp.v > (ll)1e9)
break;
it++;
}
return (querymult(0,0,n-1,0,resp.id) * y[resp.id]) % MOD;
}
int init(int N ,int X[], int Y[]){
x = X;
y = Y;
// o X[] e Y[] continuam sendo válidos mesmo depois da chamada da funcão então eu vou usar *x e *y pra acessar o array
n = N;
build(0,0,n-1);
for(int i = 0; i < n; i++)
if(x[i] >= 2)
s.insert(i);
return calc();
}
int updateX(int pos, int val){
s.erase(pos);
x[pos] = val;
if(x[pos]>=2)
s.insert(pos);
updatemult(0,0,n-1,pos);
return calc();
}
int updateY(int pos, int val){
y[pos] = val;
update(0,0,n-1,pos);
return calc();
}
/*int main(){
int N, Q;
cin >> N >> Q;
int X[N], Y[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';
for(int i = 0; i < Q; i++){
int t, a, b;
cin >> t >> a >> b;
if(t){
cout << updateY(a,b) << '\n';
} else{
cout << updateX(a,b) << '\n';
}
}
}*/
Compilation message
horses.cpp: In function 'll calc()':
horses.cpp:122:38: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
122 | return (querymult(0,0,n-1,0,resp.id) * y[resp.id]) % MOD;
| ~~~~~^~
horses.cpp: In function 'int init(int, int*, int*)':
horses.cpp:134:16: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
134 | return calc();
| ~~~~^~
horses.cpp: In function 'int updateX(int, int)':
horses.cpp:143:16: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
143 | return calc();
| ~~~~^~
horses.cpp: In function 'int updateY(int, int)':
horses.cpp:149:16: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
149 | return calc();
| ~~~~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
300 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
300 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
300 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
304 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
300 KB |
Output is correct |
17 |
Correct |
1 ms |
308 KB |
Output is correct |
18 |
Correct |
1 ms |
308 KB |
Output is correct |
19 |
Correct |
1 ms |
328 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
300 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
2 ms |
316 KB |
Output is correct |
25 |
Correct |
2 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
460 KB |
Output is correct |
27 |
Correct |
5 ms |
332 KB |
Output is correct |
28 |
Correct |
2 ms |
332 KB |
Output is correct |
29 |
Correct |
2 ms |
332 KB |
Output is correct |
30 |
Correct |
2 ms |
332 KB |
Output is correct |
31 |
Correct |
3 ms |
332 KB |
Output is correct |
32 |
Correct |
5 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
755 ms |
54164 KB |
Output is correct |
2 |
Correct |
363 ms |
53836 KB |
Output is correct |
3 |
Correct |
382 ms |
53732 KB |
Output is correct |
4 |
Correct |
393 ms |
53740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
208 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
304 KB |
Output is correct |
6 |
Correct |
1 ms |
296 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
300 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
208 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
2 ms |
332 KB |
Output is correct |
25 |
Correct |
2 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
5 ms |
332 KB |
Output is correct |
28 |
Correct |
3 ms |
332 KB |
Output is correct |
29 |
Correct |
2 ms |
332 KB |
Output is correct |
30 |
Correct |
2 ms |
332 KB |
Output is correct |
31 |
Correct |
3 ms |
332 KB |
Output is correct |
32 |
Correct |
6 ms |
332 KB |
Output is correct |
33 |
Correct |
58 ms |
29556 KB |
Output is correct |
34 |
Correct |
54 ms |
29576 KB |
Output is correct |
35 |
Correct |
194 ms |
52872 KB |
Output is correct |
36 |
Correct |
189 ms |
52824 KB |
Output is correct |
37 |
Correct |
140 ms |
29524 KB |
Output is correct |
38 |
Correct |
109 ms |
41416 KB |
Output is correct |
39 |
Correct |
49 ms |
29380 KB |
Output is correct |
40 |
Correct |
195 ms |
52860 KB |
Output is correct |
41 |
Correct |
78 ms |
29492 KB |
Output is correct |
42 |
Correct |
101 ms |
29392 KB |
Output is correct |
43 |
Correct |
172 ms |
52744 KB |
Output is correct |
44 |
Correct |
189 ms |
52804 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
304 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
308 KB |
Output is correct |
17 |
Correct |
1 ms |
208 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
2 ms |
332 KB |
Output is correct |
24 |
Correct |
2 ms |
332 KB |
Output is correct |
25 |
Correct |
2 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
5 ms |
332 KB |
Output is correct |
28 |
Correct |
3 ms |
404 KB |
Output is correct |
29 |
Correct |
2 ms |
332 KB |
Output is correct |
30 |
Correct |
2 ms |
332 KB |
Output is correct |
31 |
Correct |
3 ms |
380 KB |
Output is correct |
32 |
Correct |
5 ms |
332 KB |
Output is correct |
33 |
Correct |
749 ms |
54264 KB |
Output is correct |
34 |
Correct |
374 ms |
53828 KB |
Output is correct |
35 |
Correct |
388 ms |
53756 KB |
Output is correct |
36 |
Correct |
404 ms |
54004 KB |
Output is correct |
37 |
Correct |
84 ms |
29508 KB |
Output is correct |
38 |
Correct |
52 ms |
29548 KB |
Output is correct |
39 |
Correct |
209 ms |
52916 KB |
Output is correct |
40 |
Correct |
191 ms |
52904 KB |
Output is correct |
41 |
Correct |
139 ms |
29544 KB |
Output is correct |
42 |
Correct |
111 ms |
41332 KB |
Output is correct |
43 |
Correct |
48 ms |
29368 KB |
Output is correct |
44 |
Correct |
191 ms |
52916 KB |
Output is correct |
45 |
Correct |
76 ms |
29360 KB |
Output is correct |
46 |
Correct |
101 ms |
29420 KB |
Output is correct |
47 |
Correct |
203 ms |
52804 KB |
Output is correct |
48 |
Correct |
167 ms |
52992 KB |
Output is correct |
49 |
Correct |
203 ms |
31460 KB |
Output is correct |
50 |
Correct |
160 ms |
31428 KB |
Output is correct |
51 |
Correct |
411 ms |
53844 KB |
Output is correct |
52 |
Correct |
277 ms |
53828 KB |
Output is correct |
53 |
Correct |
923 ms |
31360 KB |
Output is correct |
54 |
Correct |
342 ms |
44300 KB |
Output is correct |
55 |
Correct |
202 ms |
29508 KB |
Output is correct |
56 |
Correct |
323 ms |
53832 KB |
Output is correct |
57 |
Correct |
489 ms |
30280 KB |
Output is correct |
58 |
Correct |
727 ms |
30276 KB |
Output is correct |
59 |
Correct |
190 ms |
52848 KB |
Output is correct |