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. SWT中在treeview中显示图片

    package com.repositoryclient.treeview; import org.eclipse.jface.resource.ImageDescriptor; import org ...

  2. IIS架构与HTTP请求处理流程

    IIS架构与HTTP请求处理流程 Windows操作系统中的IIS负责提供互联网服务,一台运行了IIS的计算机可以看成是一台Web服务器. Windows XP SP2 中IIS主版本号为5,Wind ...

  3. java api例子网站

    http://www.programcreek.com/java-api-examples/ http://www.apihome.cn/api/list/ http://www.docjar.com ...

  4. ipconfig /flushdns 清除系统DNS缓存

    1.ipconfig /flushdns的作用 ipconfig /flushdns 这是清除DNS缓存用的. 当訪问一个站点时系统将从DNS缓存中读取该域名所相应的IP地址,当查找不到时就会到系统中 ...

  5. Thread Dump 和Java应用诊断(转)

    Thread Dump 和Java应用诊断 Thread Dump是非常有用的诊断Java应用问题的工具,每一个Java虚拟机都有及时生成显示所有线程在某一点状态的thread-dump的能力.虽然各 ...

  6. Linux内核的同步机制---自旋锁

    自旋锁的思考:http://bbs.chinaunix.net/thread-2333160-1-1.html 近期在看宋宝华的<设备驱动开发具体解释>第二版.看到自旋锁的部分,有些疑惑. ...

  7. 每日回顾Shell —cat,tail,head

    Shell中常常会用到cat命令.可是总是不是特别清楚: cat命令的用途是连接文件或标准输入并打印. 这个命令经常使用来显示文件内容.或者将几个文件连接起来显示.或者从标准输入读取内容并显示,它常与 ...

  8. HttpDNS 服务详解(转)

    但凡使用域名来给用户提供服务的互联网企业,都或多或少地无法避免在有中国特色的互联网环境中遭遇到各种域名被缓存.用户跨网访问缓慢等问题.那么对于腾讯这样的域名数量在10万级别的互联网公司来讲,域名解析异 ...

  9. Codeforces Round #256 (Div. 2) C. Painting Fence 或搜索DP

    C. Painting Fence time limit per test 1 second memory limit per test 512 megabytes input standard in ...

  10. C++ Primer 学习笔记_57_类和数据抽象 --管理指针成员

    复印控制 --管理指针成员 引言: 包括指针的类须要特别注意复制控制.原因是复制指针时.一个带指针成员的指针类 class HasPtr { public: HasPtr(int *p,int i): ...