Submission #1004976

# Submission time Handle Problem Language Result Execution time Memory
1004976 2024-06-22T05:32:34 Z pawned Horses (IOI15_horses) C++17
100 / 100
346 ms 57428 KB
#pragma GCC optimize("O1,O2,O3,Ofast,unroll-loops")

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

#define fi first
#define se second
#define pb push_back
typedef long long ll;
typedef pair<ll, ll> ii;
typedef vector<ll> vi;

#include "horses.h"

const ll MOD = 1e9 + 7;
const ll MAX = 1e9 + 5;
const ll MAX_SZ = 5e5 + 5;

struct Segtree {	// point update, range max query
	int sz;
	vi vals;
	void init(int N) {
		sz = 1;
		while (sz < N)
			sz *= 2;
		vals = vi(2 * sz, 0);
	}
	void upd(int pos, ll val, int id, int left, int right) {
		if (right - left == 1) {
			vals[id] = val;
			return;
		}
		int mid = (left + right) / 2;
		if (pos < mid)
			upd(pos, val, 2 * id + 1, left, mid);
		else
			upd(pos, val, 2 * id + 2, mid, right);
		vals[id] = max(vals[2 * id + 1], vals[2 * id + 2]);
	}
	void upd(int pos, ll val) {
		upd(pos, val, 0, 0, sz);
	}
	ll query(int qleft, int qright, int id, int left, int right) {
		if (qleft <= left && right <= qright)
			return vals[id];
		if (qright <= left || right <= qleft)
			return 0;
		int mid = (left + right) / 2;
		ll s1 = query(qleft, qright, 2 * id + 1, left, mid);
		ll s2 = query(qleft, qright, 2 * id + 2, mid, right);
		return max(s1, s2);
	}
	ll query(int qleft, int qright) {
		return query(qleft, qright, 0, 0, sz);
	}
};

ll norm(ll x) {
	x %= MOD;
	x += MOD;
	x %= MOD;
	return x;
}

ll binpow(ll x, ll y) {
	x = norm(x);
	ll res = 1;
	while (y > 0) {
		if (y % 2 == 1)
			res = norm(res * x);
		x = norm(x * x);
		y /= 2;
	}
	return res;
}

ll inv(ll x) {
	return binpow(x, MOD - 2);
}

int N;

vi X;
vi Y;

Segtree st;

set<int> gp;	// pos of X w/ val > 1

ll prodall = 1;	// product of all X_i

ll solve() {
	auto it = gp.end();
	ll currn = Y[N - 1];
	ll currd = 1;
	ll bestn = currn;
	ll bestd = currd;
	while (it != gp.begin()) {
		it--;
		if (currd > MAX)
			break;
		int p1 = *it;
		int p2 = N;
		if (it != (--gp.end())) {
			auto it2 = it;
			it2++;
			p2 = *it2;
		}
		currn = st.query(p1, p2);
//		cout<<"at "<<p1<<" "<<p2<<endl;
//		cout<<"currn, currd: "<<currn<<" "<<currd<<endl;
		if (currn * bestd > bestn * currd) {
			bestn = currn;
			bestd = currd;
		}
		currd *= X[*it];	// update denominator
	}
//	cout<<"bestn, bestd: "<<bestn<<" "<<bestd<<endl;
	ll ans = norm(prodall * norm(bestn * inv(bestd)));
	return ans;
}

int init(int n, int x[], int y[]) {
	N = n;
	X = vi(N); Y = vi(N);
	for (int i = 0; i < N; i++) {
		X[i] = x[i];
		Y[i] = y[i];
	}
	st.init(N);
	for (int i = 0; i < N; i++) {
		st.upd(i, Y[i]);
	}
/*	cout<<"st: ";
	for (int i = 0; i < st.sz * 2; i++) {
		cout<<st.vals[i]<<" ";
	}
	cout<<endl;*/
	for (int i = 0; i < N; i++) {
		if (X[i] != 1)
			gp.insert(i);
	}
	gp.insert(-1);
	for (int i = 0; i < N; i++) {
		prodall *= X[i];
		prodall = norm(prodall);
	}
	ll ans = solve();
	return (int)(ans);
}

