Submission #222713

#TimeUsernameProblemLanguageResultExecution timeMemory
222713mode149256Horses (IOI15_horses)C++14
100 / 100
1037 ms158920 KiB
/*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'; } template<typename T, T (*combine)(T, T), bool lazyLoading = false> struct SegmentTree { T val; T lazy; int l, r; SegmentTree<T, combine, lazyLoading> *left = nullptr; SegmentTree<T, combine, lazyLoading> *right = nullptr; SegmentTree() { } SegmentTree(int a, int b, T defaultVal = T()) : l(a), r(b) { val = defaultVal; lazy = T(); if (!lazyLoading and l != r) { left = new SegmentTree<T, combine, lazyLoading>(l, (l + r) / 2); right = new SegmentTree<T, combine, lazyLoading>((l + r) / 2 + 1, r); upd(); } } SegmentTree(int a, int b, const vector<T> &defValues) : l(a), r(b) { lazy = T(); if (!lazyLoading and l != r) { left = new SegmentTree<T, combine, lazyLoading>(l, (l + r) / 2, defValues); right = new SegmentTree<T, combine, lazyLoading>((l + r) / 2 + 1, r, defValues); upd(); } else { val = defValues[l]; } } inline void push() { } inline void upd() { if (!lazyLoading) val = combine(left->val, right->val); else val = combine((left ? left->val : T()), (right ? right->val : T())); } inline void add_left() { if (!left) left = new SegmentTree<T, combine, lazyLoading>(l, (l + r) / 2);} inline void add_right() { if (!right) right = new SegmentTree<T, combine, lazyLoading>((l + r) / 2 + 1, r);} void update(int a, int b, T newVal) { push(); if (r < a or b < l) return; else if (a <= l and r <= b) { val = newVal; push(); return; } else { if (lazyLoading and !left) add_left(); left->update(a, b, newVal); if (lazyLoading and !right) add_right(); right->update(a, b, newVal); } upd(); } T get(int a, int b, T defaultVal = T()) { push(); if (r < a or b < l) return defaultVal; else if (a <= l and r <= b) { return val; } else { if (lazyLoading and !left) add_left(); if (lazyLoading and !right) add_right(); return combine( left->get(a, b, defaultVal), right->get(a, b, defaultVal) ); } } }; vl x, y; int n; pl combineMax(pl a, pl b) { return max(a, b); } ll combineProd(ll a, ll b) { return (a * b) % MOD; } SegmentTree<pl, combineMax> *yg; SegmentTree<ll, combineProd> *xs; set<int, greater<int> > indes; int compute() { if (indes.empty()) { pi ans = yg->val; 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->get(*it, last - 1, pl(-MOD, -MOD)); if (currProd * y[currBest] > MOD) break; 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; } last = *it; } if (last != 0 and currProd * y[currBest] <= MOD) { pi dabY = yg->get(0, last - 1, pl(-MOD, -MOD)); if (dabY.x > currProd * y[currBest]) { currBest = dabY.y; currProd = x[dabY.y]; } } ll sandauga = xs->get(0, currBest, 1); return (int)((sandauga * 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 SegmentTree<pl, combineMax>(0, N - 1); xs = new SegmentTree<ll, combineProd>(0, N - 1, x); for (int i = 0; i < N; i++) yg->update(i, i, pl(y[i], i)); return compute(); } int updateX(int pos, int val) { if (val != 1) indes.insert(pos); else if (val == 1) indes.erase(pos); xs->update(pos, pos, val); x[pos] = val; return compute(); } int updateY(int pos, int val) { yg->update(pos, pos, pl(val, pos)); y[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...