codeforces C. Lorenzo Von Matterhorn

[QMT量化交易小白入门]-六十九、ETF动量评分策略,历史年化收益率107% C . bar_date } 跳过') returnC . bar_date } 跳过') returnC . bar_date } 跳过') return作用:如果当前不是回测模式且不是最后一根K线,则记录日志并跳过本次任务。这是为了避免在实时交易中对尚未完成的数据进行处理。 阅读详情

C. Lorenzo Von Matterhorn
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
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).

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

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

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <map>
typedef long long ll;

using namespace std;

void swap(ll &x, ll &y) {
    ll t = x;
    x = y;
    y = t;
}

int main()
{
    int q, c;
    map<ll, ll> m;
    ll v, u, w;
    m.clear();
    cin >> q;
    while (q--) {
        scanf("%d", &c);
        scanf("%I64d%I64d", &v, &u);
        if (c == 1) {
            scanf("%I64d", &w);
            while (v != u) {
                //时刻保持从子节点往父节点的方向算
                if (v < u) {
                    swap(v, u);
                }
                m[v] += w;
                v /= 2;
            }
        } else {
            ll ans = 0;
            while (v != u) {
                if (v < u) {
                    swap(v, u);
                }
                ans += m[v];
                v /= 2;
            }
            printf("%I64d\n", ans);
        }
    }
    return 0;
}







Panda3d 场景管理 许多简单的 3D 引擎维护一个 3D 模型列表,然后在每一帧对这些模型进行渲染。在这些简单的引擎中,必须分配一个3D模型(或从磁盘加载它),然后将其插入到要渲染的模型列表中。模型在插入到列表中之前对渲染器不“可见”。 Panda3D稍微复杂一些。它不是维护要呈现的对象列表,而是维护要呈现的对象树。对象在插入树之前对渲染器不可见。 阅读详情

相关推荐

团子杂记:SAP收入确认工具RAR(Revenue Recognition&Reporting)在新收入准则下的应用

SAP收入确认工具RAR知识分享

Lucas1211的博客 4281

Codeforces - Binary Tree on Plane

题目链接:Codeforces - Binary Tree on Plane 什么是二叉树呢?我们需要限制每个点一个父亲(除开根节点),每个点最多两个儿子。 然后我们拆点: S到每个点的出点,流量为2,保证最多两个儿子。 每个点的入点到T,流量为1,保证每个点一个父亲。 对于能到更小的y值,流量为1,费用为两点之间的距离。 最后MCMF即可。 AC代码: #pragma GCC optimiz...

青烟绕指柔的博客 498

SQLite → PostgreSQL 迁移脚本与踩坑清单

本文提供了SQLite迁移到PostgreSQL的两种可靠路径:首选使用pgloader工具自动转换schema和数据,支持类型映射和并行复制;备选方案通过sqlite3导出CSV再用PostgreSQL的COPY命令导入,适合需要自定义清洗的场景。重点介绍了数据类型映射规则、迁移步骤、校验方法和常见问题,包括自增主键、布尔值、时间类型等关键差异处理。迁移后需进行行数核对、哈希抽样等校验,并给出了性能优化建议。最后强调迁移不仅是数据转移,更需要同步升级数据模型和索引策略,根据业务场景选择合适的数据库方案。

aifs2025的博客 1884

CodeForces960DFull Binary Tree Queries(二叉树的操作)

Full Binary Tree QueriesYou have a full binary tree having infinite levels.Each node has an initial value. If a node has value x, then its left child has value 2·x and its right child has value 2·x + ...

nka_kun的博客 709

相机图像质量研究(15)常见问题总结:光学结构对成像的影响--暗角

老式相机的暗角往往比较明显,周围一圈能够明显感觉到亮度下降的很多,当然有些电源模仿手持相机的摄影效果,也能看到周围明显的亮度下降。一、

xiaoshixiu的专栏 1120

【ECCV 2024】动态范围直方图自注意力DHSA,即插即用!

直方图自注意力机制,将空间特征按强度分段并进行分组,然后在各个分组内或分组间应用自注意力,以选择性地关注具有动态范围的空间特征,并处理长距离内相似退化的像素。

