POJ 2777 Count Color(线段树)
2014-08-10 13:37
295 查看
题目地址:POJ 2777
我去。。延迟标记写错了。标记到了叶子节点上。。。。这根本就没延迟嘛。。。怪不得一直TLE。。。
这题就是利用二进制来标记颜色的种类。然后利用或|这个符号来统计每个区间不同颜色种数。
代码如下:
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
using namespace std;
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1
int sum[410000];
int lazy[410000];
int pow1[40];
void PushUp(int rt)
{
sum[rt]=sum[rt<<1]|sum[rt<<1|1];
}
void PushDown(int rt)
{
if(lazy[rt])
{
lazy[rt<<1]=lazy[rt];
lazy[rt<<1|1]=lazy[rt];
sum[rt<<1]=lazy[rt];
sum[rt<<1|1]=lazy[rt];
lazy[rt]=0;
}
}
void update(int ll, int rr, int c, int l, int r, int rt)
{
if(ll<=l&&rr>=r)
{
sum[rt]=c;
lazy[rt]=c;
return ;
}
PushDown(rt);
int mid=l+r>>1;
if(ll<=mid) update(ll,rr,c,lson);
if(rr>mid) update(ll,rr,c,rson);
PushUp(rt);
}
int query(int ll, int rr, int l, int r, int rt)
{
if(ll<=l&&rr>=r)
{
return sum[rt];
}
PushDown(rt);
int ans=0;
int mid=l+r>>1;
if(ll<=mid) ans=ans|query(ll,rr,lson);
if(rr>mid) ans=ans|query(ll,rr,rson);
return ans;
}
int get(int x)
{
int ans=0, y;
while(x)
{
y=x%2;
if(y)
ans++;
x/=2;
}
return ans;
}
int main()
{
int n, t, q, a, b, c, i;
char ch;
pow1[0]=1;
for(i=1; i<=30; i++)
{
pow1[i]=pow1[i-1]*2;
}
scanf("%d%d%d",&n,&t,&q);
memset(lazy,0,sizeof(lazy));
for(i=1;i<=3*n;i++)
{
sum[i]=1;
}
while(q--)
{
getchar();
scanf("%c",&ch);
if(ch=='C')
{
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{
int tt=a;
a=b;
b=tt;
}
update(a,b,pow1[c-1],1,n,1);
}
else
{
scanf("%d%d",&a,&b);
if(a>b)
{
int tt=a;
a=b;
b=tt;
}
int ans=get(query(a,b,1,n,1));
printf("%d\n",ans);
}
}
return 0;
}
我去。。延迟标记写错了。标记到了叶子节点上。。。。这根本就没延迟嘛。。。怪不得一直TLE。。。
这题就是利用二进制来标记颜色的种类。然后利用或|这个符号来统计每个区间不同颜色种数。
代码如下:
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
using namespace std;
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1
int sum[410000];
int lazy[410000];
int pow1[40];
void PushUp(int rt)
{
sum[rt]=sum[rt<<1]|sum[rt<<1|1];
}
void PushDown(int rt)
{
if(lazy[rt])
{
lazy[rt<<1]=lazy[rt];
lazy[rt<<1|1]=lazy[rt];
sum[rt<<1]=lazy[rt];
sum[rt<<1|1]=lazy[rt];
lazy[rt]=0;
}
}
void update(int ll, int rr, int c, int l, int r, int rt)
{
if(ll<=l&&rr>=r)
{
sum[rt]=c;
lazy[rt]=c;
return ;
}
PushDown(rt);
int mid=l+r>>1;
if(ll<=mid) update(ll,rr,c,lson);
if(rr>mid) update(ll,rr,c,rson);
PushUp(rt);
}
int query(int ll, int rr, int l, int r, int rt)
{
if(ll<=l&&rr>=r)
{
return sum[rt];
}
PushDown(rt);
int ans=0;
int mid=l+r>>1;
if(ll<=mid) ans=ans|query(ll,rr,lson);
if(rr>mid) ans=ans|query(ll,rr,rson);
return ans;
}
int get(int x)
{
int ans=0, y;
while(x)
{
y=x%2;
if(y)
ans++;
x/=2;
}
return ans;
}
int main()
{
int n, t, q, a, b, c, i;
char ch;
pow1[0]=1;
for(i=1; i<=30; i++)
{
pow1[i]=pow1[i-1]*2;
}
scanf("%d%d%d",&n,&t,&q);
memset(lazy,0,sizeof(lazy));
for(i=1;i<=3*n;i++)
{
sum[i]=1;
}
while(q--)
{
getchar();
scanf("%c",&ch);
if(ch=='C')
{
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{
int tt=a;
a=b;
b=tt;
}
update(a,b,pow1[c-1],1,n,1);
}
else
{
scanf("%d%d",&a,&b);
if(a>b)
{
int tt=a;
a=b;
b=tt;
}
int ans=get(query(a,b,1,n,1));
printf("%d\n",ans);
}
}
return 0;
}
相关文章推荐
- poj 2777 Count Color (线段树 + 覆盖标记)
- POJ 2777 Count Color(线段树+位运算优化)
- poj 2777 Count Color(线段树)
- POJ 2777 Count Color(线段树+位运算)
- POJ 2777 Count Color 线段树区间更新位运算
- POJ 题目2777 Count Color(线段树,区间查询染色数)
- POJ 2777 Count Color (线段树+位运算)
- POJ 2777 Count Color (线段树的区间更新+lazy tag)
- poj 2777 Count Color (线段树区间更新)
- POJ 2777 Count Color 线段树
- poj 2777 Count Color(线段树)
- POJ 2777 Count Color 线段树
- POJ 2777Count Color (线段树区间更新 + 位运算压缩)
- POJ 2777 Count Color 线段树 区间更新
- POJ 2777-Count Color(线段树-区间染色查询)
- poj 2777 Count Color 线段树
- POJ 2777 Count Color 线段树 + lazy标记
- poj 2777 Count Color(线段树 区间更新)
- POJ 2777 Count Color (线段树区间更新)
- POJ 2777 Count Color(线段树 区间更新)