int updateX(int pos, int val) {
	if (X[pos] != 1 && val == 1)
		gp.erase(pos);
	if (X[pos] == 1 && val != 1)
		gp.insert(pos);
	prodall = norm(prodall * inv(X[pos]));
	X[pos] = val;
	prodall = norm(prodall * X[pos]);
	ll ans = solve();
	return (int)(ans);
}

int updateY(int pos, int val) {
	Y[pos] = val;
	st.upd(pos, val);
	ll ans = solve();
	return (int)(ans);
}
# Verdict Execution time Memory Grader output
1 Correct 1 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 348 KB Output is correct
5 Correct 0 ms 436 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 344 KB Output is correct
13 Correct 0 ms 388 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
# Verdict Execution time Memory 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 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 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 344 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 432 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 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 1 ms 348 KB Output is correct
24 Correct 2 ms 348 KB Output is correct
25 Correct 1 ms 444 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 2 ms 344 KB Output is correct
28 Correct 2 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 2 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 346 ms 48452 KB Output is correct
2 Correct 233 ms 57428 KB Output is correct
3 Correct 213 ms 48392 KB Output is correct
4 Correct 238 ms 52304 KB Output is correct
# Verdict Execution time Memory 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 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 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 1 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 1 ms 504 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 2 ms 348 KB Output is correct
28 Correct 1 ms 344 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 61 ms 24324 KB Output is correct
34 Correct 55 ms 24400 KB Output is correct
35 Correct 172 ms 54736 KB Output is correct
36 Correct 151 ms 54612 KB Output is correct
37 Correct 75 ms 22580 KB Output is correct
38 Correct 102 ms 35412 KB Output is correct
39 Correct 54 ms 22352 KB Output is correct
40 Correct 154 ms 49744 KB Output is correct
41 Correct 60 ms 22440 KB Output is correct
42 Correct 67 ms 22360 KB Output is correct
43 Correct 133 ms 50044 KB Output is correct
44 Correct 126 ms 50204 KB Output is correct
# Verdict Execution time Memory 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 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 0 ms 416 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 344 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 2 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 2 ms 348 KB Output is correct
33 Correct 341 ms 48540 KB Output is correct
34 Correct 238 ms 57308 KB Output is correct
35 Correct 210 ms 48464 KB Output is correct
36 Correct 238 ms 52308 KB Output is correct
37 Correct 61 ms 24404 KB Output is correct
38 Correct 57 ms 24400 KB Output is correct
39 Correct 170 ms 54612 KB Output is correct
40 Correct 167 ms 54624 KB Output is correct
41 Correct 75 ms 22616 KB Output is correct
42 Correct 97 ms 35408 KB Output is correct
43 Correct 58 ms 22364 KB Output is correct
44 Correct 159 ms 49748 KB Output is correct
45 Correct 66 ms 22364 KB Output is correct
46 Correct 67 ms 22364 KB Output is correct
47 Correct 137 ms 50004 KB Output is correct
48 Correct 137 ms 50000 KB Output is correct
49 Correct 142 ms 27432 KB Output is correct
50 Correct 125 ms 27476 KB Output is correct
51 Correct 243 ms 56772 KB Output is correct
52 Correct 212 ms 56024 KB Output is correct
53 Correct 294 ms 25756 KB Output is correct
54 Correct 213 ms 39248 KB Output is correct
55 Correct 128 ms 23376 KB Output is correct
56 Correct 221 ms 51536 KB Output is correct
57 Correct 197 ms 24392 KB Output is correct
58 Correct 252 ms 24608 KB Output is correct
59 Correct 143 ms 50052 KB Output is correct