Cross Counting
Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 1331   Accepted: 375

Description

Given a N × M grid with different colors on each cell, your task is to calculate the total amount of crosses of a specific color. We say there exists a cross of size
k centered at the cell (x,y) iff all cells lying in the
x-th row or the y-th column and within a distance of k from (x,y) share the same color. Note that if two crosses have the same center but different sizes we consider they are distinct. Unfortunately, the color of
each cell may varies by time and you have to respond to all the queries.

Input

There are four integers, N, M, C, Q, in the first line. (1 ≤
N, M, C ≤ 100, 1 ≤ Q ≤ 10000)

The next N lines each contains M integers between 1 and C which describe the color of cells.

The following Q lines each has either the form "C i j k" indicating to change the color of cell (i,
j) into k, or the form "Q c" indicating to query the total amount of crosses of color
c. (1 ≤ iN, 1 ≤ jM, 1 ≤ k,
cC)

Output

Output the answer to each query.

Sample Input

5 5 3 6
1 3 2 3 1
3 3 2 3 3
2 2 2 2 2
3 3 2 3 3
1 3 2 3 1
Q 1
Q 2
Q 3
C 2 3 3
C 3 2 3
Q 3

Sample Output

0
2
0
1

Source

题目看起来有线段树的味道,我线段树能力有限。

发现N,M都非常少,用O(N*M*(N+M))做预处理。

对于每次更新。改变的是十字架。所以更新须要O(N+M)个更新。

我在外层加了一层哨兵。写起来比較顺畅。

