博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
codeforces 696A A. Lorenzo Von Matterhorn(水题)
阅读量:5243 次
发布时间:2019-06-14

本文共 3479 字,大约阅读时间需要 11 分钟。

题目链接:

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

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
题意:
 
i与2*i和2*i+1相连的一个图,两种操作,一种是在u,v的道路上的每一条边权值加w,一种是询问u,v之间道路上的边权值和;
 
思路:
 
可以直接暴力,可以找出最近功公共祖先,然后找这两个点与公共祖先之间把边的权值加上;
 
AC代码:
#include 
#include
#include
#include
#include
#include
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
void read(T&num) { char CH; bool F=false; for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar()); for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar()); F && (num=-num);}int stk[70], tp;template
inline void print(T p) { if(!p) { puts("0"); return; } while(p) stk[++ tp] = p%10, p/=10; while(tp) putchar(stk[tp--] + '0'); putchar('\n');}const LL mod=1e9+7;const double PI=acos(-1.0);const LL inf=1e18;const int N=3e6+10;const int maxn=3e6;const double eps=1e-10;int q;map
mp,vis;LL getfa(LL x,LL y){ vis.clear(); while(x)vis[x]=1,x>>=1; while(1) { if(vis[y])return y; y>>=1; }}int main(){ read(q); while (q--) { int f; LL u,v,w; read(f); if(f==1) { read(u);read(v);read(w); LL fa=getfa(u,v); while(u!=fa) { mp[u]=mp[u]+w; u>>=1; } while(v!=fa) { mp[v]=mp[v]+w; v>>=1; } } else { read(u);read(v); LL fa=getfa(u,v),ans=0; while(u!=fa) { ans+=mp[u]; u>>=1; } while(v!=fa) { ans+=mp[v]; v>>=1; } print(ans); } } return 0;}

 

转载于:https://www.cnblogs.com/zhangchengc919/p/5674271.html

你可能感兴趣的文章
Docker——error pulling image configuration
查看>>
一条简单的 SQL 执行超过 1000ms,纳尼?
查看>>
Python函数(一)之杵臼之交
查看>>
关于将qt作为max插件ui库所遇到的困难
查看>>
如何设置映射网络驱动器的具体步骤和方法
查看>>
ASP.NET WebApi 基于OAuth2.0实现Token签名认证
查看>>
SendMail与Postfix的架构备忘
查看>>
paip.mysql 性能测试 报告 home right
查看>>
Atitit.跨平台预定义函数 魔术方法 魔术函数 钩子函数 api兼容性草案 v2 q216 java c# php js.docx...
查看>>
283. Move Zeroes把零放在最后面
查看>>
我的函数说明风格
查看>>
ssh 简介
查看>>
26.无向网邻接表类
查看>>
Visual Studio Code 打开.py代码报Linter pylint is not installed解决办法
查看>>
洛谷 p1352 没有上司的舞会 题解
查看>>
Python 数据类型
查看>>
Task 与 Activity
查看>>
Google Guava学习笔记——简介
查看>>
历时八年,HTML5 标准终于完工了
查看>>
17.树的子结构
查看>>