BZOJ 3110: [Zjoi2013]K大数查询
解法一:整体二分
用支持区间加区间查询的bit可降低时空常数
#include <bits/stdc++.h>
#define mid ((l + r) >> 1)
#define lp p << 1
#define rp p << 1 | 1
#define ll long long
char buf[1 << 21], *p1 = buf, *p2 = buf;
inline int getc() {
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++;
}
inline int read() {
int x = 0, f = 1; char ch = getc();
while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getc(); }
while (ch >= '0' && ch <= '9') x = x * 10LL + ch - 48LL, ch = getc();
return x * f;
}
const int N = 3e5 + 7;
struct P {
int l, r, id;
ll k;
inline void in() {
l = read(), r = read(), k = (ll)read();
}
} q[N], q1[N], q2[N];
int n, m, tol, cnt;
int v[N], ans[N];
int c1[N];
ll c2[N];
inline int lowbit(int x) { return x & -x; }
inline void add(int x, int v) {
for (int i = x; i <= n; i += lowbit(i)) {
c1[i] += v;
c2[i] += 1LL * x * v;
}
}
inline void add(int l, int r, int v) {
add(l, v); add(r + 1, -v);
}
inline ll query(int x) {
ll ans = 0;
for (int i = x; i; i -= lowbit(i)) {
ans += 1LL * (x + 1) * c1[i] - c2[i];
}
return ans;
}
inline ll query(int l, int r) {
return query(r) - query(l - 1);
}
void solve(int l, int r, int L, int R) {
if (l > r || L > R) return;
if (l == r) {
for (int i = L; i <= R; i++)
if (q[i].id)
ans[q[i].id] = v[l];
return;
}
int cnt1 = 0, cnt2 = 0;
for (int i = L; i <= R; i++) {
if (q[i].id) {
ll t = query(q[i].l, q[i].r);
if (q[i].k > t) q[i].k -= t, q2[++cnt2] = q[i];
else q1[++cnt1] = q[i];
} else {
if (q[i].k >= v[mid]) add(q[i].l, q[i].r, 1), q1[++cnt1] = q[i];
else q2[++cnt2] = q[i];
}
}
for (int i = 1; i <= cnt1; i++)
if (!q1[i].id) add(q1[i].l, q1[i].r, -1);
for (int i = 1; i <= cnt1; i++) q[L + i - 1] = q1[i];
for (int i = 1; i <= cnt2; i++) q[L + cnt1 + i - 1] = q2[i];
solve(l, mid, L, L + cnt1 - 1); solve(mid + 1, r, L + cnt1, R);
}
int main() {
n = read(), m = read();
for (int i = 1; i <= m; i++) {
int opt = read();
q[i].in();
if (opt == 2) q[i].id = ++tol;
else v[++cnt] = q[i].k;
}
std::sort(v + 1, v + 1 + cnt);
cnt = std::unique(v + 1, v + 1 + cnt) - v - 1;
std::reverse(v + 1, v + 1 + cnt);
solve(1, cnt, 1, m);
for (int i = 1; i <= tol; i++)
printf("%d\n", ans[i]);
return 0;
}
解法二:线段树套线段树
外层权值线段树,内层线段树维护每个权值的区间出现在哪些区间
两个线段树都标记永久化一下就很好写
#include <bits/stdc++.h>
#define mid ((l + r) >> 1)
#define ll long long
char buf[1 << 21], *p1 = buf, *p2 = buf;
inline char getc() {
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++;
}
inline int read() {
int x = 0, f = 1; char ch = getc();
while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getc(); }
while (ch >= '0' && ch <= '9') { x = x * 10 + ch - 48LL; ch = getc(); }
return x * f;
}
const int N = 5e4 + 7;
int n, m, root[N * 4], cnt, v[N];
struct P {
int opt, l, r, k;
void in() {
opt = read(); l = read(); r = read(); k = read();
}
} p[N];
struct Seg {
#define lp tree[p].l
#define rp tree[p].r
struct Node {
int l, r;
ll sum, tag;
} tree[N * 200];
int tol;
void update(int &p, int l, int r, int x, int y) {
if (!p) p = ++tol;
tree[p].sum += 1LL * (std::min(y, r) - std::max(l, x) + 1);
if (x <= l && y >= r) {
tree[p].tag++;
return;
}
if (x <= mid) update(lp, l, mid, x, y);
if (y > mid) update(rp, mid + 1, r, x, y);
}
ll query(int p, int l, int r, int x, int y) {
if (!p) return 0;
if (x <= l && y >= r) return tree[p].sum;
ll ans = tree[p].tag * (std::min(y, r) - std::max(l, x) + 1);
if (x <= mid) ans += query(lp, l, mid, x, y);
if (y > mid) ans += query(rp, mid + 1, r, x, y);
return ans;
}
#undef lp
#undef rp
} seg;
#define lp p << 1
#define rp p << 1 | 1
void update(int p, int l, int r, int pos, int x, int y) {
seg.update(root[p], 1, n, x, y);
if (l == r) return;
if (pos <= mid) update(lp, l, mid, pos, x, y);
else update(rp, mid + 1, r, pos, x, y);
}
int query(int p, int l, int r, int x, int y, int k) {
if (l == r) return l;
ll sum = seg.query(root[rp], 1, n, x, y);
if (sum >= k) return query(rp, mid + 1, r, x, y, k);
return query(lp, l, mid, x, y, k - sum);
}
int main() {
n = read(), m = read();
for (int i = 1; i <= m; i++) {
p[i].in();
if (p[i].opt == 1) v[++cnt] = p[i].k;
}
std::sort(v + 1, v + 1 + cnt);
cnt = std::unique(v + 1, v + 1 + cnt) - v - 1;
for (int i = 1; i <= m; i++) {
if (p[i].opt == 1) {
update(1, 1, cnt, std::lower_bound(v + 1, v + 1 + cnt, p[i].k) - v, p[i].l, p[i].r);
} else {
printf("%d\n", v[query(1, 1, cnt, p[i].l, p[i].r, p[i].k)]);
}
}
return 0;
}