博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 2777 Count Color
阅读量:4946 次
发布时间:2019-06-11

本文共 3801 字,大约阅读时间需要 12 分钟。

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 线段树区间更新 代码如下:
1 #include
2 #include
3 #include
4 #include
5 using namespace std; 6 struct wakaka{ 7 int l,r,num; 8 }tree[5000000]; 9 bool b[31],bo; 10 int n,m,x,ll,rr,v,ans,t; 11 char ch; 12 int maxx(int a,int b){ 13 if (a>b) 14 return a; 15 else 16 return b; 17 } 18 void build(int l,int r,int i){ 19 tree[i].l=l; 20 tree[i].r=r; 21 tree[i].num=1; 22 if (l!=r){ 23 int mid=(l+r)>>1; 24 build(l,mid,i*2); 25 build(mid+1,r,i*2+1); 26 } 27 } 28 void change(int i,int l,int r,int v){ 29 30 if(tree[i].num==v) 31 return ; 32 int ll=tree[i].l; 33 int rr=tree[i].r; 34 if (ll==l&&rr==r){ 35 tree[i].num=v; 36 return ; 37 } 38 if (tree[i].num!=-1) 39 { 40 tree[i*2].num=tree[i*2+1].num=tree[i].num; 41 tree[i].num=-1; 42 } 43 44 int mid=(ll+rr)>>1; 45 if (l>mid) 46 change(2*i+1,l,r,v); 47 else if (mid>=r) 48 change(2*i,l,r,v); 49 else { 50 change(2*i,l,mid,v); 51 change(2*i+1,mid+1,r,v); 52 } 53 } 54 void search1(int i,int l,int r) 55 { 56 if (tree[i].num!=-1) 57 { 58 b[tree[i].num]=1; 59 return ; 60 } 61 int mid=tree[i].l+tree[i].r; 62 mid/=2; 63 if (l>mid) 64 search1(i*2+1,l,r); 65 else if (r<=mid) 66 search1(i*2,l,r); 67 else{ 68 search1(i*2,l,mid); 69 search1(i*2+1,mid+1,r); 70 } 71 } 72 73 int main(){ 74 bo=0; 75 scanf("%d %d %d",&n,&t,&m); 76 build(1,n,1); 77 for (int i=1;i<=t;++i){ 78 b[i]=0; 79 } 80 for (int i=1;i<=m;++i){ 81 scanf("%c",&ch); 82 while(ch!='C'&&ch!='P'){ 83 scanf("%c",&ch); 84 } 85 if (ch=='C'){ 86 scanf("%d %d %d",&ll,&rr,&x); 87 change(1,min(ll,rr),max(ll,rr),x); 88 } 89 else { 90 ans=0; 91 scanf("%d %d",&ll,&rr); 92 search1(1,min(ll,rr),max(ll,rr)); 93 for (int j=1;j<=t;++j) 94 { 95 if (b[j]==1) 96 ans++; 97 b[j]=0; 98 } 99 printf("%d\n",ans);100 }101 }102 103 return 0;104 }

 

转载于:https://www.cnblogs.com/fakerv587/p/5238666.html

你可能感兴趣的文章
Stream、FileStream、MemoryStream的区别
查看>>
High Availability手册(3): 配置
查看>>
nfs+drbd+keepalived 高可用的实现
查看>>
HttpClient
查看>>
【实践】配置服务器网络环境思路
查看>>
数组重排
查看>>
leetcode[24]Swap Nodes in Pairs
查看>>
javaweb学习总结(三十八)——事务
查看>>
CRF 及CRF++ 安装与解释
查看>>
Mysql密码忘记,修改密码方法
查看>>
Gentoo: fcitx的安装
查看>>
winform windowsmediaplayer的属性
查看>>
JS获取当前页面的URL信息
查看>>
composer安装
查看>>
Struts2文件目录结构
查看>>
(六)jdk8学习心得之Stream流
查看>>
Ceph实验室:第六课:Ceph运维之横向扩展
查看>>
条件、循环和其他语句
查看>>
BZOJ4342 : CF348 Pilgrims
查看>>
Vue实现购物车功能
查看>>