标签:cassert 维护 const 区间 opera java limit upd struct
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <algorithm> #include <climits> #include <cstring> #include <string> #include <set> #include <bitset> #include <map> #include <queue> #include <stack> #include <vector> #include <cassert> #include <ctime> #define rep(i,m,n) for(i=m;i<=(int)n;i++) #define mod 1000000007 #define inf 0x3f3f3f3f #define vi vector<int> #define pb push_back #define mp make_pair #define fi first #define se second #define ll long long #define pi acos(-1.0) #define pii pair<int,int> #define sys system("pause") #define ls rt<<1 #define rs rt<<1|1 #define all(x) x.begin(),x.end() const int maxn=1e5+10; const int N=5e5+10; using namespace std; ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);} ll qmul(ll p,ll q,ll mo){ll f=0;while(q){if(q&1)f=(f+p)%mo;p=(p+p)%mo;q>>=1;}return f;} ll qpow(ll p,ll q,ll mo){ll f=1;while(q){if(q&1)f=f*p%mo;p=p*p%mo;q>>=1;}return f;} int n,m,k,t,a[maxn],fa[maxn],dep[maxn],son[maxn],pos[maxn],bl[maxn],id[maxn],dfs_cl,tot,head[maxn],cnt; ll ret[maxn],sum[maxn<<2]; struct node { int l,r,id,x; bool operator<(const node&p)const { return x<p.x; } }qu[maxn<<1]; struct node1 { int to,nxt; }e[maxn<<1]; void add(int x,int y) { e[cnt].to=y; e[cnt].nxt=head[x]; head[x]=cnt++; } bool cmp(int x,int y) { return a[x]<a[y]; } void dfs1(int x,int y) { dep[x]=dep[y]+1; fa[x]=y; son[x]=1; for(int i=head[x];i!=-1;i=e[i].nxt) { int z=e[i].to; if(z==y)continue; dfs1(z,x); son[x]+=son[z]; } } void dfs2(int x,int y,int ch) { int ma=0; pos[x]=++dfs_cl; bl[x]=ch; for(int i=head[x];i!=-1;i=e[i].nxt) { int z=e[i].to; if(z==y)continue; if(son[z]>son[ma])ma=z; } if(ma!=0)dfs2(ma,x,ch); for(int i=head[x];i!=-1;i=e[i].nxt) { int z=e[i].to; if(z==y||z==ma)continue; dfs2(z,x,z); } } void build(int l,int r,int rt) { sum[rt]=0; if(l==r)return; int mid=l+r>>1; build(l,mid,ls); build(mid+1,r,rs); } void pup(int rt) { sum[rt]=sum[ls]+sum[rs]; } void upd(int x,int y,int l,int r,int rt) { if(x==l&&x==r) { sum[rt]+=y; return; } int mid=l+r>>1; if(x<=mid)upd(x,y,l,mid,ls); else upd(x,y,mid+1,r,rs); pup(rt); } ll Query(int L,int R,int l,int r,int rt) { if(L==l&&R==r)return sum[rt]; int mid=l+r>>1; if(R<=mid)return Query(L,R,l,mid,ls); else if(L>mid)return Query(L,R,mid+1,r,rs); else return Query(L,mid,l,mid,ls)+Query(mid+1,R,mid+1,r,rs); } ll gao(int x,int y) { ll ret=0; while(bl[x]!=bl[y]) { if(dep[bl[x]]<dep[bl[y]])swap(x,y); ret+=Query(pos[bl[x]],pos[x],1,n,1); x=fa[bl[x]]; } if(pos[x]>pos[y])swap(x,y); ret+=Query(pos[x],pos[y],1,n,1); return ret; } int main() { int i,j; while(~scanf("%d%d",&n,&m)) { rep(i,1,n)scanf("%d",&a[i]),id[i]=i,head[i]=-1; cnt=0; rep(i,1,n-1) { int x,y; scanf("%d%d",&x,&y); add(x,y);add(y,x); } tot=0; rep(i,1,m) { int x,y,z,w; ret[i]=0; scanf("%d%d%d%d",&x,&y,&z,&w); qu[++tot]=node{x,y,-i,z-1}; qu[++tot]=node{x,y,i,w}; } dfs_cl=0; dfs1(1,0); dfs2(1,0,1); sort(qu+1,qu+tot+1); sort(id+1,id+n+1,cmp); build(1,n,1); int now=1; rep(i,1,tot) { while(now<=n&&a[id[now]]<=qu[i].x) { upd(pos[id[now]],a[id[now]],1,n,1); ++now; } if(qu[i].id<0)ret[-qu[i].id]-=gao(qu[i].l,qu[i].r); else ret[qu[i].id]+=gao(qu[i].l,qu[i].r); } rep(i,1,m)printf("%lld%c",ret[i],i==m?‘\n‘:‘ ‘); } return 0; }
标签:cassert 维护 const 区间 opera java limit upd struct
原文地址:http://www.cnblogs.com/dyzll/p/7413856.html