题目链接: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. openmp的g++并行执行

    #include <omp.h>#include <stdio.h>#include <stdlib.h>void Test(int n) {    for(int ...

  2. Dbvisualizer各种中文乱码问题解决方法

    1.SQL Commander里中文显示成‘口’ 解:进入Tools -> Tool Properties -> General -> Appearence -> Fonts, ...

  3. 不幸的是,我试图在Eclipse中安装Maven 1.5,但出现了以下错误:

    Cannot complete the install because one or more required items could not be found. Software being in ...

  4. linux包之下载curl-wget

    [root@localhost ~]# rpm -qa|grep curllibcurl-7.19.7-37.el6_4.x86_64python-pycurl-7.19.0-8.el6.x86_64 ...

  5. 使用eclipse运行maven web项目 插件/非插件

    一.使用插件 tomcat 8.5 tomcat-users.xml中添加这一行就ok <user username="admin" password="admin ...

  6. 【Jenkins】pipeline-hello-world项目

    1.New Item 2.Pipeline Definition 3.Build Error 4.Solution 5.Console Output

  7. python生成器yield和send

    创建生成器 #第一种创建生成器的方法 #使用()创建生成器,如果使用[]则创建列表 a = (x**2 for x in range(1, 5)) #可以通过next一直产生新的数据,直到最后一个报异 ...

  8. monorepo仓库管理方式探秘

    前言 随着功能和业务量级的飙升,前端代码量级也越来越大,管理运维的成本也进一步增加. 代码仓库的运营管理挑战也浮出水面. 主流方案有两种:一是multirepo式的分散式的独立仓库,二是monorep ...

  9. 数据导出至excle

    ASP.NET MVC导出Excel 首先下载  NPOI.dll 引用到项目中 建议下载地址:http://download.csdn.net/detail/pukuimin1226/5851747 ...

  10. 洛谷$P4249\ [WC2007]$剪刀石头布 网络流

    正解:网络流 解题报告: 传送门$QwQ$ 题目大意其实就说有一个$n$个节点的有向完全图,然后部分边的方向已经给定了,要求确定所有边的方向使三元环数目有$max$.这里三元环的定义是说三条边的方向一 ...