Submission #222259

# Submission time Handle Problem Language Result Execution time Memory
222259 2020-04-12T19:00:05 Z mode149256 Horses (IOI15_horses) C++14
100 / 100
646 ms 143048 KB
/*input

*/
#include "horses.h"
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;

typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef vector<pi> vpi;
typedef vector<vpi> vpii;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
typedef vector<pd> vpd;
typedef vector<bool> vb;
typedef vector<vb> vbb;
typedef std::string str;
typedef std::vector<str> vs;

#define x first
#define y second
#define debug(...) cout<<"["<<#__VA_ARGS__<<": "<<__VA_ARGS__<<"]\n"

const ll MOD = 1000000007;
const ll INF = std::numeric_limits<ll>::max();
const int MX = 100101;
const ld PI = 3.14159265358979323846264338327950288419716939937510582097494L;

template<typename T>
pair<T, T> operator+(const pair<T, T> &a, const pair<T, T> &b) { return pair<T, T>(a.x + b.x, a.y + b.y); }
template<typename T>
pair<T, T> operator-(const pair<T, T> &a, const pair<T, T> &b) { return pair<T, T>(a.x - b.x, a.y - b.y); }
template<typename T>
T operator*(const pair<T, T> &a, const pair<T, T> &b) { return (a.x * b.x + a.y * b.y); }
template<typename T>
T operator^(const pair<T, T> &a, const pair<T, T> &b) { return (a.x * b.y - a.y * b.x); }

template<typename T>
void print(vector<T> vec, string name = "") {
	cout << name;
	for (auto u : vec)
		cout << u << ' ';
	cout << '\n';
}

vl x, y;
int n;
struct maxNode {
	int l, r;
	pl did;
	maxNode *left = nullptr;
	maxNode *right = nullptr;

	maxNode(int a, int b): l(a), r(b) {
		if (l != r) {
			left = new maxNode(l, (l + r) / 2);
			right = new maxNode((l + r) / 2 + 1, r);
			did = max(left->did, right->did);
		} else
			did = {y[l], l};
	}

	void upd(int pos, int val) {
		if (l == r) {
			did.x = val;
			return;
		} else if (pos <= (l + r) / 2)
			left->upd(pos, val);
		else
			right->upd(pos, val);

		did = max(left->did, right->did);
	}

	pl gauk(int a, int b) {
		if (r < a or b < l) return pi(-MOD, -MOD);
		else if (a <= l and r <= b) return did;
		else
			return max(left->gauk(a, b), right->gauk(a, b));
	}
};

struct prodNode {
	int l, r;
	ll prod;
	prodNode *left = nullptr;
	prodNode *right = nullptr;

	prodNode(int a, int b): l(a), r(b) {
		if (l != r) {
			left = new prodNode(l, (l + r) / 2);
			right = new prodNode((l + r) / 2 + 1, r);
			prod = (left->prod * right->prod) % MOD;
		} else
			prod = x[l];
	}

	void upd(int pos, ll val) {
		if (l == r) {
			prod = val;
			return;
		} else if (pos <= (l + r) / 2)
			left->upd(pos, val);
		else
			right->upd(pos, val);

		prod = (left->prod * right->prod) % MOD;
	}

	ll gauk(int a, int b) {
		if (r < a or b < l) return 1;
		else if (a <= l and r <= b) return prod;
		else
			return (left->gauk(a, b) * right->gauk(a, b)) % MOD;
	}
};

maxNode *yg;
prodNode *xs;
set<int, greater<int> > indes;

int compute() {
	if (indes.empty()) {
		pi ans = yg->did;
		return ans.x;
	}
	int last = n;

	ll currProd = 1;
	int currBest = -1;

	int cn = 0;
	for (auto it = indes.begin(); it != indes.end() and cn < 66; it++, cn++) {
		pi dabY = yg->gauk(*it, last - 1);
		if (currProd * y[currBest] > MOD) break;

		// printf("ind = %d, dabY = %d %d\n", *it, dabY.x, dabY.y);
		if (currBest == -1 or dabY.x > currProd * y[currBest]) {
			currBest = dabY.y;
			currProd = x[*it];
		} else {
			if (currProd * y[currBest] > MOD) break;

			currProd *= x[*it];
			if (currProd * y[currBest] > MOD) break;
		}
		// printf("currBest = %lld, currProd  = %lld\n", currBest, currProd);

		last = *it;
	}
	if (last != 0 and currProd * y[currBest] <= MOD) {
		pi dabY = yg->gauk(0, last - 1);
		if (dabY.x > currProd * y[currBest]) {
			currBest = dabY.y;
			currProd = x[dabY.y];
		}
	}

	return (int)((xs->gauk(0, currBest) * y[currBest]) % MOD);
}

