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

题目大意:给出一个二叉树,有两种操作,第一种:把两个点u,v之间所有的边都 +w ; 第二种求出u,v两点之间的距离。

解题思路:因为是二叉树所以在这道题中任意两个节点之间的最多有128条边,所以可以采用比较暴力的做法来增加或计算两点之间的距离,方法是这样的,每次从两点中选取较大的那个点,将他除以二,也就是找到他的祖先,这样若干次操作一定能找到他们两个的最近公共祖先。

AC代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include<set>
#include<map>
#include<queue>#define maxn 1005
#define inf 0x3f3f3f3f
#define ONES(x) __builtin_popcount(x)
using namespace std;typedef long long ll ;
const double eps =1e-8;
const int mod = 1000000007;
map<ll,ll>road;
int main()
{//freopen("test.txt","r",stdin);ll q,rule,u,v,w,ans;cin>>q;while(q--){cin>>rule;if(rule==1){cin>>u>>v>>w;while(u!=v){if(u<v)swap(u,v);//make sure that u > vroad[u] += w;//cout << u << ' ' << road[u] << endl;//road[v] += w;u /= 2;}}else{cin>>u>>v;ans = 0;while(u!=v){if(u<v)swap(u,v);//make sure that u > vans += road[u];//ans += road[v];u /= 2;}cout << ans << endl;}}//cout << "Hello world!" << endl;return 0;
}

codeforces 697C Lorenzo Von Matterhorn(二叉树LCA)相关推荐

  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 - 1328E Tree Queries(dfs序/LCA)

    题目链接:点击查看 题目大意:给出一棵以点 1 为根节点的树,接下来有 m 次询问,每次询问给出 k 个点,题目问我们能否找到一个点 u ,使得从根节点到点 u 的简单路径,到 k 个点的每个点的距离 ...

  5. [bzoj3625][Codeforces Round #250]小朋友和二叉树 (生成函数)

    description 我们的小朋友很喜欢计算机科学,而且尤其喜欢二叉树. 考虑一个含有n个互异正整数的序列c[1],c[2],-,c[n].如果一棵带点权的有根二叉树满足其所有顶点的权值都在集合{c ...

  6. Mobile Phone Network CodeForces - 1023F(并查集lca+修改环)

    题意: 就是有几个点,你掌控了几条路,你的商业对手也掌控了几条路,然后你想让游客都把你的所有路都走完,那么你就有钱了,但你又想挣的钱最多,真是的过分..哈哈 游客肯定要对比一下你的对手的路 看看那个便 ...

  7. codeforces泛做

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

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

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

  9. [bzoj3625][Codeforces 250 E]The Child and Binary Tree(生成函数+多项式运算+FFT)

    3625: [Codeforces Round #250]小朋友和二叉树 Time Limit: 40 Sec  Memory Limit: 256 MB Submit: 650  Solved: 2 ...

  10. Codeforces Round #620 (Div. 2) E. 1-Trees and Queries 思维 + LCA

    传送门 文章目录 题意 思路: 题意 思路: 照例,先考虑不加边怎么做.由于可以经过重复的边或点,设aaa与bbb之间长度为lenlenlen,那么需要len<=klen<=klen< ...

最新文章

  1. react中使用scss_我如何将CSS模块和SCSS集成到我的React应用程序中
  2. iframe父子页面交互
  3. go/node/python 多进程与多核cpu
  4. 我的第一个Java程序 Hello World!
  5. Stanford CS107 Programming Paradigms 编程范式
  6. hex editor怎么搜索代码_代码审计从入门到放弃(三) phplimit
  7. 爬虫总结(四)-- 分布式爬虫
  8. 【问链财经-区块链基础知识系列】 第四十七课 蚂蚁区块链的布局和打法
  9. 答读者问:学历不高,要如何破局?
  10. C++之预处理命令以及宏定义
  11. 华为音量键只能调通话_手机音量键还在以为只能调音量?别傻了,赶紧看看下文绝对涨知识!...
  12. oracle服务没有了 原因,一例oracle服务无法启动的原因及解决方法
  13. 矢量数据压缩:道格拉斯普克算
  14. GBase 8c V3.0.0数据类型——备份控制函数
  15. PlantUML(程序员绘制流程图专用工具)
  16. 欧拉定理学习20161004
  17. 它来了!Flutter3.0新特性全接触
  18. 空气压缩机自动控制Multisim仿真
  19. 三星Galaxy s4(i9505)完美获取root权限教程
  20. 网站用户体验研究:新窗口打开链接还是当前窗口打

热门文章

  1. 年味变淡是从我们变得随便开始的
  2. alert#40;1#41; to xss.haozi.me with #0x02
  3. 在Vue中如何缓存页面
  4. 修改数据库安装的服务器 系统时间,修改数据库服务器的操作系统时间
  5. 企业app开发要多少钱[APP定制]
  6. dh参数逆运动学_UR机械臂运动学正逆解方法
  7. 树莓派(Raspberry Pi 4 Model B)编译64位内核Kernel
  8. Debian7 更换源
  9. 2021iOS最新面试总结
  10. 编程制作动态壁纸的思路_Android应用源码动态壁纸开发必看例子源码