zoukankan      html  css  js  c++  java
  • CF 696 A Lorenzo Von Matterhorn(二叉树,map)

    原题链接:http://codeforces.com/contest/696/problem/A

    原题描述:

    Lorenzo Von Matterhorn
     

    Barney lives in NYC. NYC has infinite number of intersections numbered with positive integers starting from 1. There exists a bidirectional road between intersections i and 2i and another road between i and 2i + 1 for every positive integer i. You can clearly see that there exists a unique shortest path between any two intersections.

    Initially anyone can pass any road for free. But since SlapsGiving is ahead of us, there will q consecutive events happen soon. There are two types of events:

    1. Government makes a new rule. A rule can be denoted by integers vu and w. As the result of this action, the passing fee of all roads on the shortest path from u to v increases by w dollars.

    2. Barney starts moving from some intersection v and goes to intersection u where there's a girl he wants to cuddle (using his fake name Lorenzo Von Matterhorn). He always uses the shortest path (visiting minimum number of intersections or roads) between two intersections.

    Government needs your calculations. For each time Barney goes to cuddle a girl, you need to tell the government how much money he should pay (sum of passing fee of all roads he passes).

    Input

    The first line of input contains a single integer q (1 ≤ q ≤ 1 000).

    The next q lines contain the information about the events in chronological order. Each event is described in form v u w if it's an event when government makes a new rule about increasing the passing fee of all roads on the shortest path from u to v by w dollars, or in formv u if it's an event when Barnie goes to cuddle from the intersection v to the intersection u.

    1 ≤ v, u ≤ 1018, v ≠ u, 1 ≤ w ≤ 109 states for every description line.

    Output

    For each event of second type print the sum of passing fee of all roads Barney passes in this event, in one line. Print the answers in chronological order of corresponding events.

    Example
    input
    7
    1 3 4 30
    1 4 1 2
    1 3 6 8
    2 4 3
    1 6 1 40
    2 3 7
    2 2 4
    output
    94
    0
    32
    Note

    In the example testcase:

    Here are the intersections used:

    1. Intersections on the path are 3, 1, 2 and 4.
    2. Intersections on the path are 4, 2 and 1.
    3. Intersections on the path are only 3 and 6.
    4. Intersections on the path are 4, 2, 1 and 3. Passing fee of roads on the path are 32, 32 and 30 in order. So answer equals to32 + 32 + 30 = 94.
    5. Intersections on the path are 6, 3 and 1.
    6. Intersections on the path are 3 and 7. Passing fee of the road between them is 0.
    7. Intersections on the path are 2 and 4. Passing fee of the road between them is 32 (increased by 30 in the first event and by 2 in the second).

     题目大意:有个男的想去另一个地点见妹子,每个地点有一个唯一的代号,每一段路程一开始是免费的,然后政府会公布两个地点u与v之间的最短路径中的每一段将增加收费w美元如果输入的是1 v u w则代表公布v地到u地的最短路程的每一段需要收费w美元,如果输入的是2 v u则让你求v地到u地的收费总额是多少。

    解题思路:除了标号为1的点其他每个地点向上都只有一条路,可以用map<LL,LL>M来暴力做。每次向上搜索地点代号的值除2就行了.

    AC代码:

    # include <stdio.h>
    # include <string.h>
    # include <stdlib.h>
    # include <iostream>
    # include <fstream>
    # include <vector>
    # include <queue>
    # include <stack>
    # include <map>
    # include <set>
    # include <math.h>
    # include <algorithm>
    using namespace std;
    # define pi acos(-1.0)
    # define mem(a,b) memset(a,b,sizeof(a))
    # define FOR(i,a,n) for(int i=a; i<=n; ++i)
    # define For(i,n,a) for(int i=n; i>=a; --i)
    # define FO(i,a,n) for(int i=a; i<n; ++i)
    # define Fo(i,n,a) for(int i=n; i>a ;--i)
    typedef long long LL;
    typedef unsigned long long ULL;
    
    map<LL,LL>M;
    
    int main()
    {
        //freopen("in.txt", "r", stdin);
        int q;
        scanf("%d",&q);
        while(q--)
        {
            int s,w;
            LL u,v,ans=0;
            scanf("%d",&s);
            if(s==1)
            {
                map<LL,int>m;
                scanf("%lld%lld%d",&v,&u,&w);
                while(v!=1)
                {
                    M[v]+=w;
                    m[v]=1;
                    v/=2;
                }
                while(u!=1)
                {
                    if(m[u]==1)
                    {
                        while(u!=1)
                        {
                            M[u]-=w;
                            u/=2;
                        }
                        break;
                    }
                    M[u]+=w;
                    u/=2;
                }
            }
            else
            {
                map<LL,int>m;
                scanf("%lld%lld",&v,&u);
                while(v!=1)
                {
                    m[v]=1;
                    ans+=M[v];
                    v/=2;
                }
                while(u!=1)
                {
                    if(m[u]==1)
                    {
                        while(u!=1)
                        {
                            ans-=M[u];
                            u/=2;
                        }
                        break;
                    }
                    ans+=M[u];
                    u/=2;
                }
                cout<<ans<<endl;
            }
        }
        return 0;
    }

      

  • 相关阅读:
    使用 EasyBCD 安装Ubuntu 14.04 Error 15: file not found错误的解决方法
    浅谈程序猿的职业规划,看你如何决定自己的未来吧。
    [转载]DOS循环:bat/批处理for命令详解 (史上虽详尽的总结和说明~~)
    bat 批处理 字符串 截取
    window上使用GIT的个人经验(入门级)
    Android 访问 wcf
    解决 MyEclipse 10 中 JSp页面 “return false” 报错问题
    微信公共平台(码农在努力)
    Spring Cloud 中使用 Zipkin 追踪服务
    Spring Cloud Config 分布式配置中心
  • 原文地址:https://www.cnblogs.com/teble/p/7207823.html
Copyright © 2011-2022 走看看