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. lamp+nginx代理+discuz+wordpress+phpmyadmin

    实验课题:搭建LAMP,安装Nginx,作为代理,将MySQL安装在单独的机器,apache负责动态,nginx负责静态 实验环境: 1.VMware Workstation 11 2.设备A:MyS ...

  2. python 小技巧(glob,guid,序列化,压缩字符,有序字典,sorted函数,分片)

    1.glob模块 glob模块是最简单的模块之一,内容非常少.用它可以查找符合特定规则的文件路径名.跟使用windows下的文件搜索差不多.查找文件只用到三个匹配符:”*”, “?”, “[]”.”* ...

  3. MySQL增加访问ip

    GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'IDENTIFIED BY '123456' WITH GRANT OPTION; flush privileges;

  4. CMAKE 编译报错

    报错如下: CMake Error: your C compiler: "CMAKE_C_COMPILER-NOTFOUND" was not found. 没有安装 gcc 和 ...

  5. zabbix agent被动模式配置

    zabbix agent检测分为主动(agent active)和被动(agent)两种形式,主动与被动的说法均是相对于agent来讨论的.简单说明一下主动与被动的区别如下: 主动:agent请求se ...

  6. tomcat修改默认访问首页

    找到conf下server.xml文件修改如下位置内容 <Host name="localhost" appBase="webapps" unpackWA ...

  7. 开源jabber(XMPP)架设内部即时通讯服务的解决方案

    Jabber 是著名的即时通讯服务服务器,它是一个自由开源软件,能让用户自己架即时通讯服务器,可以在Internet上应用,也可以在局域网中应用.    XMPP(可扩展消息处理现场协议)是基于可扩展 ...

  8. Oracle学习——扫盲篇

    前言 近期这几天一直在与Oracle数据库打交道.因为之前对Oracle的学习并不深入,仅仅是把Oracle当成一个数据源去使用.非常多东西了解的不是非常深,比方.数据库.数据库实例.表空间.用户.表 ...

  9. charles抓https包

    Charles是一款非常好用的抓包工具,有Window,Linux,Mac OS三个版本,个人觉得比shark和TCPDump好用多了. 闲话不多说,公司将服务从http转到https,导致今天因为抓 ...

  10. Angular 学习笔记——run

    <!DOCTYPE html> <html lang="en" ng-app="myApp"> <head> <met ...