# 主席树几道题目

1134人阅读 评论(0)

Poj2104 主席树静态区间第k值

const int MAXN = 100010;
const int MOD = 1000000;
int a[MAXN], t[MAXN];
int T[MAXN * 30], lson[MAXN * 30],rson[MAXN * 30], c[MAXN * 30];
int n, sz, q;
int tot;
void init_hash()
{
FE(i, 1, n) t[i] = a[i];
sort(t + 1, t + 1 + n);
sz = unique(t + 1, t + 1 + n) - t - 1;
}
int build(int l, int r)
{
int rt = tot++;
c[rt] = 0;
if (l != r)
{
int m = (l + r) >> 1;
lson[rt] = build(l, m);
rson[rt] = build(m + 1, r);
}
return rt;
}

int hash(int x)
{
returnlower_bound(t + 1, t + 1 + n, x) - t;
}

int update(int rt, int pos, int val)
{
int new_rt = tot++;
int tmp = new_rt;
c[new_rt] = c[rt] + val;
int l = 1, r = sz;
while (l < r)
{
int m = (l + r) >> 1;
if (pos <= m)
{
lson[new_rt] = tot++; rson[new_rt] = rson[rt];
new_rt = lson[new_rt]; rt = lson[rt];
r = m;
}
else
{
rson[new_rt] = tot++; lson[new_rt] = lson[rt];
new_rt = rson[new_rt]; rt = rson[rt];
l = m + 1;
}
c[new_rt] = c[rt] + val;
}
return tmp;
}

int query(int L, int  R, int k)
{
int l = 1, r = sz;
while (l < r)
{
int m = (l + r) >> 1;
if (c[lson[L]] - c[lson[R]] >= k)
{
L = lson[L]; R = lson[R];
r = m;
}
else
{
k -= c[lson[L]] - c[lson[R]];///!!!
L = rson[L]; R = rson[R];
l = m + 1;
}
}
return l;
}

int main()
{
int x, y, z;
while (cin >> n >> q)
{
tot = 0;
FE(i, 1, n) RI(a[i]);
init_hash();
T[n + 1] = build(1, sz);///!!!
FED(i, n, 1)
{
int pos = hash(a[i]);
T[i] = update(T[i + 1], pos, 1);
}
while (q--)
{
RIII(x, y, z);
printf("%d\n", t[query(T[x], T[y + 1], z)]);
}
}
}

spoj EQUERY 静态区间，区间中不同值的数的个数

const int MAXN = 100010;
const int MOD = 1000000;

int a[MAXN], t[MAXN];
int T[MAXN * 60], lson[MAXN * 60],rson[MAXN * 60], c[MAXN * 60];
int n, q;
int tot;
map<int, int> mp;

int build(int l, int r)
{
int rt = tot++;
c[rt] = 0;
if (l != r)
{
int m = (l + r) >> 1;
lson[rt] = build(l, m);
rson[rt] = build(m + 1, r);
}
return rt;
}

int update(int rt, int pos, int val)
{
int new_rt = tot++;
int tmp = new_rt;
c[new_rt] = c[rt] + val;
int l = 1, r = n;
while (l < r)
{
int m = (l + r) >> 1;
if (pos <= m)
{
lson[new_rt] = tot++; rson[new_rt] = rson[rt];
new_rt = lson[new_rt]; rt = lson[rt];
r = m;
}
else
{
rson[new_rt] = tot++; lson[new_rt] = lson[rt];
new_rt = rson[new_rt]; rt = rson[rt];
l = m + 1;
}
c[new_rt] = c[rt] + val;
}
return tmp;
}

int query(int rt, int pos)
{
int ans = 0;
int l = 1, r = n;
while (l < r)///(pos < r)
{
int m = (l + r) >> 1;

if (pos <= m)
{
rt = lson[rt];
r = m;
}
else
{
ans += c[lson[rt]];
rt = rson[rt];
l = m + 1;
}
}
return ans + c[rt];
}

int main()
{
int x, y, z;
while (cin >> n)
{
tot = 0;
FE(i, 1, n) RI(a[i]);
mp.clear();
T[n + 1] = build(1, n);
FED(i, n, 1)
{
if (mp.find(a[i]) == mp.end())
{
T[i] = update(T[i + 1], i, 1);
}
else
{
int tmp = update(T[i + 1],mp[a[i]], -1);
T[i] = update(tmp, i, 1);
}
mp[a[i]] = i;
}
cin >> q;
while (q--)
{
RII(x, y);
printf("%d\n", query(T[x], y));
}
}
}


### zoj 2112 Dynamic Rankings 动态第k值，树状数组套线段树

、、、

2
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：108904次
• 积分：2303
• 等级：
• 排名：第16244名
• 原创：121篇
• 转载：4篇
• 译文：0篇
• 评论：13条
评论排行
最新评论