2. 权值线段树

区间线段树中 $ [x,x] $ 所对应的节点指原数组中 $ x $ 号节点的值,而权值线段树中 $ [x,x] $ 所对应的节点指原数组中等于 $ x $ 的元素的个数。一般情况下,权值线段树的非叶子节点记录和。

在权值线段树上可以进行二分,以此查找整个区间内第 $ k $ 大的节点,方法简单,略过。

3. 动态开点线段树

当一棵线段树中只需访问没几个节点时,我们不再默认节点 $ x $ 的左儿子是 $ 2x $ ,右儿子是 $ 2x+1 $ ,而是对每个节点分别存储左儿子和右儿子。

4.可持久化线段树(主席树)

假设我们已经有了一棵线段树,现在我们需要更改一个节点的权值但还想保留原来的数据。这时我们似乎只能复制一遍了,但细观下图我们发现只改了 $ lg\text{ }n $ 个节点。

所以我们实际上可以这样存:

那么一次修改只需新建 $ lg\text{ }n $ 个节点就可以了。这就是可持久化线段树,非常简单。

如果这是一棵权值线段树,然后依次插入 $ n $ 个元素,取第 $ r $ 棵树和第 $ l-1 $ 棵树相减即可得出区间 $ l-r $ 的权值线段树,二分即可ACP3834

Tips: 初始的空树可以是一个标号为 $ 0 $ 的节点,它的左儿子是 $ 0 $ ,右儿子也是 $ 0 $ ,权值还是 $ 0 $ 。这样就 $ O(1) $ 建出了一棵 $ \infty $ 的树。


于是就可以给出代码了:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int Array_Limit=200005;
int N,M,m,a[Array_Limit<<5],d[Array_Limit<<5],root[Array_Limit<<5],record[Array_Limit<<5],ch[Array_Limit<<5][2],nodenum;
void add(int l,int r,int from,int to,int data){
if(l==r){
record[to]=record[from]+1;
return;
}
int mid=(l+r)>>1;
if(data<=mid){
ch[to][0]=++nodenum;
ch[to][1]=ch[from][1];
add(l,mid,ch[from][0],ch[to][0],data);
}
else{
ch[to][1]=++nodenum;
ch[to][0]=ch[from][0];
add(mid+1,r,ch[from][1],ch[to][1],data);
}
record[to]=record[ch[to][0]]+record[ch[to][1]];
}
int find(int l,int r,int x,int y,int k){
if(l==r)
return l;
int mid=(l+r)>>1;
if(record[ch[y][0]]-record[ch[x][0]]>=k)
return find(l,mid,ch[x][0],ch[y][0],k);
else return find(mid+1,r,ch[x][1],ch[y][1],k-(record[ch[y][0]]-record[ch[x][0]]));
}
void print2(int p1,int p2){
if(p1==0&&p2==0)
return;
printf("%d(",record[p2]-record[p1]);
print2(ch[p1][0],ch[p2][0]);
printf(",");
print2(ch[p1][1],ch[p2][1]);
printf(")");
}
void print(int p){
if(p==0)
return;
printf("%d:%d(",p,record[p]);
print(ch[p][0]);
printf(",");
print(ch[p][1]);
printf(")");
}
int main(){
scanf("%d%d",&N,&M);
for(int i=1;i<=N;i++)
scanf("%d",d+i);
memcpy(a,d,sizeof(a));
sort(a+1,a+N+1);
m=unique(a+1,a+N+1)-a-1;
for(int i=1,l,r,mid;i<=N;i++){
l=1;
r=m;
while(l<=r){
mid=(l+r)>>1;
if(a[mid]<d[i])
l=mid+1;
else if(a[mid]>d[i])
r=mid-1;
else if(a[mid]==d[i])
break;
}
d[i]=mid;
}
root[0]=ch[0][0]=ch[0][1]=0;
for(int i=1;i<=N;i++){
root[i]=++nodenum;
add(1,m,root[i-1],root[i],d[i]);
// print(root[i]);
// printf("\n");
}
for(int i=1,x,y,k;i<=M;i++){
scanf("%d%d%d",&x,&y,&k);
// print2(root[x-1],root[y]);
printf("%d\n",a[find(1,m,root[x-1],root[y],k)]);
}
return 0;
}

把它改成区间线段树,有可以AC两道紫题:

