树链剖分


树链剖分

模板

在这里插入图片描述

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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
#define int long long
const int N = 100010 , M=N*2;
int n,m;
int w[N],h[N],ne[M],e[M],idx;
int id[N],nw[N],cnt;
int dep[N],sz[N],top[N],fa[N],son[N];

struct Tree{
int l,r;
int add,sum;
}tr[N*4];

void add(int a, int b)
{
e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
}

void dfs1(int u,int father,int depth){
dep[u]=depth,fa[u]=father,sz[u]=1;
for(int i=h[u];~i;i=ne[i]){
int j=e[i];
if(j==father)continue;
dfs1(j,u,depth+1);
sz[u]+=sz[j];
if(sz[son[u]<sz[j]])son[u]=j;
}
}

void dfs2(int u,int t){
id[u]=++cnt,nw[cnt]=w[u],top[u]=t;
if(!son[u])return;
dfs2(son[u],t);
for(int i=h[u];~i;i=ne[i]){
int j=e[i];
if(j==fa[u]||j==son[u])continue;
dfs2(j,j);
}
}

void pushup(int u){
tr[u].sum=tr[u<<1].sum+tr[u<<1|1].sum;
}

void pushdown(int u){
auto &root=tr[u], &left=tr[u<<1], &right=tr[u<<1|1];
if(root.add){
left.add+=root.add;
left.sum+=root.add*(left.r-left.l+1);
right.add+=root.add;
right.sum+=root.add*(right.r-right.l+1);
root.add=0;
}
}

void build(int u,int l,int r){
tr[u]={l,r,0,nw[l]};
if(l==r)return;
int mid=l+r>>1;
build(u<<1,l,mid),build(u<<1|1,mid+1,r);
pushup(u);
}

void update(int u,int l,int r,int k){
if(l<=tr[u].l&&r>=tr[u].r){
tr[u].add+=k;
tr[u].sum+=k*(tr[u].r-tr[u].l+1);
return;
}
pushdown(u);
int mid=tr[u].l+tr[u].r>>1;
if(l<=mid)update(u<<1,l,r,k);
if(r>mid)update(u<<1|1,l,r,k);
pushup(u);
}

int query(int u,int l,int r){
if(l<=tr[u].l&&tr[u].r<=r)return tr[u].sum;
pushdown(u);
int mid=tr[u].l+tr[u].r>>1;
int ans=0;
if(l<=mid)ans+=query(u<<1,l,r);
if(r>mid)ans+=query(u<<1|1,l,r);
return ans;
}

void update_path(int u, int v, int k)
{
while (top[u] != top[v])
{
if (dep[top[u]] < dep[top[v]]) swap(u, v);
update(1, id[top[u]], id[u], k);
u = fa[top[u]];
}
if (dep[u] < dep[v]) swap(u, v);
update(1, id[v], id[u], k);
}

int query_path(int u, int v)
{
int res = 0;
while (top[u] != top[v])
{
if (dep[top[u]] < dep[top[v]]) swap(u, v);
res += query(1, id[top[u]], id[u]);
u = fa[top[u]];
}
if (dep[u] < dep[v]) swap(u, v);
res += query(1, id[v], id[u]);
return res;
}

void update_tree(int u, int k)
{
update(1, id[u], id[u] + sz[u] - 1, k);
}

int query_tree(int u)
{
return query(1, id[u], id[u] + sz[u] - 1);
}



signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
memset(h,-1,sizeof h);
cin>>n;
for(int i=1;i<=n;i++)cin>>w[i];
for(int i=1;i<n;i++){
int a,b;
cin>>a>>b;
add(a,b),add(b,a);
}
dfs1(1,-1,1);
dfs2(1,1);
build(1,1,n);
cin>>m;
while(m--){
int t,u,v,k;
cin>>t>>u;
if(t==1){
cin>>v>>k;
update_path(u,v,k);
}
else if(t==2){
cin>>k;
update_tree(u,k);
}
else if(t==3){
cin>>v;
cout<<query_path(u,v)<<endl;
}
else cout<<query_tree(u)<<endl;
}
}

换根

在这里插入图片描述

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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include <iostream>
#include <cstring>

using namespace std;

typedef long long LL;

const int N = 1e5 + 10, M = N << 1;

