HDU 3911 Black And White (线段树区间更新)
来源:互联网 发布:淘宝动态评分在哪里看 编辑:程序博客网 时间:2024/05/14 12:40
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3911
题面:
Black And White
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4358 Accepted Submission(s): 1271
Problem Description
There are a bunch of stones on the beach; Stone color is white or black. Little Sheep has a magic brush, she can change the color of a continuous stone, black to white, white to black. Little Sheep like black very much, so she want to know the longest period of consecutive black stones in a range [i, j].
Input
There are multiple cases, the first line of each case is an integer n(1<= n <= 10^5), followed by n integer 1 or 0(1 indicates black stone and 0 indicates white stone), then is an integer M(1<=M<=10^5) followed by M operations formatted as x i j(x = 0 or 1) , x=1 means change the color of stones in range[i,j], and x=0 means ask the longest period of consecutive black stones in range[i,j]
Output
When x=0 output a number means the longest length of black stones in range [i,j].
Sample Input
41 0 1 050 1 41 2 30 1 41 3 30 4 4
Sample Output
120
Source
2011 Multi-University Training Contest 8 - Host by HUST
题目大意:
初始给定一01序列,两种操作,1 a b,取反a到b间的元素,0 a b,问a b区间内最长的连续1的长度。
解题:
第一颗区间更新的线段树,来得有点晚啊,借鉴、理解也算是写出来了。题意看似简单,合并操作比较复杂。需要维护5个值。maxw,maxb,le,ri,lazy。
maxw/maxb,该区间的最长连续0/1长度,取反时直接交换两值。
le/ri,该区间的左右连续块长度,用于合并。
lazy该区间是否需要继续向下更新。
代码:
#include <iostream>#include <cmath>#include <cstdio>#define siz 100010//左右子树编号#define ls i<<1#define rs (i<<1)|1using namespace std;//左右边界,该区间最多连续黑块数,白块数,因为反转区间需要//区间左边界连续块数,右边解连续块数,正表黑,负表白,用于区间合并//lazy标记如果为1,代表更新到该区间,其下区间尚未更新struct SEGTree{int l,r,maxb,maxw,le,ri,lazy;}STree[siz<<2];int a[siz];//取大int max(int x,int y){return x>y?x:y;}//取小int min(int a,int b){ return a<b?a:b;}//向上更新void push_up(int i){ int tmp; //取左右区间最大黑块数 int maxb=max(STree[ls].maxb,STree[rs].maxb); //如果可以合并的话,将合并的值一同比较 if(STree[ls].ri>0&&STree[rs].le>0) { tmp=STree[ls].ri+STree[rs].le; if(tmp>maxb) maxb=tmp; } //更新 STree[i].maxb=maxb; //白块更新同上 int maxw=max(STree[ls].maxw,STree[rs].maxw); if(STree[ls].ri<0&&STree[rs].le<0) { tmp=STree[ls].ri+STree[rs].le; tmp=-tmp; if(tmp>maxw) maxw=tmp; } STree[i].maxw=maxw; //区间左端最长连续块数le,如果左子区单色,且能与右子区相连,更新le if(abs(STree[ls].le)==(STree[ls].r-STree[ls].l+1)&&(STree[ls].ri*STree[rs].le>0)) STree[i].le=STree[ls].le+STree[rs].le; //否则直接取左子区le else STree[i].le=STree[ls].le; //更新ri,同上 if(abs(STree[rs].ri)==(STree[rs].r-STree[rs].l+1)&&(STree[ls].ri*STree[rs].le>0)) STree[i].ri=STree[ls].ri+STree[rs].ri; else STree[i].ri=STree[rs].ri;}//向下更新void push_down(int i){//该区间的懒标记清零STree[i].lazy=0;//更新左右子区间swap(STree[ls].maxw,STree[ls].maxb);STree[ls].le=-STree[ls].le;STree[ls].ri=-STree[ls].ri;STree[ls].lazy=!STree[ls].lazy;swap(STree[rs].maxw,STree[rs].maxb);STree[rs].le=-STree[rs].le;STree[rs].ri=-STree[rs].ri;STree[rs].lazy=!STree[rs].lazy;}//建树void build(int i,int l,int r){STree[i].l=l;STree[i].r=r;STree[i].lazy=0;//叶子节点if(l==r){//如果是黑块 if(a[l]) { STree[i].maxb=1; STree[i].maxw=0; STree[i].le=1; STree[i].ri=1; } //白块 else { STree[i].maxw=1; STree[i].maxb=0; STree[i].le=-1; STree[i].ri=-1; } return;}//向下递归int mid=(l+r)>>1;build(i<<1,l,mid);build((i<<1)|1,mid+1,r);//向上更新push_up(i);}void update(int i,int l,int r){//如果刚好区间吻合if(STree[i].l==l&&STree[i].r==r){//黑白块交换swap(STree[i].maxw,STree[i].maxb);STree[i].le=-STree[i].le;STree[i].ri=-STree[i].ri;//lazy取反STree[i].lazy=!STree[i].lazy;return;}//如果没有刚好吻合,要访问的区间为当前区间子区间,//且该区间还有懒标记未下放,则向下更新,并向下访问if(STree[i].lazy)push_down(i);int mid=(STree[i].l+STree[i].r)>>1;if(r<=mid)update(ls,l,r);else if(l>mid)update(rs,l,r);else{update(ls,l,mid);update(rs,mid+1,r);}//更新之后,向上更新push_up(i);}int query(int i,int l,int r){//如果刚好吻合,则返回区间最大连续黑块if(l==STree[i].l&&r==STree[i].r) return STree[i].maxb;int mid=(STree[i].l+STree[i].r)>>1;//不能完全符合,且该区间尚有懒标记,下放懒标记if(STree[i].lazy)push_down(i); //向下查询if(r<=mid) return query(ls,l,r);else if(l>mid) return query(rs,l,r);else{int res,tmp;//取左右最值,合并的最值res=max(query(ls,l,mid),query(rs,mid+1,r));//此处不仅仅合并左右区间最值,还需限制左右边界不能超出查询区间以mid为中心的左右边界 tmp=min(mid-l+1,STree[ls].ri)+min(r-mid,STree[rs].le);if(tmp>res) res=tmp;return res;}}int main(){ int n,q,oper,fm,to;while(~scanf("%d",&n)){ //读入 for(int i=1;i<=n;i++) scanf("%d",&a[i]); //建树 build(1,1,n); scanf("%d",&q); for(int i=1;i<=q;i++) { scanf("%d",&oper); scanf("%d%d",&fm,&to); if(oper)update(1,fm,to); else { int ans=query(1,fm,to); printf("%d\n",ans); } }}return 0;}
0 0
- HDU 3911 Black And White (线段树区间更新)
- HDU 3911 Black And White(线段树区间合并)
- hdu 3911 Black And White 区间合并 线段树
- hdu 3911 Black And White 线段树区间合并
- hdu 3911 black and white 线段树区间合并
- HDU 3911 Black And White(线段树区间合并+lazy操作)
- HDU--3911[Black And White] 线段树
- hdu 3911 Black And White 线段树
- HDU 3911 Black And White 线段树
- hdu 3911 Black And White(线段树)
- HUD 3911 Black And White 线段树 区间更新 + 区间合并
- hdu 3911 Black And White(线段树)
- hdu 3911 Black And White (线段树)
- HDOJ 3911 Black And White 线段树 区间合并 成段更新
- HDOJ 3911 Black And White 【线段树 区间合并】
- hdu 3911-区间合并、更新和查询Black And White (&& hdu 1199)
- HDOJ 题目3911 Black And White(线段树区间异或区间合并)
- hdu 3911 Black and White 线段树维护01序列
- 擅长排列的小明
- 硬中断、软中断和信号
- Python笔记--除法
- FragmentPagerAdapter和FragmentStatePagerAdapter之间的差别、监听器OnPageChangeListener
- OC学习笔记——类的扩展
- HDU 3911 Black And White (线段树区间更新)
- [译] --- GPUImage README.md
- HBase 0.94 单机配置(Hadoop 1.0.4)
- ubuntu下C,C++,opencv cuda编程
- HDU5493 Queue 线段树单点更新
- 为什么匿名内部类参数必须为final类型
- PAT1018锤子剪刀布
- JAXB XML与Object之间的转换
- java(android)客户端post方式上传多图片至服务器