Submission #267184

# Submission time Handle Problem Language Result Execution time Memory
267184 2020-08-16T00:17:48 Z doowey Rectangles (IOI19_rect) C++14
59 / 100
5000 ms 195812 KB
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include "rect.h"

using namespace std;

typedef long long ll;
typedef pair<short, short> pii;

#define fi first
#define se second
#define mp make_pair

const short N = 2505;

struct segtree{
    vector<short> ele;
    short mode;
    short n;
    void init(short sz, short md){
        mode = md;
        n = sz;
        if(md == 0)
            ele.resize(2 * n, 5000);
        else
            ele.resize(2 * n, -1);
    }
    void upd(short id, short v){
        id += n;
        if(mode == 0)
            ele[id] = min(ele[id], v);
        else
            ele[id] = max(ele[id], v);
        id >>= 1;
        while(id > 0){
            if(mode == 0) ele[id] = min(ele[id << 1], ele[id << 1 | 1]);
            else ele[id] = max(ele[id << 1], ele[id << 1 | 1]);
            id >>= 1;
        }
    }
    short get(short l, short r){
        l += n;
        r += n;
        short res = -1;
        if(mode == 0) res = 5000;
        while(l <= r){
            if((l & 1)){
                if(mode == 0) res = min(res, ele[l]);
                else res = max(res, ele[l]);
                l ++ ;
            }
            if(!(r & 1)){
                if(mode == 0) res = min(res, ele[r]);
                else res = max(res, ele[r]);
                r -- ;
            }
            l >>= 1;
            r >>= 1;
        }
        return res;
    }
};

segtree L[N], R[N], U[N], D[N];
short Lf[N][N], Rf[N][N], Uf[N][N], Df[N][N];

bool is_valid(short i1, short j1, short i2, short j2){
    if(i1 == -1 || j1 == -1 || i2 == -1 || j2 == -1) return false;
    if(R[j1].get(i1 + 1, i2 - 1) < j2) return false;
    if(L[j2].get(i1 + 1, i2 - 1) > j1) return false;
    if(D[i1].get(j1 + 1, j2 - 1) < i2) return false;
    if(U[i2].get(j1 + 1, j2 - 1) > i1) return false;
    return true;
}