qq_43303160的博客 1844

codeforces——————ko

【代码】codeforces错题。

2303_79552392的博客 199

华大HC32F460从零到200MHz:手把手教你配置时钟系统(附避坑指南)

本文详细解析了华大HC32F460 MCU的时钟系统配置,从启动过程、时钟树分析到实战配置200MHz系统时钟。文章手把手指导开发者如何正确配置外部晶振、MPLL锁相环及总线分频,并提供了硬件检查、代码解读及常见问题排查的避坑指南,帮助用户从零构建稳定可靠的高性能时钟环境。

net55的博客 293

【CodeForces - 697C】Lorenzo Von Matterhorn(二叉树,思维)

题干: 巴尼住在NYC。NYC具有从1开始的正整数编号的无数个交叉点。在交叉点i和2i之间以及i和2i + 1之间存在双向道路,对任意整数i都满足。在任意两点之间都有且只有一条最短路。 最初任何人都可以通过任何道路免费。 但是后来发生了 q 个事件。 有两种类型的事件: 1. 政府制定新规。 一个规则可以用三个整数表示v,u和w。经过这次操作,点u到点v...

xuanweiace的博客 687

Swin Transformer 与 CNN 结合实现图像分类

Swin Transformer 与 CNN 结合实现图像分类。(含swin transformer 结构讲解,cnn网络搭建,代码)

这里是Mae。 5025

YOLOv11改进 | 损失函数改进篇 | EIoU、SIoU、WIoU、DIoU、FocusIoU等二十余种损失函数

这篇文章介绍了YOLOv11的重大改进,特别是在损失函数方面的创新。它不仅包括了多种IoU损失函数的改进和变体,如SIoU、WIoU、GIoU、DIoU、EIOU、CIoU,还融合了“Focus”思想,创造了一系列新的损失函数。这些组合形式的损失函数超过了二十余种,每种都针对特定的目标检测挑战进行优化。文章会详细探讨这些损失函数如何提高YOLOv11在各种检测任务中的性能,包括提升精度、加快收敛速度和增强模型对复杂场景的适应性。

Snu77的博客 6188

codeforces 722D Generating Sets (二叉树 + 贪心)

解题思路: 可以根据题中所给的操作构造出一个二叉树,题目中要求的就是将所有的元素溯源,并且最后得到的值不互相重复的序列。并且要求所得序列的最大值最小,所以考虑贪心的想法,每一次对最大的值进行操作,将它上升到与他在同一条链上最近的无重叠元素的点,用set判断当前节点的值是否存在,循环对这个set内的元素进行操作,直到最大的一个元素无法被操作截至。 AC代码: #include using n

wchhlbt的博客 871

C++从入门到进阶的系列书籍推荐(22本书)

要多读书,读好书!一本书要多读几遍才好,每次都可能有新的收获!特别在是有一定的工作年限和工作经验之后!读书百遍,其义自见! 下面就给大家推荐一下C++程序员进阶需要读的经典书籍: 1、C++入门书籍 C++是在C语言的基础上发展而来的,要学习C++之前需要先学习C语言。有了C语言基础之后再去学习C++。 1.1、C语言程序设计(谭浩强) 该书被公认为学习C语言程序设计的优秀教材,被全国大多数高校选用,是学习C语言的主流用书。本书比较通俗易懂,是初入者必备的书籍。 ........

dvlinker的技术专栏 4万+

Codeforces 697C Lorenzo Von Matterhorn 题解

Codeforces 697C题解

DarkoDu的博客 675

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

Codeforces-697C 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 betwe

Wilbert的博客 1730

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

Codeforces Round #362 (Div. 2) C. Lorenzo Von Matterhorn (map离散化)

Problem: Codeforces Round #362 (Div. 2) C. Lorenzo Von Matterhorn (map离散化) Analyse: 题意不赘述,根据题意可以构造出一个以11为根节点的二叉树。 然后我们用dp[0][i]dp[0][i]表示以ii为父亲节点,发出的连向它的左二子的边的长度。dp[1][i]dp[1][i]表示以ii为父亲节点,连到右儿子的长度。

