# LOJ2537. 「PKUWC2018」Minimax【概率DP+线段树合并】

## 思路

#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

namespace io {

const int BUFSIZE = 1 << 20;
char ibuf[BUFSIZE], *is = ibuf, *it = ibuf;
char obuf[BUFSIZE], *os = obuf, *ot = obuf + BUFSIZE - 1;

if (is == it)
it = (is = ibuf) + fread(ibuf, 1, BUFSIZE, stdin);
return *is++;
}

int x = 0, f = 1;
while (!isdigit(c)) {
if (c == '-') f = -1;
}
while (isdigit(c)) x = x * 10 + c - '0', c = read_char();
return x * f;
}

ll x = 0, f = 1;
while (!isdigit(c)) {
if (c == '-') f = -1;
}
while (isdigit(c)) x = x * 10 + c - '0', c = read_char();
return x * f;
}

while (!isspace(c)) *s++ = c, c = read_char();
*s = 0;
}

void flush() {
fwrite(obuf, 1, os - obuf, stdout);
os = obuf;
}

void print_char(char c) {
*os++ = c;
if (os == ot) flush();
}

void print_int(int x) {
static char q[20];
if (!x) print_char('0');
else {
if (x < 0) print_char('-'), x = -x;
int top = 0;
while (x) q[top++] = x % 10 + '0', x /= 10;
while (top--) print_char(q[top]);
}
}

void print_ll(ll x) {
static char q[20];
if (!x) print_char('0');
else {
if (x < 0) print_char('-'), x = -x;
int top = 0;
while (x) q[top++] = x % 10 + '0', x /= 10;
while (top--) print_char(q[top]);
}
}

struct flusher_t {
~flusher_t() {
flush();
}
} flusher;

};
using namespace io;

const int Mod = 998244353;
const int N = 3e5 + 10;
const int LOG = 50;

int rt[N], ls[N * LOG], rs[N * LOG], val[N * LOG], tag[N * LOG];
int ld[N], rd[N], tot = 0;
int n, m, w[N], pre[N], pos[N];

int add(int a, int b) {
return (a += b) >= Mod ? a - Mod : a;
}

int sub(int a, int b) {
return (a -= b) < 0 ? a + Mod : a;
}

int mul(int a, int b) {
return (long long) a * b % Mod;
}

int fast_pow(int a, int b) {
int res = 1;
for (; b; b >>= 1, a = mul(a, a))
if (b & 1) res = mul(res, a);
return res;
}

void pushup(int t) {
}

void pushnow(int t, int vl) {
if (!t) return;
val[t] = mul(val[t], vl);
tag[t] = mul(tag[t], vl);
}

void pushdown(int t) {
if (tag[t] != 1) {
pushnow(ls[t], tag[t]);
pushnow(rs[t], tag[t]);
tag[t] = 1;
}
}

void insert(int &t, int l, int r, int pos) {
t = ++tot;
tag[t] = val[t] = 1;
if (l == r) return;
int mid = (l + r) >> 1;
if (pos <= mid) insert(ls[t], l, mid, pos);
else insert(rs[t], mid + 1, r, pos);
}

int merge(int x, int y, int sumx, int sumy, int promin, int promax) {
if (!y) {pushnow(x, sumx); return x;}
if (!x) {pushnow(y, sumy); return y;}
pushdown(x), pushdown(y);
int prex = val[ls[x]], sufx = val[rs[x]];
int prey = val[ls[y]], sufy = val[rs[y]];
// 必须要提前声明 不然会被修改
ls[x] = merge(ls[x], ls[y], add(sumx, mul(promin, sufy)), add(sumy, mul(promin, sufx)), promin, promax);
rs[x] = merge(rs[x], rs[y], add(sumx, mul(promax, prey)), add(sumy, mul(promax, prex)), promin, promax);
pushup(x);
return x;
}

void dfs(int u) {
if (!u) return;
dfs(ld[u]), dfs(rd[u]);
if (!ld[u]) {
insert(rt[u], 1, m, pos[u]);
} else if (!rd[u]) { // 特判
rt[u] = rt[ld[u]];
} else {
rt[u] = merge(rt[ld[u]], rt[rd[u]], 0, 0, sub(1, w[u]), w[u]);
}
}

int calc(int t, int l, int r) {
if (!t) return 0;
if (l == r) return mul(mul(mul(val[t], val[t]), pre[l]), l);
pushdown(t);
int mid = (l + r) >> 1;
return add(calc(ls[t], l, mid), calc(rs[t], mid + 1, r));
}

int main() {
#ifdef dream_maker
freopen("input.txt", "r", stdin);
#endif
int inv = fast_pow(10000, Mod - 2);
for (int i = 1; i <= n; i++) {
if (!ld[fa]) ld[fa] = i;
else rd[fa] = i;
}
for (int i = 1; i <= n; i++) {
}