题意:给定上一棵树,每个树的结点有一个权值,有 m 个询问,每次询问 s, t ,  a, b,问你从 s 到 t 这条路上,权值在 a 和 b 之间的和。(闭区间)。

析:很明显的树链剖分,但是要用线段树来维护,首先先离线,然后按询问的 a 排序,每次把小于 a 的权值先更新上,然后再查询,这样就是区间求和了,算完小于a的,再算b的,最答案相减就好了。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(x,n)  for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 10;
const LL mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
    return r > 0 && r <= n && c > 0 && c <= m;
}


struct Val{
  int x, id;
};
Val a[maxn];

struct Edge{
  int to, next;
};
Edge edge[maxn<<1];
int head[maxn<<1], cnt;

void add(int u, int v){
  edge[cnt].to = v;
  edge[cnt].next = head[u];
  head[u] = cnt++;
}

struct Query{
  int s, t, a, b;
  int id;
};
Query q[maxn];

inline bool cmpVal(const Val &lhs, const Val &rhs){
  return lhs.x < rhs.x;
}

inline bool cmpL(const Query &lhs, const Query &rhs){
  return lhs.a < rhs.a;
}

inline bool cmpR(const Query &lhs, const Query &rhs){
  return lhs.b < rhs.b;
}

int fa[maxn], top[maxn], p[maxn];
int pos, son[maxn], num[maxn], dep[maxn];

void init(){
  cnt = 0;  pos = 0;
  ms(head, -1);
  ms(son, -1);
}

void dfs1(int u, int f, int d){
  fa[u] = f;   dep[u] = d;
  num[u] = 1;
  for(int i = head[u]; ~i; i = edge[i].next){
    int v = edge[i].to;
    if(v == f)  continue;
    dfs1(v, u, d+1);
    num[u] += num[v];
    if(son[u] == -1 || num[son[u]] < num[v])  son[u] = v;
  }
}

void dfs2(int u, int sp){
  top[u] = sp;
  p[u] = ++pos;
  if(son[u] == -1)  return ;
  dfs2(son[u], sp);
  for(int i = head[u]; ~i; i = edge[i].next){
    int v = edge[i].to;
    if(v == son[u] || v == fa[u])  continue;
    dfs2(v, v);
  }
}

LL sum[maxn<<2];

void push_up(int rt){
  sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}

void update(int M, int val, int l, int r, int rt){
  if(l == r){ sum[rt] = val;  return ; }
  int m = l + r >> 1;
  if(M <= m)  update(M, val, lson);
  else update(M, val, rson);
  pu(rt);
}

LL query(int L, int R, int l, int r, int rt){
  if(L <= l && r <= R)  return sum[rt];
  int m = l + r >> 1;
  LL ans = 0;
  if(L <= m)  ans = query(L, R, lson);
  if(R > m)  ans += query(L, R, rson);
  return ans;
}


LL solve(int u, int v){
  int f1 = top[u], f2 = top[v];
  LL ans = 0;
  while(f1 != f2){
    if(dep[f1] < dep[f2]){
      swap(f1, f2);
      swap(u, v);
    }
    ans += query(p[f1], p[u], all);
    u = fa[f1];
    f1 = top[u];
  }
  if(dep[u] > dep[v])  swap(u, v);
  return ans += query(p[u], p[v], all);
}

LL ansL[maxn], ansR[maxn];

int main(){
  while(scanf("%d %d", &n, &m) == 2){
    init();
    for(int i = 1; i <= n; ++i){
      scanf("%d", &a[i].x);
      a[i].id = i;
    }
    for(int i = 1; i < n; ++i){
      int u, v;
      scanf("%d %d", &u, &v);
      add(u, v);
      add(v, u);
    }
    dfs1(1, -1, 0);
    dfs2(1, 1);
    for(int i = 0; i < m; ++i){
      scanf("%d %d %d %d", &q[i].s, &q[i].t, &q[i].a, &q[i].b);
      --q[i].a;
      q[i].id = i;
    }
    ms(sum, 0);
    sort(a + 1, a + n + 1, cmpVal);
    sort(q, q + m, cmpL);
    int idx = 1;
    for(int i = 0; i < m; ++i){
      while(idx <= n && a[idx].x <= q[i].a)  update(p[a[idx].id], a[idx].x, all), idx++;
      ansL[q[i].id] = solve(q[i].s, q[i].t);
    }
    ms(sum, 0);
    sort(q, q + m, cmpR);
    idx = 1;
    for(int i = 0; i < m; ++i){
      while(idx <= n && a[idx].x <= q[i].b)  update(p[a[idx].id], a[idx].x, all), idx++;
      ansR[q[i].id] = solve(q[i].s, q[i].t);
    }
    for(int i = 0; i < m; ++i){
      if(i)  putchar(' ');
      printf("%I64d", ansR[i]-ansL[i]);
    }
    printf("\n");
  }
  return 0;
}