int n, m;
int h[N], e[M], ne[M], w[M], idx;
int id[N], nw[N], cnt;
int dep[N], sz[N], top[N], fa[N], son[N];
int root;
struct SegmentTree
{
int l, r;
LL sum, flag;
}tr[N << 2];

void add(int a, int b)
{
e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
}
void dfs1(int u, int father, int depth)
{
dep[u] = depth, fa[u] = father, sz[u] = 1;
for (int i = h[u]; ~i; i = ne[i])
{
int j = e[i];
if (j == father) continue;
dfs1(j, u, depth + 1);
sz[u] += sz[j];
if (sz[son[u]] < sz[j]) son[u] = j;
}
}
void dfs2(int u, int t)
{
id[u] = ++ cnt, nw[cnt] = w[u], top[u] = t;
if (!son[u]) return;
dfs2(son[u], t);
for (int i = h[u]; ~i; i = ne[i])
{
int j = e[i];
if (j == fa[u] || j == son[u]) continue;
dfs2(j, j);
}
}
//------------------------线段树的部分------------------------\\

void pushup(int u)
{
tr[u].sum = tr[u << 1].sum + tr[u << 1 | 1].sum;
}

void pushdown(int u)
{
auto &root = tr[u], &left = tr[u << 1], &right = tr[u << 1 | 1];
if (root.flag)
{
left.sum += root.flag * (left.r - left.l + 1);
left.flag += root.flag;
right.sum += root.flag * (right.r - right.l + 1);
right.flag += root.flag;
root.flag = 0;
}
}

void build(int u, int l, int r)
{
tr[u] = {l, r, nw[r], 0};
if (l == r) return;
int mid = l + r >> 1;
build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r);
pushup(u);
}

void update(int u, int l, int r, int k)
{
if (l <= tr[u].l && r >= tr[u].r)
{
tr[u].flag += k;
tr[u].sum += k * (tr[u].r - tr[u].l + 1);
return;
}
pushdown(u);
int mid = tr[u].l + tr[u].r >> 1;
if (l <= mid) update(u << 1, l, r, k);
if (r > mid) update(u << 1 | 1, l, r, k);
pushup(u);
}

LL query(int u, int l, int r)
{
if (l <= tr[u].l && r >= tr[u].r) return tr[u].sum;
pushdown(u);
int mid = tr[u].l + tr[u].r >> 1;
LL res = 0;
if (l <= mid) res += query(u << 1, l, r);
if (r > mid) res += query(u << 1 | 1, l, r);
return res;
}
//------------------------线段树的部分------------------------\\

int lca(int u, int v)
{
while (top[u] != top[v])
{
if (dep[top[u]] < dep[top[v]]) swap(u, v);
u = fa[top[u]];
}
if (dep[u] > dep[v]) swap(u, v);
return u;
}
void update_path(int u, int v, int k)
{
while (top[u] != top[v])
{
if (dep[top[u]] < dep[top[v]]) swap(u, v);
update(1, id[top[u]], id[u], k);
u = fa[top[u]];
}
if (dep[u] < dep[v]) swap(u, v);
update(1, id[v], id[u], k);
}
LL query_path(int u, int v)
{
LL res = 0;
while (top[u] != top[v])
{
if (dep[top[u]] < dep[top[v]]) swap(u, v);
res += query(1, id[top[u]], id[u]);
u = fa[top[u]];
}
if (dep[u] < dep[v]) swap(u, v);
res += query(1, id[v], id[u]);
return res;
}
void update_tree(int u, int k)
{
if (u == root) update(1, 1, n, k);
else if (lca(u, root) == u)
{
update(1, 1, n, k);
for (int i = h[u]; ~i; i = ne[i])
{
int j = e[i];
if (lca(j, root) == j)
{
update(1, id[j], id[j] + sz[j] - 1, -k);
break;
}
}
}
else update(1, id[u], id[u] + sz[u] - 1, k);
}
LL query_tree(int u)
{
if (u == root) return query(1, 1, n);
else if (lca(u, root) == u)
{
LL res = query(1, 1, n);
for (int i = h[u]; ~i; i = ne[i])
{
int j = e[i];
if (lca(j, root) == j)
{
res -= query(1, id[j], id[j] + sz[j] - 1);
return res;
}
}
}
else return query(1, id[u], id[u] + sz[u] - 1);
}

