Problem:
Fishermen Following the Rules
Kenjiro is a fisherman who collects fish from the lake. The entire lake is rectangular in shape. The lake is lined with a grid of square-shaped administrative blocks. On the map, the coordinates of the upper-left control area of the lake are (1,1), and the coordinates of the i-th control area from the top and j-th control area from the left are (i,j). From our previous experience, we know that there is one fish in every management compartment and the price of each fish.
Fishing is done by setting up a rectangular net and enclosing some of the management plots with the net. Any fish in the netted management plots can be obtained. However, it was decided to introduce rules to protect the growth of fish and fishermen will have to abide by these rules when fishing. We don't know the details of the rule yet, but we do know that the rule will be based on the price of the fish that is the $K$th from the cheapest when the fish obtained in a single net placement are listed in order of price.
Kenjirou decided to find out the price of the $K$th fish from the cheapest one for all the places where nets could be set so that he could comply with the rule and start fishing as soon as possible.
Given the price of the fish in each section of the lake, the size of the net surrounding the control section, and the location $K$ where the price of the fish is to be checked, create a program that displays the price of the fish located $K$th from the cheapest for all locations where the net can be placed. However, the net will not rotate or deform.
Input
Input is given in the following format.
$H$ $W$ $R$ $C$
$K$
$a_{1,1}$ $a_{1,2}$ ... $a_{1,W}$ 
$a_{2,1}$ $a_{2,2}$ ... $a_{2,W}$ 
：
$a_{H,1}$ $a_{H,2}$ ... $a_{H,W}$
In the first line, we are given the number of vertical plots in the lake, $H$ ($1\leq H \leq 400$), the number of horizontal plots, $W$ ($1\leq W \leq 400$), the number of vertical plots in the net surrounding the management plot, $R$ ($1\leq R \leq H$), and the number of horizontal plots, $C$ ($1\leq C \leq W $) are given. In the following line, the position $K$ ($1 \leq K \leq R \times C$) for examining the price is given. In the subsequent $H$ line, the price $a_{i,j}$ ($0 \leq a_{i,j} \leq 1,000,000,000$) of the fish in the plot at coordinates (i,j) is given.
Output
The output is $(H-R+1)\times(W-C+1)$ lines, one per line, in ascending order, with the fish prices obtained for each location where the net can be set.
Sample Input and Output
Sample Input
3 3 2 2
3
5 7 9
8 9 2
3 2 1
Sample Output
2
8
8
9
The third from the cheapest of prices 5, 7, 8, and 9 for the fish in the section where the upper left is (1,1) and the lower right is (2,2) is 8.
The third price from the lowest of 7,9,9,2 is 9 for the fish in the section where the top left is (1,2) and the bottom right is (2,3).
The third price from the lowest of 8,9,3,2 is 8 for the fish in the section where the top left is (2,1) and the bottom right is (3,2).
The third price 
...(truncated)

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, e) for (int i = (int)(s); i < (int)(e); ++i)

template<typename T>
struct FenwickTree {
	vector<T> data;
	FenwickTree(int size) : data(size + 1, 0) {}
	
	void add(int k, T x) {
		for (++k; k < data.size(); k += k & -k) data[k] += x;
	}
	
	T sum(int k) {
		if (k < 0) return 0;
		T res = 0;
		for (++k; k > 0; k -= k & -k) res += data[k];
		return res;
	}
	
	T sum(int l, int r) {
		return sum(r) - sum(l - 1);
	}
};

int main() {
	cin.tie(nullptr);
	ios_base::sync_with_stdio(false);
	
	int H, W, R, C, K;
	cin >> H >> W >> R >> C >> K;
	vector A(H, vector<int>(W));
	rep(i, 0, H) rep(j, 0, W) cin >> A[i][j];
	
	vector<int> B(H*W);
	rep(i, 0, H) rep(j, 0, W) B[i*W + j] = A[i][j];
	sort(B.begin(), B.end());
	B.erase(unique(B.begin(), B.end()), B.end());
	rep(i, 0, H) rep(j, 0, W) A[i][j] = lower_bound(B.begin(), B.end(), A[i][j]) - B.begin();
	
	FenwickTree<int> fw(H*W);
	rep(i, 0, R) rep(j, 0, C) fw.add(A[i][j], 1);
	
	vector<int> ans;
	
	int pi = 0, pj = 0;
	rep(itr, 0, (H - R + 1)*(W - C + 1)) {
		int ok = H*W - 1, ng = -1;
		while (ok - ng > 1) {
			int mid = (ok + ng) / 2;
			if (fw.sum(0, mid) >= K) ok = mid;
			else ng = mid;
		}
		ans.push_back(B[ok]);
		
		if (itr == (H - R + 1)*(W - C + 1) - 1) break;
		
		if (pi % 2 == 0) {
			if (pj == W - C) {
				rep(j, 0, C) fw.add(A[pi][pj + j], -1);
				rep(j ,0, C) fw.add(A[pi + R][pj + j], 1);
				++pi;
			} else {
				rep(i, 0, R) fw.add(A[pi + i][pj], -1);
				rep(i, 0, R) fw.add(A[pi + i][pj + C], 1);
				++pj;
			}
		} else {
			if (pj == 0) {
				rep(j, 0, C) fw.add(A[pi][pj + j], -1);
				rep(j ,0, C) fw.add(A[pi + R][pj + j], 1);
				++pi;
			} else {
				--pj;
				rep(i, 0, R) fw.add(A[pi + i][pj], 1);
				rep(i, 0, R) fw.add(A[pi + i][pj + C], -1);
			}
		}
	}
	
	sort(ans.begin(), ans.end());
	for (int t : ans) cout << t << '\n';
}
accuracy: 1
time_ms: 370
memory_kb: 6044
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;

template <typename T>
struct binary_trie{
    static_assert(is_unsigned_v<T>);
    
    struct node_t{
        T value;
        int width;
        int count;
        array<node_t*, 4> child;
        node_t() = default;
        node_t(T v, int w, int c) : value(v), width(w), count(c), child{&nil, &nil, &nil, &nil} {}
        node_t(T v, int w, int c, node_t* c0, node_t* c1, node_t* c2, node_t* c3) : value(v), width(w), count(c), child{c0, c1, c2, c3} {}
        static node_t nil;
    };
    
    struct ref_node_t{
        T val;
        bool exist;
        ref_node_t(T x, bool e) : val{x}, exist{e} {}
    };
    
    struct Pool {
        constexpr static int SIZ = 1<<12;
        node_t* ptr = nullptr, *en = nullptr;
        node_t* get() {
            if(ptr == en) {
                ptr = new node_t[SIZ];
                en = ptr + SIZ;
            }
            return ptr++;
        }
    };
    
    static constexpr T one = 1;
    static constexpr int bit_width = sizeof(T) * 8;
    static Pool pool;
    static constexpr T xor_val = 0;
    node_t* root;
    int siz = 0;
    
    template<typename _Tp>
    inline int clz(_Tp x) const {
        if constexpr(sizeof(_Tp) == 8ull) return __builtin_clzll(x);
        else return __builtin_clz(x);
    }
    
    inline T mask(int l, int r) const {
        if(r >= bit_width){
            if(l >= bit_width) return 0;
            else return -(one<<l);
        }
        return (one<<r) - (one<<l);
    }
    
    inline T masked(T v, int l, int r) const {
        return mask(l, r) & v;
    }
    
    inline int diff_bit(T x, T y) const {
        return ((bit_width-1 - clz(x^y))|1) + 1;
    }
    
    template<typename... Args>
    inline node_t* make_node(Args ...args) {
        return &((*pool.get()) = node_t(args...));
    }
    
    public:
    
    binary_trie(){
        root = make_node(0, bit_width, 0);
    }
    
    binary_trie<T>& operator=(binary_trie<T>&& o) noexcept = default;
    
    binary_trie(binary_trie&& o) noexcept = default;
    
    void insert(T v) {
        node_t* pos = root;
        int bit = bit_width;
        siz++;
        v ^= xor_val;
        while(true){
            T mv = masked(v, bit-pos->width, bit);
            T mnv = masked(pos->value, bit-pos->width, bit);
            if(mv != mnv){
                int diff = diff_bit(mv, mnv);
                int b = (mv>>(diff-2))&3;
                int nb = (mnv>>(diff-2))&3;
                node_t* inter = make_node(pos->value, pos->width-bit+diff, pos->count, pos->child[0], pos->child[1], pos->child[2], pos->child[3]);
                node_t* leaf = make_node(v, diff, 1);
                pos->width = bit-diff;
                pos->count++;
                pos->child = {&node_t::nil, &node_t::nil, &node_t::nil, &node_t::nil};
                pos->child[b] = leaf;
                pos->child[nb] = inter;
                return;
            } else {
                pos->count++;
                bit -= pos->width;
                if(bit == 0) return;
                int b = (v>>(bit-2))&3;
                if(pos->child[b] == &node_t::nil){
                    pos->child[b] = make_node(v, bit, 1);
                    return;
                }
                pos = pos->child[b];
            }
        }
    }
    
    int count(T v) const {
        node_t* pos = root;
        int bit = bit_width;
        v ^= xor_val;
        while(pos != &node_t::nil){
            T mv = masked(v, bit-pos->width, bit);
            T mnv = masked(pos->value, bit-pos->width, bit);
            if(mv != mnv) return 0;
            bit -= pos->width;
            if(bit == 0) return pos->count;
            pos = pos->child[(v>>(bit-2))&3];
        }
        return 0;
    }
    
    void erase(T v) {
        node_t* pos = root;
        int bit = bit_width;
        siz--;
        v ^= xor_val;
        while(true){
            pos->count--;
            bit -= pos->width;
            if(bit == 0) return;
            pos = pos->child[(v>>(bit-2))&3];
        }
    }
    
    void erase_all(T v) {
        int n = count(v);
        if(n == 0) return;
        node_t* pos = root;
        int bit = bit_width;
        siz -= n;
        v ^= xor_val;
        while(true){
            pos->count -= n;
            bit -= pos->width;
            if(bit == 0) return;
            pos = pos->child[(v>>(bit-2))&3];
        }
    }
    
    const T operator[](int k) const {
        if(k < 0) k += siz;
        
        k++;
        node_t* pos = root;
        int bit = bit_width;
        while(true){
            bit -= pos->width;
            if(bit == 0) return pos->value^xor_val;
            int b = (xor_val>>(bit-2))&3;
            auto &child = pos->child;
            if(k <= child[b]->count){
                pos = child[b];
            } else {
                k -= child[b]->count;
                if(k <= child[b^1]->count){
                    pos = child[b^1];
                } else {
                    k -= child[b^1]->count;
                    if(k <= child[b^2]->count){
                        pos = child[b^2];
                    } else {
                        k -= child[b^2]->count;
                        pos = child[b^3];
                    }
                }
            }
        }
    }
    
    T min() const {
        node_t* pos = root;
        int bit = bit_width;
        while(true){
            bit -= pos->width;
            if(bit == 0) return pos->value^xor_val;
            int b = (xor_val>>(bit-2))&3;
            auto &child = pos->child;
            if(child[b]->count) pos = child[b];
            else if(child[b^1]->count) pos = child[b^1];
            else if(child[b^2]->count) pos = child[b^2];
            else pos = child[b^3];
        }
    }
    
