//NETWRKNB
USES Math;
CONST
tfi = '';
tfo = '';
TYPE
node = record
u,v,k,c,pos,pa: longint;
end;
arr = array[0..100000] of node;
VAR
fi,fo : text;
n,m,cnt,res,jmax,t1,t2 : longint;
ke,next,ts : array[-100000..100000] of longint;
head,free,tin,tout,ans : array[0..100000] of longint;
p,q1,q2,pp : arr;
f : array[0..100000,0..20] of longint;
t,tt : array[0..400000] of longint;
Procedure add(i,u,v,c: longint);
Begin
ke[i]:=v;
next[i]:=head[u];
head[u]:=i;
ts[i]:=c;
End;
Procedure inp;
Var
i: longint;
Begin
Read(fi,n,m);
For i:=1 to n-1 do
with p[i] do
begin
read(fi,u,v,c);
add(i,u,v,c);
add(-i,v,u,c);
end;
Readln(fi);
jmax:=trunc(ln(n)/ln(2));
End;
Procedure dfs(u: longint);
Var
j,v: longint;
Begin
inc(cnt);
tin[u]:=cnt;
j:=1;
While f[u,j-1]<>0 do
begin
f[u,j]:=f[f[u,j-1],j-1];
inc(j);
end;
j:=head[u];
While j<>0 do
begin
v:=ke[j];
If tin[v]=0 then
begin
f[v,0]:=u;
dfs(v);
end;
j:=next[j];
end;
tout[u]:=cnt;
End;
Function check(u,v: longint): boolean;
Begin
exit((tin[u]<=tin[v]) and (tout[u]>=tout[v]));
End;
Function lca(u,v: longint): longint;
Var
j: longint;
Begin
If check(u,v) then exit(u);
If check(v,u) then exit(v);
For j:=jmax downto 0 do
If (f[u,j]<>0) and not check(f[u,j],v) then u:=f[u,j];
exit(f[u,0]);
End;
Procedure swap(var x,y: node);
Var
tg: node;
Begin
tg:=x; x:=y; y:=tg;
End;
Procedure qsort(l,r: longint; var a: arr);
Var
i,j,mid,key: longint;
Begin
i:=l;
j:=r;
mid:=l+random(r-l+1);
key:=a[mid].c;
Repeat
While a[i].c<key do inc(i);
While a[j].c>key do dec(j);
If i<=j then
begin
swap(a[i],a[j]);
inc(i);
dec(j);
end;
Until i>j;
If i<r then qsort(i,r,a);
If l<j then qsort(l,j,a);
End;
Procedure init;
Var
i,tg: longint;
ch: char;
Begin
dfs(1);
t1:=0; t2:=0;
For i:=1 to m do
begin
read(fi,ch);
If ch='P' then
begin
inc(t1);
with q1[t1] do
begin
read(fi,u,v,c);
pos:=i;
end;
end
else
begin
inc(t2);
with q2[t2] do
begin
read(fi,k,c);
pos:=i;
end;
end;
readln(fi);
end;
For i:=1 to n-1 do pp[i]:=p[i];
qsort(1,n-1,pp);
qsort(1,t1,q1);
qsort(1,t2,q2);
For i:=1 to n-1 do
with pp[i] do
If check(v,u) then
begin
tg:=u; u:=v; v:=tg;
end;
End;
Procedure new(i,con1,con2: longint);
Begin
inc(tt[con1],tt[i]);
inc(tt[con2],tt[i]);
inc(t[con1],tt[i]);
inc(t[con2],tt[i]);
tt[i]:=0;
End;
Procedure update(i,l,r,u,v: longint);
Var
mid: longint;
Begin
If (l=u) and (v=r) then
begin
inc(tt[i]);
inc(t[i]);
exit;
end;
If (r<u) or (v<l) then exit;
mid:=(l+r) div 2;
new(i,2*i,2*i+1);
update(2*i,l,mid,u,min(v,mid));
update(2*i+1,mid+1,r,max(mid+1,u),v);
t[i]:=t[2*i]+t[2*i+1];
End;
Function get(i,l,r,u,v: longint): longint;
Var
mid: longint;
Begin
If (l=u) and (v=r) then exit(t[i]);
If (r<u) or (v<l) then exit(0);
mid:=(l+r) div 2;
new(i,2*i,2*i+1);
exit(get(2*i,l,mid,u,min(mid,v))+get(2*i+1,mid+1,r,max(mid+1,u),v));
End;
Procedure query1;
Var
i,j: longint;
Begin
For i:=1 to t1 do
with q1[i] do pa:=lca(u,v);
j:=1;
For i:=1 to t1 do
with q1[i] do
begin
While (j<=n-1) and (pp[j].c<=c) do
begin
with pp[j] do update(1,1,n,tin[v],tout[v]);
inc(j);
end;
ans[pos]:=get(1,1,n,tin[u],tin[u])+get(1,1,n,tin[v],tin[v])-get(1,1,n,tin[pa],tin[pa])*2;
end;
End;
Procedure query2;
Var
i,j,tmp: longint;
Begin
For i:=1 to n*4 do
begin
t[i]:=0; tt[i]:=0;
end;
j:=1;
For i:=1 to t2 do
with q2[i] do
begin
While (j<=n-1) and (pp[j].c<=c) do
begin
with pp[j] do update(1,1,n,tin[u],tin[u]);
inc(j);
end;
with p[k] do
If check(u,v) then tmp:=get(1,1,n,tin[v],tout[v])
else
begin
tmp:=get(1,1,n,1,n)-get(1,1,n,tin[u],tout[u]);
If c<=q2[i].c then dec(res);
end;
ans[pos]:=tmp;
end;
End;
Procedure process;
Var
i: longint;
Begin
query1;
query2;
For i:=1 to m do writeln(fo,ans[i]);
End;
BEGIN
assign(fi,tfi); reset(fi);
assign(fo,tfo); rewrite(fo);
inp;
init;
process;
close(fi); close(fo);
END.
Ly9ORVRXUktOQgpVU0VTIE1hdGg7CkNPTlNUCiAgICB0ZmkgPSAnJzsKICAgIHRmbyA9ICcnOwpUWVBFCiAgICBub2RlID0gcmVjb3JkCiAgICAgICAgdSx2LGssYyxwb3MscGE6IGxvbmdpbnQ7CiAgICBlbmQ7CiAgICBhcnIgPSBhcnJheVswLi4xMDAwMDBdIG9mIG5vZGU7ClZBUgogICAgZmksZm8gICAgICAgICAgICAgICAgICAgICAgICAgICA6IHRleHQ7CiAgICBuLG0sY250LHJlcyxqbWF4LHQxLHQyICAgICAgICAgIDogbG9uZ2ludDsKICAgIGtlLG5leHQsdHMgICAgICAgICAgICAgICAgICAgICAgOiBhcnJheVstMTAwMDAwLi4xMDAwMDBdIG9mIGxvbmdpbnQ7CiAgICBoZWFkLGZyZWUsdGluLHRvdXQsYW5zICAgICAgICAgIDogYXJyYXlbMC4uMTAwMDAwXSBvZiBsb25naW50OwogICAgcCxxMSxxMixwcCAgICAgICAgICAgICAgICAgICAgICA6IGFycjsKICAgIGYgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgOiBhcnJheVswLi4xMDAwMDAsMC4uMjBdIG9mIGxvbmdpbnQ7CiAgICB0LHR0ICAgICAgICAgICAgICAgICAgICAgICAgICAgIDogYXJyYXlbMC4uNDAwMDAwXSBvZiBsb25naW50OwoKUHJvY2VkdXJlIGFkZChpLHUsdixjOiBsb25naW50KTsKICAgIEJlZ2luCiAgICAgICAga2VbaV06PXY7CiAgICAgICAgbmV4dFtpXTo9aGVhZFt1XTsKICAgICAgICBoZWFkW3VdOj1pOwogICAgICAgIHRzW2ldOj1jOwogICAgRW5kOwoKUHJvY2VkdXJlIGlucDsKICAgIFZhcgogICAgICAgIGk6IGxvbmdpbnQ7CiAgICBCZWdpbgogICAgICAgIFJlYWQoZmksbixtKTsKICAgICAgICBGb3IgaTo9MSB0byBuLTEgZG8KICAgICAgICAgICAgd2l0aCBwW2ldIGRvCiAgICAgICAgICAgICAgICBiZWdpbgogICAgICAgICAgICAgICAgICAgIHJlYWQoZmksdSx2LGMpOwogICAgICAgICAgICAgICAgICAgIGFkZChpLHUsdixjKTsKICAgICAgICAgICAgICAgICAgICBhZGQoLWksdix1LGMpOwogICAgICAgICAgICAgICAgZW5kOwogICAgICAgIFJlYWRsbihmaSk7CiAgICAgICAgam1heDo9dHJ1bmMobG4obikvbG4oMikpOwogICAgRW5kOwoKUHJvY2VkdXJlIGRmcyh1OiBsb25naW50KTsKICAgIFZhcgogICAgICAgIGosdjogbG9uZ2ludDsKICAgIEJlZ2luCiAgICAgICAgaW5jKGNudCk7CiAgICAgICAgdGluW3VdOj1jbnQ7CiAgICAgICAgajo9MTsKICAgICAgICBXaGlsZSBmW3Usai0xXTw+MCBkbwogICAgICAgICAgICBiZWdpbgogICAgICAgICAgICAgICAgZlt1LGpdOj1mW2ZbdSxqLTFdLGotMV07CiAgICAgICAgICAgICAgICBpbmMoaik7CiAgICAgICAgICAgIGVuZDsKICAgICAgICBqOj1oZWFkW3VdOwogICAgICAgIFdoaWxlIGo8PjAgZG8KICAgICAgICAgICAgYmVnaW4KICAgICAgICAgICAgICAgIHY6PWtlW2pdOwogICAgICAgICAgICAgICAgSWYgdGluW3ZdPTAgdGhlbgogICAgICAgICAgICAgICAgICAgIGJlZ2luCiAgICAgICAgICAgICAgICAgICAgICAgIGZbdiwwXTo9dTsKICAgICAgICAgICAgICAgICAgICAgICAgZGZzKHYpOwogICAgICAgICAgICAgICAgICAgIGVuZDsKICAgICAgICAgICAgICAgIGo6PW5leHRbal07CiAgICAgICAgICAgIGVuZDsKICAgICAgICB0b3V0W3VdOj1jbnQ7CiAgICBFbmQ7CgpGdW5jdGlvbiBjaGVjayh1LHY6IGxvbmdpbnQpOiBib29sZWFuOwogICAgQmVnaW4KICAgICAgICBleGl0KCh0aW5bdV08PXRpblt2XSkgYW5kICh0b3V0W3VdPj10b3V0W3ZdKSk7CiAgICBFbmQ7CgpGdW5jdGlvbiBsY2EodSx2OiBsb25naW50KTogbG9uZ2ludDsKICAgIFZhcgogICAgICAgIGo6IGxvbmdpbnQ7CiAgICBCZWdpbgogICAgICAgIElmIGNoZWNrKHUsdikgdGhlbiBleGl0KHUpOwogICAgICAgIElmIGNoZWNrKHYsdSkgdGhlbiBleGl0KHYpOwogICAgICAgIEZvciBqOj1qbWF4IGRvd250byAwIGRvCiAgICAgICAgICAgIElmIChmW3Usal08PjApIGFuZCBub3QgY2hlY2soZlt1LGpdLHYpIHRoZW4gdTo9Zlt1LGpdOwogICAgICAgIGV4aXQoZlt1LDBdKTsKICAgIEVuZDsKClByb2NlZHVyZSBzd2FwKHZhciB4LHk6IG5vZGUpOwogICAgVmFyCiAgICAgICAgdGc6IG5vZGU7CiAgICBCZWdpbgogICAgICAgIHRnOj14OyB4Oj15OyB5Oj10ZzsKICAgIEVuZDsKClByb2NlZHVyZSBxc29ydChsLHI6IGxvbmdpbnQ7IHZhciBhOiBhcnIpOwogICAgVmFyCiAgICAgICAgaSxqLG1pZCxrZXk6IGxvbmdpbnQ7CiAgICBCZWdpbgogICAgICAgIGk6PWw7CiAgICAgICAgajo9cjsKICAgICAgICBtaWQ6PWwrcmFuZG9tKHItbCsxKTsKICAgICAgICBrZXk6PWFbbWlkXS5jOwogICAgICAgIFJlcGVhdAogICAgICAgICAgICBXaGlsZSBhW2ldLmM8a2V5IGRvIGluYyhpKTsKICAgICAgICAgICAgV2hpbGUgYVtqXS5jPmtleSBkbyBkZWMoaik7CiAgICAgICAgICAgIElmIGk8PWogdGhlbgogICAgICAgICAgICAgICAgYmVnaW4KICAgICAgICAgICAgICAgICAgICBzd2FwKGFbaV0sYVtqXSk7CiAgICAgICAgICAgICAgICAgICAgaW5jKGkpOwogICAgICAgICAgICAgICAgICAgIGRlYyhqKTsKICAgICAgICAgICAgICAgIGVuZDsKICAgICAgICBVbnRpbCBpPmo7CiAgICAgICAgSWYgaTxyIHRoZW4gcXNvcnQoaSxyLGEpOwogICAgICAgIElmIGw8aiB0aGVuIHFzb3J0KGwsaixhKTsKICAgIEVuZDsKClByb2NlZHVyZSBpbml0OwogICAgVmFyCiAgICAgICAgaSx0ZzogbG9uZ2ludDsKICAgICAgICBjaDogY2hhcjsKICAgIEJlZ2luCiAgICAgICAgZGZzKDEpOwogICAgICAgIHQxOj0wOyB0Mjo9MDsKICAgICAgICBGb3IgaTo9MSB0byBtIGRvCiAgICAgICAgICAgIGJlZ2luCiAgICAgICAgICAgICAgICByZWFkKGZpLGNoKTsKICAgICAgICAgICAgICAgIElmIGNoPSdQJyB0aGVuCiAgICAgICAgICAgICAgICAgICAgYmVnaW4KICAgICAgICAgICAgICAgICAgICAgICAgaW5jKHQxKTsKICAgICAgICAgICAgICAgICAgICAgICAgd2l0aCBxMVt0MV0gZG8KICAgICAgICAgICAgICAgICAgICAgICAgICAgIGJlZ2luCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcmVhZChmaSx1LHYsYyk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcG9zOj1pOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgZW5kOwogICAgICAgICAgICAgICAgICAgIGVuZAogICAgICAgICAgICAgICAgZWxzZQogICAgICAgICAgICAgICAgICAgIGJlZ2luCiAgICAgICAgICAgICAgICAgICAgICAgIGluYyh0Mik7CiAgICAgICAgICAgICAgICAgICAgICAgIHdpdGggcTJbdDJdIGRvCiAgICAgICAgICAgICAgICAgICAgICAgICAgICBiZWdpbgogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHJlYWQoZmksayxjKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBwb3M6PWk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBlbmQ7CiAgICAgICAgICAgICAgICAgICAgZW5kOwogICAgICAgICAgICAgICAgcmVhZGxuKGZpKTsKICAgICAgICAgICAgZW5kOwogICAgICAgIEZvciBpOj0xIHRvIG4tMSBkbyBwcFtpXTo9cFtpXTsKICAgICAgICBxc29ydCgxLG4tMSxwcCk7CiAgICAgICAgcXNvcnQoMSx0MSxxMSk7CiAgICAgICAgcXNvcnQoMSx0MixxMik7CiAgICAgICAgRm9yIGk6PTEgdG8gbi0xIGRvCiAgICAgICAgICAgIHdpdGggcHBbaV0gZG8KICAgICAgICAgICAgICAgIElmIGNoZWNrKHYsdSkgdGhlbgogICAgICAgICAgICAgICAgICAgIGJlZ2luCiAgICAgICAgICAgICAgICAgICAgICAgIHRnOj11OyB1Oj12OyB2Oj10ZzsKICAgICAgICAgICAgICAgICAgICBlbmQ7CiAgICBFbmQ7CgpQcm9jZWR1cmUgbmV3KGksY29uMSxjb24yOiBsb25naW50KTsKICAgIEJlZ2luCiAgICAgICAgaW5jKHR0W2NvbjFdLHR0W2ldKTsKICAgICAgICBpbmModHRbY29uMl0sdHRbaV0pOwogICAgICAgIGluYyh0W2NvbjFdLHR0W2ldKTsKICAgICAgICBpbmModFtjb24yXSx0dFtpXSk7CiAgICAgICAgdHRbaV06PTA7CiAgICBFbmQ7ClByb2NlZHVyZSB1cGRhdGUoaSxsLHIsdSx2OiBsb25naW50KTsKICAgIFZhcgogICAgICAgIG1pZDogbG9uZ2ludDsKICAgIEJlZ2luCiAgICAgICAgSWYgKGw9dSkgYW5kICh2PXIpIHRoZW4KICAgICAgICAgICAgYmVnaW4KICAgICAgICAgICAgICAgIGluYyh0dFtpXSk7CiAgICAgICAgICAgICAgICBpbmModFtpXSk7CiAgICAgICAgICAgICAgICBleGl0OwogICAgICAgICAgICBlbmQ7CiAgICAgICAgSWYgKHI8dSkgb3IgKHY8bCkgdGhlbiBleGl0OwogICAgICAgIG1pZDo9KGwrcikgZGl2IDI7CiAgICAgICAgbmV3KGksMippLDIqaSsxKTsKICAgICAgICB1cGRhdGUoMippLGwsbWlkLHUsbWluKHYsbWlkKSk7CiAgICAgICAgdXBkYXRlKDIqaSsxLG1pZCsxLHIsbWF4KG1pZCsxLHUpLHYpOwogICAgICAgIHRbaV06PXRbMippXSt0WzIqaSsxXTsKICAgIEVuZDsKCkZ1bmN0aW9uIGdldChpLGwscix1LHY6IGxvbmdpbnQpOiBsb25naW50OwogICAgVmFyCiAgICAgICAgbWlkOiBsb25naW50OwogICAgQmVnaW4KICAgICAgICBJZiAobD11KSBhbmQgKHY9cikgdGhlbiBleGl0KHRbaV0pOwogICAgICAgIElmIChyPHUpIG9yICh2PGwpIHRoZW4gZXhpdCgwKTsKICAgICAgICBtaWQ6PShsK3IpIGRpdiAyOwogICAgICAgIG5ldyhpLDIqaSwyKmkrMSk7CiAgICAgICAgZXhpdChnZXQoMippLGwsbWlkLHUsbWluKG1pZCx2KSkrZ2V0KDIqaSsxLG1pZCsxLHIsbWF4KG1pZCsxLHUpLHYpKTsKICAgIEVuZDsKClByb2NlZHVyZSBxdWVyeTE7CiAgICBWYXIKICAgICAgICBpLGo6IGxvbmdpbnQ7CiAgICBCZWdpbgogICAgICAgIEZvciBpOj0xIHRvIHQxIGRvCiAgICAgICAgICAgIHdpdGggcTFbaV0gZG8gcGE6PWxjYSh1LHYpOwogICAgICAgIGo6PTE7CiAgICAgICAgRm9yIGk6PTEgdG8gdDEgZG8KICAgICAgICAgICAgd2l0aCBxMVtpXSBkbwogICAgICAgICAgICAgICAgYmVnaW4KICAgICAgICAgICAgICAgICAgICBXaGlsZSAoajw9bi0xKSBhbmQgKHBwW2pdLmM8PWMpIGRvCiAgICAgICAgICAgICAgICAgICAgICAgIGJlZ2luCiAgICAgICAgICAgICAgICAgICAgICAgICAgICB3aXRoIHBwW2pdIGRvIHVwZGF0ZSgxLDEsbix0aW5bdl0sdG91dFt2XSk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBpbmMoaik7CiAgICAgICAgICAgICAgICAgICAgICAgIGVuZDsKICAgICAgICAgICAgICAgICAgICBhbnNbcG9zXTo9Z2V0KDEsMSxuLHRpblt1XSx0aW5bdV0pK2dldCgxLDEsbix0aW5bdl0sdGluW3ZdKS1nZXQoMSwxLG4sdGluW3BhXSx0aW5bcGFdKSoyOwogICAgICAgICAgICAgICAgZW5kOwogICAgRW5kOwoKUHJvY2VkdXJlIHF1ZXJ5MjsKICAgIFZhcgogICAgICAgIGksaix0bXA6IGxvbmdpbnQ7CiAgICBCZWdpbgogICAgICAgIEZvciBpOj0xIHRvIG4qNCBkbwogICAgICAgICAgICBiZWdpbgogICAgICAgICAgICAgICAgdFtpXTo9MDsgdHRbaV06PTA7CiAgICAgICAgICAgIGVuZDsKICAgICAgICBqOj0xOwogICAgICAgIEZvciBpOj0xIHRvIHQyIGRvCiAgICAgICAgICAgIHdpdGggcTJbaV0gZG8KICAgICAgICAgICAgICAgIGJlZ2luCiAgICAgICAgICAgICAgICAgICAgV2hpbGUgKGo8PW4tMSkgYW5kIChwcFtqXS5jPD1jKSBkbwogICAgICAgICAgICAgICAgICAgICAgICBiZWdpbgogICAgICAgICAgICAgICAgICAgICAgICAgICAgd2l0aCBwcFtqXSBkbyB1cGRhdGUoMSwxLG4sdGluW3VdLHRpblt1XSk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBpbmMoaik7CiAgICAgICAgICAgICAgICAgICAgICAgIGVuZDsKICAgICAgICAgICAgICAgICAgICB3aXRoIHBba10gZG8KICAgICAgICAgICAgICAgICAgICAgICAgSWYgY2hlY2sodSx2KSB0aGVuIHRtcDo9Z2V0KDEsMSxuLHRpblt2XSx0b3V0W3ZdKQogICAgICAgICAgICAgICAgICAgICAgICBlbHNlCiAgICAgICAgICAgICAgICAgICAgICAgICAgICBiZWdpbgogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHRtcDo9Z2V0KDEsMSxuLDEsbiktZ2V0KDEsMSxuLHRpblt1XSx0b3V0W3VdKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBJZiBjPD1xMltpXS5jIHRoZW4gZGVjKHJlcyk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBlbmQ7CiAgICAgICAgICAgICAgICAgICAgYW5zW3Bvc106PXRtcDsKICAgICAgICAgICAgICAgIGVuZDsKICAgIEVuZDsKClByb2NlZHVyZSBwcm9jZXNzOwogICAgVmFyCiAgICAgICAgaTogbG9uZ2ludDsKICAgIEJlZ2luCiAgICAgICAgcXVlcnkxOwogICAgICAgIHF1ZXJ5MjsKICAgICAgICBGb3IgaTo9MSB0byBtIGRvIHdyaXRlbG4oZm8sYW5zW2ldKTsKICAgIEVuZDsKCkJFR0lOCiAgICBhc3NpZ24oZmksdGZpKTsgcmVzZXQoZmkpOwogICAgYXNzaWduKGZvLHRmbyk7IHJld3JpdGUoZm8pOwogICAgICAgIGlucDsKICAgICAgICBpbml0OwogICAgICAgIHByb2Nlc3M7CiAgICBjbG9zZShmaSk7IGNsb3NlKGZvKTsKRU5ELgo=