Lorenzo Von Matterhorn(STL_map的应用)
Lorenzo Von Matterhorn
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 form2 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
Note
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).
//给出一个无限节点的树,节点的标号从1到无穷大。它是这样定义的,节点i和节点2*i和节点2*i+1相连。
每条边都有一个权值,初始时为0.
给出q个操作。
1 u v w 代表把u和v之间的边的权值都加上w。
2 u v 询问u和v之间的边的权值总和。
//本来是感觉根本不能做的嘛,后来想到 STL 里的 map 可以完美的解决这个问题
#include <iostream>
#include <cstdio>
#include <map>
using namespace std; typedef long long LL; int main()
{
int q,op;
LL u,v,w;
scanf("%d",&q);
map<LL,LL> M;
while (q--)
{
scanf("%d",&op);
if(op==)
{
map<LL,int> vis;
scanf("%lld%lld%lld",&u,&v,&w);
while (u!=)
{
M[u]+=w;
vis[u]=;
u/=;
}
while (v!=)
{
if (vis[v]==)
{
while (v!=)
{
M[v]-=w;
v/=;
}
break;
}
M[v]+=w;
v/=;
}
}
else if (op==)
{
scanf("%lld%lld",&u,&v);
LL ans=; map<LL,int> vis;
while (u!=)
{
ans+=M[u];
//printf("%lld\n",ans);
vis[u]=;
u/=;
}
//printf("!!!\n");
while (v!=)
{
if (vis[v]==)
{
while(v!=)
{
ans-=M[v];
//printf("%lld\n",ans);
v/=;
}
break;
}
ans+=M[v];
v/=;
}
printf("%lld\n",ans);
}
}
return ;
}
Lorenzo Von Matterhorn(STL_map的应用)的更多相关文章
- Lorenzo Von Matterhorn
Lorenzo Von Matterhorn Barney lives in NYC. NYC has infinite number of intersections numbered with p ...
- C. Lorenzo Von Matterhorn LCA
C. Lorenzo Von Matterhorn time limit per test 1 second memory limit per test 256 megabytes input sta ...
- #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 ...
- CodeForces 696A:Lorenzo Von Matterhorn(map的用法)
http://codeforces.com/contest/697/problem/C C. Lorenzo Von Matterhorn time limit per test 1 second m ...
- CF 696 A Lorenzo Von Matterhorn(二叉树,map)
原题链接:http://codeforces.com/contest/696/problem/A 原题描述: Lorenzo Von Matterhorn Barney lives in NYC. ...
- A. Lorenzo Von Matterhorn
A. Lorenzo Von Matterhorn time limit per test 1 second memory limit per test 256 megabytes input sta ...
- 【CodeForces 697C】Lorenzo Von Matterhorn(LCA)
Least Common Ancestors 节点范围是1~1e18,至多1000次询问. 只要不断让深的节点退一层(>>1)就能到达LCA. 用点来存边权,用map储存节点和父亲连边的权 ...
- codeforces 696A Lorenzo Von Matterhorn 水题
这题一眼看就是水题,map随便计 然后我之所以发这个题解,是因为我用了log2()这个函数判断在哪一层 我只能说我真是太傻逼了,这个函数以前听人说有精度问题,还慢,为了图快用的,没想到被坑惨了,以后尽 ...
随机推荐
- 什么是ISP,他的工作原理是怎样的?
ISP是Image Signal Processor的缩写,全称是影像处理器.在相机成像的整个环节中,它负责接收感光元件(Sensor)的原始信号数据,可以理解为整个相机拍照.录像的第一步处理流程,对 ...
- ElasticSearch 监控单个节点详解
1.介绍 集群健康 就像是光谱的一端——对集群的所有信息进行高度概述. 而 节点统计值 API 则是在另一端.它提供一个让人眼花缭乱的统计数据的数组,包含集群的每一个节点统计值. 节点统计值 提供的统 ...
- python解析json文件报错No JSON object could be decoded
2017-04-25 可用Nodepad++将json文件打开并以UTF8无BOM格式保存.
- 利用eolinker实现api接口mock测试(mock server)
转载:http://blog.csdn.net/naicha_qin/article/details/78276172 前后端分离或者是进行单元测试的时候,必须要用mock api替换掉第三方调用或者 ...
- Laravel之哈希/常用函数/分页
一.哈希 1.简介Laravel Hash 门面为存储用户密码提供了安全的Bcrypt 哈希算法.如果你正在使用Laravel 应用自带的AuthController 控制器,将会自动为注册和认证使用 ...
- iOS仿支付宝首页的刷新布局效果
代码地址如下:http://www.demodashi.com/demo/12753.html XYAlipayRefreshDemo 运行效果 动画效果分析 1.UI需要变动,向上滑动的时候,顶部部 ...
- 杂(三)-The type java.lang.Object cannot be resolved It is indirectly referenced ...
The type java.lang.Object cannot be resolved. It is indirectly referenced from required .class files ...
- Ubuntu下安装配置和卸载Tomcat
转载自:http://zyjustin9.iteye.com/blog/2177291 注:此处不采用apt-get安装,因为这种方式安装后,tomcat安装目录会到处都是,像天女散花一样.此处采取下 ...
- Java + Selenium + WebDriver八大元素定位方式
UI自动化测试的第一步就是进行元素定位,下面给大家介绍一下Selenium + WebDriver的八大元素定位方式.现在我们就以百度搜索框为例进行元素定位,如下图: 一.By.name() Java ...
- DevOpsDays 活动咨询网站
站点:http://www.41huiyi.com/event-1452630998.html