HDU 5614 Baby Ming and Matrix tree 树链剖分
题意:
给出一棵树,每个顶点上有个\(2 \times 2\)的矩阵,矩阵有两种操作:
- 顺时针旋转90°,花费是2
- 将一种矩阵替换为另一种矩阵,花费是10
树上有一种操作,将一条路经上的所有矩阵都变为给出的矩阵,并输出最小花费。
分析:
矩阵可以分为两类共6种,一类是两个1相邻的矩阵共4种;一类是两个1在对角线的矩阵共2种。
同一类矩阵可以通过旋转操作得到,否则只能用替换。
事先计算好每种矩阵转换到另外一种矩阵的最少花费,然后树链剖分再用线段树维护就好了。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn = 20000 + 10;
const int maxnode = maxn * 4;
void read(int& x) {
x = 0;
char c = ' ';
while(c < '0' || c > '9') c = getchar();
while('0' <= c && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
}
int n, tot;
vector<int> G[maxn];
int son[maxn], sz[maxn], top[maxn], dep[maxn], fa[maxn];
int id[maxn], pos[maxn];
void dfs(int u) {
sz[u] = 1; son[u] = 0;
for(int v : G[u]) {
if(v == fa[u]) continue;
fa[v] = u;
dep[v] = dep[u] + 1;
dfs(v);
sz[u] += sz[v];
if(sz[v] > sz[son[u]]) son[u] = v;
}
}
void dfs2(int u, int tp) {
id[u] = ++tot;
pos[tot] = u;
top[u] = tp;
if(son[u]) dfs2(son[u], tp);
for(int v : G[u]) {
if(v == fa[u] || v == son[u]) continue;
dfs2(v, v);
}
}
int cost[6][6];
int cntv[maxnode][6], setv[maxnode];
int readMat() {
int a[4];
read(a[0]); read(a[1]); read(a[3]); read(a[2]);
for(int i = 0; i < 4; i++)
if(a[i] == 1 && a[(i+1)%4] == 1) return i;
if(a[0] == 1) return 4;
return 5;
}
int h[maxn];
void pushdown(int o, int L, int R) {
if(setv[o] != -1) {
int M = (L + R) / 2;
setv[o<<1] = setv[o<<1|1] = setv[o];
for(int i = 0; i < 6; i++)
cntv[o<<1][i] = cntv[o<<1|1][i] = 0;
cntv[o<<1][setv[o]] = M - L + 1;
cntv[o<<1|1][setv[o]] = R - M;
setv[o] = -1;
}
}
void pushup(int o) {
for(int i = 0; i < 6; i++)
cntv[o][i] = cntv[o<<1][i] + cntv[o<<1|1][i];
}
void build(int o, int L, int R) {
if(L == R) { cntv[o][h[pos[L]]] = 1; return; }
int M = (L + R) / 2;
build(o<<1, L, M);
build(o<<1|1, M+1, R);
pushup(o);
}
int q[6];
void update(int o, int L, int R, int qL, int qR, int v) {
if(qL <= L && R <= qR) {
setv[o] = v;
for(int i = 0; i < 6; i++) q[i] += cntv[o][i];
for(int i = 0; i < 6; i++) cntv[o][i] = 0;
cntv[o][v] = R - L + 1;
return;
}
pushdown(o, L, R);
int M = (L + R) / 2;
if(qL <= M) update(o<<1, L, M, qL, qR, v);
if(qR > M) update(o<<1|1, M+1, R, qL, qR, v);
pushup(o);
}
void UPDATE(int u, int v, int val) {
memset(q, 0, sizeof(q));
int t1 = top[u], t2 = top[v];
while(t1 != t2) {
if(dep[t1] < dep[t2]) { swap(u, v); swap(t1, t2); }
update(1, 1, n, id[t1], id[u], val);
u = fa[t1]; t1 = top[u];
}
if(dep[u] > dep[v]) swap(u, v);
update(1, 1, tot, id[u], id[v], val);
}
int main()
{
for(int i = 0; i < 6; i++)
for(int j = 0; j < 6; j++) {
if(i == j) { cost[i][j] = 0; continue; }
int a = ((i >> 2) & 1), b = ((j >> 2) & 1);
if(a ^ b) cost[i][j] = 10;
else if(!a) cost[i][j] = ((((j - i) % 4) + 4) % 4) * 2;
else cost[i][j] = 2;
}
int kase; scanf("%d", &kase);
while(kase--) {
read(n);
for(int i = 1; i <= n; i++) G[i].clear();
for(int i = 1; i < n; i++) {
int u, v; read(u); read(v);
G[u].push_back(v);
G[v].push_back(u);
}
sz[0] = fa[1] = 0;
dfs(1);
tot = 0;
dfs2(1, 1);
//build segment tree
memset(cntv, 0, sizeof(cntv));
memset(setv, -1, sizeof(setv));
for(int i = 1; i <= n; i++) h[i] = readMat();
build(1, 1, n);
//Queries
int _; scanf("%d", &_);
while(_--) {
int u, v, val; read(u); read(v);
val = readMat();
UPDATE(u, v, val);
int ans = 0;
for(int i = 0; i < 6; i++) ans += q[i] * cost[i][val];
printf("%d\n", ans);
}
}
return 0;
}
HDU 5614 Baby Ming and Matrix tree 树链剖分的更多相关文章
- Hdu 5274 Dylans loves tree (树链剖分模板)
Hdu 5274 Dylans loves tree (树链剖分模板) 题目传送门 #include <queue> #include <cmath> #include < ...
- POJ3237 Tree 树链剖分 边权
POJ3237 Tree 树链剖分 边权 传送门:http://poj.org/problem?id=3237 题意: n个点的,n-1条边 修改单边边权 将a->b的边权取反 查询a-> ...
- hdu 5612 Baby Ming and Matrix games
Baby Ming and Matrix games 题意: 给一个矩形,两个0~9的数字之间隔一个数学运算符(‘+’,’-‘,’*’,’/’),其中’/’表示分数除,再给一个目标的值,问是否存在从一 ...
- HDU 5044 Tree --树链剖分
题意:给一棵树,两种操作: ADD1: 给u-v路径上所有点加上值k, ADD2:给u-v路径上所有边加上k,初始值都为0,问最后每个点和每条边的值,输出. 解法:树链剖分可做,剖出来如果直接用线段树 ...
- HDU 5840 This world need more Zhu 树链剖分+暴力
This world need more Zhu 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5840 Description As we all ...
- HDU 3966:Aragorn's Story(树链剖分)
http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意:有n个点n-1条边,每个点有一个权值,有两种操作:询问一个点上权值是多少和修改u到v这条链上的权值. ...
- Query on a tree——树链剖分整理
树链剖分整理 树链剖分就是把树拆成一系列链,然后用数据结构对链进行维护. 通常的剖分方法是轻重链剖分,所谓轻重链就是对于节点u的所有子结点v,size[v]最大的v与u的边是重边,其它边是轻边,其中s ...
- 【BZOJ-4353】Play with tree 树链剖分
4353: Play with tree Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 31 Solved: 19[Submit][Status][ ...
- SPOJ Query on a tree 树链剖分 水题
You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, ...
随机推荐
- watir 的api在线文档
http://rubydoc.info/gems/watir-webdriver/frames http://rdoc.info/gems/watir-webdriver/frames http:// ...
- pytorch 0.3 win7 安装
pytorch 0.3 win7 安装 参考这个文章:https://github.com/peterjc123/pytorch-scripts 首先安装 conda 这个链接下载: python 3 ...
- 关于UITableView的性能优化(历上最全面的优化分析)
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath ...
- 基于Java实现的快速排序
简述 快速排序是一种排序执行效率很高的排序算法,它利用分治法来对待排序序列进行分治排序,它的思想主要是通过一趟排序将待排记录分隔成独立的两部分,其中的一部分比关键字小,后面一部分比关键字大,然后再对这 ...
- 小米OJ刷题日志
虽然这OJ上的题比较水,但还是挺有意思的.关键是能赚钱 特别是提交方式 居然不支持C++,垃圾OJ 4. 最长连续数列 排序后dp 5. 找出旋转有序数列的中间值 写个排序就做完了. 6. 交叉队列 ...
- css 居中问题总结
html代码: <div class="box"> <div class="box-item"> 文字 </div> < ...
- DA层(数据访问层)的方法不用静态的
1.静态方法,不会经过构造函数,所以你不能通过构造函数来初始参数,你只能通过传递参数,来初始他当你有多种参数需要传递的时候,你就要不断重载他了.当然你可以用参数型的类型,不过如果参数有一定结构,就很麻 ...
- 新萝卜家园GHOST WIN7系统32,64位官方版下载
来自系统妈:http://www.xitongma.com 新萝卜家园GHOST win7系统64位官方经典版 V2016年3月 系统概述 新萝卜家园ghost win7系统64位官方经典版加快“网上 ...
- [机器学习] 简单的机器学习算法和sklearn实现
机器学习基础算法理解和总结 KNN算法 理解 KNN其实是最好理解的算法之一,其实就是依次和空间中的每个点进行距离比较,取距离最近的N个点,看这N个点的类别,那么要判断的点的类别就是这N个点中类别占比 ...
- Forbidden You don't have permission to access /phpStudyTest/application/index/controller/Index.php on this server.
发生情况:将thinkPHP从官网上下了 http://thinkphp.cn 然后安装了phpstudy和PHPstorm,并将thinkPHP解压到www路径下 在用PHPstorm打开 thi ...