Lorenzo Von Matterhorn

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
71 3 4 301 4 1 21 3 6 82 4 31 6 1 402 3 72 2 4

Output
94032

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 to 32 + 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).

分析:这种二叉树两节点的最短路径是先找到最近公共祖先(LCA),然后就是分别从当前节点到祖先的路径;

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include <ext/rope>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define vi vector<int>
#define pii pair<int,int>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
const int maxn=1e5+10;
const int dis[4][2]={{0,1},{-1,0},{0,-1},{1,0}};
using namespace std;
using namespace __gnu_cxx;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
int n,m;
map<ll,ll>p;
ll a[4],fa,fb,ans;
int main()
{int i,j,k,t;scanf("%d",&t);while(t--){scanf("%lld",&a[0]);if(a[0]==1){rep(i,1,3)scanf("%lld",&a[i]);fa=a[1],fb=a[2];while(fa!=fb){if(fa<fb)p[fb]+=a[3],fb>>=1;else p[fa]+=a[3],fa>>=1;}}else{rep(i,1,2)scanf("%lld",&a[i]);ans=0LL;fa=a[1],fb=a[2];while(fa!=fb){if(fa<fb)ans+=p[fb],fb>>=1;else ans+=p[fa],fa>>=1;}printf("%lld\n",ans);}}//system ("pause");return 0;
}

转载于:https://www.cnblogs.com/dyzll/p/5672555.html

Lorenzo Von Matterhorn相关推荐

  1. Codeforces 697C Lorenzo Von Matterhorn(严格二叉树的LCA) - xgtao -

    Lorenzo Von Matterhorn 给出一棵二叉树,节点i与2*i和2*i+1相连,i的范围是1~10^18,每一条边有一个权值w(0~10^9),给出q(1~1000)个操作,操作有两种, ...

  2. 【CodeForces - 697C】Lorenzo Von Matterhorn(二叉树,思维)

    题干: 巴尼住在NYC.NYC具有从1开始的正整数编号的无数个交叉点.在交叉点 i 和 2i 之间以及 i和 2i + 1 之间存在双向道路,对任意整数 i 都满足.在任意两点之间都有且只有一条最短路 ...

  3. cf#362-C. Lorenzo Von Matterhorn

    http://codeforces.com/contest/697/problem/C 给你一个完全二叉树 两个操作 1: u,v,w 把u到v上的路都加w权值 2:u,v  查询u到v的权值和 uv ...

  4. codeforces泛做

    codeforces div1的题目,一般是做B.C.D 有的A题觉得有价值也会做,死活不会的就跳了-- 364 B: Connecting Universities Problem: 一颗无根树上给 ...

  5. Cf 362div2 C [map暴力,思维能力]

    题目连接 http://codeforces.com/contest/697/problem/C Description Barney lives in NYC. NYC has infinite n ...

  6. 本地应用 v-on补充

    ◆事件绑定的方法写成函数调用的形式,可以传入自定义参数 ◆定义方法时需要定义形参来接收传入的实参 ◆事件的后面跟上.修饰符可以对事件进行限制 ◆.enter可以限制触发的按键为回车 ◆事件修饰符有多种 ...

  7. VUE的本地应用-V- on

    v-On: ◆v-on指令的作用是:为元素绑定事件 ◆事件名不需要写on ◆指令可以简写为@ ◆绑定的方法定 义在methods属性中 ◆方法内部通过this关键字可以访问定义在data中数据 结果: ...

  8. vue中的v-on事件监听机制

    监听dom事件使用v-on指令: v-on:事件类型="一个函数" 这个事件类型可以自定义 简写: @事件类型="一个函数"⭐ 1.作用:绑定事件监听,表达式可 ...

  9. Betriebssystem I 操作系统课件 01. Evolution von Rechnersystemen 计算机操作系统的演变

    /**  *  * Betriebssystem I  *  * Vorlesungsfolien: Prof. Noelte, BTU Cottbus  *  * Uebersetzer: Li B ...

  10. 黑马vue---8-10、v-cloak、v-text、v-html、v-bind、v-on的基本使用

    黑马vue---8-10.v-cloak.v-text.v-html.v-bind.v-on的基本使用 一.总结 一句话总结: v-bind等这些东西都是用的vue.data里面的变量 1.使用 v- ...

最新文章

  1. ADO.Net五个对象
  2. 听说这是互联网公司招工时忽悠的黑话? | 每日趣闻
  3. 621. Task Scheduler 任务调度器
  4. anaconda3安装_Anaconda3软件安装教程
  5. Origin 移动图像
  6. android中播放gif动画之三
  7. 第一次使用博客,略激动。
  8. 数据--第33课 - 树课后练习
  9. linux 运维 经常逛的几个官网文档
  10. 多元统计分析matlab,matlab与应用多元统计分析..doc
  11. 学术论文的word排版
  12. c语言布丰投针源码,布丰投针数学分析及实验设计(原创).pdf
  13. 函数 over() 的含义
  14. 工业相机镜头的参数与选型
  15. Linux设备模型分析之device_driver(基于3.10.1内核)
  16. 【课后习题】高等数学第七版下第十二章 无穷级数 第二节 常数项级数的审敛法
  17. JS将任意格式的时间转为Date对象
  18. Python爬虫入门教程导航帖
  19. EasyNVR流媒体直播之:零基础实现摄像头的全平台直播 (一)内网直播的实现...
  20. Android ExpandableListView 展开列表控件(手机QQ好友列表)

热门文章

  1. 我们努力的目标,就是不要被看见
  2. 中国少数民族语言和文字
  3. 计算机中的八卦知识,原来计算机的核心技术来自周易八卦
  4. error occurred during initialization of VM报错解决
  5. 苹果手机linux系统版本号,Linux下查看系统版本号信息的方法(转)
  6. 基于复化辛卜生求积公式的变步长求积算法
  7. CTF supersqli
  8. TSL2591STM32固件库开发
  9. mplayer播放器管道重构版
  10. “我去图书馆”公众号代码抢座的实现