int main()
{
scanf("%d", &n);
memset(h, -1, sizeof h);
for (int i = 1; i <= n; i ++ ) scanf("%d", &w[i]);
for (int i = 2; i <= n; i ++ )
{
int a;
scanf("%d", &a);
add(a, i);
}
dfs1(1, -1, 1);
dfs2(1, 1);
build(1, 1, n);
scanf("%d", &m);
while (m -- )
{
int t, u, v, k;
scanf("%d%d", &t, &u);
if (t == 1) root = u;
else if (t == 2)
{
scanf("%d%d", &v, &k);
update_path(u, v, k);
}
else if (t == 3)
{
scanf("%d", &k);
update_tree(u, k);
}
else if (t == 4)
{
scanf("%d", &v);
printf("%lld\n", query_path(u, v));
}
else printf("%lld\n", query_tree(u));
}
return 0;
}

线段树动态开点

在这里插入图片描述

这道题不能开1e5个线段树,只能动态开点

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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
#define swpa swap
#define remove _
const int N = 2e6+10, M=N*2;
int n,m,cnt2;
int w[N],h[N],ne[M],e[M],idx;
int id[N],nw[N],cnt;
int dep[N],sz[N],top[N],fa[N],son[N];
int root[N];
struct Tree{
int ls,rs;
int sum,mx;
}tr[N*4];
int c[N];
inline void add(int a, int b)
{
e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
}

void dfs1(int u,int father,int depth){
dep[u]=depth,fa[u]=father,sz[u]=1;
for(int i=h[u];~i;i=ne[i]){
int j=e[i];
if(j==father)continue;
dfs1(j,u,depth+1);
sz[u]+=sz[j];
if(sz[son[u]]<sz[j])son[u]=j;
}
}

void dfs2(int u,int t){
id[u]=++cnt,nw[cnt]=w[u],top[u]=t;
//who[cnt]=u;
if(!son[u])return;
dfs2(son[u],t);
for(int i=h[u];~i;i=ne[i]){
int j=e[i];
if(j==fa[u]||j==son[u])continue;
dfs2(j,j);
}
}

inline void pushup(int u){
tr[u].sum=tr[tr[u].ls].sum+tr[tr[u].rs].sum;
tr[u].mx=max(tr[tr[u].ls].mx,tr[tr[u].rs].mx);
}
inline void pushdown(int u){

}
inline void modify(int &o,int l,int r,int i,int k){
if(!o)o=++cnt2;
tr[o].mx=tr[o].sum=k;
if(l==r)return;
//pushdown(o);
int mid=l+r>>1;
if(i<=mid)modify(tr[o].ls,l,mid,i,k);
else modify(tr[o].rs,mid+1,r,i,k);
pushup(o);
//if(tr[o].sum==0)o=0;
}
void remove(int &o,int l,int r,int i,int k=0){
if(l==r){tr[o].sum=tr[o].mx=0;return;}
int mid=l+r>>1;
if(i<=mid)remove(tr[o].ls,l,mid,i,k);
else remove(tr[o].rs,mid+1,r,i,k);
pushup(o);
}
inline int query_max(int u,int ql,int qr,int l,int r){
if(!u)return -1;
if(qr<l||ql>r)return -1;
if(l>=ql&&r<=qr)return tr[u].mx;
//pushdown(u);
int mid=l+r>>1;
return max(query_max(tr[u].ls,ql,qr,l,mid),
query_max(tr[u].rs,ql,qr,mid+1,r));
}
inline int query_sum(int u,int ql,int qr,int l,int r){
if(!u)return 0;
if(qr<l||ql>r)return 0;
if(l>=ql&&r<=qr)return tr[u].sum;
//pushdown(u);
int mid=l+r>>1;
return query_sum(tr[u].ls,ql,qr,l,mid)+query_sum(tr[u].rs,ql,qr,mid+1,r);
}

