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开始到气 ...
随机推荐
- Picasso – Android系统的图片下载和缓存类库
Picasso – Android系统的图片下载和缓存类库 Picasso 是Square开源的一个用于Android系统下载和缓存图片的项目.该项目和其他一些下载图片项目的主要区别之一是:使用4.0 ...
- Android中Service类onStartCommand
Android开发的过程中,每次调用startService(Intent)的时候,都会调用该Service对象的onStartCommand(Intent,int,int)方法,然后在onStart ...
- vsim仿真VHDL输出fsdb格式文件
vsim(modelsim)仿真VHDL输出fsdb格式文件 1.Dump准备 (1) 将下列设置放到顶层testbench tb.vhd文件中[注意放置的位置:关系如图] library novas ...
- C# 导出Excel 多个Sheet
以下代码中最关键的代码是 Worksheet mSheet = (Microsoft.Office.Interop.Excel.Worksheet)mBook.Worksheets.Add(miss, ...
- C# XML,XmlDocument简单操作实例
private static string _Store = LocalPathHelper.CurrentSolutionPath + "/data/bookstore.xml" ...
- js获取上传文件扩展名
File_box.value.substring(File_box.value.lastIndexOf(".") + 1);
- UIView / UIView的布局
//! 一个视图可以有n个子视图,但是一个视图只能有一个父视图 struct CGRect { CGPoint origin; CGSize size; }; CGRectMake(CGFlo ...
- OpenGL ES 2.0 混合
混合技术 混合技术就是将俩个片元调和,主要用于将通过各项测试准备进入帧缓冲的片元(源片元)与原有片元按照设定的比例加权计算出最终片元的颜色值. OpenGL ES 2.0中是通过设置混合因子来指定两个 ...
- POJ 1001 Exponentiation 模拟小数幂
模拟小数幂 小数点位 pos 非零末位 e 长度 len 只有三种情况 pos > len pos < e e < pos < len #include <iostrea ...
- hdu 1234
Problem Description 每天第一个到机房的人要把门打开,最后一个离开的人要把门关好.现有一堆杂乱的机房签 到.签离记录,请根据记录找出当天开门和关门的人. Input 测试输入的第一行 ...