主席树
模板
P3919 【模板】可持久化线段树 1(可持久化数组)
#include <bits/stdc++.h>using namespace std;const int N = 1e6 + 10;int root[N], n, m;int ls[N * 25], rs[N * 25], value[N * 25], tot;void build(int &rt, int l, int r) {rt = ++tot;if (l == r) {scanf("%d", &value[rt]);return ;}int mid = l + r >> 1;build(ls[rt], l, mid);build(rs[rt], mid + 1, r);
}void update(int &rt, int pre, int l, int r, int x, int v) {rt = ++tot, ls[rt] = ls[pre], rs[rt] = rs[pre], value[rt] = value[pre];if (l == r) {value[rt] = v;return ;}int mid = l + r >> 1;if (x <= mid) {update(ls[rt], ls[pre], l, mid, x, v);}else {update(rs[rt], rs[pre], mid + 1, r, x, v);}
}int query(int rt, int l, int r, int x) {if (l == r) {return value[rt];}int mid = l + r >> 1;if (x <= mid) {return query(ls[rt], l, mid, x);}else {return query(rs[rt], mid + 1, r, x);}
}int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);scanf("%d %d", &n, &m);build(root[0], 1, n);for (int i = 1; i <= m; i++) {int pre, op, v, x;scanf("%d %d %d", &pre, &op, &x);if (op == 1) {scanf("%d", &v);update(root[i], root[pre], 1, n, x, v);}else {printf("%d\n", query(root[pre], 1, n, x));root[i] = root[pre];}}return 0;
}
P3834 【模板】可持久化线段树 2(主席树)
#include <bits/stdc++.h>using namespace std;const int N = 2e5 + 10;int a[N], b[N], root[N], n, m;int ls[N * 20], rs[N * 20], val[N * 20], tot;void update(int &rt, int pre, int l, int r, int x, int value) {rt = ++tot, ls[rt] = ls[pre], rs[rt] = rs[pre], val[rt] = val[pre] + 1;if (l == r) {return ;}int mid = l + r >> 1;if (x <= mid) {update(ls[rt], ls[pre], l, mid, x, value);}else {update(rs[rt], rs[pre], mid + 1, r, x, value);}
}int query(int rtl, int rtr, int l, int r, int k) {if (l == r) {return l;}int sum = val[ls[rtr]] - val[ls[rtl]], mid = l + r >> 1;if (sum >= k) {return query(ls[rtl], ls[rtr], l, mid, k);}else {return query(rs[rtl], rs[rtr], mid + 1, r, k - sum);}
}int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);scanf("%d %d", &n, &m);for (int i = 1; i <= n; i++) {scanf("%d", &a[i]);b[i] = a[i];}sort(b + 1, b + 1 + n);int n1 = unique(b + 1, b + 1 + n) - (b + 1);for (int i = 1; i <= n; i++) {int p = lower_bound(b + 1, b + 1 + n1, a[i]) - b;update(root[i], root[i - 1], 1, n1, p, 1);}for (int i = 1; i <= m; i++) {int l, r, k;scanf("%d %d %d", &l, &r, &k);printf("%d\n", b[query(root[l - 1], root[r], 1, n1, k)]);}return 0;
}
P2048 [NOI2010] 超级钢琴
#include <bits/stdc++.h>using namespace std;typedef long long ll;struct Res {int value, id, k;Res(int _value = 0, int _id = 0, int _k = 0) : value(_value), id(_id), k(_k) {}bool operator < (const Res &t) const {return value < t.value;}
};const int N = 5e5 + 10;int a[N], b[N], root[N], n, k, L, R;int ls[N << 6], rs[N << 6], val[N << 6], tot;void update(int &rt, int pre, int l, int r, int x) {rt = ++tot, ls[rt] = ls[pre], rs[rt] = rs[pre], val[rt] = val[pre] + 1;if (l == r) {return ;}int mid = l + r >> 1;if (x <= mid) {update(ls[rt], ls[pre], l, mid, x);}else {update(rs[rt], rs[pre], mid + 1, r, x);}
}int query(int rtl, int rtr, int l, int r, int k) {if (l == r) {return l;}int sum = val[ls[rtr]] - val[ls[rtl]], mid = l + r >> 1;if (sum >= k) {return query(ls[rtl], ls[rtr], l, mid, k);}else {return query(rs[rtl], rs[rtr], mid + 1, r, k - sum);}
}priority_queue<Res> q;int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);scanf("%d %d %d %d", &n, &k, &L, &R);n += 1;for (int i = 2; i <= n; i++) {scanf("%d", &a[i]);a[i] += a[i - 1];b[i] = a[i];}sort(b + 1, b + 1 + n);int n1 = unique(b + 1, b + 1 + n) - (b + 1);for (int i = 1; i <= n; i++) {int p = lower_bound(b + 1, b + 1 + n1, a[i]) - b;update(root[i], root[i - 1], 1, n1, p);}for (int i = L + 1; i <= n; i++) {int l = max(1, i - R), r = i - L;int cur = a[i] - b[query(root[l - 1], root[r], 1, n1, 1)];q.push(Res(cur, i, 1));}ll ans = 0;for (int i = 1; i <= k; i++) {auto it = q.top();q.pop();ans += it.value;if (min(R - L + 1, it.id - L) == it.k) {continue;}int l = max(1, it.id - R), r = it.id - L;it.k++;int cur = a[it.id] - b[query(root[l - 1], root[r], 1, n1, it.k)];q.push(Res(cur, it.id, it.k));}printf("%lld\n", ans);return 0;
}
P2633 Count on a tree
主席树像是一种前缀和的数据结构,所以查询树上两点的第kkk大,可以通过询问sz[u]+sz[v]−sz[lca(u,v)]−sz[fa[lca(u,v)]]sz[u] + sz[v] - sz[lca(u, v)] - sz[fa[lca(u, v)]]sz[u]+sz[v]−sz[lca(u,v)]−sz[fa[lca(u,v)]]区间。
#include <bits/stdc++.h>using namespace std;const int N = 1e5 + 10;int head[N], to[N << 1], nex[N << 1], cnt = 1;int value[N], a[N], root[N], n, m, n1;int son[N], sz[N], fa[N], dep[N], top[N];int ls[N * 20], rs[N * 20], sum[N * 20], num;void add(int x, int y) {to[cnt] = y;nex[cnt] = head[x];head[x] = cnt++;
}void update(int &rt, int pre, int l, int r, int x) {rt = ++num, ls[rt] = ls[pre], rs[rt] = rs[pre], sum[rt] = sum[pre] + 1;if (l == r) {return ;}int mid = l + r >> 1;if (x <= mid) {update(ls[rt], ls[pre], l, mid, x);}else {update(rs[rt], rs[pre], mid + 1, r, x);}
}void dfs1(int rt, int f) {fa[rt] = f, dep[rt] = dep[f] + 1, sz[rt] = 1;int p = lower_bound(a + 1, a + 1 + n1, value[rt]) - a;update(root[rt], root[f], 1, n1, p);for (int i = head[rt]; i; i = nex[i]) {if (to[i] == f) {continue;}dfs1(to[i], rt);sz[rt] += sz[to[i]];if (!son[rt] || sz[son[rt]] < sz[to[i]]) {son[rt] = to[i];}}
}void dfs2(int rt, int tp) {top[rt] = tp;if (!son[rt]) {return ;}dfs2(son[rt], tp);for (int i = head[rt]; i; i = nex[i]) {if (to[i] == fa[rt] || to[i] == son[rt]) {continue;}dfs2(to[i], to[i]);}
}int lca(int x, int y) {while (top[x] != top[y]) {if (dep[top[x]] < dep[top[y]]) {swap(x, y);}x = fa[top[x]];}return dep[x] < dep[y] ? x : y;
}int query(int x, int y, int f, int ff, int l, int r, int k) {if (l == r) {return l;}int ans = sum[ls[x]] + sum[ls[y]] - sum[ls[f]] - sum[ls[ff]], mid = l + r >> 1;if (ans >= k) {return query(ls[x], ls[y], ls[f], ls[ff], l, mid, k);}else {return query(rs[x], rs[y], rs[f], rs[ff], mid + 1, r, k - ans);}
}int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);scanf("%d %d", &n, &m);for (int i = 1; i <= n; i++) {scanf("%d", &value[i]);a[i] = value[i];}for (int i = 1; i < n; i++) {int x, y;scanf("%d %d", &x, &y);add(x, y);add(y, x);}sort(a + 1, a + 1 + n);n1 = unique(a + 1, a + 1 + n) - (a + 1);dfs1(1, 0);dfs2(1, 1);int last = 0;for (int i = 1; i <= m; i++) {int u, v, k;scanf("%d %d %d", &u, &v, &k);u ^= last;int f = lca(u, v), ff = fa[f];last = query(root[u], root[v], root[f], root[ff], 1, n1, k);last = a[last];printf("%d\n", last);}return 0;
}
#1901. Zju2112 Dynamic Rankings
#include <bits/stdc++.h>using namespace std;const int N = 1e4 + 10, maxn = 1000000000;int a[N], n, m;int root[N], ls[N << 8], rs[N << 8], sum[N << 8], num;int A[N], B[N], cnt1, cnt2;inline int lowbit(int x) {return x & (-x);
}void update(int &rt, int l, int r, int x, int v) {if (!rt) {rt = ++num;}sum[rt] += v;if (l == r) {return ;}int mid = l + r >> 1;if (x <= mid) {update(ls[rt], l, mid, x, v);}else {update(rs[rt], mid + 1, r, x, v);}
}int query(int l, int r, int k) {if (l == r) {return l;}int cur = 0, mid = l + r >> 1;for (int i = 1; i <= cnt1; i++) {cur -= sum[ls[A[i]]];}for (int i = 1; i <= cnt2; i++) {cur += sum[ls[B[i]]];}if (cur >= k) {for (int i = 1; i <= cnt1; i++) {A[i] = ls[A[i]];}for (int i = 1; i <= cnt2; i++) {B[i] = ls[B[i]];}return query(l, mid, k);}else {for (int i = 1; i <= cnt1; i++) {A[i] = rs[A[i]];}for (int i = 1; i <= cnt2; i++) {B[i] = rs[B[i]];}return query(mid + 1, r, k - cur);}
}int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);scanf("%d %d", &n, &m);for (int i = 1; i <= n; i++) {scanf("%d", &a[i]);for (int j = i; j <= n; j += lowbit(j)) {update(root[j], 0, maxn, a[i], 1);}}for (int i = 1; i <= m; i++) {char op[2];int u, v, k;scanf("%s %d %d", op, &u, &v);if (op[0] == 'Q') {scanf("%d", &k);cnt1 = cnt2 = 0;for (int i = u - 1; i; i -= lowbit(i)) {A[++cnt1] = root[i];}for (int i = v; i; i -= lowbit(i)) {B[++cnt2] = root[i];}printf("%d\n", query(0, maxn, k));}else {for (int i = u; i <= n; i += lowbit(i)) {update(root[i], 0, maxn, a[u], -1);}a[u] = v;for (int i = u; i <= n; i += lowbit(i)) {update(root[i], 0, maxn, a[u], 1);}}}return 0;
}
P4175 [CTSC2008]网络管理
#include <bits/stdc++.h>using namespace std;const int N = 8e4 + 10, maxn = 100000000;int a[N], n, m;int head[N], to[N << 1], nex[N << 1], cnt = 1;int son[N], sz[N], l[N], r[N], dep[N], top[N], fa[N], tot;int root[N], sum[N << 8], ls[N << 8], rs[N << 8], num;int A[N], B[N], cnt1, cnt2;inline int lowbit(int x) {return x & (-x);
}inline void add(int x, int y) {to[cnt] = y;nex[cnt] = head[x];head[x] = cnt++;
}void update(int &rt, int l, int r, int x, int v) {if (!rt) {rt = ++num;}sum[rt] += v;if (l == r) {return ;}int mid = l + r >> 1;if (x <= mid) {update(ls[rt], l, mid, x, v);}else {update(rs[rt], mid + 1, r, x, v);}
}void dfs1(int rt, int f) {fa[rt] = f, l[rt] = ++tot, sz[rt] = 1, dep[rt] = dep[f] + 1;for (int i = l[rt]; i <= n; i += lowbit(i)) {update(root[i], 0, maxn, a[rt], 1);}for (int i = head[rt]; i; i = nex[i]) {if (to[i] == f) {continue;}dfs1(to[i], rt);sz[rt] += sz[to[i]];if (!son[rt] || sz[son[rt]] < sz[to[i]]) {son[rt] = to[i];}}r[rt] = tot;for (int i = r[rt] + 1; i <= n; i += lowbit(i)) {update(root[i], 0, maxn, a[rt], -1);}
}void dfs2(int rt, int tp) {top[rt] = tp;if (!son[rt]) {return ;}dfs2(son[rt], tp);for (int i = head[rt]; i; i = nex[i]) {if (to[i] == fa[rt] || to[i] == son[rt]) {continue;}dfs2(to[i], to[i]);}
}int lca(int x, int y) {while (top[x] != top[y]) {if (dep[top[x]] < dep[top[y]]) {swap(x, y);}x = fa[top[x]];}return dep[x] < dep[y] ? x : y;
}int query(int l, int r, int k) {if (l == r) {return l;}int cur = 0, mid = l + r >> 1;for (int i = 1; i <= cnt1; i++) {cur += sum[ls[A[i]]];}for (int i = 1; i <= cnt2; i++) {cur -= sum[ls[B[i]]];}if (cur >= k) {for (int i = 1; i <= cnt1; i++) {A[i] = ls[A[i]];}for (int i = 1; i <= cnt2; i++) {B[i] = ls[B[i]];}return query(l, mid, k);}else {for (int i = 1; i <= cnt1; i++) {A[i] = rs[A[i]];}for (int i = 1; i <= cnt2; i++) {B[i] = rs[B[i]];}return query(mid + 1, r, k - cur);}
}int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);scanf("%d %d", &n, &m);for (int i = 1; i <= n; i++) {scanf("%d", &a[i]);}for (int i = 1; i < n; i++) {int x, y;scanf("%d %d", &x, &y);add(x, y);add(y, x);}dfs1(1, 0);dfs2(1, 1);for (int i = 1; i <= m; i++) {int k, u, v;scanf("%d %d %d", &k, &u, &v);if (!k) {for (int i = l[u]; i <= n; i += lowbit(i)) {update(root[i], 0, maxn, a[u], -1);}for (int i = r[u] + 1; i <= n; i += lowbit(i)) {update(root[i], 0, maxn, a[u], 1);}a[u] = v;for (int i = l[u]; i <= n; i += lowbit(i)) {update(root[i], 0, maxn, a[u], 1);}for (int i = r[u] + 1; i <= n; i += lowbit(i)) {update(root[i], 0, maxn, a[u], -1);}}else {int f = lca(u, v), ff = fa[f], total = dep[u] + dep[v] - dep[f] - dep[ff];if (total < k) {puts("invalid request!");}else {cnt1 = cnt2 = 0;for (int i = l[u]; i; i -= lowbit(i)) {A[++cnt1] = root[i];}for (int i = l[v]; i; i -= lowbit(i)) {A[++cnt1] = root[i];}for (int i = l[f]; i; i -= lowbit(i)) {B[++cnt2] = root[i];}for (int i = l[ff]; i; i -= lowbit(i)) {B[++cnt2] = root[i];}k = total - k + 1;printf("%d\n", query(0, maxn, k));}}}return 0;
}
#279. [SYZOI Round1] 滑稽♂树
#include <bits/stdc++.h>using namespace std;const int N = 3e4 + 10, maxn = 10000;int head[N], to[N << 1], nex[N << 1], cnt = 1;int l[N], r[N], tot;int a[N], n, m;int root[N], ls[N << 8], rs[N << 8], sum[N << 8], num;int A[N], B[N], cnt1, cnt2;inline void add(int x, int y) {to[cnt] = y;nex[cnt] = head[x];head[x] = cnt++;
}inline int lowbit(int x) {return x & (-x);
}void update(int &rt, int l, int r, int x, int v) {if (!rt) {rt = ++num;}sum[rt] += v;if (l == r) {return; }int mid = l + r >> 1;if (x <= mid) {update(ls[rt], l, mid, x, v);}else {update(rs[rt], mid + 1, r, x, v);}
}void dfs(int rt, int fa) {l[rt] = ++tot;for (int i = l[rt]; i <= n; i += lowbit(i)) {update(root[i], 1, maxn, a[rt], 1);}for (int i = head[rt]; i; i = nex[i]) {if (to[i] == fa) {continue;}dfs(to[i], rt);}r[rt] = tot;
}int query1(int l, int r, int k) {if (l == r) {return l;}int cur = 0, mid = l + r >> 1;for (int i = 1; i <= cnt1; i++) {cur -= sum[ls[A[i]]];}for (int i = 1; i <= cnt2; i++) {cur += sum[ls[B[i]]];}if (cur >= k) {for (int i = 1; i <= cnt1; i++) {A[i] = ls[A[i]];}for (int i = 1; i <= cnt2; i++) {B[i] = ls[B[i]];}return query1(l, mid, k);}else {for (int i = 1; i <= cnt1; i++) {A[i] = rs[A[i]];}for (int i = 1; i <= cnt2; i++) {B[i] = rs[B[i]];}return query1(mid + 1, r, k - cur);}
}int query2(int l, int r, int L, int R) {if (l >= L && r <= R) {int res = 0;for (int i = 1; i <= cnt1; i++) {res -= sum[A[i]];}for (int i = 1; i <= cnt2; i++) {res += sum[B[i]];}return res;}int mid = l + r >> 1, res = 0;int A1[100], B1[100];if (L <= mid) {for (int i = 1; i <= cnt1; i++) {A1[i] = A[i];A[i] = ls[A[i]];}for (int i = 1; i <= cnt2; i++) {B1[i] = B[i];B[i] = ls[B[i]];}res += query2(l, mid, L, R);for (int i = 1; i <= cnt1; i++) {A[i] = A1[i];}for (int i = 1; i <= cnt2; i++) {B[i] = B1[i];}}if (R > mid) {for (int i = 1; i <= cnt1; i++) {A1[i] = A[i];A[i] = rs[A[i]];}for (int i = 1; i <= cnt2; i++) {B1[i] = B[i];B[i] = rs[B[i]];}res += query2(mid + 1, r, L, R);for (int i = 1; i <= cnt1; i++) {A[i] = A1[i];}for (int i = 1; i <= cnt2; i++) {B[i] = B1[i];}}return res;
}int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);scanf("%d", &n);for (int i = 1; i <= n; i++) {scanf("%d", &a[i]);}for (int i = 1; i < n; i++) {int x, y;scanf("%d %d", &x, &y);add(x, y);add(y, x);}dfs(1, 0);scanf("%d", &m);for (int i = 1; i <= m; i++) {int op, u, x, y;scanf("%d %d %d", &op, &u, &x);if (op == 1) {cnt1 = cnt2 = 0;for (int i = l[u] - 1; i ; i -= lowbit(i)) {A[++cnt1] = root[i];}for (int i = r[u]; i; i -= lowbit(i)) {B[++cnt2] = root[i];}printf("%d\n", query1(1, maxn, x));}else if (op == 2) {scanf("%d", &y);cnt1 = cnt2 = 0;for (int i = l[u] - 1; i ; i -= lowbit(i)) {A[++cnt1] = root[i];}for (int i = r[u]; i; i -= lowbit(i)) {B[++cnt2] = root[i];}printf("%d\n", query2(1, maxn, x, y));}else {for (int i = l[u]; i <= n; i += lowbit(i)) {update(root[i], 1, maxn, a[u], -1);}a[u] = x;for (int i = l[u]; i <= n; i += lowbit(i)) {update(root[i], 1, maxn, a[u], 1);}}}return 0;
}
Sequence II
题目大意是有mmm次询问,每次询问一段区间[l,r][l, r][l,r],从左到右,如果这个数是在这个区间第一次出现,则记录下其下标,
我们会得到一个新的数组,要求这个数组的中位数是什么。
考虑使用主席树来写,我们倒序插入这些数的信息并记录下这个数上一次是出现在哪,对于每个点我们将其下标在主席树中+1+1+1,然后对于上一次出现的下标,在主席树中−1-1−1,所以我们直接在第lll棵主席树中查询区间[l,r][l, r][l,r]的权值即是这个区间的不同数字个数,在这个区间里,对于相同的数字,我们只会认定最右边的那个一个数字,所以重复的数字会消除。
然后再在第lll棵主席树中查找第kkk大即可得到答案了。
#include <bits/stdc++.h>using namespace std;const int N = 2e5 + 10;int a[N], pos[N], n, m, ans;int root[N], ls[N << 6], rs[N << 6], sum[N << 6], num;void update(int &rt, int pre, int l, int r, int x, int v) {rt = ++num, ls[rt] = ls[pre], rs[rt] = rs[pre], sum[rt] = sum[pre] + v;if (l == r) {return ;}int mid = l + r >> 1;if (x <= mid) {update(ls[rt], ls[pre], l, mid, x, v);}else {update(rs[rt], rs[pre], mid + 1, r, x, v);}
}int query_sum(int rt, int l, int r, int L, int R) {if (!rt) {return 0;}if (l >= L && r <= R) {return sum[rt];}int mid = l + r >> 1, ans = 0;if (L <= mid) {ans += query_sum(ls[rt], l, mid, L, R);}if (R > mid) {ans += query_sum(rs[rt], mid + 1, r, L, R);}return ans;
}int query_k(int rt, int l, int r, int k) {if (l == r) {return l;}int cur = sum[ls[rt]], mid = l + r >> 1;if (cur >= k) {return query_k(ls[rt], l, mid, k);}else {return query_k(rs[rt], mid + 1, r, k - cur);}
}int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);int T;scanf("%d", &T);for (int cas = 1; cas <= T; cas++) {printf("Case #%d:", cas);memset(pos, 0, sizeof pos);for (int i = 0; i <= n; i++) {root[i] = 0;}for (int i = 0; i <= num; i++) {ls[i] = rs[i] = sum[i] = 0;}num = ans = 0;scanf("%d %d", &n, &m);for (int i = 1; i <= n; i++) {scanf("%d", &a[i]);}for (int i = n; i >= 1; i--) {update(root[i], root[i + 1], 1, n, i, 1);if (pos[a[i]]) {update(root[i], root[i], 1, n, pos[a[i]], -1);}pos[a[i]] = i;}for (int i = 1; i <= m; i++) {int l, r;scanf("%d %d", &l, &r);l = (l + ans) % n + 1, r = (r + ans) % n + 1;if (l > r) {swap(l, r);}int sum = query_sum(root[l], 1, n, l, r);ans = query_k(root[l], 1, n, sum + 1 >> 1);printf(" %d", ans);}puts("");}return 0;
}