Submission #707005

# Submission time Handle Problem Language Result Execution time Memory
707005 2023-03-08T09:09:33 Z marvinthang Horses (IOI15_horses) C++17
100 / 100
623 ms 51204 KB
/*************************************
*    author: marvinthang             *
*    created: 08.03.2023 15:25:43    *
*************************************/

#include "horses.h"
#include <bits/stdc++.h>

using namespace std;

#define                  fi  first
#define                  se  second
#define                left  ___left
#define               right  ___right
#define                TIME  (1.0 * clock() / CLOCKS_PER_SEC)
#define             MASK(i)  (1LL << (i))
#define           BIT(x, i)  ((x) >> (i) & 1)
#define  __builtin_popcount  __builtin_popcountll
#define              ALL(v)  (v).begin(), (v).end()
#define           REP(i, n)  for (int i = 0, _n = (n); i < _n; ++i)
#define          REPD(i, n)  for (int i = (n); i--; )
#define        FOR(i, a, b)  for (int i = (a), _b = (b); i < _b; ++i) 
#define       FORD(i, b, a)  for (int i = (b), _a = (a); --i >= _a; ) 
#define       FORE(i, a, b)  for (int i = (a), _b = (b); i <= _b; ++i) 
#define      FORDE(i, b, a)  for (int i = (b), _a = (a); i >= _a; --i) 
#define        scan_op(...)  istream & operator >> (istream &in, __VA_ARGS__ &u)
#define       print_op(...)  ostream & operator << (ostream &out, const __VA_ARGS__ &u)

template <class T> scan_op(vector <T>)  { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; }
template <class U, class V> scan_op(pair <U, V>)  { return in >> u.fi >> u.se; }
template <class U, class V> print_op(pair <U, V>)  { return out << '(' << u.first << ", " << u.second << ')'; }
template <class Con, class = decltype(begin(declval<Con>()))> typename enable_if <!is_same<Con, string>::value, ostream&>::type operator << (ostream &out, const Con &con) { out << '{'; for (__typeof(con.begin()) it = con.begin(); it != con.end(); ++it) out << (it == con.begin() ? "" : ", ") << *it; return out << '}'; }
template <size_t i, class T> ostream & print_tuple_utils(ostream &out, const T &tup) { if constexpr(i == tuple_size<T>::value) return out << ")";  else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); }
template <class ...U> print_op(tuple<U...>) { return print_tuple_utils<0, tuple<U...>>(out, u); }
template <class T> T invGeneral(T a, T b) {
    a %= b;
    if (!a) return b == 1 ? 0 : -1;
    T x = invGeneral(b, a);
    return x == -1 ? -1 : ((1 - 1LL * b * x) / a + b) % b;
}

// end of template

const int MOD = 1e9 + 7;
const int MAX = 5e5 + 5;

int N, X[MAX], Y[MAX], bit[MAX], st[MAX << 2];
set <int, greater <int>> pos;

void build(int i, int l, int r) {
	if (r - l == 1) {
		st[i] = Y[l];
		return;
	}
	int m = l + r >> 1;
	build(i << 1, l, m);
	build(i << 1 | 1, m, r);
	st[i] = max(st[i << 1], st[i << 1 | 1]);
}

void update(int i, int l, int r, int p) {
	if (r - l == 1) {
		st[i] = Y[l];
		return;
	}
	int m = l + r >> 1;
	if (p < m) update(i << 1, l, m, p);
	else update(i << 1 | 1, m, r, p);
	st[i] = max(st[i << 1], st[i << 1 | 1]);
}

int getMax(int i, int l, int r, int u, int v) {
	if (l >= v || r <= u) return 0;
	if (u <= l && r <= v) return st[i];
	int m = l + r >> 1;
	return max(getMax(i << 1, l, m, u, v), getMax(i << 1 | 1, m, r, u, v));
}

void update(int i, int v) {
	for (++i; i <= N; i += i & -i) bit[i] = 1LL * bit[i] * v % MOD;
}

