http://www.cnblogs.com/a2985812043/p/7224574.html

解法:这是网上看到的

因为要计算u->v的权值之和,我们可以把权值放在v中,由于题目中给定的u、v特性,我们可以从最后一个v开始倒回来每次除以2,然后把权值加起来就好了,注意输入的区间大小值

 #include <iostream>
#include <cstdio>
#include <map>
using namespace std;
#define LL long long
map<LL, LL>node;
int q, com;
LL u, v, w;
int main()
{
while(~scanf("%d", &q)){
while(q--){
scanf("%d", &com);
if(com==){
scanf("%lld%lld%lld", &u, &v, &w);
while(u!=v){
if(u>v){
node[u]+=w;
u/=;
}
else{
node[v]+=w;
v/=;
}
}
}
else{
scanf("%lld%lld", &u, &v);
LL ans=;
while(u!=v){
if(u>v){
ans+=node[u];
u/=;
}
else{
ans+=node[v];
v/=;
}
}
printf("%lld\n", ans);
}
}
}
return ;
}

http://www.cnblogs.com/fightfordream/p/5672915.html

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <map>
using namespace std;
#define N 10005
map<long long,long long> mp; void add(long long a,long long w)
{
if(mp.find(a)==mp.end()) mp[a]=;
mp[a]+=w;
} long long val(long long a)
{
if(mp.find(a)==mp.end()) return ;
return mp[a];
} int main()
{
int t;
cin>>t;
while(t--){
int s;
cin>>s;
if(s==){
long long u,v,w;
cin>>u>>v>>w;
while(u!=v){
//对节点进行更新,直到更新到根节点
if(u>v){
add(u,w);
u>>=;
}
else{
add(v,w);
v>>=;
}
}
}
else{
long long u,v,ans=;
cin>>u>>v;
while(v!=u){
//查询
if(u>v){
ans+=val(u);
u>>=;
}
else{
ans+=val(v);
v>>=;
}
}
cout<<ans<<endl;
}
}
return ;
}

Lorenzo Von Matterhorn(map的用法)的更多相关文章

  1. CodeForces 696A:Lorenzo Von Matterhorn(map的用法)

    http://codeforces.com/contest/697/problem/C C. Lorenzo Von Matterhorn time limit per test 1 second m ...

  2. #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 ...

  3. CF 696 A Lorenzo Von Matterhorn(二叉树,map)

    原题链接:http://codeforces.com/contest/696/problem/A 原题描述: Lorenzo Von Matterhorn   Barney lives in NYC. ...

  4. Lorenzo Von Matterhorn

    Lorenzo Von Matterhorn Barney lives in NYC. NYC has infinite number of intersections numbered with p ...

  5. Lorenzo Von Matterhorn(STL_map的应用)

    Lorenzo Von Matterhorn time limit per test 1 second memory limit per test 256 megabytes input standa ...

  6. codeforces 696A A. Lorenzo Von Matterhorn(水题)

    题目链接: A. Lorenzo Von Matterhorn time limit per test 1 second memory limit per test 256 megabytes inp ...

  7. A. Lorenzo Von Matterhorn

    A. Lorenzo Von Matterhorn time limit per test 1 second memory limit per test 256 megabytes input sta ...

  8. C. Lorenzo Von Matterhorn LCA

    C. Lorenzo Von Matterhorn time limit per test 1 second memory limit per test 256 megabytes input sta ...

  9. CodeForces 696A Lorenzo Von Matterhorn (LCA + map)

    方法:求出最近公共祖先,使用map给他们计数,注意深度的求法. 代码如下: #include<iostream> #include<cstdio> #include<ma ...

随机推荐

  1. 【GStreamer开发】GStreamer基础教程07——多线程和Pad的有效性

    目标 GStreamer会自动处理多线程这部分,但在有些情况下,你需要手动对线程做解耦.本教程会教你怎样才能做到这一点,另外也展示了Pad的有效性.主要内容包括: 如何针对部分的pipeline建立一 ...

  2. Django 操作已经存在的数据库

    反向操作数据库 何为反向操作.即是数据库在项目之前已经存在,不需要新建表,操作已经存在的表 # 进入站点目录下执行 python manage.py inspectdb #可以看到settings中连 ...

  3. ubuntu 18.04 安装搜狗输入法

    安装 fcitxsudo apt install fcitx 配置输入法打开fcitx配置界面,点击桌面左下角的菜单,弹出如下桌面: 点击: “Input Method”,进入设置界面 点击 “OK” ...

  4. mysql navcat备份使用详解

    mysql navcat备份使用详解 点击备份 然后新建备份 然后选择要备份的表 就可以了 以后这个表删除了 内容变更了 都可以点击 还原备份就可以了

  5. js原生导出excel和csv

    ​ 严格意义来说并不是真正的excel文件,只是可以用excel打开查看而已,实际上的格式是逗号分隔文件即csv文件. 这里有几个坑要说一下: 不加Unicode的utf8头部标识excel打开文件会 ...

  6. ubuntu gcc 安装 使用

    "人多不足以依赖,要生存只有靠自己." -- 拿破仑 gcc是什么:-------------------------- Linux系统下的Gcc(GNU C Compiler)是 ...

  7. 02 HTML

    1. HTML概念: HTML是最基础的网页开发语言 * Hyper Text Markup Language 超文本标记语言 * 超文本: * 超文本是用超链接的方法,将各种不同空间的文字信息组织在 ...

  8. spring 整合guava

    一.ApplicationContext.xml中的配置 <!--开启缓存注解--> <cache:annotation-driven /> <bean id=" ...

  9. Django框架2——模板

    django框架2--模板 直接将HTML硬编码到你的视图里却并不是一个好主意: 对页面设计进行的任何改变都必须对 Python 代码进行相应的修改. 站点设计的修改往往比底层 Python 代码的修 ...

  10. js 中加减乘除 比较精确的算法,js本身有些运算会出错,这里给出较精确的算法

    问题这样的: 37.5*5.5=206.08  (JS算出来是这样的一个结果,我四舍五入取两位小数) 我先怀疑是四舍五入的问题,就直接用JS算了一个结果为:206.08499999999998 怎么会 ...