hdu-6162

来源:互联网 发布:英译汉软件免费下载 编辑:程序博客网 时间:2024/05/29 12:52

Ch’s gift

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 642    Accepted Submission(s): 225


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?
 

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
 

Output
Output m space-separated integers in one line, and the ith number should be the answer to the ith situation.
 

Sample Input
5 31 2 1 3 21 22 43 12 54 5 1 31 1 1 13 5 2 3
 

Sample Output

7 1 4

离线操作,将a,b分开,然后一起排序,类似与cdq分治的前面预处理。

之后就是树链剖分+树状数组。

#include"bits/stdc++.h"using namespace std;typedef long long LL;const int maxn = 1e5+7;int n,m,tot;LL sum[maxn];LL ans[maxn];int fa[maxn],top[maxn],siz[maxn],son[maxn],dep[maxn],id[maxn],rear;struct dot{    int i,val;    bool operator < (const dot &_)const{        return _.val > val;    }}G[maxn];struct edge{    int v,nxt;    edge(){}    edge(int v, int nxt):v(v), nxt(nxt){}}e[maxn<<1];int tail,head[maxn];struct node{    int ty,u,v,l,id;    node(){}    node(int ty,int u, int v, int l, int id): ty(ty), u(u), v(v), l(l), id(id){}    bool operator < (const node &a)const{        return a.l > l;    }}qr[maxn<<1];void init(){    rear = tot = 0;    tail = -1;    memset(head,-1,sizeof(head));    memset(sum,0,sizeof(sum));    memset(ans,0,sizeof(ans));}void add(int u, int v){    e[++tail] = edge(v,head[u]);    head[u] = tail;}void update(int id, int val){    for(int i = id; i <= n; i += i&(-i)){        sum[i] += val;    }}LL query(int x){    LL ret = 0;    for(int i = x; i; i -= i&(-i)){        ret += sum[i];    }    return ret;}void DFS1(int u, int f, int d){    fa[u] = f; dep[u] = d;    son[u] = 0; siz[u] = 1;    for(int i = head[u]; ~i; i = e[i].nxt){        int v = e[i].v;        if(v == f) continue;        DFS1(v,u,d+1);        siz[u] += siz[v];        if(siz[son[u]] < siz[v]){            son[u] = v;        }    }}void DFS2(int u, int tp){    top[u] = tp;    id[u] = ++rear;    if(son[u]) DFS2(son[u],tp);    for(int i = head[u]; ~i; i = e[i].nxt){        int v = e[i].v;        if(v == fa[u] || v == son[u]) continue;        DFS2(v,v);    }}LL HLD_query(int c,int u,int v){    int tp1 = top[u], tp2 = top[v];    LL sum = 0;    while(tp1 != tp2){        if(dep[tp1] < dep[tp2]){            swap(u,v);            swap(tp1,tp2);        }        sum += c*(query(id[u]) - query(id[tp1]-1) );        u = fa[tp1]; tp1 = top[u];    }    if(dep[u] > dep[v]) swap(u,v);    sum += c*( query(id[v])-query(id[u]-1) );    return sum;}int main(){    while(~scanf("%d%d",&n,&m)){        init();        for(int i = 1; i<= n; i++){            int x;            scanf("%d",&x);            G[i].i = i;    G[i].val = x;        }        for(int i = 1; i < n; i++){            int u,v;            scanf("%d%d",&u,&v);            add(u,v);            add(v,u);        }        int mm = m*2;        for(int i = 1; i <= m; i++){            int u,v,l,r;            scanf("%d%d%d%d",&u,&v,&l,&r);            qr[2*i-1] = node(-1,u,v,l-1,i);            qr[2*i] = node(1,u,v,r,i);        }        sort(G+1,G+1+n);        sort(qr+1,qr+1+mm);        DFS1(1,0,1);        DFS2(1,1);        int st = 1;        for(int i = 1; i <= mm; i++){            while(G[st].val <= qr[i].l && st <=n) update(id[G[st].i],G[st].val),  st++;            ans[qr[i].id] += HLD_query(qr[i].ty, qr[i].u, qr[i].v);        }        for(int i = 1; i <= m; i++){            if(i > 1) printf(" ");            printf("%I64d",ans[i]);        }        puts("");    }    return 0;}