注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

东月之神

在单纯的观念里面,生命就容易变得比较深刻!

 
 
 

日志

 
 
关于我

别驻足,梦想要不停追逐,别认输,熬过黑暗才有日出,要记住,成功就在下一步,路很苦,汗水是最美的书!

网易考拉推荐

Count Color  

2010-05-14 10:22:10|  分类: ACM |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

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
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
struct Node
{
 int l, r, mid;
 int col;
}tree[100001*3];
bool hash[32];
int ans;
inline void make_tree(int left, int right, int now)
{
 tree[now].l = left;
 tree[now].r = right;
 tree[now].mid = (tree[now].l + tree[now].r) >> 1;
 tree[now].col = 0;
 if(left == right) return ;
 make_tree(left, tree[now].mid, now+now);
 make_tree(tree[now].mid+1, right, now+now+1);
}
inline void update(int left, int right, int c, int now)
{
 if(left == tree[now].l && right == tree[now].r)
 {
  tree[now].col = c;
  return ;
 }
 if(tree[now].col != 0)
 {
  tree[now+now].col = tree[now+now+1].col = tree[now].col;
  tree[now].col = 0;
 }
 if(right <= tree[now].mid) update(left, right, c, now+now);
 else if(left > tree[now].mid)  update(left, right, c, now+now+1);
 else
 {
  update(left, tree[now].mid, c, now+now);
  update(tree[now].mid+1, right, c, now+now+1);
 }
}
inline void query(int left, int right, int now)
{
 if(tree[now].col != 0)
 {
  if(hash[tree[now].col] != true)
  {
   hash[tree[now].col] = true;
   ans++;
  }
  return ;
 }
 if(right <= tree[now].mid) query(left, right, now+now);
 else if(left > tree[now].mid)  query(left, right, now+now+1);
 else
 {
  query(left, tree[now].mid , now+now);
  query(tree[now].mid+1, right , now+now+1);
 }
 
}
int main()
{
 int n, l, o;
 while(scanf("%d%d%d", &n, &l, &o) != EOF)
 {
  make_tree(1, n, 1); 
  tree[1].col = 1;
  while(o--)
  {     
   char a[3];
   int x, y, z;
   scanf("%s%d%d", a, &x, &y);
   if(x > y) swap(x, y);
   if(a[0] == 'C')
   {
    scanf("%d", &z);    
    update(x, y, z, 1);
   }
   else
   {
    memset(hash, false, sizeof(hash));
    ans = 0;
    query(x, y, 1);
    printf("%d\n", ans);
   }
  }
 }
return 0;
}
  评论这张
 
阅读(266)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017