P3919

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include<stdio.h>
#include<algorithm>
using namespace std;
const int Array_Limit=1000005;
int N,M,a[Array_Limit<<5],root[Array_Limit<<5],record[Array_Limit<<5],ch[Array_Limit<<5][2],nodenum;
void build(int l,int r,int p){
if(l==r){
record[p]=a[l];
return;
}
int mid=(l+r)>>1;
build(l,mid,ch[p][0]=++nodenum);
build(mid+1,r,ch[p][1]=++nodenum);
record[p]=max(record[ch[p][0]],record[ch[p][1]]);
}
void change(int l,int r,int from,int to,int k,int data){
if(l==r){
record[to]=data;
return;
}
int mid=(l+r)>>1;
if(k<=mid){
ch[to][0]=++nodenum;
ch[to][1]=ch[from][1];
change(l,mid,ch[from][0],ch[to][0],k,data);
}
else{
ch[to][1]=++nodenum;
ch[to][0]=ch[from][0];
change(mid+1,r,ch[from][1],ch[to][1],k,data);
}
record[to]=max(record[ch[to][0]],record[ch[to][1]]);
}
int find(int l,int r,int v,int k){
if(l==r)
return record[v];
int mid=(l+r)>>1;
if(k<=mid)
return find(l,mid,ch[v][0],k);
else return find(mid+1,r,ch[v][1],k);
}
void print(int p){
if(p==0)
return;
printf("%d:%d(",p,record[p]);
print(ch[p][0]);
printf(",");
print(ch[p][1]);
printf(")");
}
int main(){
scanf("%d%d",&N,&M);
for(int i=1;i<=N;i++)
scanf("%d",a+i);
build(1,N,root[0]=++nodenum);
for(int i=1,v,op,loc,data;i<=M;i++){
scanf("%d%d%d",&v,&op,&loc);
if(op==1){
scanf("%d",&data);
change(1,N,root[v],root[i]=++nodenum,loc,data);
}
else printf("%d\n",find(1,N,root[i]=root[v],loc));
}
return 0;
}

P3402

tips: 启发式合并,讲并查集时在讲

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
const int Array_Limit=200005;
int N,M,a[Array_Limit<<5],root[Array_Limit<<5],ch[Array_Limit<<5][2],nodenum;
struct node{
int fa,dep;
};
node record[Array_Limit<<5];
void build(int l,int r,int p){
if(l==r){
record[p].fa=a[l];
record[p].dep=1;
return;
}
int mid=(l+r)>>1;
build(l,mid,ch[p][0]=++nodenum);
build(mid+1,r,ch[p][1]=++nodenum);
record[p].fa=max(record[ch[p][0]].fa,record[ch[p][1]].fa);
record[p].dep=max(record[ch[p][0]].dep,record[ch[p][1]].dep);
}
void change(int l,int r,int from,int to,int k,node data){
if(l==r){
record[to].fa=data.fa;
record[to].dep=max(record[to].dep,1);
return;
}
int mid=(l+r)>>1;
if(k<=mid){
ch[to][0]=++nodenum;
ch[to][1]=ch[from][1];
change(l,mid,ch[from][0],ch[to][0],k,data);
}
else{
ch[to][1]=++nodenum;
ch[to][0]=ch[from][0];
change(mid+1,r,ch[from][1],ch[to][1],k,data);
}
record[to].fa=max(record[ch[to][0]].fa,record[ch[to][1]].fa);
record[to].dep=max(record[ch[to][0]].dep,record[ch[to][1]].dep);
}
node find(int l,int r,int v,int k){
if(l==r)
return record[v];
int mid=(l+r)>>1;
if(k<=mid)
return find(l,mid,ch[v][0],k);
else return find(mid+1,r,ch[v][1],k);
}
void print(int p){
if(p==0)
return;
printf("%d:%d(",p,record[p].fa);
print(ch[p][0]);
printf(",");
print(ch[p][1]);
printf(")");
}
int main(){
srand(time(NULL));
scanf("%d%d",&N,&M);
for(int i=1;i<=N;i++)
a[i]=i;
build(1,N,root[0]=++nodenum);
node a,b;
for(int i=1,op;i<=M;i++){
scanf("%d",&op);
if(op==1){
scanf("%d%d",&a.fa,&b.fa);
while(a.fa!=find(1,N,root[i-1],a.fa).fa)
a=find(1,N,root[i-1],a.fa);
while(b.fa!=find(1,N,root[i-1],b.fa).fa)
b=find(1,N,root[i-1],b.fa);
if(a.dep<b.dep){
a.dep=max(a.dep-1,b.dep)+1;
change(1,N,root[i-1],root[i]=++nodenum,b.fa,a);
}
else{
b.dep=max(a.dep,b.dep-1)+1;
change(1,N,root[i-1],root[i]=++nodenum,a.fa,b);
}
}
else if(op==2){
int a;
scanf("%d",&a);
root[i]=root[a];
}
else{
scanf("%d%d",&a.fa,&b.fa);
root[i]=root[i-1];
while(a.fa!=find(1,N,root[i],a.fa).fa)
a=find(1,N,root[i],a.fa);
while(b.fa!=find(1,N,root[i],b.fa).fa)
b=find(1,N,root[i],b.fa);
printf("%d\n",(a.fa==b.fa)?1:0);
}
}
return 0;
}