Submission #322161

# Submission time Handle Problem Language Result Execution time Memory
322161 2020-11-14T07:19:09 Z 12tqian Rectangles (IOI19_rect) C++17
72 / 100
5000 ms 901984 KB
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")

#include "rect.h"
#include<bits/stdc++.h>
using namespace std;
 
#define eb emplace_back
#define pb push_back
#define sz(x) int(x.size())
#define all(x) (x).begin(), (x).end()
 
template<class T> struct BIT {
    vector<T> bit;
    int n;
    
    void init(int n_) {
        n = n_;
        bit.resize(n);
    }
    
    T sum(int r) {
        T ret = 0;
        for (; r >= 0; r = (r & (r + 1)) - 1) ret += bit[r];
        return ret;
    }
    
    T sum(int l, int r) {
        return sum(r) - sum(l - 1);
    }
    
    void add(int idx, T delta) {
        for (; idx < n; idx = idx | (idx + 1)) bit[idx] += delta;
    }
};
 
struct seg { 
    short t;
    short l, r;
    short x, y;
    seg(short t_, short l_, short r_, short x_, short y_) {
        t = t_, l = l_, r = r_, x = x_, y = y_;
    }
};
 
long long count_rectangles(std::vector<std::vector<int> > a) {
    int n = (int) a.size();
    int m = (int) a[0].size();
    vector<vector<pair<short, short>>> rows(n);
    vector<vector<pair<short, short>>> cols(m);
    for (int i = 0; i < n; i++) {
        vector<int> stk;
        for (int j = 0; j < m; j++) {
            while (sz(stk) && a[i][stk.back()] < a[i][j]) {
                if (stk.back() + 2 <= j) 
                    rows[i].emplace_back(stk.back(), j);
                stk.pop_back();
            }
            if (sz(stk) && stk.back() + 2 <= j) 
                rows[i].emplace_back(stk.back(), j);    
            stk.push_back(j);
            while (sz(stk) >= 2 && a[i][stk[sz(stk) - 1]] == a[i][stk[sz(stk) - 2]]) {
                swap(stk[sz(stk) - 1], stk[sz(stk) - 2]);
                stk.pop_back();
            }
        }
    }
    for (int j = 0; j < m; j++) {
        vector<int> stk;
        for (int i = 0; i < n; i++) {
            while (sz(stk) && a[stk.back()][j] < a[i][j]) {
                if (stk.back() + 2 <= i) 
                    cols[j].emplace_back(stk.back(), i);
                stk.pop_back();
            }
            if (sz(stk) && stk.back() + 2 <= i) 
                cols[j].emplace_back(stk.back(), i);
            stk.push_back(i);
            while (sz(stk) >= 2 && a[stk[sz(stk) - 1]][j] == a[stk[sz(stk) - 2]][j]) {
                swap(stk[sz(stk) - 1], stk[sz(stk) - 2]);
                stk.pop_back();
            }
        }
    }
    vector<vector<vector<seg>>> vert_corner(n + 5, vector<vector<seg>>(m + 5));
    vector<vector<pair<short, short>>> vert(m, vector<pair<short, short>>(m, {(short) -1, (short) -1}));
    vector<seg> tot;
    for (int i = 0; i < n; i++) {
        for (auto bar : rows[i]) {
            if (vert[bar.first][bar.second] == make_pair((short) -1, (short) -1)) {
                vert[bar.first][bar.second] = {i, i};
            } else {
                if (vert[bar.first][bar.second].second + 1 == i) {
                    vert[bar.first][bar.second].second++;
                } else {
                    int lo = vert[bar.first][bar.second].first;
                    int hi = vert[bar.first][bar.second].second;
                    for (int z = lo - 1; z <= hi + 1; z++) 
                        vert_corner[z + 1][bar.first + 1].emplace_back(1, bar.first + 1, bar.second + 1, lo, hi + 2);
                    vert[bar.first][bar.second] = {i, i};
                }
            }
        }
        if (i == n - 1) {
            for (int x = 0; x < m; x++) {
                for (int y = 0; y < m; y++) {
                    if (vert[x][y] == make_pair((short) -1, (short) -1)) continue;
                    int lo = vert[x][y].first; 
                    int hi = vert[x][y].second;
                    for (int z = lo - 1; z <= hi + 1; z ++) 
                        vert_corner[z + 1][x + 1].emplace_back(1, x + 1, y + 1, lo, hi + 2);
                }
            }
        }
    }
    vector<vector<vector<seg>>> hori_corner(n + 5, vector<vector<seg>>(m + 5));
    vector<vector<pair<short, short>>> hori(n, vector<pair<short, short>>(n, {(short) -1, (short) -1}));
    for (int j = 0; j < m; j++) {
        for (auto bar : cols[j]) {
            if (hori[bar.first][bar.second] == make_pair((short) -1, (short) -1)) {
                hori[bar.first][bar.second] = {j, j};
            } else {
                if (hori[bar.first][bar.second].second + 1 == j) {
                    hori[bar.first][bar.second].second++;
                } else {
                    int lo = hori[bar.first][bar.second].first;
                    int hi = hori[bar.first][bar.second].second;
                    for (int z = lo - 1; z <= hi + 1; z++) 
                        hori_corner[bar.first + 1][z + 1].emplace_back(0, bar.first + 1, bar.second + 1, lo, hi + 2);
                    hori[bar.first][bar.second] = {j, j};
                }
            }
        }
        if (j == m - 1) {
            for (int x = 0; x < n; x++) {
                for (int y = 0; y < n; y++) {
                    if (hori[x][y] == make_pair((short) -1, (short) -1)) continue;
                    int lo = hori[x][y].first; 
                    int hi = hori[x][y].second;
                    for (int z = lo - 1; z <= hi + 1; z++) 
                        hori_corner[x + 1][z + 1].emplace_back(0, x + 1, y + 1, lo, hi + 2);           
                }
            }
        }
    }   
    long long ans = 0;
    BIT<long long> B;
    B.init(n + 5);
    for (int i = 0; i < n + 5; i++) {
        for (int j = 0; j < m + 5; j++) {
            vector<array<int, 3>> segs;
            for (auto s : hori_corner[i][j]) {
                B.add(s.r, 1);
                segs.push_back({s.y, 1, s.r});
            }
            for (auto s : vert_corner[i][j]) 
                segs.push_back({s.r, 0, s.y});
            sort(segs.begin(), segs.end());
            for (auto s : segs) {
                if (s[1] == 1) {
                    B.add(s[2], -1);
                } else {
                    ans += B.sum(0, s[2]);
                }
            }
        }
    }
	return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 492 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 2 ms 492 KB Output is correct
10 Correct 1 ms 492 KB Output is correct
11 Correct 1 ms 492 KB Output is correct
12 Correct 1 ms 492 KB Output is correct
13 Correct 0 ms 364 KB Output is correct
14 Correct 0 ms 364 KB Output is correct
15 Correct 0 ms 364 KB Output is correct
16 Correct 0 ms 364 KB Output is correct
17 Correct 0 ms 364 KB Output is correct
18 Correct 0 ms 364 KB Output is correct
19 Correct 1 ms 364 KB Output is correct
20 Correct 1 ms 364 KB Output is correct
21 Correct 0 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 492 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 2 ms 492 KB Output is correct
10 Correct 1 ms 492 KB Output is correct
11 Correct 1 ms 492 KB Output is correct
12 Correct 1 ms 492 KB Output is correct
13 Correct 0 ms 364 KB Output is correct
14 Correct 0 ms 364 KB Output is correct
15 Correct 0 ms 364 KB Output is correct
16 Correct 0 ms 364 KB Output is correct
17 Correct 3 ms 1260 KB Output is correct
18 Correct 3 ms 1260 KB Output is correct
19 Correct 3 ms 1260 KB Output is correct
20 Correct 3 ms 1132 KB Output is correct
21 Correct 6 ms 1388 KB Output is correct
22 Correct 6 ms 1388 KB Output is correct
23 Correct 6 ms 1388 KB Output is correct
24 Correct 3 ms 748 KB Output is correct
25 Correct 0 ms 364 KB Output is correct
26 Correct 0 ms 364 KB Output is correct
27 Correct 1 ms 364 KB Output is correct
28 Correct 1 ms 364 KB Output is correct
29 Correct 0 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 492 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 2 ms 492 KB Output is correct
10 Correct 1 ms 492 KB Output is correct
11 Correct 1 ms 492 KB Output is correct
12 Correct 1 ms 492 KB Output is correct
13 Correct 0 ms 364 KB Output is correct
14 Correct 0 ms 364 KB Output is correct
15 Correct 0 ms 364 KB Output is correct
16 Correct 0 ms 364 KB Output is correct
17 Correct 3 ms 1260 KB Output is correct
18 Correct 3 ms 1260 KB Output is correct
19 Correct 3 ms 1260 KB Output is correct
20 Correct 3 ms 1132 KB Output is correct
21 Correct 6 ms 1388 KB Output is correct
22 Correct 6 ms 1388 KB Output is correct
23 Correct 6 ms 1388 KB Output is correct
24 Correct 3 ms 748 KB Output is correct
25 Correct 15 ms 5868 KB Output is correct
26 Correct 15 ms 5868 KB Output is correct
27 Correct 18 ms 5868 KB Output is correct
28 Correct 20 ms 4972 KB Output is correct
29 Correct 37 ms 6764 KB Output is correct
30 Correct 40 ms 6764 KB Output is correct
31 Correct 33 ms 6252 KB Output is correct
32 Correct 34 ms 6252 KB Output is correct
33 Correct 0 ms 364 KB Output is correct
34 Correct 0 ms 364 KB Output is correct
35 Correct 1 ms 364 KB Output is correct
36 Correct 1 ms 364 KB Output is correct
37 Correct 0 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 492 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 2 ms 492 KB Output is correct
10 Correct 1 ms 492 KB Output is correct
11 Correct 1 ms 492 KB Output is correct
12 Correct 1 ms 492 KB Output is correct
13 Correct 0 ms 364 KB Output is correct
14 Correct 0 ms 364 KB Output is correct
15 Correct 0 ms 364 KB Output is correct
16 Correct 0 ms 364 KB Output is correct
17 Correct 3 ms 1260 KB Output is correct
18 Correct 3 ms 1260 KB Output is correct
19 Correct 3 ms 1260 KB Output is correct
20 Correct 3 ms 1132 KB Output is correct
21 Correct 6 ms 1388 KB Output is correct
22 Correct 6 ms 1388 KB Output is correct
23 Correct 6 ms 1388 KB Output is correct
24 Correct 3 ms 748 KB Output is correct
25 Correct 15 ms 5868 KB Output is correct
26 Correct 15 ms 5868 KB Output is correct
27 Correct 18 ms 5868 KB Output is correct
28 Correct 20 ms 4972 KB Output is correct
29 Correct 37 ms 6764 KB Output is correct
30 Correct 40 ms 6764 KB Output is correct
31 Correct 33 ms 6252 KB Output is correct
32 Correct 34 ms 6252 KB Output is correct
33 Correct 216 ms 57372 KB Output is correct
34 Correct 196 ms 56556 KB Output is correct
35 Correct 204 ms 56556 KB Output is correct
36 Correct 164 ms 58988 KB Output is correct
37 Correct 208 ms 71020 KB Output is correct
38 Correct 214 ms 71020 KB Output is correct
39 Correct 205 ms 71404 KB Output is correct
40 Correct 202 ms 67052 KB Output is correct
41 Correct 201 ms 51820 KB Output is correct
42 Correct 289 ms 57816 KB Output is correct
43 Correct 619 ms 81516 KB Output is correct
44 Correct 618 ms 83948 KB Output is correct
45 Correct 292 ms 42604 KB Output is correct
46 Correct 292 ms 42604 KB Output is correct
47 Correct 504 ms 75384 KB Output is correct
48 Correct 497 ms 76524 KB Output is correct
49 Correct 0 ms 364 KB Output is correct
50 Correct 0 ms 364 KB Output is correct
51 Correct 1 ms 364 KB Output is correct
52 Correct 1 ms 364 KB Output is correct
53 Correct 0 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 26476 KB Output is correct
2 Correct 28 ms 19436 KB Output is correct
3 Correct 38 ms 25964 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 40 ms 26348 KB Output is correct
6 Correct 40 ms 26348 KB Output is correct
7 Correct 40 ms 26348 KB Output is correct
8 Correct 39 ms 26348 KB Output is correct
9 Correct 39 ms 26496 KB Output is correct
10 Correct 37 ms 25836 KB Output is correct
11 Correct 38 ms 26092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1448 ms 299500 KB Output is correct
3 Correct 3298 ms 638132 KB Output is correct
4 Correct 3322 ms 640796 KB Output is correct
5 Correct 3369 ms 641108 KB Output is correct
6 Correct 257 ms 201080 KB Output is correct
7 Correct 523 ms 369364 KB Output is correct
8 Correct 566 ms 393580 KB Output is correct
9 Correct 0 ms 364 KB Output is correct
10 Correct 0 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 0 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 492 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 2 ms 492 KB Output is correct
10 Correct 1 ms 492 KB Output is correct
11 Correct 1 ms 492 KB Output is correct
12 Correct 1 ms 492 KB Output is correct
13 Correct 0 ms 364 KB Output is correct
14 Correct 0 ms 364 KB Output is correct
15 Correct 0 ms 364 KB Output is correct
16 Correct 0 ms 364 KB Output is correct
17 Correct 3 ms 1260 KB Output is correct
18 Correct 3 ms 1260 KB Output is correct
19 Correct 3 ms 1260 KB Output is correct
20 Correct 3 ms 1132 KB Output is correct
21 Correct 6 ms 1388 KB Output is correct
22 Correct 6 ms 1388 KB Output is correct
23 Correct 6 ms 1388 KB Output is correct
24 Correct 3 ms 748 KB Output is correct
25 Correct 15 ms 5868 KB Output is correct
26 Correct 15 ms 5868 KB Output is correct
27 Correct 18 ms 5868 KB Output is correct
28 Correct 20 ms 4972 KB Output is correct
29 Correct 37 ms 6764 KB Output is correct
30 Correct 40 ms 6764 KB Output is correct
31 Correct 33 ms 6252 KB Output is correct
32 Correct 34 ms 6252 KB Output is correct
33 Correct 216 ms 57372 KB Output is correct
34 Correct 196 ms 56556 KB Output is correct
35 Correct 204 ms 56556 KB Output is correct
36 Correct 164 ms 58988 KB Output is correct
37 Correct 208 ms 71020 KB Output is correct
38 Correct 214 ms 71020 KB Output is correct
39 Correct 205 ms 71404 KB Output is correct
40 Correct 202 ms 67052 KB Output is correct
41 Correct 201 ms 51820 KB Output is correct
42 Correct 289 ms 57816 KB Output is correct
43 Correct 619 ms 81516 KB Output is correct
44 Correct 618 ms 83948 KB Output is correct
45 Correct 292 ms 42604 KB Output is correct
46 Correct 292 ms 42604 KB Output is correct
47 Correct 504 ms 75384 KB Output is correct
48 Correct 497 ms 76524 KB Output is correct
49 Correct 40 ms 26476 KB Output is correct
50 Correct 28 ms 19436 KB Output is correct
51 Correct 38 ms 25964 KB Output is correct
52 Correct 1 ms 364 KB Output is correct
53 Correct 40 ms 26348 KB Output is correct
54 Correct 40 ms 26348 KB Output is correct
55 Correct 40 ms 26348 KB Output is correct
56 Correct 39 ms 26348 KB Output is correct
57 Correct 39 ms 26496 KB Output is correct
58 Correct 37 ms 25836 KB Output is correct
59 Correct 38 ms 26092 KB Output is correct
60 Correct 1 ms 364 KB Output is correct
61 Correct 1448 ms 299500 KB Output is correct
62 Correct 3298 ms 638132 KB Output is correct
63 Correct 3322 ms 640796 KB Output is correct
64 Correct 3369 ms 641108 KB Output is correct
65 Correct 257 ms 201080 KB Output is correct
66 Correct 523 ms 369364 KB Output is correct
67 Correct 566 ms 393580 KB Output is correct
68 Correct 3248 ms 767348 KB Output is correct
69 Correct 2463 ms 741188 KB Output is correct
70 Correct 3208 ms 740976 KB Output is correct
71 Correct 2292 ms 714456 KB Output is correct
72 Correct 3507 ms 901984 KB Output is correct
73 Execution timed out 5096 ms 621676 KB Time limit exceeded
74 Halted 0 ms 0 KB -