http://poj.org/problem?id=2777
一个坑爹的错误找了我半天,脑残呀!我用的数状态压存储的,加上延迟标记下就行了
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define maxn 555555
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef long long ll;
ll d[maxn<<2];
bool col[maxn<<2];
void pushdown(int rt)
{
if(col[rt])
{
col[rt<<1]=1;
col[rt<<1|1]=1;
d[rt<<1]=d[rt];
d[rt<<1|1]=d[rt];
col[rt]=0;
}
}
void update(int L,int R,int C,int l,int r,int rt)
{
//cout<<l<<" "<<r<<" "<<rt<<endl;
if(L<=l&&R>=r)
{
d[rt]=(1LL)<<C;
col[rt]=1; // 注意把它赋值为1
return;
}
pushdown(rt);
int m=(l+r)>>1;
if(L<=m) update(L,R,C,lson);
if(R>m) update(L,R,C,rson);
d[rt]=d[rt<<1]|d[rt<<1|1];
}
ll query(int L,int R,int l,int r,int rt)
{
if(L<=l&&R>=r) return d[rt];
pushdown(rt);
int m=(l+r)>>1;
ll ret1=0,ret2=0;
if(L<=m) ret1=query(L,R,lson);
if(R>m) ret2=query(L,R,rson);
return ret1|ret2;
}
int main()
{
int T,L,Q,l,r,c;
char ord[5];
while(scanf("%d%d%d",&L,&T,&Q)==3)
{
memset(col,0,sizeof(col));
d[1]=1;
col[1]=1;
while(Q--)
{
scanf("%s",ord);
if(ord[0]=='C')
{
scanf("%d %d %d",&l,&r,&c);
if(l>r) swap(l,r);
update(l,r,c-1,1,L,1);
}
else
{
scanf("%d %d",&l,&r);
if(l>r) swap(l,r);
ll ans=query(l,r,1,L,1);
int cnt=0;
for(int i=0;i<T;i++)
if(ans&((1LL)<<i)) cnt++;
printf("%d\n",cnt);
}
}
}
return 0;
}
本文介绍了一种使用线段树进行区间更新与查询的优化方法,通过延迟标记减少不必要的操作,提高算法效率。该方法适用于解决如POJ 2777等区间更新查询问题。
413

被折叠的 条评论
为什么被折叠?



