codeforces 696A Lorenzo Von Matterhorn 水题

这题一眼看就是水题,map随便计

然后我之所以发这个题解,是因为我用了log2()这个函数判断在哪一层

我只能说我真是太傻逼了,这个函数以前听人说有精度问题,还慢,为了图快用的,没想到被坑惨了,以后尽量不用

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
typedef long long LL;
const int N  = 3e6;
const LL mod = 1e9+7;
map<LL,LL>mp;
int get(LL x){
  for(int i=0;;++i){
    LL tmp1=(1ll<<i);
    LL tmp2=(1ll<<(i+1))-1;
    if(x>=tmp1&&x<=tmp2)return i;
  }
}
int main(){
  int q;
  scanf("%d",&q);
  while(q--){
    LL u,v,w;
    int op;
    scanf("%d%I64d%I64d",&op,&u,&v);
    int curu=get(u),curv=get(v);
    if(curu<curv){swap(u,v);swap(curu,curv);}
    if(op==1){
      scanf("%I64d",&w);
      while(curu>curv){
        if(mp.find(u)==mp.end())mp[u]=w;
        else mp[u]+=w;
        u>>=1;--curu;
      }
      while(u!=v){
        if(mp.find(u)==mp.end())mp[u]=w;
        else mp[u]+=w;
        if(mp.find(v)==mp.end())mp[v]=w;
        else mp[v]+=w;
        u>>=1;v>>=1;
      }
    }
    else{
       w=0;
       while(curu>curv){
        if(mp.find(u)!=mp.end())w+=mp[u];
        --curu;u>>=1;
       }
       while(u!=v){
        if(mp.find(u)!=mp.end())w+=mp[u];
        if(mp.find(v)!=mp.end())w+=mp[v];
        u>>=1;v>>=1;
       }
       printf("%I64d\n",w);  
    }
  }
  return 0;
}
View Code

 

posted @ 2016-07-15 21:52  shuguangzw  阅读(129)  评论(0编辑  收藏  举报