ll count_rectangles(vector<vector<int>> e){
    int n = e.size();
    int m = e[0].size();
    vector<int> mono;
    for(int i = 0 ; i < m ; i ++ ){
        L[i].init(n,1);
        R[i].init(n,0);
    }
    for(int i = 0 ; i < n; i ++ ){
        mono.clear();
        for(int j = 0 ; j < m ; j ++ ){
            Lf[i][j] = -1;
            while(!mono.empty() && e[i][mono.back()] <= e[i][j]){
                if(e[i][mono.back()] == e[i][j]){
                    L[j].upd(i, mono.back());
                }
                mono.pop_back();
            }
            if(!mono.empty()){
                Lf[i][j] = mono.back();
                L[j].upd(i, mono.back());
            }
            mono.push_back(j);
        }
        mono.clear();
        for(int j = m - 1; j >= 0 ; j -- ){
            Rf[i][j] = -1;
            while(!mono.empty() && e[i][mono.back()] <= e[i][j]){
                if(e[i][mono.back()] == e[i][j]){
                    R[j].upd(i, mono.back());
                }
                mono.pop_back();
            }
            if(!mono.empty()){
                Rf[i][j] = mono.back();
                R[j].upd(i, mono.back());
            }
            mono.push_back(j);
        }
    }
    for(int i = 0 ; i < n; i ++ ){
        U[i].init(m, 1);
        D[i].init(m, 0);
    }
    for(int j = 0 ; j < m ; j ++ ){
        mono.clear();
        for(int i = 0 ; i < n; i ++ ){
            Uf[i][j] = -1;
            while(!mono.empty() && e[mono.back()][j] <= e[i][j]){
                if(e[mono.back()][j] == e[i][j]){
                    U[i].upd(j, mono.back());
                }
                mono.pop_back();
            }
            if(!mono.empty()){
                Uf[i][j] = mono.back();
                U[i].upd(j, mono.back());
            }
            mono.push_back(i);
        }
        mono.clear();
        for(int i = n - 1; i >= 0 ; i -- ){
            Df[i][j] = -1;
            while(!mono.empty() && e[mono.back()][j] <= e[i][j]){
                if(e[mono.back()][j] == e[i][j]){
                    D[i].upd(j, mono.back());
                }
                mono.pop_back();
            }
            if(!mono.empty()){
                Df[i][j] = mono.back();
                D[i].upd(j, mono.back());
            }
            mono.push_back(i);
        }
    }
    vector<pair<pii, pii>> rect;
    for(int i = 1; i + 1 < n; i ++ ){
        for(int j = 1; j + 1 < m ; j ++ ){
            if(Uf[i][j] == -1 || Lf[i][j] ==-1 || Df[i][j] == -1 || Rf[i][j] == -1) continue;
            rect.push_back({mp(Uf[i][j], Lf[i][j]), mp(Df[i][j], Rf[i][j])});
        }
    }
    sort(rect.begin(), rect.end());
    rect.resize(unique(rect.begin(), rect.end()) - rect.begin());
    int ret = 0;
    for(auto x : rect){
        ret += is_valid(x.fi.fi, x.fi.se, x.se.fi, x.se.se);
    }
    return ret;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 768 KB Output is correct
2 Correct 1 ms 1152 KB Output is correct
3 Correct 1 ms 1152 KB Output is correct
4 Correct 1 ms 1152 KB Output is correct
5 Correct 1 ms 1152 KB Output is correct
6 Correct 1 ms 1152 KB Output is correct
7 Correct 1 ms 1152 KB Output is correct
8 Correct 1 ms 896 KB Output is correct
9 Correct 1 ms 1152 KB Output is correct
10 Correct 1 ms 1152 KB Output is correct
11 Correct 1 ms 1152 KB Output is correct
12 Correct 1 ms 1152 KB Output is correct
13 Correct 1 ms 640 KB Output is correct
14 Correct 1 ms 768 KB Output is correct
15 Correct 1 ms 768 KB Output is correct
16 Correct 1 ms 640 KB Output is correct
17 Correct 1 ms 640 KB Output is correct
18 Correct 1 ms 640 KB Output is correct
19 Correct 1 ms 1152 KB Output is correct
20 Correct 1 ms 1152 KB Output is correct
21 Correct 1 ms 768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 768 KB Output is correct
2 Correct 1 ms 1152 KB Output is correct
3 Correct 1 ms 1152 KB Output is correct
4 Correct 1 ms 1152 KB Output is correct
5 Correct 1 ms 1152 KB Output is correct
6 Correct 1 ms 1152 KB Output is correct
7 Correct 1 ms 1152 KB Output is correct
8 Correct 1 ms 896 KB Output is correct
9 Correct 1 ms 1152 KB Output is correct
10 Correct 1 ms 1152 KB Output is correct
11 Correct 1 ms 1152 KB Output is correct
12 Correct 1 ms 1152 KB Output is correct
13 Correct 1 ms 640 KB Output is correct
14 Correct 1 ms 768 KB Output is correct
15 Correct 1 ms 768 KB Output is correct
16 Correct 1 ms 640 KB Output is correct
17 Correct 3 ms 2304 KB Output is correct
18 Correct 3 ms 2304 KB Output is correct
19 Correct 3 ms 2304 KB Output is correct
20 Correct 3 ms 2176 KB Output is correct
21 Correct 3 ms 2176 KB Output is correct
22 Correct 3 ms 2176 KB Output is correct
23 Correct 4 ms 2304 KB Output is correct
24 Correct 2 ms 2048 KB Output is correct
25 Correct 1 ms 640 KB Output is correct
26 Correct 1 ms 640 KB Output is correct
27 Correct 1 ms 1152 KB Output is correct
28 Correct 1 ms 1152 KB Output is correct
29 Correct 1 ms 768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 768 KB Output is correct
2 Correct 1 ms 1152 KB Output is correct
3 Correct 1 ms 1152 KB Output is correct
4 Correct 1 ms 1152 KB Output is correct
5 Correct 1 ms 1152 KB Output is correct
6 Correct 1 ms 1152 KB Output is correct
7 Correct 1 ms 1152 KB Output is correct
8 Correct 1 ms 896 KB Output is correct
9 Correct 1 ms 1152 KB Output is correct
10 Correct 1 ms 1152 KB Output is correct
11 Correct 1 ms 1152 KB Output is correct
12 Correct 1 ms 1152 KB Output is correct
13 Correct 1 ms 640 KB Output is correct
14 Correct 1 ms 768 KB Output is correct
15 Correct 1 ms 768 KB Output is correct
16 Correct 1 ms 640 KB Output is correct
17 Correct 3 ms 2304 KB Output is correct
18 Correct 3 ms 2304 KB Output is correct
19 Correct 3 ms 2304 KB Output is correct
20 Correct 3 ms 2176 KB Output is correct
21 Correct 3 ms 2176 KB Output is correct
22 Correct 3 ms 2176 KB Output is correct
23 Correct 4 ms 2304 KB Output is correct
24 Correct 2 ms 2048 KB Output is correct
25 Correct 16 ms 5756 KB Output is correct
26 Correct 16 ms 5756 KB Output is correct
27 Correct 16 ms 5756 KB Output is correct
28 Correct 15 ms 5504 KB Output is correct
29 Correct 17 ms 5756 KB Output is correct
30 Correct 17 ms 5756 KB Output is correct
31 Correct 17 ms 5756 KB Output is correct
32 Correct 17 ms 5756 KB Output is correct
33 Correct 1 ms 640 KB Output is correct
34 Correct 1 ms 640 KB Output is correct
35 Correct 1 ms 1152 KB Output is correct
36 Correct 1 ms 1152 KB Output is correct
37 Correct 1 ms 768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 768 KB Output is correct
2 Correct 1 ms 1152 KB Output is correct
3 Correct 1 ms 1152 KB Output is correct
4 Correct 1 ms 1152 KB Output is correct
5 Correct 1 ms 1152 KB Output is correct
6 Correct 1 ms 1152 KB Output is correct
7 Correct 1 ms 1152 KB Output is correct
8 Correct 1 ms 896 KB Output is correct
9 Correct 1 ms 1152 KB Output is correct
10 Correct 1 ms 1152 KB Output is correct
11 Correct 1 ms 1152 KB Output is correct
12 Correct 1 ms 1152 KB Output is correct
13 Correct 1 ms 640 KB Output is correct
14 Correct 1 ms 768 KB Output is correct
15 Correct 1 ms 768 KB Output is correct
16 Correct 1 ms 640 KB Output is correct
17 Correct 3 ms 2304 KB Output is correct
18 Correct 3 ms 2304 KB Output is correct
19 Correct 3 ms 2304 KB Output is correct
20 Correct 3 ms 2176 KB Output is correct
21 Correct 3 ms 2176 KB Output is correct
22 Correct 3 ms 2176 KB Output is correct
23 Correct 4 ms 2304 KB Output is correct
24 Correct 2 ms 2048 KB Output is correct
25 Correct 16 ms 5756 KB Output is correct
26 Correct 16 ms 5756 KB Output is correct
27 Correct 16 ms 5756 KB Output is correct
28 Correct 15 ms 5504 KB Output is correct
29 Correct 17 ms 5756 KB Output is correct
30 Correct 17 ms 5756 KB Output is correct
31 Correct 17 ms 5756 KB Output is correct
32 Correct 17 ms 5756 KB Output is correct
33 Correct 118 ms 25976 KB Output is correct
34 Correct 122 ms 25984 KB Output is correct
35 Correct 136 ms 25976 KB Output is correct
36 Correct 131 ms 25976 KB Output is correct
37 Correct 241 ms 30308 KB Output is correct
38 Correct 249 ms 30188 KB Output is correct
39 Correct 244 ms 30188 KB Output is correct
40 Correct 227 ms 28524 KB Output is correct
41 Correct 207 ms 28332 KB Output is correct
42 Correct 221 ms 30316 KB Output is correct
43 Correct 237 ms 30188 KB Output is correct
44 Correct 241 ms 30348 KB Output is correct
45 Correct 117 ms 21872 KB Output is correct
46 Correct 113 ms 15472 KB Output is correct
47 Correct 242 ms 30320 KB Output is correct
48 Correct 242 ms 30444 KB Output is correct
49 Correct 1 ms 640 KB Output is correct
50 Correct 1 ms 640 KB Output is correct
51 Correct 1 ms 1152 KB Output is correct
52 Correct 1 ms 1152 KB Output is correct
53 Correct 1 ms 768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1024 KB Output is correct
2 Correct 3 ms 1024 KB Output is correct
3 Correct 2 ms 1024 KB Output is correct
4 Correct 1 ms 640 KB Output is correct
5 Correct 2 ms 1024 KB Output is correct
6 Correct 2 ms 1024 KB Output is correct
7 Correct 2 ms 1024 KB Output is correct
8 Correct 2 ms 1024 KB Output is correct
9 Correct 2 ms 1024 KB Output is correct
10 Correct 2 ms 896 KB Output is correct
11 Correct 2 ms 896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 768 KB Output is correct
2 Correct 1683 ms 108900 KB Output is correct
3 Execution timed out 5075 ms 195812 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 768 KB Output is correct
2 Correct 1 ms 1152 KB Output is correct
3 Correct 1 ms 1152 KB Output is correct
4 Correct 1 ms 1152 KB Output is correct
5 Correct 1 ms 1152 KB Output is correct
6 Correct 1 ms 1152 KB Output is correct
7 Correct 1 ms 1152 KB Output is correct
8 Correct 1 ms 896 KB Output is correct
9 Correct 1 ms 1152 KB Output is correct
10 Correct 1 ms 1152 KB Output is correct
11 Correct 1 ms 1152 KB Output is correct
12 Correct 1 ms 1152 KB Output is correct
13 Correct 1 ms 640 KB Output is correct
14 Correct 1 ms 768 KB Output is correct
15 Correct 1 ms 768 KB Output is correct
16 Correct 1 ms 640 KB Output is correct
17 Correct 3 ms 2304 KB Output is correct
18 Correct 3 ms 2304 KB Output is correct
19 Correct 3 ms 2304 KB Output is correct
20 Correct 3 ms 2176 KB Output is correct
21 Correct 3 ms 2176 KB Output is correct
22 Correct 3 ms 2176 KB Output is correct
23 Correct 4 ms 2304 KB Output is correct
24 Correct 2 ms 2048 KB Output is correct
25 Correct 16 ms 5756 KB Output is correct
26 Correct 16 ms 5756 KB Output is correct
27 Correct 16 ms 5756 KB Output is correct
28 Correct 15 ms 5504 KB Output is correct
29 Correct 17 ms 5756 KB Output is correct
30 Correct 17 ms 5756 KB Output is correct
31 Correct 17 ms 5756 KB Output is correct
32 Correct 17 ms 5756 KB Output is correct
33 Correct 118 ms 25976 KB Output is correct
34 Correct 122 ms 25984 KB Output is correct
35 Correct 136 ms 25976 KB Output is correct
36 Correct 131 ms 25976 KB Output is correct
37 Correct 241 ms 30308 KB Output is correct
38 Correct 249 ms 30188 KB Output is correct
39 Correct 244 ms 30188 KB Output is correct
40 Correct 227 ms 28524 KB Output is correct
41 Correct 207 ms 28332 KB Output is correct
42 Correct 221 ms 30316 KB Output is correct
43 Correct 237 ms 30188 KB Output is correct
44 Correct 241 ms 30348 KB Output is correct
45 Correct 117 ms 21872 KB Output is correct
46 Correct 113 ms 15472 KB Output is correct
47 Correct 242 ms 30320 KB Output is correct
48 Correct 242 ms 30444 KB Output is correct
49 Correct 2 ms 1024 KB Output is correct
50 Correct 3 ms 1024 KB Output is correct
51 Correct 2 ms 1024 KB Output is correct
52 Correct 1 ms 640 KB Output is correct
53 Correct 2 ms 1024 KB Output is correct
54 Correct 2 ms 1024 KB Output is correct
55 Correct 2 ms 1024 KB Output is correct
56 Correct 2 ms 1024 KB Output is correct
57 Correct 2 ms 1024 KB Output is correct
58 Correct 2 ms 896 KB Output is correct
59 Correct 2 ms 896 KB Output is correct
60 Correct 1 ms 768 KB Output is correct
61 Correct 1683 ms 108900 KB Output is correct
62 Execution timed out 5075 ms 195812 KB Time limit exceeded
63 Halted 0 ms 0 KB -