This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "horses.h"
#include <stdio.h>
#include <vector>
#include <queue>
#include <algorithm>
#include <iostream>
#include <string>
#include <bitset>
#include <map>
#include <set>
#include <tuple>
#include <string.h>
#include <math.h>
#include <random>
#include <functional>
#include <assert.h>
#include <math.h>
#define all(x) (x).begin(), (x).end()
#define xx first
#define yy second
#define MOD ((i64)1e9 + 7)
using namespace std;
template <typename T, typename Pr = less<T>>
using pq = priority_queue<T, vector<T>, Pr>;
using i64 = long long int;
using ii = pair<int, int>;
using ii64 = pair<i64, i64>;
constexpr int clog2(int n) { return ((n < 2) ? 1 : 1 + clog2(n / 2)); }
template <typename T>
class SegmentTree
{
public:
SegmentTree() = default;
template <typename M>
SegmentTree(const M &m) : merge(m) {}
void init(const vector<T> &raw_)
{
raw = raw_;
n = (int)raw.size();
int sz = (1 << (clog2(n) + 1));
data.resize(sz);
_init(raw, 1, 0, n - 1);
}
T modify(int idx, function<T(T)> modifier) { return update(idx, modifier(raw[idx])); }
T update(int idx, const T &newVal)
{
raw[idx] = newVal;
return _update(1, 0, n - 1, idx, newVal);
}
T query(int left, int right) { return _query(1, 0, n - 1, left, right); }
private:
vector<T> raw;
vector<T> data;
int n;
using Merge = function<T(const T &, const T &)>;
Merge merge;
T _init(const vector<T> &raw, int node, int start, int end)
{
int mid = (start + end) / 2;
if (start == end)
return data[node] = raw[start];
else
return data[node] = merge(_init(raw, node * 2, start, mid),
_init(raw, node * 2 + 1, mid + 1, end));
}
T _update(int node, int start, int end, int index, const T &newVal)
{
if (index < start || index > end)
return data[node];
if (start == end)
data[node] = newVal;
else
{
int mid = (start + end) / 2;
data[node] = merge(_update(node * 2, start, mid, index, newVal),
_update(node * 2 + 1, mid + 1, end, index, newVal));
}
return data[node];
}
T _query(int node, int start, int end, int left, int right)
{
if (left <= start && end <= right)
return data[node];
int mid = (start + end) / 2;
if (mid < left)
return _query(node * 2 + 1, mid + 1, end, left, right);
if (mid + 1 > right)
return _query(node * 2, start, mid, left, right);
return merge(_query(node * 2, start, mid, left, right),
_query(node * 2 + 1, mid + 1, end, left, right));
}
};
int n;
vector<i64> x;
vector<pair<i64, int>> y;
set<int> xp;
auto ytree = SegmentTree<pair<i64, int>>([](auto &l, auto &r)
{ return max(l, r); });
auto xtree = SegmentTree<i64>([](i64 l, i64 r)
{ return (l * r) % MOD; });
int getAns()
{
vector<int> xps = {n};
i64 xi = 1;
auto iter = xp.rbegin();
while (iter != xp.rend() && xi < (i64)1e9)
{
xps.push_back(*iter);
xi *= x[*iter];
++iter;
}
// x 다 곱해도 1e9 안 되는 경우, 전부 봐줘야함
if (xi < (i64)1e9 && xps.back() != 0)
xps.push_back(0);
reverse(all(xps));
i64 nowX = 1;
i64 maxX = 1;
i64 maxY = 1;
int ans = 0;
for (int k = 1; k < xps.size(); k++)
{
i64 nxtX = nowX * x[xps[k - 1]];
auto nxtY = ytree.query(xps[k - 1], xps[k] - 1);
if (maxY < nxtX / maxX * nxtY.xx)
{
maxX = nxtX;
maxY = nxtY.xx;
ans = nxtY.yy;
}
nowX = nxtX;
}
return (xtree.query(0, ans) * y[ans].xx) % MOD;
}
int init(int N, int X[], int Y[])
{
n = N;
x = vector<i64>(N + 1);
y = vector<pair<i64, int>>(N + 1);
for (int i = 0; i < N; i++)
{
x[i] = X[i];
if (x[i] >= 2)
xp.insert(i);
}
for (int i = 0; i < N; i++)
{
y[i].yy = i;
y[i].xx = Y[i];
}
ytree.init(y);
xtree.init(x);
return getAns();
}
int updateX(int pos, int val)
{
x[pos] = val;
xtree.update(pos, val);
if (x[pos] == 1)
xp.erase(pos);
else
xp.insert(pos);
return getAns();
}
int updateY(int pos, int val)
{
y[pos].xx = val;
ytree.update(pos, y[pos]);
return getAns();
}
Compilation message (stderr)
horses.cpp: In member function 'T SegmentTree<T>::_init(const std::vector<_Tp>&, int, int, int)':
horses.cpp:67:27: warning: declaration of 'raw' shadows a member of 'SegmentTree<T>' [-Wshadow]
67 | T _init(const vector<T> &raw, int node, int start, int end)
| ~~~~~~~~~~~~~~~~~^~~
horses.cpp:61:12: note: shadowed declaration is here
61 | vector<T> raw;
| ^~~
horses.cpp: In function 'int getAns()':
horses.cpp:144:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
144 | for (int k = 1; k < xps.size(); k++)
| ~~^~~~~~~~~~~~
horses.cpp:158:43: warning: conversion from 'long long int' to 'int' may change value [-Wconversion]
158 | return (xtree.query(0, ans) * y[ans].xx) % MOD;
| ^
horses.cpp: In instantiation of 'T SegmentTree<T>::_init(const std::vector<_Tp>&, int, int, int) [with T = std::pair<long long int, int>]':
horses.cpp:49:3: required from 'void SegmentTree<T>::init(const std::vector<_Tp>&) [with T = std::pair<long long int, int>]'
horses.cpp:180:14: required from here
horses.cpp:67:27: warning: declaration of 'raw' shadows a member of 'SegmentTree<std::pair<long long int, int> >' [-Wshadow]
67 | T _init(const vector<T> &raw, int node, int start, int end)
| ~~~~~~~~~~~~~~~~~^~~
horses.cpp:61:12: note: shadowed declaration is here
61 | vector<T> raw;
| ^~~
horses.cpp: In instantiation of 'T SegmentTree<T>::_init(const std::vector<_Tp>&, int, int, int) [with T = long long int]':
horses.cpp:49:3: required from 'void SegmentTree<T>::init(const std::vector<_Tp>&) [with T = long long int]'
horses.cpp:181:14: required from here
horses.cpp:67:27: warning: declaration of 'raw' shadows a member of 'SegmentTree<long long int>' [-Wshadow]
67 | T _init(const vector<T> &raw, int node, int start, int end)
| ~~~~~~~~~~~~~~~~~^~~
horses.cpp:61:12: note: shadowed declaration is here
61 | vector<T> raw;
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |