CodeForces 696A:Lorenzo Von Matterhorn(map的用法)
1 second
256 megabytes
standard input
standard output
Barney lives in NYC. NYC has infinite number of intersections numbered with positive integers starting from 1. There exists a bidirectional road between intersections i and 2i and another road between i and 2i + 1 for every positive integer i. You can clearly see that there exists a unique shortest path between any two intersections.

Initially anyone can pass any road for free. But since SlapsGiving is ahead of us, there will q consecutive events happen soon. There are two types of events:
1. Government makes a new rule. A rule can be denoted by integers v, u and w. As the result of this action, the passing fee of all roads on the shortest path from u to v increases by w dollars.
2. Barney starts moving from some intersection v and goes to intersection u where there's a girl he wants to cuddle (using his fake name Lorenzo Von Matterhorn). He always uses the shortest path (visiting minimum number of intersections or roads) between two intersections.
Government needs your calculations. For each time Barney goes to cuddle a girl, you need to tell the government how much money he should pay (sum of passing fee of all roads he passes).
The first line of input contains a single integer q (1 ≤ q ≤ 1 000).
The next q lines contain the information about the events in chronological order. Each event is described in form 1 v u w if it's an event when government makes a new rule about increasing the passing fee of all roads on the shortest path from u to v by w dollars, or in form 2 v u if it's an event when Barnie goes to cuddle from the intersection v to the intersection u.
1 ≤ v, u ≤ 1018, v ≠ u, 1 ≤ w ≤ 109 states for every description line.
For each event of second type print the sum of passing fee of all roads Barney passes in this event, in one line. Print the answers in chronological order of corresponding events.
7 1 3 4 30 1 4 1 2 1 3 6 8 2 4 3 1 6 1 40 2 3 7 2 2 4
94 0 32
In the example testcase:
Here are the intersections used:

- Intersections on the path are 3, 1, 2 and 4.
- Intersections on the path are 4, 2 and 1.
- Intersections on the path are only 3 and 6.
- Intersections on the path are 4, 2, 1 and 3. Passing fee of roads on the path are 32, 32 and 30 in order. So answer equals to32 + 32 + 30 = 94.
- Intersections on the path are 6, 3 and 1.
- Intersections on the path are 3 and 7. Passing fee of the road between them is 0.
- Intersections on the path are 2 and 4. Passing fee of the road between them is 32 (increased by 30 in the first event and by 2 in the second).
题意:在一个类似于树的图中,求从一个节点到另一个节点需要走过的距离,初始为0,输入为1时对u到v进行更新,输入为2时求u到v的距离。
思路:用map放每一个节点的对应边的权值,然后进行更新或者查询
#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 ;
}
CodeForces 696A:Lorenzo Von Matterhorn(map的用法)的更多相关文章
- 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,一开始树上的边 ...
- 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 ...
- codeforces 696A A. Lorenzo Von Matterhorn(水题)
题目链接: A. Lorenzo Von Matterhorn time limit per test 1 second memory limit per test 256 megabytes inp ...
- 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 ...
- A. Lorenzo Von Matterhorn
A. Lorenzo Von Matterhorn time limit per test 1 second memory limit per test 256 megabytes input sta ...
随机推荐
- .NET CORE EnvironmentVariable
.NET CORE System variables SETIn System variablese.g1:Variable name: ASPNETCORE_ENVIRONMENTVariable ...
- 将QuickReport报表保存为图片(使用TMetaFile和TMetafileCanvas)
//将报表第iPageNo页存为BMP文件 procedure ReportSaveToBMPFile(sFileName :string; iPageNo :integer) ...
- C#高性能大容量SOCKET并发(四):缓存设计
原文:C#高性能大容量SOCKET并发(四):缓存设计 在编写服务端大并发的应用程序,需要非常注意缓存设计,缓存的设计是一个折衷的结果,需要通过并发测试反复验证.有很多服务程序是在启动时申请足够的内存 ...
- Linux7下配置Nginx站点.
今天闲来无事,把服务器重新配置了一下,作为开发者,实际上很多人都是长时间不去配置服务器的,所以曾经很多东西又忘掉了差不多. 特在此分享一下配置成功后的配置文件内容. 其实配置后的文件内容很简单,也没有 ...
- MySQL学习-SQL约束
约束分类 主键:PRIMARY KEY用于唯一标识表中的一行,不可重复.e.g.:id INT(10) PRIMARY KEY 默认值:DEFAULT插入时,若没有指定该列的值,则为DEFAULT指定 ...
- Linux中的进程
进程,线程,程序 通俗的说,进程是程序的一次执行过程,程序是一种静态概念,如果在系统中引入线程,则进程是资源分配单元,线程是系统执行单元.此处不懂应参阅<操作系统> 进程衍生 fork-e ...
- Qt使用windows API获取程序运行时占用内存 good
使用的是psapi.h中的GetProcessMemoryInfo函数,但是运行到该函数时就强制退出了. 后来,百度到原因是 原来Qt编译时加了-mthread,createprocess时要使的Ha ...
- 浅谈stylus与sass的对比
all we konw , 这两个都是css的预编译工具,但虽然都是编译工具,但还是存在差别的,下面来讲讲其中的区别 1.变量 sass定义变量是以这种形式进行定义的$xxx:10;而stylus的定 ...
- 抓取报表ALV GRID上的数据
在项目开发过程中需要从标准报表MB5B中获取数据,以下是本人实例中的相关部分,程序同样适用于获取其他标准报表的数据. CL_SALV_BS_RUNTIME_INFO=>SET( DISPL ...
- ASP.NET Web API 直到我膝盖中了一箭【1】基础篇
蓦然回首,那些年,我竟然一直很二. 小时候,读武侠小说的时候,看到那些猪脚,常常会产生一种代入感,幻想自己也会遭遇某种奇遇,遇到悬崖跳下去是不是有本“武林秘笈”在等着?长大以后也是一样,多少人梦着醒着 ...