题目链接:

A. 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 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 ≤ 10^18, v ≠ u, 1 ≤ w ≤ 10^9 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
题意:
 
i与2*i和2*i+1相连的一个图,两种操作,一种是在u,v的道路上的每一条边权值加w,一种是询问u,v之间道路上的边权值和;
 
思路:
 
可以直接暴力,可以找出最近功公共祖先,然后找这两个点与公共祖先之间把边的权值加上;
 
AC代码:
#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<''||CH>'';F= CH=='-',CH=getchar());
for(num=;CH>=''&&CH<='';num=num*+CH-'',CH=getchar());
F && (num=-num);
}
int stk[], tp;
template<class T> inline void print(T p) {
if(!p) { puts(""); return; }
while(p) stk[++ tp] = p%, p/=;
while(tp) putchar(stk[tp--] + '');
putchar('\n');
} const LL mod=1e9+;
const double PI=acos(-1.0);
const LL inf=1e18;
const int N=3e6+;
const int maxn=3e6;
const double eps=1e-; int q;
map<LL,LL>mp,vis;
LL getfa(LL x,LL y)
{
vis.clear();
while(x)vis[x]=,x>>=;
while()
{
if(vis[y])return y;
y>>=;
}
}
int main()
{
read(q);
while (q--)
{
int f;
LL u,v,w;
read(f);
if(f==)
{
read(u);read(v);read(w);
LL fa=getfa(u,v);
while(u!=fa)
{
mp[u]=mp[u]+w;
u>>=;
}
while(v!=fa)
{
mp[v]=mp[v]+w;
v>>=;
}
}
else
{
read(u);read(v);
LL fa=getfa(u,v),ans=;
while(u!=fa)
{
ans+=mp[u];
u>>=;
}
while(v!=fa)
{
ans+=mp[v];
v>>=;
}
print(ans);
}
}
return ;
}

codeforces 696A A. Lorenzo Von Matterhorn(水题)的更多相关文章

  1. CodeForces 696A:Lorenzo Von Matterhorn(map的用法)

    http://codeforces.com/contest/697/problem/C C. Lorenzo Von Matterhorn time limit per test 1 second m ...

  2. codeforces 696A Lorenzo Von Matterhorn 水题

    这题一眼看就是水题,map随便计 然后我之所以发这个题解,是因为我用了log2()这个函数判断在哪一层 我只能说我真是太傻逼了,这个函数以前听人说有精度问题,还慢,为了图快用的,没想到被坑惨了,以后尽 ...

  3. CodeForces 696A(Lorenzo Von Matterhorn ) & CodeForces 696B(Puzzles )

    A,给一棵完全二叉树,第一个操作,给两个点,两点路径上的所有边权值都增加w,第二个操作,给两个点,求两点路径上的所有边权值和. 我看一眼题就觉得是树链剖分,而我又不会树链剖分,扔掉. 后来查了题解,首 ...

  4. 【CodeForces 697C】Lorenzo Von Matterhorn(LCA)

    Least Common Ancestors 节点范围是1~1e18,至多1000次询问. 只要不断让深的节点退一层(>>1)就能到达LCA. 用点来存边权,用map储存节点和父亲连边的权 ...

  5. #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 ...

  6. CF 696 A Lorenzo Von Matterhorn(二叉树,map)

    原题链接:http://codeforces.com/contest/696/problem/A 原题描述: Lorenzo Von Matterhorn   Barney lives in NYC. ...

  7. Lorenzo Von Matterhorn

    Lorenzo Von Matterhorn Barney lives in NYC. NYC has infinite number of intersections numbered with p ...

  8. C. Lorenzo Von Matterhorn LCA

    C. Lorenzo Von Matterhorn time limit per test 1 second memory limit per test 256 megabytes input sta ...

  9. Lorenzo Von Matterhorn(STL_map的应用)

    Lorenzo Von Matterhorn time limit per test 1 second memory limit per test 256 megabytes input standa ...

随机推荐

  1. GridView动态添加View

    activity_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout ...

  2. 使用EventNext实现基于事件驱动的业务处理

    事件驱动模型相信对大家来说并不陌生,因为这是一套非常高效的逻辑处理模型,通过事件来驱动接下来需要完成的工作,而不像传统同步模型等待任务完成后再继续!虽然事件驱动有着这样的好处,但在传统设计上基于消息回 ...

  3. Hbase优化总结

    1.JVM参数优化: –Xmn=12G –Xms=24G  -Xmx=24G  根据实际机器情况调整,一般为整个机器内存的一半,同时建议regionServer的堆内存建议不要超过32G ; -XX: ...

  4. Base64的空格 + 问题...

    BASE64  通过url传递到后台 加号变空格的处理方法 解决方法: 前台处理:str.replace("+", "%2B"); (错误) <scrip ...

  5. Linux常用C函数-接口处理篇(网络通信函数)

    接口处理篇accept,bind,connect,endprotoent,endservent,getsockopt,htonl,htons,inet_addr,inet_aton,inet_ntoa ...

  6. GoogLeNet系列解读

    GoogLeNet Incepetion V1 这是GoogLeNet的最早版本,出现在2014年的<Going deeper with convolutions>.之所以名为“GoogL ...

  7. CodeForces 321A Ciel and Robot(数学模拟)

    题目链接:http://codeforces.com/problemset/problem/321/A 题意:在一个二维平面中,開始时在(0,0)点,目标点是(a.b),问能不能通过反复操作题目中的指 ...

  8. TCP 的那些事儿(下)(转)

    TCP的RTT算法 从前面的TCP的重传机制我们知道Timeout的设置对于重传非常重要, 设长了,重发就慢,没有效率,性能差: 设短了,重发的就快,会增加网络拥塞,导致更多的超时,更多的超时导致更多 ...

  9. C#总结复习5(需要进一步复习)

    第十五章 接口 1.接口: C++中允许多继承没有接口的概念.而java与C#中有,因为C#中 是单继承多接口. 所谓的接口,其实和抽象类.方法相似.都只有一个空方法.其本身不可以为基类,但是允许被其 ...

  10. leetcode第一刷_Best Time to Buy and Sell Stock III

    这道题还是挺难的,属于我前面提到的,给个数组,线性时间找出个什么东西,尽管上面的两个买卖股票也是这类.只是相比之下稚嫩多了.有关至少至多的问题比較烦人,不好想,等再做一些题,可能会发现什么规律.这道题 ...