Submission #925557

# Submission time Handle Problem Language Result Execution time Memory
925557 2024-02-12T04:23:11 Z hmm789 Rectangles (IOI19_rect) C++14
72 / 100
2422 ms 1048576 KB
#include <bits/stdc++.h>
using namespace std;
//#define int long long
#define INF 1000000000000000000
#define MOD 998244353

int fw[2505];

void update(int x, int v) {
    for(x++; x < 2505; x += x&-x) fw[x] += v;
}

void update(int x, int y, int v) {
    update(x, v); update(y+1, -v);
}

int query(int x) {
    int ans = 0;
    for(x++; x; x -= x&-x) ans += fw[x];
    return ans;
}

long long count_rectangles(vector<vector<int>> a) {
    int m = a.size(), n = a[0].size();
    vector<int> toth[n][n], totv[m][m];
    vector<pair<int, int>> goodh[m][n], goodv[m][n];
    for(int i = 1; i < m-1; i++) {
        stack<int> s;
        int l[n], r[n];
        for(int j = 0; j < n; j++) {
            while(s.size() && a[i][s.top()] <= a[i][j]) s.pop();
            if(s.size()) l[j] = s.top();
            else l[j] = -1;
            s.push(j);
        }
        while(s.size()) s.pop();
        for(int j = n-1; j >= 0; j--) {
            while(s.size() && a[i][s.top()] <= a[i][j]) s.pop();
            if(s.size()) r[j] = s.top();
            else r[j] = -1;
            s.push(j);
        }
        for(int j = 0; j < n; j++) if(l[j] != -1 && r[j] != -1) toth[l[j]][r[j]].push_back(i);
    }
    for(int i = 0; i < n; i++) {
        for(int j = i+2; j < n; j++) {
            if(!toth[i][j].size()) continue;
            int prv = -1;
            toth[i][j].erase(unique(toth[i][j].begin(), toth[i][j].end()), toth[i][j].end());
            for(int k = 1; k < toth[i][j].size(); k++) {
                if(toth[i][j][k]-toth[i][j][k-1] > 1) {
                    while(prv < k-1) {
                        goodh[toth[i][j][prv+1]][i+1].push_back({toth[i][j][k-1], j-1});
                        prv++;
                    }
                }
            }
            while(prv < (int)toth[i][j].size()-1) {
                goodh[toth[i][j][prv+1]][i+1].push_back({toth[i][j][toth[i][j].size()-1], j-1});
                prv++;
            }
        }
    }
    for(int i = 1; i < n-1; i++) {
        stack<int> s;
        int l[m], r[m];
        for(int j = 0; j < m; j++) {
            while(s.size() && a[s.top()][i] <= a[j][i]) s.pop();
            if(s.size()) l[j] = s.top();
            else l[j] = -1;
            s.push(j);
        }
        while(s.size()) s.pop();
        for(int j = m-1; j >= 0; j--) {
            while(s.size() && a[s.top()][i] <= a[j][i]) s.pop();
            if(s.size()) r[j] = s.top();
            else r[j] = -1;
            s.push(j);
        }
        for(int j = 0; j < m; j++) if(l[j] != -1 && r[j] != -1) totv[l[j]][r[j]].push_back(i);
    }
    for(int i = 0; i < m; i++) {
        for(int j = i+2; j < m; j++) {
            if(!totv[i][j].size()) continue;
            int prv = -1;
            totv[i][j].erase(unique(totv[i][j].begin(), totv[i][j].end()), totv[i][j].end());
            for(int k = 1; k < totv[i][j].size(); k++) {
                if(totv[i][j][k]-totv[i][j][k-1] > 1) {
                    while(prv < k-1) {
                        goodv[i+1][totv[i][j][prv+1]].push_back({j-1, totv[i][j][k-1]});
                        prv++;
                    }
                }
            }
            while(prv < (int)totv[i][j].size()-1) {
                goodv[i+1][totv[i][j][prv+1]].push_back({j-1, totv[i][j][totv[i][j].size()-1]});
                prv++;
            }
        }
    }
    long long ans = 0;
    for(int i = 1; i < m-1; i++) {
        for(int j = 1; j < n-1; j++) {
            sort(goodh[i][j].begin(), goodh[i][j].end());
            sort(goodv[i][j].begin(), goodv[i][j].end());
            int idx = 0;
            for(auto k : goodh[i][j]) {
                while(idx < goodv[i][j].size() && goodv[i][j][idx].first <= k.first) {
                    update(j, goodv[i][j][idx++].second, 1);
                }
                ans += query(k.second);
            }
            for(int k = 0; k < idx; k++) update(j, goodv[i][j][k].second, -1);
        }
    }
    return ans;
}

