Codeforces Round #362 (Div. 2) C. Lorenzo Von Matterhorn

C. Lorenzo Von Matterhorn
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 and2i + 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 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.

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
Note

In the example testcase:

Here are the intersections used:

  1. Intersections on the path are 312 and 4.
  2. Intersections on the path are 42 and 1.
  3. Intersections on the path are only 3 and 6.
  4. Intersections on the path are 421 and 3. Passing fee of roads on the path are 3232 and 30 in order. So answer equals to 32 + 32 + 30 = 94.
  5. Intersections on the path are 63 and 1.
  6. Intersections on the path are 3 and 7. Passing fee of the road between them is 0.
  7. Intersections on the path are 2 and 4. Passing fee of the road between them is 32 (increased by 30in the first event and by 2 in the second)

分析:
题目描述了一棵结构简单的树(每个节点o的左右儿子分别是o*2,o*2+1)。
不妨把边的权值记录在终点上,容易发现U->V的最短路就是先走到最近公共祖先。
由于特殊的建树方式,LCA很好求,假设x,y,在树的同一层,容易发现LCA就是x,y一直除以2直到相等的值,当然,如果不在同一层,就调整到同一层。
对操作一,只需要将沿途点(x>>=1 ,直到x==lca(x,y),这些x就是x到LCA(x,y)经过的点)的权重加W;
对操作二,只需要输出沿途点权重之和即可。
问题是,x,y,范围太大,空间复杂度过高。利用类似线段树动态开点的方法可以解决这个问题,空间复杂度O(q*log n )。
因为题目中的q(操作次数)很少,所以可以做到。
代码如下:
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<queue>
#include<vector>
#include<algorithm>
#define LL long long
#define CLEAR(xxx) memset(xxx,0,sizeof(xxx))
using namespace std;
const LL maxn=5000000+5,inf=1000000000000000000+5;
LL q;
inline void _read(LL &x){
	char ch=getchar(); bool mark=false;
	for(;!isdigit(ch);ch=getchar())if(ch=='-')mark=true;
	for(x=0;isdigit(ch);ch=getchar())x=x*10+ch-'0';
	if(mark)x=-x;
}

struct node{
	LL ls,rs,v;
}tree[maxn];
LL tot=1,rt=1;

LL LCA(LL x,LL y){                
	if(y<x) swap(x,y);
	LL k=0;
	while((1LL<<k)<=x)k++;
	while(y>=(1LL<<k))y>>=1;   //调整到同一层 
	while(x!=y)x>>=1,y>>=1;
	return x;
}

LL getid(LL &o,LL L,LL R ,LL x){  //找到代表x这个数的节点编号,如果没有,建立沿途的点 
	if(!o)o=++tot;
	if(L==R) return o;
	LL mid=(L+R)>>1LL;
	if(x<=mid) return getid(tree[o].ls,L,mid,x);
	else return getid(tree[o].rs,mid+1,R,x);
}

void Add(LL x,LL y,LL w){
	LL lca=LCA(x,y);
	while(x!=lca){
		tree[getid(rt,1,inf,x)].v+=w;
		x>>=1LL;
	}
	while(y!=lca){
		tree[getid(rt,1,inf,y)].v+=w;
		y>>=1LL;
	}
}
LL query(LL x,LL y){
	LL lca=LCA(x,y),ans=0;
	while(x!=lca){
		ans+=tree[getid(rt,1,inf,x)].v;
		x>>=1LL;
	}
	while(y!=lca){
		ans+=tree[getid(rt,1,inf,y)].v;
		y>>=1LL;
	}
	return ans;
}
int main(){
	//freopen("data.in","r",stdin);
	//freopen("myans.out","w",stdout);
	LL u,v,w;
	_read(q);
	while(q--){
		_read(w); _read(u); _read(v);
		if(w==1){
			_read(w);
			Add(u,v,w);
		}
		else printf("%I64d\n",query(u,v));
	}
	return 0;
}
/*
*/
当然,如果想偷懒的话,用map也是极好的
代码如下:
#include<cstdio>
#include<map>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define LL long long
using namespace std;
inline void _read(LL &x){
	char ch=getchar(); bool mark=false;
	for(;!isdigit(ch);ch=getchar())if(ch=='-')mark=true;
	for(x=0;isdigit(ch);ch=getchar())x=x*10+ch-'0';
	if(mark)x=-x;
}

LL n;
map<LL,LL>mp;

int main(){
	_read(n);
	LL opt, x, y, v;
	for(int i=1;i<=n;i++){
		_read(opt); _read(x); _read(y);
		if (opt==1){
			_read(v);
			while(x != y){
				if (y>x) swap(x, y);
				mp[x]+=v;
				x >>= 1;
			}
		}
		else{
			LL ans=0;
			while(x!=y){
				if (y>x) swap(x, y);
				ans+=mp[x];
				x>>=1;
			}
			cout<<ans<<endl;
		}
	}
	return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值