CodeForces - 697C-Lorenzo Von Matterhorn(有点像LCA,原创
传送门: CodeForces - 697C
原创--原创--原创
第一次自己A了一道感觉有点难度的题;
题意:在一个类似于二叉树的图上,1 : u ,v,w 表示从u到v的所以路都加上w的费用;
2 : u,v 输出u,v间的花费;
思路:自己看这个图,一直想写线段树,后来想想LCA求最近公共祖先,一步一步向上跳的思想还可以,
就用 每个点 表示这个点到父节点所需要的花费;计算的时候,和求LCA相似,累加到最近公共祖先的花费;
还有就是用map< ll , ll >存每个节点的信息;
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
using namespace std;
typedef long long ll;
map<ll,ll>mp;
int q;
int log2(ll x)
{
int res=;
while(x>)
{
x/=;
res++;
}
return res+;
}
int main(){
//freopen("in","r",stdin);
scanf("%d",&q);
while(q--)
{
int tag;
ll u,v,w;
scanf("%d",&tag);
if(tag==)
{
scanf("%lld%lld%lld",&u,&v,&w);
if(log2(u)<log2(v))swap(u,v);
while(log2(u)>log2(v))
{
mp[u]+=w;
u/=;
} while(u!=v)
{
mp[u]+=w;
mp[v]+=w;
u=u/;
v=v/;
}
}
else
{
scanf("%lld%lld",&u,&v);
ll ans = ;
if(log2(u)<log2(v))swap(u,v);
while(log2(u)>log2(v))
{
ans+=mp[u];
u/=;
}
while(u!=v)
{
ans+=mp[u];
ans+=mp[v];
u=u/;
v=v/;
}
// if(u!=1)ans+=mp[u];
printf("%lld\n",ans);
// if(ans==94)cout<<"*"<<mp[6]<<endl;
}
} return ;
}
CodeForces - 697C-Lorenzo Von Matterhorn(有点像LCA,原创的更多相关文章
- CodeForces 696A Lorenzo Von Matterhorn (LCA + map)
方法:求出最近公共祖先,使用map给他们计数,注意深度的求法. 代码如下: #include<iostream> #include<cstdio> #include<ma ...
- 【STL】【模拟】Codeforces 696A Lorenzo Von Matterhorn
题目链接: http://codeforces.com/problemset/problem/696/A 题目大意: 一个满二叉树,深度无限,节点顺序编号,k的儿子是k+k和k+k+1,一开始树上的边 ...
- cf 697C Lorenzo Von Matterhorn 思维
题目链接:https://codeforces.com/problemset/problem/697/C 两种操作: 1是对树上u,v之间的所有边的权值加上w 2是查询树上u,v之间的边权和 树是满二 ...
- codeforces 696A Lorenzo Von Matterhorn 水题
这题一眼看就是水题,map随便计 然后我之所以发这个题解,是因为我用了log2()这个函数判断在哪一层 我只能说我真是太傻逼了,这个函数以前听人说有精度问题,还慢,为了图快用的,没想到被坑惨了,以后尽 ...
- #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 ...
- C. Lorenzo Von Matterhorn LCA
C. Lorenzo Von Matterhorn time limit per test 1 second memory limit per test 256 megabytes input sta ...
- CodeForces 696A:Lorenzo Von Matterhorn(map的用法)
http://codeforces.com/contest/697/problem/C C. Lorenzo Von Matterhorn time limit per test 1 second m ...
- codeforces 696A A. Lorenzo Von Matterhorn(水题)
题目链接: A. Lorenzo Von Matterhorn time limit per test 1 second memory limit per test 256 megabytes inp ...
- Lorenzo Von Matterhorn
Lorenzo Von Matterhorn Barney lives in NYC. NYC has infinite number of intersections numbered with p ...
- CF 696 A Lorenzo Von Matterhorn(二叉树,map)
原题链接:http://codeforces.com/contest/696/problem/A 原题描述: Lorenzo Von Matterhorn Barney lives in NYC. ...
随机推荐
- 整理github总结
之前记述过两篇关于git命令的随笔,那时候对git的理解还不太深(虽然现在理解的也不是很多...菜哭了),只会用那些命令,并没有形成自己的知识,今天下载完Git之后,规整了一下自己的github,在吃 ...
- middleware中间件
django 中的中间件(middleware),在django中,中间件其实就是一个类,在请求到来和结束后,django会根据自己的规则在合适的时机执行中间件中相应的方法. 在django项目的se ...
- Template(模板)模式
第三章:模板模式 Template模式比较简单,是基于继承关系的一种设计模式,由父类定义处理流程框架,由子类中实现具体处理. Code: package example.template; /*** ...
- nginx基本运维及常用配置
nginx基本运维及常用配置 ========================================================== 基本运维 nginx 的启动 nginx -c /p ...
- Paxos算法原理
1.从ACID到CAP 我们知道传统集中式系统中实现ACID是很简单的,在分布式环境中,涉及到不同的节点,节点内的ACID可以控制,那么节点间的ACID如何控制呢?构建一个可用性和一致性的分布系统成为 ...
- alluxio源码解析-rpc调用概述-client和worker之间的block模块的通讯架构(netty版本)(3)
(1.8版本)client和worker之间的block模块的通讯架构 block作为alluxio文件读取或者存储的最小基本单位,都是通过BlockOutStream和BlockInputtream ...
- vue过滤器微信小程序过滤器和百度智能小程序过滤器
因为最近写了微信小程序和百度小程序,用到了过滤器,感觉还挺好用的,所以就来总结一下,希望能帮到你们. 1. 微信小程序过滤器: 1.1:首先建一个单独的wxs后缀的文件,一般放在utils文件夹里面. ...
- 前端项目优化 -Web 开发常用优化方案、Vue & React 项目优化
github github-myBlob 从输入URL到页面加载完成的整个过程 首先做 DNS 查询,如果这一步做了智能 DNS 解析的话,会提供访问速度最快的 IP 地址回来 接下来是 TCP 握手 ...
- MacOS VSCode 安装 GO 插件失败问题解决
0x00 问题重现 Installing golang.org/x/tools/cmd/guru FAILED Installing golang.org/x/tools/cmd/gorename F ...
- 垂直渐变的Button
public partial class RoundButton : Button { Rectangle r; private Brush _myBrush = null; , , ); , , ) ...