线段树10pts求助
查看原帖
线段树10pts求助
1305692
xiangixuan楼主2025/1/18 19:36
#include<bits/stdc++.h>
#define int long long
#define N 100001
using namespace std;
int n, m;
struct seg{
	int l, r, v; bool t;
	#define l(x) t[x].l
	#define r(x) t[x].r
	#define v(x) t[x].v
	#define t(x) t[x].t
} t[4*N];
int mid(int x) {return l(x)+r(x)>>1; }
int ls(int x) {return x<<1; }
int rs(int x) {return (x<<1)+1; }
void build(int p, int l, int r) {
	l(p)=l, r(p)=r;
	if (l==r) return;
	build(ls(p), l, mid(p));
	build(rs(p), mid(p)+1, r);
}
void spread(int p) {
	if (!t(p)) return;
	v(ls(p))=(r(ls(p))-l(ls(p))+1)-v(ls(p)); t(ls(p))=1;
	v(rs(p))=(r(rs(p))-l(rs(p))+1)-v(rs(p)); t(rs(p))=1;
	t(p)=0;
}
void change(int p, int l, int r) {
	if (l<=l(p) && r>=r(p)) {
		v(p)=(r(p)-l(p)+1)-v(p);
		t(p)=1; return;
	}
	spread(p);
	if (l<=mid(p)) change(ls(p), l, r);
	if (r>mid(p)) change(rs(p), l, r);
	v(p)=v(ls(p))+v(rs(p));
}
int query(int p, int l, int r) {
	if (l<=l(p) && r>=r(p)) return v(p);
	spread(p);
	int v=0;
	if (l<=mid(p)) v+=query(ls(p), l, r);
	if (r>mid(p)) v+=query(rs(p), l, r);
	return v;
}
signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	cin >> n >> m;
	build(1, 1, n);
	int opt, x, y;
	while (m--) {
		cin >> opt >> x >> y;
		if (!opt) change(1, x, y);
		else cout << query(1, x, y) << '\n';
	}
	return 0;
}

2025/1/18 19:36
加载中...