Submission #1343894

#TimeUsernameProblemLanguageResultExecution timeMemory
1343894madamadam3Horses (IOI15_horses)C++20
54 / 100
1596 ms20872 KiB
#include "horses.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;

const ll MOD = 1e9+7;
const ll INF = 4e18;

#define bg(x) (x).begin()
#define en(x) (x).end()
#define all(x) (x).begin(), (x).end()
#define sz(x) (int((x).size()))
#define f first
#define s second
#define pb push_back
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define rev(i, a, b) for (int i = a; i >= b; i--)

template<class T> bool chmax(T& a, const T& b) {return a < b ? a = b, 1 : 0;}
template<class T> bool chmin(T& a, const T& b) {return b < a ? a = b, 1 : 0;}

#ifdef LOCAL
	#define dbg(x) cerr << #x << " = " << x << "\n"
#else
	#define dbg(x)
#endif

ll modexp(ll a, ll b) {
	if (b <= 0) return 1LL;
	if (b == 1) return a % MOD;

	ll r = modexp(a, b/2);
	r = (r*r) % MOD;
	if (b%2==1) r = (r*a)%MOD;
	return r%MOD;
}

ll inv(ll m) {
	return modexp(m, MOD-2);
}

struct Fenw {
	int n; vl BIT;
	Fenw(int n = 0) : n(n), BIT(n, 1) {}
	ll query(int r) {
		ll res = 1; for (; r >= 0; r &= r+1, r--) res = (res * BIT[r]) % MOD;
		return res;
	}

	ll point_query(int i) {
		ll a = query(i-1), b = query(i);
		return (inv(a) * b) % MOD;
	}

	void update(int i, ll delta) {
		for (; i < n; i |= i+1) BIT[i] = (BIT[i] * delta) % MOD;
	}

	void set(int i, ll v) {
		update(i, inv(point_query(i)));
		update(i, v % MOD);
	}
};

int n;
vl x, y;
Fenw fenwx, fenwy;

int compute() {
	ll ans = x[n-1] * y[n-1];
	bool didmod = ans >= MOD; 
	ans %= MOD;

	rev(i, n-2, 0) {
		if (!didmod && y[i] > ans) ans = y[i];
		ans *= x[i];

		didmod = didmod || (ans >= MOD);
		ans %= MOD;

		if (didmod) {
			ans = (fenwx.query(i-1) * ans) % MOD;
			break;
		}
	}
	return ans % MOD;
}

int init(int N, int X[], int Y[]) {
	n = N; x.assign(X, X+N); y.assign(Y, Y+N);
	fenwx = Fenw(n); fenwy = Fenw(n);
	for (int i = 0; i < n; i++) fenwx.set(i, x[i]), fenwy.set(i, y[i]);
	return compute();
}

int updateX(int pos, int val) {	
	x[pos] = val; fenwx.set(pos, val);
	return compute();
}

int updateY(int pos, int val) {
	y[pos] = val; fenwy.set(pos, val);
	return compute();
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...