# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
559495 |
2022-05-10T04:37:33 Z |
joshjms |
Horses (IOI15_horses) |
C++14 |
|
836 ms |
65544 KB |
#include "horses.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define fi first
#define se second
#define debug(x) cout << #x << " => " << x << "\n";
const long long mod = 1e9 + 7;
ll n, x[500005], y[500005];
struct node {
ll idx, val, nxt, prv;
} arr[500005];
set <ll> st;
ll tree[2000005];
void upd(ll idx, ll l, ll r, ll x, ll v) {
if(l == r) {
tree[idx] = v;
return;
}
ll mid = (l + r) / 2;
if(x <= mid) upd(idx * 2, l, mid, x, v);
else upd(idx * 2 + 1, mid + 1, r, x, v);
tree[idx] = (tree[idx * 2] * tree[idx * 2 + 1]) % mod;
}
ll query(ll idx, ll l, ll r, ll x, ll y) {
if(l > r || l > y || r < x) return 1;
if(l >= x && r <= y) return tree[idx];
ll mid = (l + r) / 2;
return (query(idx * 2, l, mid, x, y) * query(idx * 2 + 1, mid + 1, r, x, y)) % mod;
}
ll maxy[2000005];
void updy(ll idx, ll l, ll r, ll x, ll v) {
if(l == r) {
maxy[idx] = v;
return;
}
ll mid = (l + r) / 2;
if(x <= mid) updy(idx * 2, l, mid, x, v);
else updy(idx * 2 + 1, mid + 1, r, x, v);
maxy[idx] = max(maxy[idx * 2], maxy[idx * 2 + 1]);
}
ll queryy(ll idx, ll l, ll r, ll x, ll y) {
if(l > r || l > y || r < x) return 0;
if(l >= x && r <= y) return maxy[idx];
ll mid = (l + r) / 2;
return max(queryy(idx * 2, l, mid, x, y), queryy(idx * 2 + 1, mid + 1, r, x, y));
}
int init(int N, int X[], int Y[]) {
n = N;
for(ll i = 1; i <= n; i++) {
x[i] = X[i - 1];
y[i] = Y[i - 1];
upd(1, 1, n, i, x[i]);
updy(1, 1, n, i, y[i]);
}
for(ll i = 1; i <= n; i++) {
if(x[i] > 1) {
st.insert(i);
}
}
x[n + 1] = 1;
st.insert(n + 1);
x[0] = 1;
st.insert(0);
auto it = st.end();
pair <ll,ll> maxi = {0, 0};
vector <ll> tmp;
for(ll mul = 1; it != st.begin();) {
it--;
mul *= x[*it];
tmp.pb(*it);
if(mul > 1e9) break;
}
reverse(tmp.begin(), tmp.end());
for(ll i = 0, mul = 1; i < tmp.size() - 1; i++) {
ll max_y = queryy(1, 1, n, tmp[i], tmp[i + 1] - 1);
maxi = max(maxi, {mul * max_y, tmp[i]});
mul *= x[tmp[i + 1]];
}
auto nuit = st.upper_bound(maxi.se);
return (query(1, 1, n, 1, maxi.se) * queryy(1, 1, n, maxi.se, *nuit - 1)) % mod;
}
int updateX(int pos, int val) {
pos++;
x[pos] = val;
if(x[pos] == 1) st.erase(pos);
else st.insert(pos);
upd(1, 1, n, pos, val);
auto it = st.end();
pair <ll,ll> maxi = {0, 0};
vector <ll> tmp;
for(ll mul = 1; it != st.begin();) {
it--;
mul *= x[*it];
tmp.pb(*it);
if(mul > 1e9) break;
}
reverse(tmp.begin(), tmp.end());
for(ll i = 0, mul = 1; i < tmp.size() - 1; i++) {
ll max_y = queryy(1, 1, n, tmp[i], tmp[i + 1] - 1);
maxi = max(maxi, {mul * max_y, tmp[i]});
mul *= x[tmp[i + 1]];
}
auto nuit = st.upper_bound(maxi.se);
return (query(1, 1, n, 1, maxi.se) * queryy(1, 1, n, maxi.se, *nuit - 1)) % mod;
}
int updateY(int pos, int val) {
pos++;
y[pos] = val;
updy(1, 1, n, pos, val);
auto it = st.end();
pair <ll,ll> maxi = {0, 0};
vector <ll> tmp;
for(ll mul = 1; it != st.begin();) {
it--;
mul *= x[*it];
tmp.pb(*it);
if(mul > 1e9) break;
}
reverse(tmp.begin(), tmp.end());
for(ll i = 0, mul = 1; i < tmp.size() - 1; i++) {
ll max_y = queryy(1, 1, n, tmp[i], tmp[i + 1] - 1);
maxi = max(maxi, {mul * max_y, tmp[i]});
mul *= x[tmp[i + 1]];
}
auto nuit = st.upper_bound(maxi.se);
return (query(1, 1, n, 1, maxi.se) * queryy(1, 1, n, maxi.se, *nuit - 1)) % mod;
}
// static char _buffer[1024];
// static int _currentChar = 0;
// static int _charsNumber = 0;
// static FILE *_inputFile, *_outputFile;
// static inline int _read() {
// if (_charsNumber < 0) {
// exit(1);
// }
// if (!_charsNumber || _currentChar == _charsNumber) {
// _charsNumber = (int)fread(_buffer, sizeof(_buffer[0]), sizeof(_buffer), _inputFile);
// _currentChar = 0;
// }
// if (_charsNumber <= 0) {
// return -1;
// }
// return _buffer[_currentChar++];
// }
// static inline int _readInt() {
// int c, x, s;
// c = _read();
// while (c <= 32) c = _read();
// x = 0;
// s = 1;
// if (c == '-') {
// s = -1;
// c = _read();
// }
// while (c > 32) {
// x *= 10;
// x += c - '0';
// c = _read();
// }
// if (s < 0) x = -x;
// return x;
// }
// int main() {
// // _inputFile = fopen("horses.in", "rb");
// // _outputFile = fopen("horses.out", "w");
// int N; cin >> N;
// int *X = (int*)malloc(sizeof(int)*(unsigned int)N);
// int *Y = (int*)malloc(sizeof(int)*(unsigned int)N);
// for (int i = 0; i < N; i++) {
// cin >> X[i];
// }
// for (int i = 0; i < N; i++) {
// cin >> Y[i];
// }
// // fprintf(_outputFile,"%d\n",init(N,X,Y));
// cout << init(N, X, Y) << "\n";
// int M; cin >> M;
// for (int i = 0; i < M; i++) {
// int type; cin >> type;
// int pos; cin >> pos;
// int val; cin >> val;
// if (type == 1) {
// cout << updateX(pos, val) << "\n";
// } else if (type == 2) {
// cout << updateY(pos, val) << "\n";
// }
// }
// return 0;
// }
// int main() {
// // _inputFile = fopen("horses.in", "rb");
// // _outputFile = fopen("horses.out", "w");
// int N; cin >> N;
// int *X = (int*)malloc(sizeof(int)*(unsigned int)N);
// int *Y = (int*)malloc(sizeof(int)*(unsigned int)N);
// for (int i = 0; i < N; i++) {
// cin >> X[i];
// }
// for (int i = 0; i < N; i++) {
// cin >> Y[i];
// }
// // fprintf(_outputFile,"%d\n",init(N,X,Y));
// int M; cin >> M;
// for (int i = 0; i < 7; i++) {
// int type; cin >> type;
// int pos; cin >> pos;
// int val; cin >> val;
// if (type == 1) {
// X[pos] = val;
// } else if (type == 2) {
// Y[pos] = val;
// }
// }
// cout << init(N, X, Y) << "\n";
// return 0;
// }
Compilation message
horses.cpp: In function 'void upd(long long int, long long int, long long int, long long int, long long int)':
horses.cpp:25:33: warning: declaration of 'x' shadows a global declaration [-Wshadow]
25 | void upd(ll idx, ll l, ll r, ll x, ll v) {
| ^
horses.cpp:15:7: note: shadowed declaration is here
15 | ll n, x[500005], y[500005];
| ^
horses.cpp: In function 'long long int query(long long int, long long int, long long int, long long int, long long int)':
horses.cpp:36:39: warning: declaration of 'y' shadows a global declaration [-Wshadow]
36 | ll query(ll idx, ll l, ll r, ll x, ll y) {
| ^
horses.cpp:15:18: note: shadowed declaration is here
15 | ll n, x[500005], y[500005];
| ^
horses.cpp:36:33: warning: declaration of 'x' shadows a global declaration [-Wshadow]
36 | ll query(ll idx, ll l, ll r, ll x, ll y) {
| ^
horses.cpp:15:7: note: shadowed declaration is here
15 | ll n, x[500005], y[500005];
| ^
horses.cpp: In function 'void updy(long long int, long long int, long long int, long long int, long long int)':
horses.cpp:45:34: warning: declaration of 'x' shadows a global declaration [-Wshadow]
45 | void updy(ll idx, ll l, ll r, ll x, ll v) {
| ^
horses.cpp:15:7: note: shadowed declaration is here
15 | ll n, x[500005], y[500005];
| ^
horses.cpp: In function 'long long int queryy(long long int, long long int, long long int, long long int, long long int)':
horses.cpp:56:40: warning: declaration of 'y' shadows a global declaration [-Wshadow]
56 | ll queryy(ll idx, ll l, ll r, ll x, ll y) {
| ^
horses.cpp:15:18: note: shadowed declaration is here
15 | ll n, x[500005], y[500005];
| ^
horses.cpp:56:34: warning: declaration of 'x' shadows a global declaration [-Wshadow]
56 | ll queryy(ll idx, ll l, ll r, ll x, ll y) {
| ^
horses.cpp:15:7: note: shadowed declaration is here
15 | ll n, x[500005], y[500005];
| ^
horses.cpp: In function 'int init(int, int*, int*)':
horses.cpp:87:12: warning: conversion from 'long long int' to 'double' may change value [-Wconversion]
87 | if(mul > 1e9) break;
| ^~~
horses.cpp:90:30: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
90 | for(ll i = 0, mul = 1; i < tmp.size() - 1; i++) {
| ~~^~~~~~~~~~~~~~~~
horses.cpp:96:79: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
96 | return (query(1, 1, n, 1, maxi.se) * queryy(1, 1, n, maxi.se, *nuit - 1)) % mod;
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
horses.cpp: In function 'int updateX(int, int)':
horses.cpp:112:12: warning: conversion from 'long long int' to 'double' may change value [-Wconversion]
112 | if(mul > 1e9) break;
| ^~~
horses.cpp:115:30: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
115 | for(ll i = 0, mul = 1; i < tmp.size() - 1; i++) {
| ~~^~~~~~~~~~~~~~~~
horses.cpp:121:79: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
121 | return (query(1, 1, n, 1, maxi.se) * queryy(1, 1, n, maxi.se, *nuit - 1)) % mod;
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
horses.cpp: In function 'int updateY(int, int)':
horses.cpp:135:12: warning: conversion from 'long long int' to 'double' may change value [-Wconversion]
135 | if(mul > 1e9) break;
| ^~~
horses.cpp:138:30: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
138 | for(ll i = 0, mul = 1; i < tmp.size() - 1; i++) {
| ~~^~~~~~~~~~~~~~~~
horses.cpp:144:79: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
144 | return (query(1, 1, n, 1, maxi.se) * queryy(1, 1, n, maxi.se, *nuit - 1)) % mod;
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
312 KB |
Output is correct |
2 |
Correct |
1 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 |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
312 KB |
Output is correct |
10 |
Correct |
1 ms |
316 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 |
1 ms |
316 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
312 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
312 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 |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
312 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 |
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 |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
316 KB |
Output is correct |
23 |
Correct |
2 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
424 KB |
Output is correct |
25 |
Correct |
2 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
448 KB |
Output is correct |
27 |
Correct |
4 ms |
324 KB |
Output is correct |
28 |
Correct |
3 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
2 ms |
468 KB |
Output is correct |
31 |
Correct |
3 ms |
340 KB |
Output is correct |
32 |
Correct |
4 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
806 ms |
56664 KB |
Output is correct |
2 |
Correct |
500 ms |
65528 KB |
Output is correct |
3 |
Correct |
577 ms |
56704 KB |
Output is correct |
4 |
Correct |
592 ms |
60604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
312 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 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 |
0 ms |
316 KB |
Output is correct |
14 |
Correct |
1 ms |
360 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 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 |
1 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 |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
420 KB |
Output is correct |
25 |
Correct |
3 ms |
480 KB |
Output is correct |
26 |
Correct |
1 ms |
476 KB |
Output is correct |
27 |
Correct |
4 ms |
340 KB |
Output is correct |
28 |
Correct |
2 ms |
460 KB |
Output is correct |
29 |
Correct |
2 ms |
340 KB |
Output is correct |
30 |
Correct |
2 ms |
460 KB |
Output is correct |
31 |
Correct |
3 ms |
412 KB |
Output is correct |
32 |
Correct |
4 ms |
340 KB |
Output is correct |
33 |
Correct |
204 ms |
32612 KB |
Output is correct |
34 |
Correct |
195 ms |
32568 KB |
Output is correct |
35 |
Correct |
345 ms |
62900 KB |
Output is correct |
36 |
Correct |
345 ms |
62892 KB |
Output is correct |
37 |
Correct |
269 ms |
30796 KB |
Output is correct |
38 |
Correct |
294 ms |
43536 KB |
Output is correct |
39 |
Correct |
186 ms |
30612 KB |
Output is correct |
40 |
Correct |
330 ms |
57936 KB |
Output is correct |
41 |
Correct |
214 ms |
30524 KB |
Output is correct |
42 |
Correct |
237 ms |
30580 KB |
Output is correct |
43 |
Correct |
300 ms |
58356 KB |
Output is correct |
44 |
Correct |
310 ms |
58336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
316 KB |
Output is correct |
6 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
312 KB |
Output is correct |
11 |
Correct |
1 ms |
316 KB |
Output is correct |
12 |
Correct |
1 ms |
312 KB |
Output is correct |
13 |
Correct |
1 ms |
312 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 |
320 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 |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
328 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
2 ms |
468 KB |
Output is correct |
26 |
Correct |
2 ms |
468 KB |
Output is correct |
27 |
Correct |
4 ms |
340 KB |
Output is correct |
28 |
Correct |
2 ms |
468 KB |
Output is correct |
29 |
Correct |
2 ms |
316 KB |
Output is correct |
30 |
Correct |
2 ms |
464 KB |
Output is correct |
31 |
Correct |
3 ms |
340 KB |
Output is correct |
32 |
Correct |
5 ms |
328 KB |
Output is correct |
33 |
Correct |
782 ms |
56848 KB |
Output is correct |
34 |
Correct |
481 ms |
65544 KB |
Output is correct |
35 |
Correct |
487 ms |
56740 KB |
Output is correct |
36 |
Correct |
565 ms |
60560 KB |
Output is correct |
37 |
Correct |
206 ms |
32560 KB |
Output is correct |
38 |
Correct |
213 ms |
32588 KB |
Output is correct |
39 |
Correct |
375 ms |
62896 KB |
Output is correct |
40 |
Correct |
335 ms |
62852 KB |
Output is correct |
41 |
Correct |
244 ms |
30780 KB |
Output is correct |
42 |
Correct |
276 ms |
43524 KB |
Output is correct |
43 |
Correct |
191 ms |
30444 KB |
Output is correct |
44 |
Correct |
334 ms |
57952 KB |
Output is correct |
45 |
Correct |
203 ms |
30528 KB |
Output is correct |
46 |
Correct |
216 ms |
30596 KB |
Output is correct |
47 |
Correct |
294 ms |
58356 KB |
Output is correct |
48 |
Correct |
326 ms |
58316 KB |
Output is correct |
49 |
Correct |
326 ms |
35556 KB |
Output is correct |
50 |
Correct |
309 ms |
35592 KB |
Output is correct |
51 |
Correct |
498 ms |
64748 KB |
Output is correct |
52 |
Correct |
423 ms |
64276 KB |
Output is correct |
53 |
Correct |
836 ms |
33868 KB |
Output is correct |
54 |
Correct |
487 ms |
47436 KB |
Output is correct |
55 |
Correct |
341 ms |
31632 KB |
Output is correct |
56 |
Correct |
433 ms |
59828 KB |
Output is correct |
57 |
Correct |
494 ms |
32208 KB |
Output is correct |
58 |
Correct |
674 ms |
32764 KB |
Output is correct |
59 |
Correct |
322 ms |
58328 KB |
Output is correct |