(http://acm.hust.edu.cn/vjudge/contest/view.action?cid=106364#problem/C)
题意:一棵树,点权值,Q询问,询问a到b的路径上最长的一段上升序列。
解法:树链剖分点操作,是带方向的,并且必须是a到b,不能是b到a。
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//#include
#include
#include
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define inf 1e9
#define debug(a) cout << #a" = " << (a) << endl;
#define debugarry(a, n) for (int i = 0; i < (n); i++) { cout << #a"[" << i << "] = " << (a)[i] << endl; }
#define clr(x, y) memset(x, y, sizeof x)
#define ll long long
#define ull unsigned long long
#define FOR(i,a,b) \
for(i=a;a=b;astruct sad{
int l1,l2,r1,r2,s;
int ans1,ans2,vl,vr;
sad(){ans2=ans1=-1;}
sad(int v)
{
l1 = l2 = r1 = r2 = s = 1;
ans1 = ans2 = 1;
vl = vr = v;
}
sad operator +(const sad &b)const{
if( ans1 == -1 ) return b;
if( b.ans1 == -1 ) return *this;
sad c;
c.s = s + b.s;
c.vl = vl;
c.vr = b.vr;
c.ans1 = max(ans1,b.ans1);
c.l1 = l1;
c.r1 = b.r1;
if( vr < b.vl )
{
c.ans1 = max(c.ans1,r1+b.l1);
if( l1 == s )
c.l1 = s + b.l1;
if( b.r1 == b.s )
c.r1 = b.s + r1;
}
c.ans2 = max(ans2,b.ans2);
c.l2 = l2;
c.r2 = b.r2;
if( vr > b.vl )
{
c.ans2 = max(c.ans2,r2+b.l2);
if( l2 == s )
c.l2 = s + b.l2;
if( b.r2 == b.s )
c.r2 = b.s + r2;
}
return c;
}
sad rev()
{
swap(l1,r1);
swap(l2,r2);
swap(vl,vr);
swap(ans1,ans2);
swap(l1,l2);
swap(r1,r2);
return *this;
}
};
const int maxn = 1e5+30;
struct edhe{
int to,next;
}G[maxn<<1];
int h[maxn],si;
void add(int u,int v)
{
G[si].to=v;
G[si].next=h[u];
h[u]=si++;
}
int siz[maxn],dep[maxn];
int fa[maxn],son[maxn],top[maxn];
void dfs1(int u,int f,int d)
{
// debug(u);
fa[u] = f;
dep[u] = d;
siz[u] = 1;
son[u] = -1;
for(int i=h[u];~i;i=G[i].next){
int v=G[i].to;
// debug(v);
if(v^f)
{
dfs1(v,u,d+1);
siz[u]+=siz[v];
if(son[u]==-1||siz[son[u]]int p[maxn],fp[maxn],pos;
void dfs2(int u,int sf)
{
// printf("u %d sf %d fa %d\n",u,sf,fa[u]);
top[u] = sf;
p[u] = pos++;
fp[p[u]]=u;
if(son[u]==-1) return ;
dfs2(son[u],sf);
for(int i=h[u];~i;i=G[i].next){
int v=G[i].to;
if( son[u]!=v&&fa[u]!=v )
dfs2(v,v);
}
}
int val[maxn];
sad rs[maxn<<2];
void pushup(int rt)
{
rs[rt] = rs[rt<<1] + rs[rt<<1|1];
}
void build(int l,int r,int rt)
{
if(l==r)
{
rs[rt] = sad(val[fp[l]]);
return ;
}
int m=l+r>>1;
build(lson);
build(rson);
pushup(rt);
}
sad query(int L,int R,int l,int r,int rt)
{
if( L <= l && r <= R )
return rs[rt];
int m=l+r>>1;
sad ret;
if(L <= m) ret = ret + query(L,R,lson);
if( m < R ) ret = ret + query(L,R,rson);
return ret;
}
int Query(int u,int v)
{
int f1 = top[u] , f2 = top[v];
int r = 0;
sad ans1,ans2;
while(f1!=f2)
{
if(dep[f1] < dep[f2])
{
r^=1;
swap(f1,f2);
swap(u,v);
swap(ans1,ans2);
}
// printf("f1 %d u %d\n",f1,u);
ans1 = query(p[f1],p[u],1,pos-1,1) + ans1;
u=fa[f1];
f1=top[u];
}
if( dep[u] > dep[v] )
{
r^=1;
swap(u,v);
swap(ans1,ans2);
}
// printf("v %d u %d\n",v,u);
ans2 = query(p[u],p[v],1,pos-1,1) +ans2;
sad ans = ans1.rev() +ans2;
if(!r) return ans.ans1;
else return ans.ans2;
}
void init()
{
clr(h,-1);
si=0;
pos=1;
}
int main()
{
// freopen("input.txt","r",stdin);
int T,CASE=0,n,q;
scanf("%d",&T);
while(T--){
if(CASE) printf("\n");
init();
scanf("%d",&n);
int u,v;
for(int i=1;i<=n;i++)
scanf("%d",&val[i]);
for(int i=2;i<=n;i++)
{
scanf("%d",&u);
add(u,i);
add(i,u);
}
dfs1(1,-1,1);
dfs2(1,1);
build(1,pos-1,1);
scanf("%d",&q);
printf("Case #%d:\n",++CASE);
for(int i=0;iscanf("%d%d",&u,&v);
printf("%d\n",Query(u,v));
}
}
return 0;
}