poj 2777 Count Color(位运算+线段树区间更新 可用bitset记录)
来源:互联网 发布:nginx docker 负载均衡 编辑:程序博客网 时间:2024/06/14 02:56
Count Color
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 43754 Accepted: 13239
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
题意:
有一块长为l的板子,初始时涂上颜色1,现在有两种操作:
C l r c:将区间[l,r]涂上颜色c
P l r:将区间[l,r]颜色种类输出。
总的颜色种类数<=30。
题解:
看了题解才恍然大悟,由于颜色数目很少,可以在线段树结点上用二进制的一位记录该区间是否有该颜色(也可以用bitset记录),那么区间合并时只需要|一下就行了。
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<vector>using namespace std;#define rep(i,a,n) for (int i=a;i<n;i++)#define per(i,a,n) for (int i=n-1;i>=a;i--)#define pb push_back#define mp make_pair#define fi first#define se secondtypedef vector<int> VI;typedef long long ll;typedef pair<int,int> PII;const ll mod=1000000007;const int MAXN=100000+100;struct node{int l,r,tag,lazy,sum;}a[MAXN*4];void build(int i,int l,int r){a[i].l=l,a[i].r=r,a[i].sum=2,a[i].tag=0;if(l==r) return;int m=(l+r)/2;build(i*2,l,m);build(i*2+1,m+1,r);}void pushdown(int i){//a[i].tag=a[i].sum=0;if(a[i].l!=a[i].r){a[i*2].tag=a[i*2+1].tag=a[i].tag;a[i*2].sum=a[i*2+1].sum=(1<<a[i].tag);a[i].tag=0;}return;}void update(int i,int l,int r,int c){if(a[i].l==l&&a[i].r==r){a[i].tag=c;a[i].sum=(1<<c);return;}if(a[i].tag)pushdown(i);int m=(a[i].l+a[i].r)/2;if(r<=m) update(i*2,l,r,c);else if(l>m) update(i*2+1,l,r,c);else {update(i*2,l,m,c);update(i*2+1,m+1,r,c);}a[i].sum=a[i*2].sum|a[i*2+1].sum;return;}int query(int i,int l,int r){if(a[i].l==l&&a[i].r==r){return a[i].sum;}if(a[i].tag) pushdown(i);int m=(a[i].l+a[i].r)/2;if(r<=m) return query(i*2,l,r);else if(l>m) return query(i*2+1,l,r);else return query(i*2,l,m)|query(i*2+1,m+1,r);}int get(int x){int ans=0;while(x){if(x&1) ans++;x/=2;}return ans;}int main(){int l1,t,q;scanf("%d%d%d",&l1,&t,&q);build(1,1,l1);while(q--){char s[2];scanf("%s",s);int l,r,c;if(s[0]=='C') {scanf("%d%d%d",&l,&r,&c);if(l>r) swap(l,r);update(1,l,r,c);}else {scanf("%d%d",&l,&r);if(l>r) swap(l,r);int ans=get(query(1,l,r));printf("%d\n",ans);}}return 0;}
bitset记录:
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<vector>#include<bitset>using namespace std;#define rep(i,a,n) for (int i=a;i<n;i++)#define per(i,a,n) for (int i=n-1;i>=a;i--)#define pb push_back#define mp make_pair#define fi first#define se secondtypedef vector<int> VI;typedef long long ll;typedef pair<int,int> PII;const ll mod=1000000007;const int MAXN=100000+100;typedef bitset<31> P;struct node{int l,r,tag,lazy;P sum;}a[MAXN*4];void build(int i,int l,int r){a[i].l=l,a[i].r=r,a[i].sum=2,a[i].tag=0;if(l==r) return;int m=(l+r)/2;build(i*2,l,m);build(i*2+1,m+1,r);}void pushdown(int i){if(a[i].l!=a[i].r){a[i*2].tag=a[i*2+1].tag=a[i].tag;a[i*2].sum=(1<<a[i].tag);a[i*2+1].sum=(1<<a[i].tag);a[i].tag=0;}return;}void update(int i,int l,int r,int c){if(a[i].l==l&&a[i].r==r){a[i].tag=c;a[i].sum=(1<<c);return;}if(a[i].tag)pushdown(i);int m=(a[i].l+a[i].r)/2;if(r<=m) update(i*2,l,r,c);else if(l>m) update(i*2+1,l,r,c);else {update(i*2,l,m,c);update(i*2+1,m+1,r,c);}a[i].sum=a[i*2].sum|a[i*2+1].sum;return;}P query(int i,int l,int r){if(a[i].l==l&&a[i].r==r){return a[i].sum;}if(a[i].tag) pushdown(i);int m=(a[i].l+a[i].r)/2;if(r<=m) return query(i*2,l,r);else if(l>m) return query(i*2+1,l,r);else return query(i*2,l,m)|query(i*2+1,m+1,r);}int get(int x){int ans=0;while(x){if(x&1) ans++;x/=2;}return ans;}int main(){int l1,t,q;scanf("%d%d%d",&l1,&t,&q);build(1,1,l1);while(q--){char s[2];scanf("%s",s);int l,r,c;if(s[0]=='C') {scanf("%d%d%d",&l,&r,&c);if(l>r) swap(l,r);update(1,l,r,c);}else {scanf("%d%d",&l,&r);if(l>r) swap(l,r);P ans=query(1,l,r);printf("%d\n",ans.count());}}return 0;}
0 0
- poj 2777 Count Color(位运算+线段树区间更新 可用bitset记录)
- POJ 2777 Count Color 线段树区间更新位运算
- POJ 2777 Count Color(位运算+线段树+lazy+区间更新)
- POJ 2777 Count Color 【线段树 区间更新 按位或运算】
- POJ 2777Count Color (线段树区间更新 + 位运算压缩)
- poj 2777 Count Color(线段树区间更新+位运算)
- Count Color+POJ+线段树区间更新+位运算(lazy标志的运用)
- 线段树区间更新,区间统计 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 (线段树区间更新)
- 平民
- 闭包的好处
- 不使用乘号 除号 计算两个数字的乘 和 除
- 网络编程
- HTML 13 表格
- poj 2777 Count Color(位运算+线段树区间更新 可用bitset记录)
- Problem 38 Pandigital multiples (set + vector)
- Swift3.0 popToViewController 的使用
- 2016 NCPC Game Rank(模拟)
- c++实现日期类(class Date) 构造函数 拷贝构造 操作符重载(输入输出 比较操作 算数运算 自增自减)
- Leetcode #343 Integer Break
- 优化php效率,提高php性能的一些方法:
- C# socket学习--IPAddress、IPEndPoint、DNS
- Python 包管理工具解惑