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

最新文章

  1. ArrowLayer : A coustom layer animation
  2. 医院管理者必须知道的医院客户关系管理(CRM)
  3. MySQL查询缓存
  4. ST05 跟踪SQL
  5. BCP及自增标识列
  6. 实现OAUTH协议 实现 QQ 第三方登录效果
  7. Qt Creator调试
  8. 【ios开发】Block编程
  9. 使用TCP/IP Monitor监视Soap协议
  10. struts2(六)之ognl表达式与ActionContext、ValueStack
  11. es6属性基础教学,30分钟包会
  12. ASP.NET Core 使用 SignalR 遇到的 CORS 问题
  13. Git使用一:git客户端安装与创建用户
  14. git----------SourceTree如何连接ssh的仓库地址,这里记录的是客户端需要做的事
  15. zabbix监控k8s出现的pod error status
  16. Java @Override 注解
  17. 在vi 按了Ctrl s 之后..
  18. hive 数据导出三种方式
  19. http2.0之头部压缩
  20. Flume在企业大数据仓库架构中位置及功能

热门文章

  1. FZU 1920 Left Mouse Button 简单搜索
  2. Asp.Net MVC 在后台获取PartialView、View文件生成的字符串
  3. sftp配置多用户权限
  4. 《JavaScript高级程序设计》读书笔记 ---语句
  5. POJ 2418 Hardwood Species (哈希,%f 和 %lf)
  6. RPC框架基本原理(二):客户端注册
  7. 关于LCD的分屏与切屏 Tearing effect
  8. 错误记录-spring+mybatis
  9. C语言_函数【转】
  10. Unity3d之将terrain转化成mesh