社区讨论

样例能过,但只对了一个点,求助思路及代码问题

P3870[TJOI2009] 开关参与者 1已保存回复 1

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
1 条
当前快照
1 份
快照标识符
@lo1uk1it
此快照首次捕获于
2023/10/23 03:13
2 年前
此快照最后确认于
2023/11/03 03:45
2 年前
查看原帖
CPP
#include<bits/stdc++.h>
#define lson p*2
#define rson p*2+1
using namespace std;
const int N=100100;
int n,m,c,aa,bb;
struct node{
	int l;
	int r;
	int s;
	int lazy;
}tree[4*N];
void pushup(int p)
{
	tree[p].s=tree[lson].s+tree[rson].s;
}
void pushdown(int p)
{
	if(tree[p].lazy%2==0)
	{
		tree[p].lazy=0;
		return;
	}
	if(tree[p].s%2==0)
	{
		tree[lson].s=tree[lson].r-tree[lson].l+1;
		tree[rson].s=tree[rson].r-tree[rson].l+1;
		tree[lson].lazy++;
		tree[rson].lazy++;
		tree[p].lazy=0;
		return;
	}
	else
	{
		tree[lson].s=0;
		tree[rson].s=0;
		tree[lson].lazy++;
		tree[rson].lazy++;
		tree[p].lazy=0;
		return;
	}
}
void build(int p,int x,int y)
{
	tree[p]={x,y,0,0};
	if(x==y) return;
	int b=(x+y)/2;
	build(lson,x,b);
	build(rson,b+1,y);
	pushup(p);
}
int query(int p,int x,int y)
{
	if(x<=tree[p].l&&y>=tree[p].r) return tree[p].s;
	int b=(tree[p].l+tree[p].r)/2;
	pushdown(p);
	int sum=0;
	if(x<=b) sum+=query(lson,x,y);
	if(y>b) sum+=query(rson,x,y);
	return sum;
}
void update(int p,int x,int y)
{
	if(x<=tree[p].l&&y>=tree[p].r)
	{
		if(tree[p].s==0)
		{
			tree[p].s=tree[p].r-tree[p].l+1;
			tree[p].lazy++;
			return;
		}
		else
		{
			tree[p].s=0;
			tree[p].lazy++;
			return;
		}
	}
	int b=(tree[p].l+tree[p].r)/2;
	pushdown(p);
	if(x<=b) update(lson,x,y);
	if(y>b) update(rson,x,y);
	pushup(p);
	return;
}
int main()
{
	cin>>n>>m;
	build(1,1,n);
	for(int i=1;i<=m;i++)
	{
		cin>>c>>aa>>bb;
		if(c==0) update(1,aa,bb);
		else cout<<query(1,aa,bb)<<endl;
	}
	return 0;
}

回复

1 条回复,欢迎继续交流。

正在加载回复...