BZOJ1452 [JSOI2009]Count Solution

题意:自行脑补

做法:直接开权值那么多的二维树状数组暴力。

Code:

#include <cstdio>
#include <cctype>
#include <iostream>
#include <algorithm>
using namespace std;

inline int getc() {
    static const int L = 1 << 15;
    static char buf[L], *S = buf, *T = buf;
    if (S == T) {
        T = (S = buf) + fread(buf, 1, L, stdin);
        if (S == T)
            return EOF;
    }
    return *S++;
}
inline int getint() {
    int c;
    while(!isdigit(c = getc()));
    int tmp = c - '0';
    while(isdigit(c = getc()))
        tmp = (tmp << 1) + (tmp << 3) + c - '0';
    return tmp;
}

int A[101][301][301], w[301][301];
int n, m;
void modify(int c, int x, int y, int add) {
    int t;
    for(; x <= n; x += x & -x) {
        for(t = y; t <= m; t += t & -t)
            A[c][x][t] += add;
    }
}
int get(int c, int x, int y) {
    int res = 0, t;
    for(; x; x -= x & -x) {
        for(t = y; t; t -= t & -t)
            res += A[c][x][t];
    }
    return res;
}

int main() {
    n = getint();
    m = getint();

    register int i, j;
    int x;
    for(i = 1; i <= n; ++i)
        for(j = 1; j <= m; ++j) {
            x = getint();
            w[i][j] = x;
            modify(x, i, j, 1);
        }

    int Q = getint();
    int ope, x1, y1, x2, y2, c;
    while(Q--) {
        ope = getint();
        if (ope == 1) {
            x1 = getint(), y1 = getint(), c = getint();
            modify(w[x1][y1], x1, y1, -1);
            modify(w[x1][y1] = c, x1, y1, 1);
        }
        else {
            x1 = getint(), x2 = getint(), y1 = getint(), y2 = getint();
            c = getint();
            printf("%d\n", get(c, x2, y2) - get(c, x1 - 1, y2) - get(c, x2, y1 - 1) + get(c, x1 - 1, y1 - 1));
        }
    }

    return 0;
}
时间: 2024-08-07 08:24:12

BZOJ1452 [JSOI2009]Count Solution的相关文章

[bzoj1452][JSOI2009]Count(树状数组)

1452: [JSOI2009]Count Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 2057  Solved: 1216[Submit][Status][Discuss] Description Input Output Sample Input Sample Output 1 2 HINT Source 裸得不能再裸了 暴力100个二维即可 1 #include<stdio.h> 2 #include<stdlib.h> 3

[bzoj1452] [JSOI2009]Count

来自FallDream的博客,未经允许,请勿转载,谢谢. 有一个n*m的矩阵,每个点有一个权值. 需要支持两种操作:1)改变一个点的权值2)查询一个矩形内权值为c的个数 n,m<=300 q<=200000 权值<=100 权值有点小吧?那就每种权值开一个二维树状数组呗 #include<cstdio> #include<iostream> #define MN 300 using namespace std; inline int read() { int x=

【二维树状数组】bzoj1452 [JSOI2009]Count

权值的种类只有100种,对每种开个二维BIT,然后是经典操作. 1 #include<cstdio> 2 using namespace std; 3 int n,m,q,x1,y1,x2,y2,val,op,a[301][301]; 4 struct BIT_2D 5 { 6 int d[301][301]; 7 void update(int x,const int &y,const int &V) 8 { 9 for(;x<=n;x+=(x&(-x))) 1

【BZOJ-1452】Count 树状数组 套 树状数组

1452: [JSOI2009]Count Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1769  Solved: 1059[Submit][Status][Discuss] Description Input Output Sample Input Sample Output 1 2 HINT Source Solution 忽略标题的说法...撞壁用的.... 简单题,裸树状数组套树状数组 颜色数目$c<=100$很小,考虑对于每种颜色单独进

1452: [JSOI2009]Count

1452: [JSOI2009]Count Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 2083  Solved: 1228[Submit][Status][Discuss] Description Input Output Sample Input Sample Output 1 2 HINT 思路:二维树状数组: 将数组开三维的然后,每一维维护一个值,然后直接二维树状数组维护即可: 1 #include<stdio.h> 2 #includ

BZOJ 1452: [JSOI2009]Count (二维树状数组)

Description Input Output Sample Input Sample Output 1 2 HINT 二维树状数组的简单应用,c数组的第一维坐标相当于哈希.如果是修改操作,修改前 将当前的值的个数以及祖先都减1, 修改后将个数加1. #include <iostream> #include <cstring> #include <cstdio> #include <cmath> #include <set> #include

1452: [JSOI2009]Count - BZOJ

Description Input Output Sample Input Sample Output 1 2HINT 一开始还想什么离线做,其实不用,空间足够,我们直接开100个二维树状数组,然后就行了 但是如果c范围很大就离线做好一些 1 type 2 tree=array[0..300,0..300]of longint; 3 var 4 s:array[0..100]of tree; 5 a:array[0..300,0..300]of longint; 6 n,m,q:longint;

BZOJ 1452: [JSOI2009]Count 二维树状数组

1452: [JSOI2009]Count Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/problem.php?id=1452 Description Input Output Sample Input Sample Output 1 2 HINT 题意 题解: 裸的二维树状数组 代码: //qscqesze #include <cstdio> #include <cmath&g

【BZOJ】1452: [JSOI2009]Count 树状数组

Description Input Output Sample Input Sample Output 1 2 HINT 题解: 二维的树状数组啊+一维的颜色状态,然后直接做就好……实际上比照一维的树状数组就是多了一个for循环,然后查询操作的时候就相当于查询某一矩阵的大小,树状数组起到一个类似前缀和的作用.   1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <