【HDU
来源:互联网 发布:淘宝99大促 编辑:程序博客网 时间:2024/06/06 01:11
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
4
1 0 1 0
5
0 1 4
1 2 3
0 1 4
1 3 3
0 4 4
Sample Output
1
2
0
可以提前先把这个题写了
题意:有n个数和m此操作,操作分两种——
1,0 x y 表示把区间[x, y]里面的1改为0,0改为1;
2,1 x y 表示查询区间[x, y]最多的连续的1的个数。
代码
#include<bits/stdc++.h>using namespace std;typedef pair<int,int>pii;#define first fi#define second se#define LL long long#define fread() freopen("in.txt","r",stdin)#define fwrite() freopen("out.txt","w",stdout)#define CLOSE() ios_base::sync_with_stdio(false)const int MAXN = 1e5+11;const int MAXM = 1e6;const int mod = 1e9+7;const int inf = 0x3f3f3f3f;struct Tree{ int l,r,len;// 同时维护区间的连续1的个数,和0的个数。 int l1,r1,s1; int l0,r0,s0; int lazy;}tree[MAXN<<2];void Up(int o){ tree[o].l1=tree[o<<1].l1; //维护1 tree[o].r1=tree[o<<1|1].r1; tree[o].s1=max(tree[o<<1].s1,tree[o<<1|1].s1); if(tree[o<<1].l1==tree[o<<1].len) tree[o].l1+=tree[o<<1|1].l1; if(tree[o<<1|1].r1==tree[o<<1|1].len) tree[o].r1+=tree[o<<1].r1; tree[o].s1=max(tree[o].s1,max(tree[o].l1,tree[o].r1)); tree[o].s1=max(tree[o].s1,tree[o<<1].r1+tree[o<<1|1].l1); tree[o].l0=tree[o<<1].l0;//维护0 tree[o].r0=tree[o<<1|1].r0; tree[o].s0=max(tree[o<<1].s0,tree[o<<1|1].s0); if(tree[o<<1].l0==tree[o<<1].len) tree[o].l0+=tree[o<<1|1].l0; if(tree[o<<1|1].r0==tree[o<<1|1].len) tree[o].r0+=tree[o<<1].r0; tree[o].s0=max(tree[o].s0,max(tree[o].l0,tree[o].r0)); tree[o].s0=max(tree[o].s0,tree[o<<1].r0+tree[o<<1|1].l0);}void Change(int o){// 如果操作0时候,就是相当于把0和1的数据交换就可以了。 swap(tree[o].l1,tree[o].l0); swap(tree[o].r1,tree[o].r0); swap(tree[o].s1,tree[o].s0); tree[o].lazy^=1; // lazy是延迟标记,如果有2次lazy到这个区间,就相当于没有变化,所以用的^}void Down(int o){ if(tree[o].lazy){ Change(o<<1); Change(o<<1|1); tree[o].lazy=0; }}void Build(int o,int le,int ri){ tree[o].l=le,tree[o].r=ri,tree[o].len=ri-le+1; tree[o].lazy=0; if(le==ri){ int val;scanf("%d",&val); tree[o].l1=tree[o].r1=tree[o].s1=val?1:0; tree[o].l0=tree[o].r0=tree[o].s0=val?0:1; return ; } int mid=(le+ri)>>1; Build(o<<1,le,mid); Build(o<<1|1,mid+1,ri); Up(o);}void Update(int o,int le,int ri){ if(le<=tree[o].l&&tree[o].r<=ri) { Change(o); return ; } Down(o); int mid=(tree[o].l+tree[o].r)>>1; if(ri<=mid) Update(o<<1,le,ri); else if(le>mid) Update(o<<1|1,le,ri); else { Update(o<<1,le,mid); Update(o<<1|1,mid+1,ri); } Up(o);}int Query(int o,int le,int ri){ if(tree[o].l>=le&&tree[o].r<=ri) { return tree[o].s1; } Down(o); int mid=(tree[o].l+tree[o].r)>>1; if(ri<=mid) return Query(o<<1,le,ri); else if(le>mid) return Query(o<<1|1,le,ri); else { int ans=Query(o<<1,le,mid); ans=max(ans,Query(o<<1|1,mid+1,ri)); return ans=max(ans,min(mid-le+1,tree[o<<1].r1)+min(ri-mid,tree[o<<1|1].l1)); }}int main(){ CLOSE();// fread();// fwrite(); int n; while(scanf("%d",&n)!=EOF){ Build(1,1,n); int m;scanf("%d",&m); while(m--){ int op,a,b; scanf("%d%d%d",&op,&a,&b); if(op) Update(1,a,b); else printf("%d\n",Query(1,a,b)); } } return 0;}
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- hdu
- hdu
- HDU
- Hdu
- hdu
- hdu-
- hdu
- hdu
- hdu
- HDU
- Gradle 配置环境变量
- 237-delete node in single list
- NO.7帮助与类型转换
- Windows10下python3和python2同时安装(一)安装python3和python2
- Java面试题全集(下)
- 【HDU
- HDFS+NFS+ZOOKEEPER实现Hadoop高可用集群
- Windows10下python3和python2同时安装(二)python2.exe、python3.exe和pip2、pip3设置
- github上fork了别人的项目后,再同步更新别人的提交
- Windows去除显示EFI分区
- Angular 2-组件
- python3 使用静态属性节约对象实例的内存开销
- linux shell之数字加减
- java语言程序设计 第十二章 (12.15、12.16、12.17)