Submission #274215

# Submission time Handle Problem Language Result Execution time Memory
274215 2020-08-19T09:53:42 Z ne4eHbKa Rectangles (IOI19_rect) C++17
72 / 100
5000 ms 704744 KB
#include "rect.h"
#include <bits/stdc++.h>
using namespace std;

#define ft first
#define sd second
#define _ <<' '<<

void* MEM = malloc(600 << 20);

void* operator new(const size_t f) {
    void* res = MEM;
    MEM += f;
    return res;
}

void operator delete(void*) {}

//#pragma GCC optimize("Ofast,O3")

namespace S {

//#include <ext/pb_ds/assoc_container.hpp>

//typedef __gnu_pbds::cc_hash_table<int, int> ht;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vv;
typedef pair<int, int> pii;

const int N = 2505;

int n, m;
forward_list<short> l[N][N], u[N][N];
int p[N], length[N], la[N];
map<int, int> fl[N], cl, fu, cu;
bool mk[N];
int c[2 * N], cn, xn, yn, rc[2 * N];
pii x[N], y[N];
int f[N], fn;

int fw[2 * N], fmax, nx[N + N], pr[N + N];
inline void finit(const int n) { fmax = n; memset(fw, 0, n + 1 << 2); }
inline void fadd(int i) { for(i = fmax - i; i <= fmax; i = nx[i]) fw[i]++; }
inline int fget(int i) { int v{}; for(i = fmax - i; i; i = pr[i]) v += fw[i]; return v; }

inline int get(const map<int, int> &a, const int &b) {
    const auto f = a.find(b);
    if(f == a.end()) return 0;
    return f->sd;
}

ll solve() {
    for(int i = 0; i < N + N; ++i) nx[i] = i + (i & -i), pr[i] = i - (i & -i);
    ll ans = 0;
    for(int i = 0; i < n; ++i) {
        for(int j = 0; j < m; ++j) {
            swap(cl, fl[j]);
            for(const int &t : l[i][j])
                fl[j][t] = get(cl, t) + 1;
            cl.clear();
            swap(cu, fu);
            for(const int &t : u[i][j])
                fu[t] = get(cu, t) + 1;
            cu.clear();
            const auto &a = fl[j], &b = fu;
            if(a.size() * b.size() < 5) {
                for(const auto &i : a)
                    for(const auto &j : b)
                        ans += i.ft <= j.sd && j.ft <= i.sd;
                continue;
            }
            cn = 0;
            for(const auto &i : a) c[cn++] = i.ft;
            for(const auto &i : b) c[cn++] = i.sd;
            sort(c, c + cn);
            cn = unique(c, c + cn) - c;
            xn = yn = 0;
            finit(cn);
            for(int i = 0; i < cn; ++i) rc[c[i]] = i;
            for(const auto &i : a) x[xn++] = {i.sd, rc[i.ft]};
            for(const auto &i : b) y[yn++] = {i.ft, rc[i.sd]};
            sort(x, x + xn);
            int it = 0;
            for(int i = 0; i < xn; ++i) {
                for(; it < yn && y[it].ft <= x[i].ft; ++it)
                    fadd(y[it].sd);
                ans += fget(x[i].sd);
            }
        }
        fu.clear();
    }
    return ans;
}

int last(int i) {
    return la[i] == i ? i : la[i] = last(la[i]);
}

void unite(int i, int j) {
    i = last(i), j = last(j);
    length[j] += length[i];
    la[i] = j;
}

}

S::ll count_rectangles(S::vv a) {
    using namespace S;
    n = a.size();
    m = a[0].size();
    for(int i = 0; i < n; ++i) {
        iota(p, p + m, 0);
        iota(la, la + m, 0);
        memset(mk, 0, m);
        sort(p, p + m, [&] (const int &fi, const int &se) {return a[i][fi] < a[i][se];});
        for(int t = 0; t < m; ) {
            int v = a[i][p[t]];
            fn = 0;
            for(; t < m && a[i][p[t]] == v; ++t) {
                int j = p[t];
                mk[j] = true;
                length[j] = 1;
                f[fn++] = j;
                if(j + 1 < m && mk[j + 1]) unite(j, j + 1);
                if(j && mk[j - 1]) unite(j - 1, j);
            }
            for(int it = 0; it < fn; ++it) mk[f[it] = last(f[it])] = false;
            for(int it = 0; it < fn; ++it) {
                int j = f[it];
                if(mk[j]) continue;
                mk[j] = true;
                if(i == 0 || i == n - 1) continue;
                if(j == m - 1 || length[j] == j + 1) continue;
                l[i][j].push_front(length[j]);
            }
        }
    }
    for(int j = 0; j < m; ++j) {
        iota(p, p + n, 0);
        iota(la, la + n, 0);
        memset(mk, 0, n);
        sort(p, p + n, [&] (const int &fi, const int &se) {return a[fi][j] < a[se][j];});
        for(int t = 0; t < n; ) {
            int v = a[p[t]][j];
            fn = 0;
            for(; t < n && a[p[t]][j] == v; ++t) {
                int i = p[t];
                mk[i] = true;
                length[i] = 1;
                f[fn++] = i;
                if(i + 1 < n && mk[i + 1]) unite(i, i + 1);
                if(i && mk[i - 1]) unite(i - 1, i);
            }
            for(int it = 0; it < fn; ++it) mk[f[it] = last(f[it])] = false;
            for(int it = 0; it < fn; ++it) {
                int i = f[it];
                if(mk[i]) continue;
                mk[i] = true;
                if(j == 0 || j == m - 1) continue;
                if(i == n - 1 || length[i] == i + 1) continue;
                u[i][j].push_front(length[i]);
            }
        }
    }
    return solve();
}