    T max() const {
        node_t* pos = root;
        int bit = bit_width;
        while(true){
            bit -= pos->width;
            if(bit == 0) return pos->value^xor_val;
            int b = (xor_val>>(bit-2))&3;
            auto &child = pos->child;
            if(child[b^3]->count) pos = child[b^3];
            else if(child[b^2]->count) pos = child[b^2];
            else if(child[b^1]->count) pos = child[b^1];
            else pos = child[b];
        }
    }
    
    int order(T v) const {
        if(v == 0) return 0;
        v--;
        int res = 0;
        node_t* pos = root;
        int bit = bit_width;
        while(pos != &node_t::nil){
            T mv = masked(v, bit-pos->width, bit);
            T mnv = masked(pos->value^xor_val, bit-pos->width, bit);
            if(mv < mnv){
                return res;
            } else {
                if(mv > mnv){
                    res += pos->count;
                    return res;
                } else {
                    bit -= pos->width;
                    if(bit == 0) return res + pos->count;
                    int b = (v>>(bit-2))&3;
                    auto &child = pos->child;
                    T mxv = (xor_val>>(bit-2))&3;
                    
                    if(b >= 1){
                        res += child[mxv]->count;
                        if(b >= 2){
                            res += child[mxv^1]->count;
                            if(b >= 3){
                                res += child[mxv^2]->count;
                            }
                        }
                    }
                    pos = child[b^mxv];
                }
            }
        }
        return res;
    }
    
    const ref_node_t lower_bound(T v) const {
        int ord = order(v);
        if(siz == ord) return ref_node_t{0, false};
        else return ref_node_t{(*this)[ord], true};
    }
    
    const ref_node_t less_bound(T v) const {
        int ord = v!=numeric_limits<T>::max() ? order(v+1) : siz;
        if(ord == 0) return ref_node_t{0, false};
        else return ref_node_t{(*this)[ord-1], true};
    }
    
    int size() const {
        return siz;
    }
};
template<typename T> typename binary_trie<T>::node_t binary_trie<T>::node_t::nil = node_t(0, bit_width, 0, &node_t::nil, &node_t::nil, &node_t::nil, &node_t::nil);
template<typename T> typename binary_trie<T>::Pool binary_trie<T>::pool = Pool();

int main(){
    cin.tie(0)->sync_with_stdio(0);
    
    int h, w, r, c;
    cin >> h >> w >> r >> c;
    int k;
    cin >> k;
    vector<vector<int>> A(h, vector<int>(w));
    for(auto &i : A) for(auto &j : i) cin >> j;
    
    vector<int> ans;
    binary_trie<unsigned> bin;
    
    for(int i = 0; i < h-r+1; i++){
        for(int j = 0; j < r; j++) for(int k = 0; k < c; k++) bin.insert(A[i+j][k]);
        for(int j = 0; j < w-c+1; j++){
            ans.push_back(bin[k-1]);
            if(j != w-c){
                for(int k = 0; k < r; k++) bin.erase(A[i+k][j]);
                for(int k = 0; k < r; k++) bin.insert(A[i+k][j+c]);
            }
        }
        for(int j = 0; j < r; j++) for(int k = 0; k < c; k++) bin.erase(A[i+j][k+(w-c)]);
    }
    
    sort(ans.begin(), ans.end());
    for(int i : ans) cout << i << '\n';
}
accuracy: 1
time_ms: 1520
memory_kb: 16536
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;
// #include<atcoder/all>
// using mint = atcoder::modint998244353;
using ld = long double;
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define rep(i,n) for(int i=0;i<(int)(n);++i)
template<typename T>bool chmin(T&a,T b){return b<a?(a=b,1):0;}
template<typename T>bool chmax(T&a,T b){return b>a?(a=b,1):0;}

template <class T> struct fenwick_tree {
    using U = T;
  public:
    fenwick_tree() : _n(0) {}
    explicit fenwick_tree(int n) : _n(n), data(n) {}

    void add(int p, T x) {
        assert(0 <= p && p < _n);
        p++;
        while (p <= _n) {
            data[p - 1] += U(x);
            p += p & -p;
        }
    }

    T sum(int l, int r) {
        assert(0 <= l && l <= r && r <= _n);
        return sum(r) - sum(l);
    }

    int lower_bound(T x) {
        if (x <= 0) return 0;
        int i = 0;
        T sum = 0;
        int k = 1;
        while (k << 1 <= _n) k <<= 1;
        for (; k > 0; k >>= 1) {
            if (i + k <= _n && sum + data[i + k - 1] < x) {
                sum += data[i + k - 1];
                i += k;
            }
        }
        return i;
    }

  private:
    int _n;
    std::vector<U> data;
    U sum(int r) {
        U s = 0;
        while (r > 0) {
            s += data[r - 1];
            r -= r & -r;
        }
        return s;
    }
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int H,W,R,C;
    cin>>H>>W>>R>>C;
    int K;
    cin>>K;
    vector A(H,vector<int>(W));
    rep(i,H)rep(j,W)cin>>A[i][j];


    vector<int> z;
    rep(i,H)rep(j,W)z.push_back(A[i][j]);
    sort(all(z));
    z.erase(unique(all(z)),z.end());
    rep(i,H)rep(j,W)A[i][j]=lower_bound(all(z), A[i][j])-z.begin();

    vector<int> ans;
    rep(h,H-R+1){
        fenwick_tree<int> bit(ssize(z));
        rep(i,R){
            rep(j,C){
                bit.add(A[i+h][j], 1);
            }
        }
        ans.push_back(z[bit.lower_bound(K)]);
        rep(w,W-C){
            rep(i,R){
                bit.add(A[h+i][w], -1);
                bit.add(A[h+i][w+C], 1);
            }
            ans.push_back(z[bit.lower_bound(K)]);
        }
    }
    sort(all(ans));
    for(int e: ans)cout<<e<<"\n";
}
accuracy: 1
time_ms: 350
memory_kb: 6748
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;
// #include<atcoder/all>
// using mint = atcoder::modint998244353;
using ld = long double;
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define rep(i,n) for(int i=0;i<(int)(n);++i)
template<typename T>bool chmin(T&a,T b){return b<a?(a=b,1):0;}
template<typename T>bool chmax(T&a,T b){return b>a?(a=b,1):0;}

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int H,W,R,C;
    cin>>H>>W>>R>>C;
    int K;
    cin>>K;
    K--;
    vector A(H,vector<int>(W));
    rep(i,H)rep(j,W)cin>>A[i][j];

    vector<int> z;
    rep(i,H)rep(j,W)z.push_back(A[i][j]);
    sort(all(z));
    z.erase(unique(all(z)),z.end());
    rep(i,H)rep(j,W)A[i][j]=lower_bound(all(z), A[i][j])-z.begin();

    vector<int> ans;
    rep(h,H-R+1){
        ordered_set<pair<int,int>> st;
        rep(i,R){
            rep(j,C){
                st.insert({A[i+h][j], (i+h)*W+j});
            }
        }
        ans.push_back(z[st.find_by_order(K)->fi]);
        rep(w,W-C){
            rep(i,R){
                st.erase({A[h+i][w], (h+i)*W+w});
                st.insert({A[h+i][w+C], (h+1)*W+w+C});
            }
            ans.push_back(z[st.find_by_order(K)->fi]);
        }
    }
    sort(all(ans));
    for(int e: ans)cout<<e<<"\n";
}
accuracy: 0.10810810810810811
time_ms: 20
memory_kb: 6252
final_rank: 20

code_snippet: // AOJ #463
// Fishermen Following the Rules 2025.2.9

#include <bits/stdc++.h>
using namespace std;

#define gc() getchar_unlocked()
#define pc(c) putchar_unlocked(c)