inline int query_path_sum(int o,int u, int v)
{
int res = 0;
while (top[u] != top[v])
{
if (dep[top[u]]<dep[top[v]])swap(u, v);
res+=query_sum(root[o],id[top[u]],id[u],1,n);
u=fa[top[u]];
}
if (dep[u]>dep[v])swap(u, v);
res+=query_sum(root[o],id[u],id[v],1,n);
return res;
}
inline int query_path_max(int o,int u,int v){
int res=0;
while(top[u]!=top[v]){
if(dep[top[u]]<dep[top[v]])swpa(u,v);
res=max(res,query_max(root[o],id[top[u]],id[u],1,n));
u=fa[top[u]];
}
if(dep[u]>dep[v])swap(u,v);
res=max(res,query_max(root[o],id[u],id[v],1,n));
return res;
}
signed main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
memset(h,-1,sizeof h);
cin>>n>>m;
for(int i=1;i<=n;i++)cin>>w[i]>>c[i];
for(int i=1;i<n;i++){
int a,b;
cin>>a>>b;
add(a,b),add(b,a);
}
dfs1(1,-1,1);
dfs2(1,1);
for(int i=1;i<=n;i++)modify(root[c[i]],1,n,id[i],w[i]);
while(m--){
string op;
int x,y;
cin>>op>>x>>y;
if(op[1]=='C')remove(root[c[x]],1,n,id[x]),modify(root[y],1,n,id[x],w[x]),c[x]=y;
if(op[1]=='W')remove(root[c[x]],1,n,id[x]),modify(root[c[x]],1,n,id[x],y),w[x]=y;
if(op[1]=='S')cout<<query_path_sum(c[x],x,y)<<'\n';
if(op[1]=='M')cout<<query_path_max(c[x],x,y)<<'\n';
}
}

在这里插入图片描述

这种题可以套板子来写

如果有颜色的限制就开个color数组读入颜色,如果没有颜色的限制就把color数组全部置为1

注意如果点权有负数的情况更新最大值的时候是要把-1和0变为-1e9

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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
#define swpa swap
#define remove _
#define int long long
const int N = 2e6+10, M=N*2;
int n,m,cnt2;
int w[N],h[N],ne[M],e[M],idx;
int id[N],nw[N],cnt;
int dep[N],sz[N],top[N],fa[N],son[N];
int root[N];
struct Tree{
int ls,rs;
int sum,mx;
}tr[N*4];
int c[N];
inline void add(int a, int b)
{
e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
}

void dfs1(int u,int father,int depth){
dep[u]=depth,fa[u]=father,sz[u]=1;
for(int i=h[u];~i;i=ne[i]){
int j=e[i];
if(j==father)continue;
dfs1(j,u,depth+1);
sz[u]+=sz[j];
if(sz[son[u]]<sz[j])son[u]=j;
}
}

void dfs2(int u,int t){
id[u]=++cnt,nw[cnt]=w[u],top[u]=t;
//who[cnt]=u;
if(!son[u])return;
dfs2(son[u],t);
for(int i=h[u];~i;i=ne[i]){
int j=e[i];
if(j==fa[u]||j==son[u])continue;
dfs2(j,j);
}
}

inline void pushup(int u){
tr[u].sum=tr[tr[u].ls].sum+tr[tr[u].rs].sum;
tr[u].mx=max(tr[tr[u].ls].mx,tr[tr[u].rs].mx);
}
inline void pushdown(int u){

}
inline void modify(int &o,int l,int r,int i,int k){
if(!o)o=++cnt2;
tr[o].mx=tr[o].sum=k;
if(l==r)return;
//pushdown(o);
int mid=l+r>>1;
if(i<=mid)modify(tr[o].ls,l,mid,i,k);
else modify(tr[o].rs,mid+1,r,i,k);
pushup(o);
//if(tr[o].sum==0)o=0;
}
void remove(int &o,int l,int r,int i,int k=0){
if(l==r){tr[o].sum=tr[o].mx=0;return;}
int mid=l+r>>1;
if(i<=mid)remove(tr[o].ls,l,mid,i,k);
else remove(tr[o].rs,mid+1,r,i,k);
pushup(o);
}
inline int query_max(int u,int ql,int qr,int l,int r){
if(!u)return -1e9;
if(qr<l||ql>r)return -1e9;
if(l>=ql&&r<=qr)return tr[u].mx;
//pushdown(u);
int mid=l+r>>1;
return max(query_max(tr[u].ls,ql,qr,l,mid),
query_max(tr[u].rs,ql,qr,mid+1,r));
}
inline int query_sum(int u,int ql,int qr,int l,int r){
if(!u)return 0;
if(qr<l||ql>r)return 0;
if(l>=ql&&r<=qr)return tr[u].sum;
//pushdown(u);
int mid=l+r>>1;
return query_sum(tr[u].ls,ql,qr,l,mid)+query_sum(tr[u].rs,ql,qr,mid+1,r);
}

