POJ2777
来源:互联网 发布:画网络计划图的步骤 编辑:程序博客网 时间:2024/04/27 17:36
摘要:颜色覆盖,典型的线段树应用
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
typedef struct Node{
int left;
int right;
int color;
Node * left_child;
Node * right_child;
void Insert(int, int, int);
void Calculate(int, int);
void Construct(int, int);
}Node;
const int size = 100000;
Node sTree[size * 6];
Node *root = &sTree[0];
int len = 1;
int result[size+1] = {0};
int cnt = 0;
bool hash_table[31] = {0};
void Node::Construct(int l, int r)
{
left = l;
right = r;
color = 1;
if(l == r){
left_child = right_child = NULL;
return;
}
left_child = &sTree[len++];
right_child = &sTree[len++];
int mid = (l + r) >> 1;
left_child->Construct(l, mid);
right_child->Construct(mid+1, r);
}
void Node::Insert(int l, int r, int c)
{
if(color == c){
return;
}
if(left==l && right==r){
color = c;
return;
}
if( color != -1){
left_child->color = color;
right_child->color = color;
}
color = -1;
int mid = (left + right) >> 1;
if(l > mid){
right_child->Insert(l, r, c);
return;
}
if(r <= mid){
left_child->Insert(l, r, c);
return;
}
left_child->Insert(l, mid, c);
right_child->Insert(mid+1, r, c);
}
void Node::Calculate(int l, int r)
{
if(color != -1){
if( !hash_table[color] ){
hash_table[color] = true;
cnt++;
}
return;
}
int mid = (left + right) >> 1;
if( l > mid ){
right_child->Calculate(l, r);
return;
}
if( r <= mid ){
left_child->Calculate(l, r);
return;
}
left_child->Calculate(l, mid);
right_child->Calculate(mid+1, r);
}
int main()
{
int L, T, N;
scanf("%d%d%d", &L, &T, &N);
len = 1;
root->Construct(1, L);
getchar();
for(int i=1; i<=N; i++){
int first, second, color;
char op;
op = getchar();
scanf("%d%d", &first , &second);
if( op == 'C' ){
scanf("%d", &color);
int left = first<second ? first : second;
int right = first + second - left;
root->Insert(left, right, color);
}else{
int left = first<second ? first : second;
int right = first + second - left;
memset(hash_table, 0, sizeof(hash_table));
cnt = 0;
root->Calculate(left, right);
printf("%d/n", cnt);
}
getchar();
}
return 0;
}
- POJ2777
- poj2777
- poj2777
- poj2777
- poj2777
- POJ2777
- POJ2777
- poj2777
- poj2777
- poj2777
- poj2777
- POJ2777
- poj2528 &&poj2777
- Count Color[poj2777]
- 线段树模板 poj2777
- POJ2777---线段树
- poj2777 Count Color
- poj2777 Count Color
- 关于Npmaker的臆想
- Struts2标签使用
- POJ2352
- eclipse 中svn的安装和使用
- 深入学习Heritrix---解析Frontier(链接工厂)
- POJ2777
- POJ2828
- POJ3277
- 091114(星期六) 项目回顾和近两个月的调整
- struts2标签使用
- 如何在sprint会议上讨论backlog的细节?
- The Standard Librarian: What Are Allocators Good For?
- Abstract from(1)
- Heritrix 架构