int Cin() {  // 整数の入力
	int n = 0, c = gc();
	if (c == '-') {	c = gc();
		do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
		return -n;
	}
	do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

void Cout(int n) { // 整数の表示（出力）
	char b[30];
	if (!n) pc('0');
	else {
		if (n < 0) pc('-'), n = -n;
		int i = 0; while (n) b[i++] = n % 10 + '0', n /= 10;
		while (i--) pc(b[i]);
	}
	pc('\n');
}

int main(){
    int H = Cin(), W = Cin(), R = Cin(), C = Cin();
    int K = Cin();
    vector<vector<int>> grid(H, vector<int>(W));
    vector<int> all;
    all.reserve(H * W);
    for (int i = 0; i < H; i++){
        for (int j = 0; j < W; j++){
            grid[i][j] = Cin();
            all.push_back(grid[i][j]);
        }
    }
    sort(all.begin(), all.end());
    all.erase(unique(all.begin(), all.end()), all.end());
    int D = all.size();
    vector<vector<int>> disc(H, vector<int>(W));
    for (int i = 0; i < H; i++){
        for (int j = 0; j < W; j++){
            int pos = int(lower_bound(all.begin(), all.end(), grid[i][j]) - all.begin());
            disc[i][j] = pos + 1;
        }
    }
 
    vector<int> freq(D, 0);
    const int BS = 256;
    int nb = (D + BS - 1) / BS;
    vector<int> block(nb, 0);
 
    auto updateFreq = [&](int pos, int delta) {
        freq[pos] += delta;
        block[pos / BS] += delta;
    };
 
    auto kthQuery = [&]() -> int {
        int need = K;
        int b = 0;
        for(; b < nb; b++){
            if(block[b] < need)
                need -= block[b];
            else {
                int start = b * BS;
                int end = min(D, start + BS);
                for (int i = start; i < end; i++){
                    if(freq[i] < need)
                        need -= freq[i];
                    else
                        return i;
                }
            }
        }
        return -1;
    };
 
    int totalWindows = (H - R + 1) * (W - C + 1);
    vector<int> answers;
    answers.reserve(totalWindows);
 
    for (int r = 0; r < R; r++){
        for (int c = 0; c < C; c++){
            updateFreq(disc[r][c] - 1, 1);
        }
    }
 
    for (int i = 0; i <= H - R; i++){
        for (int j = 0; j <= W - C; j++){
            int idx = kthQuery();
            int price = all[idx];
            answers.push_back(price);
 
            if(j < W - C){
                for (int r = i; r < i + R; r++){
                    updateFreq(disc[r][j] - 1, -1);
                    updateFreq(disc[r][j + C] - 1, 1);
                }
            }
        }
 
        for (int j = W - C; j >= 1; j--){
            for (int r = i; r < i + R; r++){
                updateFreq(disc[r][j - 1 + C] - 1, -1);
                updateFreq(disc[r][j - 1] - 1, 1);
            }
        }
 
        if(i < H - R){
            for (int c = 0; c < C; c++){
                updateFreq(disc[i][c] - 1, -1);
                updateFreq(disc[i + R][c] - 1, 1);
            }
        }
    }
 
    sort(answers.begin(), answers.end());
    for (int val : answers) Cout(val);
    return 0;
}
accuracy: 1
time_ms: 110
memory_kb: 6152
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;

#ifdef LOCAL
#include <debug_print.hpp>
#define debug(...) debug_print::multi_print(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (static_cast<void>(0))
#endif

template <class T>
struct BinaryIndexedTree {
    int N;
    vector<T> data;

    BinaryIndexedTree() = default;
    BinaryIndexedTree(int size) : N(size), data(size + 1, 0) {}
    BinaryIndexedTree(const vector<T>& v) : N(int(v.size())), data(N + 1, 0) {
        for (int i = 0; i < N; i++) add(i, v[i]);
    }
    void add(int i, T x) {
        ++i;
        while (i <= N) {
            data[i] += x;
            i += i & -i;
        }
    }
    void add(int l, int r, T x) { add(l, x), add(r + 1, -x); }
    T sum(int i) const {
        if (i < 0) return T{};
        T res = 0;
        while (i > 0) {
            res += data[i];
            i -= i & -i;
        }
        return res;
    }
    T sum(int l, int r) const { return sum(r) - sum(l); }
    T operator[](int i) const { return sum(i + 1) - sum(i); }
    // minimize i s.t. sum(i) >= w
    int lower_bound(T w) const {
        if (w <= 0) return 0;
        int i = 0;
        for (int k = 1 << __lg(N); k > 0; k >>= 1) {
            if (i + k <= N && data[i + k] < w) {
                w -= data[i + k];
                i += k;
            }
        }
        return i;
    }
    // minimize i s.t. sum(i) > w
    int upper_bound(T w) const {
        if (w < 0) return 0;
        int i = 0;
        for (int k = 1 << __lg(N); k > 0; k >>= 1) {
            if (i + k <= N && data[i + k] <= w) {
                w -= data[i + k];
                i += k;
            }
        }
        return i;
    }
};

template <class T>
vector<T> compress(vector<T>& v) {
    int n = v.size();
    vector<T> res = v;
    sort(res.begin(), res.end());
    res.erase(unique(res.begin(), res.end()), res.end());
    for (int i = 0; i < n; i++) {
        v[i] = lower_bound(res.begin(), res.end(), v[i]) - res.begin();
    }
    return res;
}

// O(M^3 log M^2) 時間
int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    int H, W, R, C, K;
    cin >> H >> W >> R >> C >> K;
    vector A(H, vector<int>(W));
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            cin >> A[i][j];
        }
    }

    // compress
    vector<int> values;
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            values.emplace_back(A[i][j]);
        }
    }
    sort(values.begin(), values.end());
    values.erase(unique(values.begin(), values.end()), values.end());
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            A[i][j] = lower_bound(values.begin(), values.end(), A[i][j]) - values.begin();
        }
    }

    debug(values, A);
    vector<int> ans;
    vector<BinaryIndexedTree<int>> bit(H - R + 1, BinaryIndexedTree<int>(values.size() + 1));
    // [i, i + R)[0, C) の bit 
    for (int i = 0; i < H - R + 1; i++) {
        for (int j = 0; j < C; j++) {
            for (int k = 0; k < R; k++) {
                bit[i].add(A[i + k][j], 1);
            }
        }
        ans.emplace_back(values[bit[i].lower_bound(K)]);
    }

    // 移動
    for (int i = 0; i < H - R + 1; i++) {
        for (int j = 1; j < W - C + 1; j++) {
            for (int k = 0; k < R; k++) {
                bit[i].add(A[i + k][j - 1], -1);
                bit[i].add(A[i + k][j + C - 1], 1);
            }
            ans.emplace_back(values[bit[i].lower_bound(K)]);
        }
    }

    sort(ans.begin(), ans.end());
    for (auto&& v: ans) cout << v << '\n';
}
accuracy: 1
time_ms: 500
memory_kb: 257120
final_rank: 14

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF (ll)2e18
using pii = pair<int,int>;
using qii = priority_queue<pii,vector<pii>,greater<pii>>;//{cost,node}
vector<pii> d = {{0,1},{1,0},{-1,0},{0,-1}};

template<typename T> bool chmin(T &a, T b) { if (a > b){a = b;return true;}else return false; }
template<typename T> bool chmax(T &a, T b) { if (a < b){a = b;return true;}else return false; }
template<typename T> void vec_out(vector<T> a){for (int i = 0;i < a.size();i++)cout << a[i] << ' ';}


template<typename T>
struct segtree {//completely varified
    function<T(T, T)> op;
    T e;
    vector<T> tree;
    int n;

    segtree(function<T(T, T)> op_, T e_, const vector<T>& v_) 
        : op(op_), e(e_), n(v_.size()) {
        tree.resize(2 * n);
        for (int i = 0; i < n; ++i) {
            tree[n + i] = v_[i];
        }
        for (int i = n - 1; i > 0; --i) {
            tree[i] = op(tree[i * 2], tree[i * 2 + 1]);
        }
    }

    T query(int l, int r) {
        T resl = e, resr = e;
        l += n;
        r += n;
        while (l < r) {
            if (l & 1) resl = op(resl, tree[l++]);
            if (r & 1) resr = op(tree[--r], resr);
            l /= 2;
            r /= 2;
        }
        return op(resl, resr);
    }

    void update(int idx, T val) {
        idx += n;
        tree[idx] = val;
        while (idx > 1) {
            idx /= 2;
            tree[idx] = op(tree[2 * idx], tree[2 * idx + 1]);
        }
    }
    void incriment(int idx){
        update(idx,query(idx,idx + 1) + 1);
    } 
    void decriment(int idx){
        update(idx,query(idx,idx + 1) - 1);
    }
    int kth_min(int k){//op(a,b) return a + b;
        int l = 0,r = n;
        while (l < r){
            int mid = (l + r) / 2;
            if (query(0,mid + 1) < k){
                l = mid + 1;
            }
            else{
                r = mid;
            }
        }
        //update(r,tree[r + n] - 1);
        return r;
    }
};

int op(int a,int b){return a + b;}
int e = 0;

int main(){
    int h,w,r,c;
    cin >> h >> w >> r >> c;
    int k;
    cin >> k;
    vector<vector<int>> a(h,vector<int> (w));
    vector<int> uniq;
    for (int i = 0;i < h;i++){
        for (int j = 0;j < w;j++){
            cin >> a[i][j];
            uniq.push_back(a[i][j]);
        }
    }
    sort(uniq.begin(),uniq.end());
    uniq.erase(unique(uniq.begin(),uniq.end()),uniq.end());
    vector<vector<int>> compressed(h,vector<int> (w));
    for (int i= 0;i < h;i++){
        for (int j = 0;j < w;j++){
            compressed[i][j] = lower_bound(uniq.begin(),uniq.end(),a[i][j]) - uniq.begin();
        }
    }
    vector<int> b(160001);
    segtree<int> seg(op,e,b);
    for (int i = 0;i < r;i++){
        for (int j = 0;j < c;j++){
            seg.incriment(compressed[i][j]);
        }
    }
    
    vector<vector<int>> ans(h - r + 1,vector<int> (w - c + 1));
    bool straight = true;
    int idx = 0;
    for (int i = 0;i < h - r + 1;i++){
        if (straight){
            for (int j = 0;j < w -c + 1;j++){
                ans[i][j] = seg.kth_min(k);
                if (j == w - c && i != h - r){
                    for (int k = w - c;k < w;k++){
                        seg.decriment(compressed[i][k]);
                        seg.incriment(compressed[i + r][k]);
                    }
                    continue;
                }
                if (j == w - c)continue;
                for (int k = 0;k < r;k++){
                    seg.decriment(compressed[i + k][j]);
                    seg.incriment(compressed[i + k][j + c]);
                }
            }
            straight = false;
        }
        else{
            for (int j = w - c;j >= 0;j--){
                ans[i][j] = seg.kth_min(k);
                if (j == 0 && i != h - r){
                    for (int k = 0;k < c;k++){
                        seg.decriment(compressed[i][k]);
                        seg.incriment(compressed[i + r][k]);
                    }
                    continue;
                }
                if (j == 0)continue;
                for (int k = 0;k < r;k++){
                    seg.decriment(compressed[i + k][j + c - 1]);
                    seg.incriment(compressed[i + k][j - 1]);
                }
            }
            straight = true;
        }
    }

    vector<int> sortedans;
    for (int i = 0;i < h - r + 1;i++){
        for (int j = 0;j < w - c + 1;j++){
            sortedans.push_back(uniq[ans[i][j]]);
        }
    }
    sort(sortedans.begin(),sortedans.end());
    for (int i = 0;i < (h - r + 1) * (w - c + 1);i++){
        cout << sortedans[i] << endl;
    }
    

}
accuracy: 1
time_ms: 1180
memory_kb: 8956
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct segtree{
  int n;
  vector<ll> node;
  segtree(int N){
    n = 1;
    while(n<N) n*=2;
    node.resize(2*n-1,0);
  }
  void update(int a,ll x){
    int i = a+n-1;
    node[i] = x;
    while(i>0){
      i = (i-1)/2;
      node[i] = node[2*i+1]+node[2*i+2];
    }
  }
  ll find(ll a,ll b,ll k=0,ll l=0,ll r=-1){
    if(r<0) r = n;
    if(b<=l||r<=a) return 0;
    if(a<=l&&r<=b) return node[k];
    ll vl = find(a,b,2*k+1,l,(l+r)/2);
    ll vr = find(a,b,2*k+2,(l+r)/2,r);
    return vl+vr;
  }
};
int main() {
  int H,W,R,C;
  cin >> H >> W >> R >> C;
  int K;
  cin >> K;
  vector<vector<ll>> a(H,vector<ll>(W));
  for(int i=0;i<H;i++){
    for(int j=0;j<W;j++) cin >> a[i][j];
  }
  vector<pair<ll,ll>> B;
  for(int i=0;i<H;i++){
    for(int j=0;j<W;j++) B.push_back(pair(a[i][j],i*W+j));
  }
  sort(B.begin(),B.end());
  vector<ll> sB(H*W);
  vector<ll> iB(H*W);
  segtree seg(H*W);
  for(int i=0;i<B.size();i++){
    sB[i] = B[i].first;
    iB[B[i].second] = i;
  }
  vector<ll> ans;
  for(int i=0;i<=H-R;i++){
    if(i%2==0){
      for(int j=0;j<=W-C;j++){
        if(R*C==1){
        }
        if(i==0&&j==0){
          for(int k=0;k<R;k++){
            for(int l=0;l<C;l++){
              seg.update(iB[k*W+l],1);
            }
          }
        }else if(j==0){
          for(int k=j;k<j+C;k++){
            seg.update(iB[(i-1)*W+k],0);
          }
          for(int k=j;k<j+C;k++){
            seg.update(iB[(i+R-1)*W+k],1);
          }
        }else{
          for(int k=i;k<i+R;k++){
            seg.update(iB[k*W+j-1],0);
          }
          for(int k=i;k<i+R;k++){
            seg.update(iB[k*W+j+C-1],1);
          }
        }
        ll left = -1;
        ll right = H*W;
        while(right-left>1){
          ll mid = (left+right)/2;
          if(seg.find(0,mid+1)<K) left = mid;
          else right = mid;
        }
        ans.push_back(sB[right]);
      }
    }else{
      for(int j=W-C;j>=0;j--){
        if(j==W-C){
          for(int k=j;k<j+C;k++){
            seg.update(iB[(i-1)*W+k],0);
          }
          for(int k=j;k<j+C;k++){
            seg.update(iB[(i+R-1)*W+k],1);
          }
        }else{
          for(int k=i;k<i+R;k++){
            seg.update(iB[k*W+j],1);
          }
          for(int k=i;k<i+R;k++){
            seg.update(iB[k*W+j+C],0);
          }
        }
        ll left = -1;
        ll right = H*W;
        while(right-left>1){
          ll mid = (left+right)/2;
          if(seg.find(0,mid+1)<K) left = mid;
          else right = mid;
        }
        ans.push_back(sB[right]);
      }
    }
  }
  sort(ans.begin(),ans.end());
  for(auto x:ans){
    cout << x << endl;
  }
}
accuracy: 1
time_ms: 780
memory_kb: 18112
final_rank: 7

