Title Description
It has an n-$ $ $ 1 points to the root of the tree $, $ and $ n-th integer variable $ x_i $. $ I $ tree father $ f_i $, each edge $ (i, f_i) $ has a weight $ w_i $, equation represents a $ x_i + x_ {f_i} = w_i $, $ n-1 $ which equations constitute a system of equations.
$ Q $ now given operations, there are two types:
$ \ bullet. 1 \ U \ V \ S $, indicating an inquiry of the solution plus case $ x_u + x_v = s $ this equation, the whole equations. Specifically, the equation has a unique solution if, at this time the output value of $ $ x_1; if there are an infinite number of solutions, INF $ $ output; if no solution, output $ none $. Note that each inquiry is independent.
$ \ bullet 2 \ u \ w $, $ w_u shows a modification to $ $ w $.
Input Format
Read data from files $ equation.in $ in.
The first line of two integers $ n, q $.
The next line $ n-1 $, $ I $ row of two integers $ f_ {i + 1} $ and $ w_ {i + 1} $ .
$ Q $ next row, each row represents an operation, formatted as described problems.
Output Format
Output to a file in $ equation.out $.
For each query output line represents the answer.
Sample
Sample input:
2 7
1 4
1 1 2 5
1 1 2 4
1 1 1 3
1 2 2 6
2 2 3
1 2 2 10
1 2 2 -10
Sample output:
none
inf
none
1
-2
8
Data range and tips
For all data, there is $ 1 \ leqslant n, q \ leqslant 10 ^ 6,1 \ leqslant f_i \ leqslant i-1,1 \ leqslant u, v \ leqslant n, -10 ^ 3 \ leqslant w, w_i \ leqslant 10 ^ 3, -10 ^ 9 \ leqslant s \ leqslant 10 ^ 9 $.
$ \ bullet Subtask1 (3 \% ) $, $ n \ leqslant 10, q = 0 $.
$ \ bullet Subtask2 (18 \% ) $, $ n = 2 $.
$ \ bullet Subtask3 (32 \% ) $, $ n, q \ leqslant 10 ^ 3 $.
$ \ bullet Subtask4 (33 \% ) $, $ n, q \ leqslant 10 ^ 5 $.
$ \ bullet Subtask5 (14 \% ) $, no special constraints.
answer
First, think about how quickly obtained without modifying the solution $ 1 $, in fact, nothing more than the depth of the sub-parity, then a prefix operator side and nothing more, say the figure below:
It may assume $ 4 $ known $ x $, then $ 3 $ is $ w_3-x $, $ 2 $ is $ w_2-w_3 + x $, $ 1 $ is $ w_1-w_2 + w_3-x $, what the law found no ?
Somewhat similar to a multi-step repellent capacity even Qijia Save.
Then consider how to modify, if we want to change an edge, then this will affect the entire sub-tree edges two sons in the company, obviously can not modify the violence.
I found that the problem is actually a range of changes, a single point of inquiry; wish Fenwick tree?
Use $ DFS $ Fenwick tree maintenance order can be.
This problem is often a little card ......
I heard that segment tree will be $ TLE $?
Time complexity: $ \ Theta ((n + q) \ log n) $.
Expectations score: $ 100 $ points.
Actual score: $ 100 $ points.
Code time
#include<bits/stdc++.h>
using namespace std;
struct rec{int nxt,to,w;}e[1000001];
int head[1000001],cnt;
int n,q;
int f[1000001],w[1000001];
int sum[1000001],depth[1000001],l[1000001],r[1000001],tim;
int tr[4000001];
void add(int x,int y,int w)
{
e[++cnt].nxt=head[x];
e[cnt].to=y;
e[cnt].w=w;
head[x]=cnt;
}
void pre_dfs(int x)
{
l[x]=++tim;
for(int i=head[x];i;i=e[i].nxt)
{
depth[e[i].to]=depth[x]+1;
sum[e[i].to]=w[e[i].to]-sum[x];
pre_dfs(e[i].to);
}
r[x]=tim;
}
int lowbit(int x){return x&-x;}
void change(int l,int r,int w)
{
for(int i=l;i<=n;i+=lowbit(i))tr[i]+=w;
for(int i=r+1;i<=n;i+=lowbit(i))tr[i]-=w;
}
int ask(int x)
{
int res=0;
for(int i=l[x];i;i-=lowbit(i))res+=tr[i];
return res*(depth[x]&1?1:-1);
}
int main()
{
scanf("%d%d",&n,&q);
for(int i=2;i<=n;i++)
{
scanf("%d%d",&f[i],&w[i]);
add(f[i],i,w[i]);
}
depth[1]=1;
pre_dfs(1);
while(q--)
{
int opt;
scanf("%d",&opt);
if(opt&1)
{
int u,v,s;
scanf("%d%d%d",&u,&v,&s);
int flagu=sum[u]+ask(u);
int flagv=sum[v]+ask(v);
if((depth[u]&1)&&(depth[v]&1))
{
long long res=1LL*s-flagu-flagv;
if(res&1)puts("none");
else printf("%lld\n",res>>1);
}
else if(!(depth[u]&1)&&!(depth[v]&1))
{
long long res=1LL*flagu+flagv-s;
if(res&1)puts("none");
else printf("%lld\n",res>>1);
}
else
{
if(flagu+flagv==s)puts("inf");
else puts("none");
}
}
else
{
int u,v;
scanf("%d%d",&u,&v);
int delta=v-w[u];
w[u]=v;
if(!(depth[u]&1))delta=-delta;
change(l[u],r[u],delta);
}
}
return 0;
}
rp++