Xdoj1344: Love Live! Yazawa Nicole

A line segment tree is modified by the interval + interval query, but the interval modification requires a delay modification operation, which has not been added yet, so it has timed out.

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
struct node 
{
	int l,r,v;
}s[3*2000009];
void build(int rot,int l,int r)
{
	s[rot].v=-1;
	s[rot].l=l;s[rot].r=r;
	if(l==r) return;
	int mid=(l+r)/2;
	build(2*rot,l,mid);
	build(2*rot+1,mid+1,r);
}
void add(int rot,int v,int l,int r,int p)
{
	s[rot].v=max(s[rot].v,v); 
	if(l==r) return;
	int mid=(l+r)>>1;
	if(p<=mid)
	    add(2*rot,v,l,mid,p);
	else
		add(2*rot+1,v,mid+1,r,p);  	
}
void deal(int rot,int l,int r,int x,int mode)
{
	if(s[rot].l==s[rot].r)
	{
		if(!mode)
		{
			s[rot].v=s[rot].v&x;
		}else
		{
			s[rot].v=s[rot].v|x;	
		}		
		return;
	}
	int mid=(s[rot].l+s[rot].r)>>1;
	if(l>mid)
		deal(2*rot+1,l,r,x,mode);
	else if(r<=mid)
		deal(2*rot,l,r,x,mode);
	else
	{
		deal(2*rot+1,mid+1,r,x,mode);
		deal(2*rot,l,mid,x,mode);
	}
	s[rot].v=max(s[rot*2].v,s[2*rot+1].v);
}
int query(int rot,int l,int r)
{
	if(s[rot].l==l&&s[rot].r==r)
	{
		return s[rot].v;
	}
	int mid=(s[rot].l+s[rot].r)>>1;
	if(l>mid)
	{
		return query(2*rot+1,l,r);
	}
	else if(r<=mid)
	{
		return query(2*rot,l,r);
	}else
	{
		return max(query(2*rot,l,mid),query(2*rot+1,mid+1,r));
	}
}
int main()
{
	FILE *fp=fopen("t.txt","r");
	int a,n,q,l,r,x;
	fscanf(fp,"%d%d",&n,&q);	
	build(1,1,n);
	for(int i=1;i<=n;i++)
	{
		fscanf(fp,"%d",&a);
		add(1,a,1,n,i);
	}
	while(q--)
	{
		fscanf(fp,"%d",&a);
		if(a==1)
		{
			fscanf(fp,"%d%d%d",&l,&r,&x);
			deal(1,l,r,x,0);
		}else if(a==2)
		{
			fscanf(fp,"%d%d%d",&l,&r,&x);
			deal(1,l,r,x,1);
		}else if(a==3)
		{
			fscanf(fp,"%d%d",&l,&r);
			printf("%d\n",query(1,l,r));
		}
	}
	return 0;
}