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. XShell命令行使用

    1.建立连接: 2.查看总体目录: 3.查看对应服务目录: 4.删除对应jar包后,再查看目录下文件: 5.上传对应的jar文件: 6.重启服务 7.查看服务日志: 8.mv old-name new ...

  2. ASP.NET MVC生成安全验证码

    html部分: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  3. vscode - 添加背景图片

    首先,Ctrl+Shift+P安装backround , 而后重启vscode会有默认的背景图片 修改背景图,可自定义三张 具体请看gif图 最开始时,发现png根本不是全透明,用ps处理了一下(下列 ...

  4. Android学习(十) SQLite 基于SQL语句的操作方式

    main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns ...

  5. Python_Select解析

    selcet(等待I/O完成)的介绍: select同时监控多个socket,select()的机制提供了fd_set的数据结构,实际是long类型的数组,优点是跨平台性,select的缺点在于单个进 ...

  6. Nginx:处理HTTP请求

    参考资料<深入理解Nginx>(陶辉) 处理HTTP请求 接着上一次的内容,本次将说明HTTP框架是如何召集负责具体功能的各HTTP模块合作处理请求的. 在http://www.cnblo ...

  7. 压力测试衡量CPU的三个指标:CPU Utilization、Load Average和Context Switch Rate

    分类: 4.软件设计/架构/测试 2010-01-12 19:58 34241人阅读 评论(4) 收藏 举报 测试loadrunnerlinux服务器firebugthread 上篇讲如何用LoadR ...

  8. 关于解决 http 状态码200,php 文件有输出,但是不显示模板文件的问题

    一 问题 给公司搭建一个在线测试站点之后,在浏览器地址栏输入 "http://xxx.xxx.xxx/index.php",页面什么都没显示.调出浏览器的开发者工具查看,http ...

  9. java读取clob字段的几种方法

    http://cache.baiducontent.com/c?m=9f65cb4a8c8507ed4fece7631046893b4c4380143fd3d1027fa3c215cc790f1a06 ...

  10. eclipse / ADT(Android Develop Tool) 一些方便的初始设置

      1.设置编辑窗口的背景色eclipse的主编辑窗口的背景色,默认为白色,个人感觉太亮,推荐保护视力的“墨绿色”,当然也可以根据个人喜好更改,如下图 2.主编辑窗口的字体字号等,也可以根据自己的爱好 ...