• Ch’s gift


    Ch’s gift

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

    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
    分析:离线按点权排序,然后树剖;
       或者离散化点权主席树维护有序区间;
    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <bitset>
    #include <map>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <cassert>
    #include <ctime>
    #define rep(i,m,n) for(i=m;i<=(int)n;i++)
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define vi vector<int>
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define ll long long
    #define pi acos(-1.0)
    #define pii pair<int,int>
    #define sys system("pause")
    #define ls rt<<1
    #define rs rt<<1|1
    #define all(x) x.begin(),x.end()
    const int maxn=1e5+10;
    const int N=5e5+10;
    using namespace std;
    ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
    ll qmul(ll p,ll q,ll mo){ll f=0;while(q){if(q&1)f=(f+p)%mo;p=(p+p)%mo;q>>=1;}return f;}
    ll qpow(ll p,ll q,ll mo){ll f=1;while(q){if(q&1)f=f*p%mo;p=p*p%mo;q>>=1;}return f;}
    int n,m,k,t,a[maxn],fa[maxn],dep[maxn],son[maxn],pos[maxn],bl[maxn],id[maxn],dfs_cl,tot,head[maxn],cnt;
    ll ret[maxn],sum[maxn<<2];
    struct node
    {
        int l,r,id,x;
        bool operator<(const node&p)const
        {
            return x<p.x;
        }
    }qu[maxn<<1];
    struct node1
    {
        int to,nxt;
    }e[maxn<<1];
    void add(int x,int y)
    {
        e[cnt].to=y;
        e[cnt].nxt=head[x];
        head[x]=cnt++;
    }
    bool cmp(int x,int y)
    {
        return a[x]<a[y];
    }
    void dfs1(int x,int y)
    {
        dep[x]=dep[y]+1;
        fa[x]=y;
        son[x]=1;
        for(int i=head[x];i!=-1;i=e[i].nxt)
        {
            int z=e[i].to;
            if(z==y)continue;
            dfs1(z,x);
            son[x]+=son[z];
        }
    }
    void dfs2(int x,int y,int ch)
    {
        int ma=0;
        pos[x]=++dfs_cl;
        bl[x]=ch;
        for(int i=head[x];i!=-1;i=e[i].nxt)
        {
            int z=e[i].to;
            if(z==y)continue;
            if(son[z]>son[ma])ma=z;
        }
        if(ma!=0)dfs2(ma,x,ch);
        for(int i=head[x];i!=-1;i=e[i].nxt)
        {
            int z=e[i].to;
            if(z==y||z==ma)continue;
            dfs2(z,x,z);
        }
    }
    void build(int l,int r,int rt)
    {
        sum[rt]=0;
        if(l==r)return;
        int mid=l+r>>1;
        build(l,mid,ls);
        build(mid+1,r,rs);
    }
    void pup(int rt)
    {
        sum[rt]=sum[ls]+sum[rs];
    }
    void upd(int x,int y,int l,int r,int rt)
    {
        if(x==l&&x==r)
        {
            sum[rt]+=y;
            return;
        }
        int mid=l+r>>1;
        if(x<=mid)upd(x,y,l,mid,ls);
        else upd(x,y,mid+1,r,rs);
        pup(rt);
    }
    ll Query(int L,int R,int l,int r,int rt)
    {
        if(L==l&&R==r)return sum[rt];
        int mid=l+r>>1;
        if(R<=mid)return Query(L,R,l,mid,ls);
        else if(L>mid)return Query(L,R,mid+1,r,rs);
        else return Query(L,mid,l,mid,ls)+Query(mid+1,R,mid+1,r,rs);
    }
    ll gao(int x,int y)
    {
        ll ret=0;
        while(bl[x]!=bl[y])
        {
            if(dep[bl[x]]<dep[bl[y]])swap(x,y);
            ret+=Query(pos[bl[x]],pos[x],1,n,1);
            x=fa[bl[x]];
        }
        if(pos[x]>pos[y])swap(x,y);
        ret+=Query(pos[x],pos[y],1,n,1);
        return ret;
    }
    int main()
    {
        int i,j;
        while(~scanf("%d%d",&n,&m))
        {
            rep(i,1,n)scanf("%d",&a[i]),id[i]=i,head[i]=-1;
            cnt=0;
            rep(i,1,n-1)
            {
                int x,y;
                scanf("%d%d",&x,&y);
                add(x,y);add(y,x);
            }
            tot=0;
            rep(i,1,m)
            {
                int x,y,z,w;
                ret[i]=0;
                scanf("%d%d%d%d",&x,&y,&z,&w);
                qu[++tot]=node{x,y,-i,z-1};
                qu[++tot]=node{x,y,i,w};
            }
            dfs_cl=0;
            dfs1(1,0);
            dfs2(1,0,1);
            sort(qu+1,qu+tot+1);
            sort(id+1,id+n+1,cmp);
            build(1,n,1);
            int now=1;
            rep(i,1,tot)
            {
                while(now<=n&&a[id[now]]<=qu[i].x)
                {
                    upd(pos[id[now]],a[id[now]],1,n,1);
                    ++now;
                }
                if(qu[i].id<0)ret[-qu[i].id]-=gao(qu[i].l,qu[i].r);
                else ret[qu[i].id]+=gao(qu[i].l,qu[i].r);
            }
            rep(i,1,m)printf("%lld%c",ret[i],i==m?'
    ':' ');
        }
        return 0;
    }
  • 相关阅读:
    sqlservr 命令行启动
    提高程序性能、何为缓存
    NoSQL和MemeryCache的出现意味着传统数据库使用方式的变革吗?
    jQuery UI Autocomplete是jQuery UI的自动完成组件
    MongoDB
    一步步 jQuery (一)概念,使用,$名称冲突4种解决方法,使用层次及次数问题
    淘宝API开发系列
    MongoDB学习笔记
    WF Workflow 状态机工作流 开发
    MongoDb与MVC3的增删改查采用官方驱动
  • 原文地址:https://www.cnblogs.com/dyzll/p/7413856.html
Copyright © 2020-2023  润新知