HDU 4362 Dragon Ball 线段树
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef __int64 ll;
const ll Inf = (ll)(1e15);
const int N = 1000 + 10;
const int M = 50 + 5;
struct node {
ll addv, min;
};
struct tnode {
int pos;
ll cos;
tnode() {
}
tnode(int _pos, ll _cos) {
pos = _pos;
cos = _cos;
}
}; node seg[N << 2];
vector<tnode> a[M];
int val[M][N][2];
ll d[M][N]; void Up(node& fa, node &ls, node& rs) {
if (ls.min > rs.min) {
fa.min = rs.min;
} else {
fa.min = ls.min;
}
}
void Down(node& fa, node& ls, node& rs) {
if (fa.addv != 0) {
ls.min += fa.addv;
rs.min += fa.addv;
ls.addv += fa.addv;
rs.addv += fa.addv;
fa.addv = 0;
}
}
void build(int i, int p, int l, int r, int rt) {
seg[rt].addv = 0;
if (l == r) {
seg[rt].min = d[i][l] + abs(p - a[i][l].pos);
} else {
int mid = (l + r) >> 1;
build(i, p, lson);
build(i, p, rson);
Up(seg[rt], seg[rt << 1], seg[rt << 1 | 1]);
}
}
void update(int L, int R, ll v, int l, int r, int rt) {
if (L <= l && r <= R) {
seg[rt].min += v;
seg[rt].addv += v;
} else {
int mid = (l + r) >> 1;
Down(seg[rt], seg[rt << 1], seg[rt << 1 | 1]);
if (L > mid)
update(L, R, v, rson);
else if (R <= mid)
update(L, R, v, lson);
else {
update(L, mid, v, lson);
update(mid + 1, R, v, rson);
}
Up(seg[rt], seg[rt << 1], seg[rt << 1 | 1]);
}
}
void update_pos(int i, int j, int from, int to, int l, int r, int rt) {
if (l == r) {
int p = a[i][j].pos;
ll v = (ll)-abs(p - from) + abs(p - to);
seg[rt].min += v;
seg[rt].addv += v;
} else {
Down(seg[rt], seg[rt << 1], seg[rt << 1 | 1]);
int mid = (l + r) >> 1;
if (j <= mid)
update_pos(i, j, from, to, lson);
else
update_pos(i, j, from, to, rson);
Up(seg[rt], seg[rt << 1], seg[rt << 1 | 1]);
}
}
bool cc(const tnode& i, const tnode& j) {
return i.pos < j.pos;
}
void work() {
for (int i = 0; i < M; ++i)
a[i].clear(); int m, n, x, st, p, idx, idy;
scanf("%d%d%d", &n, &m, &st);
for (int i = 1; i <= n; ++i)
for (int j = 0; j < m; ++j)
scanf("%d", &val[i][j][0]);
for (int i = 1; i <= n; ++i)
for (int j = 0; j < m; ++j)
scanf("%d", &val[i][j][1]);
a[0].push_back(tnode(st, 0));
for (int i = 1; i <= n; ++i) {
for (int j = 0; j < m; ++j)
a[i].push_back(tnode(val[i][j][0], val[i][j][1]));
sort(a[i].begin(), a[i].end(), cc);
}
// dp
for (int i = 0; i <= n; ++i)
for (int j = 0; j < a[i].size(); ++j)
d[i][j] = Inf;
d[0][0] = 0;
for (int i = 1; i <= n; ++i) {
p = a[i][0].pos;
build(i - 1, p, 0, a[i - 1].size() - 1, 1);
d[i][0] = a[i][0].cos + seg[1].min;
//
idx = -1;
while (idx + 1 < a[i - 1].size() && a[i - 1][idx + 1].pos < p)
++ idx;
idy = a[i - 1].size();
while (idy - 1 >= 0 && a[i - 1][idy - 1].pos > p)
-- idy;
//
for (int j = 1; j < a[i].size(); ++j) {
while (idy < a[i - 1].size() && a[i - 1][idy].pos <= a[i][j].pos)
++ idy;
if (idx >= 0)
update(0, idx, a[i][j].pos - p, 0, a[i - 1].size() - 1, 1);
if (idy < a[i - 1].size())
update(idy, a[i - 1].size() - 1, p - a[i][j].pos, 0, a[i - 1].size() - 1, 1);
//
for (int k = idx + 1; k < idy; ++k)
update_pos(i - 1, k, p, a[i][j].pos, 0, a[i - 1].size() - 1, 1);
//
d[i][j] = a[i][j].cos + seg[1].min;
p = a[i][j].pos;
while (idx + 1 < a[i - 1].size() && a[i - 1][idx + 1].pos < p)
++ idx;
}
}
ll ans = Inf;
for (int i = 0; i < a[n].size(); ++i)
ans = min(ans, d[n][i]);
cout << ans << endl;
}
int main() {
int cas;
scanf("%d", &cas);
while (cas -- > 0)
work();
return 0;
}
HDU 4362 Dragon Ball 线段树的更多相关文章
- HDU-3872 Dragon Ball 线段树+DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3872 题意:有n个龙珠按顺序放在一列,每个龙珠有一个type和一个权值,要求你把这n个龙珠分成k个段, ...
- HDU 4362 Dragon Ball 贪心DP
Dragon Ball Problem Description Sean has got a Treasure map which shows when and where the dragon ...
- HDU.1556 Color the ball (线段树 区间更新 单点查询)
HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...
- hdu 5700区间交(线段树)
区间交 Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submiss ...
- Snacks HDU 5692 dfs序列+线段树
Snacks HDU 5692 dfs序列+线段树 题意 百度科技园内有n个零食机,零食机之间通过n−1条路相互连通.每个零食机都有一个值v,表示为小度熊提供零食的价值. 由于零食被频繁的消耗和补充, ...
- HDU 1556 Color the ball(线段树区间更新)
Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...
- hdu 1556 Color the ball (线段树+代码详解)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- hdu 1556 Color the ball(线段树区间维护+单点求值)
传送门:Color the ball Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/3276 ...
- hdu 1556 Color the ball 线段树
题目链接:HDU - 1556 N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气 ...
随机推荐
- HDU 1027 Ignatius and the Princess II 选择序列题解
直接选择序列的方法解本题,可是最坏时间效率是O(n*n),故此不能达到0MS. 使用删除优化,那么就能够达到0MS了. 删除优化就是当须要删除数组中的元素为第一个元素的时候,那么就直接移动数组的头指针 ...
- 安装VMware Sphere ESXi 5.5
安装VMware Sphere ESXi 5.5 1.准备 待安装ESXi 5.5的机器需要大于2GB以上内存,并且支持64位和虚拟化. 下载:VMware-VMvisor-Installer-5.5 ...
- (转)2.4.1 基础知识--添加服务引用与Web引用的区别
<Web服务开发学习实录>第2章构建ASP.NET Web服务,本章我们将学习创建Web服务的各种方法,并重点对使用Visual Studio创建ASP.NET Web服务和修改Web服务 ...
- HLS(HTTP Live Streaming)协议之m3u8文件生成方式
HLS(HTTP Live Streaming)是Apple的动态码率自适应技术.主要用于PC和Apple终端的音视频服务.包括一个m3u(8)的索引文件,TS媒体分片文件和key加密串文件. HLS ...
- 三组I/O复用模型的比较
概论: select.poll和epoll三组I/O复用系统调用,这3组系统调用都能同时监听多个文件描述符.它们将等待由timeout参数指定的超时时间,直到一个或者多个文件描述符上有事件发生时返回. ...
- AIX下解决POWERHA的脑裂问题
一.安装创建并发vg时必需的软件包clvm包,该包安装.升级.后必须重启os clvm包的描述:Enhanced Concurrent Logical Volume Manager 软件包在aix61 ...
- redis数据结构与主要命令
redis的数据类型有:string.hashes.lists.sets,sorted sets 1.string类型: set.get添加键值对获得键值对.如果多次赋值会覆盖掉原来的value se ...
- hadoop调优之一:概述
hadoop集群性能低下的常见原因 (一)硬件环境 1.CPU/内存不足,或未充分利用 2.网络原因 3.磁盘原因 (二)map任务原因 1.输入文件中小文件过多,导致多次启动和停止JVM进程.可以设 ...
- jQuery封装的表单验证,模仿网易或者腾讯登录的风格
模仿网易邮箱做了一个登录表单验证,不太好,请指教 上代码 <form action="" name="" id="form1"> ...
- python之简单入门01
python简单的介绍使用: 一.个人感觉写Python程序,最好用的工具就是pycharm了,自动补全功能可以满足大多数不太喜欢记忆的人群: 安装pycharm之前应该先安装python解释器,目 ...