CodeForces 696A Lorenzo Von Matterhorn (LCA + map)
2024-08-25 22:52:39
方法:求出最近公共祖先,使用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 ;
}
最新文章
- ArrowLayer : A coustom layer animation
- 医院管理者必须知道的医院客户关系管理(CRM)
- MySQL查询缓存
- ST05 跟踪SQL
- BCP及自增标识列
- 实现OAUTH协议 实现 QQ 第三方登录效果
- Qt Creator调试
- 【ios开发】Block编程
- 使用TCP/IP Monitor监视Soap协议
- struts2(六)之ognl表达式与ActionContext、ValueStack
- es6属性基础教学,30分钟包会
- ASP.NET Core 使用 SignalR 遇到的 CORS 问题
- Git使用一:git客户端安装与创建用户
- git----------SourceTree如何连接ssh的仓库地址,这里记录的是客户端需要做的事
- zabbix监控k8s出现的pod error status
- Java @Override 注解
- 在vi 按了Ctrl s 之后..
- hive 数据导出三种方式
- http2.0之头部压缩
- Flume在企业大数据仓库架构中位置及功能
热门文章
- FZU 1920 Left Mouse Button 简单搜索
- Asp.Net MVC 在后台获取PartialView、View文件生成的字符串
- sftp配置多用户权限
- 《JavaScript高级程序设计》读书笔记 ---语句
- POJ 2418 Hardwood Species (哈希,%f 和 %lf)
- RPC框架基本原理(二):客户端注册
- 关于LCD的分屏与切屏 Tearing effect
- 错误记录-spring+mybatis
- C语言_函数【转】
- Unity3d之将terrain转化成mesh