求调 P1558
  • 板块学术版
  • 楼主szlm
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/7/19 08:53
  • 上次更新2024/7/19 08:58:38
查看原帖
求调 P1558
1159516
szlm楼主2024/7/19 08:53

P1558 色板游戏

我的思路:利用二进制储存每个区间的颜色种类数

WA 本蒟蒻不知道问题在哪里,求大佬帮忙

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10; 
int n,t,o;
struct segtree
{
	int lazytag;
	int sum;
	int L,R; 
}tr[N<<2];
int change(int a)
{
	int x=a,s=0;
	while(x)
	{
		s+=x%2;
		x/=2;
	}
	return s;
}
int k(int p)
{
	int cnt=1;
	for(int i=1;i<=p;i++) cnt*=2;
	return cnt;
}
void pushdown(int p)
{
	if(!tr[p].lazytag) return;
	int la=tr[p].lazytag;
	tr[p<<1].sum=la;
	tr[p<<1].lazytag=la;
	tr[p<<1|1].sum=la;
	tr[p<<1|1].lazytag=la;
	tr[p].lazytag=0;
	return;
}
void pushup(int p){tr[p].sum=tr[p<<1].sum|tr[p<<1|1].sum;}
void build(int p,int l,int r)
{
	tr[p].L=l;
	tr[p].R=r;
	tr[p].lazytag=0;
	if(l==r) tr[p].sum=1;
	else
	{
		int mid=(l+r)>>1;
		build(p<<1,l,mid);
		build(p<<1|1,mid+1,r);
		pushup(p);
	}
}
void update(int p,int l,int r,int v)
{
	if(tr[p].L>=l&&tr[p].R<=r)
	{
		int t=k(v-1);
		tr[p].lazytag=t;
		tr[p].sum=t;
		return;
	}
	pushdown(p);
	int mid=(tr[p].L+tr[p].R)>>1;
	if(l<=mid) update(p<<1,l,r,v);
	if(r>mid) update(p<<1|1,l,r,v);
	pushup(p);
}
int query(int p,int l,int r)
{
	if(tr[p].L>=l&&tr[p].R<=r) return tr[p].sum;
	pushdown(p);
	int mid=(tr[p].L+tr[p].R)>>1;
	int v=0;
	if(l<=mid) v|=query(p<<1,l,r);
	if(r>mid) v|=query(p<<1|1,l,r);
	return v;
}
void work()
{
	cin>>n>>t>>o;
	build(1,1,n);
	char ch;
	while(o--)
	{
		int a,b,c;
		cin>>ch>>a>>b;
		if(ch=='C')
		{
			cin>>c;
			update(1,a,b,c);
		}
		else cout<<change(query(1,a,b))<<'\n';
	}
}
int main()
{
	work();
} 
2024/7/19 08:53
加载中...