poj 2777 Count Color(线段树)
题目地址:http://poj.org/problem?id=2777
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 30995 | Accepted: 9285 |
Description
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
Output
Sample Input
- 2 2 4
C 1 1 2- P 1 2
- C 2 2 2
- P 1 2
Sample Output
- 2
- 1
【题解】:典型线段树区间更新
【code】:
- /**
- result:Accepted memory:4264K
- time:375MS language:C++
- code lenght: 2147B Acmer:cj
- */
- #include<iostream>
- #include<stdio.h>
- #include<string.h>
- #include<algorithm>
- #define N 100010
- #define lson p<<1
- #define rson p<<1|1
- using namespace std;
- struct Nod
- {
- int l,r;
- int color; //颜色
- int flag; //[l,r]区间是否同色
- }node[N<<];
- int mark[]; //颜色统计标记
- void building(int l,int r,int p) //建树
- {
- node[p].l = l;
- node[p].r = r;
- node[p].color = ;
- node[p].flag = ;
- if(l==r) return;
- int mid = (l+r)>>;
- building(l,mid,lson);
- building(mid+,r,rson);
- }
- void update(int l,int r,int p,int c)
- {
- if(node[p].l==l&&node[p].r==r)
- {
- node[p].flag = ;
- node[p].color = c;
- return;
- }
- if(node[p].color!=c&&node[p].flag) //向下更新
- {
- node[p].flag = ;
- node[lson].flag=node[rson].flag = ;
- node[lson].color=node[rson].color = node[p].color;
- }
- int mid = (node[p].l+node[p].r)>>;
- if(r<=mid) update(l,r,lson,c);
- else if(l>mid) update(l,r,rson,c);
- else
- {
- update(l,mid,lson,c);
- update(mid+,r,rson,c);
- }
- if(node[lson].flag&&node[rson].flag&&node[lson].color==node[rson].color) //向上更新
- {
- node[p].flag = ;
- node[p].color = node[lson].color;
- }
- }
- void query(int l,int r,int p)
- {
- if(node[p].flag) //区间同色
- {
- mark[node[p].color]=; //标记颜色出现过
- return;
- }
- int mid = (node[p].l+node[p].r)>>;
- if(r<=mid) query(l,r,lson);
- else if(l>mid) query(l,r,rson);
- else
- {
- query(l,mid,lson);
- query(mid+,r,rson);
- }
- }
- int main()
- {
- int L,T,O;
- scanf("%d%d%d",&L,&T,&O);
- building(,L,);
- while(O--)
- {
- char op[];
- int a,b,c;
- scanf("%s",op);
- if(op[]=='C')
- {
- scanf("%d%d%d",&a,&b,&c);
- update(a,b,,c);
- }
- else if(op[]=='P')
- {
- int i;
- scanf("%d%d",&a,&b);
- memset(mark,,sizeof(mark)); //初始化颜色标记
- query(a,b,);
- int ans = ;
- for(i=;i<=T;i++) if(mark[i]) ans++;
- printf("%d\n",ans);
- }
- }
- return ;
- }
poj 2777 Count Color(线段树)的更多相关文章
- poj 2777 Count Color(线段树区区+染色问题)
题目链接: poj 2777 Count Color 题目大意: 给出一块长度为n的板,区间范围[1,n],和m种染料 k次操作,C a b c 把区间[a,b]涂为c色,P a b 查 ...
- poj 2777 Count Color - 线段树 - 位运算优化
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 42472 Accepted: 12850 Description Cho ...
- poj 2777 Count Color(线段树、状态压缩、位运算)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 38921 Accepted: 11696 Des ...
- POJ 2777 Count Color(线段树之成段更新)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33311 Accepted: 10058 Descrip ...
- POJ 2777 Count Color (线段树成段更新+二进制思维)
题目链接:http://poj.org/problem?id=2777 题意是有L个单位长的画板,T种颜色,O个操作.画板初始化为颜色1.操作C讲l到r单位之间的颜色变为c,操作P查询l到r单位之间的 ...
- POJ P2777 Count Color——线段树状态压缩
Description Chosen Problem Solving and Program design as an optional course, you are required to sol ...
- POJ 2777 Count Color(段树)
职务地址:id=2777">POJ 2777 我去.. 延迟标记写错了.标记到了叶子节点上.. . . 这根本就没延迟嘛.. .怪不得一直TLE... 这题就是利用二进制来标记颜色的种 ...
- poj 2777 Count Color
题目连接 http://poj.org/problem?id=2777 Count Color Description Chosen Problem Solving and Program desig ...
- POJ 2777 Count Color(线段树染色,二进制优化)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 42940 Accepted: 13011 Des ...
随机推荐
- Qt之读取配置文件
一.读取配置文件增删功能与修改参数数据 #ifndef CONFIG_H #define CONFIG_H #define QS_FILEPATH "E:\\woo\\Code\\Qt\\A ...
- Java Concurrency - ReadWriteLock & ReentrantReadWriteLock
锁所提供的最重要的改进之一就是 ReadWriteLock 接口和它的实现类 ReentrantReadWriteLock.这个类提供两把锁,一把用于读操作和一把用于写操作.同一时间可以有多个线程执行 ...
- django 学习-6 定义模型--数据库的使用
1.service mysqld start 首先数据库是可用的 2.rpm -qa |grep MySQL-python 这个包是存在的 3.vim settings 修改databases 加 ...
- 推荐一款App运营工具:AYL爱盈利App榜单监控
对包括开发者.产品运营.投资人在内的诸多移动互联网从业人员而言,国内Android应用市场和IOS应用市场的榜单变化数据时大家的必修功课之一:看看这段时间所关注的垂直领域里最火的是哪几款应用:看看竞争 ...
- Objective-C(iOS)严格单例模式正确实现
注:本文所有权归作者所有,转载请注明出处 当希望在一个应用程序中某个类的对象只能存在一个的时候就可以考虑用单例模式来实现,单例模式在C++中比较容易实现(只需把构造函数声明为private),而在Ob ...
- host文件的作用和介绍
在Window系统中有个Hosts文件(没有后缀名)在Windows98系统下该文件在Windows目录,在Windows2000/XP系统中位于C:\Winnt\System32\Drivers\E ...
- C++对象的JSON序列化与反序列化探索续-复杂对象的序列化与反序列化
本文是基本上一篇博文进行改进而成,上一篇请见: C++对象的JSON序列化与反序列化探索 此处就不多说了,直接上代码. 1. 序列化基类 #pragma once #include <strin ...
- Windows Phone使用sliverlight toolkit实现页面切换动画效果
使用应用时,好多app在页面切换的时候都有一个动画效果,感觉很炫,也大大增加了用户体验,怎么实现呢? 界面的切换,可以用Windows Phone Toolkit中的TransitionService ...
- stl::search
template<class ForwardIt1, class ForwardIt2> ForwardIt1 search(ForwardIt1 first, ForwardIt1 la ...
- 01_mvc保存时出错
修改实体类报错 存储区更新.插入或删除语句影响到了意外的行数(0).实体在加载后可能被修改或删除.刷新 ObjectStateManager 项. 原因是 数据表中的自增主键列未赋值.