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. Deepin-安装php

    点击即可:下载:PHP5.6 下载完后执行下面这条shell命令即可完成安装 文件保存为:php_install.sh ,运行时:sh php_install.sh .tar.bz2 cd php- ...

  2. es5~es6

    1.用箭头函数减少代码(相信你在Vue已经看到了) ES5: function greetings (name) { return 'hello ' + name } ES6: const greet ...

  3. libevent编程疑难解答

    http://blog.csdn.net/luotuo44/article/details/39547391 转载请注明出处:http://blog.csdn.net/luotuo44/article ...

  4. win7下装ubuntu双系统后无法进入win7的解决方法

    本来电脑的系统是win7,然后用u盘装了ubuntu之后可能会出现开机没有引导界面而直接进入ubuntu系统的情况. 原因:没有设置gurb引导 解决方法:需要更新gurb来使ubuntu识别出win ...

  5. 2 AngularJS 1 概念浓缩

    Angular Web APP 结构图: module   --> 模块     :相当于一个容器,Angular里的所有东西都得放在模块里,才能够被引用和加载. directive  --&g ...

  6. JavaScript操作符(关系操作符、相等操作符和条件操作符)

    关系操作符用于对两个值进行比较,返回一个布尔值.关系操作符包括大于(>),小于(<),大于等于(>=),小于等于(<=).当关系操作符用于非数值时,也要先进行数值的转换.如 v ...

  7. 10601 - Cubes(Ploya)

    UVA 10601 - Cubes 题目链接 题意:给定正方体12条棱的颜色,要求用这些棱能组成多少不同的正方体 思路:利用ploya定理去求解,分类讨论,正方体一共24种旋转.相应的旋转方式有4种: ...

  8. ALLOWED_HOSTS = ['*']

    https://docs.djangoproject.com/en/1.10/ref/settings/#std:setting-ALLOWED_HOSTS https://stackoverflow ...

  9. windows下的java+maven项目环境搭建

    年底了,进公司刚好半年,于是全新一轮的挑战开始:让我接触java项目了,真的是全新的,完全一片茫然.经过了半个月的折腾,把环境搭了一遍又一遍,还可以小小的改一下程序,还OK啦~继续努力.接下来,进入正 ...

  10. 在ubuntu18.0下安装qt4.7以及qt-creator安装过程中遇到的坑

    最近的嵌入式Linux系统上要做课程设计= =要用贼老贼老的qt4.7,配环境踩坑都费了我1天时间.....所以记录下来,希望能给和我遇到相同问题的朋友一点帮助 apt-get install g++ ...