code_snippet: #pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
using namespace std;
#define all(v) (v).begin(),(v).end()
#define pb(a) push_back(a)
#define rep(i, n) for(int i=0;i<n;i++)
#define foa(e, v) for(auto& e : v)
using ll = long long;
const ll MOD7 = 1000000007, MOD998 = 998244353, INF = (1LL << 60);
#define dout(a) cout<<fixed<<setprecision(10)<<a<<endl;

namespace internal {

#ifndef _MSC_VER
template <class T>
using is_signed_int128 =
    typename std::conditional<std::is_same<T, __int128_t>::value ||
                                  std::is_same<T, __int128>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using is_unsigned_int128 =
    typename std::conditional<std::is_same<T, __uint128_t>::value ||
                                  std::is_same<T, unsigned __int128>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using make_unsigned_int128 =
    typename std::conditional<std::is_same<T, __int128_t>::value,
                              __uint128_t,
                              unsigned __int128>;

template <class T>
using is_integral = typename std::conditional<std::is_integral<T>::value ||
                                                  is_signed_int128<T>::value ||
                                                  is_unsigned_int128<T>::value,
                                              std::true_type,
                                              std::false_type>::type;

template <class T>
using is_signed_int = typename std::conditional<(is_integral<T>::value &&
                                                 std::is_signed<T>::value) ||
                                                    is_signed_int128<T>::value,
                                                std::true_type,
                                                std::false_type>::type;

template <class T>
using is_unsigned_int =
    typename std::conditional<(is_integral<T>::value &&
                               std::is_unsigned<T>::value) ||
                                  is_unsigned_int128<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<
    is_signed_int128<T>::value,
    make_unsigned_int128<T>,
    typename std::conditional<std::is_signed<T>::value,
                              std::make_unsigned<T>,
                              std::common_type<T>>::type>::type;

#else

template <class T> using is_integral = typename std::is_integral<T>;

template <class T>
using is_signed_int =
    typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using is_unsigned_int =
    typename std::conditional<is_integral<T>::value &&
                                  std::is_unsigned<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<is_signed_int<T>::value,
                                              std::make_unsigned<T>,
                                              std::common_type<T>>::type;

#endif

template <class T>
using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;

template <class T>
using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;

template <class T> using to_unsigned_t = typename to_unsigned<T>::type;

}  // namespace internal

// Reference: https://en.wikipedia.org/wiki/Fenwick_tree
template <class T> struct fenwick_tree {
    using U = internal::to_unsigned_t<T>;

  public:
    fenwick_tree() : _n(0) {}
    fenwick_tree(int n) : _n(n), data(n) {}

    void add(int p, T x) {
        assert(0 <= p && p < _n);
        p++;
        while (p <= _n) {
            data[p - 1] += U(x);
            p += p & -p;
        }
    }

    T sum(int l, int r) {
        assert(0 <= l && l <= r && r <= _n);
        return sum(r) - sum(l);
    }

  private:
    int _n;
    std::vector<U> data;

    U sum(int r) {
        U s = 0;
        while (r > 0) {
            s += data[r - 1];
            r -= r & -r;
        }
        return s;
    }
};


int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int h, w, r, c;
    cin >> h >> w >> r >> c;
    int k;
    cin >> k;
    vector<vector<int>> a(h, vector(w, 0));
    rep(i, h) rep(j, w) cin >> a[i][j];
    map<tuple<int, int, int>, int> mp;
    rep(i, h) rep(j, w) mp[{a[i][j], i, j}];
    int id = 0;
    vector ID(h, vector(w, 0));
    vector<int> inv;
    for(auto [x, y] : mp) {
        auto [A, B, C] = x;
        ID[B][C] = id ++;
        inv.pb(A);
    }
    vector<int> ans;
    rep(i, h - r + 1) {
        fenwick_tree<int> bit(h * w);
        for(int x = 0; x < r; x ++) {
            for(int y = 0; y < c - 1; y ++) {
                bit.add(ID[i + x][y], 1);
            }
        }
        for(int y = c - 1; y < w; y ++) {
            for(int x = 0; x < r; x ++) bit.add(ID[i + x][y], 1);
            int le = -1, ri = h * w;
            while(ri - le > 1) {
                int mid = (ri + le) / 2;
                if(bit.sum(0, mid) >= k) ri = mid; 
                else le = mid;
            }
            ans.pb(inv[le]);
            for(int x = 0; x < r; x ++) bit.add(ID[i + x][y - (c - 1)], -1);
        }
    }
    sort(all(ans));
    foa(e, ans) cout << e << endl;
    return 0;
}
accuracy: 1
time_ms: 490
memory_kb: 17008
final_rank: 5

code_snippet: #pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;

int main()
{
	cin.tie(nullptr);
	ios_base::sync_with_stdio(false);
	int h,w,r,c,K;
	cin >> h >> w >> r >> c >> K;
	vector<pair<int,pair<int,int>>> t;
	int x = 1,y = 1;
	while(x * x < w) x++;
	while(y * y < h) y++;
	if(r >= 100){
		y = 11;
	}
	if(c >= 100){
		x = 11;
	}
	if(r >= 300){
		y = 17;
	}
	if(c >= 300){
		x = 17;
	}
	vector<vector<vector<int>>> s(h / y + 1,vector<vector<int>>(w / x + 1,vector<int>(K,0)));
	vector<vector<int>> now(h,vector<int>(w,0));
	vector<vector<int>> cou(h / y + 1,vector<int>(w / x + 1,0));
	for(int i = 0;i < h;i++){
		for(int j = 0;j < w;j++){
			int tmp;
			cin >> tmp;
			t.push_back({tmp,{i,j}});
			if(i <= (h - r + 1) && j <= (w - c + 1)){
				s[i / y][j / x][0]++;
			}
		}
	}
	sort(t.begin(),t.end());
	auto f = [](pair<pair<int,int>,pair<int,int>> a,pair<pair<int,int>,pair<int,int>> b) -> bool {
		pair<int,int> flg;
		flg.first = b.second.first < a.first.first || a.second.first < b.first.first;
		flg.second = b.second.second < a.first.second || a.second.second < b.first.second;
		if(flg.first || flg.second) return false;
		return true;
	};
	int ans = 0;
	for(pair<int,pair<int,int>> i : t){
		pair<pair<int,int>,pair<int,int>> p = {{max(0,i.second.first - r + 1),max(0,i.second.second - c + 1)},{min(h - r + 1,i.second.first),min(w - c + 1,i.second.second)}};
		if(ans >= (h - r + 1) * (w - c + 1)) break;
		for(int k = p.first.first / y;k <= p.second.first / y;k++){
			for(int l = p.first.second / x;l <= p.second.second / x;l++){
				pair<pair<int,int>,pair<int,int>> it = {{k * y,l * x},{k * y + y - 1,l * x + x - 1}};
				if(!f(p,it)){
					continue;
				}
				if(p.first.first <= it.first.first && it.second.first <= p.second.first && p.first.second <= it.first.second && it.second.second <= p.second.second){
					cou[k][l]++;
					if(K - cou[k][l] >= 0){
						for(int j = 0;j < s[k][l][K - cou[k][l]];j++){
							ans++;
							cout << i.first << "\n";
						}
					}
				}else{
					for(int u = it.first.first;u <= it.second.first;u++){
						for(int v = it.first.second;v <= it.second.second;v++){
							if(u < 0 || u > (h - r) || v < 0 || v > (w - c)) continue;
							if(now[u][v] < K){
								s[k][l][now[u][v]]--;
							}
							now[u][v] += cou[k][l];
							if(now[u][v] < K){
								s[k][l][now[u][v]]++;
							}
							if(p.first.first <= u && u <= p.second.first && p.first.second <= v && v <= p.second.second){
								if(now[u][v] >= K) continue;
								s[k][l][now[u][v]]--;
								now[u][v]++;
								if(now[u][v] < K){
									s[k][l][now[u][v]]++;
								}
								if(now[u][v] == K){
									ans++;
									cout << i.first << "\n";
								}
							}
						}
					}
					cou[k][l] = 0;
				}
			}
		}
	}
}
accuracy: 1
time_ms: 1970
memory_kb: 222728
final_rank: 15

code_snippet: #pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;