int init(int N, int X[], int Y[]) {
	n = N;
	for (int i = 0; i < N; ++i)
	{
		x.emplace_back(X[i]);
		y.emplace_back(Y[i]);
		if (X[i] != 1) indes.insert(i);
	}
	yg = new maxNode(0, N - 1);
	xs = new prodNode(0, N - 1);

	return compute();
}


int updateX(int pos, int val) {
	if (val != 1) indes.insert(pos);
	else if (val == 1) indes.erase(pos);
	xs->upd(pos, val);
	x[pos] = val;
	return compute();
}

int updateY(int pos, int val) {
	yg->upd(pos, val);
	y[pos] = val;
	return compute();
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 256 KB Output is correct
2 Correct 4 ms 128 KB Output is correct
3 Correct 4 ms 384 KB Output is correct
4 Correct 5 ms 256 KB Output is correct
5 Correct 5 ms 256 KB Output is correct
6 Correct 4 ms 256 KB Output is correct
7 Correct 5 ms 256 KB Output is correct
8 Correct 4 ms 256 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 4 ms 384 KB Output is correct
11 Correct 4 ms 256 KB Output is correct
12 Correct 4 ms 256 KB Output is correct
13 Correct 4 ms 256 KB Output is correct
14 Correct 4 ms 256 KB Output is correct
15 Correct 5 ms 256 KB Output is correct
16 Correct 4 ms 256 KB Output is correct
17 Correct 5 ms 256 KB Output is correct
18 Correct 4 ms 256 KB Output is correct
19 Correct 5 ms 256 KB Output is correct
20 Correct 4 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 256 KB Output is correct
2 Correct 4 ms 256 KB Output is correct
3 Correct 5 ms 256 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 4 ms 256 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 256 KB Output is correct
8 Correct 5 ms 256 KB Output is correct
9 Correct 4 ms 384 KB Output is correct
10 Correct 4 ms 256 KB Output is correct
11 Correct 4 ms 256 KB Output is correct
12 Correct 5 ms 256 KB Output is correct
13 Correct 5 ms 256 KB Output is correct
14 Correct 4 ms 256 KB Output is correct
15 Correct 4 ms 256 KB Output is correct
16 Correct 4 ms 256 KB Output is correct
17 Correct 4 ms 256 KB Output is correct
18 Correct 4 ms 384 KB Output is correct
19 Correct 4 ms 384 KB Output is correct
20 Correct 4 ms 256 KB Output is correct
21 Correct 5 ms 256 KB Output is correct
22 Correct 4 ms 256 KB Output is correct
23 Correct 5 ms 640 KB Output is correct
24 Correct 7 ms 640 KB Output is correct
25 Correct 6 ms 640 KB Output is correct
26 Correct 5 ms 640 KB Output is correct
27 Correct 7 ms 640 KB Output is correct
28 Correct 6 ms 640 KB Output is correct
29 Correct 6 ms 512 KB Output is correct
30 Correct 5 ms 640 KB Output is correct
31 Correct 8 ms 640 KB Output is correct
32 Correct 7 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 522 ms 130760 KB Output is correct
2 Correct 455 ms 143048 KB Output is correct
3 Correct 468 ms 134308 KB Output is correct
4 Correct 495 ms 138316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 256 KB Output is correct
3 Correct 5 ms 256 KB Output is correct
4 Correct 5 ms 256 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 4 ms 256 KB Output is correct
7 Correct 4 ms 256 KB Output is correct
8 Correct 5 ms 256 KB Output is correct
9 Correct 5 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 4 ms 256 KB Output is correct
12 Correct 4 ms 256 KB Output is correct
13 Correct 5 ms 256 KB Output is correct
14 Correct 5 ms 256 KB Output is correct
15 Correct 4 ms 256 KB Output is correct
16 Correct 4 ms 256 KB Output is correct
17 Correct 4 ms 256 KB Output is correct
18 Correct 5 ms 256 KB Output is correct
19 Correct 5 ms 256 KB Output is correct
20 Correct 4 ms 256 KB Output is correct
21 Correct 4 ms 256 KB Output is correct
22 Correct 5 ms 384 KB Output is correct
23 Correct 5 ms 640 KB Output is correct
24 Correct 5 ms 640 KB Output is correct
25 Correct 6 ms 640 KB Output is correct
26 Correct 5 ms 640 KB Output is correct
27 Correct 8 ms 640 KB Output is correct
28 Correct 6 ms 640 KB Output is correct
29 Correct 5 ms 512 KB Output is correct
30 Correct 6 ms 640 KB Output is correct
31 Correct 7 ms 512 KB Output is correct
32 Correct 7 ms 512 KB Output is correct
33 Correct 149 ms 110200 KB Output is correct
34 Correct 147 ms 110160 KB Output is correct
35 Correct 291 ms 140744 KB Output is correct
36 Correct 273 ms 140364 KB Output is correct
37 Correct 178 ms 108368 KB Output is correct
38 Correct 201 ms 121288 KB Output is correct
39 Correct 128 ms 108104 KB Output is correct
40 Correct 252 ms 135500 KB Output is correct
41 Correct 141 ms 108112 KB Output is correct
42 Correct 162 ms 108368 KB Output is correct
43 Correct 250 ms 135884 KB Output is correct
44 Correct 247 ms 135880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 512 KB Output is correct
2 Correct 4 ms 256 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 4 ms 256 KB Output is correct
5 Correct 4 ms 256 KB Output is correct
6 Correct 4 ms 256 KB Output is correct
7 Correct 4 ms 256 KB Output is correct
8 Correct 4 ms 256 KB Output is correct
9 Correct 4 ms 256 KB Output is correct
10 Correct 4 ms 256 KB Output is correct
11 Correct 4 ms 256 KB Output is correct
12 Correct 5 ms 256 KB Output is correct
13 Correct 5 ms 256 KB Output is correct
14 Correct 4 ms 256 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 5 ms 256 KB Output is correct
17 Correct 4 ms 384 KB Output is correct
18 Correct 5 ms 256 KB Output is correct
19 Correct 4 ms 256 KB Output is correct
20 Correct 4 ms 256 KB Output is correct
21 Correct 4 ms 256 KB Output is correct
22 Correct 5 ms 256 KB Output is correct
23 Correct 5 ms 640 KB Output is correct
24 Correct 6 ms 640 KB Output is correct
25 Correct 6 ms 640 KB Output is correct
26 Correct 5 ms 640 KB Output is correct
27 Correct 7 ms 512 KB Output is correct
28 Correct 6 ms 640 KB Output is correct
29 Correct 5 ms 512 KB Output is correct
30 Correct 5 ms 640 KB Output is correct
31 Correct 6 ms 512 KB Output is correct
32 Correct 7 ms 512 KB Output is correct
33 Correct 537 ms 134344 KB Output is correct
34 Correct 478 ms 143044 KB Output is correct
35 Correct 452 ms 134328 KB Output is correct
36 Correct 476 ms 138060 KB Output is correct
37 Correct 149 ms 110200 KB Output is correct
38 Correct 147 ms 110160 KB Output is correct
39 Correct 281 ms 140484 KB Output is correct
40 Correct 274 ms 140360 KB Output is correct
41 Correct 181 ms 108368 KB Output is correct
42 Correct 194 ms 121032 KB Output is correct
43 Correct 127 ms 108136 KB Output is correct
44 Correct 251 ms 135496 KB Output is correct
45 Correct 141 ms 108112 KB Output is correct
46 Correct 156 ms 108368 KB Output is correct
47 Correct 249 ms 135884 KB Output is correct
48 Correct 251 ms 135884 KB Output is correct
49 Correct 309 ms 113272 KB Output is correct
50 Correct 295 ms 113104 KB Output is correct
51 Correct 405 ms 142428 KB Output is correct
52 Correct 319 ms 141772 KB Output is correct
53 Correct 646 ms 111632 KB Output is correct
54 Correct 330 ms 125184 KB Output is correct
55 Correct 212 ms 109120 KB Output is correct
56 Correct 339 ms 137548 KB Output is correct
57 Correct 369 ms 109776 KB Output is correct
58 Correct 475 ms 110400 KB Output is correct
59 Correct 248 ms 135880 KB Output is correct