题目
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN = 100009;
int n, q, cnt, sz, Max[MAXN * 20], T[MAXN * 20], ls[MAXN * 20], rs[MAXN * 20];
void pushup(int rt) { Max[rt] = max(Max[ls[rt]], Max[rs[rt]]); }
void build(int &rt, int l, int r)
{
rt = ++sz;
if (l == r) {
scanf("%d", &Max[rt]);
return;
}
int mid = (l + r) >> 1;
build(ls[rt], l, mid);
build(rs[rt], mid + 1, r);
pushup(rt);
}
int query(int rt, int l, int r, int ql, int qr)
{
if (ql == l && qr == r) return Max[rt];
int mid = (l + r) >> 1;
if (qr <= mid) return query(ls[rt], l, mid, ql, qr);
else if (ql > mid) return query(rs[rt], mid + 1, r, ql, qr);
else return max(query(ls[rt], l, mid, ql, mid), query(rs[rt], mid + 1, r, mid + 1, qr));
}
void update(int &rt, int fa, int l, int r, int id, int v)
{
rt = ++sz;
Max[rt] = Max[fa];
if (l == r) {
Max[rt] = v;
return;
}
ls[rt] = ls[fa];
rs[rt] = rs[fa];
int mid = (l + r) >> 1;
if (id <= mid) update(ls[rt], ls[fa], l, mid, id, v);
else update(rs[rt], rs[fa], mid + 1, r, id, v);
pushup(rt);
}
int main()
{
sz = cnt = 0;
scanf("%d %d", &n, &q);
build(T[++cnt], 1, n);
while (q--) {
int op, x, y, z;
scanf("%d %d %d %d", &op, &x, &y, &z);
if (op == 0) {
int ans = query(T[x], 1, n, y, z);
printf("%d
", ans);
}
else {
update(T[++cnt], T[x], 1, n, y, z);
}
}
return 0;
}
题目
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 100009;
int t, n, m, a[MAXN], b[MAXN], sz, sum[MAXN * 20], ls[MAXN * 20], rs[MAXN * 20], T[MAXN * 20];
void pushup(int rt) { sum[rt] = sum[ls[rt]] + sum[rs[rt]]; }
/*
void build(int &rt, int l, int r)
{
rt = ++sz;
sum[rt] = 0;
if (l == r) return;
int mid = (l + r) >> 1;
build(ls[rt], l, mid);
build(rs[rt], mid + 1, r);
}
*/
void update(int &rt, int fa, int l, int r, int id)
{
rt = ++sz;
sum[rt] = sum[fa] + 1;
if (l == r) return;
ls[rt] = ls[fa];
rs[rt] = rs[fa];
int mid = (l + r) >> 1;
if (id <= mid) update(ls[rt], ls[fa], l, mid, id);
else update(rs[rt], rs[fa], mid + 1, r, id);
pushup(rt);
}
int query(int u, int v, int l, int r, int z)
{
if (l == r) return l;
int tmp = sum[ls[v]] - sum[ls[u]];
int mid = (l + r) >> 1;
if (z <= tmp) {
return query(ls[u], ls[v], l, mid, z);
}
else return query(rs[u], rs[v], mid + 1, r, z - tmp);
}
int main()
{
scanf("%d", &t);
while (t--) {
sz = 0;
scanf("%d %d", &n, &m);
memset(sum, 0, sizeof(sum));
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
b[i] = a[i];
}
sort(a + 1, a + 1 + n);
int nn = unique(a + 1, a + 1 + n) - a - 1;
//build(T[0], 1, nn);
for (int i = 1; i <= n; ++i) {
int id = lower_bound(a + 1, a + 1 + nn, b[i]) - a;
update(T[i], T[i - 1], 1, nn, id);
}
for (int i = 1; i <= m; ++i) {
int x, y, z;
scanf("%d %d %d", &x, &y, &z);
int id = query(T[x - 1], T[y], 1, nn, z);
printf("%d
", a[id]);
}
}
return 0;
}