10pts 线段树TLE
查看原帖
10pts 线段树TLE
1419923
ZYStream楼主2025/1/23 22:04

只AC了#2,其余全部 TLETLE

#include<cstdio>
using namespace std;

typedef long long ll;
const ll N=1e6;

ll n,m,tr[N],tag[N];

void build(ll id,ll s,ll t){
	if (s==t){
		tr[id]=0;
		tag[id]=1;
		return ;
	}
	ll mid=s+t>>1;
	build(id<<1,s,mid);
	build(id<<1|1,mid+1,t);
	tr[id]=0;
	tag[id]=1;
	return ;
}

void change(ll id,ll l,ll r,ll s,ll t){
	if (l<=s&&t<=r){
		tr[id]^=(1<<(t-s+1))-1;
		tag[id]*=-1;
		return ;
	}
	ll mid=s+t>>1;
	if (tag[id]==-1&&s!=t){
		tr[id<<1]^=(1<<(mid-s+1))-1;
		tr[id<<1|1]^=(1<<(t-mid))-1;
		tag[id<<1]*=-1;
		tag[id<<1|1]*=-1;
		tag[id]=1;
	}
	if (l<=mid) change(id<<1,l,r,s,mid);
	if (r>mid) change(id<<1|1,l,r,mid+1,t);
	tr[id]=(tr[id<<1]<<(t-mid))+tr[id<<1|1];
	return ;
}

inline ll get1(ll x){
	ll res=0;
	while (x){
		if (x&1){
			res++;
			x>>=1;
		}
		else x>>=1;
	}
	return res;
}

ll getsum(ll id,ll l,ll r,ll s,ll t){
	if (l<=s&&t<=r) return get1(tr[id]);
	ll mid=s+t>>1;
	if (tag[id]==-1){
		tr[id<<1]^=(1<<(mid-s+1))-1;
		tr[id<<1|1]^=(1<<(t-mid))-1;
		tag[id<<1]*=-1;
		tag[id<<1|1]*=-1;
		tag[id]=1;
	}
	ll sum=0;
	if (l<=mid) sum+=getsum(id<<1,l,r,s,mid);
	if (r>mid) sum+=getsum(id<<1|1,l,r,mid+1,t);
	return sum;
}

int main(){
	scanf("%lld%lld",&n,&m);
	build(1,1,n);
	while (m--){
		ll c,a,b;
		scanf("%lld%lld%lld",&c,&a,&b);
		if (!c) change(1,a,b,1,n);
		else printf("%lld\n",getsum(1,a,b,1,n));
	}
	return 0;
}
2025/1/23 22:04
加载中...