zoukankan      html  css  js  c++  java
  • CodeForces 696A:Lorenzo Von Matterhorn(map的用法)

    C. Lorenzo Von Matterhorn
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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 v, u 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 1 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 form 2 v 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).

    题意:在一个类似于树的图中,求从一个节点到另一个节点需要走过的距离,初始为0,输入为1时对u到v进行更新,输入为2时求u到v的距离。

    思路:用map放每一个节点的对应边的权值,然后进行更新或者查询

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 #include <iostream>
     5 #include <map>
     6 using namespace std;
     7 #define N 10005
     8 map<long long,long long> mp;
     9 
    10 void add(long long a,long long w)
    11 {
    12     if(mp.find(a)==mp.end()) mp[a]=0;
    13     mp[a]+=w;
    14 }
    15 
    16 long long val(long long a)
    17 {
    18     if(mp.find(a)==mp.end()) return 0;
    19     return mp[a];
    20 }
    21 
    22 int main()
    23 {
    24     int t;
    25     cin>>t;
    26     while(t--){
    27         int s;
    28         cin>>s;
    29         if(s==1){
    30             long long u,v,w;
    31             cin>>u>>v>>w;
    32             while(u!=v){
    33                 //对节点进行更新,直到更新到根节点
    34                 if(u>v){
    35                     add(u,w);
    36                     u>>=1;
    37                 }
    38                 else{
    39                     add(v,w);
    40                     v>>=1;
    41                 }
    42             }
    43         }
    44         else{
    45             long long u,v,ans=0;
    46             cin>>u>>v;
    47             while(v!=u){
    48                 //查询
    49                 if(u>v){
    50                     ans+=val(u);
    51                     u>>=1;
    52                 }
    53                 else{
    54                     ans+=val(v);
    55                     v>>=1;
    56                 }
    57             }
    58             cout<<ans<<endl;
    59         }
    60     }
    61     return 0;
    62 }
  • 相关阅读:
    mysql注入小测试
    让函数返回指定值实用写法
    源码下载网址
    带宽
    九度oj 题目1080:进制转换
    九度oj 题目1079:手机键盘
    poj 3046 Ant Counting
    整数拆分问题
    poj 2229 Sumsets
    九度oj 题目1411:转圈
  • 原文地址:https://www.cnblogs.com/fightfordream/p/5672915.html
Copyright © 2011-2022 走看看