方法:求出最近公共祖先,使用map给他们计数,注意深度的求法。

  代码如下:

#include<iostream>
#include<cstdio>
#include<map>
#include<cstring>
using namespace std;
#define LL long long
map<LL,LL> sum;
int Get_Deep(LL x)
{
for(int i = ; i < ; i++)
{
if((1LL<<i)<=x && (1LL<<(i+))>x) return i+;
}
return ;
}
void F_LCA(LL x,LL y,LL w)
{
// cout<<"deep "<<x<<" "<<Get_Deep(x)<<endl;
// cout<<"deep "<<y<<" "<<Get_Deep(y)<<endl;
while(Get_Deep(x) > Get_Deep(y))
{
sum[x] += w;
x /= ;
}
while(Get_Deep(y) > Get_Deep(x))
{
sum[y] += w;
y /= ;
}
while(x != y)
{
sum[x] += w;
sum[y] += w;
x /= ;
y /= ;
}
}
LL G_LCA(LL x,LL y)
{
LL ans = ;
while(Get_Deep(x) > Get_Deep(y))
{
ans += sum[x];
x /= ;
}
while(Get_Deep(y) > Get_Deep(x))
{
ans += sum[y];
y /= ;
}
while(x != y)
{
ans += sum[x];
ans += sum[y];
x /= ;
y /= ;
}
return ans;
}
int main()
{
int q,op;
LL u,v,w;
cin>>q;
sum.clear();
while(q--)
{
cin>>op;
if(op == )
{
cin>>u>>v>>w;
F_LCA(u,v,w);
}
else
{
cin>>u>>v;
cout<<G_LCA(u,v)<<endl;
}
}
return ;
}

CodeForces 696A Lorenzo Von Matterhorn (LCA + map)的更多相关文章

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

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

  2. 【STL】【模拟】Codeforces 696A Lorenzo Von Matterhorn

    题目链接: http://codeforces.com/problemset/problem/696/A 题目大意: 一个满二叉树,深度无限,节点顺序编号,k的儿子是k+k和k+k+1,一开始树上的边 ...

  3. codeforces 696A Lorenzo Von Matterhorn 水题

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

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

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

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

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

  6. Codeforces Round #362 (Div. 2) C. Lorenzo Von Matterhorn (类似LCA)

    题目链接:http://codeforces.com/problemset/problem/697/D 给你一个有规则的二叉树,大概有1e18个点. 有两种操作:1操作是将u到v上的路径加上w,2操作 ...

  7. Lorenzo Von Matterhorn(map的用法)

    http://www.cnblogs.com/a2985812043/p/7224574.html 解法:这是网上看到的 因为要计算u->v的权值之和,我们可以把权值放在v中,由于题目中给定的u ...

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

  9. codeforces 696A A. Lorenzo Von Matterhorn(水题)

    题目链接: A. Lorenzo Von Matterhorn time limit per test 1 second memory limit per test 256 megabytes inp ...

随机推荐

  1. java增加时间

    一个简单的东西. 示例如下: /** * 增加时间 * @param oldDate 老时间 * @param addtime 增加的时间 * @return */ public Date addDa ...

  2. 2016NEFU集训第n+5场 A - Chinese Girls' Amusement

    Description       You must have heard that the Chinese culture is quite different from that of Europ ...

  3. MAC VIM 自定义主题

    先从百度云下载 两个 文件 molokai.vim MacVim-snapshot-73-Mavericks.tbz 把macvim 拖到 应用程序里 ,把 mvim 放到 /bin/ 文件夹里 把 ...

  4. tableviewcell 中使用autolayout自适应高度

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { [ce ...

  5. Spring 表单提交,后台自动封装有集合对象的对象

    from表单: <form action="${pageContext.request.contextPath}/vote/save" method="post&q ...

  6. Activity之间的隐士跳转

    /**             * 方法一:在构造函数中指定             */            /*Intent intent=new Intent(this,TwoActivity ...

  7. RPC框架基本原理(一):服务注册

    什么是RPC框架 RPC整个过程涉及四类对象:客户端.客户端代理.服务端和服务端代理.RPC要求客户端和服务端之间约定好调用接口和传输格式(如JSON,Xml等),客户端在调用该接口时,由客户端的代理 ...

  8. ubuntu/linux 下 git 通过代理下载数据 (最简单的方式)

    git国内访问较慢,走代理较快. 方法:git config --global http.proxy http://proxyuser:proxypwd@proxy.server.com:port_n ...

  9. JQuery中常用方法备忘

    本文转载自博客园,原文地址 http://www.cnblogs.com/xzf158/archive/2008/10/14/logan.html 1.Window.onload 的JQuery方法 ...

  10. sql 在将 nvarchar 值 转换成数据类型 int 时失败。

    假设有存储过程:proc_test2 create proc proc_test2 @Id int begin as declare @sql varchar(max) @sql = 'select ...