Submission #268149

# Submission time Handle Problem Language Result Execution time Memory
268149 2020-08-16T09:31:40 Z doowey Rectangles (IOI19_rect) C++14
72 / 100
5000 ms 654340 KB
#include <bits/stdc++.h>
#include "rect.h"

using namespace std;

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

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

const int N = 2505;
vector<int> I[N][N], J[N][N];

int L[N][N], R[N][N], U[N][N], D[N][N];

bool check(int i1, int j1, int i2, int j2){
    if(i1 == -1 || j1 == -1 || i2 == -1 || j2 == -1) return false;
    int il = lower_bound(I[j1][j2].begin(), I[j1][j2].end(), i1+1) - I[j1][j2].begin();
    int ir = upper_bound(I[j1][j2].begin(), I[j1][j2].end(), i2-1) - I[j1][j2].begin();
    ir -- ;
    if(ir - il + 1 != (i2-1)-(i1+1)+1) return false;
    il = lower_bound(J[i1][i2].begin(), J[i1][i2].end(), j1+1) - J[i1][i2].begin();
    ir = upper_bound(J[i1][i2].begin(), J[i1][i2].end(), j2-1) - J[i1][i2].begin();
    ir -- ;
    if(ir - il + 1 != (j2-1)-(j1+1)+1) return false;
    return true;
}

