BZOJ 1452 JSOI 2009 Count 二維樹狀數組

來源:互聯網
上載者:User

標籤:bzoj   bzoj1452   二維樹狀數組   區間修改   

題目大意:有一個m*n的方格,每一個格子有他自己的權值。2種操作:

1.改變一個格子的權值。

2.查詢所有的x1 <= x <= x2 && y1 <= y <= y2的中,有多少個格子顏色是c。


思路:好像是二維樹狀數組的樣子,但是不知道怎麼搞。後來研究了資料範圍,發現格子最大300*300,顏色最多才100種,於是算一下300*300*100*4/1024/1024大概是35M,題目要求64M,可以搞了。(這裡算的精確一點,我當時沒怎麼算,吧顏色開成300的了,結果100+M就MLE了。。)

維護100個二維樹狀數組,每一個代表一種顏色,然後根據容斥原理計算一個方塊區間內的數量。更改的時候要改兩次,第一次把之前的減去,然後加上改變之後的。


CODE:


#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define MAX 310using namespace std;int m,n;struct Complex{    int tree[MAX][MAX];    void Fix(int x,int y,int c) {        for(int i = x;i <= m;i += i&-i)            for(int j = y;j <= n;j += j&-j)                tree[i][j] += c;    }    int GetSum(int x,int y) {        int re = 0;        for(int i = x;i;i -= i&-i)            for(int j = y;j;j -= j&-j)                re += tree[i][j];        return re;    }}tree_arr[110];int src[MAX][MAX];int asks;int main(){    cin >> m >> n;    for(int i = 1;i <= m; ++i)        for(int j = 1;j <= n; ++j) {            scanf("%d",&src[i][j]);            tree_arr[src[i][j]].Fix(i,j,1);        }    cin >> asks;    for(int flag,i = 1;i <= asks; ++i) {        scanf("%d",&flag);        if(flag == 1) {            int x,y,z;            scanf("%d%d%d",&x,&y,&z);            tree_arr[src[x][y]].Fix(x,y,-1);            src[x][y] = z;            tree_arr[src[x][y]].Fix(x,y,1);        }        else {            int x1,y1,x2,y2,c;            scanf("%d%d%d%d%d",&x1,&x2,&y1,&y2,&c);            int ans = tree_arr[c].GetSum(x2,y2);            ans -= tree_arr[c].GetSum(x1 - 1,y2);            ans -= tree_arr[c].GetSum(x2,y1 - 1);            ans += tree_arr[c].GetSum(x1 - 1,y1 - 1);            printf("%d\n",ans);        }    }    return 0;}


BZOJ 1452 JSOI 2009 Count 二維樹狀數組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.