请问代码哪里有错?只过了一个点
查看原帖
请问代码哪里有错?只过了一个点
335890
Jason66661010楼主2020/5/4 11:44
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define MAX 200005
struct node
{
	long long zero;
	long long one;
}c[MAX << 2];
int a[MAX],lazy[MAX<<2];
int n, m;
void build(int l,int r,int k)
{
	if (l == r)
	{
		if (a[l] == 1)c[k].one=1;
		else c[k].zero=1;	
	}
	else
	{
		int mid = (l + r) >> 1;
		build(l, mid, k << 1);
		build(mid + 1, r, k << 1 | 1);
		c[k].one = c[k << 1].one + c[k<<1|1].one;
		c[k].zero = c[k << 1].zero + c[k << 1 | 1].zero;
	}	
}
void pushdown(int l,int r,int k)
{
	if (lazy[k])
	{
		lazy[k << 1] = 1;
		lazy[k << 1|1] = 1;
		swap(c[k << 1].one,c[k<<1].zero);
		swap(c[k << 1 | 1].one, c[k << 1 | 1].zero);
		lazy[k] = 0;
	}
}
void update(int L,int R,int l,int r,int k)
{
	if (L <= l&&R >= r)
	{
		swap(c[k].one, c[k].zero);
		lazy[k] = 1;
	}
	else
	{
		pushdown(l, r, k);
		int mid = (l + r) >> 1;
		if (L <= mid)
			update(L, R, l, mid, k << 1);
		if (R > mid)
			update(L, R, mid + 1, r, k << 1 | 1);
		c[k].one = c[k << 1].one + c[k << 1 | 1].one;
		c[k].zero = c[k << 1].zero + c[k << 1 | 1].zero;
	}
}
long long query(int L, int R, int l, int r, int k)
{
	if (L <= l&&R >= r)
	{
		return c[k].one;
	}
	else
	{
		long long res = 0;
		pushdown(l, r, k);
		int mid = (l + r) >> 1;
		if (L <= mid)res = res + query(L, R, l, mid, k << 1);
		if (R > mid)res = res + query(L, R, mid + 1, r, k << 1 | 1);
		return res;
	}

}
int main()
{
	int aa, bb, cc;
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++)
	{
		scanf("%1d", &a[i]);
	}
	build(1, n, 1);
	for (int i = 0; i < m; i++)
	{
		scanf("%d %d %d", &aa, &bb, &cc);
		if (aa == 0)update(bb, cc, 1, n, 1);
		else printf("%lld\n", query(bb, cc, 1, n, 1));
	}

}
2020/5/4 11:44
加载中...