inline int query_path_sum(int o,int u, int v)
{
int res = 0;
while (top[u] != top[v])
{
if (dep[top[u]]<dep[top[v]])swap(u, v);
res+=query_sum(root[o],id[top[u]],id[u],1,n);
u=fa[top[u]];
}
if (dep[u]>dep[v])swap(u, v);
res+=query_sum(root[o],id[u],id[v],1,n);
return res;
}
inline int query_path_max(int o,int u,int v){
int res=-1e9;
while(top[u]!=top[v]){
if(dep[top[u]]<dep[top[v]])swpa(u,v);
res=max(res,query_max(root[o],id[top[u]],id[u],1,n));
u=fa[top[u]];
}
if(dep[u]>dep[v])swap(u,v);
res=max(res,query_max(root[o],id[u],id[v],1,n));
return res;
}
signed main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
memset(h,-1,sizeof h);
cin>>n;

for(int i=1;i<n;i++){
int a,b;
cin>>a>>b;
add(a,b),add(b,a);
}
for(int i=1;i<=n;i++)cin>>w[i];
for(int i=1;i<=n;i++)c[i]=1;
dfs1(1,-1,1);
dfs2(1,1);
for(int i=1;i<=n;i++)modify(root[c[i]],1,n,id[i],w[i]);
cin>>m;
while(m--){
string op;
int x,y;
cin>>op>>x>>y;
//if(op=="CHANGE")remove(root[c[x]],1,n,id[x]),modify(root[y],1,n,id[x],w[x]),c[x]=y;
if(op=="CHANGE")remove(root[c[x]],1,n,id[x]),modify(root[c[x]],1,n,id[x],y),w[x]=y;
if(op=="QSUM")cout<<query_path_sum(c[x],x,y)<<'\n';
if(op=="QMAX")cout<<query_path_max(c[x],x,y)<<'\n';
}
}

倍增求LCA,树上差分

在这里插入图片描述
每一个点除了起点,都会作为新的起点多了一个1,所以要求出最终答案后减去
按照题意,终点不用给糖果所以第n个点也要减去一个1

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
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10,M=2*N;
int h[N],e[M],ne[M],idx,n;
int fa[N][31],ans[N],a[N],dep[N];
void add(int a,int b){
e[++idx]=b,ne[idx]=h[a],h[a]=idx;
}
void dfs(int u,int father=0){
dep[u]=dep[father]+1,fa[u][0]=father;
for(int i=1;i<=30;i++){
fa[u][i]=fa[fa[u][i-1]][i-1];
}
for(int i=h[u];i;i=ne[i]){
int j=e[i];
if(j==father)continue;
dfs(j,u);
}
}
int lca(int x,int y){
if(dep[x]<dep[y])swap(x,y);
for(int i=30;i>=0;i--){
if(dep[fa[x][i]]>=dep[y])x=fa[x][i];
}
if(x==y)return x;
for(int i=30;i>=0;i--){
if(fa[x][i]!=fa[y][i])x=fa[x][i],y=fa[y][i];
}
return fa[x][0];
}
void cal(int u,int father=0){
for(int i=h[u];i;i=ne[i]){
int j=e[i];
if(j==father)continue;
cal(j,u);
ans[u]+=ans[j];
}
}
signed main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i];
for(int i=1;i<n;i++){
int x,y;
cin>>x>>y;
add(x,y);
add(y,x);
}
dfs(1,0);
for(int i=1;i<n;i++){
int x=a[i],y=a[i+1];
int t=lca(x,y);
ans[x]++;
ans[y]++;
ans[t]--;
ans[fa[t][0]]--;
}
cal(1,0);
//除了第一个作为起点以外,其他的所有点都会作为新的起点被计算多次
for(int i=2;i<=n;i++)ans[a[i]]--;
for(int i=1;i<=n;i++)cout<<ans[i]<<'\n';
}


文章作者: 胡耀文
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 胡耀文 !
  目录