codeforces 696A A. Lorenzo Von Matterhorn(水题)
题目链接:
1 second
256 megabytes
standard input
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).
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 ≤ 10^18, v ≠ u, 1 ≤ w ≤ 10^9 states for every description line.
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.
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
94
0
32
#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<''||CH>'';F= CH=='-',CH=getchar());
for(num=;CH>=''&&CH<='';num=num*+CH-'',CH=getchar());
F && (num=-num);
}
int stk[], tp;
template<class T> inline void print(T p) {
if(!p) { puts(""); return; }
while(p) stk[++ tp] = p%, p/=;
while(tp) putchar(stk[tp--] + '');
putchar('\n');
} const LL mod=1e9+;
const double PI=acos(-1.0);
const LL inf=1e18;
const int N=3e6+;
const int maxn=3e6;
const double eps=1e-; int q;
map<LL,LL>mp,vis;
LL getfa(LL x,LL y)
{
vis.clear();
while(x)vis[x]=,x>>=;
while()
{
if(vis[y])return y;
y>>=;
}
}
int main()
{
read(q);
while (q--)
{
int f;
LL u,v,w;
read(f);
if(f==)
{
read(u);read(v);read(w);
LL fa=getfa(u,v);
while(u!=fa)
{
mp[u]=mp[u]+w;
u>>=;
}
while(v!=fa)
{
mp[v]=mp[v]+w;
v>>=;
}
}
else
{
read(u);read(v);
LL fa=getfa(u,v),ans=;
while(u!=fa)
{
ans+=mp[u];
u>>=;
}
while(v!=fa)
{
ans+=mp[v];
v>>=;
}
print(ans);
}
}
return ;
}
codeforces 696A A. Lorenzo Von Matterhorn(水题)的更多相关文章
- CodeForces 696A:Lorenzo Von Matterhorn(map的用法)
http://codeforces.com/contest/697/problem/C C. Lorenzo Von Matterhorn time limit per test 1 second m ...
- codeforces 696A Lorenzo Von Matterhorn 水题
这题一眼看就是水题,map随便计 然后我之所以发这个题解,是因为我用了log2()这个函数判断在哪一层 我只能说我真是太傻逼了,这个函数以前听人说有精度问题,还慢,为了图快用的,没想到被坑惨了,以后尽 ...
- CodeForces 696A(Lorenzo Von Matterhorn ) & CodeForces 696B(Puzzles )
A,给一棵完全二叉树,第一个操作,给两个点,两点路径上的所有边权值都增加w,第二个操作,给两个点,求两点路径上的所有边权值和. 我看一眼题就觉得是树链剖分,而我又不会树链剖分,扔掉. 后来查了题解,首 ...
- 【CodeForces 697C】Lorenzo Von Matterhorn(LCA)
Least Common Ancestors 节点范围是1~1e18,至多1000次询问. 只要不断让深的节点退一层(>>1)就能到达LCA. 用点来存边权,用map储存节点和父亲连边的权 ...
- #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 ...
- CF 696 A Lorenzo Von Matterhorn(二叉树,map)
原题链接:http://codeforces.com/contest/696/problem/A 原题描述: Lorenzo Von Matterhorn Barney lives in NYC. ...
- Lorenzo Von Matterhorn
Lorenzo Von Matterhorn Barney lives in NYC. NYC has infinite number of intersections numbered with p ...
- C. Lorenzo Von Matterhorn LCA
C. Lorenzo Von Matterhorn time limit per test 1 second memory limit per test 256 megabytes input sta ...
- Lorenzo Von Matterhorn(STL_map的应用)
Lorenzo Von Matterhorn time limit per test 1 second memory limit per test 256 megabytes input standa ...
随机推荐
- Python入门--1--基本中的基本
一. 1.这是一个面向对面的编程,一种解释性语言. 2.缩进是python的灵魂,使代码变得非常简洁,正确使用冒号“:”,IDLE的 下一行会自动缩进 3.if语句中 python拒绝接受 ...
- 最小费用最大流粗解 poj2516
最小费用最大流,一般解法如下: 在流量基础上,每条边还有权费用,即单位流量下的所需费用.在最大流量下,求最小费用.解法:在最大流算法基础上,每次按可行流增广改为每次用spfa按最小费用(用单位费用)增 ...
- npm 安装vue-cli
TIP:win10下安装,使用管理员身份进行,否则会有权限限制. 1,安装完成node,node有自带的npm,可以直接在cmd中,找到nodeJs安装的路径下,进行命令行全局安装vue-cli.(n ...
- 洛谷——P1746 离开中山路
P1746 离开中山路 题目背景 <爱与愁的故事第三弹·shopping>最终章. 题目描述 爱与愁大神买完东西后,打算坐车离开中山路.现在爱与愁大神在x1,y1处,车站在x2,y2处.现 ...
- eclispe集成web插件
最近公司需要使用开源框架开发,所有下载了最新版本的eclispe工具,但是在官网下载的eclispe是不包含web插件的,无法创建web项目,需要自行集成web插件 eclipse官网下载地址:htt ...
- MySQL---笔记之视图的使用详解
什么是视图 视图是从一个或多个表中导出来的表,是一种虚拟存在的表. 视图就像一个窗口,通过这个窗口可以看到系统专门提供的数据. 这样,用户可以不用看到整个数据库中的数据,而之关心对自己有用的数据. ...
- 【转】Code Your Own PHP MVC Framework in 1 Hour
原文: https://www.codeproject.com/Articles/1080626/Code-Your-Own-PHP-MVC-Framework-in-Hour --------- ...
- 从头认识java-15.5 使用LinkedHashSet须要注意的地方
再接着上一个章节.我们来聊一下使用LinkedHashSet须要注意的地方. LinkedHashSet特点: (1)元素是有顺序的 (2)元素是不反复的 (3)底层数据结构是依照链表的结构存储的 ( ...
- Spring -- Bean自己主动装配&Bean之间关系&Bean的作用域
对于学习spring有帮助的站点:http://jinnianshilongnian.iteye.com/blog/1482071 Bean的自己主动装配 Spring IOC 容器能够自己主动装配 ...
- 走入asp.net mvc不归路:[6]linq常见用法
asp.net mvc结合linq,先不说性能问题,对于增删查改的操作还是相当方便的.以下我们就来介绍一下linq在asp.net mvc的Controller中的常见用法. 1 首先来看看整个数据表 ...