CodeForces 696 A. Lorenzo Von Matterhorn(水~)
2016-08-01 18:24
357 查看
Description
一个完全二叉树,i的左右儿子为2*i和2*i+1,1为根节点,两种操作,第一种操作是给u到v的最短路径上的所有边权加上w,第二种操作是查询u到v的最短路径上各边边权和,初始各边权为0
Input
第一行一整数q表示操作数,之后q行每行表示一个操作,首先输入操作类型(1 or 2),对于1操作输入u,v,w,对于2操作输入u,v
(1<=q<=1000,1<=u,v<=10^18,u!=v,1<=w<=10^9)
Output
对于2操作,输出查询结果
Sample 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
Sample Output
94
0
32
Solution
简单题,因为查询数不超过1000,所以被操作的边不会超过1000*log(10^18)=60000条,开一个map记录每个出现过的边的边权即可
Code
一个完全二叉树,i的左右儿子为2*i和2*i+1,1为根节点,两种操作,第一种操作是给u到v的最短路径上的所有边权加上w,第二种操作是查询u到v的最短路径上各边边权和,初始各边权为0
Input
第一行一整数q表示操作数,之后q行每行表示一个操作,首先输入操作类型(1 or 2),对于1操作输入u,v,w,对于2操作输入u,v
(1<=q<=1000,1<=u,v<=10^18,u!=v,1<=w<=10^9)
Output
对于2操作,输出查询结果
Sample 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
Sample Output
94
0
32
Solution
简单题,因为查询数不超过1000,所以被操作的边不会超过1000*log(10^18)=60000条,开一个map记录每个出现过的边的边权即可
Code
#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #include<map> using namespace std; typedef long long ll; map<ll,ll>m; int main() { int q,op; ll u,v,w; while(~scanf("%d",&q)) { m.clear(); while(q--) { scanf("%d%I64d%I64d",&op,&u,&v); if(op==1) { scanf("%I64d",&w); while(u!=v) { if(u<v)swap(u,v); m[u]+=w; u/=2; } } else { ll ans=0; if(u!=v) { while(u!=v) { if(u<v)swap(u,v); ans+=m[u]; u/=2; } } printf("%I64d\n",ans); } } } return 0; }
相关文章推荐
- CodeForces 696A(Lorenzo Von Matterhorn ) & CodeForces 696B(Puzzles )
- 【CodeForces 697C】Lorenzo Von Matterhorn(LCA)
- CodeForces 697C——Lorenzo Von Matterhorn(二叉树,map)
- codeforces 696 A. Lorenzo Von Matterhorn
- codeforces #362(Div.2)C.Lorenzo Von Matterhorn【最近公祖先相关】
- Pineapple Incident,Barnicle,Lorenzo Von Matterhorn(cf#362)
- [CF697C]Lorenzo Von Matterhorn(LCA,思路好题)
- Codeforces Round #362 (Div. 2) C. Lorenzo Von Matterhorn LCA(最近公共祖先)
- Codeforces Round #362 (Div. 1) A (696A) Lorenzo Von Matterhorn (LCA)
- Codeforces Round #362 (Div. 2) C. Lorenzo Von Matterhorn(LCA思想)
- codeforces #362(div2)-> C. Lorenzo Von Matterhorn (map的使用)
- Codeforces Round #362 (Div. 1) A. Lorenzo Von Matterhorn(map&LCA)
- Codeforces Round #362 (Div. 2)C. Lorenzo Von Matterhorn 二叉树结点规律
- Codeforces Round #362 (Div. 2) C. Lorenzo Von Matterhorn (map离散化)
- 解题报告:Codeforces Round #362 (Div. 1) A. Lorenzo Von Matterhorn 树状概率DP
- Codeforces Round #362 (Div. 2) C. Lorenzo Von Matterhorn(暴力)
- 【Codeforces Round 362 (Div 2)C】【STL-map 最近公共祖先思想】Lorenzo Von Matterhorn 数域二叉树的路径权值变更查询
- Codeforces Round #362 (Div. 2) C. Lorenzo Von Matterhorn (类似LCA)
- codeforces 696A A. Lorenzo Von Matterhorn(水题)
- codeforces 697C Lorenzo Von Matterhorn(二叉树LCA)