Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <assert.h>
- #include <bits/stdc++.h>
- #include <atcoder/segtree>
- #include <atcoder/modint>
- using namespace std;
- #define dbg(...) logger(#__VA_ARGS__, __VA_ARGS__)
- template <typename... Args> void logger(string vars, Args &&... values)
- {
- cerr << vars << " = ";
- string delim = "";
- (..., (cerr << delim << values, delim = ", "));
- cerr << endl;
- }
- template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
- using ll = long long;
- using pii = pair<int, int>;
- using atcoder::segtree;
- using mint = atcoder::modint998244353;
- const ll p = 998244353;
- ll n;
- ll arr[200003];
- struct S {
- mint val, cnt, sum;
- };
- S op(S a, S b) { return {0, a.cnt + b.cnt, a.sum + b.sum}; }
- S e() { return {0, 0, 0}; }
- int main(int argc, char **argv)
- {
- vector<ll> uniq;
- cin >> n;
- for (int i = 1; i <= n; ++i)
- cin >> arr[i], uniq.push_back(arr[i]);
- sort(uniq.begin(), uniq.end());
- uniq.erase(unique(uniq.begin(), uniq.end()), uniq.end());
- int tlen = uniq.size();
- segtree<S, op, e> seg(tlen);
- mint prev_expect = arr[1];
- {
- cout << prev_expect.val() << endl;
- int lb = lower_bound(uniq.begin(), uniq.end(), arr[1]) - uniq.begin();
- auto v = seg.get(lb);
- v.val = arr[1];
- v.cnt += 1;
- v.sum += v.val;
- seg.set(lb, v);
- }
- for (int i = 2; i <= n; ++i) {
- int lb = lower_bound(uniq.begin(), uniq.end(), arr[i]) - uniq.begin();
- auto v1 = seg.prod(0, lb);
- auto v2 = seg.prod(lb, tlen);
- mint a = (i - 1), b = 1;
- a /= i, b /= i;
- mint c = 2 * a * b;
- mint comb = v1.cnt;
- comb *= arr[i];
- comb += v2.sum;
- comb /= i;
- mint this_expect = a * a * prev_expect + c * comb + b * b * arr[i];
- cout << this_expect.val() << endl;
- prev_expect = this_expect;
- auto v3 = seg.get(lb);
- v3.val = arr[i];
- v3.cnt += 1;
- v3.sum += v3.val;
- seg.set(lb, v3);
- }
- return 0;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement