Codeforces Round #362 (Div. 2) C. Lorenzo Von Matterhorn (类似LCA)
题目链接: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)的更多相关文章
- #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 ...
- 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 ...
- Codeforces Round #362 (Div. 2)
闲来无事一套CF啊,我觉得这几个题还是有套路的,但是很明显,这个题并不难 A. Pineapple Incident time limit per test 1 second memory limit ...
- 【转载】【树形DP】【数学期望】Codeforces Round #362 (Div. 2) D.Puzzles
期望计算的套路: 1.定义:算出所有测试值的和,除以测试次数. 2.定义:算出所有值出现的概率与其乘积之和. 3.用前一步的期望,加上两者的期望距离,递推出来. 题意: 一个树,dfs遍历子树的顺序是 ...
- 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 ...
- 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 ...
- Codeforces Round #362 (Div. 2) B 模拟
B. Barnicle time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...
- Codeforces Round #362 (Div. 2) A 水也挂
A. Pineapple Incident time limit per test 1 second memory limit per test 256 megabytes input standar ...
- 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 ...
随机推荐
- uva live 6170
Esspe-Peasee Esspe-Peasee is an ancient game played by children throughout the land of Acmania. The ...
- EASYUI+MVC4通用权限管理平台
通用权限案例平台在经过几年的实际项目使用,并取得了不错的用户好评.在平台开发完成后,特抽空总结一下平台知识,请各位在以后的时间里,关注博客的更新. 1.EASYUI+MVC4通用权限管理平台--前言 ...
- websocket webworker
对我来说最快的学习途径是实践,所以找两个东西来练手.一个是websocket一个是webwoker,今天先说第一个. 要理解socket就要先理解http和tcp的区别,简单说就是一个是短链,一个是长 ...
- 安装IIS之后运行aspx 显示“服务器应用程序不可用” 解决办法
引起这个的原因大概是现安装了.Net Framework,后装的IIS导致.Net没有在IIS里注册. 另外,还有可能是ASPNET账户没有IIS所指定服务器目录的权限.在资源管理器中找到“工具-文 ...
- js学习总结
转自 http://blog.sina.com.cn/s/blog_75cf5f3201011csu.html 一: 关于基本数据类型在栈内存和堆内存中的关系 基本数据对于栈内存和堆内存是可以复制的, ...
- 极光推送使用实例(二) Android客户端
上一篇简单介绍了极光推送在Java服务端的实现,如果感兴趣的可以看一下极光推送使用实例(一)JAVA服务端.这篇文章介绍下极光推送在Android客户端的实现. JPush Android SDK 是 ...
- Java [Leetcode 112]Path Sum
题目描述: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding ...
- jquery生成二维码
下载Demo js下载: jquery-1.8.3.js .qrcode.js.jquery.qrcode.js <!DOCTYPE html> <html> <head ...
- SharePoint 2010中列表Add和Delete事件解析
转:http://winsystem.ctocio.com.cn/26/11400026_2.shtml [IT专家网独家撰稿]SharePoint 2010与以前的版本相比,天翻地覆的变化并不为过. ...
- MySQL压测中遇到的一些问题
批量insert http://blog.csdn.net/xiaoxian8023/article/details/20155429 Mysql jdbc 批处理数据,需要给jdbc连接加上rewr ...