原题链接:http://codeforces.com/contest/696/problem/A

原题描述:

Lorenzo Von Matterhorn
 

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 vu 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).

Input

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.

Output

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.

Example
input
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
output
94
0
32
Note

In the example testcase:

Here are the intersections used:

  1. Intersections on the path are 3, 1, 2 and 4.
  2. Intersections on the path are 4, 2 and 1.
  3. Intersections on the path are only 3 and 6.
  4. 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.
  5. Intersections on the path are 6, 3 and 1.
  6. Intersections on the path are 3 and 7. Passing fee of the road between them is 0.
  7. 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).

题目大意:有个男的想去另一个地点见妹子,每个地点有一个唯一的代号,每一段路程一开始是免费的,然后政府会公布两个地点u与v之间的最短路径中的每一段将增加收费w美元如果输入的是1 v u w则代表公布v地到u地的最短路程的每一段需要收费w美元,如果输入的是2 v u则让你求v地到u地的收费总额是多少。

解题思路:除了标号为1的点其他每个地点向上都只有一条路,可以用map<LL,LL>M来暴力做。每次向上搜索地点代号的值除2就行了.

AC代码:

# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <iostream>
# include <fstream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <set>
# include <math.h>
# include <algorithm>
using namespace std;
# define pi acos(-1.0)
# define mem(a,b) memset(a,b,sizeof(a))
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define For(i,n,a) for(int i=n; i>=a; --i)
# define FO(i,a,n) for(int i=a; i<n; ++i)
# define Fo(i,n,a) for(int i=n; i>a ;--i)
typedef long long LL;
typedef unsigned long long ULL; map<LL,LL>M; int main()
{
//freopen("in.txt", "r", stdin);
int q;
scanf("%d",&q);
while(q--)
{
int s,w;
LL u,v,ans=;
scanf("%d",&s);
if(s==)
{
map<LL,int>m;
scanf("%lld%lld%d",&v,&u,&w);
while(v!=)
{
M[v]+=w;
m[v]=;
v/=;
}
while(u!=)
{
if(m[u]==)
{
while(u!=)
{
M[u]-=w;
u/=;
}
break;
}
M[u]+=w;
u/=;
}
}
else
{
map<LL,int>m;
scanf("%lld%lld",&v,&u);
while(v!=)
{
m[v]=;
ans+=M[v];
v/=;
}
while(u!=)
{
if(m[u]==)
{
while(u!=)
{
ans-=M[u];
u/=;
}
break;
}
ans+=M[u];
u/=;
}
cout<<ans<<endl;
}
}
return ;
}

  

CF 696 A 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. Lorenzo Von Matterhorn(map的用法)

    http://www.cnblogs.com/a2985812043/p/7224574.html 解法:这是网上看到的 因为要计算u->v的权值之和,我们可以把权值放在v中,由于题目中给定的u ...

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

  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. cf 697C Lorenzo Von Matterhorn 思维

    题目链接:https://codeforces.com/problemset/problem/697/C 两种操作: 1是对树上u,v之间的所有边的权值加上w 2是查询树上u,v之间的边权和 树是满二 ...

随机推荐

  1. 初学node.js-nodejs中实现用户登录路由

    经过前面几次的学习,已经可以做下小功能,今天要实现的事用户登录路由. 一.users_model.js  功能:定义用户对象模型 var mongoose=require('mongoose'), S ...

  2. API接口设计

    1.场景描述 比如说我们要做一款APP,需要通过api接口给app提供数据.假设我们是做商城,比如我们卖书的.我们可以想象下这个APP大概有哪些内容: 1)首页:banner区域(可以是一些热门书籍的 ...

  3. [Git] 015 远程仓库篇 第二话

    0. 前言 在 [Git] 006 在本地新建一个仓库 中,我在本地建了一个仓库 "git_note" 这回的任务 在 GitHub 上建一个远程仓库:为方便记忆,我就起名为 &q ...

  4. IDEA导入Junit jar包,在JavaSE的Module中使用Junit测试

    写代码时偶尔想试一下自己的小想法,于是在IDEA中建了一个JavaEE项目.JavaEE项目中只能在main方法中运行代码块,不如单元测试的@Test灵活. 于是在网上找到了Junit的jar包:Do ...

  5. HNUSTOJ 1604:Operations

    1604: Operations 时间限制: 2 Sec  内存限制: 128 MB 提交: 313  解决: 97 [提交][状态][讨论版] 题目描述 You can perform the fo ...

  6. nodejs版实现properties后缀文件解析

    1.propertiesParser.js let readline = require('readline'); let fs = require('fs'); // properties文件路径 ...

  7. C# 静态方法调用非静态方法

    转载:http://blog.csdn.net/seattle1215/article/details/6657814 using System; using System.Collections.G ...

  8. C语言scanf的返回值

    #include <stdio.h> int main(void) { long num; long sum = 0L; int status; printf("Please e ...

  9. Python基础2——数据类型的操作

    列表操作 列表.字符串.元组的切片总结: num=[1, 2, 3, 4, 5, 6] 1.切片是通过下面来切,下标可以正数,也可以是复数.注意:复数的话,最后一个值的下标为-1,往前面推导就是-1, ...

  10. 对数据集做标准化处理的几种方法——基于R语言

    数据集——iris(R语言自带鸢尾花包) 一.scale函数 scale函数默认的是对制定数据做均值为0,标准差为1的标准化.它的两个参数center和scale: 1)center和scale默认为 ...