compro-library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub ningenMe/compro-library

:heavy_check_mark: test/segment-tree/SegmentTree2D-rsq.test.cpp

Depends on

Code

#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_5_B"

#include <iostream>
#include <vector>
#include <tuple>
using namespace std;
#include "../../lib/00-util/FastIO.cpp"
#include "../../lib/10-segment-tree/SegmentTree2D.cpp"
#include "../../lib/99-operator/monoid/MonoidRangeSumPointAdd.cpp"

int main(void){
    cin.tie(0);ios::sync_with_stdio(false);
    int N; read(N);
    int L = 1002;
    SegmentTree2D<MonoidRangeSumPointAdd<int>> seg(L,L);
    while(N--) {
        int x1,y1,x2,y2; 
        read(x1),read(y1),read(x2),read(y2);
        seg.operate(y1,x1, 1);
        seg.operate(y1,x2,-1);
        seg.operate(y2,x1,-1);
        seg.operate(y2,x2, 1);
    }
    for(int i=0;i<L;++i) {
        for(int j=1;j<L;++j) {
            seg.operate(i,j,seg.fold(i,i+1,j-1,j));
        }
    }
    for(int j=0;j<L;++j) {
        for(int i=1;i<L;++i) {
            seg.operate(i,j,seg.fold(i-1,i,j,j+1));
        }
    }
    int ans = 0;
    for(int i=0;i<L;++i) {
        for(int j=0;j<L;++j) {
            ans=max(ans,seg.fold(i,i+1,j,j+1));
        }
    }
    cout << ans << "\n";
	return 0;
}
#line 1 "test/segment-tree/SegmentTree2D-rsq.test.cpp"
#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_5_B"

#include <iostream>
#include <vector>
#include <tuple>
using namespace std;
#line 1 "lib/00-util/FastIO.cpp"
/*
 * @title FastIO
 * @docs md/util/FastIO.md
 */
class FastIO{
private:
    inline static constexpr int ch_0='0';
    inline static constexpr int ch_9='9';
    inline static constexpr int ch_n='-';
    inline static constexpr int ch_s=' ';
    inline static constexpr int ch_l='\n';
    inline static void endline_skip(char& ch) {
        while(ch==ch_l) ch=getchar();
    }
    template<typename T> inline static void read_integer(T &x) {
        int neg=0; char ch; x=0;
        ch=getchar();
        endline_skip(ch);
        if(ch==ch_n) neg=1,ch=getchar();
        for(;(ch_0 <= ch && ch <= ch_9); ch = getchar()) x = x*10 + (ch-ch_0);
        if(neg) x*=-1;
    }
    template<typename T> inline static void read_unsigned_integer(T &x) {
        char ch; x=0;
        ch=getchar();
        endline_skip(ch);
        for(;(ch_0 <= ch && ch <= ch_9); ch = getchar()) x = x*10 + (ch-ch_0);
    }
    inline static void read_string(string &x) {
        char ch; x="";
        ch=getchar();
        endline_skip(ch);
        for(;(ch != ch_s && ch!=ch_l); ch = getchar()) x.push_back(ch);
    }
    inline static char ar[40];
    inline static char *ch_ar;
    template<typename T> inline static void write_integer(T x) {
        ch_ar=ar;
        if(x< 0) putchar(ch_n), x=-x;
        if(x==0) putchar(ch_0);
        for(;x;x/=10) *ch_ar++=(ch_0+x%10);
        while(ch_ar--!=ar) putchar(*ch_ar);
    }
public:
    inline static void read(int &x) {read_integer<int>(x);}
    inline static void read(long long &x) {read_integer<long long>(x);}
    inline static void read(unsigned int &x) {read_unsigned_integer<unsigned int>(x);}
    inline static void read(unsigned long long &x) {read_unsigned_integer<unsigned long long>(x);}
    inline static void read(string &x) {read_string(x);}
    inline static void read(__int128_t &x) {read_integer<__int128_t>(x);}
    inline static void write(__int128_t x) {write_integer<__int128_t>(x);}
    inline static void write(char x) {putchar(x);}
};
#define read(arg) FastIO::read(arg)
#define write(arg) FastIO::write(arg)
#line 1 "lib/10-segment-tree/SegmentTree2D.cpp"
/*
 * @title SegmentTree2D - 非再帰抽象化セグメント木2D
 * @docs md/segment-tree/SegmentTree2D.md
 */