/***********************************************************
> OS : Linux 3.13.0-24-generic (Mint-17)
> Author : yaolong
> Mail : dengyaolong@yeah.net
> Time : 2014年10月15日 星期三 07时11分26秒
**********************************************************/
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
using namespace std;
const int N = 205;
int mp[N][N];
int cnt[N][N];
int color[N];
void update ( int i, int j )
{
cnt[i][j] = 0;
for ( int k = 1;; k++ )
{
if ( mp[i + k][j] == mp[i][j] && mp[i - k][j] == mp[i][j] && mp[i][j + k] == mp[i][j] && mp[i][j - k] == mp[i][j] )
{
cnt[i][j]++;
}
else
{
return ;
}
}
}
int main()
{
int n, m, c, q;
while ( scanf ( "%d%d%d%d", &n, &m, &c, &q ) != EOF )
{
int i, j, k;
memset ( color, 0, sizeof ( color ) );
memset ( mp, 63, sizeof ( mp ) );
memset ( cnt, 0, sizeof ( cnt ) );
for ( i = 1; i <= n; i++ )
{
for ( j = 1; j <= m; j++ )
{
scanf ( "%d", &mp[i][j] );
}
}
for ( i = 1; i <= n; i++ )
{
for ( j = 1; j <= m; j++ )
{
update ( i, j );
color[mp[i][j]] += cnt[i][j];
}
}
char tmp;
while ( q-- )
{
scanf ( " %c", &tmp );
if ( tmp == 'Q' )
{
scanf ( "%d", &i );
printf ( "%d\n", color[i] );
}
else
{
scanf ( "%d%d%d", &i, &j, &k );
if ( mp[i][j] == k )
{
continue;
}
color[mp[i][j]] -= cnt[i][j];
mp[i][j] = k;
update ( i, j );
color[mp[i][j]] += cnt[i][j];
//cout<<cnt[i][j]<<" "<<i<<j<<endl;
for ( int ak = 1; ak <= n; ak++ )
{
if ( i != ak )
{
color[mp[ak][j]] -= cnt[ak][j]; //减掉
update ( ak, j );
color[mp[ak][j]] += cnt[ak][j];
}
}
for ( int ak = 1; ak <= m; ak++ )
{
if ( j != ak )
{
color[mp[i][ak]] -= cnt[i][ak]; //减掉
update ( i, ak );
color[mp[i][ak]] += cnt[i][ak];
}
}
}
}
}
return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

POJ3467(预处理)的更多相关文章

  1. 借助 SIMD 数据布局模板和数据预处理提高 SIMD 在动画中的使用效率

    原文链接 简介 为发挥 SIMD1 的最大作用,除了对其进行矢量化处理2外,我们还需作出其他努力.可以尝试为循环添加 #pragma omp simd3,查看编译器是否成功进行矢量化,如果性能有所提升 ...

  2. 前端学PHP之PDO预处理语句

    × 目录 [1]定义 [2]准备语句 [3]绑定参数[4]执行查询[5]获取数据[6]大数据对象 前面的话 本来要把预处理语句和前面的基础操作写成一篇的.但是,由于博客园的限制,可能是因为长度超出,保 ...

  3. C语言之预处理

    这是2016年的最后一篇博客,年初定的计划是写12篇博客,每月一篇,1/3转载,2/3原创,看来是实现不了了! -- 题外话.今天要写的东西是C语言中的预处理器,我们常说的宏定义的用法.为什么要写这个 ...

  4. CSS3与页面布局学习总结(七)——前端预处理技术(Less、Sass、CoffeeScript、TypeScript)

    CSS不像其它高级语言一样支持算术运算.变量.流程控制与面向对象特性,所以CSS样式较多时会引起一些问题,如修改复杂,冗余,某些别的语言很简单的功能实现不了等.而javascript则是一种半面向对象 ...

  5. 【NLP】Tika 文本预处理:抽取各种格式文件内容

    Tika常见格式文件抽取内容并做预处理 作者 白宁超 2016年3月30日18:57:08 摘要:本文主要针对自然语言处理(NLP)过程中,重要基础部分抽取文本内容的预处理.首先我们要意识到预处理的重 ...

  6. GCC 预处理、编译、汇编、链接..

    1简介 GCC 的意思也只是 GNU C Compiler 而已.经过了这么多年的发展,GCC 已经不仅仅能支持 C 语言:它现在还支持 Ada 语言.C++ 语言.Java 语言.Objective ...

  7. 预处理指令#pragma

    #pragma介绍 #pragma是一个预处理指令,pragma的中文意思是『编译指示』.它不是Objective-C中独有的东西(貌似在C/C++中使用比较多),最开始的设计初衷是为了保证代码在不同 ...

  8. 预处理命令[#define]说明

    宏定义 宏定义是对一些常见的变量.字符串等进行定义,被定义的数据在编译会进行自动替换.有时一些变量或字符串被多次使用,当需要修改时,就需要对源文件中它们出现的地方一一修改,效率比较低,而通过宏定义,只 ...

  9. iOS开发系列--C语言之预处理

    概述 大家都知道一个C程序的运行包括编译和链接两个阶段,其实在编译之前预处理器首先要进行预处理操作,将处理完产生的一个新的源文件进行编译.由于预处理指令是在编译之前就进行了,因此很多时候它要比在程序运 ...

随机推荐

  1. 美版SOLOWHEEL与盗版SOLOWHEEL-IPS独轮车终极PK【图】_厂商资讯_太平洋电脑网

    http://g.pconline.com.cn/x/330/3304676.html

  2. OpenCV 通过 MFC 的 Picture Control 控件操作图像

    假设希望对显示在MFC Picture Control 控件里的图像进行操作,比方画线画点之类的,能够利用 OpenCV 结合 MFC 本身的鼠标响应函数来实现. 怎样将图像显示到 Picture C ...

  3. java获取日期之间的差异

    转载请注明出处.谢谢http://blog.csdn.net/harryweasley/article/details/42121485 当想到要计算差值.我们肯定想的是"2014.12.1 ...

  4. Linux 文件系统(二)---运行过程及结构间的关系

    (内核2.4.37) 一.首先.看看磁盘.超级块,inode节点在物理上总体的分布情况: (图示来自:www.daoluan.net) 对于一个分区,相应一个文件系统,一个文件系统事实上本质上还是磁盘 ...

  5. 蓝缘管理系统第二个版本号开源了。springMVC+springSecurity3.x+Mybaits3.x 系统

    蓝缘管理系统第二个版本号开源了 继于 http://blog.csdn.net/mmm333zzz/article/details/16863543 版本号一.版本号二 对springMVC+spri ...

  6. 搭建ganglia集群而且监视hadoop CDH4.6

    前言 近期在研究云监控的相关工具,感觉ganglia颇有亮点,能从一个集群总体的角度来展现数据. 但是安装过程稍过复杂,相关依赖稍多,故写此文章与大家分享下. 本文不解说相关原理,若想了解请參考其它资 ...

  7. HibernateReview Day1 - Introduction

    Hibernate已经学过去大概有半个月了,然后默默的忘掉了……所谓Practice makes perfect. 我尽力重新拾起来. 1.什么是ORM 在介绍Hibernate之前,我们先学习下OR ...

  8. hibernate 大对象类型hibernate制图

    基础知识: 在 Java 在, java.lang.String 它可以用来表示长串(超过长度 255), 字节数组 byte[] 可用于存放图片或文件的二进制数据. 此外, 在 JDBC API 中 ...

  9. shell脚本中的数学运算

    shell中的赋值和操作默认都是字符串处理,在此记下shell中进行数学运算的几个特殊方法.以后用到的时候能够来看,呵呵 1.错误方法举例 a) var=1+1 echo $var 输出的结果是1+1 ...

  10. Java实现字符全阵列阵列

    import org.junit.Test; public class AllSort { public void permutation(char[] buf, int start, int end ...