Ch’s gift
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2144 Accepted Submission(s): 756
Problem Description
Mr. Cui is working off-campus and he misses his girl friend very much. After a whole night tossing and turning, he decides to get to his girl friend's city and of course, with well-chosen gifts. He knows neither too low the price could a gift be since his girl friend won't like it, nor too high of it since he might consider not worth to do. So he will only buy gifts whose price is between [a,b].
There are n cities in the country and (n-1) bi-directional roads. Each city can be reached from any other city. In the ith city, there is a specialty of price ci Cui could buy as a gift. Cui buy at most 1 gift in a city. Cui starts his trip from city s and his girl friend is in city t. As mentioned above, Cui is so hurry that he will choose the quickest way to his girl friend(in other words, he won't pass a city twice) and of course, buy as many as gifts as possible. Now he wants to know, how much money does he need to prepare for all the gifts?
There are n cities in the country and (n-1) bi-directional roads. Each city can be reached from any other city. In the ith city, there is a specialty of price ci Cui could buy as a gift. Cui buy at most 1 gift in a city. Cui starts his trip from city s and his girl friend is in city t. As mentioned above, Cui is so hurry that he will choose the quickest way to his girl friend(in other words, he won't pass a city twice) and of course, buy as many as gifts as possible. Now he wants to know, how much money does he need to prepare for all the gifts?
Input
There are multiple cases.
For each case:
The first line contains tow integers n,m(1≤n,m≤10^5), representing the number of cities and the number of situations.
The second line contains n integers c1,c2,...,cn(1≤ci≤10^9), indicating the price of city i's specialty.
Then n-1 lines follows. Each line has two integers x,y(1≤x,y≤n), meaning there is road between city x and city y.
Next m line follows. In each line there are four integers s,t,a,b(1≤s,t≤n;1≤a≤b≤10^9), which indicates start city, end city, lower bound of the price, upper bound of the price, respectively, as the exact meaning mentioned in the description above
For each case:
The first line contains tow integers n,m(1≤n,m≤10^5), representing the number of cities and the number of situations.
The second line contains n integers c1,c2,...,cn(1≤ci≤10^9), indicating the price of city i's specialty.
Then n-1 lines follows. Each line has two integers x,y(1≤x,y≤n), meaning there is road between city x and city y.
Next m line follows. In each line there are four integers s,t,a,b(1≤s,t≤n;1≤a≤b≤10^9), which indicates start city, end city, lower bound of the price, upper bound of the price, respectively, as the exact meaning mentioned in the description above
Output
Output m space-separated integers in one line, and the ith number should be the answer to the ith situation.
Sample Input
5 3 1 2 1 3 2 1 2 2 4 3 1 2 5 4 5 1 3 1 1 1 1 3 5 2 3
Sample Output
7 1 4
Source
【代码】
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=8e5+5;
struct tree{
int cnt,L[N],R[N],T[N]; //p地址,LR孩子//根指针
ll sum[4*N];
void init()
{
cnt=0; //address
memset(L,0,sizeof(L));
memset(R,0,sizeof(R));
memset(T,0,sizeof(T));
memset(sum,0,sizeof(sum));
}
int creat(int l,int r) //返回地址.数值[l,r]
{
int p=++cnt; //新地址
if(l==r) //叶子节点
{
sum[p]=0; //初始
return p;
}
ll mid=(l+r)/2;
L[p]=creat(l,mid); //左子树的创建
R[p]=creat(mid+1,r); //右子树
sum[p]=sum[L[p]]+sum[R[p]];
return p;
}
int addtree(int pre,int l,int r,int pos,ll num)//由pre树pos叶子增加num产生新树
{
int p=++cnt; //新地址
sum[p]=sum[pre];
L[p]=L[pre];
R[p]=R[pre];
if(l==r){
sum[p]+=num;
return p;
}
ll mid=(l+r)/2;
if(pos<=mid)
L[p]=addtree(L[pre],l,mid,pos,num);
if(pos>mid)
R[p]=addtree(R[pre],mid+1,r,pos,num);
sum[p]=sum[L[p]]+sum[R[p]];
return p;
}
ll Qsum(int pre,int l,int r,int ql,int qr) //pre树[ql,qr]区间和
{
if(ql<=l&&r<=qr)
return sum[pre];
ll mid=(l+r)/2;
ll t1=0,t2=0;
if(ql<=mid)
t1=Qsum(L[pre],l,mid,ql,qr); //区间在左子树
if(qr>mid)
t2=Qsum(R[pre],mid+1,r,ql,qr); //在右子树
return t1+t2;
}
}C;
ll sta[N],top;
int dex(ll num){
return lower_bound(sta+1,sta+1+top,num)-sta;
}
struct node{
int v,len,next;
}e[N];
int head[N],cnt;
void add(int u,int v,int len){ //树
e[cnt]=node{v,len,head[u]};
head[u]=cnt++;
}
struct query{
int u,v;
ll x,y;
int lca,next;
}eq[N];
int hq[N],cq;
void addq(int u,int v,ll x,ll y){ //询问
eq[cq]=query{u,v,x,y,0,hq[u]};
hq[u]=cq++;
}
void lcainit()
{
cq=cnt=0;
memset(head,-1,sizeof(head));
memset(hq,-1,sizeof(hq));
}
int n,m;
ll a[N];
bool vis[N];
int fa[N]; //tarjan祖先
int father(int x){return x==fa[x]?x:fa[x]=father(fa[x]);} //找祖先
void dfs(int u) //tarjan处理LCA
{
vis[u]=1;
fa[u]=u;
for(int i=head[u];~i;i=e[i].next)if(!vis[e[i].v]){
C.T[e[i].v]=C.addtree(C.T[u],1,top,dex( a[e[i].v] ),a[e[i].v]); //添加结点
//cout<<"addtree:"<<e[i].v<<' '<<a[e[i].v]<<' '<<dex(a[e[i].v])<<endl;
dfs(e[i].v);
fa[e[i].v]=u; //必须在dfs后边
}
for(int i=hq[u];~i;i=eq[i].next){
if(vis[eq[i].v])
eq[i^1].lca=eq[i].lca=father(eq[i].v);
}
}
int main()
{
while(cin>>n>>m)
{
int u,v;
ll x,y;
top=0;
for(int i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
sta[++top]=a[i];
}
lcainit();
for(int i=1;i<n;i++){
scanf("%d%d",&u,&v);
add(u,v,0);
add(v,u,0);
}
for(int i=0;i<m;i++)
{
scanf("%d%d%lld%lld",&u,&v,&x,&y);
sta[++top]=x;
sta[++top]=y;
addq(u,v,x,y);
addq(v,u,x,y);
}
sort(sta+1,sta+top+1);
top=unique(sta+1,sta+top+1)-sta-1;
C.init();
C.T[0]=C.creat(1,top);
C.T[1]=C.addtree(C.T[0],1,top,dex(a[1]),a[1]);
memset(vis,0,sizeof(vis));
dfs(1); //LCA
for(int i=0;i<m;i++)
{
query &q=eq[i*2];
if(i)putchar(' ');
ll ans=C.Qsum(C.T[q.u],1,top,dex(q.x),dex(q.y));
ans+=C.Qsum(C.T[q.v],1,top,dex(q.x),dex(q.y));
ans-=2*C.Qsum(C.T[q.lca],1,top,dex(q.x),dex(q.y));
if(q.x<=a[q.lca]&&a[q.lca]<=q.y) ans+=a[q.lca];
printf("%lld",ans);
}
puts("");
}
}
/*
6 5
3 5 9 2 7 4
1 2
2 4
2 5
1 3
3 6
1 4 2 3
5 6 7 9
2 3 3 3
6 1 4 9
4 3 9 9
*/