Submission #899479

# Submission time Handle Problem Language Result Execution time Memory
899479 2024-01-06T09:24:26 Z rxlfd314 Rectangles (IOI19_rect) C++17
37 / 100
5000 ms 45348 KB
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2")
#include "rect.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ari2 = array<int, 2>;

#define vt vector
#define size(x) (int((x).size()))
#define all(x) begin(x), end(x)

#define REP(a, b, c, d) for (int a = (b); (d) > 0 ? a <= (c) : a >= (c); a += (d))
#define FOR(a, b, c) REP(a, b, c, 1)
#define ROF(a, b, c) REP(a, b, c, -1)

struct BIT {
  vt<int> fen;
  BIT(int n) {
    fen.resize(n);
  }
  void upd(int i, int v) {
    for (; i < size(fen); i += i+1 & -i-1)
      fen[i] += v;
  }
  int qry(int i) {
    int ret = 0;
    for (; i >= 0; i -= i+1 & -i-1)
      ret += fen[i];
    return ret;
  }
  int qry(int l, int r) {
    return r < l ? 0 : qry(r) - qry(l-1);
  }
};

ll count_rectangles(vt<vt<int>> A) {
  const int N = size(A), M = size(A[0]);
  int X[N][M], Y[N][M];
  FOR(i, 0, N-1) {
    vt<int> sk;
    FOR(j, 0, M-1) {
      while (size(sk) && A[i][sk.back()] < A[i][j])
        sk.pop_back();
      X[i][j] = size(sk) ? sk.back() + 1 : 0;
      sk.push_back(j);
    }
    sk.clear();
    ROF(j, M-1, 0) {
      while (size(sk) && A[i][sk.back()] < A[i][j])
        sk.pop_back();
      Y[i][j] = size(sk) ? sk.back() - 1 : M - 1;
      sk.push_back(j);
    }
  }
  #ifdef DEBUG
  cout << "X:\n";
  FOR(i, 0, N-1)
    FOR(j, 0, M-1)
      cout << X[i][j] << "\n "[j+1<M];
  cout << "Y:\n";
  FOR(i, 0, N-1)
    FOR(j, 0, M-1)
      cout << Y[i][j] << "\n "[j+1<M];
  #endif
  
  ll ans = 0;
  int R[M], L[M], mx[M];
  FOR(l, 1, N-2) {
    memset(R, 0x3f, sizeof(R));
    memset(L, 0xc0, sizeof(L));
    memset(mx, 0xc0, sizeof(mx));
    FOR(r, l, N-2) {
      FOR(i, 0, M-1) {
        R[i] = min(R[i], Y[r][i]);
      }
      int ord[M];
      iota(ord, ord+M, 0);
      sort(ord, ord+M, [&](const int &a, const int &b) {
        return R[a] < R[b];    
      });
      BIT fen(M);
      priority_queue<ari2, vt<ari2>, greater<ari2>> ma;
      int dead_bef = 0, j = 0, k = 0;
      bool dead[M] = {};
      FOR(i, 0, M-1) {
        for (; k < M && R[ord[k]] < i-1; k++)
          if (!dead[ord[k]]) {
            dead[ord[k]] = true;
            fen.upd(ord[k], -1);
          }
        for (; j < dead_bef; j++)
          if (!dead[j]) {
            fen.upd(j, -1);
            dead[j] = true;
          }
        R[i] = min(R[i], Y[r][i]);
        L[i] = max(L[i], X[r][i]);
        mx[i] = max(mx[i], A[r][i]);
        if (i && mx[i-1] < min(A[l-1][i-1], A[r+1][i-1])) {
          ans += fen.qry(L[i]-1, i-2);
#ifdef DEBUG
          cout << "contribution from " << i-1 << ' ' << l << ' ' << r << ": " << fen.qry(L[i]-1, i-2) << '\n';
#endif
        } else {
          dead_bef = i - 1;
        }
        ma.push({R[i], i});
        fen.upd(i, 1);
      }
    }
  }
  return ans;
}