#undef ft
#undef sd
#undef _

Compilation message

rect.cpp: In function 'void* operator new(size_t)':
rect.cpp:13:9: warning: pointer of type 'void *' used in arithmetic [-Wpointer-arith]
   13 |     MEM += f;
      |     ~~~~^~~~
rect.cpp: In function 'void S::finit(int)':
rect.cpp:43:60: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
   43 | inline void finit(const int n) { fmax = n; memset(fw, 0, n + 1 << 2); }
      |                                                          ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 80 ms 98680 KB Output is correct
2 Correct 81 ms 98808 KB Output is correct
3 Correct 88 ms 98936 KB Output is correct
4 Correct 84 ms 98808 KB Output is correct
5 Correct 81 ms 98808 KB Output is correct
6 Correct 82 ms 98808 KB Output is correct
7 Correct 89 ms 98808 KB Output is correct
8 Correct 84 ms 98808 KB Output is correct
9 Correct 95 ms 98808 KB Output is correct
10 Correct 84 ms 98808 KB Output is correct
11 Correct 84 ms 98908 KB Output is correct
12 Correct 94 ms 98864 KB Output is correct
13 Correct 86 ms 98680 KB Output is correct
14 Correct 83 ms 98680 KB Output is correct
15 Correct 81 ms 98936 KB Output is correct
16 Correct 85 ms 98680 KB Output is correct
17 Correct 82 ms 98680 KB Output is correct
18 Correct 82 ms 98680 KB Output is correct
19 Correct 83 ms 98808 KB Output is correct
20 Correct 82 ms 98680 KB Output is correct
21 Correct 84 ms 98680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 80 ms 98680 KB Output is correct
2 Correct 81 ms 98808 KB Output is correct
3 Correct 88 ms 98936 KB Output is correct
4 Correct 84 ms 98808 KB Output is correct
5 Correct 81 ms 98808 KB Output is correct
6 Correct 82 ms 98808 KB Output is correct
7 Correct 89 ms 98808 KB Output is correct
8 Correct 84 ms 98808 KB Output is correct
9 Correct 95 ms 98808 KB Output is correct
10 Correct 84 ms 98808 KB Output is correct
11 Correct 84 ms 98908 KB Output is correct
12 Correct 94 ms 98864 KB Output is correct
13 Correct 86 ms 98680 KB Output is correct
14 Correct 83 ms 98680 KB Output is correct
15 Correct 81 ms 98936 KB Output is correct
16 Correct 85 ms 98680 KB Output is correct
17 Correct 83 ms 99448 KB Output is correct
18 Correct 84 ms 99448 KB Output is correct
19 Correct 84 ms 99448 KB Output is correct
20 Correct 82 ms 99064 KB Output is correct
21 Correct 88 ms 99436 KB Output is correct
22 Correct 88 ms 99448 KB Output is correct
23 Correct 83 ms 99448 KB Output is correct
24 Correct 82 ms 99068 KB Output is correct
25 Correct 82 ms 98680 KB Output is correct
26 Correct 82 ms 98680 KB Output is correct
27 Correct 83 ms 98808 KB Output is correct
28 Correct 82 ms 98680 KB Output is correct
29 Correct 84 ms 98680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 80 ms 98680 KB Output is correct
2 Correct 81 ms 98808 KB Output is correct
3 Correct 88 ms 98936 KB Output is correct
4 Correct 84 ms 98808 KB Output is correct
5 Correct 81 ms 98808 KB Output is correct
6 Correct 82 ms 98808 KB Output is correct
7 Correct 89 ms 98808 KB Output is correct
8 Correct 84 ms 98808 KB Output is correct
9 Correct 95 ms 98808 KB Output is correct
10 Correct 84 ms 98808 KB Output is correct
11 Correct 84 ms 98908 KB Output is correct
12 Correct 94 ms 98864 KB Output is correct
13 Correct 86 ms 98680 KB Output is correct
14 Correct 83 ms 98680 KB Output is correct
15 Correct 81 ms 98936 KB Output is correct
16 Correct 85 ms 98680 KB Output is correct
17 Correct 83 ms 99448 KB Output is correct
18 Correct 84 ms 99448 KB Output is correct
19 Correct 84 ms 99448 KB Output is correct
20 Correct 82 ms 99064 KB Output is correct
21 Correct 88 ms 99436 KB Output is correct
22 Correct 88 ms 99448 KB Output is correct
23 Correct 83 ms 99448 KB Output is correct
24 Correct 82 ms 99068 KB Output is correct
25 Correct 99 ms 103416 KB Output is correct
26 Correct 100 ms 103416 KB Output is correct
27 Correct 97 ms 103396 KB Output is correct
28 Correct 92 ms 100736 KB Output is correct
29 Correct 102 ms 103268 KB Output is correct
30 Correct 99 ms 103160 KB Output is correct
31 Correct 109 ms 102904 KB Output is correct
32 Correct 97 ms 102776 KB Output is correct
33 Correct 82 ms 98680 KB Output is correct
34 Correct 82 ms 98680 KB Output is correct
35 Correct 83 ms 98808 KB Output is correct
36 Correct 82 ms 98680 KB Output is correct
37 Correct 84 ms 98680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 80 ms 98680 KB Output is correct
2 Correct 81 ms 98808 KB Output is correct
3 Correct 88 ms 98936 KB Output is correct
4 Correct 84 ms 98808 KB Output is correct
5 Correct 81 ms 98808 KB Output is correct
6 Correct 82 ms 98808 KB Output is correct
7 Correct 89 ms 98808 KB Output is correct
8 Correct 84 ms 98808 KB Output is correct
9 Correct 95 ms 98808 KB Output is correct
10 Correct 84 ms 98808 KB Output is correct
11 Correct 84 ms 98908 KB Output is correct
12 Correct 94 ms 98864 KB Output is correct
13 Correct 86 ms 98680 KB Output is correct
14 Correct 83 ms 98680 KB Output is correct
15 Correct 81 ms 98936 KB Output is correct
16 Correct 85 ms 98680 KB Output is correct
17 Correct 83 ms 99448 KB Output is correct
18 Correct 84 ms 99448 KB Output is correct
19 Correct 84 ms 99448 KB Output is correct
20 Correct 82 ms 99064 KB Output is correct
21 Correct 88 ms 99436 KB Output is correct
22 Correct 88 ms 99448 KB Output is correct
23 Correct 83 ms 99448 KB Output is correct
24 Correct 82 ms 99068 KB Output is correct
25 Correct 99 ms 103416 KB Output is correct
26 Correct 100 ms 103416 KB Output is correct
27 Correct 97 ms 103396 KB Output is correct
28 Correct 92 ms 100736 KB Output is correct
29 Correct 102 ms 103268 KB Output is correct
30 Correct 99 ms 103160 KB Output is correct
31 Correct 109 ms 102904 KB Output is correct
32 Correct 97 ms 102776 KB Output is correct
33 Correct 240 ms 129400 KB Output is correct
34 Correct 212 ms 129400 KB Output is correct
35 Correct 227 ms 129400 KB Output is correct
36 Correct 189 ms 129400 KB Output is correct
37 Correct 334 ms 156024 KB Output is correct
38 Correct 333 ms 156024 KB Output is correct
39 Correct 338 ms 156152 KB Output is correct
40 Correct 345 ms 152312 KB Output is correct
41 Correct 181 ms 115960 KB Output is correct
42 Correct 206 ms 123384 KB Output is correct
43 Correct 372 ms 154576 KB Output is correct
44 Correct 385 ms 155220 KB Output is correct
45 Correct 231 ms 126712 KB Output is correct
46 Correct 218 ms 126712 KB Output is correct
47 Correct 359 ms 150716 KB Output is correct
48 Correct 367 ms 151036 KB Output is correct
49 Correct 82 ms 98680 KB Output is correct
50 Correct 82 ms 98680 KB Output is correct
51 Correct 83 ms 98808 KB Output is correct
52 Correct 82 ms 98680 KB Output is correct
53 Correct 84 ms 98680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 89 ms 99192 KB Output is correct
2 Correct 85 ms 99192 KB Output is correct
3 Correct 82 ms 98808 KB Output is correct
4 Correct 81 ms 98680 KB Output is correct
5 Correct 95 ms 99064 KB Output is correct
6 Correct 91 ms 99064 KB Output is correct
7 Correct 84 ms 99064 KB Output is correct
8 Correct 83 ms 99064 KB Output is correct
9 Correct 82 ms 98936 KB Output is correct
10 Correct 81 ms 98808 KB Output is correct
11 Correct 83 ms 98808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 80 ms 98684 KB Output is correct
2 Correct 751 ms 199416 KB Output is correct
3 Correct 1693 ms 317432 KB Output is correct
4 Correct 1731 ms 318792 KB Output is correct
5 Correct 1701 ms 318840 KB Output is correct
6 Correct 494 ms 123000 KB Output is correct
7 Correct 1020 ms 144888 KB Output is correct
8 Correct 1078 ms 147960 KB Output is correct
9 Correct 82 ms 98680 KB Output is correct
10 Correct 82 ms 98680 KB Output is correct
11 Correct 83 ms 98808 KB Output is correct
12 Correct 82 ms 98680 KB Output is correct
13 Correct 84 ms 98680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 80 ms 98680 KB Output is correct
2 Correct 81 ms 98808 KB Output is correct
3 Correct 88 ms 98936 KB Output is correct
4 Correct 84 ms 98808 KB Output is correct
5 Correct 81 ms 98808 KB Output is correct
6 Correct 82 ms 98808 KB Output is correct
7 Correct 89 ms 98808 KB Output is correct
8 Correct 84 ms 98808 KB Output is correct
9 Correct 95 ms 98808 KB Output is correct
10 Correct 84 ms 98808 KB Output is correct
11 Correct 84 ms 98908 KB Output is correct
12 Correct 94 ms 98864 KB Output is correct
13 Correct 86 ms 98680 KB Output is correct
14 Correct 83 ms 98680 KB Output is correct
15 Correct 81 ms 98936 KB Output is correct
16 Correct 85 ms 98680 KB Output is correct
17 Correct 83 ms 99448 KB Output is correct
18 Correct 84 ms 99448 KB Output is correct
19 Correct 84 ms 99448 KB Output is correct
20 Correct 82 ms 99064 KB Output is correct
21 Correct 88 ms 99436 KB Output is correct
22 Correct 88 ms 99448 KB Output is correct
23 Correct 83 ms 99448 KB Output is correct
24 Correct 82 ms 99068 KB Output is correct
25 Correct 99 ms 103416 KB Output is correct
26 Correct 100 ms 103416 KB Output is correct
27 Correct 97 ms 103396 KB Output is correct
28 Correct 92 ms 100736 KB Output is correct
29 Correct 102 ms 103268 KB Output is correct
30 Correct 99 ms 103160 KB Output is correct
31 Correct 109 ms 102904 KB Output is correct
32 Correct 97 ms 102776 KB Output is correct
33 Correct 240 ms 129400 KB Output is correct
34 Correct 212 ms 129400 KB Output is correct
35 Correct 227 ms 129400 KB Output is correct
36 Correct 189 ms 129400 KB Output is correct
37 Correct 334 ms 156024 KB Output is correct
38 Correct 333 ms 156024 KB Output is correct
39 Correct 338 ms 156152 KB Output is correct
40 Correct 345 ms 152312 KB Output is correct
41 Correct 181 ms 115960 KB Output is correct
42 Correct 206 ms 123384 KB Output is correct
43 Correct 372 ms 154576 KB Output is correct
44 Correct 385 ms 155220 KB Output is correct
45 Correct 231 ms 126712 KB Output is correct
46 Correct 218 ms 126712 KB Output is correct
47 Correct 359 ms 150716 KB Output is correct
48 Correct 367 ms 151036 KB Output is correct
49 Correct 89 ms 99192 KB Output is correct
50 Correct 85 ms 99192 KB Output is correct
51 Correct 82 ms 98808 KB Output is correct
52 Correct 81 ms 98680 KB Output is correct
53 Correct 95 ms 99064 KB Output is correct
54 Correct 91 ms 99064 KB Output is correct
55 Correct 84 ms 99064 KB Output is correct
56 Correct 83 ms 99064 KB Output is correct
57 Correct 82 ms 98936 KB Output is correct
58 Correct 81 ms 98808 KB Output is correct
59 Correct 83 ms 98808 KB Output is correct
60 Correct 80 ms 98684 KB Output is correct
61 Correct 751 ms 199416 KB Output is correct
62 Correct 1693 ms 317432 KB Output is correct
63 Correct 1731 ms 318792 KB Output is correct
64 Correct 1701 ms 318840 KB Output is correct
65 Correct 494 ms 123000 KB Output is correct
66 Correct 1020 ms 144888 KB Output is correct
67 Correct 1078 ms 147960 KB Output is correct
68 Correct 3086 ms 490128 KB Output is correct
69 Correct 2561 ms 491792 KB Output is correct
70 Correct 2717 ms 491772 KB Output is correct
71 Correct 2077 ms 491608 KB Output is correct
72 Execution timed out 5095 ms 704744 KB Time limit exceeded
73 Halted 0 ms 0 KB -