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. python 注释xml的元素

    use xml.dom.minidom 注释xml元素和去除xml注释. code is: #!/usr/bin/env python from xml.dom import minidom impo ...

  2. 2017.6.27 跟开涛学spring3--spring概述

    参考来自:http://www.importnew.com/17474.html 注意,项目中使用的是spring4,这里学习的是spring3.关于spring4的变化:http://ningand ...

  3. Elasticsearch教程(八) elasticsearch delete 删除数据(Java)

    Elasticsearch的删除也是很灵活的,下次我再介绍,DeleteByQuery的方式.今天就先介绍一个根据ID删除.上代码. package com.sojson.core.elasticse ...

  4. [Tools] Deploy a Monorepo to Now V2

    Now by Zeit has recently been updated and now supports multi-language monorepos. In this lesson we'l ...

  5. TestNG+ReportNG+IDEA+Git+Jenkins+surefire持续集成数据驱动dubbo接口测试

    一.pom.xml增加testng相关配置 <!--添加插件 关联testNg.xml--><plugin> <groupId>org.apache.maven.p ...

  6. python可hash 不可hash类型

    不可变类型是可hash #tuple str freezeset 可变类型是不可hash ##list set

  7. 使用RTTI为继承体系编写”==”运算符

    转载请注明出处:http://www.cnblogs.com/inevermore/p/4012079.html   RTTI,指的是运行时类型识别技术.   先看一个貌似无关的问题:   为继承体系 ...

  8. mkdir的参数-p的作用

    mkdir -p /nfs 也就是加上-p参数,之前只知道是递归创建目录,于是就发问了,得到的答案是: -p, --parents              no error if existing, ...

  9. 2015级C++第7周项目 友元、共享数据保护、多文件结构

    [项目1-成员函数.友元函数和一般函数有差别]參考解答 (1)阅读以下的程序,体会凝视中的说明(要执行程序,请找到课程主页并复制代码) //例:使用成员函数.友元函数和一般函数的差别 #include ...

  10. js获取时间查并实现倒计时读条

    <script type="text/javascript"> $().ready(function () {// 每增加一个切换,就要增加一行,tab1不变,其他的都 ...