int getMul(int i) {
	int res = 1;
	for (++i; i > 0; i &= i - 1) res = 1LL * res * bit[i] % MOD;
	return res;
}

int solve(void) {
	int res = -1;
	long long cb = 1;
	int r = N, ma = 0, mb = 0;
	for (int l: pos) {
		int ca = getMax(1, 0, N, l, r);
		// ma / ca * cb / mb > 1
		// ma * cb > ca * mb
		if (1LL * ca * mb >= 1LL * ma * cb) {
			res = 1LL * getMul(l) * ca % MOD;
			ma = ca; mb = cb;
		}
		// cout << l << ' ' << r << ' ' << ca << ' ' << cb << ' ' << res << '\n';
		cb *= X[l];
		if (cb > MOD) break;
		r = l;
	}
	// cout << "-------\n";
	return res;
}

int init(int n, int x[], int y[]) {
	N = n;
	fill(bit, bit + N + 1, 1);
	REP(i, N) {
		X[i] = x[i]; Y[i] = y[i];
		if (!i || X[i] != 1) {
			update(i, X[i]);
			pos.insert(i);
		}
	}
	build(1, 0, N);
	return solve();
}

int updateX(int i, int val) {	
	if (X[i] != 1) {
		update(i, invGeneral(X[i], MOD));
		pos.erase(i);
	}
	X[i] = val;
	if (!i || X[i] != 1) {
		update(i, X[i]);
		pos.insert(i);
	}
	return solve();
}

int updateY(int i, int val) {
	Y[i] = val;
	update(1, 0, N, i);
	return solve();
}

#ifdef LOCAL
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; N = _readInt();

	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++) {
		X[i] = _readInt();
	}

	for (int i = 0; i < N; i++) {
		Y[i] = _readInt();
	}	

	fprintf(_outputFile,"%d\n",init(N,X,Y));

	int M; M = _readInt();

	for (int i = 0; i < M; i++) {
		int type; type = _readInt();
		int pos; pos = _readInt();
		int val; val = _readInt(); 

		if (type == 1) {
			fprintf(_outputFile,"%d\n",updateX(pos,val));
		} else if (type == 2) {
			fprintf(_outputFile,"%d\n",updateY(pos,val));
		}
	}

	return 0;
}
#endif

Compilation message

horses.cpp: In function 'void build(int, int, int)':
horses.cpp:55:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   55 |  int m = l + r >> 1;
      |          ~~^~~
horses.cpp: In function 'void update(int, int, int, int)':
horses.cpp:66:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   66 |  int m = l + r >> 1;
      |          ~~^~~
horses.cpp: In function 'int getMax(int, int, int, int, int)':
horses.cpp:75:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   75 |  int m = l + r >> 1;
      |          ~~^~~
horses.cpp: In function 'void update(int, int)':
horses.cpp:80:59: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
   80 |  for (++i; i <= N; i += i & -i) bit[i] = 1LL * bit[i] * v % MOD;
      |                                          ~~~~~~~~~~~~~~~~~^~~~~
horses.cpp: In function 'int getMul(int)':
horses.cpp:85:56: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
   85 |  for (++i; i > 0; i &= i - 1) res = 1LL * res * bit[i] % MOD;
      |                                     ~~~~~~~~~~~~~~~~~~~^~~~~
horses.cpp: In function 'int solve()':
horses.cpp:98:31: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
   98 |    res = 1LL * getMul(l) * ca % MOD;
      |          ~~~~~~~~~~~~~~~~~~~~~^~~~~
horses.cpp:99:18: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
   99 |    ma = ca; mb = cb;
      |                  ^~
horses.cpp: In instantiation of 'T invGeneral(T, T) [with T = int]':
horses.cpp:126:33:   required from here
horses.cpp:39:20: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
   39 |     return x == -1 ? -1 : ((1 - 1LL * b * x) / a + b) % b;
      |            ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 312 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 304 KB Output is correct
