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. ...
随机推荐
- 【JDK】JDK源码分析-Collection
Java 集合框架(Java Collections Framework, JCF)包含很多平时开发中的常用类,例如 List.Set.ArrayList.HashMap.HashSet 等,因此打算 ...
- K8S 部署 Web UI
在早期的版本中 Kubernetes可以在 Dashboard 中看到 heapster 提供的一些图表信息, 在后续的版本中会陆续移除掉 heapster,现在更加流行的监控工具是 promethe ...
- BootStrap实现简单响应式导航菜单
用BootStrap实现响应式导航栏,我会对其中的一些样式进行说明. 先上代码,是一个很简单的Demo. <!doctype html> <html> <head&g ...
- 案例实战:每日上亿请求量的电商系统,JVM年轻代垃圾回收参数如何优化?
出自:http://1t.click/7TJ 目录: 案例背景引入 特殊的电商大促场景 抗住大促的瞬时压力需要几台机器? 大促高峰期订单系统的内存使用模型估算 内存到底该如何分配? 新生代垃圾回收优化 ...
- Spring MVC浅入浅出——不吹牛逼不装逼
Spring MVC浅入浅出——不吹牛逼不装逼 前言 上文书说了Spring相关的知识,对Spring来了个浅入浅出,大家应该了解到,Spring在三层架构中主做Service层,那还有Web层,也就 ...
- IOS7.0唯一“设备ID”的获取方法
ios7.0 以后通过sysctl获得的mac地址已经失效,所有设备均为020000000000. 可以通过苹果的keychain机制,实现设备的唯一ID标示. 具体过程:在app第一次安装时,生成一 ...
- React躬行记(13)——React Router
在网络工程中,路由能保证信息从源地址传输到正确地目的地址,避免在互联网中迷失方向.而前端应用中的路由,其功能与之类似,也是保证信息的准确性,只不过来源变成URL,目的地变成HTML页面. 在传统的前端 ...
- android ——Intent
Intent是android程序中各组件之间进行交互的重要方式,它可以用于指明当前组件想要执行的动作,也可以在不同组件之间传递数据,Intent一般被用于启动活动,启动服务以及发送广播. 一.显式的使 ...
- mysql主从不同步处理过程分享
背景 8月7日15:58收到报障数据库出现不同步:数据库共四台,分别为10.255.70.11,10.255.70.12,10.255.70.13,10.255.70.14(ip为虚拟ip) 数据库 ...
- Spring Cloud Gateway 服务网关快速上手
Spring Cloud Gateway 服务网关 API 主流网关有NGINX.ZUUL.Spring Cloud Gateway.Linkerd等:Spring Cloud Gateway构建于 ...