求助大佬,WA最后一个点
查看原帖
求助大佬,WA最后一个点
105222
zhangyuzhe楼主2020/11/30 19:29
#include <cmath>
#include <cstdio>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 1e6 + 10;
int n, m;
int L[N], R[N], pos[N];
int clo[N], opn[N];
int a[N], tag[N];

void build () {
	int t = sqrt (n * 1.0);
	for (int i = 1; i <= t; i ++ ) L[i] = (i - 1) * t + 1,  R[i] = i * t;
	if (R[t] < n) L[++t] = R[t - 1] + 1, R[t] = n;
	for (int i = 1; i <= t; i ++ )
		for (int j = L[i]; j <= R[i]; j ++ ) {
			clo[i] ++;
			pos[j] = i;
		}
}

void change (int l, int r) {
	int p = pos[l], q = pos[r];
	if (p == q) {
		for (int i = l; i <= r; i ++ ) {
			a[i] ^= 1;
			if ((a[i] ^ tag[p]) == 1) opn[p] ++, clo[p] --;
			else clo[p] ++, opn[p] --;
		}
	}
	else {
		for (int i = p + 1; i <= q - 1; i ++ ) {
			tag[i] ^= 1;
			swap (clo[i], opn[i]);
		}
		for (int i = l; i <= R[p]; i ++ ) {
			a[i] ^= 1;
			if ((a[i] ^ tag[p]) == 1) opn[p] ++, clo[p] --;
			else clo[p] ++, opn[p] --;
		}
		for (int i = L[q]; i <= r; i ++ ) {
			a[i] ^= 1;
			if ((a[i] ^ tag[q]) == 1) opn[q] ++, clo[q] --;
			else clo[q] ++, opn[q] --;
		}
	}
}

int ask (int l, int r) {
	int p = pos[l], q = pos[r], ans = 0;
	if (p == q) {
		for (int i = l; i <= r; i ++ ) if (a[i] ^ tag[p]) ans ++;
	}
	else {
		for (int i = p + 1; i <= q - 1; i ++ ) ans += opn[i];
		for (int i = l; i <= R[p]; i ++ ) ans += (a[i] ^ tag[p]);
		for (int i = L[q]; i <= r; i ++ ) ans += (a[i] ^ tag[q]);
	}
	return ans;
}

int main () {
	scanf ("%d%d", &n, &m);
	build ();
	for (int i = 1; i <= m; i ++ ) {
		int opt, a, b;
		scanf ("%d%d%d", &opt, &a, &b);
		if (opt == 0) change (a, b);
		else printf ("%d\n", ask (a, b));
	}
	return 0;
}
2020/11/30 19:29
加载中...