提交时间:2026-02-09 20:17:10

运行 ID: 39945

#include<bits/stdc++.h> // #define int long long #define lb(x) ((x)&(-(x))) #define endl '\n' using namespace std; const int N=2e6+7; vector<int> E[N]; int n,q,Sum[N]; int a[N],Ans[N]; struct OP{ int x,Con,op,id; OP(int _x=0,int _Con=0,int _op=0,int _id=0){ x=_x,Con=_Con,op=_op,id=_id; } //op=0:a[v],v,u,0,0; //op=1:0,r,v,1,qid; bool operator<(OP r){return x==r.x?abs(op)<abs(r.op):x<r.x;} }qq[N<<2]; vector<OP> vc[N][2]; int cnt=0; // #define DEBUG 1 // 调试开关 struct IO { #define MAXSIZE (1 << 20) #define isdigit(x) (x >= '0' && x <= '9') char buf[MAXSIZE], *p1, *p2; char pbuf[MAXSIZE], *pp; #if DEBUG #else IO() : p1(buf), p2(buf), pp(pbuf) {} ~IO() { fwrite(pbuf, 1, pp - pbuf, stdout); } #endif char gc() { #if DEBUG // 调试,可显示字符 return getchar(); #endif if (p1 == p2) p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin); return p1 == p2 ? ' ' : *p1++; } void read(int &x) { bool neg = false; x = 0; char ch = gc(); for (; !isdigit(ch); ch = gc()) if (ch == '-') neg = true; if (neg) for (; isdigit(ch); ch = gc()) x = x * 10 + ('0' - ch); else for (; isdigit(ch); ch = gc()) x = x * 10 + (ch - '0'); } void read(char *s) { char ch = gc(); for (; isspace(ch); ch = gc()); for (; !isspace(ch); ch = gc()) *s++ = ch; *s = 0; } void read(char &c) { for (c = gc(); isspace(c); c = gc()); } void push(const char &c) { #if DEBUG // 调试,可显示字符 putchar(c); #else if (pp - pbuf == MAXSIZE) fwrite(pbuf, 1, MAXSIZE, stdout), pp = pbuf; *pp++ = c; #endif } void write(int x) { bool neg = false; if (x < 0) { neg = true; push('-'); } static int sta[40]; int top = 0; do { sta[top++] = x % 10; x /= 10; } while (x); if (neg) while (top) push('0' - sta[--top]); else while (top) push('0' + sta[--top]); } void write(int x, char lastChar) { write(x), push(lastChar); } } io; signed main(){ #ifndef ONLINE_JUDGE freopen("count.in","r",stdin); freopen("count.out","w",stdout); #endif io.read(n),io.read(q); for(int i=1;i<=n;i++)io.read(a[i]); for(int i=1;i<n;i++){ int u,v;io.read(u),io.read(v); E[u].push_back(v); E[v].push_back(u); } for(int i=1;i<=q;i++){ int p,l,r; io.read(p),io.read(l),io.read(r); if(E[p].size()==1){ Ans[i]=(l<=E[p][0]&&E[p][0]<=r)*a[E[p][0]]; continue; } vc[l-1][1].push_back(OP(l-1,p,-1,i)); vc[r][1].push_back(OP(r,p,1,i)); } for(int u=1;u<=n;u++){ for(auto v:E[u])vc[v][0].push_back(OP(v,u,0,0)); } for(int i=1;i<=n;i++)for(int j=0;j<2;j++){ for(auto x:vc[i][j])qq[++cnt]=x; } for(int i=1;i<=cnt;i++){ if(qq[i].op==0)Sum[qq[i].Con]+=a[qq[i].x]; else Ans[qq[i].id]+=Sum[qq[i].Con]*qq[i].op; } for(int i=1;i<=q;i++)io.write(Ans[i],'\n'); fflush(stdout); // cerr<<abs(&ST-&ED)/1048576.0<<endl; return 0; }