poj2777(线段树)

来源:互联网 发布:网络舆情监控 宣传部 编辑:程序博客网 时间:2024/05/18 01:45

Count Color
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 47751 Accepted: 14431

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 4C 1 1 2P 1 2C 2 2 2P 1 2

Sample Output

21

题意:一条很长(L)的画板,有T种颜色,O个操作;每次操作将一个区间刷成一种颜色,或者查询一个区间内所含的颜色数。

第一次做线段树,不知道延迟更新,又不会使用二进制,导致一开始t得很惨,加上延迟更新,每次要对相关区间再次更新之前再更新,减少了更新次数。


#include<iostream>#include<stdio.h>#include<string.h>#include<math.h>using namespace std;int n,T,O,a,b;const int e=100006;  struct qq  {  bool cover;    //true表示这个区间用一种颜色覆盖     int vis;       //二进制表示区间使用的颜色 }tree[4*e];//线段树要开4倍的点的个数  int color;void PushDown(int root)   //线段树延迟更新 {tree[root*2].vis=tree[root].vis;tree[root*2].cover=1;tree[root*2+1].vis=tree[root].vis;tree[root*2+1].cover=1;tree[root].cover=0;}void updata(int l,int r,int root)//更新数据  {    if(b<l || r<a)return;    if(l>=a && r<=b)         //完全位于更新区间内,直接更新     {        tree[root].vis=1<<color;tree[root].cover=true;        return;      }    if(tree[root].vis==(1<<color)) //剪枝,更新与不更新都一样     return;if(tree[root].cover) //延迟更新子节点颜色         PushDown(root);    int mid=(l+r)/2;    updata(l,mid,root*2);      updata(mid+1,r,root*2+1);tree[root].vis=tree[root*2].vis|tree[root*2+1].vis;      //回溯更新父节点颜色     return;}  int search(int l,int r,int root)  {      if(l>b || r<a)    //位于查询区间外 return 0;      if(l>=a && r<=b)     //完全包含与查询区间 {    return tree[root].vis;}if(tree[root].cover)       //整个区间都已经染成一样,就直接返回颜色 {return tree[root].vis;}    int mid=(l+r)/2;      return search(l,mid,root*2)|search(mid+1,r,root*2+1);  }void init(){for(int i=0;i<4*e;i++){tree[i].vis=1<<1;tree[i].cover=1;}}int main(){init();scanf("%d%d%d",&n,&T,&O);char c[10];for(int i=0;i<O;i++){scanf("%s",c);if(c[0]=='C'){scanf("%d%d%d",&a,&b,&color);if(a>b)swap(a,b);updata(1,n,1);}else if(c[0]=='P'){scanf("%d%d",&a,&b);if(a>b)swap(a,b);int x=search(1,n,1);int cnt=0;for(int i=1;i<=30;i++){if(((x>>i)&1)==1)cnt++;}printf("%d\n",cnt);}}}