天天看点

2017多校训练赛第九场 HDU 6162(离线处理+树链剖分+线段树解法)

Ch’s gift

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 1091    Accepted Submission(s): 412

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 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

​​2017 Multi-University Training Contest - Team 9​​

        离线处理的题目千千万万,但很多时候还是能够让你耳目一新……

#include<bits/stdc++.h>
#define LL long long
#define N 100100
using namespace std;

int id[N],Rank[N],top[N],son[N],size[N],fa[N],dep[N];
struct Query{int x,y,id,val,on;} q[N<<1];
struct Val{int val,id;} a[N];
int num,n,m,Q;
LL ans[N][2];
vector<int> g[N];

struct ST
{
    struct node
    {
        int l,r;
        LL sum;
    } tree[N<<2];

    inline void push_up(int i)
    {
        tree[i].sum=tree[i<<1].sum+tree[i<<1|1].sum;
    }

    inline void build(int i,int l,int r)
    {
        tree[i].r=r;
        tree[i].l=l;
        if (l==r)
        {
            tree[i].sum=0; return;
        }
        int mid=(l+r)>>1;
        build(i<<1,l,mid);
        build(i<<1|1,mid+1,r);
        push_up(i);
    }

    inline void update(int i,int l,int r,LL x)
    {
        if ((tree[i].l==l)&&(tree[i].r==r))
        {
            tree[i].sum+=x;
            return;
        }
        int mid=(tree[i].l+tree[i].r)>>1;
        if (mid>=r) update(i<<1,l,r,x);
        else if (mid<l) update(i<<1|1,l,r,x);
        else
        {
            update(i<<1,l,mid,x);
            update(i<<1|1,mid+1,r,x);
        }
        push_up(i);
    }

    inline LL getsum(int i,int l,int r)
    {
        if ((tree[i].l==l)&&(tree[i].r==r)) return tree[i].sum;
        int mid=(tree[i].l+tree[i].r)>>1;
        if (mid>=r) return getsum(i<<1,l,r);
        else if (mid<l) return getsum(i<<1|1,l,r);
        else return getsum(i<<1,l,mid)+getsum(i<<1|1,mid+1,r);
    }
} seg;


inline void dfs1(int u,int d,int f)
{
    son[u]=0;
    dep[u]=d;
    size[u]=1;
    for(int i=0;i<g[u].size();i++)
        if (g[u][i]!=f)
        {
            fa[g[u][i]]=u;
            dfs1(g[u][i],d+1,u);
            size[u]+=size[g[u][i]];
            if (size[g[u][i]]>size[son[u]]) son[u]=g[u][i];
        }
}

inline void dfs2(int u,int f)
{
    top[u]=f;
    id[u]=++num;
    Rank[id[u]]=u;
    if (son[u]) dfs2(son[u],f);
    for(int i=0;i<g[u].size();i++)
        if (g[u][i]!=son[u]&&g[u][i]!=fa[u]) dfs2(g[u][i],g[u][i]);
}

inline LL query(int u, int v)
{
    int tp1 = top[u], tp2 = top[v];
    LL res = 0;
    while (tp1 != tp2)
    {
        if (dep[tp1] < dep[tp2])
        {
            swap(tp1, tp2);
            swap(u, v);
        }
        res+=seg.getsum(1,id[tp1],id[u]);
        u = fa[tp1]; tp1 = top[u];
    }
    if (dep[u] > dep[v]) swap(u, v);
    res+=seg.getsum(1,id[u],id[v]);
    return res;
}

bool cmp1(Val a,Val b)
{
    return a.val<b.val;
}

bool cmp2(Query a,Query b)
{
    return a.val<b.val;
}

int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        num=Q=0;
        memset(g,0,sizeof(g));
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i].val);
            a[i].id=i;
        }
        sort(a+1,a+1+n,cmp1);
        for(int i=1;i<n;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            g[u].push_back(v);
            g[v].push_back(u);
        }
        dfs1(1,1,1); dfs2(1,1);                        //树链剖分
        for(int i=1;i<=m;i++)
        {
            int x,y,l,r;
            scanf("%d%d%d%d",&x,&y,&l,&r);
            q[++Q]=Query{x,y,i,l-1,0};                    //对于每一个询问,把它拆成两个
            q[++Q]=Query{x,y,i,r,1};                    //on为0表示为左端点,为1表示右端点
        }
        sort(q+1,q+1+Q,cmp2);
        seg.build(1,1,n);
        for(int i=1,j=1;i<=Q;i++)
        {
            for(;q[i].val>=a[j].val&&j<=n;j++)                //离线顺序更新点权
                seg.update(1,id[a[j].id],id[a[j].id],a[j].val);
            ans[q[i].id][q[i].on]=query(q[i].x,q[i].y);            //计算结果
        }
        for(int i=1;i<m;i++)
            printf("%I64d ",ans[i][1]-ans[i][0]);            //两值相减即为结果
        printf("%I64d\n",ans[m][1]-ans[m][0]);
    }
    return 0;
}