标签:ase desc star team while last NPU family +=
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2534 Accepted Submission(s): 887
http://acm.hdu.edu.cn/showproblem.php?pid=6162
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define ll long long 4 #define N 100050 5 #define INF 123456789 6 int n,m; 7 int tot,last[N]; 8 ll ans[N]; 9 int cnt,fa[N],dp[N],size[N],son[N],rk[N],kth[N],top[N]; 10 struct Query 11 { 12 int l,r,id; ll val; 13 bool operator <(const Query&b)const 14 {return val<b.val;} 15 }a[N],que[N<<1]; 16 struct Edge{int from,to,s;}edges[N<<1]; 17 struct Tree{int l,r;ll sum;}tr[N<<2]; 18 template<typename T>void read(T&x) 19 { 20 ll k=0; char c=getchar(); 21 x=0; 22 while(!isdigit(c)&&c!=EOF)k^=c==‘-‘,c=getchar(); 23 if (c==EOF)exit(0); 24 while(isdigit(c))x=x*10+c-‘0‘,c=getchar(); 25 x=k?-x:x; 26 } 27 void read_char(char &c) 28 {while(!isalpha(c=getchar())&&c!=EOF);} 29 void AddEdge(int x,int y) 30 { 31 edges[++tot]=Edge{x,y,last[x]}; 32 last[x]=tot; 33 } 34 void dfs1(int x,int pre) 35 { 36 fa[x]=pre; 37 dp[x]=dp[pre]+1; 38 size[x]=1; 39 son[x]=0; 40 for(int i=last[x];i;i=edges[i].s) 41 { 42 Edge &e=edges[i]; 43 if (e.to==pre)continue; 44 dfs1(e.to,x); 45 size[x]+=size[e.to]; 46 if (size[e.to]>size[son[x]])son[x]=e.to; 47 } 48 } 49 void dfs2(int x,int y) 50 { 51 rk[x]=++cnt; 52 kth[cnt]=x; 53 top[x]=y; 54 if (son[x]==0)return; 55 dfs2(son[x],y); 56 for(int i=last[x];i;i=edges[i].s) 57 { 58 Edge &e=edges[i]; 59 if (e.to==fa[x]||e.to==son[x])continue; 60 dfs2(e.to,e.to); 61 } 62 } 63 void bt(int x,int l,int r) 64 { 65 tr[x].l=l; tr[x].r=r; tr[x].sum=0; 66 if (l==r)return; 67 int mid=(l+r)>>1; 68 bt(x<<1,l,mid); 69 bt(x<<1|1,mid+1,r); 70 } 71 void update(int x,int p,ll tt) 72 { 73 if (p<=tr[x].l&&tr[x].r<=p) 74 { 75 tr[x].sum+=tt; 76 return; 77 } 78 int mid=(tr[x].l+tr[x].r)>>1; 79 if (p<=mid)update(x<<1,p,tt); 80 if (mid<p)update(x<<1|1,p,tt); 81 tr[x].sum=tr[x<<1].sum+tr[x<<1|1].sum; 82 } 83 ll query(int x,int l,int r) 84 { 85 if (l<=tr[x].l&&tr[x].r<=r) 86 return tr[x].sum; 87 int mid=(tr[x].l+tr[x].r)>>1; ll ans=0; 88 if (l<=mid)ans+=query(x<<1,l,r); 89 if (mid<r)ans+=query(x<<1|1,l,r); 90 return ans; 91 } 92 ll get_sum(int x,int y) 93 { 94 int fx=top[x],fy=top[y];ll ans=0; 95 while(fx!=fy) 96 { 97 if (dp[fx]<dp[fy])swap(x,y),swap(fx,fy); 98 ans+=query(1,rk[fx],rk[x]); 99 x=fa[fx]; fx=top[x]; 100 } 101 if (dp[x]<dp[y])swap(x,y); 102 ans+=query(1,rk[y],rk[x]); 103 return ans; 104 } 105 void work() 106 { 107 read(n); read(m); 108 for(int i=1;i<=n;i++)read(a[i].val),a[i].id=i; 109 for(int i=1;i<=n-1;i++) 110 { 111 int x,y; 112 read(x); read(y); 113 AddEdge(x,y); 114 AddEdge(y,x); 115 } 116 int num=0; 117 for(int i=1;i<=m;i++) 118 { 119 int l,r,x,y; 120 read(l); read(r); read(x);read(y); 121 que[++num]=Query{l,r,-i,x-1}; 122 que[++num]=Query{l,r,i,y}; 123 } 124 sort(a+1,a+n+1); 125 sort(que+1,que+num+1); 126 dfs1(1,0); 127 dfs2(1,1); 128 bt(1,1,n); 129 int ds=1; 130 for(int i=1;i<=num;i++) 131 { 132 while(ds<=n&&a[ds].val<=que[i].val) 133 { 134 update(1,rk[a[ds].id],a[ds].val); 135 ds++; 136 } 137 ll sum=get_sum(que[i].l,que[i].r); 138 if (que[i].id<0) ans[-que[i].id]-=sum; 139 else ans[que[i].id]+=sum; 140 } 141 printf("%lld",ans[1]); 142 for(int i=2;i<=m;i++)printf(" %lld",ans[i]); 143 printf("\n"); 144 } 145 void clear() 146 { 147 tot=0; cnt=0; 148 memset(last,0,sizeof(last)); 149 memset(ans,0,sizeof(ans)); 150 } 151 int main() 152 { 153 #ifndef ONLINE_JUDGE 154 freopen("aa.in","r",stdin); 155 //freopen("my.out","w",stdout); 156 #endif 157 while(1) 158 { 159 clear(); 160 work(); 161 } 162 }
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define ll long long 4 #define N 100050 5 #define INF 123456789 6 int n,m,w[N];ll b[N]; 7 int tot,last[N]; 8 int tree_num,root[N]; 9 int cnt,fa[N],dp[N],size[N],son[N],rk[N],kth[N],top[N]; 10 struct Edge{int from,to,s;}edges[N<<1]; 11 struct Tree{int l,r,ls,rs;ll sum;}tr[2500000]; 12 template<typename T>void read(T&x) 13 { 14 ll k=0; char c=getchar(); 15 x=0; 16 while(!isdigit(c)&&c!=EOF)k^=c==‘-‘,c=getchar(); 17 if (c==EOF)exit(0); 18 while(isdigit(c))x=x*10+c-‘0‘,c=getchar(); 19 x=k?-x:x; 20 } 21 void read_char(char &c) 22 {while(!isalpha(c=getchar())&&c!=EOF);} 23 void AddEdge(int x,int y) 24 { 25 edges[++tot]=Edge{x,y,last[x]}; 26 last[x]=tot; 27 } 28 void dfs1(int x,int pre) 29 { 30 fa[x]=pre; 31 dp[x]=dp[pre]+1; 32 size[x]=1; 33 son[x]=0; 34 for(int i=last[x];i;i=edges[i].s) 35 { 36 Edge &e=edges[i]; 37 if (e.to==pre)continue; 38 dfs1(e.to,x); 39 size[x]+=size[e.to]; 40 if (size[e.to]>size[son[x]])son[x]=e.to; 41 } 42 } 43 void dfs2(int x,int y) 44 { 45 rk[x]=++cnt; 46 kth[cnt]=x; 47 top[x]=y; 48 if (son[x]==0)return; 49 dfs2(son[x],y); 50 for(int i=last[x];i;i=edges[i].s) 51 { 52 Edge &e=edges[i]; 53 if (e.to==fa[x]||e.to==son[x])continue; 54 dfs2(e.to,e.to); 55 } 56 } 57 void bt(int &x,int l,int r) 58 { 59 x=++tree_num; 60 tr[x].l=l; tr[x].r=r; tr[x].sum=0; 61 if (l==r)return; 62 int mid=(l+r)>>1; 63 bt(tr[x].ls,l,mid); 64 bt(tr[x].rs,mid+1,r); 65 } 66 void add(int &x,int last,int p) 67 { 68 x=++tree_num; 69 tr[x]=tr[last]; 70 tr[x].sum+=b[p]; 71 if (tr[x].l==tr[x].r)return; 72 int mid=(tr[x].l+tr[x].r)>>1; 73 if(p<=mid)add(tr[x].ls,tr[last].ls,p); 74 else add(tr[x].rs,tr[last].rs,p); 75 } 76 ll ask(int x,int y,int p) 77 { 78 if (tr[x].r<=p)return tr[y].sum-tr[x].sum; 79 int mid=(tr[x].l+tr[x].r)>>1;ll ans=0; 80 if (1<=mid)ans+=ask(tr[x].ls,tr[y].ls,p); 81 if (mid<p)ans+=ask(tr[x].rs,tr[y].rs,p); 82 return ans; 83 } 84 ll get_sum(int x,int y,int tt) 85 { 86 int fx=top[x],fy=top[y];ll ans=0; 87 while(fx!=fy) 88 { 89 if (dp[fx]<dp[fy])swap(x,y),swap(fx,fy); 90 ans+=ask(root[rk[fx]-1],root[rk[x]],tt); 91 x=fa[fx]; fx=top[x]; 92 } 93 if (dp[x]<dp[y])swap(x,y); 94 ans+=ask(root[rk[y]-1],root[rk[x]],tt); 95 return ans; 96 } 97 void work() 98 { 99 read(n); read(m); 100 int num=0; 101 for(int i=1;i<=n;i++)read(w[i]),b[++num]=w[i]; 102 b[++num]=INF; 103 for(int i=1;i<=n-1;i++) 104 { 105 int x,y; 106 read(x); read(y); 107 AddEdge(x,y); 108 AddEdge(y,x); 109 } 110 sort(b+1,b+num+1); 111 num=unique(b+1,b+num+1)-b-1; 112 dfs1(1,0); 113 dfs2(1,1); 114 bt(root[0],1,num); 115 for(int i=1;i<=n;i++) 116 { 117 int tt=lower_bound(b+1,b+num+1,w[kth[i]])-b; 118 add(root[i],root[i-1],tt); 119 } 120 for(int i=1;i<=m;i++) 121 { 122 if (i>1)printf(" "); 123 int x,y,l,r; 124 read(x); read(y); read(l); read(r); 125 l=lower_bound(b+1,b+num+1,l)-b-1; 126 r=upper_bound(b+1,b+num+1,r)-b-1; 127 ll ans=get_sum(x,y,r); 128 ans-=get_sum(x,y,l); 129 printf("%lld",ans); 130 } 131 printf("\n"); 132 } 133 void clear() 134 { 135 tot=0; cnt=0; tree_num=0; 136 memset(last,0,sizeof(last)); 137 } 138 int main() 139 { 140 #ifndef ONLINE_JUDGE 141 freopen("aa.in","r",stdin); 142 #endif 143 while(1) 144 { 145 clear(); 146 work(); 147 } 148 }
标签:ase desc star team while last NPU family +=
原文地址:https://www.cnblogs.com/mmmqqdd/p/10799395.html