count color线段树java_POJ 2777 Count Color (线段树)

Count Color

Time Limit: 1000MS

Memory Limit: 65536K

Total Submissions: 29895

Accepted: 8919

Description

Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds of problems. Here, we get a new problem.

There is a very long board with length L centimeter, L is a positive integer, so we can evenly divide the board into L segments, and they are labeled by 1, 2, ... L from left to right, each is 1 centimeter long. Now we have to color the board - one segment with only one color. We can do following two operations on the board:

1. "C A B C" Color the board from segment A to segment B with color C.

2. "P A B" Output the number of different colors painted between segment A and segment B (including).

In our daily life, we have very few words to describe a color (red, green, blue, yellow…), so you may assume that the total number of different colors T is very small. To make it simple, we express the names of colors as color 1, color 2, ... color T. At the beginning, the board was painted in color 1. Now the rest of problem is left to your.

Input

First line of input contains L (1 <= L <= 100000), T (1 <= T <= 30) and O (1 <= O <= 100000). Here O denotes the number of operations. Following O lines, each contains "C A B C" or "P A B" (here A, B, C are integers, and A may be larger than B) as an operation defined previously.

Output

Ouput results of the output operation in order, each line contains a number.

Sample Input

2 2 4

C 1 1 2

P 1 2

C 2 2 2

P 1 2

Sample Output

2

1

Source

题意:长度为n(1~100000)个单位的画板,有t(1~30,位运算的可能性)种颜料。现在叫你完成m组操作:

1. "C A B C" Color the board from segment A to segment B with color C.

2. "P A B" Output the number of different colors painted between segment A and segment B (including).

思路:很经典的线段树。学习到了很多的新知识,最重要的是三点:1.延迟覆盖的操作。2.位操作,用 | 来合并颜色种类。3.updata操作时递归回来,两个子节点的信息对父节点的更新。

#include#include#include

using namespacestd;const int N=100010;#define L(rt) (rt<<1)

#define R(rt) (rt<<1|1)

structTree{intl,r;int col; //用一个32位的int型,每一位对应一种颜色,用位运算代替bool col[32]

bool cover; //表示这个区间都被涂上同一种颜色,线段树效率的体现,否则插入就是0(n)了。

}tree[N<<2];void PushUp(int rt){ //最后递归回来再更改父节点的颜色

tree[rt].col=tree[L(rt)].col |tree[R(rt)].col;

}void build(int L,int R,intrt){

tree[rt].l=L;

tree[rt].r=R;

tree[rt].col=1; //开始时都为涂有颜色1,看题要仔细,要注意状态。

tree[rt].cover=1;if(tree[rt].l==tree[rt].r)return;int mid=(L+R)>>1;

build(L,mid,L(rt));

build(mid+1,R,R(rt));

}void PushDown(int rt){ //延迟覆盖的操作

tree[L(rt)].col=tree[rt].col;

tree[L(rt)].cover=1;

tree[R(rt)].col=tree[rt].col;

tree[R(rt)].cover=1;

tree[rt].cover=0;

}void update(int val,int L,int R,intrt){if(L<=tree[rt].l && R>=tree[rt].r){

tree[rt].col=val;

tree[rt].cover=1;return;

}if(tree[rt].col==val) //剪枝

return;if(tree[rt].cover)

PushDown(rt);int mid=(tree[rt].l+tree[rt].r)>>1;if(R<=mid)

update(val,L,R,L(rt));else if(L>=mid+1)

update(val,L,R,R(rt));else{

update(val,L,mid,L(rt));

update(val,mid+1,R,R(rt));

}

PushUp(rt);//最后递归回来再更改父节点的颜色

}intsum;void query(int L,int R,intrt){if(L<=tree[rt].l && R>=tree[rt].r){

sum|=tree[rt].col;return;

}if(tree[rt].cover){ //这个区间全部为1种颜色,就没有继续分割区间的必要了

sum |= tree[rt].col; //颜色种类相加的位运算代码

return;

}int mid=(tree[rt].l+tree[rt].r)>>1;if(R<=mid)

query(L,R,L(rt));else if(L>=mid+1)

query(L,R,R(rt));else{

query(L,mid,L(rt));

query(mid+1,R,R(rt));

}

}intsolve(){int ans=0;while(sum){if(sum&1)

ans++;

sum>>=1;

}returnans;

}void swap(int &a,int &b){int tmp=a;a=b;b=tmp;

}intmain(){//freopen("input.txt","r",stdin);

intn,t,m;while(~scanf("%d%d%d",&n,&t,&m)){

build(1,n,1);char op[3];inta,b,c;while(m--){

scanf("%s",op);if(op[0]=='C'){

scanf("%d%d%d",&a,&b,&c);if(a>b)

swap(a,b);

update(1<

}else{

scanf("%d%d",&a,&b);if(a>b)

swap(a,b);

sum=0;

query(a,b,1);

printf("%d\n",solve());

}

}

}return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值