poj 2155 Matrix(樹狀數組的應用)

來源:互聯網
上載者:User

標籤:style   blog   http   io   os   ar   strong   sp   art   

http://poj.org/problem?id=2155


對於一個n*n(n <= 1000)的01矩陣,初始全為0,有兩種操作。

C x1 y1 x2 y2 ,分別代表矩陣的左上方和右下角,將這個矩陣中的01互換,原為0的變為1,原為1的變為0。

Q x y詢問A[x,y]現在是幾。


因為只有01的互換,且原始為0,那麼只需計算[x,y]處被換了幾次就能確定現在這個格子是幾。重點就是怎樣快速計算[x,y]格子被換了幾次。操作方法是將[x1,y1][x1,y2+1][x2+1,y1][x2+1,y2+1]格子出增加1,對於[x,y]只需求[1,1]到[x,y]的和就是[x,y]出被換了幾次。


若轉化成一維的,感覺和多校的一道題挺像,hdu 4970

詳解 


#include <stdio.h>#include <iostream>#include <map>#include <set>#include <bitset>#include <list>#include <stack>#include <vector>#include <math.h>#include <string.h>#include <queue>#include <string>#include <stdlib.h>#include <algorithm>#define LL __int64//#define LL long long#define eps 1e-9#define PI acos(-1.0)using namespace std;const int INF = 0x3f3f3f3f;const int mod = 10000007;int c[1010][1010];int n,m;int Lowbit(int x){return x&(-x);}void update(int x,int y, int add){while(x <= n){int tmp = y;while(tmp <= n){c[x][tmp] += add;tmp += Lowbit(tmp);}x += Lowbit(x);}}int sum(int x, int y){int s = 0;while(x >= 1){int tmp = y;while(tmp >= 1){s += c[x][tmp];tmp -= Lowbit(tmp);}x -= Lowbit(x);}return s;}int main(){int test;int x1,y1,x2,y2;scanf("%d",&test);while(test--){char ch[2];memset(c,0,sizeof(c));scanf("%d %d",&n,&m);while(m--){scanf("%s",ch);if(ch[0] == 'C'){scanf("%d %d %d %d",&x1,&y1,&x2,&y2);update(x1,y1,1);update(x1,y2+1,1);update(x2+1,y1,1);update(x2+1,y2+1,1);}else{scanf("%d %d",&x1,&y1);int s = sum(x1,y1);if(s&1)printf("1\n");elseprintf("0\n");}}printf("\n");}return 0;}


poj 2155 Matrix(樹狀數組的應用)

聯繫我們

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