2759: 一个动态树好题
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 980 Solved: 334
[Submit][Status][Discuss]
Description
有N个未知数x[1..n]和N个等式组成的同余方程组:
x[i]=k[i]*x[p[i]]+b[i] mod 10007
其中,k[i],b[i],x[i]∈[0,10007)∩Z
你要应付Q个事务,每个是两种情况之一:
一.询问当前x[a]的解
A a
无解输出-1
x[a]有多解输出-2
否则输出x[a]
二.修改一个等式
C a k[a] p[a] b[a]
Input
N
下面N行,每行三个整数k[i] p[i] b[i]
Q
下面Q行,每行一个事务,格式见题目描述
Output
对每个询问,输出一行一个整数。
对100%的数据,1≤N≤30000,0≤Q≤100000,时限2秒,其中询问事务约占总数的80%
Sample Input
5
2 2 1
2 3 2
2 4 3
2 5 4
2 3 5
5
A 1
A 2
C 5 3 1 1
A 4
A 5
Sample Output
4276
7141
4256
2126
HINT
Source
By 范浩强
sol:
容易注意到图是一个基环树森林。我们考虑如果x[i]=kx[p]+b,则fa[x]=p,根则是fa[x]=0,efa[x]=p。如果我们想要求x的解,就要求出他所在的数中环的一个解。我们用splay维护子树中深度最大的点用深度最小的点的fa表示的k和b。updata显然。因为这题每个基环树的根都被我们指定了,所以不能make_root。那么要求x的解的时候,只要access(x),splay(x)就能得到x=kroot+b的系数,access(efa),splay(efa)就能得到efa=kefa+b的系数,这里我一开始以为得到的是efa=kroot+b,然后还解了一下,但我们发现我们最后进行了b+a的操作,其实那个方程已经被代出来了。对于修改操作,大力讨论一下,画个图都能明白,主要是讨论原边是不是指向efa,和原边在不在环上,新边在不在环上3个问题。实在不愿意想的话可以看一下代码实现。
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
using namespace std;
typedef long long ll;
typedef double s64;
int n,m;
inline int read()
{
char c;
int res,flag=0;
while((c=getchar())>'9'||c<'0') if(c=='-')flag=1;
res=c-'0';
while((c=getchar())>='0'&&c<='9') res=(res<<3)+(res<<1)+c-'0';
return flag?-res:res;
}
const int N=31000;
int lc[N],rc[N],size[N],fa[N],val[N];
int efa[N];
bool rev[N];
const int pyz=10007;
struct cc
{
int k,b;
friend inline cc operator +(const cc &a,const cc &b)
{
return (cc){a.k*b.k%pyz,(b.k*a.b%pyz+b.b)%pyz};
}
}a[N],b[N];
inline int ksm(int s,int t)
{
int res=1;
while(t)
{
if(t&1) res=(ll)res*s%pyz;
s=(ll)s*s%pyz;
t>>=1;
}
return res;
}
inline int inv(int s)
{
s=(s%pyz+pyz)%pyz;
return ksm(s,pyz-2);
}
int ID;
int id[N];
inline void dfs(int u)
{
id[u]=ID;
if(id[fa[u]]==ID)
{
efa[u]=fa[u];
fa[u]=0;
}
else dfs(fa[u]);
}
inline void updata(int x)
{
b[x]=a[x];
if(lc[x]) b[x]=b[lc[x]]+a[x];
if(rc[x]) b[x]=b[x]+b[rc[x]];
}
inline bool is_root(int x)
{
return lc[fa[x]]!=x&&rc[fa[x]]!=x;
}
inline void rotate(int x)
{
int y=fa[x],z=fa[y];
int b=lc[y]==x?rc[x]:lc[x];
if(b) fa[b]=y;
fa[y]=x;fa[x]=z;
if(z)
{
if(lc[z]==y) lc[z]=x;
if(rc[z]==y) rc[z]=x;
}
if(lc[y]==x) lc[y]=b,rc[x]=y;
else rc[y]=b,lc[x]=y;
updata(y);
}
int sta[N];
inline void splay(int x)
{
while(!is_root(x))
{
if(!is_root(fa[x]))
{
if((lc[fa[x]]==x)==(lc[fa[fa[x]]]==fa[x])) rotate(fa[x]);
else rotate(x);
}
rotate(x);
}
updata(x);
}
inline void access(int q)
{
for(int p=0;q;p=q,q=fa[q])
{
splay(q);
rc[q]=p;
updata(q);
}
}
inline int find(int x)
{
access(x);
splay(x);
while(lc[x]) x=lc[x];
return x;
}
inline void cut(int x)
{
access(x);
splay(x);
lc[x]=fa[lc[x]]=0;
updata(x);
}
inline void debug(int x)
{
printf("%d %d %d\n",x,lc[x],rc[x]);
if(lc[x]) debug(lc[x]);
if(rc[x]) debug(rc[x]);
}
char sr[5];
inline bool in_circle(int x,int y)
{
access(efa[y]);
splay(efa[y]);
splay(x);
return x==efa[y]||!is_root(efa[y]);
}
int main()
{
// freopen("2759.in","r",stdin);
// freopen(".out","w",stdout);
n=read();
for(int i=1;i<=n;++i)
{
a[i].k=read();
fa[i]=read();
a[i].b=read();
}
for(int i=1;i<=n;++i)
if(!id[i]) ++ID,dfs(i);
m=read();
while(m--)
{
scanf("%s",sr+1);
int x;
x=read();
if(sr[1]=='A')
{
access(x);
splay(x);
cc t1=b[x];
int t=find(x);
access(efa[t]);
splay(efa[t]);
cc t2=b[efa[t]];
cc p=t2;
// cc p=t2+a[t];
if(p.k==1&&p.b==0)
{
printf("-2\n");
continue;
}
if(p.k==1&&p.b!=0)
{
printf("-1\n");
continue;
}
int o=inv((1-p.k+pyz)%pyz)*p.b%pyz;
// int o=(-p.b*inv(p.k-1)%pyz+pyz)%pyz;
// printf("%d %d %d %d %d %d\n",ni,o,a[t].k,a[t].b,t2.k,t2.b);
printf("%d\n",(t1.k*o%pyz+t1.b)%pyz);
}
else
{
int k,p,b;
a[x].k=read();
p=read();
a[x].b=read();
splay(x);
if(efa[x])
{
efa[x]=0;
if(find(p)==x) efa[x]=p;
else fa[x]=p;
}
else
{
int root=find(x);
if(in_circle(x,root))
{
cut(x);
access(root);splay(root);
fa[root]=efa[root];efa[root]=0;
}
else cut(x);
if(find(p)==x) efa[x]=p;
else fa[x]=p;
}
}
}
}