POJ3467(預先處理)

來源:互聯網
上載者:User

標籤:

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 sizek centered at the cell (x,y) iff all cells lying in thex-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 colorc. (1 ≤ i ≤ N, 1 ≤ j ≤ M, 1 ≤ k,c ≤ C)

Output

Output the answer to each query.

Sample Input

5 5 3 61 3 2 3 13 3 2 3 32 2 2 2 23 3 2 3 31 3 2 3 1Q 1Q 2Q 3C 2 3 3C 3 2 3Q 3

Sample Output

0201

Source

POJ Monthly--2007.11.25, Yang Yi

題目看起來有線段樹的味道,我線段樹能力有限。

發現N,M都非常少,用O(N*M*(N+M))做預先處理。

對於每次更新。改變的是十字架。所以更新須要O(N+M)個更新。

我在外層加了一層哨兵。寫起來比較順暢。


/***********************************************************> OS     : Linux 3.13.0-24-generic (Mint-17)> Author : yaolong> Mail   : [email protected]> 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(預先處理)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.