蒟蒻问一下为什么线段树只过了两点TLE了
查看原帖
蒟蒻问一下为什么线段树只过了两点TLE了
544643
whty楼主2022/1/26 17:26
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=1e5+5;
ll n,m,a[maxn],ans[maxn<<2],tag[maxn<<2];
inline int read()
{
	char ch=getchar(); 
	int x=0,f=1;
	while(ch<'0'||ch>'9')
	{
		 if(ch=='-')
    		f=-1;
		ch=getchar();
	}
	while('0'<=ch&&ch<='9')
	{
		x=x*10+ch-'0';
		ch=getchar();
	}
	return x*f;
}
inline void write(int x)
{
     if(x<0) putchar('-'),x=-x;
     if(x>9) write(x/10);
     putchar(x%10+'0');
}
inline ll ls(ll x)
{
	return x<<1;
}
inline ll rs(ll x)
{
	return x<<1|1;
}
inline void push_up(ll p)
{
	ans[p]=ans[ls(p)]+ans[rs(p)];
}
inline void build(ll p,ll l,ll r)
{
	tag[p]=0;
	if(l==r)
	{
		ans[p]=0;
		return;
	}
	ll mid=l+r>>1;
	build(ls(p),l,mid);
	build(rs(p),mid+1,r);
	push_up(p);
}
inline void push_down(ll p,ll l,ll r)
{
	if(tag[p])
	{
		ll mid=l+r>>1;
		ans[ls(p)]=mid-l+1-ans[ls(p)];
		ans[rs(p)]=r-mid-ans[rs(p)];
		if(tag[ls(p)])
			tag[ls(p)]=0;
		else
			tag[ls(p)]=1;
		if(tag[rs(p)])
			tag[rs(p)]=0;
		else
			tag[rs(p)]=1;
		tag[p]=0;
	}
	else
		return;
}
inline void update(ll nl,ll nr,ll l,ll r,ll p)
{
	if(nl<=l&&nr>=r)
	{
		ans[p]=r-l+1-ans[p];
		if(tag[p])
			tag[p]=0;
		else
			tag[p]=1;
		return;
	}
	push_down(p,l,r);
	ll mid=l+r>>1;
	if(nl<=mid)
		update(nl,nr,l,mid,ls(p));
	if(nr>mid)
		update(nl,nr,mid+1,r,rs(p));
	push_up(p);
}
inline ll query(ll nl,ll nr,ll l,ll r,ll p)
{
	ll res=0;
	if(l==r)
		return ans[p];
	push_down(p,l,r);
	ll mid=l+r>>1;
	if(nl<=mid)
		res+=query(nl,nr,l,mid,ls(p));
	if(nr>mid)
		res+=query(nl,nr,mid+1,r,rs(p));
	return res;
}
int main()
{
	n=read();m=read();
	build(1,1,n);
	while(m--)
	{
		ll q,a,b;
		q=read();a=read();b=read();
		if(!q)
			update(a,b,1,n,1);
		else
			write(query(a,b,1,n,1)),putchar('\n');
	}
	return 0;
}
2022/1/26 17:26
加载中...