H - Hello Ms. Ze
给定nnn种不同的材料,第iii种材料有aia_iai个,有mmm个操作,操作分为两类:
- 把第xxx种材料修改为yyy个,
- 只用[l,r][l, r][l,r]区间的材料制作衣服,每件衣服要用kkk个不同的材料,最多能做多少件,
先不考虑修改操作,对于区间[l,r][l, r][l,r]中的原料,我们可以做最多多少衣服,如何求解,
考虑二分答案,假设我们可以最多做xxx件衣服,显然对于个数大于等于xxx的材料,在每件衣服中我们只能使用一次,
假设我们当前二分的区间为[l,r][l, r][l,r],设材料数量大于midmidmid的有aaa种,材料数量小于等于midmidmid的数量和为sumsumsum,
当a+summid+1≥ka + \frac{sum}{mid + 1} \geq ka+mid+1sum≥k,说明答案在右区间,否则答案在左区间,其实这整个过程我们可以在线段树上直接二分执行。
考虑带修,树状数组套主席树,然后主席树上二分即可,由于答案值域在[1,105×106][1, 10 ^ 5 \times 10 ^ 6][1,105×106],所以我直接动态开点建了一颗值域在[1,1<<37][1, 1 << 37][1,1<<37]的主席树。
树套树
#include <bits/stdc++.h>using namespace std;const int N = 1e5 + 10;const long long maxn = 1ll << 37;int a[N], n, m;int root[N], ls[N << 9], rs[N << 9], tot[N << 9], num;long long sum[N << 9];void update(int &rt, long long l, long long r, int x, int v) {if (!rt) {rt = ++num;}sum[rt] += x * v, tot[rt] += v;if (l == r) {return ;}long long mid = l + r >> 1;if (x <= mid) {update(ls[rt], l, mid, x, v);}else {update(rs[rt], mid + 1, r, x, v);}
}inline int lowbit(int x) {return x & (-x);
}void update(int pos, int x, int v) {while (pos <= n) {update(root[pos], 1, maxn, x, v);pos += lowbit(pos);}
}int A[50], B[50], cnt1, cnt2;long long query(long long l, long long r, long long S, int cnt, int k) {if (l == r) {return l;}long long mid = l + r >> 1;long long ans = 0, res = 0;for (int i = 1; i <= cnt1; i++) {ans -= sum[ls[A[i]]];res -= tot[ls[A[i]]];}for (int i = 1; i <= cnt2; i++) {ans += sum[ls[B[i]]];res += tot[ls[B[i]]];}if ((S + ans) / (mid + 1) + cnt - res >= k) {S += ans, cnt -= res;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, S, cnt, k);}else {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, S, cnt, k);}
}int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);scanf("%d %d", &n, &m);for (int i = 1; i <= n; i++) {scanf("%d", &a[i]);update(i, a[i], 1);}for (int i = 1, op, l, r, k; i <= m; i++) {scanf("%d %d %d", &op, &l, &r);if (op == 2) {update(l, a[l], -1);a[l] = r;update(l, a[l], 1);}else {scanf("%d", &k);cnt1 = cnt2 = 0;for (int j = l - 1; j; j -= lowbit(j)) {A[++cnt1] = root[j];}for (int j = r; j; j -= lowbit(j)) {B[++cnt2] = root[j];}printf("%lld\n", query(1, maxn, 0, r - l + 1, k));}}return 0;
}