int main()
{
	cin.tie(nullptr);
	ios_base::sync_with_stdio(false);
	long long h,w,r,c,K;
	cin >> h >> w >> r >> c >> K;
	vector<vector<long long>> a(h,vector<long long>(w));
	vector<pair<long long,pair<long long,long long>>> t;
	long long x = 1ll,y = 1ll;
	while(x * x < w) x++;
	while(y * y < h) y++;
	if(r >= 100){
		y = 10;
	}
	if(c >= 100){
		x = 10;
	}
	vector<vector<vector<long long>>> s(y,vector<vector<long long>>(x,vector<long long>(K,0ll)));
	vector<vector<long long>> now(h,vector<long long>(w,0ll));
	vector<vector<long long>> cou(h / y + 1,vector<long long>(w / x + 1,0ll));
	for(long long i = 0ll;i < h;i++){
		for(long long j = 0ll;j < w;j++){
			cin >> a[i][j];
			t.push_back({a[i][j],{i,j}});
			if(i <= (h - r + 1ll) && j <= (w - c + 1ll)){
				s[i / y][j / x][0ll]++;
			}
		}
	}
	sort(t.begin(),t.end());
	auto f = [](pair<pair<long long,long long>,pair<long long,long long>> a,pair<pair<long long,long long>,pair<long long,long long>> b) -> bool {
		pair<long long,long long> flg;
		flg.first = b.second.first < a.first.first || a.second.first < b.first.first;
		flg.second = b.second.second < a.first.second || a.second.second < b.first.second;
		if(flg.first || flg.second) return false;
		return true;
	};
	vector<long long> ans;
	for(pair<long long,pair<long long,long long>> i : t){
		pair<pair<long long,long long>,pair<long long,long long>> p = {{max(0ll,i.second.first - r + 1ll),max(0ll,i.second.second - c + 1ll)},{min(h - r + 1ll,i.second.first),min(w - c + 1ll,i.second.second)}};
		if((int)ans.size() >= (h - r + 1) * (w - c + 1)) break;
		for(long long k = 0ll;k < h / y + 1;k++){
			for(long long l = 0ll;l < w / x + 1;l++){
				pair<pair<long long,long long>,pair<long long,long long>> it = {{k * y,l * x},{k * y + y - 1ll,l * x + x - 1ll}};
				if(!f(p,it)){
					continue;
				}
				if(p.first.first <= it.first.first && it.second.first <= p.second.first && p.first.second <= it.first.second && it.second.second <= p.second.second){
					cou[k][l]++;
					if(K - cou[k][l] >= 0ll){
						for(long long j = 0ll;j < s[k][l][K - cou[k][l]];j++){
							ans.push_back(i.first);
						}
					}
				}else{
					for(long long u = it.first.first;u <= it.second.first;u++){
						for(long long v = it.first.second;v <= it.second.second;v++){
							if(u < 0ll || u > (h - r) || v < 0ll || v > (w - c)) continue;
							if(now[u][v] < K){
								s[k][l][now[u][v]]--;
							}
							now[u][v] += cou[k][l];
							if(now[u][v] < K){
								s[k][l][now[u][v]]++;
							}
						}
					}
					cou[k][l] = 0ll;
					for(long long u = it.first.first;u <= it.second.first;u++){
						for(long long v = it.first.second;v <= it.second.second;v++){
							if(u < 0ll || u > (h - r) || v < 0ll || v > (w - c)) continue;
							if(p.first.first <= u && u <= p.second.first && p.first.second <= v && v <= p.second.second){
								if(now[u][v] >= K) continue;
								s[k][l][now[u][v]]--;
								now[u][v]++;
								if(now[u][v] < K){
									s[k][l][now[u][v]]++;
								}
								if(now[u][v] == K){
									ans.push_back(i.first);
								}
							}
						}
					}
				}
			}
		}
	}
	for(long long i = 0ll;i < (long long)ans.size();i++){
		cout << ans[i] << endl;
	}
}
accuracy: 0.32432432432432434
time_ms: 840
memory_kb: 12964
final_rank: 17

code_snippet: #pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;

int main()
{
	cin.tie(nullptr);
	ios_base::sync_with_stdio(false);
	long long h,w,r,c,K;
	cin >> h >> w >> r >> c >> K;
	vector<vector<long long>> a(h,vector<long long>(w));
	vector<pair<long long,pair<long long,long long>>> t;
	long long x = 1ll,y = 1ll;
	while(x * x < w) x++;
	while(y * y < h) y++;
	if(r >= 100){
		y = 10;
	}
	if(c >= 100){
		x = 10;
	}
	vector<vector<vector<long long>>> s(y,vector<vector<long long>>(x,vector<long long>(K,0ll)));
	vector<vector<long long>> now(h,vector<long long>(w,0ll));
	vector<vector<long long>> cou(y,vector<long long>(x,0ll));
	for(long long i = 0ll;i < h;i++){
		for(long long j = 0ll;j < w;j++){
			cin >> a[i][j];
			t.push_back({a[i][j],{i,j}});
			if(i <= (h - r + 1ll) && j <= (w - c + 1ll)){
				s[i / y][j / x][0ll]++;
			}
		}
	}
	sort(t.begin(),t.end());
	auto f = [](pair<pair<long long,long long>,pair<long long,long long>> a,pair<pair<long long,long long>,pair<long long,long long>> b) -> bool {
		pair<long long,long long> flg;
		flg.first = b.second.first < a.first.first || a.second.first < b.first.first;
		flg.second = b.second.second < a.first.second || a.second.second < b.first.second;
		if(flg.first || flg.second) return false;
		return true;
	};
	vector<long long> ans;
	for(pair<long long,pair<long long,long long>> i : t){
		pair<pair<long long,long long>,pair<long long,long long>> p = {{max(0ll,i.second.first - r + 1ll),max(0ll,i.second.second - c + 1ll)},{min(h - r + 1ll,i.second.first),min(w - c + 1ll,i.second.second)}};
		if((int)ans.size() >= (h - r + 1) * (w - c + 1)) break;
		for(long long k = 0ll;k < y;k++){
			for(long long l = 0ll;l < x;l++){
				pair<pair<long long,long long>,pair<long long,long long>> it = {{k * y,l * x},{k * y + y - 1ll,l * x + x - 1ll}};
				if(!f(p,it)){
					continue;
				}
				if(p.first.first <= it.first.first && it.second.first <= p.second.first && p.first.second <= it.first.second && it.second.second <= p.second.second){
					cou[k][l]++;
					if(K - cou[k][l] >= 0ll){
						for(long long j = 0ll;j < s[k][l][K - cou[k][l]];j++){
							ans.push_back(i.first);
						}
					}
				}else{
					for(long long u = it.first.first;u <= it.second.first;u++){
						for(long long v = it.first.second;v <= it.second.second;v++){
							if(u < 0ll || u > (h - r) || v < 0ll || v > (w - c)) continue;
							if(now[u][v] < K){
								s[k][l][now[u][v]]--;
							}
							now[u][v] += cou[k][l];
							if(now[u][v] < K){
								s[k][l][now[u][v]]++;
							}
						}
					}
					cou[k][l] = 0ll;
					for(long long u = it.first.first;u <= it.second.first;u++){
						for(long long v = it.first.second;v <= it.second.second;v++){
							if(u < 0ll || u > (h - r) || v < 0ll || v > (w - c)) continue;
							if(p.first.first <= u && u <= p.second.first && p.first.second <= v && v <= p.second.second){
								if(now[u][v] >= K) continue;
								s[k][l][now[u][v]]--;
								now[u][v]++;
								if(now[u][v] < K){
									s[k][l][now[u][v]]++;
								}
								if(now[u][v] == K){
									ans.push_back(i.first);
								}
							}
						}
					}
				}
			}
		}
	}
	for(long long i = 0ll;i < (long long)ans.size();i++){
		cout << ans[i] << endl;
	}
}
accuracy: 0.32432432432432434
time_ms: 390
memory_kb: 14732
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;

int main()
{
	long long h,w,r,c,K;
	cin >> h >> w >> r >> c >> K;
	vector<vector<long long>> a(h,vector<long long>(w));
	vector<pair<long long,pair<long long,long long>>> t;
	long long x = 1ll,y = 1ll;
	while(x * x < w) x++;
	while(y * y < h) y++;
	vector<vector<vector<long long>>> s(y,vector<vector<long long>>(x,vector<long long>(K,0ll)));
	vector<vector<long long>> now(h,vector<long long>(w,0ll));
	vector<vector<long long>> cou(y,vector<long long>(x,0ll));
	for(long long i = 0ll;i < h;i++){
		for(long long j = 0ll;j < w;j++){
			cin >> a[i][j];
			t.push_back({a[i][j],{i,j}});
			if(i <= (h - r + 1ll) && j <= (w - c + 1ll)){
				s[i / y][j / x][0ll]++;
			}
		}
	}
	sort(t.begin(),t.end());
	auto f = [](pair<pair<long long,long long>,pair<long long,long long>> a,pair<pair<long long,long long>,pair<long long,long long>> b) -> bool {
		pair<long long,long long> flg;
		flg.first = b.second.first < a.first.first || a.second.first < b.first.first;
		flg.second = b.second.second < a.first.second || a.second.second < b.first.second;
		if(flg.first || flg.second) return false;
		return true;
	};
	vector<long long> ans;
	for(pair<long long,pair<long long,long long>> i : t){
		pair<pair<long long,long long>,pair<long long,long long>> p = {{max(0ll,i.second.first - r + 1ll),max(0ll,i.second.second - c + 1ll)},{min(h - r + 1ll,i.second.first),min(w - c + 1ll,i.second.second)}};
		for(long long k = 0ll;k < y;k++){
			for(long long l = 0ll;l < x;l++){
				pair<pair<long long,long long>,pair<long long,long long>> it = {{k * y,l * x},{k * y + y - 1ll,l * x + x - 1ll}};
				if(!f(p,it)){
					continue;
				}
				if(p.first.first <= it.first.first && it.second.first <= p.second.first && p.first.second <= it.first.second && it.second.second <= p.second.second){
					cou[k][l]++;
					if(K - cou[k][l] >= 0ll){
						for(long long j = 0ll;j < s[k][l][K - cou[k][l]];j++){
							ans.push_back(i.first);
						}
					}
				}else{
					for(long long u = it.first.first;u <= it.second.first;u++){
						for(long long v = it.first.second;v <= it.second.second;v++){
							if(u < 0ll || u > (h - r) || v < 0ll || v > (w - c)) continue;
							now[u][v] += cou[k][l];
						}
					}
					cou[k][l] = 0ll;
					for(long long u = it.first.first;u <= it.second.first;u++){
						for(long long v = it.first.second;v <= it.second.second;v++){
							if(u < 0ll || u > (h - r) || v < 0ll || v > (w - c)) continue;
							if(p.first.first <= u && u <= p.second.first && p.first.second <= v && v <= p.second.second){
								if(now[u][v] >= K) continue;
								s[k][l][now[u][v]]--;
								now[u][v]++;
								if(now[u][v] < K){
									s[k][l][now[u][v]]++;
								}
								if(now[u][v] == K){
									ans.push_back(i.first);
								}
							}
						}
					}
				}
			}
		}
	}
	for(long long i = 0ll;i < (long long)ans.size();i++){
		cout << ans[i] << endl;
	}
}
accuracy: 0.16216216216216217
time_ms: 390
memory_kb: 13720
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;

int main()
{
	int h,w,r,c,K;
	cin >> h >> w >> r >> c >> K;
	vector<vector<int>> a(h,vector<int>(w));
	vector<pair<int,pair<int,int>>> t;
	int x = 1,y = 1;
	while(x * x < w) x++;
	while(y * y < h) y++;
	vector<vector<vector<int>>> s(y,vector<vector<int>>(x,vector<int>(K,0)));
	vector<vector<int>> now(h,vector<int>(w,0));
	vector<vector<int>> cou(y,vector<int>(x,0));
	for(int i = 0;i < h;i++){
		for(int j = 0;j < w;j++){
			cin >> a[i][j];
			t.push_back({a[i][j],{i,j}});
			if(i <= (h - r + 1) && j <= (w - c + 1)){
				s[i / y][j / x][0]++;
			}
		}
	}
	sort(t.begin(),t.end());
	auto f = [](pair<pair<int,int>,pair<int,int>> a,pair<pair<int,int>,pair<int,int>> b) -> bool {
		pair<int,int> flg;
		flg.first = b.second.first < a.first.first || a.second.first < b.first.first;
		flg.second = b.second.second < a.first.second || a.second.second < b.first.second;
		if(flg.first || flg.second) return false;
		return true;
	};
	vector<int> ans;
	for(pair<int,pair<int,int>> i : t){
		pair<pair<int,int>,pair<int,int>> p = {{max(0,i.second.first - r + 1),max(0,i.second.second - c + 1)},{min(h - r + 1,i.second.first),min(w - c + 1,i.second.second)}};
		for(int k = 0;k < y;k++){
			for(int l = 0;l < x;l++){
				pair<pair<int,int>,pair<int,int>> it = {{k * y,l * x},{k * y + y - 1,l * x + x - 1}};
				if(!f(p,it)){
					continue;
				}
				if(p.first.first <= it.first.first && it.second.first <= p.second.first && p.first.second <= it.first.second && it.second.second <= p.second.second){
					cou[k][l]++;
					if(K - cou[k][l] >= 0){
						for(int j = 0;j < s[k][l][K - cou[k][l]];j++){
							ans.push_back(i.first);
						}
					}
				}else{
					for(int u = it.first.first;u <= it.second.first;u++){
						for(int v = it.first.second;v <= it.second.second;v++){
							if(u < 0 || u > (h - r) || v < 0 || v > (w - c)) continue;
							now[u][v] += cou[k][l];
						}
					}
					cou[k][l] = 0;
					for(int u = it.first.first;u <= it.second.first;u++){
						for(int v = it.first.second;v <= it.second.second;v++){
							if(u < 0 || u > (h - r) || v < 0 || v > (w - c)) continue;
							if(p.first.first <= u && u <= p.second.first && p.first.second <= v && v <= p.second.second){
								if(now[u][v] >= K) continue;
								s[k][l][now[u][v]]--;
								now[u][v]++;
								if(now[u][v] < K){
									s[k][l][now[u][v]]++;
								}
								if(now[u][v] == K){
									ans.push_back(i.first);
								}
							}
						}
					}
				}
			}
		}
	}
	for(int i = 0;i < (int)ans.size();i++){
		cout << ans[i] << endl;
	}
}
accuracy: 0.16216216216216217
time_ms: 430
memory_kb: 8796
final_rank: 19

