QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#252556 | #3835. Oracle | rgnerdplayer | AC ✓ | 735ms | 66584kb | C++20 | 2.5kb | 2023-11-15 21:06:32 | 2023-11-15 21:06:32 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
using u64 = unsigned long long;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
struct Treap {
Treap *lc = nullptr, *rc = nullptr;
int sz = 1;
i64 k = 0, b = 0;
i64 val = 0;
u64 w = rng();
Treap(i64 v) : val(v) {}
};
int size(Treap *t) {
return t ? t->sz : 0;
}
void pull(Treap *t) {
t->sz = size(t->lc) + size(t->rc) + 1;
}
void apply(Treap *t, i64 k, i64 b) {
if (!t) { return; }
t->k += k;
t->b += b;
t->val += k * (1 + size(t->lc)) + b;
}
void push(Treap *t) {
apply(t->lc, t->k, t->b);
apply(t->rc, t->k, t->k * (1 + size(t->lc)) + t->b);
t->k = t->b = 0;
}
template <typename F>
pair<Treap*, Treap*> split(Treap *t, int s, F f) {
if (!t) { return {t, t}; }
push(t);
Treap *a, *b;
if (f(t, s + size(t->lc) + 1)) {
b = t;
tie(a, b->lc) = split(t->lc, s, f);
} else {
a = t;
tie(a->rc, b) = split(t->rc, s + size(t->lc) + 1, f);
}
pull(t);
return {a, b};
}
Treap* merge(Treap *a, Treap *b) {
if (!a) { return b; }
if (!b) { return a; }
push(a), push(b);
if (a->w > b->w) {
a->rc = merge(a->rc, b);
pull(a);
return a;
} else {
b->lc = merge(a, b->lc);
pull(b);
return b;
}
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int t;
cin >> t;
auto solve = [&]() {
int n;
cin >> n;
vector<int> p(n);
for (int i = 0; i < n; i++) {
cin >> p[i];
}
int a, b;
cin >> a >> b;
vector<i64> func(n + 1);
for (int i = 1; i <= n; i++) {
func[i] = 1LL * i * i + a * i + b;
}
Treap *t = nullptr;
for (auto p : p) {
auto [t1, t2] = split(t, 0, [&](Treap *t, int s) {
return func[s] * p >= t->val;
});
int s = size(t1) + 1;
apply(t2, 2 * p, 1LL * (2 * s + a - 1) * p);
t = merge(merge(t1, new Treap (func[s] * p)), t2);
}
i64 ans = 0;
for (int i = 1; i <= n; i++) {
auto [t1, t2] = split(t, 0, [&](Treap *t, int s) {
return s > 1;
});
ans += t1->val;
t = t2;
cout << ans << " \n"[i == n];
}
};
while (t--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 735ms
memory: 66584kb
input:
20 50000 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100...
output:
50000 249999 699994 1499980 2749950 4549895 6999804 10199664 14249460 19249175 25298790 32498284 40947634 50746815 61995800 74794560 89243064 105441279 123489170 143486700 165533830 189730519 216176724 244972400 276217500 310011975 346455774 385648844 427691130 472682575 520723120 571912704 62635126...
result:
ok 20 lines