hdu 6012 Lotus and Horticulture 打标记
http://acm.hdu.edu.cn/showproblem.php?pid=6012
我们希望能够快速算出,对于每一个温度,都能够算出它在这n颗植物中,能得到多少价值。
那么,对于第i科植物,在[0, L[i] - 1]这些温度中,得到的价值是低温那个价值,同理在[L[i], R[i]]中,和[R[i], mx]中,
那么可以用O(1)打标记的思路去完成。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
map<int, LL>book;
void work() {
book.clear();
int n;
scanf("%d", &n);
for (int i = ; i <= n; ++i) {
int L, R, a, b, c;
scanf("%d%d%d%d%d", &L, &R, &a, &b, &c);
book[] += c;
book[L << ] += a - c;
book[(R << ) + ] += b - a;
}
LL ans = ;
LL tans = ;
for (map<int, LL> :: iterator it = book.begin(); it != book.end(); ++it) {
tans += it->second;
ans = max(ans, tans);
}
printf("%I64d\n", ans);
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
int t;
scanf("%d", &t);
while (t--) work();
return ;
}
同样可以用线段树完成,就是先把坐标离散了,然后区间更新和上面一样的东西。
线段树维护最大值,seg[cur]表示这课树覆盖的区间,其中某个温度能取得的最大值。
但是我一直wa,不知为何。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
#define lson L, mid, cur << 1
#define rson mid + 1, R, cur << 1 | 1
#define root 1, mx, 1
const int maxn = + ;
int L[maxn], R[maxn];
LL a[maxn], b[maxn], c[maxn];
LL add[maxn << ], seg[maxn << ];
vector<int>da;
void pushDown(int cur) {
if (add[cur]) {
add[cur << ] += add[cur];
add[cur << | ] += add[cur];
seg[cur << | ] += add[cur];
seg[cur << ] += add[cur];
add[cur] = ;
}
}
void pushUp(int cur) {
seg[cur] = max(seg[cur << ], seg[cur << | ]);
assert(seg[cur] >= );
}
void build(int L, int R, int cur) {
seg[cur] = add[cur] = ;
if (L == R) {
return;
}
int mid = (L + R) >> ;
build(lson);
build(rson);
pushUp(cur);
}
void upDate(int be, int en, LL val, int L, int R, int cur) {
if (L >= be && R <= en) {
seg[cur] += val;
add[cur] += val;
return;
}
pushDown(cur);
int mid = (L + R) >> ;
if (be <= mid) upDate(be, en, val, lson);
if (en > mid) upDate(be, en, val, rson);
pushUp(cur);
}
LL query(int be, int en, int L, int R, int cur) {
if (L >= be && R <= en) return seg[cur];
pushDown(cur);
LL ans = ;
int mid = (L + R) >> ;
if (be <= mid) ans += query(be, en, lson);
if (en > mid) ans += query(be, en, rson);
return ans;
}
void work() {
da.clear();
int n;
scanf("%d", &n);
da.push_back(-inf);
da.push_back(-inf);
da.push_back(-inf);
for (int i = ; i <= n; ++i) {
scanf("%d%d%I64d%I64d%I64d", &L[i], &R[i], &a[i], &b[i], &c[i]);
assert(L[i] <= R[i]);
if (L[i] > R[i]) swap(L[i], R[i]);
L[i] *= ;
R[i] *= ;
da.push_back(L[i]);
da.push_back(R[i]);
}
sort(da.begin(), da.end());
int gg = da.size();
for (int i = ; i < gg; ++i) {
if (da[i] - da[i - ] == ) {
da.push_back(da[i] - );
}
}
sort(da.begin(), da.end());
int mx = ;
for (int i = ; i <= n; ++i) {
L[i] = lower_bound(da.begin(), da.end(), L[i]) - da.begin();
R[i] = lower_bound(da.begin(), da.end(), R[i]) - da.begin();
// cout << L[i] << " " << R[i] << endl;
mx = max(mx, R[i] + );
}
// cout << endl;
build(root);
for (int i = ; i <= n; ++i) {
upDate(, L[i] - , c[i], root);
upDate(L[i], R[i], a[i], root);
upDate(R[i] + , mx, b[i], root);
}
cout << query(, mx, root) << endl;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
int t;
scanf("%d", &t);
while (t--) work();
return ;
}
原来是我离散化错了
一开始的时候,认为[70, 73] [74, 76]这样,73和74相邻,那么乘以2后,变成146 148,那么相隔2的时候才添加些元素进去,比如添加147进去隔着,这样离散化。
这样有bug(还没想到有什么bug)
现在的思路是:乘以2后,把L[i] + 1和R[i] + 1也放进去,这样就不会有挨着了
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
#define lson L, mid, cur << 1
#define rson mid + 1, R, cur << 1 | 1
#define root 1, mx, 1
const int maxn = + ;
int L[maxn], R[maxn];
int a[maxn], b[maxn], c[maxn];
LL add[maxn << ], seg[maxn << ];
vector<int>da;
void pushDown(int cur) {
if (add[cur]) {
add[cur << ] += add[cur];
add[cur << | ] += add[cur];
seg[cur << | ] += add[cur];
seg[cur << ] += add[cur];
add[cur] = ;
}
}
void pushUp(int cur) {
seg[cur] = max(seg[cur << ], seg[cur << | ]);
}
void build(int L, int R, int cur) {
seg[cur] = add[cur] = ;
if (L == R) {
return;
}
int mid = (L + R) >> ;
build(lson);
build(rson);
pushUp(cur);
}
void upDate(int be, int en, LL val, int L, int R, int cur) {
if (L >= be && R <= en) {
seg[cur] += val;
add[cur] += val;
return;
}
pushDown(cur);
int mid = (L + R) >> ;
if (be <= mid) upDate(be, en, val, lson);
if (en > mid) upDate(be, en, val, rson);
pushUp(cur);
}
void work() {
da.clear();
int n;
scanf("%d", &n);
da.push_back(-inf);
da.push_back(-inf);
da.push_back(-inf);
for (int i = ; i <= n; ++i) {
scanf("%d%d%d%d%d", &L[i], &R[i], &a[i], &b[i], &c[i]);
L[i] *= ;
R[i] *= ;
da.push_back(L[i]);
da.push_back(L[i] + );
da.push_back(R[i] + );
da.push_back(R[i]);
}
sort(da.begin(), da.end());
int mx = ;
for (int i = ; i <= n; ++i) {
L[i] = lower_bound(da.begin(), da.end(), L[i]) - da.begin();
R[i] = lower_bound(da.begin(), da.end(), R[i]) - da.begin();
// cout << L[i] << " " << R[i] << endl;
mx = max(mx, R[i] + );
}
// cout << endl;
build(root);
for (int i = ; i <= n; ++i) {
upDate(, L[i] - , c[i], root);
upDate(L[i], R[i], a[i], root);
upDate(R[i] + , mx, b[i], root);
}
printf("%I64d\n", seg[]);
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
int t;
scanf("%d", &t);
while (t--) work();
return ;
}
现在的方法是直接
hdu 6012 Lotus and Horticulture 打标记的更多相关文章
- HDU 6012 Lotus and Horticulture(离散化)
题目代号:HDU 6012 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6012 Lotus and Horticulture Time Limit: ...
- 【HDU】6012 Lotus and Horticulture (BC#91 T2)
[算法]离散化 [题解] 答案一定存在于区间的左右端点.与区间左右端点距离0.5的点上 于是把所有坐标扩大一倍,排序(即离散化). 让某个点的前缀和表示该点的答案. 初始sum=∑c[i] 在l[i] ...
- Lotus and Horticulture
Lotus and Horticulture Accepts: 91 Submissions: 641 Time Limit: 4000/2000 MS (Java/Others) Memory Li ...
- BestCoder Round #91 1002 Lotus and Horticulture
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6012 题意: 这几天Lotus对培养盆栽很感兴趣,于是她想搭建一个温室来满足她的研究欲望. Lotus ...
- HUD-2112 HDU Today(最短路map标记)
题目链接:HUD-2112 HDU Today 思路: 1.最短路spfa模板. 2.map标记建图. 3.考虑距离为0或者-1的情况. 总结:下次map记得清空orz. AC代码: #include ...
- HDU 2846 Repository(字典树,标记)
题目 字典树,注意初始化的位置~!!位置放错,永远也到不了终点了org.... 我是用数组模拟的字典树,这就要注意内存开多少了,,要开的不大不小刚刚好真的不容易啊.... 我用了val来标记是否是同一 ...
- hdu 6011 Lotus and Characters 贪心
http://acm.hdu.edu.cn/showproblem.php?pid=6011 先把数字从小到大排好,比如是-6.3.4这样, 然后处理出后缀和,当后缀和 <= 0的时候马上停止就 ...
- Transformation HDU - 4578(线段树——懒惰标记的妙用)
Yuanfang is puzzled with the question below: There are n integers, a 1, a 2, …, a n. The initial val ...
- HDU 3397 线段树 双懒惰标记
这个是去年遗留历史问题,之前思路混乱,搞了好多发都是WA,就没做了 自从上次做了大白书上那个双重懒惰标记的题目,做这个就思路很清晰了 跟上次大白上那个差不多,这个也是有一个sets标记,代表这个区间全 ...
随机推荐
- webpack-Modules(模块)
模块(Modules) 在模块化编程中,开发者将程序分解成离散功能块(discrete chunks of functionality),并称之为模块. 每个模块具有比完整程序更小的接触面,使得校验. ...
- vue 自定义报警组件
1.自定义报警组件 Alarm.vue <!-- 报警 组件 --> <template> <div class="alarm"> <!- ...
- ansible学习之--简单学习笔记1
1.利用dm-crypt来创建加密文件系统.编写shell脚本(安装和卸载两个shell脚本) 2.编写ansible,playbook文件 3.编写python脚本 首先编写shell脚本 inst ...
- cmake使用演示样例与整理总结
本文代码托管于github cmake_demo cmake中一些提前定义变量 PROJECT_SOURCE_DIR project的根文件夹 PROJECT_BINARY_DIR 执行cmake命 ...
- What to do about Eclipse's “No repository found containing: …” error messages?
As Mauro said: "you have to remove and re-add the Eclipse Project Update site, so that its meta ...
- VS2010调用外部webservice
vs2010怎么调用web服务webservice方法,以vs2010为例.Vs的各个版本的此项功能操作基本一致. 工具/原料 vs2010 在“服务引用设置”对话框中,单击“添加 Web 引用”. ...
- YTU 2851: 数字游戏
2851: 数字游戏 时间限制: 1 Sec 内存限制: 128 MB 提交: 164 解决: 85 题目描述 输入若干个正整数,将其中能写成其它两个正整数的平方和的数输出来. 例,若输入的数中有 ...
- sqlserver游标使用误区
在使用游标出现逻辑错误时,查阅资料,只有改掉while中的游标取值在while循环最后,没有说明while以前会出现的错误,而且没有具体说明原因,今天在工作中解决了这个问题,写了这个博客,希望对使用游 ...
- 主机与虚拟机互ping
1.设置虚拟机网络连接方式(如下图): 2.设置主机和虚拟机的ip为同一个网段,如主机ip:192.168.28.1,虚拟机ip:192.168.28.128 3.如果相互还是ping不通,就检查一下 ...
- 初识Spring Boot框架和快速入门
前面的铺垫文章已经连着写了六篇了,主要是介绍了spring和SpringMVC框架,小伙伴们在学习的过程中大概也发现了这两个框架需要我们手动配置的地方非常多,不过做JavaEE开发的小伙伴们肯定也听说 ...