zoukankan      html  css  js  c++  java
  • 【JSOI 2009】 Count

    【题目链接】

               点击打开链接

    【算法】

                二维树状数组

    【代码】

               

    #include<bits/stdc++.h>
    using namespace std;
    #define MAXN 300
    #define MAXC 100
    
    int N,M,Q,i,j,opt,x,y,xa,ya,xb,yb,c;
    int a[MAXN+10][MAXN+10];
    
    template <typename T> inline void read(T &x) {
            int f = 1; x = 0;
            char c = getchar();
            for (; !isdigit(c); c = getchar()) { if (c == '-') f = -f; }
            for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0';
            x *= f;
    }
    
    template <typename T> inline void write(T x) {
        if (x < 0) { putchar('-'); x = -x; }
        if (x > 9) write(x/10);
        putchar(x%10+'0');
    }
    
    template <typename T> inline void writeln(T x) {
        write(x);
        puts("");
    }
    
    struct BinaryIndexedTree {
            int bit[MAXC+10][MAXN+10][MAXN+10];
            int lowbit(int x) { return x & (-x); }
            inline void modify(int c,int x,int y,int val) {
                    int i,j;
                    for (i = x; i <= N; i += lowbit(i)) {
                            for (j = y; j <= M; j += lowbit(j)) {
                                    bit[c][i][j] += val;
                            }
                    }
            }
            inline int query(int c,int x,int y) {
                    int i,j,ret=0;
                    for (i = x; i >= 1; i -= lowbit(i)) {
                            for (j = y; j >= 1; j -= lowbit(j)) {
                                    ret += bit[c][i][j];
                            }
                    }
                    return ret;
            }
            inline int query(int c,int xa,int xb,int ya,int yb) {
                    return query(c,xb,yb) - query(c,xb,ya-1) - query(c,xa-1,yb) + query(c,xa-1,ya-1);
            } 
    } BIT;
    
    int main() {
            
            read(N); read(M);
            for (i = 1; i <= N; i++) {
                    for (j = 1; j <= M; j++) {
                            read(a[i][j]);
                            BIT.modify(a[i][j],i,j,1);                
                    }
            }
            read(Q);
            while (Q--) {
                    read(opt);
                    if (opt == 1) {
                            read(x); read(y); read(c);
                            BIT.modify(a[x][y],x,y,-1);
                            BIT.modify(c,x,y,1);
                            a[x][y] = c;
                    }    else {
                            read(xa); read(xb); read(ya); read(yb); read(c);
                            writeln(BIT.query(c,xa,xb,ya,yb));
                    }
            }
            
            return 0;
        
    }
  • 相关阅读:
    104.Maximum Depth of Binary Tree
    103.Binary Tree Zigzag Level Order Traversal
    102.Binary Tree Level Order Traversal
    101.Symmetric Tree
    100.Same Tree
    99.Recover Binary Search Tree
    98.Validate Binary Search Tree
    97.Interleaving String
    static静态初始化块
    serialVersionUID作用
  • 原文地址:https://www.cnblogs.com/evenbao/p/9196390.html
Copyright © 2011-2022 走看看