跳到正文

个人记录

1

Pizar约 2 分钟
原文

这是 2026年10月2日 存档的第 6 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年10月1日
收录
2026年10月1日
更新
2026年11月10日
篇幅
约 2 分钟

版本

  1. v7当前
  2. v6
  3. v5
  4. v4
更早的 3 个版本
  1. v3
  2. v2
  3. v1
C++
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ls ((p)<<1)
#define rs ((p)<<1|1)
#define mid ((x+y)>>1)
const int N=1e5+5;
struct node{
	int c0,c1;
}a[N<<2];
node merge(node lhs,node rhs){
	return (node){
		lhs.c0+rhs.c0,
		lhs.c1+rhs.c1
	};
}
bool tag[N<<2];
void push(int p){
	swap(a[p].c0,a[p].c1);
	tag[p]^=1;
}
void pushdown(int p){
	if(tag[p]){
		push(ls);
		push(rs);
		tag[p]=0;
	}
}
void build(int x,int y,int p){
	tag[p]=0;
	if(x==y){
		a[p]={1,0};
		return;
	}
	build(x,mid,ls);
	build(mid+1,y,rs);
	a[p]=merge(a[ls],a[rs]);
}
void mdf(int x,int y,int l,int r,int p){
	if(l>r)return;
	if(l<=x&&r>=y)return push(p);
	pushdown(p);
	if(l<=mid)mdf(x,mid,l,r,ls);
	if(r>mid)mdf(mid+1,y,l,r,rs);
	a[p]=merge(a[ls],a[rs]);
}
node qry(int x,int y,int l,int r,int p){
	if(l>r)return {0,0};
	if(l<=x&&r>=y)return a[p];
	pushdown(p);
	if(r<=mid)return qry(x,mid,l,r,ls);
	if(l>mid)return qry(mid+1,y,l,r,rs);
	return merge(qry(x,mid,l,r,ls),qry(mid+1,y,l,r,rs));
}
int n,m;
int main(){
	cin>>n>>m;
	build(1,n,1);
	while(m--){
		int op,x,y;
		scanf("%d%d%d",&op,&x,&y);
		if(op==0)mdf(1,n,x,y,1);
		else printf("%d\n",qry(1,n,x,y,1).c1);
	}
}