萌新分块50pts求调
查看原帖
萌新分块50pts求调
911545
yshs楼主2024/10/9 20:49
#include<bits/stdc++.h>
using namespace std;

constexpr int N = 1e5 + 1,SQ = sqrt(N) + 1;

int n,m,a[N],t,siz,st[SQ],ed[SQ],pos[N],tag[SQ],sum[SQ];

void updata(int L,int R) {
	int p = pos[L],q = pos[R];
	if(p == q) { 
		for(int i=L;i<=R;++i) {
			if(a[i] ^ tag[pos[i]]) --sum[pos[i]];
			else ++sum[pos[i]];
			a[i] ^= 1;
		}
		return ;
	} 
	for(int i=p+1;i<=q-1;++i) tag[i] ^= 1,sum[i] = siz - sum[i];
	for(int i=L;i<=ed[p];++i) {
		if(a[i] ^ tag[pos[i]]) --sum[pos[i]];
		else ++sum[pos[i]];
		a[i] ^= 1;
	}
	for(int i=st[q];i<=R;++i) {
		if(a[i] ^ tag[pos[i]]) --sum[pos[i]];
		else ++sum[pos[i]];
		a[i] ^= 1;
	}
}

int query(int L,int R) {
	int p = pos[L],q = pos[R],res = 0;
	if(p == q) {
		for(int i=L;i<=R;++i) {
			res += (a[i] ^ tag[pos[i]]);
		}
		return res;
	}
	for(int i=p + 1;i<=q - 1;++i) {
		res += sum[i];
	}
	for(int i=L;i<=ed[p];++i) {
		res += (a[i] ^ tag[pos[i]]);
	}
	for(int i=st[q];i<=R;++i) {
		res += (a[i] ^ tag[pos[i]]);
	}
	return res;
}

int main() {
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin >> n >> m;	
	siz = sqrt(n);
	t = n % siz?n/siz + 1:n/siz;
	//	cout << siz << ' ' << t << '\n';
	for(int i=1;i<=t;++i) {
		st[i] = (i - 1) * siz + 1;
		ed[i] = i * siz;
		//		cout << i << ' ' << st[i] << ' ' << ed[i] << '\n';
	}
	ed[t] = n;
	for(int i=1;i<=t;++i) {
		for(int j=st[i];j<=ed[i];++j) {
			pos[j] = i;
		}
	}
	//	for(int i=1;i<=n;++i) cout << pos[i] << ' ';
	//	cout << '\n';
	while(m--) {
		int opt,x,y;
		cin >> opt >> x >> y;
		if(opt == 0) {
			updata(x,y);
		}else {
			cout << query(x,y) << '\n';
		}
	}
	return 0;
}
/*
  6 3 
  0 3 4 
  1 1 3 
  1 2 6 
  
 */
2024/10/9 20:49
加载中...