Compilation message

rect.cpp: In member function 'void BIT::upd(int, int)':
rect.cpp:23:33: warning: suggest parentheses around '+' in operand of '&' [-Wparentheses]
   23 |     for (; i < size(fen); i += i+1 & -i-1)
      |                                ~^~
rect.cpp: In member function 'int BIT::qry(int)':
rect.cpp:28:26: warning: suggest parentheses around '+' in operand of '&' [-Wparentheses]
   28 |     for (; i >= 0; i -= i+1 & -i-1)
      |                         ~^~
# 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 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 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 344 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 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 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 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 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 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 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 344 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 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 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 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 6 ms 540 KB Output is correct
23 Correct 7 ms 548 KB Output is correct
24 Correct 6 ms 540 KB Output is correct
25 Correct 9 ms 540 KB Output is correct
26 Correct 7 ms 344 KB Output is correct
27 Correct 7 ms 348 KB Output is correct
28 Correct 7 ms 348 KB Output is correct
29 Correct 4 ms 348 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 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 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 344 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 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 6 ms 540 KB Output is correct
18 Correct 7 ms 548 KB Output is correct
19 Correct 6 ms 540 KB Output is correct
20 Correct 9 ms 540 KB Output is correct
21 Correct 7 ms 344 KB Output is correct
22 Correct 7 ms 348 KB Output is correct
23 Correct 7 ms 348 KB Output is correct
24 Correct 4 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 98 ms 1056 KB Output is correct
31 Correct 98 ms 1048 KB Output is correct
32 Correct 95 ms 1060 KB Output is correct
33 Correct 106 ms 1112 KB Output is correct
34 Correct 110 ms 860 KB Output is correct
35 Correct 109 ms 856 KB Output is correct
36 Correct 105 ms 860 KB Output is correct
37 Correct 108 ms 856 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 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 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 344 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 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 6 ms 540 KB Output is correct
18 Correct 7 ms 548 KB Output is correct
19 Correct 6 ms 540 KB Output is correct
20 Correct 9 ms 540 KB Output is correct
21 Correct 7 ms 344 KB Output is correct
22 Correct 7 ms 348 KB Output is correct
23 Correct 7 ms 348 KB Output is correct
24 Correct 4 ms 348 KB Output is correct
25 Correct 98 ms 1056 KB Output is correct
26 Correct 98 ms 1048 KB Output is correct
27 Correct 95 ms 1060 KB Output is correct
28 Correct 106 ms 1112 KB Output is correct
29 Correct 110 ms 860 KB Output is correct
30 Correct 109 ms 856 KB Output is correct
31 Correct 105 ms 860 KB Output is correct
32 Correct 108 ms 856 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 4768 ms 8156 KB Output is correct
39 Execution timed out 5060 ms 8028 KB Time limit exceeded
40 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 1 ms 600 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Execution timed out 5056 ms 45348 KB Time limit exceeded
8 Halted 0 ms 0 KB -
# 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 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 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 344 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 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 6 ms 540 KB Output is correct
18 Correct 7 ms 548 KB Output is correct
19 Correct 6 ms 540 KB Output is correct
20 Correct 9 ms 540 KB Output is correct
21 Correct 7 ms 344 KB Output is correct
22 Correct 7 ms 348 KB Output is correct
23 Correct 7 ms 348 KB Output is correct
24 Correct 4 ms 348 KB Output is correct
25 Correct 98 ms 1056 KB Output is correct
26 Correct 98 ms 1048 KB Output is correct
27 Correct 95 ms 1060 KB Output is correct
28 Correct 106 ms 1112 KB Output is correct
29 Correct 110 ms 860 KB Output is correct
30 Correct 109 ms 856 KB Output is correct
31 Correct 105 ms 860 KB Output is correct
32 Correct 108 ms 856 KB Output is correct
33 Correct 4768 ms 8156 KB Output is correct
34 Execution timed out 5060 ms 8028 KB Time limit exceeded
35 Halted 0 ms 0 KB -