POJ 2155 二維樹狀數組+差分

來源:互聯網
上載者:User

題目大意:給定一個初始時全部為0的n*n的矩陣,然後將一個子矩陣中的全部元素取反,詢問一個元素當前是什麼
思路:將取反操作當成轉化為操作次數,容易得到若操作次數為偶數則為0否則為1.將問題轉化為區間修改+單點查詢.對於區間修改用差分實現,則答案為所有首碼和.用樹狀數組維護即可
PS:這題輸入很噁心,數後還有一個空格

#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>#define fo(i,a,b) for(int i=a;i<=b;i++)#define fod(i,a,b) for(int i=a;i>=b;i--)using namespace std;const int N=1e3+10;int n,q,tr[N][N];#define lowbit(x) ((x)&(-x))void update(int idx,int idy,int val){    for(int i=idx;i<=n;i+=lowbit(i))         for(int j=idy;j<=n;j+=lowbit(j))            tr[i][j]+=val;}int query(int idx,int idy){    int ret=0;    for(int i=idx;i;i-=lowbit(i))         for(int j=idy;j;j-=lowbit(j))             ret+=tr[i][j];    return ret;}int main(){    int T;scanf("%d",&T);    while(T--) {        scanf("%d%d ",&n,&q);        memset(tr,0,sizeof(tr));        for(int x1,x2,y1,y2,i=1;i<=q;i++) {            char op[2]; scanf("%s",op);            if(op[0]=='C') {                scanf("%d%d%d%d",&x1,&y1,&x2,&y2);                  update(x2+1,y2+1,1);                update(x1,y2+1,-1);                update(x2+1,y1,-1);                update(x1,y1,1);            } else {                scanf("%d%d",&x1,&y1);                printf("%d\n",query(x1,y1)%2);            }        }        if(T) puts("");    }    return 0;}

聯繫我們

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