求孙悟空(不是猴子的意思)
查看原帖
求孙悟空(不是猴子的意思)
1284815
canwen楼主2024/9/7 22:59

求找错。

一开始以为这么水的线段树题结果调到自闭了,不知道是不是什么逆天错误。

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define lc p<<1
#define rc p<<1|1
const int N = 1e5+5;

struct node{
	int l,r,x,add;
}tr[N<<2];
int n,m;
inline int in(){
	int k=1,ans=0;
	char a = getchar();
	while(a<'0'||a>'9'){
		if(a=='-') k=-1;
		a=getchar();
	}
	while(a<='9'&&a>='0'){
		ans = ans*10+a-'0';
		a=getchar();
	}
	return ans;
}
void build(int p,int l,int r){
	tr[p]=(node){l,r};
	if(l==r) {tr[p].x=0;return;}
	int m = (l+r)>>1;
	build(lc,l,m);
	build(rc,m+1,r);
}
void pushdown(int p){
	if(tr[p].add){
		tr[lc].x = tr[lc].r-tr[lc].l+1-tr[lc].x;
		tr[rc].x = tr[rc].r-tr[rc].l+1-tr[rc].x;
		if(tr[lc].add==1) tr[lc].add=0;
		else tr[lc].add=1;
		if(tr[rc].add==1) tr[rc].add=0;
		else tr[rc].add=1;
		tr[p].add=0;
	}
}
void pushup(int p){
	tr[p].x = tr[lc].x + tr[rc].x;
}
void change(int p,int l,int r){
	if(l<=tr[p].l&&r>=tr[p].r){
		if(tr[p].add==0) tr[p].add=1;
		else tr[p].add=0;
		tr[p].x=(tr[p].r-tr[p].l+1)-tr[p].x;
		return;
	}
	int m = (tr[p].l+tr[p].r)>>1;
	pushdown(p);
	if(l<=m) change(lc,l,m);
	if(r>m) change(rc,m+1,r);
	pushup(p);
}
int query(int p,int l,int r){
	if(l<=tr[p].l&&r>=tr[p].r){
		return tr[p].x;
	}
	int m = (tr[p].l+tr[p].r)>>1,ans=0;
	pushdown(p);
	if(l<=m) ans+=query(lc,l,r);
	if(r>m) ans+=query(rc,l,r);
	return ans;
}

signed main(){
	n = in();
	m = in();
	build(1,1,n);
	while(m--){
		int c,a,b;
		c = in() , a = in() , b =in();
//		cout << a << " " << b << " " << c << endl;
		if(c==0){
			change(1,a,b);
		}else{
			printf("%lld\n",query(1,a,b));
		}
	}
	return 0;
}
2024/9/7 22:59
加载中...