题目链接:https://codeforces.com/problemset/problem/697/C

两种操作:

1是对树上u,v之间的所有边的权值加上w

2是查询树上u,v之间的边权和

树是满二叉树,用map存点到其父亲的边权值,对于操作一,当u!=v时我们先更新深度最大的点到其父亲的边权值,再令其等于其父亲,不断循环直至u=v。

操作二与操作一类似

#include<iostream>
#include<algorithm>
#include<map>
#include<cmath>
using namespace std;
#define ll long long
map<ll,ll>mp;
void update(ll x,ll y,ll val)
{
while(x!=y)
{
if(log(y)>log(x))swap(x,y);
mp[x]+=val;
x>>=;
}
}
ll query(ll x,ll y)
{
ll ret=;
while(x!=y)
{
if(log(y)>log(x))swap(x,y);
ret+=mp[x];
x>>=;
}
return ret;
}
int main()
{
mp.clear();
ll q,op,u,v,w;
cin>>q;
while(q--)
{
cin>>op;
if(op==)
{
cin>>v>>u>>w;
update(v,u,w);
}
else
{
cin>>v>>u;
cout<<query(v,u)<<endl;
}
}
return ;
}

cf 697C Lorenzo Von Matterhorn 思维的更多相关文章

  1. CF 696 A Lorenzo Von Matterhorn(二叉树,map)

    原题链接:http://codeforces.com/contest/696/problem/A 原题描述: Lorenzo Von Matterhorn   Barney lives in NYC. ...

  2. Lorenzo Von Matterhorn

    Lorenzo Von Matterhorn Barney lives in NYC. NYC has infinite number of intersections numbered with p ...

  3. C. Lorenzo Von Matterhorn LCA

    C. Lorenzo Von Matterhorn time limit per test 1 second memory limit per test 256 megabytes input sta ...

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

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

    http://codeforces.com/contest/697/problem/C C. Lorenzo Von Matterhorn time limit per test 1 second m ...

  8. A. Lorenzo Von Matterhorn

    A. Lorenzo Von Matterhorn time limit per test 1 second memory limit per test 256 megabytes input sta ...

  9. 【CodeForces 697C】Lorenzo Von Matterhorn(LCA)

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

随机推荐

  1. 2018-2-13-wpf-使用-Dispatcher.Invoke-冻结窗口

    title author date CreateTime categories wpf 使用 Dispatcher.Invoke 冻结窗口 lindexi 2018-2-13 17:23:3 +080 ...

  2. 学习vue就是那么简单,一个简单的案例

    vue是前端兴起的一个javascript库,相信大家都使用过jQuery,虽然vue和jQuery没有可比性,但从熟悉的角度去理解新的东西或许会容易接受一些,有时候由于思想和模式的转变会带来阵痛,但 ...

  3. 2019-4-10-win10-uwp-自定义标记扩展

    title author date CreateTime categories win10 uwp 自定义标记扩展 lindexi 2019-04-10 09:46:13 +0800 2019-04- ...

  4. linux 位操作

    atomic_t 类型在进行整数算术时是不错的. 但是, 它无法工作的好, 当你需要以原子方 式操作单个位时. 为此, 内核提供了一套函数来原子地修改或测试单个位. 因为整个操作 在单步内发生, 没有 ...

  5. vue在html中写动态背景图片

    <div class="img" :style="`background: url(`+item.img+'?any_string_is_ok'+`)center ...

  6. Linux USB 和 sysfs

    由于单个 USB 物理设备的复杂性, 设备在 sysfs 中的表示也非常复杂. 物理 USB 设备 (通过 struct usb_device 表示)和单个 USB 接口(由 struct usb_i ...

  7. Java 学习笔记(4)——java 常见类

    上次提前说了java中的面向对象,主要是为了使用这些常见类做打算,毕竟Java中一切都是对象,要使用一些系统提供的功能必须得通过类对象调用方法.其实Java相比于C来说强大的另一个原因是Java中提供 ...

  8. git之github推送篇

    1.创建项目  2.生成ssh密钥并设置到github 在文件夹里面右键打开git命令行,输入下面命令,然后一直回车. ssh-keygen -t rsa 生成位置在当前用户的.ssh文件夹里,带pu ...

  9. 分布式大牛详解Zookeeper底层原理

    很多学员都在反馈,说zk很难学,学的不是很明白,在这里,我继续带着大家详解一遍Zookeeper 首先zk是什么呢首先肯定是一个个分布式服务框架,是Apache Hadoop 的一个子项目,它主要是用 ...

  10. 最长无重复子串问题 leetcode 3

    一.代码及注释 class Solution { public: int lengthOfLongestSubstring(string s) { int n = s.size(); //字符串的长度 ...