JZOJ 2474. 【GDKOI 2021普及组DAY2】我的世界
题解
这题很明显发现一个点到另一个点,必然最多只有一个进入下界的点和一个出来的点
分类讨论入点和出点的位置
要么都在 \(u->lca\) 或都在 \(lca->v\) 或分别有一个
那就有一个倍增做法
维护最优入点和最优出点即可
但考场并没想到这种方法
反而只想到了没脑的倍增维护矩阵转移的方法
我们设 \(f_{u,0/1}\) 表示从它的儿子 \(v\) 过来的最优答案
把转移式子写成矩阵的形式,倍增维护即可
但我的常数太大了,沦为和暴力老哥同分
\(Code\)
#pragma GCC optimize(3)
#pragma GCC optimize("inline")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse3","sse2","sse")
#pragma GCC diagnostic error "-std=c++14"
#pragma GCC diagnostic error "-fwhole-program"
#pragma GCC diagnostic error "-fcse-skip-blocks"
#pragma GCC diagnostic error "-funsafe-loop-optimizations"
#pragma GCC optimize("fast-math","unroll-loops","no-stack-protector","inline")
#include<cstdio>
#include<iostream>
#define LL long long
using namespace std;
const int N = 2e5 + 5;
int n;
LL a[N];
inline void read(int &x)
{
x = 0; int f = 1; char ch = getchar();
while (ch < '0' || ch > '9') f = (ch == '-' ? -1 : f), ch = getchar();
while (ch >= '0' && ch <= '9') x = (x<<3) + (x<<1) + ch - '0', ch = getchar();
x *= f;
}
inline void read(LL &x)
{
x = 0; int f = 1; char ch = getchar();
while (ch < '0' || ch > '9') f = (ch == '-' ? -1 : f), ch = getchar();
while (ch >= '0' && ch <= '9') x = (x<<3) + (x<<1) + ch - '0', ch = getchar();
x *= f;
}
int h[N], tot;
struct edge{int to, nxt; LL w;}e[N << 1];
inline void add(int x, int y, LL z){e[++tot] = edge{y, h[x], z}, h[x] = tot;}
const LL INF = 1e18;
struct Matrix{
LL a[3][3];
inline Matrix()
{
for(register int i = 0; i < 3; i++)
for(register int j = 0; j < 3; j++) a[i][j] = INF;
}
inline Matrix operator * (const Matrix &b)
{
Matrix c;
for(register int i = 0; i < 3; i++)
for(register int j = 0; j < 3; j++)
for(register int k = 0; k < 3; k++)
c.a[i][j] = min(c.a[i][j], a[i][k] + b.a[k][j]);
return c;
}
}f[N][20];
inline Matrix I()
{
Matrix c;
c.a[0][0] = c.a[1][1] = c.a[2][2] = 0;
return c;
}
inline Matrix BOT(int u)
{
Matrix c;
c.a[0][0] = c.a[1][0] = 0, c.a[2][0] = a[u];
return c;
}
inline Matrix Node(int u, int v, LL w)
{
Matrix c;
c.a[0][0] = 8 * w, c.a[0][1] = w + a[u] + a[v], c.a[0][2] = a[u] + w;
c.a[1][0] = 8 * w, c.a[1][1] = w + a[u] + a[v], c.a[1][2] = a[u] + w;
c.a[2][0] = 8 * w + a[u], c.a[2][1] = w + a[v], c.a[2][2] = w;
return c;
}
int dep[N], anc[N][20], d[N];
void bfs()
{
int head = 0, tail = 1;
d[1] = 1;
while (head < tail)
{
int x = d[++head];
for(register int i = 1; i <= 18; i++)
if (anc[x][i - 1]) anc[x][i] = anc[anc[x][i - 1]][i - 1], f[x][i] = f[anc[x][i - 1]][i - 1] * f[x][i - 1];
else break;
for(register int i = h[x]; i; i = e[i].nxt)
{
int v = e[i].to;
if (v == anc[x][0]) continue;
anc[v][0] = x, f[v][0] = Node(x, v, e[i].w);
dep[v] = dep[x] + 1, d[++tail] = v;
}
}
}
inline LL getans(int x, int y)
{
if (dep[x] < dep[y]) swap(x, y);
int deep = dep[x] - dep[y];
Matrix retx = I(), rety = I(), Bx = BOT(x), By = BOT(y);
for(register int i = 18; i >= 0; i--)
if ((deep >> i) & 1) retx = f[x][i] * retx , x = anc[x][i];
if (x == y)
{
retx = retx * Bx;
return min(retx.a[0][0], retx.a[2][0] + a[x]);
}
for(register int i = 18; i >= 0; i--)
if (anc[x][i] ^ anc[y][i])
{
retx = f[x][i] * retx , rety = f[y][i] * rety;
x = anc[x][i], y = anc[y][i];
}
retx = f[x][0] * retx * Bx, rety = f[y][0] * rety * By;
return min(min(retx.a[0][0] + rety.a[0][0], retx.a[2][0] + rety.a[2][0]),
min(retx.a[0][0] + rety.a[2][0] + a[anc[x][0]], retx.a[2][0] + rety.a[0][0] + a[anc[x][0]]));
}
int main()
{
freopen("minecraft.in", "r", stdin);
freopen("minecraft.out", "w", stdout);
read(n);
for(register int i = 1; i <= n; i++) read(a[i]);
int x, y; LL z;
for(register int i = 1; i < n; i++) read(x), read(y), read(z), add(x, y, z), add(y, x, z);
bfs();
int q; read(q);
for(; q; --q) read(x), read(y), printf("%lld\n", getans(x, y));
}
JZOJ 2474. 【GDKOI 2021普及组DAY2】我的世界的更多相关文章
- [GDKOI2021] 普及组 Day2 总结
[ G D K O I 2021 ] 普 及 组 D a y 2 总 结 [GDKOI2021] 普及组 Day2 总结 [GDKOI2021]普及组Day2总结 时间安排和昨天的GDKOI2021 ...
- JZOJ.2117. 【2016-12-30普及组模拟】台风
题目大意: 天气预报频道每天从卫星上接受卫星云图.图片被看作是一个矩阵,每个位置上要么是”#”,要么”.”,”#”表示该位置没有云,”.”表示有云,地图上每个位置有多达8个相邻位置,分别是,左上.上. ...
- GDOI 2021 普及组溺水记
Day 1 T1 一看样例:答案不就是 \(\dfrac{\max_{i=1}^n a_i +1}{2}\) 吗? 于是自信打上,拍都不拍.然后就,,对了? 插曲:自己出了一个极端数据,发现 scan ...
- [GDKOI2021] 普及组 Day3 总结 && 题解
[ G D K O I 2021 ] 普 及 组 D a y 3 总 结 时间安排和昨天的GDKOI2021 Day2一样. 早上四个小时的快乐码题时间,然鹅我打了半小时的表 然后就是下午的题目讲解和 ...
- NOIP2017普及组初赛试题及答案
普及组C++语言试题 一.单项选择题(共 20 题,每题 1.5 分,共计 30 分:每题有且仅有一个正确选项) 1.在 8 位二进制补码中,10101011 表示的数是十进制下的( ). A. 43 ...
- noip2017爆炸记——题解&总结&反省(普及组+提高组)
相关链接: noip2018总结 noip2017是我见过的有史以来最坑爹的一场考试了. 今年北京市考点有一个是我们学校,我还恰好被分到了自己学校(还是自己天天上课的那个教室),于是我同时报了普及提高 ...
- 2016.8.15上午纪中初中部NOIP普及组比赛
2016.8.15上午纪中初中部NOIP普及组比赛 链接:https://jzoj.net/junior/#contest/home/1333 这次比赛不怎么好,因为这套题目我并不是很擅长. 可同学们 ...
- 2016.9.10初中部上午NOIP普及组比赛总结
2016.9.10初中部上午NOIP普及组比赛总结 链接:https://jzoj.net/junior/#contest/home/1340 好不爽!翻车了!不过排名差不多在中间偏上一点, 还好不是 ...
- 2016.9.3初中部上午NOIP普及组比赛总结
2016.9.3初中部上午NOIP普及组比赛总结 链接:https://jzoj.net/junior/#contest/home/1339 这次真爽,拿了个第四!(我还被班主任叫过去1小时呢!) 进 ...
- 2016.8.19上午初中部NOIP普及组比赛总结
2016.8.19上午初中部NOIP普及组比赛总结 链接:https://jzoj.net/junior/#contest/home/1338 这次总结发得有点晚啊!我在这里解释一下, 因为浏览器的问 ...
随机推荐
- 如何禁止win7自动锁屏
前言 我是真的服了,就解决这个问题百度查了一大堆(浪费很长时间),都说是电源管理的问题,也不知道是谁抄谁的,改完还会自动锁屏. 然后我google一下子就解决了(这里有一个搜索技巧,就是将你的问题翻译 ...
- 移动 VR 开发时要避免的 PC 渲染技术
更新:本文是为 Quest 1 开发人员编写的.虽然 Quest 2 建立在相同的架构上,但现在更容易为阴影贴图(以及其他需要从先前渲染过程中生成的纹理读取的简单技术)做预算. 尽管移动芯片组可以支持 ...
- day24 JDBC批处理(通用泛型查询方法 & 下划线转驼峰命名法)
批处理 public static Integer addBatch(String[] sqls){ init(); try { //设置关闭自动提交 conn.setAutoCommit(false ...
- 【每日一题】【比较中右,内部比较中右,注意边界带>=】2021年11月2日-搜索旋转排序数组-211102/220211
[某下标处进行了旋转]整数数组 nums 按升序排列,数组中的值 互不相同 . 在传递给函数之前,nums 在预先未知的某个下标 k(0 <= k < nums.length)上进行了 旋 ...
- CTF中RSA常见类型解法
Python脚本 #十六进制转ASCII编码 import binascii print(binascii.unhexlify(hex(m)[2:])) #rsa import gmpy2 phi = ...
- can not be used when making a PIE object
编译报错 relocation R_X86_64_32 against `.rodata' can not be used when making a PIE object; recompile wi ...
- texlive编译lshort-zh-cn
项目 lshort-zh-cn是一篇latex的中文文档,本身也是latex编写的. 项目地址:https://github.com/ctex-org/lshort-zh-cn 编译 texlive打 ...
- 1、mybatis逆向工程
mybatis逆向工程可以针对单表自动生成mybatis执行所需要的mapper.java.mapper.xml代码(dao层),可以让程序员将更多的精力放在繁杂的业务逻辑上(service层与con ...
- 对于goland相对较新一些版本新建项目时没用go mod模式选项的坑
前言 对于一些小白在网上看很早的一些go视频,使用goland2020.3.x版本或者其之前版本创建新项目,里面会有GO Modules(vgo)这个选项,也就是gomod模式创建新项目,然而对于现在 ...
- xpath解析数据的方法
1 功能描述 2 1.实例化一个etree对象,且需要将被解析的页面源码数据加载到该对象中 3 2.调用etree对象中的XPath表达式实现标签的定位和内容捕获 4 3.环境安装 pip insta ...