ll count_rectangles(vector<vector<int>> a){
    int n = a.size();
    int m = a[0].size();
    for(int i = 0 ; i < n; i ++ ){
        vector<int> st;
        for(int j = 0 ; j < m ; j ++ ){
            L[i][j] = -1;
            while(!st.empty() && a[i][st.back()] <= a[i][j]) st.pop_back();
            if(!st.empty()) L[i][j] = st.back();
            st.push_back(j);
        }
        st.clear();
        for(int j = 0 ; j < m ; j ++ ){
            while(!st.empty() && a[i][st.back()] < a[i][j]) st.pop_back();
            if(!st.empty()){
                if(st.back() + 1 < j)I[st.back()][j].push_back(i);
            }
            st.push_back(j);
        }
        st.clear();
        for(int j = m-1 ; j >= 0 ; j -- ){
            R[i][j] = -1;
            while(!st.empty() && a[i][st.back()] <= a[i][j]) st.pop_back();
            if(!st.empty()) R[i][j] = st.back();
            st.push_back(j);
        }
        st.clear();
        for(int j = m - 1; j >= 0 ; j -- ){
            while(!st.empty() && a[i][st.back()] < a[i][j]) st.pop_back();
            if(!st.empty()){
                if(j + 1 < st.back())I[j][st.back()].push_back(i);
            }
            st.push_back(j);
        }
    }
    for(int j = 0 ; j < m ; j ++ ){
        vector<int> st;
        for(int i = 0 ; i < n; i ++ ){
            U[i][j] = -1;
            while(!st.empty() && a[st.back()][j] <= a[i][j]) st.pop_back();
            if(!st.empty()) U[i][j] = st.back();
            st.push_back(i);
        }
        st.clear();
        for(int i = 0 ; i < n; i ++ ){
            while(!st.empty() && a[st.back()][j] < a[i][j]) st.pop_back();
            if(!st.empty()){
                if(st.back() + 1 < i)
                    J[st.back()][i].push_back(j);
            }
            st.push_back(i);
        }
        st.clear();
        for(int i = n - 1; i >= 0 ; i -- ){
            D[i][j] = -1;
            while(!st.empty() && a[st.back()][j] <= a[i][j]) st.pop_back();
            if(!st.empty()) D[i][j] = st.back();
            st.push_back(i);
        }
        st.clear();
        for(int i = n - 1; i >= 0; i -- ){
            while(!st.empty() && a[st.back()][j] < a[i][j]) st.pop_back();
            if(!st.empty())
                if(i + 1 < st.back())
                    J[i][st.back()].push_back(j);
            st.push_back(i);
        }
    }
    for(int i = 0; i < m; i ++ ){
        for(int j = i + 1; j < m ; j ++ ){
            sort(I[i][j].begin(), I[i][j].end());
            I[i][j].resize(unique(I[i][j].begin(), I[i][j].end()) - I[i][j].begin());
        }
    }
    for(int i = 0 ; i < n ; i ++ ){
        for(int j = i + 1; j < n ; j ++ ){
            sort(J[i][j].begin(), J[i][j].end());
            J[i][j].resize(unique(J[i][j].begin(), J[i][j].end()) - J[i][j].begin());
        }
    }
    vector<pair<pii, pii>> sols;
    for(int i = 1 ; i + 1 < n; i ++ ){
        for(int j = 1; j + 1 < m ; j ++ ){
            if(check(U[i][j], L[i][j], D[i][j], R[i][j])){
                sols.push_back({mp(U[i][j], L[i][j]), mp(D[i][j], R[i][j])});
            }
        }
    }
    sort(sols.begin(), sols.end());
    sols.resize(unique(sols.begin(), sols.end()) - sols.begin());
    return sols.size();
}
# Verdict Execution time Memory Grader output
1 Correct 185 ms 295160 KB Output is correct
2 Correct 182 ms 295588 KB Output is correct
3 Correct 207 ms 295544 KB Output is correct
4 Correct 187 ms 295544 KB Output is correct
5 Correct 183 ms 295544 KB Output is correct
6 Correct 182 ms 295544 KB Output is correct
7 Correct 183 ms 295544 KB Output is correct
8 Correct 187 ms 295288 KB Output is correct
9 Correct 186 ms 295544 KB Output is correct
10 Correct 187 ms 295672 KB Output is correct
11 Correct 193 ms 295544 KB Output is correct
12 Correct 190 ms 295544 KB Output is correct
13 Correct 185 ms 295036 KB Output is correct
14 Correct 185 ms 295160 KB Output is correct
15 Correct 191 ms 295292 KB Output is correct
16 Correct 193 ms 295160 KB Output is correct
17 Correct 187 ms 295032 KB Output is correct
18 Correct 186 ms 295032 KB Output is correct
19 Correct 227 ms 295548 KB Output is correct
20 Correct 198 ms 295544 KB Output is correct
21 Correct 182 ms 295160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 185 ms 295160 KB Output is correct
2 Correct 182 ms 295588 KB Output is correct
3 Correct 207 ms 295544 KB Output is correct
4 Correct 187 ms 295544 KB Output is correct
5 Correct 183 ms 295544 KB Output is correct
6 Correct 182 ms 295544 KB Output is correct
7 Correct 183 ms 295544 KB Output is correct
8 Correct 187 ms 295288 KB Output is correct
9 Correct 186 ms 295544 KB Output is correct
10 Correct 187 ms 295672 KB Output is correct
11 Correct 193 ms 295544 KB Output is correct
12 Correct 190 ms 295544 KB Output is correct
13 Correct 185 ms 295036 KB Output is correct
14 Correct 185 ms 295160 KB Output is correct
15 Correct 191 ms 295292 KB Output is correct
16 Correct 193 ms 295160 KB Output is correct
17 Correct 193 ms 296824 KB Output is correct
18 Correct 188 ms 296812 KB Output is correct
19 Correct 189 ms 296772 KB Output is correct
20 Correct 185 ms 296568 KB Output is correct
21 Correct 185 ms 296696 KB Output is correct
22 Correct 185 ms 296764 KB Output is correct
23 Correct 186 ms 296696 KB Output is correct
24 Correct 187 ms 296644 KB Output is correct
25 Correct 187 ms 295032 KB Output is correct
26 Correct 186 ms 295032 KB Output is correct
27 Correct 227 ms 295548 KB Output is correct
28 Correct 198 ms 295544 KB Output is correct
29 Correct 182 ms 295160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 185 ms 295160 KB Output is correct
2 Correct 182 ms 295588 KB Output is correct
3 Correct 207 ms 295544 KB Output is correct
4 Correct 187 ms 295544 KB Output is correct
5 Correct 183 ms 295544 KB Output is correct
6 Correct 182 ms 295544 KB Output is correct
7 Correct 183 ms 295544 KB Output is correct
8 Correct 187 ms 295288 KB Output is correct
9 Correct 186 ms 295544 KB Output is correct
10 Correct 187 ms 295672 KB Output is correct
11 Correct 193 ms 295544 KB Output is correct
12 Correct 190 ms 295544 KB Output is correct
13 Correct 185 ms 295036 KB Output is correct
14 Correct 185 ms 295160 KB Output is correct
15 Correct 191 ms 295292 KB Output is correct
16 Correct 193 ms 295160 KB Output is correct
17 Correct 193 ms 296824 KB Output is correct
18 Correct 188 ms 296812 KB Output is correct
19 Correct 189 ms 296772 KB Output is correct
20 Correct 185 ms 296568 KB Output is correct
21 Correct 185 ms 296696 KB Output is correct
22 Correct 185 ms 296764 KB Output is correct
23 Correct 186 ms 296696 KB Output is correct
24 Correct 187 ms 296644 KB Output is correct
25 Correct 196 ms 301044 KB Output is correct
26 Correct 198 ms 301044 KB Output is correct
27 Correct 198 ms 301044 KB Output is correct
28 Correct 204 ms 299816 KB Output is correct
29 Correct 206 ms 300536 KB Output is correct
30 Correct 210 ms 300536 KB Output is correct
31 Correct 202 ms 300660 KB Output is correct
32 Correct 201 ms 300408 KB Output is correct
33 Correct 187 ms 295032 KB Output is correct
34 Correct 186 ms 295032 KB Output is correct
35 Correct 227 ms 295548 KB Output is correct
36 Correct 198 ms 295544 KB Output is correct
37 Correct 182 ms 295160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 185 ms 295160 KB Output is correct
2 Correct 182 ms 295588 KB Output is correct
3 Correct 207 ms 295544 KB Output is correct
4 Correct 187 ms 295544 KB Output is correct
5 Correct 183 ms 295544 KB Output is correct
6 Correct 182 ms 295544 KB Output is correct
7 Correct 183 ms 295544 KB Output is correct
8 Correct 187 ms 295288 KB Output is correct
9 Correct 186 ms 295544 KB Output is correct
10 Correct 187 ms 295672 KB Output is correct
11 Correct 193 ms 295544 KB Output is correct
12 Correct 190 ms 295544 KB Output is correct
13 Correct 185 ms 295036 KB Output is correct
14 Correct 185 ms 295160 KB Output is correct
15 Correct 191 ms 295292 KB Output is correct
16 Correct 193 ms 295160 KB Output is correct
17 Correct 193 ms 296824 KB Output is correct
18 Correct 188 ms 296812 KB Output is correct
19 Correct 189 ms 296772 KB Output is correct
20 Correct 185 ms 296568 KB Output is correct
21 Correct 185 ms 296696 KB Output is correct
22 Correct 185 ms 296764 KB Output is correct
23 Correct 186 ms 296696 KB Output is correct
24 Correct 187 ms 296644 KB Output is correct
25 Correct 196 ms 301044 KB Output is correct
26 Correct 198 ms 301044 KB Output is correct
27 Correct 198 ms 301044 KB Output is correct
28 Correct 204 ms 299816 KB Output is correct
29 Correct 206 ms 300536 KB Output is correct
30 Correct 210 ms 300536 KB Output is correct
31 Correct 202 ms 300660 KB Output is correct
32 Correct 201 ms 300408 KB Output is correct
33 Correct 294 ms 336376 KB Output is correct
34 Correct 289 ms 336252 KB Output is correct
35 Correct 282 ms 336248 KB Output is correct
36 Correct 290 ms 336296 KB Output is correct
37 Correct 406 ms 335164 KB Output is correct
38 Correct 426 ms 335224 KB Output is correct
39 Correct 411 ms 335452 KB Output is correct
40 Correct 382 ms 333648 KB Output is correct
41 Correct 326 ms 322548 KB Output is correct
42 Correct 343 ms 323580 KB Output is correct
43 Correct 440 ms 330456 KB Output is correct
44 Correct 459 ms 332396 KB Output is correct
45 Correct 310 ms 319600 KB Output is correct
46 Correct 305 ms 313940 KB Output is correct
47 Correct 456 ms 330208 KB Output is correct
48 Correct 464 ms 331236 KB Output is correct
49 Correct 187 ms 295032 KB Output is correct
50 Correct 186 ms 295032 KB Output is correct
51 Correct 227 ms 295548 KB Output is correct
52 Correct 198 ms 295544 KB Output is correct
53 Correct 182 ms 295160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 201 ms 295416 KB Output is correct
2 Correct 197 ms 295372 KB Output is correct
3 Correct 209 ms 295292 KB Output is correct
4 Correct 200 ms 295036 KB Output is correct
5 Correct 211 ms 295416 KB Output is correct
6 Correct 256 ms 295544 KB Output is correct
7 Correct 214 ms 295584 KB Output is correct
8 Correct 235 ms 295544 KB Output is correct
9 Correct 222 ms 295544 KB Output is correct
10 Correct 197 ms 295160 KB Output is correct
11 Correct 207 ms 295544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 201 ms 295160 KB Output is correct
2 Correct 1126 ms 389788 KB Output is correct
3 Correct 2308 ms 495848 KB Output is correct
4 Correct 2333 ms 496684 KB Output is correct
5 Correct 2425 ms 496788 KB Output is correct
6 Correct 630 ms 373752 KB Output is correct
7 Correct 982 ms 444448 KB Output is correct
8 Correct 985 ms 447272 KB Output is correct
9 Correct 187 ms 295032 KB Output is correct
10 Correct 186 ms 295032 KB Output is correct
11 Correct 227 ms 295548 KB Output is correct
12 Correct 198 ms 295544 KB Output is correct
13 Correct 182 ms 295160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 185 ms 295160 KB Output is correct
2 Correct 182 ms 295588 KB Output is correct
3 Correct 207 ms 295544 KB Output is correct
4 Correct 187 ms 295544 KB Output is correct
5 Correct 183 ms 295544 KB Output is correct
6 Correct 182 ms 295544 KB Output is correct
7 Correct 183 ms 295544 KB Output is correct
8 Correct 187 ms 295288 KB Output is correct
9 Correct 186 ms 295544 KB Output is correct
10 Correct 187 ms 295672 KB Output is correct
11 Correct 193 ms 295544 KB Output is correct
12 Correct 190 ms 295544 KB Output is correct
13 Correct 185 ms 295036 KB Output is correct
14 Correct 185 ms 295160 KB Output is correct
15 Correct 191 ms 295292 KB Output is correct
16 Correct 193 ms 295160 KB Output is correct
17 Correct 193 ms 296824 KB Output is correct
18 Correct 188 ms 296812 KB Output is correct
19 Correct 189 ms 296772 KB Output is correct
20 Correct 185 ms 296568 KB Output is correct
21 Correct 185 ms 296696 KB Output is correct
22 Correct 185 ms 296764 KB Output is correct
23 Correct 186 ms 296696 KB Output is correct
24 Correct 187 ms 296644 KB Output is correct
25 Correct 196 ms 301044 KB Output is correct
26 Correct 198 ms 301044 KB Output is correct
27 Correct 198 ms 301044 KB Output is correct
28 Correct 204 ms 299816 KB Output is correct
29 Correct 206 ms 300536 KB Output is correct
30 Correct 210 ms 300536 KB Output is correct
31 Correct 202 ms 300660 KB Output is correct
32 Correct 201 ms 300408 KB Output is correct
33 Correct 294 ms 336376 KB Output is correct
34 Correct 289 ms 336252 KB Output is correct
35 Correct 282 ms 336248 KB Output is correct
36 Correct 290 ms 336296 KB Output is correct
37 Correct 406 ms 335164 KB Output is correct
38 Correct 426 ms 335224 KB Output is correct
39 Correct 411 ms 335452 KB Output is correct
40 Correct 382 ms 333648 KB Output is correct
41 Correct 326 ms 322548 KB Output is correct
42 Correct 343 ms 323580 KB Output is correct
43 Correct 440 ms 330456 KB Output is correct
44 Correct 459 ms 332396 KB Output is correct
45 Correct 310 ms 319600 KB Output is correct
46 Correct 305 ms 313940 KB Output is correct
47 Correct 456 ms 330208 KB Output is correct
48 Correct 464 ms 331236 KB Output is correct
49 Correct 201 ms 295416 KB Output is correct
50 Correct 197 ms 295372 KB Output is correct
51 Correct 209 ms 295292 KB Output is correct
52 Correct 200 ms 295036 KB Output is correct
53 Correct 211 ms 295416 KB Output is correct
54 Correct 256 ms 295544 KB Output is correct
55 Correct 214 ms 295584 KB Output is correct
56 Correct 235 ms 295544 KB Output is correct
57 Correct 222 ms 295544 KB Output is correct
58 Correct 197 ms 295160 KB Output is correct
59 Correct 207 ms 295544 KB Output is correct
60 Correct 201 ms 295160 KB Output is correct
61 Correct 1126 ms 389788 KB Output is correct
62 Correct 2308 ms 495848 KB Output is correct
63 Correct 2333 ms 496684 KB Output is correct
64 Correct 2425 ms 496788 KB Output is correct
65 Correct 630 ms 373752 KB Output is correct
66 Correct 982 ms 444448 KB Output is correct
67 Correct 985 ms 447272 KB Output is correct
68 Correct 2106 ms 643400 KB Output is correct
69 Correct 1997 ms 644556 KB Output is correct
70 Correct 2136 ms 644380 KB Output is correct
71 Correct 1775 ms 644580 KB Output is correct
72 Correct 4283 ms 654340 KB Output is correct
73 Correct 2917 ms 467804 KB Output is correct
74 Correct 2948 ms 516672 KB Output is correct
75 Correct 4929 ms 588660 KB Output is correct
76 Correct 3046 ms 470632 KB Output is correct
77 Correct 3707 ms 539076 KB Output is correct
78 Execution timed out 5074 ms 596344 KB Time limit exceeded
79 Halted 0 ms 0 KB -