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 ...
随机推荐
- 【Todo】深入理解Javascript系列
真的很好,要看 http://www.cnblogs.com/TomXu/archive/2011/12/15/2288411.html
- bzoj2241: [SDOI2011]打地鼠
暴力. O(n^6)暴力卡过,72ms. 莫名其妙做这道题时感觉十分烦躁,难受,只能这样了. O(n^4)的方法是这样差分一下.判断的时候tmp=t[i][j],t[i][j]-=tmp,t[i+r] ...
- UVa 10868 (物理) Bungee Jumping
题意: 有个人在蹦极,给出悬崖的高度,绳子的长度,弹簧绳的胡克系数 以及 人的质量. 判断人是否能够着地,能的话是否能安全着地.所谓安全着地就是到达地面的速度不超过10m/s. 分析: 学过一点高中物 ...
- UVa 1262 (第k字典序) Password
题意: 给出两个6行5列的字母矩阵,一个密码满足:密码的第i个字母在两个字母矩阵的第i列均出现. 然后找出字典序为k的密码,如果不存在输出NO 分析: 我们先统计分别在每一列均在两个矩阵出现的字母,然 ...
- ASP.NET MVC ActionResult的实现
1.11种ActionResult 在System.Web.Mvc命名空间下: ActionResult是一个抽象类, 在Action中返回的都是其派生类.下面是我整理的ASP.NET MVC 1.0 ...
- OK335xS psplash make-image-header.sh hacking
/***************************************************************************** * OK335xS psplash mak ...
- 【C#学习笔记】LinkedList容器使用
using System; using System.Collections.Generic; namespace ConsoleApplication { class Program { stati ...
- Uva 10881 Piotr’s Ants 蚂蚁
一根长度为 L 厘米的木棍上有 n 只蚂蚁,每只蚂蚁要么朝左爬,要么朝右爬,速度为 1 厘米/秒.当两只蚂蚁相撞时,二者同时调头(掉头用的时间忽略不计).给出每只蚂蚁的初始位置和朝向,计算 T 秒之后 ...
- Android平台程序崩溃的类型及原因列举
Android平台程序崩溃大家都应该遇到过,force close和ANR应该是大家遇到较多的. 这里把Android平台程序崩溃的各种类型做一个简述和原因列举. 1.ANR(可见ANR): 发生场景 ...
- PLSQL Developer报“动态执行表不可访问,本会话的自动统计被禁止”的解决方案
现象与提示: 第一次用PLSQL Developer连接数据库,若用sys用户登录并操作则正常,若用普通用户比如haishu登录并创建一个表则报错"动态执行表不可访问,本会话的自动统计被禁止 ...