博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
BZOJ 4094 USACO 2013 Dec. Optimal Milking
阅读量:4677 次
发布时间:2019-06-09

本文共 1456 字,大约阅读时间需要 4 分钟。

 

线段树

每个节点保存4个值,both表示左右端点都取,neither表示左右端点都不取,left表示只取左端点,right表示只取右端点。

维护的特殊姿势:

$cur$的$both=max(ls.l+rs.r,ls.both+rs.r,ls.l+rs.both)$

$cur$的$neither=max(ls.nei+rs.nei,ls.r+rs.nei,ls.nei+rs.l)$

$cur$的$left=max(ls.l+rs.l,ls.l+rs.nei,ls.both+ls.nei)$

$cur$的$right=max(ls.r+rs.r,ls.nei+rs.r,ls.nei+rs.both)$

1 #include
2 #include
3 #define LL long long 4 #define ls (cur<<1) 5 #define rs (cur<<1|1) 6 #define mid ((l+r)>>1) 7 using namespace std; 8 const int maxn=800010; 9 LL n,m,ans;10 struct tree{11 LL both,l,r,nei;12 }a[maxn];13 void read(LL &k){14 k=0; int f=1; char c=getchar();15 while (c<'0'||c>'9')c=='-'&&(f=-1),c=getchar();16 while ('0'<=c&&c<='9')k=k*10+c-'0',c=getchar();17 k*=f;18 }19 void pushup(int cur,int l,int r){20 if (l==r) return;21 int t1=a[ls].both+a[rs].r,t2=a[ls].l+a[rs].both,t3=a[ls].l+a[rs].r, //both22 t4=a[ls].l+a[rs].l,t5=a[ls].l+a[rs].nei,t6=a[ls].both+a[rs].nei, //left23 t7=a[ls].r+a[rs].r,t8=a[ls].nei+a[rs].r,t9=a[ls].nei+a[rs].both, //right24 t10=a[ls].nei+a[rs].nei,t11=a[ls].nei+a[rs].l,t12=a[ls].r+a[rs].nei; //neither25 a[cur].both=max(max(t1,t2),t3);26 a[cur].l=max(max(t4,t5),t6);27 a[cur].r=max(max(t7,t8),t9);28 a[cur].nei=max(max(t10,t11),t12);29 }30 void build(int cur,int l,int r){31 a[cur].l=0; a[cur].r=0; a[cur].nei=0;32 if (l
View Code

 

转载于:https://www.cnblogs.com/DriverLao/p/7690700.html

你可能感兴趣的文章
SQL Server 重置Identity标识列的值(INT爆了)
查看>>
如何将Android Studio项目提交(更新)到github
查看>>
DB2 Error
查看>>
【bzoj 3669】[Noi2014]魔法森林
查看>>
第二章寄存器总结
查看>>
辗转相除法的原理
查看>>
C Primer Plus note7
查看>>
shell 常用命令
查看>>
How to show only next line after the matched one?
查看>>
手续费
查看>>
yii2框架随笔19
查看>>
为什么要使用getter/setter
查看>>
使用7zip把jre集成到绿色运行程序内
查看>>
07_Python的控制判断循环语句1(if判断for循环)_Python编程之路
查看>>
15_Python模块化编程_Python编程之路
查看>>
【leetcode 简单】第十七题 x 的平方根
查看>>
cocos2d-x 3.1 编译脚本android-build.py
查看>>
Java web servers 间是如何实现 session 同步的
查看>>
HDU 6319(单调队列)
查看>>
Codeforces 1041C(贪心+set)
查看>>