POJ 2777 Count Color (線段樹+狀態壓縮)
Count Color
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 |
題意:
L T Q,L代表數量,T代表顏色種類數量,Q代表操作次數。
C操作為 (A-B)區間內所有顏色變為C。
P操作為輸出(A-B)區間內所有顏色的種類。
POINT:
用線段樹,儲存每個區間的顏色種類,用狀態壓縮來存顏色,2的30次-int。
之後用位運算合起來就行。
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
using namespace std;
const int N = 100000*10;
int num[N],add[N];
void pushdown(int x)
{
add[2*x]=add[x];
num[2*x]=add[x];
add[2*x+1]=add[x];
num[2*x+1]=add[x];
add[x]=0;
}
void change(int x,int l,int r,int ll,int rr,int c)
{
if(add[x]) pushdown(x);
if(ll<=l&&rr>=r)
{
num[x]=1<<(c-1);
add[x]=num[x];
}
else
{ int mid=(r+l)>>1;
if(ll<=mid) change(2*x,l,mid,ll,rr,c);
if(rr>mid) change(2*x+1,mid+1,r,ll,rr,c);
num[x]=num[x*2+1]|num[x*2];
}
}
int output(int x,int l,int r,int ll,int rr)
{
int ans=0;
if(add[x]) pushdown(x);
if(ll<=l&&rr>=r) ans=ans|num[x];
else
{
int mid=(r+l)>>1;
if(ll<=mid) ans=ans|output(2*x,l,mid,ll,rr);
if(rr>mid) ans=ans|output(2*x+1,mid+1,r,ll,rr);
}
return ans;
}
int main()
{
int l,t,o;
while(~scanf("%d %d %d",&l,&t,&o))
{
for(int i=1;i<=6*l;i++)
{
num[i]=1;
}
memset(add,0,sizeof add);
while(o--)
{
char q;
getchar();
scanf("%c",&q);
if(q=='C')
{
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
change(1,1,l,a,b,c);
}
else
{
int a, b;
scanf("%d %d",&a,&b);
if(a>b) swap(a,b);
int now=output(1,1,l,a,b);
int ans=0;
while(now)
{
ans+=now&1;
now>>=1;
}
printf("%d\n",ans);
}
}
}
}
相關文章
- POJ3279 Fliptile【狀態壓縮+DFS】
- HDU 1556 Color the ball(線段樹|樹狀陣列)陣列
- POJ 3667 Hotel 線段樹
- poj 2667 hotel 線段樹
- 狀態壓縮
- POJ 3468 【區間修改+區間查詢 樹狀陣列 | 線段樹 | 分塊】陣列
- 簡易狀態壓縮DP
- HDU 3006 The Number of set (狀態壓縮)
- 論文閱讀 狀態壓縮
- TZOJ 8472 : Tree (重鏈剖分+線段樹) POJ 3237
- POJ 3468 A Simple Problem with Integers (線段樹 區間共加)
- 動態開點線段樹
- HDU 5816 Hearthstone(狀態壓縮DP+概率)
- 動態規劃中初識狀態壓縮(入門)動態規劃
- POJ 2528 Mayor's posters (線段樹 區間更新+離散化)
- hdu--5418Victor and World+狀態壓縮DP
- POJ-2352 Stars(樹狀陣列)陣列
- 動態開點線段樹說明
- bzoj4145: [AMPPZ2014]The Prices(狀態壓縮+Dp)
- 線段樹
- 線~段~樹
- HDU 1541 & POJ 2352 Stars (樹狀陣列)陣列
- 01 線段樹
- 線段樹--RMQMQ
- 【模版】線段樹
- 線段樹模板
- 線段樹 hate it
- 動態規劃——用二進位制表示集合的狀態壓縮DP動態規劃
- ut.cpp 最大線段並減線段交 [線段樹]
- 李超線段樹
- 線段樹進階
- 權值線段樹
- 線段樹筆記筆記
- Segment Tree(線段樹)
- 線段樹入門
- bbed修改undo段狀態
- 壓縮錶轉非壓縮表(線上重定義)
- 2024年暑假關於線段樹和樹狀陣列的小知識點陣列