求助
  • 板块学术版
  • 楼主wmq2006
  • 当前回复19
  • 已保存回复19
  • 发布时间2020/5/1 10:03
  • 上次更新2023/11/7 03:30:33
查看原帖
求助
330026
wmq2006楼主2020/5/1 10:03
#include<bits/stdc++.h>
using namespace std;
int a[500001],n,m,ans=0;
int x,y,k,p;
struct q{
 long long l,r,sum,lazy,colorl,colorr;
}tree[500001];
inline int read(){
 int x=0,f=1;char ch=getchar();
 while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
 while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
 return x*f;
}
void build(int x,int l,int r){
 tree[x].l=l,tree[x].r=r;
 if(l==r){tree[x].sum=1,tree[x].colorl=a[l],tree[x].colorr=a[r];return;}
 int mid=(l+r)/2;
 build(2*x,l,mid),build(2*x+1,mid+1,r);
 if(tree[2*x].colorr==tree[2*x+1].colorl)tree[x].sum=tree[2*x].sum+tree[2*x+1].sum-1;
 else tree[x].sum=tree[2*x].sum+tree[2*x+1].sum;
 tree[x].colorl=tree[2*x].colorl,tree[x].colorr=tree[2*x+1].colorr;
}
void pushdown(int x){
	tree[2*x].lazy=1,tree[2*x+1].lazy=1;
	tree[2*x].colorl=1-tree[2*x].colorl,tree[2*x+1].colorr=1-tree[2*x+1].colorr,tree[2*x].colorr=1-tree[2*x].colorr,tree[2*x+1].colorl=1-tree[2*x+1].colorl;
	tree[x].lazy=0;
}
void add(int x,int l,int r){
 //cout<<x<<" "<<l<<" "<<r<<" "<<k<<endl;
 if(tree[x].l>=l&&tree[x].r<=r){
  tree[x].lazy=1;
  tree[x].sum=1;
  tree[x].colorr=1-tree[x].colorr,tree[x].colorl=1-tree[x].colorl;
  return;
  	}
 if(tree[x].lazy){
 		pushdown(x);
 	}
 int mid=(tree[x].l+tree[x].r)/2;
 if(mid>=l)add(2*x,l,r);
 if(mid+1<=r)add(2*x+1,l,r);
 if(tree[2*x].colorr==tree[2*x+1].colorl)tree[x].sum=tree[2*x].sum+tree[2*x+1].sum-1;
 else tree[x].sum=tree[2*x].sum+tree[2*x+1].sum;
 tree[x].colorl=tree[2*x].colorl,tree[x].colorr=tree[2*x+1].colorr;
}
/*long long get(int x,int l,int r){
 if(tree[x].l>=l&&tree[x].r<=r)return tree[x].sum;
 if(tree[x].lazy){
  tree[2*x].lazy+=tree[x].lazy;
  tree[2*x].sum+=tree[x].lazy;
  tree[2*x+1].lazy+=tree[x].lazy;
  tree[2*x+1].sum+=tree[x].lazy;
  tree[x].lazy=0;
 }
 long long sum=-1e18;
 int mid=(tree[x].l+tree[x].r)/2;
 if(mid>=l)sum=maxx(sum,get(2*x,l,r));
 if(mid+1<=r)sum=maxx(sum,get(2*x+1,l,r));
 return sum;
}*/
int main(){
 n=read();
 m=read();
 for(int i=1;i<=n;i++)a[i]=read();
 build(1,1,n);
 for(int i=1;i<=m;i++){
 	p=read();
 	if(p==1){
 		x=read(),y=read();
   add(1,x,y);
   //for(int i=1;i<=9;i++)cout<<tree[i].lazy<<" "<<tree[i].sum<<endl;
	  }
  /*if(p==2){
   	x=read(),y=read();
   	cout<<get(1,x,y)<<endl;
   	}*/
 }
 for(int i=1;i<=15;i++)cout<<i<<" "<<tree[i].colorl<<" "<<tree[i].colorr<<" "<<tree[i].l<<" "<<tree[i].r<<" "<<tree[i].sum<<endl;
 return 0;
}
2020/5/1 10:03
加载中...