int last[maxn], mid[maxn], first[maxn], lc[maxn], rc[maxn], tot, val[maxn];

int newnode() {
	tot++;
	lc[tot] = rc[tot] = val[tot] = -1;
	return tot;
}
//已知中序 前序
//l1,r1是中序的指针,l2,r2是前序的指针
void build(int &p, int l1, int r1, int l2, int r2) {
	if(l1 > r1) return;
	if(p == -1) p = newnode();
	val[p] = first[l2];
	int pos = l1;
	while(val[p] != mid[pos]) pos++;
	int cnt = pos-l1;
	build(lc[p], l1, pos-1, l2+1, l2+cnt);
	build(rc[p], pos+1, r1, l2+cnt+1, r2);
}
已知中序 后序
void build(int &p, int l1, int r1, int l2, int r2) {
	if(l1 > r1) return;
	p = newnode();
	val[p] = last[r2];
	int pos = l1;
	while(mid[pos] != val[p]) pos++;
	int cnt = pos-l1;
	build(lc[p], l1, pos-1, l2, l2+cnt-1);
	build(rc[p], pos+1, r1, l2+cnt, r2-1);
}