做了这么多扫描线的题,,基本都是一个思路。

改来改去,,无非就是维护的节点的内容以及push_up越写越复杂了而已

首先将价格排序处理一下编号,变成编号越大的powerfol越大

然后后面加入扫描线的时候将旧编号直接转换成新编号即可了

对于这题

S[rt][i]维护的是。rt节点相应的区间中品种为i的长度

S[rt][i]维护的是。rt节点相应的区间的品种为i出现的次数

那么,假设出现了cnt[rt][3],就将S[rt][3]赋值为区间长度,S[rt][1]和S[rt][2]都赋值为0,由于已经有3覆盖了整个区间,那么另外两种已经不须要考虑了,或者说成,已经被3遮挡了

假设出现了cnt[rt][2],首先S[rt][3]应该继承左右的子树,S[rt][2]应该是整个的区间长度减去S[rt][3]的长度,由于那一部分的种类是3,2是覆盖不了3的。须要保留下来,然后让S[rt][1]变成0,由于2可以覆盖3,假设出现cnt[rt][1],S[rt][3]和S[rt][2]都继承左右子树的,S[rt][1]的长度应该是区间长度减去S[rt][2]和S[rt][3]。

假设没有出现cnt,就看是否是l==r的情况。这样的情况下就没有子树了。。就所有赋值为0直接返回,否则。就所有继承左右子树的

以后遇到这样的题。每次仅仅须要考虑push_up就好了,,仅仅是有时候确实维护的方法非常难想到..

#include<map>
#include<set>
#include<cmath>
#include<stack>
#include<queue>
#include<cstdio>
#include<string>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional> using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define root 1,rear,1 int const MX = 1e5 + 5; int m, ID[10];
int rear, cnt[MX << 2][4];
int A[MX << 1], S[MX << 2][4]; struct Que {
int d, s;
int top, L, R;
Que() {}
Que(int _top, int _L, int _R, int _d, int _s) {
top = _top; L = _L; R = _R; d = _d; s = _s;
}
bool operator<(const Que &b)const {
if(top == b.top) return d < b.d;
return top < b.top;
}
} Q[MX << 1]; struct Price {
int id, money;
bool operator<(const Price &b)const {
return money < b.money;
}
} price[10]; int BS(int x) {
int L = 1, R = rear, m;
while(L <= R) {
m = (L + R) >> 1;
if(A[m] == x) return m;
if(A[m] > x) R = m - 1;
else L = m + 1;
}
return -1;
} void push_up(int l, int r, int rt) {
if(cnt[rt][3]) {
S[rt][1] = S[rt][2] = 0;
S[rt][3] = A[r + 1] - A[l];
} else if(cnt[rt][2]) {
S[rt][3] = S[rt << 1][3] + S[rt << 1 | 1][3];
S[rt][2] = A[r + 1] - A[l] - S[rt][3];
S[rt][1] = 0;
} else if(cnt[rt][1]) {
S[rt][3] = S[rt << 1][3] + S[rt << 1 | 1][3];
S[rt][2] = S[rt << 1][2] + S[rt << 1 | 1][2];
S[rt][1] = A[r + 1] - A[l] - S[rt][3] - S[rt][2];
} else if(l == r) S[rt][1] = S[rt][2] = S[rt][3] = 0;
else {
S[rt][1] = S[rt << 1][1] + S[rt << 1 | 1][1];
S[rt][2] = S[rt << 1][2] + S[rt << 1 | 1][2];
S[rt][3] = S[rt << 1][3] + S[rt << 1 | 1][3];
}
} void update(int L, int R, int d, int s, int l, int r, int rt) {
if(L <= l && r <= R) {
cnt[rt][s] += d;
push_up(l, r, rt);
return;
} int m = (l + r) >> 1;
if(L <= m) update(L, R, d, s, lson);
if(R > m) update(L, R, d, s, rson);
push_up(l, r, rt);
} int main() {
//freopen("input.txt", "r", stdin);
int T, n, ansk = 0;
scanf("%d", &T);
while(T--) {
rear = 0;
memset(cnt, 0, sizeof(cnt));
memset(S, 0, sizeof(S)); scanf("%d%d", &n, &m);
for(int i = 1; i <= m; i++) {
scanf("%d", &price[i].money);
price[i].id = i;
}
sort(price + 1, price + m + 1);
for(int i = 1; i <= m; i++) {
ID[price[i].id] = i;
} for(int i = 1; i <= n; i++) {
int x1, y1, x2, y2, s;
scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &s);
Q[i] = Que(y1, x1, x2, 1, s);
Q[i + n] = Que(y2, x1, x2, -1, s); A[++rear] = x1; A[++rear] = x2;
}
sort(Q + 1, Q + 1 + 2 * n);
sort(A + 1, A + 1 + rear);
rear = unique(A + 1, A + 1 + rear) - A - 1; LL ans = 0; int last = 0;
for(int i = 1; i <= 2 * n; i++) {
int sum = 0;
for(int j = 1; j <= m; j++) {
sum += price[j].money * S[1][j];
}
ans += (LL)(Q[i].top - last) * sum;
update(BS(Q[i].L), BS(Q[i].R) - 1, Q[i].d, ID[Q[i].s], root);
last = Q[i].top;
}
printf("Case %d: %I64d\n", ++ansk, ans);
}
return 0;
}

