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标记,代表这个区间全 ...
随机推荐
- SpringMVC+Hibernate+Junit4+json基本框架近乎0配置
公司是做APP开发的,须要后台来提供接口,于是乎,这个任务就交给我,经过重复的尝试,学习和參考别人的demo,最终搭出自己还算惬意的框架.SpringMVC+Sping3+Hibernate4+Jun ...
- 3.将maven项目jar纳入maven仓库,Mave项目依赖另外一个Maven项目的案例
1 若想让maven项目依赖另外一个maven项目.被依赖的项目要在maven仓库中有对应的jar包,所以要对依赖的项目运行mvninstall命令. 2 新建第二个项目模块HelloFrien ...
- 迅雷CTO李金波:致创业者的一封信
我的创业感悟:写给正在寻找机会的你 李金波 我在迅雷的6年里,经历了许多困难.最折磨人的,是寻找人才:最惋惜的,莫过于看着优秀的人擦肩而过.今天再次创业(http://myhada.com),再次招聘 ...
- Selenium系列之--04 常见元素操作总结
一.Selenium总共有八种定位方法 By.id() 通过id定位 By.name() 通过name 定位 By.xpath() 通过xpath定位 By.className() 通过clas ...
- java8--集合(疯狂java讲义3复习笔记)
1.集合分四类:set,map,list,queue 位于java.util包下. 集合类和数组的区别,数组可以保存基本类型的值或者是对象的引用,而集合里只能保存对象的引用. 集合类主要由两个接口派生 ...
- Java中抽象类和接口的区别?
深入理解Java的接口和抽象类 对于面向对象编程来说,抽象是它的一大特征之一.在Java中,可以通过两种形式来体现OOP的抽象:接口和抽象类.这两者有太多相似的地方,又有太多不同的地方.很多人在初学的 ...
- The type exists in both DLLs
2>C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\c0b37647\aaceda91\Ap ...
- regular expression 在线检测的网站
http://regexone.com/ 学习网站 http://regexone.com/lesson/optional_characters? http://regexone.com/less ...
- HEOI2016 树
传送门 这道题还是很简单的,可以树剖,然后还有看大佬暴力模拟AC的????!! 我们就执行俩操作,一个是单点修改,这个随便修,然后就是查询一个点,离他最近的被打过标记过的祖先.这个可以这么想,我们先q ...
- 【OpenFOAM】——OpenFOAM入门算例学习
1 明确目标——为啥费老大劲儿学习OpenFOAM 学习OpenFOAM主要出于课题需要,希望实现以下几个目标: l [ ]学会用SnappyHexMesh生成高质量网格: l [ ]学习使用O ...