POJ 2155 二維線段樹

來源:互聯網
上載者:User

標籤:style   blog   os   io   2014   ar   amp   log   

POJ 2155  二維線段樹

思路:二維線段樹就是每個節點套一棵線段樹的樹。

剛開始因為題目是求A[I,J],然後在y查詢那直接ans^=Map[i][j]的時候沒看懂,後面自己把圖畫出來了才理解。

因為只有0和1,所以可以用異或來搞,而不需要每次都需要修改。

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<map>#include<queue>#include<set>#include<cmath>#include<bitset>#define mem(a,b) memset(a,b,sizeof(a))#define lson i<<1,l,mid#define rson i<<1|1,mid+1,r#define llson j<<1,l,mid#define rrson j<<1|1,mid+1,r#define INF 510010#define maxn 4010using namespace std;typedef long long ll;typedef unsigned long long ull;bool Map[maxn][maxn];int n,q,t,ans;void update_y(int i,int j,int l,int r,int y1,int y2){    if(l==y1&&r==y2) {Map[i][j]^=1;return ;}    int mid=(l+r)>>1;    if(y2<=mid) update_y(i,llson,y1,y2);    else if(y1>mid) update_y(i,rrson,y1,y2);    else    {        update_y(i,llson,y1,mid);        update_y(i,rrson,mid+1,y2);    }}void update_x(int i,int l,int r,int x1,int x2,int y1,int y2){    if(l==x1&&r==x2)    {        update_y(i,1,1,n,y1,y2);        return ;    }    int mid=(l+r)>>1;    if(x2<=mid) update_x(lson,x1,x2,y1,y2);    else if(x1>mid) update_x(rson,x1,x2,y1,y2);    else    {        update_x(lson,x1,mid,y1,y2);        update_x(rson,mid+1,x2,y1,y2);    }}void query_y(int i,int j,int l,int r,int y){    ans^=Map[i][j];    if(l==r) return ;    int mid=(l+r)>>1;    if(y<=mid) query_y(i,llson,y);    else query_y(i,rrson,y);}void query_x(int i,int l,int r,int x,int y){    query_y(i,1,1,n,y);    if(l==r) return ;    int mid=(l+r)>>1;    if(x<=mid) query_x(lson,x,y);    else query_x(rson,x,y);}int main(){    //freopen("test.txt","r",stdin);    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&n,&q);        mem(Map,0);        while(q--)        {            char s[2];            scanf("%s",s);            ans=0;            if(s[0]=='C')            {                int x1,x2,y1,y2;                scanf("%d%d%d%d",&x1,&y1,&x2,&y2);                update_x(1,1,n,x1,x2,y1,y2);            }            else            {                int x,y;                scanf("%d%d",&x,&y);                query_x(1,1,n,x,y);                printf("%d\n",ans);            }        }        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.