BZOJ3600:沒有人的算術
阿新 • • 發佈:2019-01-12
傳送門
如果能給每個 \(pair\) 按照權值編號就好了
假設之前已經有了所有的權值的編號,現在考慮編號新的 \(pair\)
如果看過了陳立傑的論文的話,不難得到一個重量平衡樹的做法
給樹上每個子樹一個實數權值區間 \([l,r]\),這個點權值為 \(mid=\frac{l+r}{2}\)
左子樹 \([l,mid]\) 右子樹 \([mid,r]\)
只需要選擇一個樹高 \(log\) 的樹(treap/替罪羊樹)使得滿足精度要求即可
# include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn(5e5 + 5); const double alpha(0.75); int ls[maxn], rs[maxn], rt, tot, size[maxn], que[maxn], cnt, id[maxn], n, m; double val[maxn]; pair <int, int> info[maxn]; int mx[maxn << 2]; inline int operator <(pair <int, int> a, pair <int, int> b) { return val[a.first] == val[b.first] ? val[a.second] < val[b.second] : val[a.first] < val[b.first]; } void Dfs(int u) { if (!u) return; Dfs(ls[u]), que[++cnt] = u, Dfs(rs[u]); } int Build(int l, int r, double vl, double vr) { if (l > r) return 0; double midv; int mid, o; mid = (l + r) >> 1, o = que[mid], midv = (vl + vr) * 0.5; ls[o] = rs[o] = 0, val[o] = midv; ls[o] = Build(l, mid - 1, vl, midv); rs[o] = Build(mid + 1, r, midv, vr); size[o] = size[ls[o]] + size[rs[o]] + 1; return o; } int Rebuild(int x, double vl, double vr) { cnt = 0, Dfs(x); return Build(1, cnt, vl, vr); } int Insert(int &x, double vl, double vr, pair <int, int> v) { double midv; int ret; midv = (vl + vr) * 0.5; if (!x) { x = ++tot, val[x] = midv, info[x] = v, size[x] = 1; return x; } if (alpha * size[x] < max(size[ls[x]], size[rs[x]])) x = Rebuild(x, vl, vr); if (v == info[x]) return x; else if (v < info[x]) ret = Insert(ls[x], vl, midv, v); else ret = Insert(rs[x], midv, vr, v); size[x] = size[ls[x]] + size[rs[x]] + 1; return ret; } void Modify(int x, int l, int r, int p) { int mid; if (l == r) mx[x] = l; else { mid = (l + r) >> 1; p <= mid ? Modify(x << 1, l, mid, p) : Modify(x << 1 | 1, mid + 1, r, p); mx[x] = val[id[mx[x << 1]]] >= val[id[mx[x << 1 | 1]]] ? mx[x << 1] : mx[x << 1 | 1]; } } int Query(int x, int l, int r, int ql, int qr) { int mid, ret, v; if (ql <= l && qr >= r) return mx[x]; mid = (l + r) >> 1, ret = -1, v; if (ql <= mid) ret = Query(x << 1, l, mid, ql, qr); if (qr > mid) { v = Query(x << 1 | 1, mid + 1, r, ql, qr); ret = (ret == -1 || val[id[v]] > val[id[ret]]) ? v : ret; } return ret; } int main() { int i, l, r, k; char op; scanf("%d%d", &n, &m); val[0] = -1, Insert(rt, 0, 1, make_pair(0, 0)); for (i = 1; i <= n; ++i) Modify(1, 1, n, i); for (i = 1; i <= m; ++i) { scanf(" %c%d%d", &op, &l, &r); if (op == 'C') { scanf("%d", &k); id[k] = Insert(rt, 0, 1, make_pair(id[l], id[r])); Modify(1, 1, n, k); } else printf("%d\n", Query(1, 1, n, l, r)); } return 0; }