方法:求出最近公共祖先,使用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. Swift-HELP

    //获取网页地址对应的字符串 var urlString = url.absoluteURL.absoluteString

  2. kvstore之memcached为存储介质

    ecstore中kvstore选用memcached作为存储介质 kvstore存储类选用base_kvstore_memcached(app/base/lib/kvstore/memcached.p ...

  3. ( ̄▽ ̄") 没钱了

    ( ̄▽ ̄") 没钱了 TimeLimit: 1000ms  MenoryLimit:65536KB 64-bit integer IO format:%lld Problem Descrip ...

  4. sublime 配置 python IDE

    安装sublime下载地址:http://www.sublimetext.com/,请自行根据系统版本进行下载.下载好之后直接安装即可. 汉化包 汉化包下载: 汉化方法: 1.运行sublime te ...

  5. Python in minute

    Python 性能优化相关专题:    https://www.ibm.com/developerworks/cn/linux/l-cn-python-optim/   Python wikipedi ...

  6. python基础(三)--列表、元组、字典

    一.列表: 有序序列,支持索引.切片.循环(for,while) 元素可以被修改: 元素可以是任何数据类型(数字,字符串,列表,布尔值...),可以嵌套: ##增 1.append(object)   ...

  7. Crowd安装和破解

    参考资料来自:http://www.it165.net/os/html/201311/6653.html 这个破解比较简单

  8. oracle行转列函数

  9. 部署php项目

    1.不写端口号,即127.0.0.1,其隐含一个默认端口号80 2.查看当前占用的端口号 3.nginx修改工作路径,nginx修改端口号 4.查看端口号是否可访问telnet命令 在dos界面输入命 ...

  10. sqlDeveloper连接oracle

    1.解决oracle11g的ORA-12505问题 启动oraclehome92TNSlistener服务,启动oracleserviceXXXX,XXXX就是你的database SID. < ...