Lorenzo Von Matterhorn(map的用法)
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的用法)的更多相关文章
- CodeForces 696A:Lorenzo Von Matterhorn(map的用法)
http://codeforces.com/contest/697/problem/C C. Lorenzo Von Matterhorn time limit per test 1 second m ...
- #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 ...
- CF 696 A Lorenzo Von Matterhorn(二叉树,map)
原题链接:http://codeforces.com/contest/696/problem/A 原题描述: Lorenzo Von Matterhorn Barney lives in NYC. ...
- Lorenzo Von Matterhorn
Lorenzo Von Matterhorn Barney lives in NYC. NYC has infinite number of intersections numbered with p ...
- Lorenzo Von Matterhorn(STL_map的应用)
Lorenzo Von Matterhorn time limit per test 1 second memory limit per test 256 megabytes input standa ...
- codeforces 696A A. Lorenzo Von Matterhorn(水题)
题目链接: A. Lorenzo Von Matterhorn time limit per test 1 second memory limit per test 256 megabytes inp ...
- A. Lorenzo Von Matterhorn
A. Lorenzo Von Matterhorn time limit per test 1 second memory limit per test 256 megabytes input sta ...
- 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 (LCA + map)
方法:求出最近公共祖先,使用map给他们计数,注意深度的求法. 代码如下: #include<iostream> #include<cstdio> #include<ma ...
随机推荐
- EC11编码器的使用方法
1. EC11编码器的原理图如下 2. 旋转的时候,波形如下,EC11转1格,产生一个上升沿的中断,思路就是检测AX4-1的上升沿中断(平时是低电平),进入中断服务函数,检测AX4-2的电平,低电平逆 ...
- DES 指定键的大小对于此算法无效
KEY (byte[]) 长度不为8. 一般KEY使用UTF8编码. byte[] byKey = Encoding.UTF8.GetBytes(key); 加密内容的编码,由两方协商. Sys ...
- Collectd 和 InfluxDB 的部署和使用
更新软件包 $ sudo apt-get update$ sudo apt-get upgrade$ sudo reboot 安装influxdb hanwei@ubuntu-lab:~$ wget ...
- <automate the boring stuff with python> 正则强口令实例
书中7.18的强口令实践题 写一个函数,它使用正则表达式,确保传入的口令字符串是强口令.强口令的定义是: 长度不少于8 个字符,同时包含大写和小写字符,至少有一位数字. 你可能需要用多个正则表达式来测 ...
- 软件素材---linux C语言:拼接字符串函数 strcat的用例(与char数组联合使用挺好)
[头文件]#include <string.h> [原型] 1 char *strcat(char *dest, const char *src); [参数]: dest 为目标字符串指针 ...
- 029 Android 轮播图广告Banner开源框架使用
1.Banner介绍 现在的绝大数app都有banner界面,实现循环播放多个广告图片和手动滑动循环等功能. 2.使用环境配置(具体可见github开源项目) (1)添加依赖 在build.gradl ...
- 剑指offer66:机器人的运动范围
1 题目描述 地上有一个m行和n列的方格.一个机器人从坐标0,0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于k的格子. 例如,当k为18时,机器 ...
- Django基础之django分页
一.Django的内置分页器(paginator) view from django.shortcuts import render,HttpResponse # Create your views ...
- C 语言字符串的比较
C 语言字符串的比较 #include <stdio.h> #include <Windows.h> #include <string.h> int main(vo ...
- 创建 Python Virtualenv 虚拟隔离环境
video:创建 Python Virtualenv 虚拟隔离环境 python 虚拟环境 venv 简单用法 - littlemore - 博客园 创建 Python Virtualenv 虚拟隔离 ...