HDU6447 YJJ's Salesman-2018CCPC网络赛-线段树求区间最值+离散化+dp
(有任何问题欢迎留言或私聊 && 欢迎交流讨论哦
Catalog
Problem:Portal传送门
原题目描述在最下面。
1e5个点,问从(0,0)走到(1e9,1e9)的最大收益。
当你从(u-1,v-1)走到(u,v)时,你可以获得点(u,v)的权值。
Solution:
十分详细了。
直接线段树区间最值。当然也可以树状数组,不能st表。
\(dp[i] = max(query\_max(0,dp[i]-1,1)+val[i] ,dp[i])\)
\(update(i, dp[i],1)\)
记得离散化再排序,先x从小到大,再y从大到小,每次更行一行。
按01背包的更新顺序,滚动数组优化为一维。
细节见代码。
AC_Code:
#include<bits/stdc++.h>
#define lson rt<<1
#define rson rt<<1|1
#define mme(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef unsigned long long LL;
const int N = 2e5 + 7;
const int M = 1e5 + 7;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
int ar[N],br[N];//离散化
int dp[N];
struct lh{//储存1e5个点
int x,y,v;
}op[N];
bool cmp(lh &a,lh &b){
if(a.x!=b.x)return a.x<b.x;
return a.y>b.y;
}
int n;
/**********线段树区间最值**********/
struct lp{
int l, r, sum;
}cw[N<<2];
void push_up(int rt){
cw[rt].sum = max(cw[lson].sum, cw[rson].sum);
}
void build(int l,int r,int rt){
cw[rt].l = l;cw[rt].r = r;cw[rt].sum = 0;
if(l==r){
return;
}
int mid = (l+r)/2;
build(l,mid,lson);build(mid+1,r,rson);
push_up(rt);
}
void update(int p,int c,int rt){
int l = cw[rt].l, r = cw[rt].r,mid = (l+r)/2;
if(l==r){
cw[rt].sum = c;
return;
}
if(p<=mid)update(p,c,lson);
else update(p,c,rson);
push_up(rt);
}
int query(int L,int R,int rt){
int l = cw[rt].l, r = cw[rt].r,mid = (l+r)/2;
if(L<=l&&r<=R){
return cw[rt].sum;
}
if(L>mid)return query(L,R,rson);
else if(R<=mid)return query(L,R,lson);
return max(query(L,mid,lson),query(mid+1,R,rson));
}
/****************/
int main(){
int tim;
scanf("%d", &tim);
while(tim--){
scanf("%d", &n);
for(int i = 0; i < n; ++i){
scanf("%d%d%d",&op[i].x,&op[i].y,&op[i].v);
ar[i] = op[i].x;br[i]=op[i].y;
}
int p = n + 1;
ar[n] = 0;br[n] = 0;
sort(ar,ar+p);
sort(br,br+p);
int a = unique(ar,ar+p)-ar;
int b = unique(br,br+p)-br;
for(int i = 0; i < n; ++i){
op[i].x=lower_bound(ar,ar+a,op[i].x)-ar;
op[i].y=lower_bound(br,br+b,op[i].y)-br;
}
//以上离散化
sort(op,op+n,cmp);
build(0, b, 1);
mme(dp, 0);
for(int i = 0; i < n; ++i){
int flag = op[i].x, j;
for(j = i; j < n; ++j){
if(op[j].x != flag){
break;
}
int tmp = query(0, op[j].y-1, 1) + op[j].v;
if(tmp > dp[op[j].y]){
dp[op[j].y] = tmp;
update(op[j].y, dp[op[j].y], 1);
}
}
i = j - 1;
}
int ans = 0;
for(int i = 0; i <= b; ++i){
ans = max(ans, dp[i]);
}
printf("%d\n", ans);
}
return 0;
}
####Problem Description:

HDU6447 YJJ's Salesman-2018CCPC网络赛-线段树求区间最值+离散化+dp的更多相关文章
- xdoj-1324 (区间离散化-线段树求区间最值)
思想 : 1 优化:题意是覆盖点,将区间看成 (l,r)转化为( l-1,r) 覆盖区间 2 核心:dp[i] 覆盖从1到i区间的最小花费 dp[a[i].r]=min (dp[k])+a[i]s; ...
- hdu 1754 I Hate It (线段树求区间最值)
HDU1754 I Hate It Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u D ...
- 2016年湖南省第十二届大学生计算机程序设计竞赛---Parenthesis(线段树求区间最值)
原题链接 http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1809 Description Bobo has a balanced parenthes ...
- hdu 5443 (2015长春网赛G题 求区间最值)
求区间最值,数据范围也很小,因为只会线段树,所以套了线段树模板=.= Sample Input3110011 151 2 3 4 551 21 32 43 43 531 999999 141 11 2 ...
- 【线段树求区间第一个不大于val的值】Lpl and Energy-saving Lamps
https://nanti.jisuanke.com/t/30996 线段树维护区间最小值,查询的时候优先向左走,如果左边已经找到了,就不用再往右了. 一个房间装满则把权值标记为INF,模拟一遍,注意 ...
- 滑动窗口(poj,线段树维护区间最值)
题目描述 现在有一堆数字共N个数字(N<=10^6),以及一个大小为k的窗口.现在这个从左边开始向右滑动,每次滑动一个单位,求出每次滑动后窗口中的最大值和最小值. 例如: The array i ...
- hdu1754 I hate it线段树模板 区间最值查询
题目链接:这道题是线段树,树状数组最基础的问题 两种分类方式:按照更新对象和查询对象 单点更新,区间查询; 区间更新,单点查询; 按照整体维护的对象: 维护前缀和; 维护区间最值. 线段树模板代码 # ...
- poj 3264 线段树 求区间最大最小值
Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same ...
- POJ 2482 Stars in Your Window (线段树+扫描线+区间最值,思路太妙了)
该题和 黑书 P102 采矿 类似 参考链接:http://blog.csdn.net/shiqi_614/article/details/7819232http://blog.csdn.net/ts ...
随机推荐
- PHP ftp_chmod() 函数
定义和用法 ftp_chmod() 函数设置 FTP 服务器上指定文件的权限. 如果成功,该函数返回新的权限.如果失败,则返回 FALSE 和一个警告. 语法 ftp_chmod(ftp_connec ...
- luoguP3806 【模板】点分治1 [点分治]
题目背景 感谢hzwer的点分治互测. 题目描述 给定一棵有n个点的树 询问树上距离为k的点对是否存在. 输入输出格式 输入格式: n,m 接下来n-1条边a,b,c描述a到b有一条长度为c的路径 接 ...
- NOIp2018集训test-10-22 (联考六day2)
中间值 两个log肯定会被卡.我用的第一种做法,就是要各种特判要在两个序列都要二分比较麻烦. //Achen #include<bits/stdc++.h> #define For(i,a ...
- js面向对象编程(第2版)——js继承多种方式
附带书籍地址: js面向对象编程(第2版)
- Go语言中接口组合的实现方法
在Go语言中,可以在接口A中组合其它的一个或多个接口(如接口B.C),这种方式等价于在接口A中添加接口B.C中声明的方法. 代码如下: //接口中可以组合其它接口,这种方式等效于在接口中添加其它接口的 ...
- golang 读取 ini配置信息
package main //BY: 29295842@qq.com//这个有一定问题 如果配置信息里有中文就不行//[Server] ;MYSQL配置//Server=localhost ...
- Java-Class-C:com.github.pagehelper.PageInfo
ylbtech-Java-Class-C:com.github.pagehelper.PageInfo 1.返回顶部 2.返回顶部 1.1. import com.github.pagehelpe ...
- (1)Redis 基本类型
https://redis.io/ http://redisdoc.com/ 中文 一. 库 redis默认16个库,0-15.默认端口号 6379 使用某个库 测试服务器是否连通 ping // ...
- Eclipse lombok get set方法报错
在maven仓库中找到 lombok jar包 如:C:\Program File\.m2\Repository\org\projectlombok\lombok\1.18.2\lombok-1.18 ...
- linux:lrzsz安装
Linux中的lrzsc是linux里可代替ftp上传和下载的程序. yum install lrzsc 没有可用软件包 lrzsc. 这时使用 -y即可安装 centos安装:yum -y inst ...