Compilation message

rect.cpp: In function 'long long int count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:50:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   50 |             for(int k = 1; k < toth[i][j].size(); k++) {
      |                            ~~^~~~~~~~~~~~~~~~~~~
rect.cpp:87:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   87 |             for(int k = 1; k < totv[i][j].size(); k++) {
      |                            ~~^~~~~~~~~~~~~~~~~~~
rect.cpp:108:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  108 |                 while(idx < goodv[i][j].size() && goodv[i][j][idx].first <= k.first) {
      |                       ~~~~^~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 604 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 344 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 604 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 344 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 2 ms 1368 KB Output is correct
23 Correct 2 ms 1372 KB Output is correct
24 Correct 2 ms 1372 KB Output is correct
25 Correct 2 ms 1216 KB Output is correct
26 Correct 3 ms 1372 KB Output is correct
27 Correct 3 ms 1372 KB Output is correct
28 Correct 2 ms 1372 KB Output is correct
29 Correct 1 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 604 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 2 ms 1368 KB Output is correct
18 Correct 2 ms 1372 KB Output is correct
19 Correct 2 ms 1372 KB Output is correct
20 Correct 2 ms 1216 KB Output is correct
21 Correct 3 ms 1372 KB Output is correct
22 Correct 3 ms 1372 KB Output is correct
23 Correct 2 ms 1372 KB Output is correct
24 Correct 1 ms 860 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 344 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 11 ms 7512 KB Output is correct
31 Correct 10 ms 7516 KB Output is correct
32 Correct 11 ms 7516 KB Output is correct
33 Correct 9 ms 5720 KB Output is correct
34 Correct 14 ms 6492 KB Output is correct
35 Correct 16 ms 6792 KB Output is correct
36 Correct 13 ms 6492 KB Output is correct
37 Correct 15 ms 6492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 604 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 2 ms 1368 KB Output is correct
18 Correct 2 ms 1372 KB Output is correct
19 Correct 2 ms 1372 KB Output is correct
20 Correct 2 ms 1216 KB Output is correct
21 Correct 3 ms 1372 KB Output is correct
22 Correct 3 ms 1372 KB Output is correct
23 Correct 2 ms 1372 KB Output is correct
24 Correct 1 ms 860 KB Output is correct
25 Correct 11 ms 7512 KB Output is correct
26 Correct 10 ms 7516 KB Output is correct
27 Correct 11 ms 7516 KB Output is correct
28 Correct 9 ms 5720 KB Output is correct
29 Correct 14 ms 6492 KB Output is correct
30 Correct 16 ms 6792 KB Output is correct
31 Correct 13 ms 6492 KB Output is correct
32 Correct 15 ms 6492 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 344 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 119 ms 83932 KB Output is correct
39 Correct 106 ms 79188 KB Output is correct
40 Correct 117 ms 79360 KB Output is correct
41 Correct 92 ms 74280 KB Output is correct
42 Correct 134 ms 89680 KB Output is correct
43 Correct 132 ms 89428 KB Output is correct
44 Correct 135 ms 90012 KB Output is correct
45 Correct 128 ms 84516 KB Output is correct
46 Correct 103 ms 61780 KB Output is correct
47 Correct 123 ms 65324 KB Output is correct
48 Correct 197 ms 76436 KB Output is correct
49 Correct 208 ms 78420 KB Output is correct
50 Correct 99 ms 42448 KB Output is correct
51 Correct 99 ms 42324 KB Output is correct
52 Correct 186 ms 75372 KB Output is correct
53 Correct 194 ms 76436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 109 ms 148080 KB Output is correct
2 Correct 73 ms 106832 KB Output is correct
3 Correct 101 ms 147616 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 101 ms 147836 KB Output is correct
6 Correct 102 ms 147792 KB Output is correct
7 Correct 101 ms 147720 KB Output is correct
8 Correct 100 ms 147796 KB Output is correct
9 Correct 102 ms 147704 KB Output is correct
10 Correct 104 ms 147552 KB Output is correct
11 Correct 97 ms 147280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 711 ms 387612 KB Output is correct
8 Correct 1587 ms 780368 KB Output is correct
9 Correct 1520 ms 784268 KB Output is correct
10 Correct 1542 ms 784604 KB Output is correct
11 Correct 318 ms 358128 KB Output is correct
12 Correct 577 ms 609164 KB Output is correct
13 Correct 617 ms 648876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 604 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 2 ms 1368 KB Output is correct
18 Correct 2 ms 1372 KB Output is correct
19 Correct 2 ms 1372 KB Output is correct
20 Correct 2 ms 1216 KB Output is correct
21 Correct 3 ms 1372 KB Output is correct
22 Correct 3 ms 1372 KB Output is correct
23 Correct 2 ms 1372 KB Output is correct
24 Correct 1 ms 860 KB Output is correct
25 Correct 11 ms 7512 KB Output is correct
26 Correct 10 ms 7516 KB Output is correct
27 Correct 11 ms 7516 KB Output is correct
28 Correct 9 ms 5720 KB Output is correct
29 Correct 14 ms 6492 KB Output is correct
30 Correct 16 ms 6792 KB Output is correct
31 Correct 13 ms 6492 KB Output is correct
32 Correct 15 ms 6492 KB Output is correct
33 Correct 119 ms 83932 KB Output is correct
34 Correct 106 ms 79188 KB Output is correct
35 Correct 117 ms 79360 KB Output is correct
36 Correct 92 ms 74280 KB Output is correct
37 Correct 134 ms 89680 KB Output is correct
38 Correct 132 ms 89428 KB Output is correct
39 Correct 135 ms 90012 KB Output is correct
40 Correct 128 ms 84516 KB Output is correct
41 Correct 103 ms 61780 KB Output is correct
42 Correct 123 ms 65324 KB Output is correct
43 Correct 197 ms 76436 KB Output is correct
44 Correct 208 ms 78420 KB Output is correct
45 Correct 99 ms 42448 KB Output is correct
46 Correct 99 ms 42324 KB Output is correct
47 Correct 186 ms 75372 KB Output is correct
48 Correct 194 ms 76436 KB Output is correct
49 Correct 109 ms 148080 KB Output is correct
50 Correct 73 ms 106832 KB Output is correct
51 Correct 101 ms 147616 KB Output is correct
52 Correct 0 ms 344 KB Output is correct
53 Correct 101 ms 147836 KB Output is correct
54 Correct 102 ms 147792 KB Output is correct
55 Correct 101 ms 147720 KB Output is correct
56 Correct 100 ms 147796 KB Output is correct
57 Correct 102 ms 147704 KB Output is correct
58 Correct 104 ms 147552 KB Output is correct
59 Correct 97 ms 147280 KB Output is correct
60 Correct 0 ms 344 KB Output is correct
61 Correct 711 ms 387612 KB Output is correct
62 Correct 1587 ms 780368 KB Output is correct
63 Correct 1520 ms 784268 KB Output is correct
64 Correct 1542 ms 784604 KB Output is correct
65 Correct 318 ms 358128 KB Output is correct
66 Correct 577 ms 609164 KB Output is correct
67 Correct 617 ms 648876 KB Output is correct
68 Correct 0 ms 348 KB Output is correct
69 Correct 1 ms 348 KB Output is correct
70 Correct 0 ms 348 KB Output is correct
71 Correct 0 ms 344 KB Output is correct
72 Correct 0 ms 348 KB Output is correct
73 Correct 2422 ms 1048576 KB Output is correct
74 Correct 1802 ms 1007112 KB Output is correct
75 Correct 2123 ms 1007576 KB Output is correct
76 Correct 1271 ms 940420 KB Output is correct
77 Runtime error 1627 ms 1048576 KB Execution killed with signal 9
78 Halted 0 ms 0 KB -