线段树 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:给区 ...
随机推荐
- IIS Express配置多站点同时运行
环境:Win10 Pro.Visual Studio 2015 Community.IIS Express 10 VS2015集成IIS Express,所以无需单独下载, 默认安装位置:C:\Pro ...
- python编辑csv
import csv import time def timestamp_to_timestr(timeStamp): timeArray = time.localtime(int(timeStamp ...
- selenium的三种等待时间
//隐式等待(20秒以内没哥一段时间就会去找元素,如果没找大也不会报错,过了20s才会报错) //driver.manage().timeouts().implicitlyWait(20, TimeU ...
- cookie和sessionStorage 、localStorage 对比
相同点:都存储在客户端 不同点:1.存储大小 cookie数据大小不能超过4k. sessionStorage和localStorage 虽然也有存储大小的限制,但比cookie大得多,可以达到5M或 ...
- spring boot 2.0 报错:“jdbcUrl is required with driverClassName.” 解决办法!
springboot 升级到2.0之后发现配置多数据源的时候报错: "jdbcUrl is required with driverClassName."或者Cause: java ...
- windows程序设为开机自启动
在Windows文件管理器中输入 %APPDATA%\Microsoft\Windows\Start Menu\Programs\Startup 把程序快捷方式放到此处即可.
- BZOJ 2096: [Poi2010]Pilots 单调队列
Code: #include<bits/stdc++.h> #define maxn 4000000 using namespace std; void setIO(string s) { ...
- 使用final关键字修饰一个引用类型变量时,是引用不能变,还是引用的对象不能变?
使用final关键字修饰一个引用类型变量时,是指引用变量不能变,引用变量所指向的对象中的内容还是可以改变的. 测试代码如下: package reviewTest; /** * @ClassName: ...
- Python模块 os.walk
Os.walk os.walk(top,topdown=True,onerror=None,followlinks=False) os.walk()是python中内置(built-in)的目录树生成 ...
- BZOJ 1232 USACO 2008 Nov. 安慰奶牛Cheer
[题解] 对于每一条边,我们通过它需要花费的代价是边权的两倍加上这条边两个端点的点权. 我们把每条边的边权设为上述的值,然后跑一边最小生成树,再把答案加上最小的点权就好了. #include<c ...