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开始到气 ...
随机推荐
- ORA-24324、ORA-12560、ORA-12514
SQL> startup ERROR: ORA-24324: 未初始化服务句柄 ORA-01041: 内部错误, hostdef 扩展名不存在. SQL> conn sys /nolog; ...
- iphone开发 IOS 组织架构图
转载自 :http://blog.csdn.net/mashi321323/article/details/18267719 登录|注册 mashi321323的专栏 目录视图 ...
- delete 用法
1.对象属性的删除 function fun(){ this.name = 'mm'; } var obj = new fun(); console.log(obj.name);//mm delete ...
- 如何禁用不需要的HTTP方法
IIS7.0默认开启了不安全的OPTIONS和TRACE方法,建议关闭这两个方法. 以下环境为windows server 2008.IIS7.0 方法(1):web.config 在<conf ...
- Java ----------- SQL语句总结(更新中。。。。。。)
#对数据库的操作 *创建数据库 CREATE DATABASE database_name:database_name为创建的数据库的变量名称. #对表的操作
- javascript实现倒计时程序
最近在网上看到一道这样的面试题: 题: 网页中实现一个计算当年还剩多少时间的倒数计时程序,要求网页上实时动态显示“××年还剩××天××时××分××秒”? 我实现了,发现挺有意思,下面把我的代码贴出来 ...
- ADO.NET中连接SQL Sever
1.在配置文件中定义数据库连接信息. 在配置文件*.config中添加这段代码在<configuration>与</configuration>之间: <connecti ...
- 常见的DoDataExchange什么意思
该函数中的代码是由ClassWizard自动加入的.DoDataExchange只有一个参数,即一个CDataExchange对象的指针pDX.在该函数中调用了DDX函数来完成数据交换,调用DDV函数 ...
- MYSQL 磁盘临时表和文件排序
因为Memory引擎不支持BOLB和TEXT类型,所以,如果查询使用了BLOB或TEXT列并且需要使用隐式临时表,将不得不使用MyISAM磁盘临时表,即使只有几行数据也是如此. 这会导致严重的性能开销 ...
- AFNetworking 使用方法(2.0)
AFNetworking 使用方法(2.0) 分类: IOS2014-11-12 09:17 2018人阅读 评论(0) 收藏 举报 目录(?)[+] 本文介绍的是AFNetworking-2 ...