线段树 hdu3255 Farming的更多相关文章

  1. hdu3255 线段树扫描线求体积

    题意:       给你n个矩形,每个矩形上都有一个权值(该矩形单位面积的价值),矩形之间可能重叠,重叠部分的权值按照最大的算,最后问这n个矩形组成的图形的最大价值. 思路:       线段树扫描线 ...

  2. 线段树总结 (转载 里面有扫描线类 还有NotOnlySuccess线段树大神的地址)

    转载自:http://blog.csdn.net/shiqi_614/article/details/8228102 之前做了些线段树相关的题目,开学一段时间后,想着把它整理下,完成了大牛NotOnl ...

  3. [转载]完全版线段树 by notonlysuccess大牛

    原文出处:http://www.notonlysuccess.com/ (好像现在这个博客已经挂掉了,在网上找到的全部都是转载) 今天在清北学堂听课,听到了一些很令人吃惊的消息.至于这消息具体是啥,等 ...

  4. 【转】线段树完全版~by NotOnlySuccess

    线段树完全版  ~by NotOnlySuccess 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文章了,觉 ...

  5. 《完全版线段树》——notonlysuccess

    转载自:NotOnlySuccess的博客 [完全版]线段树 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文 ...

  6. 【转】 线段树完全版 ~by NotOnlySuccess

    载自:NotOnlySuccess的博客 [完全版]线段树 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文章 ...

  7. 【转载】完全版线段树 by notonlysuccess大牛

    原文出处:http://www.notonlysuccess.com/ 今晚上比赛就考到了 排兵布阵啊,难受. [完全版]线段树 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时 ...

  8. bzoj3932--可持久化线段树

    题目大意: 最近实验室正在为其管理的超级计算机编制一套任务管理系统,而你被安排完成其中的查询部分.超级计算机中的 任务用三元组(Si,Ei,Pi)描述,(Si,Ei,Pi)表示任务从第Si秒开始,在第 ...

  9. codevs 1082 线段树练习 3(区间维护)

    codevs 1082 线段树练习 3  时间限制: 3 s  空间限制: 128000 KB  题目等级 : 大师 Master 题目描述 Description 给你N个数,有两种操作: 1:给区 ...

随机推荐

  1. TensorFlow-Gpu环境搭建——Win10+ Python+Anaconda+cuda

    参考:http://blog.csdn.net/sb19931201/article/details/53648615 https://segmentfault.com/a/1190000009803 ...

  2. IOS7 APP 升级的10个TIP 建议

    There is no way to preserve the iOS 6 style status bar layout. The status bar will always overlap yo ...

  3. PHP 之微信小程序支付封装

    <?php /** * Created by PhpStorm. * User: yangs * Date: 2019/4/26 * Time: 14:28 */ class WeixinPay ...

  4. Java数组数据类型

    Java数组数据类型 数组是多个相同类型的数据的组合,数组中的元素可以是任何类型的数据: 一维数组 package com.ahabest.array; public class ArratTest ...

  5. linux中快速查找文件

    在使用linux时,经常需要进行文件查找.其中查找的命令主要有find和grep.两个命令是有区的. 区别:(1)find命令是根据文件的属性进行查找,如文件名,文件大小,所有者,所属组,是否为空,访 ...

  6. demo__webpack

    webpack 中使用的包更新非常频繁,使用方式可能很快就会改变,解决方式 看webapck文档 和 包的使用文档 看包的源码 其他... 环境 win10 + webstorm 2019.1.3 + ...

  7. iptables详解(4):iptables匹配条件总结之一

    所属分类:IPtables  Linux基础 在本博客中,从理论到实践,系统的介绍了iptables,如果你想要从头开始了解iptables,可以查看iptables文章列表,直达链接如下 iptab ...

  8. pandas格式化str为时间,pandas将int转化为str

    code_300['HISTORY_DATE'] = code_300['HISTORY_DATE'].map(str)code_300['HISTORY_DATE'] = pd.to_datetim ...

  9. 【VIP视频网站项目】VIP视频网站项目v1.0.3版本发布啦(程序一键安装+电影后台自动抓取+代码结构调整)

    在线体验地址:http://vip.52tech.tech/ GIthub源码:https://github.com/xiugangzhang/vip.github.io 项目预览 主页面 登录页面 ...

  10. MySQL之索引以及正确使用索引

    一.MySQL中常见索引类型 普通索引:仅加速查询 主键索引:加速查询.列值唯一.表中只有一个(不可有null) 唯一索引:加速查询.列值唯一(可以有null) 组合索引:多列值组成一个索引,专门用于 ...