code_snippet: #include <bits/stdc++.h>

struct FenwickTree {
    int lsb(int x) const { return x & (-x); }
    std::vector<int> dat;
    FenwickTree(int n) : dat(n + 1) {}
    void add(int i, int v) {
        for (i++ ; i < (int)dat.size() ; i += lsb(i)) {
            dat[i] += v;
        }
    }
    int pref(int r) const {
        int res{};
        for (int i{r} ; i ; i -= lsb(i)) {
            res += dat[i];
        }
        return res;
    }
    int prod(int l, int r) const {
        return -pref(l) + pref(r);
    }
};

int main() {
    int H, W, R, C;
    std::cin >> H >> W >> R >> C;
    int K;
    std::cin >> K;
    std::vector A(H, std::vector<int>(W));
    std::vector<int> app;
    for (int i{} ; i < H ; i++) {
        for (int j{} ; j < W ; j++) {
            std::cin >> A[i][j];
            app.push_back(A[i][j]);
        }
    }
    std::sort(app.begin(), app.end());
    app.erase(std::unique(app.begin(), app.end()), app.end());
    std::vector B(H, std::vector<int>(W));
    for (int i{} ; i < H ; i++) {
        for (int j{} ; j < W ; j++) {
            B[i][j] = (int)std::distance(app.begin(), std::lower_bound(app.begin(), app.end(), A[i][j]));
        }
    }
    FenwickTree fen(app.size());
    for (int i{} ; i < R ; i++) {
        for (int j{} ; j < C ; j++) {
            fen.add(B[i][j], 1);
        }
    }
    std::vector ans(H - R + 1, std::vector<int>(W - C + 1));
    std::vector<int> sorted;
    for (int i{} ; i < H - R + 1 ; i++) {
        for (int j{} ; j < W - C + 1 ; j++) {
            int ok{(int)app.size()}, ng{};
            while (ok - ng > 1) {
                int mid{(ok + ng) / 2};
                if (fen.prod(0, mid) >= K) {
                    ok = mid;
                }
                else {
                    ng = mid;
                }
            }
            assert(ok > 0);
            ans[i][j] = app[ok - 1];
            sorted.push_back(ans[i][j]);
            // std::cout << i << ' ' << j << std::endl;
            if (j + 1 < W - C + 1) {
                if (i % 2 == 0) {
                    for (int k{} ; k < R ; k++) {
                        fen.add(B[i + k][j], -1);
                        fen.add(B[i + k][j + C], +1);
                    }
                }
                else {
                    for (int k{} ; k < R ; k++) {
                        fen.add(B[i + k][W - j - 1], -1);
                        fen.add(B[i + k][W - C - j - 1], +1);
                    }
                }
            }
        }
        if (i + 1 < H - R + 1) {
            if (i % 2 == 0) {
                for (int k{} ; k < C ; k++) {
                    fen.add(B[i][W - C + k], -1);
                    fen.add(B[i + R][W - C + k], +1);
                }
            }
            else {
                for (int k{} ; k < C ; k++) {
                    fen.add(B[i][k], -1);
                    fen.add(B[i + R][k], +1);
                }
            }
        }
    }
    std::sort(sorted.begin(), sorted.end());
    for (auto s : sorted) {
        std::cout << s << '\n';
    }
    // for (int i{} ; i < H - R + 1 ; i++) {
    //     for (int j{} ; j < W - C + 1 ; j++) {
    //         std::cout << ans[i][j] << (j + 1 == W - C + 1 ? '\n' : ' ');
    //     }
    // }
}
accuracy: 1
time_ms: 400
memory_kb: 7828
final_rank: 4

code_snippet: #include <bits/stdc++.h>

using namespace std;

typedef long long ll;

struct Mo {
private:
    int n;
    vector<pair<int, int> > ij;

public:
    Mo(int n) : n(n) {}

    // ([0, i), [0, j))
    void insert(int i, int j){
        ij.emplace_back(i, j);
    }

    template<typename II, typename DI, typename IJ, typename DJ, typename O>
    void run(const II &inc_i, const DI &dec_i, const IJ &inc_j, const DJ &dec_j, const O &out){
        int q = (int) ij.size();
        int bs = n / min<int>(n, sqrt(q));
        vector<int> ord(q);
        iota(ord.begin(), ord.end(), 0);
        sort(ord.begin(), ord.end(), [&](int a, int b){
            int ablock = ij[a].first / bs, bblock = ij[b].first / bs;
            if(ablock != bblock) return (ablock < bblock);
            if(ablock & 1) return (ij[a].second > ij[b].second);
            return (ij[a].second < ij[b].second);
        });
        int i = 0, j = 0;
        for(auto idx : ord){
            while(i < ij[idx].first) inc_i(i++, j);
            while(i > ij[idx].first) dec_i(--i, j);
            while(j < ij[idx].second) inc_j(i, j++);
            while(j > ij[idx].second) dec_j(i, --j);
            out(idx);
        }
    }
};

int main() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);

    int h, w, r, c, k;
    cin >> h >> w >> r >> c >> k;

    vector<vector<int> > a(h, vector<int>(w));
    vector<tuple<int, int, int> > cod_xy, cod_yx;
    vector<int> v;

    for (int i = 0; i < h; ++i) {
        for (int j = 0; j < w; ++j) {
            cin >> a[i][j];
            v.emplace_back(a[i][j]);
        }
    }

    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());

    int sq = 1;
    while (sq * sq < v.size()) sq++;

    for (int i = 0; i < h; ++i) {
        for (int j = 0; j < w; ++j) {
            a[i][j] = lower_bound(v.begin(), v.end(), a[i][j]) - v.begin();
            cod_xy.emplace_back(i, j, a[i][j]);
        }
    }

    cod_xy.emplace_back(INT_MAX, INT_MAX, INT_MAX);
    cod_yx = cod_xy;

    for (auto &&[x, y, v] : cod_yx) swap(x, y);

    sort(cod_xy.begin(), cod_xy.end());
    sort(cod_yx.begin(), cod_yx.end());

    Mo mo(h * w);
    vector<int> sign;
    int q = 0;

    for (int i = r; i <= h; ++i) {
        for (int j = c; j <= w; ++j) {
            q++;
            int l = i - r, r = i, d = j - c, u = j;
            l = lower_bound(cod_xy.begin(), cod_xy.end(), tuple(l, 0, 0)) - cod_xy.begin();
            d = lower_bound(cod_yx.begin(), cod_yx.end(), tuple(d, 0, 0)) - cod_yx.begin();
            r = lower_bound(cod_xy.begin(), cod_xy.end(), tuple(r, 0, 0)) - cod_xy.begin();
            u = lower_bound(cod_yx.begin(), cod_yx.end(), tuple(u, 0, 0)) - cod_yx.begin();
            mo.insert(l, d);
            mo.insert(l, u);
            mo.insert(r, d);
            mo.insert(r, u);
            sign.emplace_back(1);
            sign.emplace_back(-1);
            sign.emplace_back(-1);
            sign.emplace_back(1);
        }
    }

    vector<int> re(q, 0), cnt(q);

    {
        // vector<vector<int> > memo(q, vector<int>(sq));
        vector memo(q, vector<int16_t>(sq));
        vector<int> seg(sq * sq);
        mo.run([&](int i, int j) {
            if (get<1>(cod_xy[i]) < get<0>(cod_yx[j])) {
                seg[get<2>(cod_xy[i]) / sq]++;
            }
        }, [&](int i, int j) {
            if (get<1>(cod_xy[i]) < get<0>(cod_yx[j])) {
                seg[get<2>(cod_xy[i]) / sq]--;
            }
        }, [&](int i, int j) {
            if (get<1>(cod_yx[j]) < get<0>(cod_xy[i])) {
                seg[get<2>(cod_yx[j]) / sq]++;
            }
        }, [&](int i, int j) {
            if (get<1>(cod_yx[j]) < get<0>(cod_xy[i])) {
                seg[get<2>(cod_yx[j]) / sq]--;
            }
        }, [&](int i) {
            for (int j = 0; j < sq; ++j) {
                memo[i >> 2][j] += seg[j] * sign[i];
            }
        });
        for (int i = 0; i < q; ++i) {
            for (int j = 0; j < sq; ++j) {
                if ((cnt[i] + memo[i][j]) >= k) {
                    re[i] = j * sq;
                    break;
                }
                cnt[i] += memo[i][j];
            }
        }
    }

    {
        vector memo(q, vector<int16_t>(sq));
        vector<int> seg(sq * sq);
        mo.run([&](int i, int j) {
            if (get<1>(cod_xy[i]) < get<0>(cod_yx[j])) {
                seg[get<2>(cod_xy[i])]++;
            }
        }, [&](int i, int j) {
            if (get<1>(cod_xy[i]) < get<0>(cod_yx[j])) {
                seg[get<2>(cod_xy[i])]--;
            }
        }, [&](int i, int j) {
            if (get<1>(cod_yx[j]) < get<0>(cod_xy[i])) {
                seg[get<2>(cod_yx[j])]++;
            }
        }, [&](int i, int j) {
            if (get<1>(cod_yx[j]) < get<0>(cod_xy[i])) {
                seg[get<2>(cod_yx[j])]--;
            }
        }, [&](int i) {
            for (int j = 0; j < sq; ++j) {
                memo[i >> 2][j] += seg[re[i >> 2] + j] * sign[i];
            }
        });
        for (int i = 0; i < q; ++i) {
            for (int j = 0; j < sq; ++j) {
                if ((cnt[i] += memo[i][j]) >= k) {
                    re[i] = re[i] + j;
                    break;
                }
            }
        }
    }

    vector<int> res;

    for (int i = 0; i < q; ++i) {
        res.emplace_back(v[re[i]]);
    }

    sort(res.begin(), res.end());

    for (auto e : res) {
        cout << e << '\n';
    }

    return (0);
}
accuracy: 1
time_ms: 960
memory_kb: 153188
final_rank: 13

