new
SalN
·
·
个人记录
#include<bits/stdc++.h>
#define int long long
#define up(i,l,r) for(int i=l; i<=r; ++i)
#define dn(i,r,l) for(int i=r; i>=l; --i)
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define fir first
#define sec second
using namespace std;
const int N=100005, M=__lg(N)+1, P=19491001;
int n, m, T, val[N], dep[N], fa[N][M];
pii f[N][M], g[N][M];
vector<pii> to[N];
pii operator+(const pii i,const pii j) {
return mp(i.fir*j.fir%P,(i.sec*j.fir%P+j.sec)%P);
}
void dfs(int x,int fad) {
dep[x]=dep[fad]+1, fa[x][0]=fad;
up(i,1,T) {
fa[x][i]=fa[fa[x][i-1]][i-1];
f[x][i]=f[x][i-1]+f[fa[x][i-1]][i-1];
g[x][i]=g[fa[x][i-1]][i-1]+g[x][i-1];
}
for(pii u:to[x]) {
int y=u.fir, opt=u.sec;
if(y==fad) continue;
if(opt==1) f[y][0]=mp(1,+val[x]), g[y][0]=mp(1,+val[y]);
if(opt==2) f[y][0]=mp(1,-val[x]), g[y][0]=mp(1,-val[y]);
if(opt==3) f[y][0]=mp(val[x],0), g[y][0]=mp(val[y],0);
dfs(y,x);
}
}
int lca(int x,int y) {
if(dep[x]<dep[y]) swap(x,y);
dn(i,T,0) if(dep[fa[x][i]]>=dep[y]) x=fa[x][i];
if(x==y) return x;
dn(i,T,0) if(fa[x][i]!=fa[y][i]) x=fa[x][i], y=fa[y][i];
return fa[x][0];
}
signed main() {
freopen("tree.in","r",stdin);
freopen("tree.out","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m, T=__lg(n);
up(i,1,n) cin >> val[i], val[i]%=P;
up(i,2,n) {
int x, y, z;
cin >> x >> y >> z;
to[x].pb(mp(y,z));
to[y].pb(mp(x,z));
}
dfs(1,0);
while(m--) {
int x, y, p, sav;
pii ans, l(1,0), r(1,0);
cin >> x >> y;
sav=val[x], p=lca(x,y);
dn(i,T,0) if(dep[fa[x][i]]>=dep[p]) l=l+f[x][i], x=fa[x][i];
dn(i,T,0) if(dep[fa[y][i]]>=dep[p]) r=g[y][i]+r, y=fa[y][i];
ans=l+r;
cout << ((ans.fir*sav%P+ans.sec)%P+P)%P << '\n';
}
return 0;
}