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
| namespace SegTrees { const int maxm = maxn * 32; int tot, root[maxn << 1], ls[maxm], rs[maxm]; PII val[maxm]; PII cal(PII a, PII b) { if (a.first > b.first) return a; else if (a.first == b.first && a.second < b.second) return a; else return b; } void update(int i, int l, int r, int& rt) { if (!rt) rt = ++tot; if (l == r) { val[rt].first++; val[rt].second = i; return ; } int m = (l + r) / 2; if (i <= m) update(i, l, m, ls[rt]); else update(i, m + 1, r, rs[rt]); val[rt] = cal(val[ls[rt]], val[rs[rt]]); } int join(int x, int y, int l, int r) { if (!x || !y) return x + y; int u = ++tot; if (l == r) { val[u].first = val[x].first + val[y].first; val[u].second = l; } else { int m = (l + r) / 2; ls[u] = join(ls[x], ls[y], l, m); rs[u] = join(rs[x], rs[y], m + 1, r); val[u] = cal(val[ls[u]], val[rs[u]]); } return u; } PII query(int L, int R, int l, int r, int rt) { if (!rt) return { -1, inf }; if (L <= l && r <= R) return val[rt]; int m = (l + r) / 2; if (R <= m) return query(L, R, l, m, ls[rt]); else if (L > m) return query(L, R, m + 1, r, rs[rt]); else return cal(query(L, R, l, m, ls[rt]), query(L, R, m + 1, r, rs[rt])); } }
|