HDU-6162---Ch’s gift (树链剖分+离线)(2017多校9)

来源:互联网 发布:firefly ubuntu 编辑:程序博客网 时间:2024/06/08 11:59

Ch’s gift

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 638    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

题意:n个数,m次查询x y a b,每次查询x-y的点中点权在a-b之间的所有点的点权和;
思路:应该是很容易想到用树链剖分,但是这题由于计算点权和的时候有要求,所以需要离线处理,对于一组询问x y a b,开始一直想着这个整体要怎么去排序才能符合题目要求,发现怎么高都不行,然后同学讲了一个思路把询问的左右区间分开,那么现在就很好处理了;把一组询问分成两组:x y a-1、x y b,(为什么是a-1,好好想一想)所有的询问就变成了x y z的形式,然后对所有的询问按z从小到大排序,每次把小于z的数更新到线段树里面,然后查询x y区间和就能求出x-y内小于等于z的和,最后用(x y b)-(x y a-1)就刚好得到每次询问的值,知道方法了剩下的就是简单的树链剖分加上线段树的简单操作了~

AC代码:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>#include<vector>#include<map>#include<functional>#define inf 0xfffffff#define maxn 100100#define ll  segtree[root].l#define rr  segtree[root].r#define ls root<<1#define rs root<<1|1using namespace std;//priority_queue<int,vector<int>,greater<int> > que;int n,m;int tot,num;int head[maxn],top[maxn],son[maxn],fa[maxn],sz[maxn],dep[maxn],id[maxn];struct val{    long long val;    int id;}arr[maxn];struct E{    int to;    int next;}edge[maxn*2];struct node{    int l;    int r;    long long int val;}segtree[maxn*4];struct tt{    int x;    int y;    int k;    int id;    long long val;}q[maxn*2];bool cmp1(tt a,tt b){    return a.k<b.k;}bool cmp2(val a,val b){    if(a.val==b.val)        return a.id<b.id;    return a.val<b.val;}bool cmp3(tt a,tt b){    if(a.id==b.id)        return a.k<b.k;    return a.id<b.id;}void add_edge(int u,int v){    edge[tot].to=v;    edge[tot].next=head[u];    head[u]=tot++;}void dfs1(int u,int f,int deep){    son[u]=0;    fa[u]=f;    sz[u]=1;    dep[u]=deep;    for(int i=head[u];i!=-1;i=edge[i].next)    {        int ff=edge[i].to;        if(ff==fa[u])continue;        dfs1(ff,u,deep+1);        sz[u]+=sz[ff];        if(sz[son[u]]<sz[ff])            son[u]=ff;    }}void dfs2(int u,int tp){    top[u]=tp;    id[u]=++num;    if(son[u]) dfs2(son[u],tp);    for(int i=head[u];i!=-1;i=edge[i].next)    {        int ff=edge[i].to;        if(ff==fa[u]||ff==son[u]) continue;        dfs2(ff,ff);    }}void build(int root,int l,int r){    ll=l;rr=r;    segtree[root].val=0;    if(l==r) return;    int mid=(l+r)>>1;    build(ls,l,mid);    build(rs,mid+1,r);}void pushup(int root){    segtree[root].val=segtree[ls].val+segtree[rs].val;}void update(int root,int p,long long val){    if(ll==rr)    {        segtree[root].val=val;        return;    }    int mid=(ll+rr)>>1;    if(p<=mid) update(ls,p,val);    else update(rs,p,val);    pushup(root);}long long query(int root,int l,int r){    if(l<=ll&&rr<=r)        return segtree[root].val;    int mid=(ll+rr)>>1;    long long ans=0;    if(l<=mid)        ans+=query(ls,l,r);    if(r>mid)        ans+=query(rs,l,r);    return ans;}long long Yougth(int u,int v){    int tp1=top[u];    int tp2=top[v];    long long ans=0;    while(tp1!=tp2)    {        if(dep[tp1]<dep[tp2])        {            swap(u,v);            swap(tp1,tp2);        }        ans+=query(1,id[tp1],id[u]);        u=fa[tp1];        tp1=top[u];    }    if(dep[u]>dep[v])swap(u,v);    ans+=query(1,id[u],id[v]);    return ans;}void init(){    tot=num=0;    memset(head,-1,sizeof(head));}int main(){    while(~scanf("%d%d",&n,&m))    {        init();        for(int i=1;i<=n;i++)        {            scanf("%lld",&arr[i].val);            arr[i].id=i;        }        int x,y;        for(int i=1;i<n;i++)        {            scanf("%d%d",&x,&y);            add_edge(x,y);            add_edge(y,x);        }        dfs1(1,0,1);        dfs2(1,1);        build(1,1,n);        int l,r,cnt=0;        for(int i=1;i<=m;i++)        {            scanf("%d%d%d%d",&x,&y,&l,&r);            q[++cnt].x=x;q[cnt].y=y;q[cnt].k=l-1;q[cnt].id=i;//左边,对每次询问分开后两组记录相同的id,最后输出只需要按id排序然后简单处理一下就好            q[++cnt].x=x;q[cnt].y=y;q[cnt].k=r;q[cnt].id=i;//右边        }        sort(q+1,q+cnt+1,cmp1);        sort(arr+1,arr+1+n,cmp2);        int p=1,tmp=1;        while(tmp<=cnt)        {            long long f=q[tmp].k;            while(arr[p].val<=f&&p<=n)            {                update(1,id[arr[p].id],arr[p].val);                 p++;            }            q[tmp].val=Yougth(q[tmp].x,q[tmp].y);            tmp++;        }        sort(q+1,q+cnt+1,cmp3);        for(int i=1;i<=cnt-1;i+=2)        {            printf("%lld%c",q[i+1].val-q[i].val,i==cnt-1?'\n':' ');        }    }    return 0;}


原创粉丝点击