code_snippet: #pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

struct Mo {
private:
    int n;
    vector<pair<int, int> > ij;

public:
    Mo(int n) : n(n) {}

    // ([0, i), [0, j))
    void insert(int i, int j){
        ij.emplace_back(i, j);
    }

    template<typename II, typename DI, typename IJ, typename DJ, typename O>
    void run(const II &inc_i, const DI &dec_i, const IJ &inc_j, const DJ &dec_j, const O &out){
        int q = (int) ij.size();
        int bs = n / min<int>(n, sqrt(q));
        vector<int> ord(q);
        iota(ord.begin(), ord.end(), 0);
        sort(ord.begin(), ord.end(), [&](int a, int b){
            int ablock = ij[a].first / bs, bblock = ij[b].first / bs;
            if(ablock != bblock) return (ablock < bblock);
            if(ablock & 1) return (ij[a].second > ij[b].second);
            return (ij[a].second < ij[b].second);
        });
        int i = 0, j = 0;
        for(auto idx : ord){
            while(i < ij[idx].first) inc_i(i++, j);
            while(i > ij[idx].first) dec_i(--i, j);
            while(j < ij[idx].second) inc_j(i, j++);
            while(j > ij[idx].second) dec_j(i, --j);
            out(idx);
        }
    }
};

int main() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);

    int h, w, r, c, k;
    cin >> h >> w >> r >> c >> k;

    vector<vector<int> > a(h, vector<int>(w));
    vector<tuple<int, int, int> > cod_xy, cod_yx;
    vector<int> v;

    for (int i = 0; i < h; ++i) {
        for (int j = 0; j < w; ++j) {
            cin >> a[i][j];
            v.emplace_back(a[i][j]);
        }
    }

    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());

    int sq = 1;
    while (sq * sq < v.size()) sq++;

    for (int i = 0; i < h; ++i) {
        for (int j = 0; j < w; ++j) {
            a[i][j] = lower_bound(v.begin(), v.end(), a[i][j]) - v.begin();
            cod_xy.emplace_back(i, j, a[i][j]);
        }
    }

    cod_xy.emplace_back(INT_MAX, INT_MAX, INT_MAX);
    cod_yx = cod_xy;

    for (auto &&[x, y, v] : cod_yx) swap(x, y);

    sort(cod_xy.begin(), cod_xy.end());
    sort(cod_yx.begin(), cod_yx.end());

    Mo mo(h * w);
    vector<int> sign;
    int q = 0;

    for (int i = r; i <= h; ++i) {
        for (int j = c; j <= w; ++j) {
            q++;
            int l = i - r, r = i, d = j - c, u = j;
            l = lower_bound(cod_xy.begin(), cod_xy.end(), tuple(l, 0, 0)) - cod_xy.begin();
            d = lower_bound(cod_yx.begin(), cod_yx.end(), tuple(d, 0, 0)) - cod_yx.begin();
            r = lower_bound(cod_xy.begin(), cod_xy.end(), tuple(r, 0, 0)) - cod_xy.begin();
            u = lower_bound(cod_yx.begin(), cod_yx.end(), tuple(u, 0, 0)) - cod_yx.begin();
            mo.insert(l, d);
            mo.insert(l, u);
            mo.insert(r, d);
            mo.insert(r, u);
            sign.emplace_back(1);
            sign.emplace_back(-1);
            sign.emplace_back(-1);
            sign.emplace_back(1);
        }
    }

    vector<int> re(q, 0), cnt(q);

    {
        // vector<vector<int> > memo(q, vector<int>(sq));
        vector memo(q, vector<int16_t>(sq));
        vector<int> seg(sq * sq);
        mo.run([&](int i, int j) {
            if (get<1>(cod_xy[i]) < get<0>(cod_yx[j])) {
                seg[get<2>(cod_xy[i]) / sq]++;
            }
        }, [&](int i, int j) {
            if (get<1>(cod_xy[i]) < get<0>(cod_yx[j])) {
                seg[get<2>(cod_xy[i]) / sq]--;
            }
        }, [&](int i, int j) {
            if (get<1>(cod_yx[j]) < get<0>(cod_xy[i])) {
                seg[get<2>(cod_yx[j]) / sq]++;
            }
        }, [&](int i, int j) {
            if (get<1>(cod_yx[j]) < get<0>(cod_xy[i])) {
                seg[get<2>(cod_yx[j]) / sq]--;
            }
        }, [&](int i) {
            for (int j = 0; j < sq; ++j) {
                memo[i >> 2][j] += seg[j] * sign[i];
            }
        });
        for (int i = 0; i < q; ++i) {
            for (int j = 0; j < sq; ++j) {
                if ((cnt[i] + memo[i][j]) >= k) {
                    re[i] = j * sq;
                    break;
                }
                cnt[i] += memo[i][j];
            }
        }
    }

    {
        vector memo(q, vector<int16_t>(sq));
        vector<int> seg(sq * sq);
        mo.run([&](int i, int j) {
            if (get<1>(cod_xy[i]) < get<0>(cod_yx[j])) {
                seg[get<2>(cod_xy[i])]++;
            }
        }, [&](int i, int j) {
            if (get<1>(cod_xy[i]) < get<0>(cod_yx[j])) {
                seg[get<2>(cod_xy[i])]--;
            }
        }, [&](int i, int j) {
            if (get<1>(cod_yx[j]) < get<0>(cod_xy[i])) {
                seg[get<2>(cod_yx[j])]++;
            }
        }, [&](int i, int j) {
            if (get<1>(cod_yx[j]) < get<0>(cod_xy[i])) {
                seg[get<2>(cod_yx[j])]--;
            }
        }, [&](int i) {
            for (int j = 0; j < sq; ++j) {
                memo[i >> 2][j] += seg[re[i >> 2] + j] * sign[i];
            }
        });
        for (int i = 0; i < q; ++i) {
            for (int j = 0; j < sq; ++j) {
                if ((cnt[i] += memo[i][j]) >= k) {
                    re[i] = re[i] + j;
                    break;
                }
            }
        }
    }

    vector<int> res;

    for (int i = 0; i < q; ++i) {
        res.emplace_back(v[re[i]]);
    }

    sort(res.begin(), res.end());

    for (auto e : res) {
        cout << e << '\n';
    }

    return (0);
}
accuracy: 1
time_ms: 690
memory_kb: 152800
final_rank: 10

code_snippet: #pragma GCC target("avx2")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

struct Mo {
private:
    int n;
    vector<pair<int, int> > ij;

public:
    Mo(int n) : n(n) {}

    // ([0, i), [0, j))
    void insert(int i, int j){
        ij.emplace_back(i, j);
    }

    template<typename II, typename DI, typename IJ, typename DJ, typename O>
    void run(const II &inc_i, const DI &dec_i, const IJ &inc_j, const DJ &dec_j, const O &out){
        int q = (int) ij.size();
        int bs = n / min<int>(n, sqrt(q));
        vector<int> ord(q);
        iota(ord.begin(), ord.end(), 0);
        sort(ord.begin(), ord.end(), [&](int a, int b){
            int ablock = ij[a].first / bs, bblock = ij[b].first / bs;
            if(ablock != bblock) return (ablock < bblock);
            if(ablock & 1) return (ij[a].second > ij[b].second);
            return (ij[a].second < ij[b].second);
        });
        int i = 0, j = 0;
        for(auto idx : ord){
            while(i < ij[idx].first) inc_i(i++, j);
            while(i > ij[idx].first) dec_i(--i, j);
            while(j < ij[idx].second) inc_j(i, j++);
            while(j > ij[idx].second) dec_j(i, --j);
            out(idx);
        }
    }
};

int main() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);

    int h, w, r, c, k;
    cin >> h >> w >> r >> c >> k;

    vector<vector<int> > a(h, vector<int>(w));
    vector<tuple<int, int, int> > cod_xy, cod_yx;
    vector<int> v;

    for (int i = 0; i < h; ++i) {
        for (int j = 0; j < w; ++j) {
            cin >> a[i][j];
            v.emplace_back(a[i][j]);
        }
    }

    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());

    int sq = 1;
    while (sq * sq < v.size()) sq++;

    for (int i = 0; i < h; ++i) {
        for (int j = 0; j < w; ++j) {
            a[i][j] = lower_bound(v.begin(), v.end(), a[i][j]) - v.begin();
            cod_xy.emplace_back(i, j, a[i][j]);
        }
    }

    cod_xy.emplace_back(INT_MAX, INT_MAX, INT_MAX);
    cod_yx = cod_xy;

    for (auto &&[x, y, v] : cod_yx) swap(x, y);

    sort(cod_xy.begin(), cod_xy.end());
    sort(cod_yx.begin(), cod_yx.end());

    Mo mo(h * w);
    vector<int> sign;
    int q = 0;

    for (int i = r; i <= h; ++i) {
        for (int j = c; j <= w; ++j) {
            q++;
            int l = i - r, r = i, d = j - c, u = j;
            l = lower_bound(cod_xy.begin(), cod_xy.end(), tuple(l, 0, 0)) - cod_xy.begin();
            d = lower_bound(cod_yx.begin(), cod_yx.end(), tuple(d, 0, 0)) - cod_yx.begin();
            r = lower_bound(cod_xy.begin(), cod_xy.end(), tuple(r, 0, 0)) - cod_xy.begin();
            u = lower_bound(cod_yx.begin(), cod_yx.end(), tuple(u, 0, 0)) - cod_yx.begin();
            mo.insert(l, d);
            mo.insert(l, u);
            mo.insert(r, d);
            mo.insert(r, u);
            sign.emplace_back(1);
            sign.emplace_back(-1);
            sign.emplace_back(-1);
            sign.emplace_back(1);
        }
    }

    vector<int> re(q, 0), cnt(q);

    {
        // vector<vector<int> > memo(q, vector<int>(sq));
        vector memo(q, vector<int16_t>(sq));
        vector<int> seg(sq * sq);
        mo.run([&](int i, int j) {
            if (get<1>(cod_xy[i]) < get<0>(cod_yx[j])) {
                seg[get<2>(cod_xy[i]) / sq]++;
            }
        }, [&](int i, int j) {
            if (get<1>(cod_xy[i]) < get<0>(cod_yx[j])) {
                seg[get<2>(cod_xy[i]) / sq]--;
            }
        }, [&](int i, int j) {
            if (get<1>(cod_yx[j]) < get<0>(cod_xy[i])) {
                seg[get<2>(cod_yx[j]) / sq]++;
            }
        }, [&](int i, int j) {
            if (get<1>(cod_yx[j]) < get<0>(cod_xy[i])) {
                seg[get<2>(cod_yx[j]) / sq]--;
            }
        }, [&](int i) {
            for (int j = 0; j < sq; ++j) {
                memo[i >> 2][j] += seg[j] * sign[i];
            }
        });
        for (int i = 0; i < q; ++i) {
            for (int j = 0; j < sq; ++j) {
                if ((cnt[i] + memo[i][j]) >= k) {
                    re[i] = j * sq;
                    break;
                }
                cnt[i] += memo[i][j];
            }
        }
    }

    {
        vector memo(q, vector<int16_t>(sq));
        vector<int> seg(sq * sq);
        mo.run([&](int i, int j) {
            if (get<1>(cod_xy[i]) < get<0>(cod_yx[j])) {
                seg[get<2>(cod_xy[i])]++;
            }
        }, [&](int i, int j) {
            if (get<1>(cod_xy[i]) < get<0>(cod_yx[j])) {
                seg[get<2>(cod_xy[i])]--;
            }
        }, [&](int i, int j) {
            if (get<1>(cod_yx[j]) < get<0>(cod_xy[i])) {
                seg[get<2>(cod_yx[j])]++;
            }
        }, [&](int i, int j) {
            if (get<1>(cod_yx[j]) < get<0>(cod_xy[i])) {
                seg[get<2>(cod_yx[j])]--;
            }
        }, [&](int i) {
            for (int j = 0; j < sq; ++j) {
                memo[i >> 2][j] += seg[re[i >> 2] + j] * sign[i];
            }
        });
        for (int i = 0; i < q; ++i) {
            for (int j = 0; j < sq; ++j) {
                if ((cnt[i] += memo[i][j]) >= k) {
                    re[i] = re[i] + j;
                    break;
                }
            }
        }
    }

    vector<int> res;

    for (int i = 0; i < q; ++i) {
        res.emplace_back(v[re[i]]);
    }

    sort(res.begin(), res.end());

    for (auto e : res) {
        cout << e << '\n';
    }

    return (0);
}
accuracy: 1
time_ms: 810
memory_kb: 153032
final_rank: 11

