线段树 hdu3255 Farming
做了这么多扫描线的题,,基本都是一个思路。
改来改去,,无非就是维护的节点的内容以及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的更多相关文章
- hdu3255 线段树扫描线求体积
题意: 给你n个矩形,每个矩形上都有一个权值(该矩形单位面积的价值),矩形之间可能重叠,重叠部分的权值按照最大的算,最后问这n个矩形组成的图形的最大价值. 思路: 线段树扫描线 ...
- 线段树总结 (转载 里面有扫描线类 还有NotOnlySuccess线段树大神的地址)
转载自:http://blog.csdn.net/shiqi_614/article/details/8228102 之前做了些线段树相关的题目,开学一段时间后,想着把它整理下,完成了大牛NotOnl ...
- [转载]完全版线段树 by notonlysuccess大牛
原文出处:http://www.notonlysuccess.com/ (好像现在这个博客已经挂掉了,在网上找到的全部都是转载) 今天在清北学堂听课,听到了一些很令人吃惊的消息.至于这消息具体是啥,等 ...
- 【转】线段树完全版~by NotOnlySuccess
线段树完全版 ~by NotOnlySuccess 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文章了,觉 ...
- 《完全版线段树》——notonlysuccess
转载自:NotOnlySuccess的博客 [完全版]线段树 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文 ...
- 【转】 线段树完全版 ~by NotOnlySuccess
载自:NotOnlySuccess的博客 [完全版]线段树 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文章 ...
- 【转载】完全版线段树 by notonlysuccess大牛
原文出处:http://www.notonlysuccess.com/ 今晚上比赛就考到了 排兵布阵啊,难受. [完全版]线段树 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时 ...
- bzoj3932--可持久化线段树
题目大意: 最近实验室正在为其管理的超级计算机编制一套任务管理系统,而你被安排完成其中的查询部分.超级计算机中的 任务用三元组(Si,Ei,Pi)描述,(Si,Ei,Pi)表示任务从第Si秒开始,在第 ...
- codevs 1082 线段树练习 3(区间维护)
codevs 1082 线段树练习 3 时间限制: 3 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 给你N个数,有两种操作: 1:给区 ...
随机推荐
- Android 使用MySQL直接访问数据库
在实际项目中,一般很少直接访问MySQL数据库,一般情况下会通过http请求将数据传送到服务端,然后在服务端连接mysql数据库. 在android 中,会通过使用Jdbc 连接MySQL 服务器 p ...
- #2028 Lowest Common Multiple Plus
http://acm.hdu.edu.cn/showproblem.php?pid=2028 应该是比较简单的一道题啊...求输入的数的最小公倍数. 先用百度来的(老师教的已经不知道跑哪去了)辗转相除 ...
- (转) 分布式文件存储FastDFS(一)初识FastDFS
http://blog.csdn.net/xingjiarong/article/details/50559849 一.FastDFS简介 FastDFS是一款开源的.分布式文件系统(Distribu ...
- HDU_Reward_拓扑排序
Reward Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- linux修改hosts配置
参考 https://blog.csdn.net/qq_15192373/article/details/81093542 1. terminal中输入: sudo gedit /etc/hosts ...
- windows上关闭Nagle算法
下面的设置可以调整或禁用 nagel 算法.禁用 nagel 算法以后, 允许很小的包没有延迟立即发送.建议对某些游戏关闭 nagel 算法, 这样做对文件传输/吞吐量有负面影响.默认状态( 开启na ...
- 从 UI 交互角度说语音识别产品
语言是人类进化的主要特征,而人工智能拥有了说话的能力也是科技进步的一个特征.在很多科幻的电影里面,我们可以看到人工智能的身影.在电影 her 里面见到的人工智能,真的让人叹为观止,他可以随意的和你聊天 ...
- react 父组件调用子组件方法
import React from 'react'import '../page1/header.css'import { Table } from 'antd'import Child from ' ...
- Oracle最大游标数控制
/************************************************************************ ********* Oracle最大游标数控制 ** ...
- 【VIP视频网站项目三】项目框架搭建、项目路由配置、数据库表结构设计
一.项目路由的设计 目前项目代码已经全部开源:项目地址:https://github.com/xiugangzhang/vip.github.io 视频网站前台页面路由设计 路由 请求方法 模板 作用 ...