POJ 2777 Count Color(线段树)
来源:互联网 发布:换手机登陆淘宝账号 编辑:程序博客网 时间:2024/05/16 17:32
Count Color
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 47124 Accepted: 14246
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.
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
Source
POJ Monthly--2006.03.26,dodo
题意:有一个长板子,多次操作,有两种操作,第一种是给从a到b那段染一种颜色c,另一种是询问a到b有多少种不同的颜色。
思路:线覆盖型线段树,不能传递。
代码:
#include<iostream>#include <string.h>using namespace std; struct edge{ int l,r; int color;}tree[800001];int n,m,t;int v[31]; void build(int s,int t,int p){ tree[p].l=s; tree[p].r=t; tree[p].color=1; if(t>s) { int mid=(s+t)>>1; build(s,mid,p*2); build(mid+1,t,p*2+1); }} void update(int l,int r,int color,int p){ if(l==tree[p].l&&r==tree[p].r) { tree[p].color=color; return; } else { if(tree[p].color>0) { tree[2*p].color=tree[p].color; tree[2*p+1].color=tree[p].color; tree[p].color=0; } int mid=(tree[p].l+tree[p].r)>>1; if(r<=mid) update(l,r,color,2*p); else { if(l>mid) update(l,r,color,2*p+1); else { update(l,mid,color,2*p); update(mid+1,r,color,2*p+1); } } }} void query(int l,int r,int p){ int i,j,k; if(tree[p].color>0) { v[tree[p].color]=1; return; } int mid=(tree[p].l+tree[p].r)>>1; if(r<=mid) query(l,r,2*p); else if(l>mid) query(l,r,2*p+1); else { query(l,mid,2*p); query(mid+1,r,2*p+1); }} int main(){ int i,j,k,s,tmp; scanf("%d%d%d",&n,&t,&m); build(1,n,1); char c; for(i=1;i<=m;i++) { cin>>c; if(c=='C') { scanf("%d%d%d",&j,&k,&s); if(j>k) swap(j,k);update(j,k,s,1); } else { scanf("%d%d",&j,&k); if(j>k) swap(j,k); memset(v,0,sizeof(v)); query(j,k,1); j=0; for(k=1;k<=t;k++) if(v[k]) j++; cout<<j<<endl; } } return 0;}
阅读全文
0 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(线段树)
- 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(线段树#1)
- POJ 2777 Count Color (线段树)
- POJ 2777 Count Color 线段树
- POJ 2777 Count Color 线段树
- Nginx+Tomcat+Memcached负载均衡集群服务搭建
- 机器学习-正则
- js最新手机号码、电话号码正则表达式
- 《Android开发艺术探索》笔记——Bitmap的加载和Cache(一)
- 网上资源汇总深度学习和机器学习实战例子
- POJ 2777 Count Color(线段树)
- 面试题3
- 基于Linux系统的TCP协议的即时通信系统
- AIM Tech Round 4 (Div. 2) C
- matlab2c使用c++实现matlab函数系列教程-flipud函数
- 安卓开发艺术探索笔记第一章
- 区块链3.0(二):超越货币、经济和市场的效率和协作应用
- Neo4J CQL -(19)- 字符串函数
- JQuery自定义命名空间