• Lorenzo Von Matterhorn(STL_map的应用)


    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 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).

    //给出一个无限节点的树,节点的标号从1到无穷大。它是这样定义的,节点i和节点2*i和节点2*i+1相连。

    每条边都有一个权值,初始时为0.

    给出q个操作。

    1 u v w 代表把u和v之间的边的权值都加上w。

    2 u v 询问u和v之间的边的权值总和。

    //本来是感觉根本不能做的嘛,后来想到 STL 里的 map 可以完美的解决这个问题

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <map>
     4 using namespace std;
     5 
     6 typedef long long LL;
     7 
     8 int main()
     9 {
    10     int q,op;
    11     LL u,v,w;
    12     scanf("%d",&q);
    13     map<LL,LL> M;
    14     while (q--)
    15     {
    16         scanf("%d",&op);
    17         if(op==1)
    18         {
    19             map<LL,int> vis;
    20             scanf("%lld%lld%lld",&u,&v,&w);
    21             while (u!=1)
    22             {
    23                 M[u]+=w;
    24                 vis[u]=1;
    25                 u/=2;
    26             }
    27             while (v!=1)
    28             {
    29                 if (vis[v]==1)
    30                 {
    31                     while (v!=1)
    32                     {
    33                         M[v]-=w;
    34                         v/=2;
    35                     }
    36                     break;
    37                 }
    38                 M[v]+=w;
    39                 v/=2;
    40             }
    41         }
    42         else if (op==2)
    43         {
    44             scanf("%lld%lld",&u,&v);
    45             LL ans=0;
    46 
    47             map<LL,int> vis;
    48             while (u!=1)
    49             {
    50                 ans+=M[u];
    51                 //printf("%lld
    ",ans);
    52                 vis[u]=1;
    53                 u/=2;
    54             }
    55             //printf("!!!
    ");
    56             while (v!=1)
    57             {
    58                 if (vis[v]==1)
    59                 {
    60                     while(v!=1)
    61                     {
    62                         ans-=M[v];
    63                         //printf("%lld
    ",ans);
    64                         v/=2;
    65                     }
    66                     break;
    67                 }
    68                 ans+=M[v];
    69                 v/=2;
    70             }
    71             printf("%lld
    ",ans);
    72         }
    73     }
    74     return 0;
    75 }
    View Code
  • 相关阅读:
    【linux命令 】chmod设置权限 + chown设置属主和属组 + 文件特殊权限(SUID、SGID、SBIT)
    【linux命令】chgrp改变文件或目录的属组
    【Linux命令】setfacl、getfacl命令基本用法(文件权限全文控制列表acl)
    【linux命令】权限管理命令(chattr、lsattr、sudo)
    【linux运维】linux系统上忘记密码如何操作
    【zibbix自定义监控】zabbix服务自定义监控mysql的状态信息
    【zabbix告警配置】zabbix服务配置邮件告警
    【mail邮件系统】linux上安装部署sendmail邮件系统
    【nginx+keepalived】nginx+keepalived搭建高可用
    【Linux命令】centos防火墙使用和配置
  • 原文地址:https://www.cnblogs.com/haoabcd2010/p/6087146.html
Copyright © 2020-2023  润新知