code_snippet: #include <bits/stdc++.h>

using namespace std;

typedef long long ll;

struct Mo {
private:
    int n;
    vector<pair<int, int> > ij;

public:
    Mo(int n) : n(n) {}

    // ([0, i), [0, j))
    void insert(int i, int j){
        ij.emplace_back(i, j);
    }

    template<typename II, typename DI, typename IJ, typename DJ, typename O>
    void run(const II &inc_i, const DI &dec_i, const IJ &inc_j, const DJ &dec_j, const O &out){
        int q = (int) ij.size();
        int bs = n / min<int>(n, sqrt(q));
        vector<int> ord(q);
        iota(ord.begin(), ord.end(), 0);
        sort(ord.begin(), ord.end(), [&](int a, int b){
            int ablock = ij[a].first / bs, bblock = ij[b].first / bs;
            if(ablock != bblock) return (ablock < bblock);
            if(ablock & 1) return (ij[a].second > ij[b].second);
            return (ij[a].second < ij[b].second);
        });
        int i = 0, j = 0;
        for(auto idx : ord){
            while(i < ij[idx].first) inc_i(i++, j);
            while(i > ij[idx].first) dec_i(--i, j);
            while(j < ij[idx].second) inc_j(i, j++);
            while(j > ij[idx].second) dec_j(i, --j);
            out(idx);
        }
    }
};

int main() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);

    int h, w, r, c, k;
    cin >> h >> w >> r >> c >> k;

    vector<vector<int> > a(h, vector<int>(w));
    vector<tuple<int, int, int> > cod_xy, cod_yx;
    vector<int> v;

    for (int i = 0; i < h; ++i) {
        for (int j = 0; j < w; ++j) {
            cin >> a[i][j];
            v.emplace_back(a[i][j]);
        }
    }

    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());

    int sq = 1;
    while (sq * sq < v.size()) sq++;

    for (int i = 0; i < h; ++i) {
        for (int j = 0; j < w; ++j) {
            a[i][j] = lower_bound(v.begin(), v.end(), a[i][j]) - v.begin();
            cod_xy.emplace_back(i, j, a[i][j]);
        }
    }

    cod_xy.emplace_back(INT_MAX, INT_MAX, INT_MAX);
    cod_yx = cod_xy;

    for (auto &&[x, y, v] : cod_yx) swap(x, y);

    sort(cod_xy.begin(), cod_xy.end());
    sort(cod_yx.begin(), cod_yx.end());

    Mo mo(h * w);
    vector<int> sign;
    int q = 0;

    for (int i = r; i <= h; ++i) {
        for (int j = c; j <= w; ++j) {
            q++;
            int l = i - r, r = i, d = j - c, u = j;
            l = lower_bound(cod_xy.begin(), cod_xy.end(), tuple(l, 0, 0)) - cod_xy.begin();
            d = lower_bound(cod_yx.begin(), cod_yx.end(), tuple(d, 0, 0)) - cod_yx.begin();
            r = lower_bound(cod_xy.begin(), cod_xy.end(), tuple(r, 0, 0)) - cod_xy.begin();
            u = lower_bound(cod_yx.begin(), cod_yx.end(), tuple(u, 0, 0)) - cod_yx.begin();
            mo.insert(l, d);
            mo.insert(l, u);
            mo.insert(r, d);
            mo.insert(r, u);
            sign.emplace_back(1);
            sign.emplace_back(-1);
            sign.emplace_back(-1);
            sign.emplace_back(1);
        }
    }

    vector<int> re(q, 0), cnt(q);

    {
        // vector<vector<int> > memo(q, vector<int>(sq));
        vector memo(q, vector<int16_t>(sq));
        vector<int> seg(sq * sq);
        mo.run([&](int i, int j) {
            if (get<1>(cod_xy[i]) < get<0>(cod_yx[j])) {
                seg[get<2>(cod_xy[i]) / sq]++;
            }
        }, [&](int i, int j) {
            if (get<1>(cod_xy[i]) < get<0>(cod_yx[j])) {
                seg[get<2>(cod_xy[i]) / sq]--;
            }
        }, [&](int i, int j) {
            if (get<1>(cod_yx[j]) < get<0>(cod_xy[i])) {
                seg[get<2>(cod_yx[j]) / sq]++;
            }
        }, [&](int i, int j) {
            if (get<1>(cod_yx[j]) < get<0>(cod_xy[i])) {
                seg[get<2>(cod_yx[j]) / sq]--;
            }
        }, [&](int i) {
            for (int j = 0; j < sq; ++j) {
                memo[i >> 2][j] += seg[j] * sign[i];
            }
        });
        for (int i = 0; i < q; ++i) {
            for (int j = 0; j < sq; ++j) {
                if ((cnt[i] + memo[i][j]) >= k) {
                    re[i] = j * sq;
                    break;
                }
                cnt[i] += memo[i][j];
            }
        }
    }

    {
        vector memo(q, vector<int16_t>(sq));
        vector<int> seg(sq * sq);
        mo.run([&](int i, int j) {
            if (get<1>(cod_xy[i]) < get<0>(cod_yx[j])) {
                seg[get<2>(cod_xy[i])]++;
            }
        }, [&](int i, int j) {
            if (get<1>(cod_xy[i]) < get<0>(cod_yx[j])) {
                seg[get<2>(cod_xy[i])]--;
            }
        }, [&](int i, int j) {
            if (get<1>(cod_yx[j]) < get<0>(cod_xy[i])) {
                seg[get<2>(cod_yx[j])]++;
            }
        }, [&](int i, int j) {
            if (get<1>(cod_yx[j]) < get<0>(cod_xy[i])) {
                seg[get<2>(cod_yx[j])]--;
            }
        }, [&](int i) {
            for (int j = 0; j < sq; ++j) {
                memo[i >> 2][j] += seg[re[i >> 2] + j] * sign[i];
            }
        });
        for (int i = 0; i < q; ++i) {
            for (int j = 0; j < sq; ++j) {
                if ((cnt[i] += memo[i][j]) >= k) {
                    re[i] = re[i] + j;
                    break;
                }
            }
        }
    }

    vector<int> res;

    for (int i = 0; i < q; ++i) {
        res.emplace_back(v[re[i]]);
    }

    sort(res.begin(), res.end());

    for (auto e : res) {
        cout << e << '\n';
    }

    return (0);
}
accuracy: 1
time_ms: 930
memory_kb: 154640
final_rank: 12

code_snippet: #include <bits/stdc++.h>

struct Compressor {
    std::vector<int> data;
    Compressor() {}
    void add(int val) {
        data.push_back(val);
    }
    void build() {
        std::sort(data.begin(), data.end());
        data.erase(std::unique(data.begin(), data.end()), data.end());
    }
    int compress(int val) {
        return std::lower_bound(data.begin(), data.end(), val) - data.begin();
    }
    int decompress(int pos) {
        return data[pos];
    }
    int size() {
        return data.size();
    }
};

struct SegmentTree {
    std::vector<int> data;
    int size;
    SegmentTree(int n) {
        int log = 0;
        while (n > (1 << log)) log++;
        size = 1 << log;
        data.resize(2 * size);
    }
    void add(int pos, int diff) {
        pos += size;
        data[pos] += diff;
        for (pos >>= 1; pos > 0; pos >>= 1) {
            data[pos] = data[2 * pos] + data[2 * pos + 1];
        }
    }
    int kth_element(int k) {
        int pos = 1;
        int now = 0;
        while (pos < size) {
            if (now + data[2 * pos] < k) {
                now += data[2 * pos];
                pos = 2 * pos + 1;
            } else {
                pos = 2 * pos;
            }
        }
        assert(now < k);
        assert(now + data[pos] >= k);
        return pos - size;
    }
    void clear() {
        data.assign(2 * size, 0);
    }
};

void test() {
    SegmentTree segtree(10);
    segtree.add(0, 1);
    segtree.add(3, 1);
    segtree.add(7, 1);
    std::cout << segtree.kth_element(1) << '\n';
    std::cout << segtree.kth_element(2) << '\n';
    std::cout << segtree.kth_element(3) << '\n';
}

int main() {
    int h, w, r, c;
    std::cin >> h >> w >> r >> c;
    int k;
    std::cin >> k;
    std::vector a(h, std::vector<int>(w));
    Compressor compressor;
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            std::cin >> a[i][j];
            compressor.add(a[i][j]);
        }
    }
    compressor.build();
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            a[i][j] = compressor.compress(a[i][j]);
        }
    }
    std::vector<int> ans;
    SegmentTree segtree(compressor.size());
    for (int i = 0; i + r <= h; i++) {
        int right = 0;
        segtree.clear();
        for (int left = 0; left + c <= w; left++) {
            while (right < left + c) {
                for (int j = 0; j < r; j++) {
                    segtree.add(a[i + j][right], 1);
                }
                right++;
            }
            ans.push_back(compressor.decompress(segtree.kth_element(k)));
            for (int j = 0; j < r; j++) {
                segtree.add(a[i + j][left], -1);
            }
        }
    }
    std::sort(ans.begin(), ans.end());
    for (auto v: ans) {
        std::cout << v << '\n';
    }
}
accuracy: 1
time_ms: 630
memory_kb: 7964
final_rank: 6