题目描述
Description
给定一个n个点的以1为根的树,每个点有一个正整数点权。
有q个操作,每个操作为以下类型之一:
1. 1 u v 询问树上所有在u到v的简单路径的节点(含u,v)中,是否存在三个不同的节点,使得以这三个节点的点权为边长的三条边能够构成一个三角形。
2. 2 u v 将节点u的权值改成v。
3. 3 u v 若节点v不在以节点u为根的子树里,那么令u的父节点为v,否则令v的父节点为u,如果u=v那么忽略这一条操作。
所有操作按输入的顺序进行。
这个题面应该不会看不懂吧。。
Input
第一行有两个正整数n,q,表示树的节点数和操作数。
第二行n个正整数,表示每个节点的点权。
第三行有n-1个正整数,第i个数表示节点i+1的父节点编号,保证所给定的是一棵树。
接下来q行,每行三个正整数t u v,意义如问题描述中所示。
Output
对每个询问输出一行表示答案,“Y”表示有解,“N”表示无解。
Sample Input
5 5
1 2 3 4 5
1 2 3 1
1 1 3
1 4 5
2 1 4
1 2 5
1 2 3
Sample Output
N
Y
Y
N
Data Constraint
对于20%的数据,保证n,q<=1000
对于另外30%的数据,保证对于每个操作,t<=2
对于再另外的30%的数据,保证询问操作不超过20次。
对于100%的数据,保证n<=100000 q<=200000 每个节点的权值<=(2^31)-1
20%
暴力不解释
50%
t<=2,没有修改操作
题目说要找一个三元组 (a,b,c) ( a , b , c ) 满足 a+b>c a + b > c ,那么可以发现如果一个路径上的点不满足条件,最小时为 a+b=c a + b = c
所以最小不满足的序列就是斐波那契数列
然后可以发现
231−1
2
31
−
1
内最多只有50项
于是可以求两点之间的lca,如果往上跳25次后还没有相遇就一定满足
80%
不会
100%
直接上LCT
修改操作时就打通1~v的路径,看u在不在上面。
(也可以用splay维护dfs序但是我太弱了)
code
#include <iostream>
#include <cstdlib>
#include <cstdio>
#define fo(a,b,c) for (a=b; a<=c; a++)
#define fd(a,b,c) for (a=b; a>=c; a--)
using namespace std;
long long a[100001];
int tr[100001][2];
int fa[100001];
int f[100001];
bool isroot[100001];
bool bz[100001];
bool Bz[100001];
int d[100];
int i,j,k,l,n,Q,x,y,z,h,t;
bool BZ;
void down(int t)
{
if (bz[t])
{
tr[0][0]=tr[t][0];
tr[t][0]=tr[t][1];
tr[t][1]=tr[0][0];
bz[t]=0;
bz[tr[t][0]]^=1;
bz[tr[t][1]]^=1;
}
}
void rotate(int t)
{
down(fa[t]);
down(t);
if (!isroot[t])
{
int x=tr[fa[t]][1]==t;
int Fa=fa[t];
tr[Fa][x]=tr[t][x^1];
fa[tr[t][x^1]]=Fa;
tr[t][x^1]=Fa;
fa[t]=fa[Fa];
fa[Fa]=t;
if (!isroot[Fa] && fa[t]) tr[fa[t]][tr[fa[t]][1]==Fa]=t;
isroot[t]=isroot[Fa];
isroot[Fa]=0;
}
}
void splay(int t)
{
while (!isroot[t])
{
if (!isroot[fa[t]])
{
if ((tr[fa[fa[t]]][0]==fa[t])^(tr[fa[t]][0]==t))
rotate(t),rotate(t);
else
rotate(fa[t]),rotate(t);
}
else
rotate(t);
}
}
void access(int t)
{
int x=0;
while (t)
{
splay(t),down(t);
isroot[tr[t][1]]=1;
isroot[x]=0;
tr[t][1]=x;
x=t;
t=fa[t];
}
}
void moveroot(int t)
{
access(t);
splay(t);
bz[t]=1;
}
int gf(int t)
{
for (; !isroot[t]; t=fa[t]);
return t;
}
void link(int x,int y)
{
moveroot(x);
fa[x]=y;
}
void cut(int x,int y)
{
moveroot(x);
access(y);
splay(x);
fa[y]=0;
isroot[y]=1;
tr[x][1]=0;
}
void bfs(int T)
{
h=0;
t=1;
d[1]=T;
Bz[T]=1;
while (h<t)
{
h++;
for (int i=0; i<=1; i++)
if (tr[d[h]][i] && !Bz[tr[d[h]][i]])
{
d[++t]=tr[d[h]][i];
Bz[d[t]]=1;
if (t>50)
break;
}
if (t>50)
break;
}
fo(i,1,t)
Bz[d[i]]=0;
}
void swap(int &a,int &b) {int c=a;a=b;b=c;}
void qsort(int l,int r)
{
int i,j,mid;
i=l;
j=r;
mid=a[d[(l+r)/2]];
while (i<=j)
{
while (a[d[i]]<mid) i++;
while (a[d[j]]>mid) j--;
if (i<=j)
{
swap(d[i],d[j]);
i++,j--;
}
}
if (l<j) qsort(l,j);
if (i<r) qsort(i,r);
return;
}
int main()
{
freopen("triangle.in","r",stdin);
freopen("triangle.out","w",stdout);
scanf("%d%d",&n,&Q);
fo(i,1,n)
scanf("%lld",&a[i]),isroot[i]=1;
fo(i,1,n-1)
scanf("%d",&f[i+1]),fa[i+1]=f[i+1];
for (; Q; Q--)
{
scanf("%d%d%d",&x,&y,&z);
switch (x)
{
case 1:
{
moveroot(y);
access(z);
splay(z);
bfs(z);
if (t>50) printf("Y\n");
else
{
qsort(1,t);
BZ=0;
fo(i,1,t-2)
if (a[d[i]]+a[d[i+1]]>a[d[i+2]])
{
BZ=1;
break;
}
printf(BZ?"Y\n":"N\n");
}
break;
}
case 2:
{
a[y]=z;
break;
}
case 3:
{
if (y==z) break;
moveroot(1);
access(z);
splay(y);
if (gf(z)==y)
{
cut(z,f[z]);
link(z,y);
f[z]=y;
}
else
{
cut(y,f[y]);
link(y,z);
f[y]=z;
}
break;
}
}
}
fclose(stdin);
fclose(stdout);
return 0;
}