当前位置: 首页 > 工具软件 > Matterhorn > 使用案例 >

Codeforces 696A Lorenzo Von Matterhorn ( LCA )

胥诚
2023-12-01

http://www.codeforces.com/problemset/problem/696/A

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

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


题意: 一棵规范的二叉树,开始每条边的边权都为0。
两个操作,第一个操作是 将u,v这两个点之间所有的边的边权加上w。第二个操作是询问u,v这两点之间边权和。 u,v 小于 1e18。操作总数小于1000。

解法: 开始看到u,v这么大 有点不知道怎么写。居然想到离散化的什么鬼上面去了。然后想想操作数这么小,最多也就2000个点出现。而最大的深度也不过30,也就是说最大最大有60000条边。还是可以建个图来写的。因为u,v这条路必然通过 LCA(u,v)这个点来转折。求LCA(u,v)的过程并建图。
map< LL ,LL > mm; 表示每个点到其父亲节点的距离。

#include <iostream>
#include <map>
#include <algorithm>
using namespace std;
typedef long long LL;

map <LL ,LL > mm;
int main()
{
        int q;
        cin >> q;
        while( q--)
        {
                int op;
                LL u, v, w;
                cin >> op;
                if( op==1 )
                {
                        cin >> u >> v >> w;
                        while( u!=v )
                        {
                                if(u<v) swap(u,v);
                                mm[u] += w;
                                u/=2;
                        }
                }
                else
                {
                        cin >> u >> v;
                        LL ans = 0;
                        while( u!=v )
                        {
                                if(u < v) swap(u,v);
                                ans += mm[u];
                                u/=2;
                        }
                        cout << ans << endl;
                }
        }
        return 0;
}
 类似资料:

相关阅读

相关文章

相关问答