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. Yii学习笔记之三(在windows 上安装 advanced )

    首先说一下下载地址: http://www.yiiframework.com/download/ 然后将下载下来的文件进行解压到 你指定的文件夹 解压过程中假设报什么错误 直接忽略掉 我的解压文件夹是 ...

  2. POJ3050 Hopscotch 【DFS】

    Hopscotch Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2113   Accepted: 1514 Descrip ...

  3. 关于http接口开发中json格式数据编码问题处理

    关于http接口开发中json格式数据编码问题处理 在实际工作中,接口很多时候返回json格式,但有时返回的格式会有编码问题 假设如下接口:http://service.test.com/interf ...

  4. alertify、js、css 使用简介

    Alertify.js which helped me resolve my issues regarding prompts, alerts, confirms, etc in iOS7. 1.al ...

  5. mysql 利用触发器(Trigger)让代码更简单

    一,什么触发器 1,个人理解 触发器,从字面来理解,一触即发的一个器,简称触发器(哈哈,个人理解),举个例子吧,好比天黑了,你开灯了,你看到东西了.你放炮仗,点燃了,一会就炸了. 2,官方定义 触发器 ...

  6. Java&Android反编工具打包

    Java&Android反编工具: 1.Eclipse反编插件:安装到Eclipse后,可以简要的查看jar包中的*.class; 2.DoAPK:反编*.apk为smali和一些资源文件,可 ...

  7. Red Gate系列之二 SQL Source Control 3.0.13.4214 Edition 数据库版本控制器 完全破解+使用教程

    原文:Red Gate系列之二 SQL Source Control 3.0.13.4214 Edition 数据库版本控制器 完全破解+使用教程 Red Gate系列之二 SQL Source Co ...

  8. Python多线程的threading Event

    Python threading模块提供Event对象用于线程间通信.它提供了一组.拆除.等待用于线程间通信的其他方法. event它是沟通中最简单的一个过程之中,一个线程产生一个信号,号.Pytho ...

  9. 解决SMARTFORMS 中table 控件单行跨页的问题

    在CX项目中,MM模块做了大量的的单据打印的工作,一个问题困扰了我好久,一直不能解决.当物料描述很长时,table控件在单元格中能自动换行,这样就有可能在换页处出现一行记录的一部分打在上一页,一部分记 ...

  10. doc-remote-debugging.html

    https://studio.zerobrane.com/doc-remote-debugging.html