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. vb.net版机房收费——助你学会七层架构(一)

    我自己写机房的时候,看非常多高人的博客,各种的借鉴,当初务必的纠结,如今整个机房敲完了,写这篇博客给大家一个总体上的.简单理解的七层,期望大家看完这篇文章之后,不会这个纠结了. 首先大家得看了我的上一 ...

  2. Mac 登录界面多了一个其它账户删除

    原因分析: 在安装一些软件时会自己主动启用root账户,可是在安装完毕后没有关闭root账户,这样就造成系统以为用户要使用root账户,所以在登录界面出现了一个"其它"账户 解决方 ...

  3. Beginning Python From Novice to Professional (4) - 演示样本格式字符串

    $ gedit price.py #!/usr/bin/env python width = input('Please enter width: ') price_width = 10 item_w ...

  4. Codeforces 191 C Fools and Roads (树链拆分)

    主题链接~~> 做题情绪:做了HDU 5044后就感觉非常easy了. 解题思路: 先树链剖分一下,把树剖分成链,由于最后全是询问,so~能够线性操作.经过树链剖分后,就会形成很多链,可是每条边 ...

  5. java中线程机制

    java中线程机制,一开始我们都用的单线程.现在接触到多线程了. 多线性首先要解决的问题是:创建线程,怎么创建线程的问题: 1.线程的创建: 四种常用的实现方法 1.继承Thread. Thread是 ...

  6. linux环境下的线程的创建问题

    pthread_create函数用于创建一个线程 函数原型 #include<pthread.h> int pthread_create(pthread_t *restrict tidp, ...

  7. Python学习入门基础教程(learning Python)--3.3.1 Python下的布尔表达式

    简单的说就是if要判断condition是真是假,Python和C语言一样非0即真,所以如果if的condition是布尔表达式我们可以用True或者非0数(不可是浮点数)表示真,用False或者0表 ...

  8. Java EE (7) -- Java EE 6 Enterprise Architect Certified Master(1z0-807)

    Application Design Concepts and Principles Identify the effects of an object-oriented approach to sy ...

  9. C# 如何获取某用户的“我的文档”的目录

    Console.WriteLine(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)); System.E ...

  10. POJ---2243 Knight Moves 使用A*算法的广度优先搜索

    题目链接:http://poj.org/problem?id=2243 启发式搜索:启发式搜索就是在状态空间中的搜索对每一个搜索的位置进行评估,得到最好的位置,再从这个位置进行搜索直到目标.这样可以省 ...