题目链接:http://codeforces.com/problemset/problem/697/D

给你一个有规则的二叉树,大概有1e18个点。

有两种操作:1操作是将u到v上的路径加上w,2操作是求u到v上的路径和。

我们可以看出任意一个点到1节点的边个数不会超过64(差不多就是log2(1e18)),所以可以找最近相同祖节点的方式写。

用一条边的一个唯一的端点作为边的编号(比如1到2,那2就为这条边的编号),由于数很大,所以用map来存。

进行1操作的时候就是暴力加w至u到LCA(u,v)上的各个边,同样加w至v到LCA(u , v)上的各个边。同理,进行2操作的时候也是暴力求和。

下面这是简单的写法

 #include <bits/stdc++.h>
using namespace std;
typedef __int64 LL;
map <LL , LL> cnt; int main()
{
int n;
LL choose , u , v , val;
scanf("%d" , &n);
while(n--) {
scanf("%lld" , &choose);
if(choose == ) {
scanf("%lld %lld %lld" , &u , &v , &val);
while(u != v) {
if(u > v) {
cnt[u] += val;
u /= ;
}
else {
cnt[v] += val;
v /= ;
}
}
}
else {
scanf("%lld %lld" , &u , &v);
LL res = ;
while(u != v) {
if(u > v) {
res += cnt[u];
u /= ;
}
else {
res += cnt[v];
v /= ;
}
}
printf("%lld\n" , res);
}
}
return ;
}

下面是我一开始的写法,也A了,不过有点麻烦,可以不看。

 #include <bits/stdc++.h>
using namespace std;
typedef __int64 LL;
const int N = 1e3 + ;
vector <int> vc;
vector <LL> q1[N] , q2[N] , ans1 , ans2;
LL x[N] , y[N] , val[N];
int main()
{
int n;
LL choose , u , v;
scanf("%d" , &n);
for(int i = ; i <= n ; ++i) {
scanf("%lld" , &choose);
if(choose == ) {
scanf("%lld %lld %lld" , x + i , y + i , val + i);
vc.push_back(i);
LL num1 = x[i] , num2 = y[i];
q1[i].push_back(num1) , q2[i].push_back(num2);
while(num1 != num2) {
if(num1 > num2) {
num1 /= ;
q1[i].push_back(num1);
}
else {
num2 /= ;
q2[i].push_back(num2);
}
}
}
else {
scanf("%lld %lld" , &u , &v);
LL temp1 = u , temp2 = v , res = ;
ans1.clear() , ans2.clear();
ans1.push_back(temp1) , ans2.push_back(temp2);
while(temp1 != temp2) {
if(temp1 > temp2) {
temp1 /= ;
ans1.push_back(temp1);
}
else {
temp2 /= ;
ans2.push_back(temp2);
}
}
for(int j = ; j < vc.size() ; ++j) {
int l = , r = ans1.size() - , k = ;
while(l + <= r) {
for( ; k + < q1[vc[j]].size() ; ++k) {
if(ans1[l] > q1[vc[j]][k])
break;
if(ans1[l + ] == q1[vc[j]][k + ] && ans1[l] == q1[vc[j]][k]) {
res += val[vc[j]];
}
}
l++;
}
k = , l = , r = ans1.size() - ;
while(l + <= r) {
for( ; k + < q2[vc[j]].size() ; ++k) {
if(ans1[l] > q2[vc[j]][k])
break;
if(ans1[l + ] == q2[vc[j]][k + ] && ans1[l] == q2[vc[j]][k]) {
res += val[vc[j]];
}
}
l++;
} l = , r = ans2.size() - , k = ;
while(l + <= r) {
for( ; k + < q1[vc[j]].size() ; ++k) {
if(ans2[l] > q1[vc[j]][k])
break;
if(ans2[l + ] == q1[vc[j]][k + ] && ans2[l] == q1[vc[j]][k]) {
res += val[vc[j]];
}
}
l++;
}
k = , l = , r = ans2.size() - ;
while(l + <= r) {
for( ; k + < q2[vc[j]].size() ; ++k) {
if(ans2[l] > q2[vc[j]][k])
break;
if(ans2[l + ] == q2[vc[j]][k + ] && ans2[l] == q2[vc[j]][k]) {
res += val[vc[j]];
}
}
l++;
}
}
printf("%lld\n" , res);
}
}
return ;
}

