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

题目链接:点这里!!!!


题意:

给你一颗满二叉树,i节点到2*i节点存在一条费用为0的双向边,i节点到2*i+1节点存在一条费用为0的双向边(节点数为10^18)。

给你q(q<=1000)个操作。操作类型分为2种:

1 u v w u到v的最短路径上,每条边的费用加上w。

2 u v     询问u到v的最短路径。


题解:

1、点数为10^18的满二叉树的深度为63,u->v经过最多63*2-1个点,q<=1000,所以我最多操作了125*1000个点,直接暴力搞就可以了。

2、先找到了u,v的最近公共祖先p(实现方法根求基本LCA差不多),然后对u->p和v->p这两条路操作就可以了。


代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<vector>
#include<bitset>
#include<set>
#include<queue>
#include <cassert>
#include<stack>
#include<map>
#include<cstdlib>
#include<cmath>
#define LL long long
#define pb push_back
#define pa pair<int,int>
#define clr(a,b) memset(a,b,sizeof(a))
#define lson lr<<1,l,mid
#define rson lr<<1|1,mid+1,r
#define bug(x) printf("%d++++++++++++++++++++%d\n",x,x)
#define key_value ch[ch[root][1]][0]
#pragma comment(linker, "/STACK:102400000000,102400000000")
const LL  MOD = 1000000007;
const int N = 1e6+15;
const int maxn = 8e3+15;
const int letter = 130;
const LL INF = 1e18;
const double pi=acos(-1.0);
const double eps=1e-10;
using namespace std;
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int q;
map<LL,LL>mp;
LL bin[100];
LL fat(LL a,LL b){
    int x=lower_bound(bin+1,bin+63,a)-bin;
    int y=lower_bound(bin+1,bin+63,b)-bin;
    if(x<y) swap(a,b),swap(x,y);
    int num=x-y;
    while(num--){
        a=a/2;
    }
    if(a==b) return a;
    while(a!=b){
        a/=2,b/=2;
    }
    return a;
}
int main(){

    bin[1]=1;
    for(int i=2;i<=63;i++) bin[i]=bin[i-1]*2ll;
    for(int i=1;i<=63;i++) bin[i]+=bin[i-1];
    int id;
    LL x,y,z;
    scanf("%d",&q);
    mp.clear();
    while(q--){
        scanf("%d",&id);
        if(id==1) {
            scanf("%I64d%I64d%I64d",&x,&y,&z);
            LL vs=fat(x,y);
            while(x!=vs){mp[x]+=z,x/=2;}
            while(y!=vs){mp[y]+=z,y/=2;}
        }
        else {
            scanf("%I64d%I64d",&x,&y);
            LL ans=0;
            LL vs=fat(x,y);
            while(x!=vs){ans+=mp[x],x/=2;}
            while(y!=vs){ans+=mp[y],y/=2;}
            printf("%I64d\n",ans);
        }
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值