首页 > 其他 > 详细

codeforces 696A Lorenzo Von Matterhorn 水题

时间:2016-07-16 00:41:27      阅读:130      评论:0      收藏:0      [点我收藏+]

这题一眼看就是水题,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

 

codeforces 696A Lorenzo Von Matterhorn 水题

原文:http://www.cnblogs.com/shuguangzw/p/5674869.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!