Codeforces Round #362 (Div. 2) C. Lorenzo Von Matterhorn (类似LCA)的更多相关文章

  1. #map+LCA# Codeforces Round #362 (Div. 2)-C. Lorenzo Von Matterhorn

    2018-03-16 http://codeforces.com/problemset/problem/697/C C. Lorenzo Von Matterhorn time limit per t ...

  2. Codeforces Round #362 (Div. 2) A.B.C

    A. Pineapple Incident time limit per test 1 second memory limit per test 256 megabytes input standar ...

  3. Codeforces Round #362 (Div. 2)

    闲来无事一套CF啊,我觉得这几个题还是有套路的,但是很明显,这个题并不难 A. Pineapple Incident time limit per test 1 second memory limit ...

  4. 【转载】【树形DP】【数学期望】Codeforces Round #362 (Div. 2) D.Puzzles

    期望计算的套路: 1.定义:算出所有测试值的和,除以测试次数. 2.定义:算出所有值出现的概率与其乘积之和. 3.用前一步的期望,加上两者的期望距离,递推出来. 题意: 一个树,dfs遍历子树的顺序是 ...

  5. Codeforces Round #362 (Div. 2)->B. Barnicle

    B. Barnicle time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...

  6. Codeforces Round #362 (Div. 2)->A. Pineapple Incident

    A. Pineapple Incident time limit per test 1 second memory limit per test 256 megabytes input standar ...

  7. Codeforces Round #362 (Div. 2) B 模拟

    B. Barnicle time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...

  8. Codeforces Round #362 (Div. 2) A 水也挂

    A. Pineapple Incident time limit per test 1 second memory limit per test 256 megabytes input standar ...

  9. Codeforces Round #362 (Div. 2) D. Puzzles

    D. Puzzles time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

随机推荐

  1. Hbase总结(一)-hbase命令,hbase安装,与Hive的区别,与传统数据库的区别,Hbase数据模型

    Hbase总结(一)-hbase命令 下面我们看看HBase Shell的一些基本操作命令,我列出了几个常用的HBase Shell命令,如下: 名称 命令表达式 创建表 create '表名称', ...

  2. struts.custom.i18n.resources 如何配置多个资源文件?

    struts.custom.i18n.resources = resources1,resources2,resources3   配置properties文件

  3. 1493: [NOI2007]项链工厂

    线段树. 真还就是个线段树.. 除去操作1,2的话,线段树很容易就处理了,问题在于如何处理操作1和2.(这点没想到).. 我们用一个delta维护操作1,如果没有旋转就+k,不然就-k. 每次读入i和 ...

  4. ffmpeg开发指南

    FFmpeg是一个集录制.转换.音/视频编码解码功能为一体的完整的开源解决方案.FFmpeg的开发是基于Linux操作系统,但是可以在大多数操作系统中编译和使用.FFmpeg支持MPEG.DivX.M ...

  5. 《C++ Primer 4th》读书笔记 第8章-标准IO库

    原创文章,转载请注明出处:http://www.cnblogs.com/DayByDay/p/3936457.html

  6. validator的验证

    通常喜欢这么写验证 <form method="post" data-ajax="false" action="/Shppping/PlaceO ...

  7. String.IsNullOrEmpty 方法

    参数 value:一个String引用 返回值 如果 value 参数为 空引用(在 Visual Basic 中为 Nothing) 或空字符串 (""),则为 true:否则为 ...

  8. PL/Sql 中创建、调试、调用存储过程

    存储过程的详细建立方法 1.先建存储过程 左边的浏览窗口选择 procedures ,会列出所有的存储过程,右击文件夹procedures单击菜单"new",弹出 template ...

  9. RTNETLINK answers: File exists错误

    解决ssh连接虚拟机出错,RTNETLINK answers: File exists 解决步骤如下: 使用ssh连接虚拟机的时候,发现目标主机无法连接,登录虚拟机,查看ssh监听是否开启: 发现监听 ...

  10. python多线程ctrl-c退出问题

    场景: 经常会遇到下述问题:很多io busy的应用采取多线程的方式来解决,但这时候会发现python命令行不响应ctrl-c 了,而对应的java代码则没有问题: public class Test ...