【STL】【模拟】Codeforces 696A Lorenzo Von Matterhorn

 

题目链接:

  http://codeforces.com/problemset/problem/696/A

题目大意:

  一个满二叉树,深度无限,节点顺序编号,k的儿子是k+k和k+k+1,一开始树上的边权都为0

  N(N<=1000)个操作,操作两种,1是从u到v的路径上的所有边权+w,2是求u到v的边权和。(1 ≤ v, u ≤ 1018, v ≠ u, 1 ≤ w ≤ 109)

题目思路:

  【STL】【模拟】

  用map写很快,第一次用很生疏。现学只看了一点点。

  因为是满二叉树所以直接暴力求LCA和求解,把每条边全加上w,因为数很大 所以用map映射查找修改。

 

 1 //
 2 //by coolxxx
 3 ////<bits/stdc++.h>
 4 #include<iostream>
 5 #include<algorithm>
 6 #include<string>
 7 #include<iomanip>
 8 #include<map>
 9 #include<memory.h>
10 #include<time.h>
11 #include<stdio.h>
12 #include<stdlib.h>
13 #include<string.h>
14 //#include<stdbool.h>
15 #include<math.h>
16 #define min(a,b) ((a)<(b)?(a):(b))
17 #define max(a,b) ((a)>(b)?(a):(b))
18 #define abs(a) ((a)>0?(a):(-(a)))
19 #define lowbit(a) (a&(-a))
20 #define sqr(a) ((a)*(a))
21 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
22 #define mem(a,b) memset(a,b,sizeof(a))
23 #define eps (1e-8)
24 #define J 10
25 #define MAX 0x7f7f7f7f
26 #define PI 3.14159265358979323
27 #define N 1004
28 using namespace std;
29 typedef long long LL;
30 int cas,cass;
31 int n,m,lll,ans;
32 map<LL,LL>t;
33 int main()
34 {
35     #ifndef ONLINE_JUDGE
36     freopen("1.txt","r",stdin);
37 //    freopen("2.txt","w",stdout);
38     #endif
39     int i,j;
40     LL x,y,z;
41 //    for(scanf("%d",&cas);cas;cas--)
42 //    for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
43 //    while(~scanf("%s",s))
44     while(~scanf("%d",&n))
45     {
46         for(i=1;i<=n;i++)
47         {
48             scanf("%d",&j);j--;
49             if(!j)
50             {
51                 scanf("%I64d%I64d%I64d",&x,&y,&z);
52                 while(x!=y)
53                 {
54                     if(x>y)
55                         t[x]+=z,x>>=1;
56                     else
57                         t[y]+=z,y>>=1;
58                 }
59             }
60             else
61             {
62                 scanf("%I64d%I64d",&x,&y);
63                 z=0;
64                 while(x!=y)
65                 {
66                     if(x>y)z+=t[x],x>>=1;
67                     else z+=t[y],y>>=1;
68                 }
69                 printf("%I64d\n",z);
70             }
71         }
72     }
73     return 0;
74 }
75 /*
76 //
77 
78 //
79 */
View Code

 

posted @ 2016-08-15 23:18  Cool639zhu  阅读(225)  评论(0编辑  收藏  举报