首页 > Web开发 > 详细

洛谷4054 [JSOI2009]计数问题

时间:2018-09-24 15:33:30      阅读:128      评论:0      收藏:0      [点我收藏+]

原题链接

二维树状数组模板题。
对每一种颜色开一棵二维树状数组统计即可。

#include<cstdio>
using namespace std;
const int N = 310;
const int M = 110;
int C[M][N][N], a[N][N], n, m;
inline int re()
{
    int x = 0;
    char c = getchar();
    bool p = 0;
    for (; c < '0' || c > '9'; c = getchar())
        p |= c == '-';
    for (; c >= '0' && c <= '9'; c = getchar())
        x = x * 10 + c - '0';
    return p ? -x : x;
}
inline int lowbit(int x)
{
    return x & -x;
}
void add(int co, int x, int y, int z)
{
    int i;
    for (; x <= n; x += lowbit(x))
        for (i = y; i <= m; i += lowbit(i))
            C[co][x][i] += z;
}
int ask(int co, int x, int y)
{
    int s = 0, i;
    for (; x; x -= lowbit(x))
        for (i = y; i; i -= lowbit(i))
            s += C[co][x][i];
    return s;
}
int main()
{
    int i, j, x, y, xx, yy, q, p, z;
    n = re();
    m = re();
    for (i = 1; i <= n; i++)
        for (j = 1; j <= m; j++)
        {
            a[i][j] = re();
            add(a[i][j], i, j, 1);
        }
    q = re();
    while (q--)
    {
        p = re();
        if (!(p ^ 1))
        {
            x = re();
            y = re();
            z = re();
            add(a[x][y], x, y, -1);
            add(a[x][y] = z, x, y, 1);
        }
        else
        {
            x = re();
            xx = re();
            y = re();
            yy = re();
            z = re();
            printf("%d\n", ask(z, xx, yy) - ask(z, xx, y - 1) - ask(z, x - 1, yy) + ask(z, x - 1, y - 1));
        }
    }
    return 0;
}

洛谷4054 [JSOI2009]计数问题

原文:https://www.cnblogs.com/Iowa-Battleship/p/9695505.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!