| Run ID | 作者 | 问题 | 语言 | 测评结果 | 分数 | 时间 | 内存 | 代码长度 | 提交时间 |
|---|---|---|---|---|---|---|---|---|---|
| 39958 | 氩_wjy | 【S】T1 | C++ | 运行出错 | 0 | 1040 MS | 401176 KB | 2693 | 2026-02-09 20:41:28 |
#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],t[N<<2]; int vc[N][2]; int cnt=0; struct IO{ char buf[1<<20],*S=buf,*E=buf; inline char gc(){ (S==E)&&(E=(S=buf)+fread(buf,1,1<<20,stdin)); return S==E?EOF:*S++; } char buf2[1<<20];int topp=0; inline void flush(){ fwrite(buf2,1,topp,stdout);topp=0; } inline void pc(char c){ buf2[topp++]=c; if(topp==(1<<20)) fwrite(buf2,1,topp,stdout),topp=0; } inline int read(){ int t=0,f=1;char ch=gc(); while(!isdigit(ch)){ if(ch=='-') f=-1;ch=gc(); }while(isdigit(ch)) t=t*10+ch-'0',ch=gc(); return t*f; }char sta[22]; inline void write(long long x){ int top=0;(x<0)?pc('-'),x=-x:x=x; if(x==0) pc('0'); while(x>0) sta[++top]=x%10+'0',x/=10; while(top>0) pc(sta[top--]); } ~IO(){flush();} IO &operator>>(int &n){n=read();return *this;} IO &operator<<(int n){write(n);return *this;} IO &operator<<(char ch){pc(ch);return *this;} }fastio; signed main(){ #ifndef ONLINE_JUDGE freopen("count.in","r",stdin); freopen("count.out","w",stdout); #endif fastio>>n>>q; for(int i=1;i<=n;i++)fastio>>a[i]; for(int i=1;i<n;i++){ int u,v;fastio>>u>>v; E[u].push_back(v); E[v].push_back(u); } for(int i=1;i<=q;i++){ int p,l,r; fastio>>p>>l>>r; if(E[p].size()==1){ Ans[i]=(l<=E[p][0]&&E[p][0]<=r)*a[E[p][0]]; continue; } if(l-1){ t[++cnt]=OP(l-1,p,-1,i);vc[l-1][1]++; } t[++cnt]=OP(r,p,1,i);vc[r][1]++; } for(int u=1;u<=n;u++){ for(auto v:E[u])t[++cnt]=OP(v,u,0,0),vc[v][0]++; } for(int i=1;i<=n;i++){ for(int j=0;j<2;j++){ if(i>1)vc[i][j]+=vc[i-1][j]; if(j)vc[i][j]+=vc[i][j-1]; } } for(int i=1;i<=cnt;i++){ if(t[i].x)qq[vc[t[i].x][abs(t[i].op)]--]=t[i]; } 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++)fastio<<Ans[i]<<endl; fflush(stdout); // cerr<<abs(&ST-&ED)/1048576.0<<endl; return 0; }