zoukankan      html  css  js  c++  java
  • (全国多校重现赛一)B-Ch's gifts

    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

    题解:LCA求最近公共祖先,里面加一个判断节点的权值是否满足在L与R之间,如满足求和;(树链剖分&线段树也可)

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=1e5+10;
    typedef long long LL;
    int n,m,u,v,tot,s,t,up,down,sum;
    int first[maxn],dep[maxn],fa[maxn];
    LL flag[maxn],val[maxn];
    struct Node{
    	int to,net;
    } edge[maxn<<1];
    
    void addedge(int u,int v)
    {
    	edge[tot].to=v;
    	edge[tot].net=first[u];
    	first[u]=tot++;
    }
    
    void dfs(int u,int f)
    {
    	for(int e=first[u];e!=-1;e=edge[e].net)
    	{
    		int v=edge[e].to;
    		if(v==f) continue;
    		fa[v]=u; dep[v]=dep[u]+1;
    		dfs(v,u);
    	}
    }
    
    LL LCA(int a,int b)
    {
    	LL ans=0;
    	if(dep[a]<dep[b]) swap(a,b);
    	while(dep[a]!=dep[b])
    	{
    		if(val[a]>=down&&val[a]<=up) ans+=val[a]; 
    		a=fa[a];
    	}
    	while(a!=b)
    	{
    		if(val[a]>=down&&val[a]<=up) ans+=val[a];
    		if(val[b]>=down&&val[b]<=up) ans+=val[b];
    		a=fa[a],b=fa[b];
    	}
    	if(val[a]>=down&&val[a]<=up) ans+=val[a];
    	return ans;
    }
    
    void Init()
    {
    	tot=0; sum=0;
    	memset(first,-1,sizeof first);
    	memset(fa,0,sizeof fa);
    	memset(dep,0,sizeof dep);
    }
    
    int main()
    {
    	while(~scanf("%d%d",&n,&m))
    	{
    		Init();
    		for(int i=1;i<=n;i++) scanf("%d",val+i);
    		for(int i=1;i<=n-1;i++)
    		{
    			scanf("%d%d",&u,&v);
    			addedge(u,v);
    			addedge(v,u);
    		}
    		dfs(1,0);
    		
    		for(int i=0;i<m;i++)
    		{
    			scanf("%d%d%d%d",&s,&t,&down,&up);
    			flag[sum++]=LCA(s,t);
    		}
    		for(int i=0;i<sum;i++) i==sum-1 ? printf("%lld
    ",flag[i]) : printf("%lld ",flag[i]); 
    	}
    	
    	
    	return 0;
    }
    
  • 相关阅读:
    Send EMail from your .NET Application using your GMail Account
    ObjectContext 是开发人员在查询、添加和删除其实体实例以及将新状态保存回数据库时用到的主要构造
    AjaxPro使用说明
    .NET Framework 类库
    sysobjects syscolumns和SysTypes笔记
    javascript 懒加载技术(lazyload)简单实现
    WEB前端开发笔试题2
    css方框模型(盒模型Box Model)
    让IE支持CSS3选择器的方法(利用JS)
    让IE6/IE7/IE8浏览器支持CSS3属性
  • 原文地址:https://www.cnblogs.com/csushl/p/9386500.html
Copyright © 2011-2022 走看看