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 4907 Task schedule
对于询问q 假设q不存在直接输出q 否则输出后面第一个不存在的数 从2*10^5到1遍历一边ac #include<bits/stdc++.h> using namespace std; ...
- MongoDB[mark]总忘记它们是干啥的
MongoDB集群包括一定数量的mongod(分片存储数据).mongos(路由处理).config server(配置节点).clients(客户端).arbiter(仲裁节点:为了选举某个分片存储 ...
- Hadoop动态加入/删除节点(datanode和tacktracker)
大体,正确的做法是首选的配置文件,然后开始详细机对应的进程/停止操作. 网上一些资料说在调整配置文件的时候,优先使用主机名而不是IP进行配置. 总的来说加入/删除DataNode和TaskTracke ...
- HDU--1584--蜘蛛牌--深搜版本号
蜘蛛牌 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- MySql 跟踪命令
SHOW ; SHOW FULL PROCESSLIST; ; USE table1; ; SHOW PROFILES; ; SHOW TABLES; SHOW PROFILES; SHOW PROF ...
- 使用Dataset
string sqlStr="Select * from Tb_news"; SqlDataAdapter myDa=new SqlDataAdapter(SqlStr,myCon ...
- oracle数据库连接
///宁采花 8:37:39 /// <summary> /// 获取数据链接 /// </summary> /// <returns></returns&g ...
- Windows命令行(DOS命令)教程–2 (转载) http://arch.pconline.com.cn//pcedu/rookie/basic/10111/15325_1.html
二.符号约定 为了便于说明格式,这里我们使用了一些符号约定,它们是通用的: C: 盘符 Path 路径 Filename 文件名 .ext 扩展名 Filespec 文件标识符 [ ] 方括号中的项目 ...
- cell的各种使用和赋值 总结
cell可以分为:自定义cell,系统的cell ,cell的自适应,.xib的cell //第一种cell:系统cell 在 UIViewController下创建UITableView //1.0 ...
- 1、工程log4j 配置
### set log levels ### log4j.rootLogger = debug,stdout,R ### 输出到控制台 ### log4j.appender.stdout = org. ...