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 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 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放每一个节点的对应边的权值,然后进行更新或者查询

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <map>
using namespace std;
#define N 10005
map<long long,long long> mp; void add(long long a,long long w)
{
if(mp.find(a)==mp.end()) mp[a]=;
mp[a]+=w;
} long long val(long long a)
{
if(mp.find(a)==mp.end()) return ;
return mp[a];
} int main()
{
int t;
cin>>t;
while(t--){
int s;
cin>>s;
if(s==){
long long u,v,w;
cin>>u>>v>>w;
while(u!=v){
//对节点进行更新,直到更新到根节点
if(u>v){
add(u,w);
u>>=;
}
else{
add(v,w);
v>>=;
}
}
}
else{
long long u,v,ans=;
cin>>u>>v;
while(v!=u){
//查询
if(u>v){
ans+=val(u);
u>>=;
}
else{
ans+=val(v);
v>>=;
}
}
cout<<ans<<endl;
}
}
return ;
}

CodeForces 696A:Lorenzo Von Matterhorn(map的用法)的更多相关文章

  1. CodeForces 696A Lorenzo Von Matterhorn (LCA + map)

    方法:求出最近公共祖先,使用map给他们计数,注意深度的求法. 代码如下: #include<iostream> #include<cstdio> #include<ma ...

  2. 【STL】【模拟】Codeforces 696A Lorenzo Von Matterhorn

    题目链接: http://codeforces.com/problemset/problem/696/A 题目大意: 一个满二叉树,深度无限,节点顺序编号,k的儿子是k+k和k+k+1,一开始树上的边 ...

  3. codeforces 696A Lorenzo Von Matterhorn 水题

    这题一眼看就是水题,map随便计 然后我之所以发这个题解,是因为我用了log2()这个函数判断在哪一层 我只能说我真是太傻逼了,这个函数以前听人说有精度问题,还慢,为了图快用的,没想到被坑惨了,以后尽 ...

  4. #map+LCA# Codeforces Round #362 (Div. 2)-C. Lorenzo Von Matterhorn

    2018-03-16 http://codeforces.com/problemset/problem/697/C C. Lorenzo Von Matterhorn time limit per t ...

  5. codeforces 696A A. Lorenzo Von Matterhorn(水题)

    题目链接: A. Lorenzo Von Matterhorn time limit per test 1 second memory limit per test 256 megabytes inp ...

  6. CF 696 A Lorenzo Von Matterhorn(二叉树,map)

    原题链接:http://codeforces.com/contest/696/problem/A 原题描述: Lorenzo Von Matterhorn   Barney lives in NYC. ...

  7. Lorenzo Von Matterhorn

    Lorenzo Von Matterhorn Barney lives in NYC. NYC has infinite number of intersections numbered with p ...

  8. Lorenzo Von Matterhorn(STL_map的应用)

    Lorenzo Von Matterhorn time limit per test 1 second memory limit per test 256 megabytes input standa ...

  9. A. Lorenzo Von Matterhorn

    A. Lorenzo Von Matterhorn time limit per test 1 second memory limit per test 256 megabytes input sta ...

随机推荐

  1. 阿里将成为下一个谷歌?是谁Google真正的挑战者

    非常多观点觉得阿里下一步即将成为google的挑战者,但不管从技术储备还是产业布局来看,阿里都难当此任.在产业模式上.电商挑战搜索尚有诸多短板,在解决这些根本问题前,空泛谈论这些是没有意义的. < ...

  2. JAVASCRIPT高程笔记-------JSON与AJAX

    json对象——语法 简单值:与JS相同语法,可以是字符串,数值,布尔值,null:但不支持undefined 对象: 复杂数据类型,表示一组有序的键值对,键值对的值可以是简单数据,也可以是复杂数据 ...

  3. sql 从未连续的Id中选择10~30条数据

    select * from(select *,row_number()over(order by ProductID) as num from Products) as t where t.num&g ...

  4. Java Socket 爬虫

    # 地址 https://github.com/mofadeyunduo/crawler # 前言 1.代码不断优化更新. 2.有建议请留言. # 介绍 1.多线程,基于 ExcutorServcie ...

  5. 【wpf】在win10系统上弹出toast和notification

    原文:[wpf]在win10系统上弹出toast和notification 老规矩,先看效果 右下角的notification: 操作中心的notification: 整体效果: 前提条件 1.需要在 ...

  6. List集合去重方式及效率对比

    List集合相信大家在开发过程中几乎都会用到.有时候难免会遇到集合里的数据是重复的,需要进行去除.然而,去重方式有好几种方式,你用的是哪种方式呢?去重方式效率是否是最高效.最优的呢?今天就给大家讲解一 ...

  7. WebAPI增加Area以支持无限层级同名Controller

    原文:WebAPI增加Area以支持无限层级同名Controller 微软的WebAPI默认实现逻辑 默认实现中不支持同名Controller,否则在访问时会报HttpError,在网上找到了各种路由 ...

  8. Android之Log封装

    blog原文地址:http://yuxingxin.com/2015/10/26/AndroidLog/ Github:https://github.com/fallblank/CodeEssay

  9. Qt实现长文件名(字符串)在QLabel中自适应缩短

    一.应用场景简述 当在有限宽度的QLable中显示很长的文件名/字符串时,超出QLabel宽度部分将不会显示,此时采取缩短文件名策略(也可实现为字符串滚动动画)可以缓解这一问题.在实现这一想法的过程中 ...

  10. QML Settings 小的示例

    QML 中使用 Settings 可以保存一些简单的信息,例如用户名,密码,窗口位置,大小等,没有Sqlite那么麻烦,简单易用哦~~~(环境:Qt5.8  for android ,Windows ...