zoukankan      html  css  js  c++  java
  • POJ 2155 Matrix 二维线段树

    一开始还想跟一维一样用懒惰标记,死活搞不出,看到网上有人说二维的只能做到区间查询单点更新,或单点更新区间查询,用不了懒惰标记。释怀了。。。

    用标记永久化,也就是说一个节点的标记不用往下传,从顶到下查询节点的时候每遇到一个标记就修改答案就行了。

    //#pragma comment(linker, "/STACK:1024000000,1024000000")
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<algorithm>
    #include<iostream>
    #include<sstream>
    #include<cmath>
    #include<climits>
    #include<string>
    #include<map>
    #include<queue>
    #include<vector>
    #include<stack>
    #include<set>
    using namespace std;
    typedef long long ll;
    typedef pair<int,int> pii;
    #define pb(a) push_back(a)
    #define INF 0x1f1f1f1f
    #define lson idx<<1,l,mid
    #define rson idx<<1|1,mid+1,r
    #define PI  3.1415926535898
    template<class T> T min(const T& a,const T& b,const T& c) {
        return min(min(a,b),min(a,c));
    }
    template<class T> T max(const T& a,const T& b,const T& c) {
        return max(max(a,b),max(a,c));
    }
    void debug() {
    #ifdef ONLINE_JUDGE
    #else
    
        freopen("d:\in.txt","r",stdin);
       // freopen("d:\out1.txt","w",stdout);
    #endif
    }
    int getch() {
        int ch;
        while((ch=getchar())!=EOF) {
            if(ch!=' '&&ch!='
    ')return ch;
        }
        return EOF;
    }
    bool res;
    const int maxn=1000;
    struct sub_tree
    {
        int v[maxn<<2];
        int build()
        {
            memset(v,0,sizeof(v));
            return 0;
        }
        int update(int idx,int l,int r,int tl,int tr)
        {
            if(tl<=l&&tr>=r)
            {
                v[idx]^=1;
                return 0;
            }
            int mid=(r+l)>>1;
            if(tl<=mid)update(lson,tl,tr);
            if(tr>mid)update(rson,tl,tr);
            return 0;
        }
        int query(int idx,int l,int r,int a)
        {
            res^=v[idx];
            if(l==r)
                return 0;
            int mid=(r+l)>>1;
            if(a<=mid)return query(lson,a);
            else return query(rson,a);
        }
    };
    sub_tree tree[maxn<<2];
    int n;
    void build(int idx,int l,int r)
    {
        tree[idx].build();
        if(l==r)return ;
        int mid=(r+l)>>1;
        build(lson);
        build(rson);
    }
    int update(int idx,int l,int r,int tlx,int trx,int tla,int tra)
    {
        if(tlx<=l&&trx>=r)
        {
            tree[idx].update(1,1,n,tla,tra);
            return 0;
        }
        int mid=(r+l)>>1;
        if(tlx<=mid)update(lson,tlx,trx,tla,tra);
        if(trx>mid)update( rson,tlx,trx,tla,tra);
        return 0;
    }
    int query(int idx,int l,int r,int x,int y)
    {
        tree[idx].query(1,1,n,y);
        if(l==r)
            return 0;
        int mid=(r+l)>>1;
        if(x<=mid)return query(lson,x,y);
        else return query(rson,x,y);
    }
    int main()
    {
        debug();
        int t;
        scanf("%d",&t);
        for(int ca=1;ca<=t;ca++)
        {
            int q;
            scanf("%d%d",&n,&q);
            build(1,1,n);
            for(int i=1;i<=q;i++)
            {
                char op[10];
                scanf("%s",op);
                if(op[0]=='C')
                {
                    int x1,x2,y1,y2;
                    scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
                    update(1,1,n,x1,x2,y1,y2);
                }else
                {
                    int x,y;
                    scanf("%d%d",&x,&y);
                    res=0;
                    query(1,1,n,x,y);
                    printf("%d
    ",res);
                }
            }
        }
        return 0;
    }
    View Code
  • 相关阅读:
    基础语法 -实验楼
    JavaSE案例-Bank
    初识Java
    Java学习大纲-0412更新
    增量法
    蛮力法
    Host‘116.77.33.xx’is not allowed to connect to this MySQL server
    Maven坐标
    HotSpot虚拟机对象创建
    程序计数器为什么是线程私有的?
  • 原文地址:https://www.cnblogs.com/BMan/p/3312492.html
Copyright © 2011-2022 走看看