QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#644941 | #8577. 평균 최대화 | user10086 | 11 | 1692ms | 179736kb | C++23 | 9.3kb | 2024-10-16 16:06:44 | 2024-10-16 16:06:46 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 3e5 + 10;
int n, idx, l[N], r[N], a[N], s[N], rx[N], rs[N];
// #define __int128 int
int pc;
__int128 gcd(__int128 a, __int128 b)
{
pc++;
if (b == 0) return a;
return gcd(b, a % b);
}
struct F
{
__int128 i, j;
F()
{
i = j = 0;
}
F(__int128 a, __int128 b)
{
i = a, j = b;
__int128 d = gcd(i, j);
i /= d, j /= d;
}
bool operator> (const F& f2) const
{
return i * f2.j > j * f2.i;
}
bool operator< (const F& f2) const
{
return i * f2.j < j * f2.i;
}
F operator + (const F& f2) const
{
__int128 d = gcd(j, f2.j);
F f;
f.i = i * f2.j + j * f2.i, f.j = j * f2.j;
f.i /= d, f.j /= d;
return f;
}
}ans[N];
mt19937 rng(20241015);
namespace Tree
{
const int R = 2;
int idx;
int ls[N * R], rs[N * R], sz[N * R];
F tass[N * R];
F val[N * R], sum[N * R];
void init()
{
val[0] = sum[0] = {0, 1};
}
int newnode(F v)
{
idx++;
assert(idx < N * R);
ls[idx] = rs[idx] = 0, sz[idx] = 1, tass[idx] = {0, 1};
val[idx] = sum[idx] = v;
return idx;
}
inline void pushup(int u)
{
if (!u) return;
sum[u] = sum[ls[u]] + val[u] + sum[rs[u]];
sz[u] = sz[ls[u]] + 1 + sz[rs[u]];
}
inline void apply(int u, const F& dass)
{
if (!u) return;
sum[u] = {dass.i * sz[u], dass.j}, val[u] = dass;
tass[u] = dass;
}
inline void pushdown(int u)
{
if (!u) return;
if (tass[u].i == 0) return;
apply(ls[u], tass[u]), apply(rs[u], tass[u]);
tass[u] = {0, 1};
}
array<int, 2> splitr(int u, int k)
{
// [1, k], [k+1, inf)
// printf("splitr(%lld, %lld)\n", u, k);
if (!u) return {0, 0};
pushdown(u);
if (sz[ls[u]] >= k)
{
auto res = splitr(ls[u], k);
ls[u] = res[1];
pushup(u);
return {res[0], u};
}
else
{
auto res = splitr(rs[u], k - sz[ls[u]] - 1);
rs[u] = res[0];
pushup(u);
// printf("&&&");
return {u, res[1]};
}
}
array<int, 2> splitv(int u, const F& v)
{
// [1, v] [v, inf)
if (!u) return {0, 0};
pushdown(u);
if (v > val[u])
{
auto res = splitv(ls[u], v);
ls[u] = res[1];
pushup(u);
return {res[0], u};
}
else
{
auto res = splitv(rs[u], v);
rs[u] = res[0];
pushup(u);
return {u, res[1]};
}
}
int merge(int l, int r)
{
// printf("merge(%lld, %lld)\n", l, r);
if (!l) swap(l, r);
if (!r) return l;
pushdown(l), pushdown(r);
if (rng() % (sz[l] + sz[r]) < sz[l])
{
rs[l] = merge(rs[l], r);
pushup(l);
return l;
}
else
{
ls[r] = merge(l, ls[r]);
pushup(r);
return r;
}
}
inline void insert(int& rt, const F& v)
{
auto res = splitv(rt, v);
int c = newnode(v);
rt = merge(merge(res[0], c), res[1]);
}
inline void assign(int& rt, int pos, const F& v)
{
auto res = splitr(rt, pos);
apply(res[0], v);
rt = merge(res[0], res[1]);
}
inline F qsum(int rt, int pos)
{
if (!rt) return {0, 1};
pushdown(rt);
if (pos <= sz[ls[rt]]) return qsum(ls[rt], pos);
else return sum[ls[rt]] + val[rt] + qsum(rs[rt], pos - sz[ls[rt]] - 1);
// auto res = splitr(rt, pos);
// printf("qsum(%lld, %lld)\n", rt, pos);
// F ret = sum[res[0]];
// rt = merge(res[0], res[1]);
// return ret;
}
const F& kth(int rt, int k)
{
int p = sz[ls[rt]];
pushdown(rt);
if (k <= p) return kth(ls[rt], k);
else if (k > p + 1) return kth(rs[rt], k - sz[ls[rt]] - 1);
else return val[rt];
// auto res = splitr(rt, k);
// auto res2 = splitr(res[0], k - 1);
// F ret = val[res2[1]];
// rt = merge(merge(res2[0], res2[1]), res[1]);
// return ret;
}
void print(int rt)
{
if (!rt) return;
print(ls[rt]), printf("node %lld: sum = %lld/%lld, val = %lld/%lld, tass = %lld/%lld, ls = %lld, rs = %lld\n", rt, sum[rt].i, sum[rt].j, val[rt].i, val[rt].j, tass[rt].i, tass[rt].j, ls[rt], rs[rt]), print(rs[rt]);
}
}
int pc2;
int dp[N];
vector<int> son[N];
vector<int> rg[N];
map<array<int, 2>, int> mp;
int build(int l, int r)
{
idx++, ::l[idx] = l, ::r[idx] = r, mp[{l, r}] = idx;
int x = idx; rx[x] = r - l + 1, rs[x] = s[r] - s[l - 1];
for (int i = l; i <= r; i++)
{
if (rg[i].empty()) continue;
int j = rg[i].back(); rg[i].pop_back();
int y = build(i, j);
son[x].push_back(y);
rx[x] -= (j - i + 1), rs[x] -= (s[j] - s[i - 1]);
i = j;
}
return x;
}
template<class T>
void chkmax(T &x, T y)
{
if (y > x) x = y;
}
void merge(int& a, int b)
{
if (!b) return;
Tree::pushdown(b);
merge(a, Tree::ls[b]), merge(a, Tree::rs[b]);
Tree::ls[b] = Tree::rs[b] = 0, Tree::sz[b] = 1, Tree::sum[b] = Tree::val[b];
auto res = Tree::splitv(a, Tree::val[b]);
a = Tree::merge(Tree::merge(res[0], b), res[1]);
}
void getp(int rt, int a, int b, F cur, int rk, pair<int, F>& ans)
{
// printf("getp(%lld, %lld, %lld, {%lld, %lld}, %lld)\n", rt, a, b, cur.i, cur.j, rk);
if (!rt) return;
Tree::pushdown(rt);
F f = cur + Tree::sum[Tree::ls[rt]], d = Tree::val[rt];
f.i -= b * f.j, f.j *= (rk + Tree::sz[Tree::ls[rt]] - a);
// printf("f\' = {%lld, %lld}, d = {%lld, %lld}\n", f.i, f.j, d.i, d.j);
if (f > d) getp(Tree::ls[rt], a, b, cur, rk, ans);
else
{
f = cur + Tree::sum[Tree::ls[rt]] + Tree::val[rt];
f.i -= b * f.j, f.j *= (rk + Tree::sz[Tree::ls[rt]] + 1 - a);
ans = {rk + Tree::sz[Tree::ls[rt]] + 1, f};
getp(Tree::rs[rt], a, b, cur + Tree::sum[Tree::ls[rt]] + Tree::val[rt], rk + Tree::sz[Tree::ls[rt]] + 1, ans);
}
}
// pair<int, F> getp(int rt, int a, int b)
// {
// // max (y - b) / (x - a)
// int l = 0, r = Tree::sz[rt];
// auto getres = [&](int x)
// {
// F y1 = Tree::qsum(rt, x);
// y1.i -= b * y1.j, y1.j *= (x - a);
// // y1.reduce();
// return y1;
// };
// int dpc = 0;
// while (l < r)
// {
// // int mid1 = (l + r) >> 1, mid2 = mid1 + 1;
// dpc += 2;
// int mid = (l + r) >> 1;
// F f = Tree::qsum(rt, mid), d = Tree::kth(rt, mid + 1);
// f.i -= b * f.j, f.j *= (mid - a);
// if (f > d) r = mid;
// else l = mid + 1;
// }
// // printf(")))"), Tree::print(rt);
// // printf("res(l) = (%lld, %lld/%lld), res(r) = (%lld, %lld/%lld)\n", l, getres(l).i, getres(l).j, r, getres(r).i, getres(r).j);
// dpc++;
// assert(dpc < 19 * 2 + 1);
// pc2 += dpc;
// auto resl = getres(l);
// return {l, resl};
// }
void dfs(int x)
{
// printf("dfs(%lld)\n", x);
dp[x] = 0;
if (son[x].empty()) for (int i = 1; i <= rx[x]; i++) Tree::insert(dp[x], {rs[x], rx[x]});
else
{
for (int y : son[x])
{
dfs(y);
if (Tree::sz[dp[x]] < Tree::sz[dp[y]]) swap(dp[x], dp[y]);
merge(dp[x], dp[y]);
}
if (x == 1) return;
// Tree::print(dp[x]);
// (-rx[x], -rs[x])
//printf("getp(%lld)\n", x);
pair<int, F> res = {0, {rx[x], rs[x]}};
getp(dp[x], -rx[x], -rs[x], {0, 1}, 0, res);
// printf("proc: res = {%lld, %lld/%lld}\n", res.first, res.second.i, res.second.j);
int p = res.first;
F v = res.second;
Tree::assign(dp[x], p, v);
for (int i = 1; i <= rx[x]; i++) Tree::insert(dp[x], v);
}
// Tree::print(dp[x]);
// cout << "***";
// assert(pc < 3.5e7);
pair<int, F> res = {0, {2, a[l[x] - 1] + a[r[x] + 1]}};
getp(dp[x], -2, -(a[l[x] - 1] + a[r[x] + 1]), {0, 1}, 0, res);
ans[x] = res.second;
// printf("%lld: (%lld, %lld/%lld)\n", x, res.first, ans[x].i, ans[x].j);
}
bool flag;
void initialize(vector<signed> A)
{
n = (int)(A.size());
for (int i = 1; i <= n; i++) a[i] = A[i - 1];
for (int i = 1; i <= n; i++) s[i] = a[i] + s[i - 1];
vector<int> s;
for (int i = n; i >= 1; i--)
{
while (!s.empty() && a[s.back()] > a[i])
{
int x = s.back(); s.pop_back();
if (x != i + 1) rg[i + 1].push_back(x - 1);
}
if (!s.empty() && s.back() != i + 1) rg[i + 1].push_back(s.back() - 1);
if (!s.empty() && a[s.back()] >= a[i]) s.pop_back();
s.push_back(i);
}
// for (int i = 1; i <= n; i++)
// for (int j : rg[i]) printf("[%lld, %lld]\n", i, j);
build(2, n - 1);
// for (int i = 1; i <= n; i++)
// printf("rx[%lld] = %lld, rs[%lld] = %lld\n", i, rx[i], i, rs[i]);
Tree::init();
if (!flag) dfs(1);
// for (int i = 1; i <= n; i++)
// for (int j = 0; j <= sz[i]; j++)
// printf("dp[%lld][%lld] = %lld\n", i, j, dp[i][j]);
// for (int i = 1; i <= n; i++)
// for (int j : son[i]) printf("[%lld, %lld] -> [%lld, %lld]\n", l[i], r[i], l[j], r[j]);
}
array<long long, 2> maximum_average(signed i, signed j)
{
i++, j++;
if (j == i + 1) return {a[i] + a[j], 2};
assert(mp.find({i + 1, j - 1}) != mp.end());
int id = mp[{i + 1, j - 1}];
if (flag) return {idx, ans[id].j};
return {ans[id].i, ans[id].j};
}
// signed main()
// {
// cin.tie(0)->sync_with_stdio(0);
//
// int n; cin >> n;
// vector<signed> A;
// for (int i = 1, ai; i <= n; i++) cin >> ai, A.push_back(ai);
//// for (int i = 1, ai; i <= n; i++) A.push_back(rng() % 20 + 1);
// initialize(A);
// printf("idx = %lld, pc = %lld, pc2 = %lld, time = %lld\n", idx, pc, pc2, (int)clock());
// int l, r;
// while (cin >> l >> r)
// {
// auto res = maximum_average(l, r);
// cout << res[0] << ' ' << res[1] << '\n';
// }
// }
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 5
Accepted
Test #1:
score: 5
Accepted
time: 0ms
memory: 88040kb
input:
10 2 4 3 9 9 9 9 9 9 1 2 0 2 0 9
output:
9 3 420 63
result:
ok correct!
Test #2:
score: 5
Accepted
time: 11ms
memory: 89876kb
input:
15 4596730 8340349 4612555 5692442 3914918 5213545 5248236 1276073 3844119 2943960 9231647 5091649 2239006 9139001 4735414 100 7 8 5 6 2 4 0 4 8 9 10 11 3 4 0 1 10 11 10 11 3 4 4 5 12 13 0 2 2 4 11 12 12 14 2 3 7 8 12 14 6 7 4 5 11 12 10 11 7 12 8 9 8 9 0 2 2 3 12 14 7 9 7 9 12 13 10 11 9 11 13 14 8...
output:
5120192 2 10461781 2 14219915 3 54313988 10 6788079 2 14323296 2 9607360 2 12937079 2 14323296 2 14323296 2 9607360 2 9128463 2 11378007 2 17549634 3 14219915 3 7330655 2 16113421 3 10304997 2 5120192 2 16113421 3 6524309 2 9128463 2 7330655 2 14323296 2 124694010 30 6788079 2 6788079 2 17549634 3 1...
result:
ok correct!
Test #3:
score: 5
Accepted
time: 106ms
memory: 87792kb
input:
15 962724 8815662 7612372 5708998 125165 5107756 9366378 9514244 2381600 4299006 9423670 8225791 7458292 2315903 7210561 600000 7 8 0 4 6 8 9 10 11 12 4 5 13 14 8 13 9 11 2 3 7 8 9 12 6 8 0 4 0 2 12 13 1 2 8 13 0 3 9 10 4 5 6 7 6 8 6 7 0 2 4 13 3 4 6 7 8 9 6 7 11 12 5 8 0 3 9 13 4 8 4 8 8 9 8 13 4 1...
output:
11895844 2 139349526 30 21262222 3 13722676 2 15684083 2 5232921 2 9526464 2 818502288 144 21948467 3 13321370 2 11895844 2 58813518 8 21262222 3 139349526 30 17390758 3 9774195 2 16428034 2 818502288 144 46199512 8 13722676 2 5232921 2 18880622 2 21262222 3 18880622 2 17390758 3 11177818560 1920 58...
result:
ok correct!
Test #4:
score: 5
Accepted
time: 0ms
memory: 87760kb
input:
15 1 8446287 2 999999 3000000 5533975 3000000 3816891 3000000 7671276 3000000 999999 5836790 8574548 1 23 0 14 1 2 0 1 2 14 0 2 3 11 2 3 4 6 3 4 5 6 4 5 6 8 7 8 6 7 8 10 9 10 8 9 10 11 11 14 12 14 11 12 13 14 12 13
output:
3232786140 900 8446289 2 8446288 2 2726008860 780 8446290 3 186132840 54 1000001 2 11533975 3 3999999 2 8533975 2 8533975 2 9816891 3 6816891 2 6816891 2 13671276 3 10671276 2 10671276 2 3999999 2 30822676 8 14411339 3 6836789 2 8574549 2 14411338 2
result:
ok correct!
Test #5:
score: 5
Accepted
time: 0ms
memory: 85748kb
input:
15 1 15 16 14 18 13 20 12 22 11 24 10 26 9 1 27 0 14 1 3 0 1 2 3 1 2 3 5 0 3 4 5 3 4 5 7 0 5 6 7 5 6 7 9 0 7 8 9 7 8 9 11 0 9 10 11 9 10 11 13 0 11 12 13 11 12 13 14 0 13
output:
3816 270 45 3 16 2 30 2 31 2 45 3 92 8 31 2 32 2 45 3 154 12 32 2 33 2 45 3 218 16 33 2 34 2 45 3 284 20 34 2 35 2 45 3 352 24 35 2 36 2 10 2 422 28
result:
ok correct!
Subtask #2:
score: 6
Accepted
Dependency #1:
100%
Accepted
Test #6:
score: 6
Accepted
time: 3ms
memory: 87720kb
input:
48 225555 4046145 5839635 7194994 4703765 1253415 2526352 3198926 6313532 2368195 5024833 9436074 1792945 7650559 3393464 2402026 7697170 5205463 9830460 5392966 1687150 9984223 3014343 8856776 1412298 9773499 6469768 5802450 758943 2748325 7110370 4498454 2674137 8596714 8823659 9855644 6654297 367...
output:
48826396 8 16509941 2 17738394 3 1802088120 360 34062572 8 48826396 8 11608824 2 1802088120 360 17738394 3 13283417 3 9885780 2 46916030 8 14460907 2 5795490 2 25333600 3 11880653 3 7923098 2 138227724 30 16627832686080 3294720 22841241 3 10545933 2 17738394 3 13283417 3 18983962 3 17655565 3 112708...
result:
ok correct!
Test #7:
score: 6
Accepted
time: 121ms
memory: 88048kb
input:
50 7121308 7345583 6899063 282017 6341784 3680369 5436234 9663519 633330 6333746 7783999 6482701 567072 4276742 8011254 1944632 5712778 8002712 306241 4160326 5728910 1328677 6357927 2565549 4232827 255999 3544802 2039097 494486 2383883 9963617 175242 2913048 5502915 9123911 4881811 2516781 8926134 ...
output:
5394962 2 12522742 3 14466891 2 12347500 2 4567430 2 14631630 3 6947205 2 14631630 3 31932726 8 15264170006400 3427200 31932726 8 12522742 3 6967076 2 3800801 2 12668768 8 2533583 2 152694167808 32760 4488826 2 1014170707200 215040 14626826 2 2533583 2 15733083 3 15099753 2 10138859 2 1014170707200 ...
result:
ok correct!
Test #8:
score: 6
Accepted
time: 0ms
memory: 89852kb
input:
50 1 3859136 7745573 6119170 3863010 2 3 4 3498508 5 6608915 6662164 999999 3000000 7880751 3000000 4473437 3000000 7609368 3000000 4750778 3000000 8554401 3000000 5166495 3000000 4156171 3000000 9941061 3000000 7323881 3000000 3334344 3000000 3959127 3000000 999999 6 5 5322820 9189056 4 8127987 797...
output:
7109701042531230000 1838083104000 129521346 30 3859137 2 13864743 2 11604709 2 9982180 2 17723879 3 3863012 2 43173778 8 139623680753013600 35740504800 518085408 144 3179244892032000 807325200 5 2 85690493156400 21819600 7 2 3498513 2 3498512 2 2533705540800 638400 3498517 3 14271078 3 6608920 2 766...
result:
ok correct!
Test #9:
score: 6
Accepted
time: 0ms
memory: 87812kb
input:
50 1 50 51 49 53 48 55 47 57 46 59 45 61 44 63 43 65 42 67 41 69 40 71 39 73 38 75 37 77 36 79 35 81 34 83 33 85 32 87 31 89 30 91 29 93 28 95 27 97 1 97 0 49 1 3 0 1 2 3 1 2 3 5 0 3 4 5 3 4 5 7 0 5 6 7 5 6 7 9 0 7 8 9 7 8 9 11 0 9 10 11 9 10 11 13 0 11 12 13 11 12 13 15 0 13 14 15 13 14 15 17 0 15 ...
output:
3514 64 150 3 51 2 100 2 101 2 150 3 302 8 101 2 102 2 150 3 504 12 102 2 103 2 150 3 708 16 103 2 104 2 150 3 914 20 104 2 105 2 150 3 1122 24 105 2 106 2 150 3 1332 28 106 2 107 2 150 3 1544 32 107 2 108 2 150 3 1758 36 108 2 109 2 150 3 1974 40 109 2 110 2 150 3 2192 44 110 2 111 2 150 3 2412 48 ...
result:
ok correct!
Subtask #3:
score: 0
Wrong Answer
Dependency #2:
100%
Accepted
Test #10:
score: 0
Wrong Answer
time: 118ms
memory: 87832kb
input:
240 6858784 4989917 9746109 9800650 9356541 6503323 7401498 4493451 2801567 3386165 2481047 9837911 8949606 8663384 5535990 833163 922389 2217653 4643612 8798924 859732 616449 7786902 4457600 9298353 6097782 1517199 1575123 3272602 8273488 8507227 5716403 4182244 3701458 1150320 7526997 7126600 8466...
output:
16368254 2 7471044 2 11386428 3 3195008 2 9658656 2 15523880 2 3140042 2 1379800008 216 9688346 3 3267230 2 10959355 2 11038795 2 9761526 2 7883702 2 14605972 2 15088697 2 17640709 2 4918214 2 871226424 144 89325048 30 6296185 2 876017 2 6164558 2 13307829 3 19546759 2 8377911 2 14987510 3 20631197 ...
result:
wrong answer Wrong Answer on query #578: -2835645163246526464/12055751769600 != 276607600/47
Subtask #4:
score: 0
Runtime Error
Test #15:
score: 0
Runtime Error
input:
300000 1 2 4 4 4 4 3 2 4 4 3 4 4 4 4 4 4 4 4 4 3 4 3 4 4 4 4 4 4 4 4 3 3 4 4 4 3 4 3 4 4 4 4 4 4 4 4 4 4 3 3 4 4 4 3 4 4 3 4 4 4 4 4 4 4 3 2 4 4 4 4 4 4 4 4 4 4 4 4 4 3 4 4 4 4 4 4 4 4 4 4 4 2 4 4 2 4 4 3 4 4 4 2 3 4 4 4 4 4 4 3 2 4 4 4 2 4 4 4 4 4 4 4 4 4 4 4 2 4 4 4 4 4 4 3 4 4 3 4 4 4 4 4 4 4 4 4...
output:
Unauthorized output
result:
Subtask #5:
score: 0
Skipped
Dependency #3:
0%
Subtask #6:
score: 0
Runtime Error
Test #28:
score: 17
Accepted
time: 703ms
memory: 179736kb
input:
300000 1 300000 300001 299999 300003 299998 300005 299997 300007 299996 300009 299995 300011 299994 300013 299993 300015 299992 300017 299991 300019 299990 300021 299989 300023 299988 300025 299987 300027 299986 300029 299985 300031 299984 300033 299983 300035 299982 300037 299981 300039 299980 3000...
output:
1834500604 4900
result:
ok correct!
Test #29:
score: 17
Accepted
time: 1692ms
memory: 131492kb
input:
262143 1 36 17 55 17 36 16 94 16 36 17 55 17 36 15 173 15 36 17 55 17 36 16 94 16 36 17 55 17 36 14 332 14 36 17 55 17 36 16 94 16 36 17 55 17 36 15 173 15 36 17 55 17 36 16 94 16 36 17 55 17 36 13 651 13 36 17 55 17 36 16 94 16 36 17 55 17 36 15 173 15 36 17 55 17 36 16 94 16 36 17 55 17 36 14 332 ...
output:
3932178 15
result:
ok correct!
Test #30:
score: 0
Runtime Error
input:
30000 1 8655519 5053769 2 3 4 5 6991720 3321347 8718018 4464302 8356978 6 7 8 8314637 5910310 3734266 7192139 9183941 9 4352483 6266035 4417558 10 6855725 6999520 4363735 11 12 3499845 8360666 13 14 15 16 9638440 7317746 17 9282787 18 7466678 7930596 9367029 9821794 19 9771448 5441062 6451096 328952...
output:
Unauthorized output
result:
Subtask #7:
score: 0
Skipped
Dependency #4:
0%
Subtask #8:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Dependency #3:
0%