【BZOJ4756】线段树合并学习及模板
感谢starkmal大神的权限号使得我有机会来敲板学习orz。
BZOJ4756(权限题)
就是一道线段树合并的板题。线段树合并的想法和主席树还是有的区别的其实核心代码并不长:
4756: [Usaco2017 Jan]Promotion Counting
Time Limit: 10 Sec Memory Limit: 128 MB Submit: 444 Solved: 316 [Submit][Status][Discuss]Description
The cows have once again tried to form a startup company, failing to remember from past experience t
hat cows make terrible managers!The cows, conveniently numbered 1…N1…N (1≤N≤100,000), organize t
he company as a tree, with cow 1 as the president (the root of the tree). Each cow except the presid
ent has a single manager (its "parent" in the tree). Each cow ii has a distinct proficiency rating,
p(i), which describes how good she is at her job. If cow ii is an ancestor (e.g., a manager of a man
ager of a manager) of cow jj, then we say jj is a subordinate of ii.
Unfortunately, the cows find that it is often the case that a manager has less proficiency than seve
ral of her subordinates, in which case the manager should consider promoting some of her subordinate
s. Your task is to help the cows figure out when this is happening. For each cow ii in the company,
please count the number of subordinates jj where p(j)>p(i).
n只奶牛构成了一个树形的公司,每个奶牛有一个能力值pi,1号奶牛为树根。
问对于每个奶牛来说,它的子树中有几个能力值比它大的。
Input
The first line of input contains N
The next N lines of input contain the proficiency ratings p(1)…p(N)
for the cows. Each is a distinct integer in the range 1…1,000,000,000
The next N-1 lines describe the manager (parent) for cows 2…N
Recall that cow 1 has no manager, being the president.
n,表示有几只奶牛 n<=100000
接下来n行为1-n号奶牛的能力值pi
接下来n-1行为2-n号奶牛的经理(树中的父亲)
Output
Please print N lines of output. The ith line of output should tell the number of
subordinates of cow ii with higher proficiency than cow i.
共n行,每行输出奶牛i的下属中有几个能力值比i大
Sample Input
5
804289384
846930887
681692778
714636916
957747794
1
1
2
3
Sample Output
2
0
1
0
0
HINT
Source
就是一道线段树合并的板题。线段树合并的想法和主席树还是有的区别的其实核心代码并不长:
int merge(int ll,int rr) { if((!ll)||(!rr)) return ll+rr; int t = ++cnt; ls[t] = merge(ls[ll],ls[rr]); rs[t] = merge(rs[ll],rs[rr]); znum[t] = znum[ls[t]] + znum[rs[t]]; return t; }就是这样,两棵线段树数值相加,如果遇到有一个结点为空,那么就回溯这个结点。这个是带可持续版本,可以建很多线段树,如果不想可持久化,可以不新加这个t结点,直接用ll代替。 剩下的都和线段树写法差不多(非堆式存储,也就是x的左儿子是2*x,右儿子2*x+1那种) 于是这道题就做完了!离散化之后,每次对自己和自己的所有子树直接合并,然后线段树查询即可。 参考代码:
#include<bits/stdc++.h> using namespace std; const int maxn = 100005; int n; int ls[maxn*100],rs[maxn*100],cnt,ccc,rt[maxn],znum[maxn*100]; int haha[maxn],col[maxn],fa[maxn],nt[maxn],la[maxn],owo,en[maxn]; int ans[maxn]; void addedge(int a,int b) { en[++owo]=b; nt[owo]=la[a]; la[a]=owo; } void insert(int l,int r,int &pos,int zhi,int oo) { pos=++cnt; znum[pos]+=oo; if(l==r) return; int mid = (l+r)>>1; if(zhi<=mid) insert(l,mid,ls[pos],zhi,oo); else insert(mid+1,r,rs[pos],zhi,oo); } int merge(int ll,int rr) { if((!ll)||(!rr)) return ll+rr; int t = ++cnt; ls[t] = merge(ls[ll],ls[rr]); rs[t] = merge(rs[ll],rs[rr]); znum[t] = znum[ls[t]] + znum[rs[t]]; return t; } int query(int p,int l,int r,int x,int y) { if(x<=l&&r<=y) return znum[p]; int mid = (l+r)>>1; if(y<=mid) return query(ls[p],l,mid,x,y); else if(x>mid) return query(rs[p],mid+1,r,x,y); else return query(ls[p],l,mid,x,y)+query(rs[p],mid+1,r,x,y); } void dfs(int x) { insert(0,ccc,rt[x],col[x],1); for(int it=la[x];it;it=nt[it]) if(en[it]!=fa[x]) dfs(en[it]); for(int it=la[x];it;it=nt[it]) if(en[it]!=fa[x]) rt[x] = merge(rt[x],rt[en[it]]); ans[x] = query(rt[x],0,ccc,col[x]+1,ccc); } int main() { scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&col[i]),haha[i]=col[i]; sort(haha+1,haha+1+n); ccc = unique(haha+1,haha+1+n)-haha-1; for(int i=1;i<=n;i++) col[i] = lower_bound(haha+1,haha+1+ccc,col[i])-haha-1; for(int i=2;i<=n;i++) scanf("%d",&fa[i]),addedge(fa[i],i); dfs(1); for(int i=1;i<=n;i++) printf("%d\n",ans[i]); }