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

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

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

给出q个操作。

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

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

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

 #include <iostream>
#include <cstdio>
#include <map>
using namespace std; typedef long long LL; int main()
{
int q,op;
LL u,v,w;
scanf("%d",&q);
map<LL,LL> M;
while (q--)
{
scanf("%d",&op);
if(op==)
{
map<LL,int> vis;
scanf("%lld%lld%lld",&u,&v,&w);
while (u!=)
{
M[u]+=w;
vis[u]=;
u/=;
}
while (v!=)
{
if (vis[v]==)
{
while (v!=)
{
M[v]-=w;
v/=;
}
break;
}
M[v]+=w;
v/=;
}
}
else if (op==)
{
scanf("%lld%lld",&u,&v);
LL ans=; map<LL,int> vis;
while (u!=)
{
ans+=M[u];
//printf("%lld\n",ans);
vis[u]=;
u/=;
}
//printf("!!!\n");
while (v!=)
{
if (vis[v]==)
{
while(v!=)
{
ans-=M[v];
//printf("%lld\n",ans);
v/=;
}
break;
}
ans+=M[v];
v/=;
}
printf("%lld\n",ans);
}
}
return ;
}

Lorenzo Von Matterhorn(STL_map的应用)的更多相关文章

  1. Lorenzo Von Matterhorn

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

  2. C. Lorenzo Von Matterhorn LCA

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

  3. #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 ...

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

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

  5. CodeForces 696A:Lorenzo Von Matterhorn(map的用法)

    http://codeforces.com/contest/697/problem/C C. Lorenzo Von Matterhorn time limit per test 1 second m ...

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

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

  7. A. Lorenzo Von Matterhorn

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

  8. 【CodeForces 697C】Lorenzo Von Matterhorn(LCA)

    Least Common Ancestors 节点范围是1~1e18,至多1000次询问. 只要不断让深的节点退一层(>>1)就能到达LCA. 用点来存边权,用map储存节点和父亲连边的权 ...

  9. codeforces 696A Lorenzo Von Matterhorn 水题

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

随机推荐

  1. HTML5的测试总结

    HTML5其实也是web的一种,所以基本的web测试的一些重点,HTML5上都要过一遍,不过也有其特殊之处. [需求设计测试] 需求是否合理.是否有更好的实现方法或者功能的遗漏,以及原型图测试,包括用 ...

  2. Solr6.6.0 用 SimplePostTool索引文件

    一.背景介绍 Solr启动并运行之后,并不包含任何数据,在solr的安装目录下的bin目录中,有一个post工具,我们可以使用这个工具往solr上传数据,这个工具必须在命令行中执行,post工具是一个 ...

  3. Hadoop 变更磁盘的方法总结

    背景说明HDFS文件系统使用一段时间后,可能会出现磁盘空间不足或是磁盘损坏的现象,此时需要对DataNode节点的磁盘进行扩充或是更换,本文对操作流程做一个简单的总结 操作步骤 挂载硬盘 添加硬盘的操 ...

  4. JStorm文档

    Jstorm的性能测试 JStorm 大概是Apache Storm 4倍, Apache Flink 1.5 倍, Twitter Heron 2 ~ 10 倍 Jstorm是一个分布式实时计算引擎 ...

  5. 13 Basic Cat Command Examples in Linux

    FROM: http://www.tecmint.com/13-basic-cat-command-examples-in-linux/ The cat (short for “concatenate ...

  6. Keepalived高可用集群应用

    Keepalived高可用集群应用 1.keepalived服务说明 1.1.keepalived介绍 Keepalived是一个用C语言编写的路由软件.该项目的主要目标是为Linux系统和基于Lin ...

  7. javascript - 封装jsonp

    jsonp牵扯到同源策略.跨域等问题,这里不细说了. 实现就是创建动态的script标签来请求后台地址: 示例: jsonp('xxx.php', { uid: 1 }, function (res) ...

  8. Java种八种常用排序算法

    1 直接插入排序 经常碰到这样一类排序问题:把新的数据插入到已经排好的数据列中. 将第一个数和第二个数排序,然后构成一个有序序列 将第三个数插入进去,构成一个新的有序序列. 对第四个数.第五个数……直 ...

  9. smali文件内容具体介绍

    大家都应该知道APK文件其实就是一个MIME为ZIP的压缩包,我们修改ZIP后缀名方式可以看到内部的文件结构,例如修改后缀后用RAR打开鳄鱼小顽皮APK能看到的是(Google Play下载的完整版版 ...

  10. Intent获取Activity返回值

    /* Intent获取Activity返回值* 三步:* 子Activity关闭后的返回值处理函数,requestCode是子Activity返回的请求码,与页面顶端的两个请求码相匹配,resultC ...