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 打标记的更多相关文章

  1. HDU 6012 Lotus and Horticulture(离散化)

    题目代号:HDU 6012 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6012 Lotus and Horticulture Time Limit: ...

  2. 【HDU】6012 Lotus and Horticulture (BC#91 T2)

    [算法]离散化 [题解] 答案一定存在于区间的左右端点.与区间左右端点距离0.5的点上 于是把所有坐标扩大一倍,排序(即离散化). 让某个点的前缀和表示该点的答案. 初始sum=∑c[i] 在l[i] ...

  3. Lotus and Horticulture

    Lotus and Horticulture Accepts: 91 Submissions: 641 Time Limit: 4000/2000 MS (Java/Others) Memory Li ...

  4. BestCoder Round #91 1002 Lotus and Horticulture

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6012 题意: 这几天Lotus对培养盆栽很感兴趣,于是她想搭建一个温室来满足她的研究欲望. Lotus ...

  5. HUD-2112 HDU Today(最短路map标记)

    题目链接:HUD-2112 HDU Today 思路: 1.最短路spfa模板. 2.map标记建图. 3.考虑距离为0或者-1的情况. 总结:下次map记得清空orz. AC代码: #include ...

  6. HDU 2846 Repository(字典树,标记)

    题目 字典树,注意初始化的位置~!!位置放错,永远也到不了终点了org.... 我是用数组模拟的字典树,这就要注意内存开多少了,,要开的不大不小刚刚好真的不容易啊.... 我用了val来标记是否是同一 ...

  7. hdu 6011 Lotus and Characters 贪心

    http://acm.hdu.edu.cn/showproblem.php?pid=6011 先把数字从小到大排好,比如是-6.3.4这样, 然后处理出后缀和,当后缀和 <= 0的时候马上停止就 ...

  8. Transformation HDU - 4578(线段树——懒惰标记的妙用)

    Yuanfang is puzzled with the question below: There are n integers, a 1, a 2, …, a n. The initial val ...

  9. HDU 3397 线段树 双懒惰标记

    这个是去年遗留历史问题,之前思路混乱,搞了好多发都是WA,就没做了 自从上次做了大白书上那个双重懒惰标记的题目,做这个就思路很清晰了 跟上次大白上那个差不多,这个也是有一个sets标记,代表这个区间全 ...

随机推荐

  1. Mybatis加入Ehcache支持

    1.Mybatis默认的缓存配置 MyBatis 包括一个很强大的查询缓存特性,它能够很方便地配置和定制. Mybatis缓存包括全局的缓存和局部的缓存.全局的缓存能够讲主配置文件的setting属性 ...

  2. Unity Critter地图导出到server配置

    普通情况下,从Critter导出的地图会与Unity自带的Navigation洪培出的地图会有比較大的差异.须要耐心调整Critter的參数才干够. 以下是我调的參数,与Unity导出的地图基本相似. ...

  3. A7139 无线通信驱动(STM32) 添加FIFO扩展模式,能够发送超大数据包

    A7139 拥有电磁波唤醒以及10mW的发射功率,很easy实现长距离通信,眼下測试有障碍物能够轻松达到300m以上. 通过几天的调试,眼下能够发送随意大小的数据包,大小为1-16KB.所有使用中断收 ...

  4. centos7 64位系统jdbc连接oracle报错问题

    这两天发生了一个错误,记录下来. 报错如下: ### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could n ...

  5. uml时序图的初印象-------Day64

    近期有好多想法迫不及待的想去实现,但是其实是在那些最開始想的很明确,感觉会没问题的地方也总是会出现故障,导致稍微有些急躁,还是要淡定啊.又到了周末.明后天要收拾东西搬家,不知道宽带能不能顺利的给挪过去 ...

  6. ffmpeg+libx264+facc交叉编译 实现264流录制avi文件

    默认交叉编译器已经搭建好. .. . . . . .... .... 需求: 把标准的h264流保存成avi格式的实现文件,所以须要h264的解码器,avi文件容器,传输协议类型是文件.这些会体如今f ...

  7. ABAP doi 导出程序

    PERFORM FRM_OUTPUT_EXCEL_38 TABLES GT_RESULT USING 'ZHRPA038' LDF_PATH . FORM FRM_OUTPUT_EXCEL_38 TA ...

  8. YTU 2887: D--机器人Bill

    2887: D--机器人Bill 时间限制: 1 Sec  内存限制: 128 MB 提交: 112  解决: 22 题目描述 最近,Dr. Jiang 新设计一个机器人Bill.Bill很聪明,会做 ...

  9. Easier SQL with Cupboard

    Overview Cupboard is a way to manage persistence in a sqlite instance for your app. It was written b ...

  10. ConfigParser模块用法

    ConfigParser 是Python自带的模块, 用来读写配置文件, 用法及其简单. 直接上代码,不解释,不多说. 配置文件的格式是: []包含的叫section,    section 下有op ...