JIBANCANYANG 549

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

CodeForces 697C Lorenzo Von Matterhorn

题意:有q个节点构成二叉树,有两种输入,第一种是1 v u w,代表是从v节点走到u节点需要w元。第二种是2 v u,代表是从v节点走到u节点,并且输出所需的钱。 思路:看到这道题的时候发现1 ≤ v, u ≤ 1018,这两个数特别大,感觉会用到map去存。另一个难点就是怎么去判断v节点和u节点是否在1的两侧或同侧。判断方法就是,先比较v,u两个点的大小,再将大的那个节点除以二(就一定是它的父...

JTY的博客 525

【CodeForces 697C】Lorenzo Von Matterhorn(LCA)

Least Common Ancestors 节点范围是1~1e18,至多1000次询问。 只要不断让深的节点退一层(>>1)就能到达LCA。 用点来存边权,用map储存节点和父亲连边的权值。 #include<cstdio> #include<map> #define ll long long using namespace std...

dingguayi7025的博客 211

Lorenzo Von Matterhorn【Codeforces 697 C】【map + 模拟LCA过程】

Codeforces Round #362 (Div. 2) C 题目说,每个点(价值为i的点)的下面的两个点分别是“2 * i”和“2 * i + 1”,那么不就是一个二叉树的嘛?这样子就算N、M是1e18,那样的每次找最近公共祖先结点的复杂度也不过是log2(1e18)而已。所以我们可以直接把这条链上的每一条边给加上权值,查询也是一样的操作,因为Q只有1000次,所以,我们用个map维护...

既然弱小,就只顾变强就是了 425

【 Codeforces Round #362 C】Lorenzo Von Matterhorn(思维)

题意: 巴尼住在NYC。NYC具有从1开始的正整数编号的无数个交叉点。在交叉点i和2i之间以及i和2i + 1之间存在双向道路,对任意整数i都满足。在任意两点之间都有且只有一条最短路。 最初任何人都可以通过任何道路免费。 但是后来发生了 q 个事件。 有两种类型的事件: 1. 政府制定新规。 一个规则可以用三个整数表示v,u和w。经过这次操作,点u到点...

__似水流年__的博客 282

Codeforces697C Lorenzo Von Matterhorn

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

Low Coder的博客 360

codeforces 697C Lorenzo Von Matterhorn(二叉树LCA)

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

wchhlbt的博客 802

CodeForces 697C——Lorenzo Von Matterhorn(二叉树,map)

C. Lorenzo Von Matterhorn time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Barney lives in NYC. NYC has infin

say_c_box的博客 512

CodeForces 697C Lorenzo Von Matterhorn (二叉树寻径)

C - Lorenzo Von Matterhorn Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 697C Description Barney lives in NYC. NYC

DT2131 610

CodeForces - 697C-Lorenzo Von Matterhorn(有点像LCA,原创

传送门:CodeForces - 697C 原创--原创--原创 第一次自己A了一道感觉有点难度的题; 题意:在一个类似于二叉树的图上,1 : u ,v,w 表示从u到v的所以路都加上w的费用;                 2 : u,v 输出u,v间的花费; 思路:自己看这个图,一直想写线段树,后来想想LCA求最近公共祖先,一步一步向上跳的思想还可以,    就用 每个点 ...

weixin_30918633的博客 320

CodeForces 697C. Lorenzo Von Matterhorn(二叉树+map)

题目:http://codeforces.com/problemset/problem/697/C 题意: 一颗二叉树; 操作1:u-&gt;v路径上所有边权值+w 操作2:计算u-&gt;v路径权值和 分析: 一开始写了状压,毕竟深度最多60,0向左走,1向右走,写太长了放弃了。 然后,考虑到q很小,只有1e3,实际上有用的边也很少; 所有,直接用map存边权。 代码: ...

OI -> ACM -> AI 1188
上一篇: PAT (Advanced Level) Practise 1015. Reversible Primes (20) 素数筛法 进制转换
下一篇: hdu5723 Abandoned country
Daemoonn
博客等级 码龄11年 42粉丝 198原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值