8 Correct 0 ms 212 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 212 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 308 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 212 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 304 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 304 KB Output is correct
5 Correct 0 ms 312 KB Output is correct
6 Correct 1 ms 308 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 304 KB Output is correct
9 Correct 1 ms 304 KB Output is correct
10 Correct 1 ms 308 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 308 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 288 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 308 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 2 ms 432 KB Output is correct
26 Correct 1 ms 316 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 2 ms 324 KB Output is correct
31 Correct 2 ms 340 KB Output is correct
32 Correct 3 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 590 ms 38652 KB Output is correct
2 Correct 302 ms 51124 KB Output is correct
3 Correct 308 ms 42384 KB Output is correct
4 Correct 384 ms 46340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 304 KB Output is correct
3 Correct 1 ms 308 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 308 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 304 KB Output is correct
8 Correct 1 ms 308 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 312 KB Output is correct
11 Correct 1 ms 308 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 308 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 304 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 312 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 320 KB Output is correct
24 Correct 1 ms 324 KB Output is correct
25 Correct 2 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 3 ms 392 KB Output is correct
28 Correct 2 ms 308 KB Output is correct
29 Correct 1 ms 320 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 2 ms 340 KB Output is correct
32 Correct 3 ms 340 KB Output is correct
33 Correct 37 ms 18328 KB Output is correct
34 Correct 33 ms 18276 KB Output is correct
35 Correct 205 ms 48508 KB Output is correct
36 Correct 180 ms 48720 KB Output is correct
37 Correct 79 ms 16420 KB Output is correct
38 Correct 107 ms 29260 KB Output is correct
39 Correct 27 ms 16204 KB Output is correct
40 Correct 164 ms 43672 KB Output is correct
41 Correct 52 ms 16264 KB Output is correct
42 Correct 66 ms 16536 KB Output is correct
43 Correct 174 ms 43980 KB Output is correct
44 Correct 151 ms 44052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 308 KB Output is correct
3 Correct 0 ms 308 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 304 KB Output is correct
8 Correct 0 ms 308 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 248 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 1 ms 316 KB Output is correct
24 Correct 1 ms 320 KB Output is correct
25 Correct 2 ms 452 KB Output is correct
26 Correct 2 ms 316 KB Output is correct
27 Correct 4 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 2 ms 320 KB Output is correct
31 Correct 2 ms 340 KB Output is correct
32 Correct 3 ms 340 KB Output is correct
33 Correct 623 ms 42448 KB Output is correct
34 Correct 301 ms 51204 KB Output is correct
35 Correct 310 ms 42360 KB Output is correct
36 Correct 378 ms 46296 KB Output is correct
37 Correct 37 ms 18308 KB Output is correct
38 Correct 34 ms 18244 KB Output is correct
39 Correct 175 ms 48544 KB Output is correct
40 Correct 180 ms 48584 KB Output is correct
41 Correct 78 ms 16456 KB Output is correct
42 Correct 94 ms 29160 KB Output is correct
43 Correct 26 ms 16256 KB Output is correct
44 Correct 157 ms 43684 KB Output is correct
45 Correct 49 ms 16332 KB Output is correct
46 Correct 79 ms 16380 KB Output is correct
47 Correct 153 ms 43944 KB Output is correct
48 Correct 158 ms 44048 KB Output is correct
49 Correct 126 ms 21236 KB Output is correct
50 Correct 101 ms 21356 KB Output is correct
51 Correct 298 ms 50408 KB Output is correct
52 Correct 227 ms 50012 KB Output is correct
53 Correct 555 ms 19584 KB Output is correct
54 Correct 223 ms 33188 KB Output is correct
55 Correct 99 ms 17336 KB Output is correct
56 Correct 261 ms 45596 KB Output is correct
57 Correct 326 ms 17896 KB Output is correct
58 Correct 478 ms 18432 KB Output is correct
59 Correct 170 ms 43980 KB Output is correct