template<class Monoid> class SegmentTree2D {
    using TypeNode = typename Monoid::TypeNode;
    class SegmentTree {
        size_t length;
        vector<TypeNode> node;
    public:
        //unitで初期化
        SegmentTree(const size_t num) {
            for (length = 1; length <= num; length *= 2);
            node.resize(2 * length, Monoid::unit_node);
            for (int i = length - 1; i >= 0; --i) node[i] = Monoid::func_fold(node[(i<<1)+0],node[(i<<1)+1]);
        }
        //[idx,idx+1)
        void operate(size_t idx, const TypeNode var) {
            if(idx < 0 || length <= idx) return;
            idx += length;
            node[idx] = Monoid::func_operate(node[idx],var);
            while(idx >>= 1) node[idx] = Monoid::func_fold(node[(idx<<1)+0],node[(idx<<1)+1]);
        }
        //[l,r)
        TypeNode fold(int l, int r) {
            if (l < 0 || length <= l || r < 0 || length < r) return Monoid::unit_node;
            TypeNode vl = Monoid::unit_node, vr = Monoid::unit_node;
            for(l += length, r += length; l < r; l >>=1, r >>=1) {
                if(l&1) vl = Monoid::func_fold(vl,node[l++]);
                if(r&1) vr = Monoid::func_fold(node[--r],vr);
            }
            return Monoid::func_fold(vl,vr);
        }
        void print() {
            cout << "{";
            for(int i=0; i < length; ++i) cout << fold(i,i+1) << " }"[i+1==length];
            cout << endl;
        }
    };
    size_t height,width;
    vector<SegmentTree> node;
public:
    SegmentTree2D(const size_t h, const size_t w):width(w) {
        for (height = 1; height <= h; height *= 2);
        SegmentTree seg(width);
        node.resize(2 * height, seg);
        for (int i = height - 1; i >= 0; --i) {
            for(int j=0; j < width; ++j) {
                node[i].operate(j, Monoid::func_fold(node[(i<<1)+0].fold(j,j+1),node[(i<<1)+1].fold(j,j+1)));
            }
        }
    }
    //[u,u+1)*[l,l+1) に operate
    void operate(size_t u, size_t l, const TypeNode var) {
        if(u < 0 || height <= u || l < 0 || width <= l) return;
        u += height;
        node[u].operate(l, var);
        while(u >>= 1) node[u].operate(l, Monoid::func_fold(node[(u<<1)+0].fold(l,l+1),node[(u<<1)+1].fold(l,l+1)));
    }
    //[u,d),[l,r)
    TypeNode fold(int u, int d, int l, int r) {
        if(u < 0 || height <= u || d < 0 || height < d || l < 0 || width <= l || r < 0 || width < r) return Monoid::unit_node;
        TypeNode vu = Monoid::unit_node, vd = Monoid::unit_node;
        for(u += height, d += height; u < d; u >>=1, d >>=1) {
            if(u&1) vu = Monoid::func_fold(vu,node[u++].fold(l,r));
            if(d&1) vd = Monoid::func_fold(node[--d].fold(l,r),vd);
        }
        return Monoid::func_fold(vu,vd);
    }
    void print() {
        for(int i=height; i < 2*height; ++i) {
            node[i].print();
        }
    }
};
#line 1 "lib/99-operator/monoid/MonoidRangeSumPointAdd.cpp"
/*
 * @title MonoidRangeSumPointAdd - [区間和, 一点加算]
 * @docs md/operator/monoid/MonoidRangeSumPointAdd.md
 */
template<class T> struct MonoidRangeSumPointAdd {
    using TypeNode = T;
    inline static constexpr TypeNode unit_node = 0;
    inline static constexpr TypeNode func_fold(TypeNode l,TypeNode r){return l+r;}
    inline static constexpr TypeNode func_operate(TypeNode l,TypeNode r){return l+r;}
    inline static constexpr bool func_check(TypeNode nodeVal,TypeNode var){return var == nodeVal;}
};
#line 10 "test/segment-tree/SegmentTree2D-rsq.test.cpp"

int main(void){
    cin.tie(0);ios::sync_with_stdio(false);
    int N; read(N);
    int L = 1002;
    SegmentTree2D<MonoidRangeSumPointAdd<int>> seg(L,L);
    while(N--) {
        int x1,y1,x2,y2; 
        read(x1),read(y1),read(x2),read(y2);
        seg.operate(y1,x1, 1);
        seg.operate(y1,x2,-1);
        seg.operate(y2,x1,-1);
        seg.operate(y2,x2, 1);
    }
    for(int i=0;i<L;++i) {
        for(int j=1;j<L;++j) {
            seg.operate(i,j,seg.fold(i,i+1,j-1,j));
        }
    }
    for(int j=0;j<L;++j) {
        for(int i=1;i<L;++i) {
            seg.operate(i,j,seg.fold(i-1,i,j,j+1));
        }
    }
    int ans = 0;
    for(int i=0;i<L;++i) {
        for(int j=0;j<L;++j) {
            ans=max(ans,seg.fold(i,i+1,j,j+1));
        }
    }
    cout << ans << "\n";
	return 0;
}
Back to top page