Submission #918691

# Submission time Handle Problem Language Result Execution time Memory
918691 2024-01-30T08:59:51 Z xynex Rectangles (IOI19_rect) C++17
49 / 100
930 ms 72244 KB
/**
  * Author: Tenjin
  * Created: 27.04.2022 18:58
  * Why am I so stupid? :c
  * Slishkom slab
**/

#include <bits/stdc++.h>

// #pragma GCC optimize("inline")
// #pragma GCC optimize("-fgcse,-fgcse-lm")
// #pragma GCC optimize("-ftree-pre,-ftree-vrp")
// #pragma GCC optimize("-ffast-math")
// #pragma GCC optimize("-fipa-sra")
// #pragma GCC optimize("-fpeephole2")
// #pragma GCC optimize("-fsched-spec")
// #pragma GCC optimize("Ofast,no-stack-protector")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
// #pragma GCC optimize("unroll-loops")

using namespace std;

#define ll long long
#define pr pair
#define vt vector
#define ff first
#define ss second
#define sz(a) (int)a.size()
#define pb push_back
#define pf push_front
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

template<typename T> T get_rand(T l, T r) {
  random_device rd;
  mt19937 gen(rd());
  return uniform_int_distribution<T>(l, r)(gen);
}

template<class A> void read(vt<A>& v);
template<class A, size_t S> void read(array<A, S>& a);
template<class T> void read(T& x) { cin >> x; }
void read(double& d) { string t; read(t); d = stod(t); }
void read(long double& d) { string t; read(t); d = stold(t); }
template<class H, class... T> void read(H& h, T&... t) { read(h); read(t...); }
template<class A> void read(vt<A>& x) { for (auto& a : x) read(a); }
template<class A, size_t S> void read(array<A, S>& x) { for (auto& a : x) read(a); }

string to_string(char c) { return string(1, c); }
string to_string(bool b) { return b ? "true" : "false"; }
string to_string(const char* s) { return string(s); }
string to_string(string s) { return s; }
string to_string(vt<bool> v) { string res; for (int i = 0; i < sz(v); ++i) res += char('0' + v[i]); return res; }
template<size_t S> string to_string(bitset<S> b) { string res; for (int i = 0; i < S; ++i) res += char('0' + b[i]); return res; }
template<class T> string to_string(T v) { bool f = 1; string res; for (auto x : v) { if (!f) res += ' '; f = 0; res += to_string(x); } return res; }

template<class A> void write(A x) { cout << to_string(x); }
template<class H, class... T> void write(const H& h, const T&... t) { write(h); write(t...); }

void print() { write("\n"); }
template<class H, class... T> void print(const H& h, const T&... t) { write(h); if (sizeof...(t)) write(' '); print(t...); }

void freop(string s) {
  freopen((s + ".in").c_str(), "r", stdin);
  freopen((s + ".out").c_str(), "w", stdout);
}

const int MOD = 1e9 + 7;
const ll INF = 1e14;
const int M = 4e5 + 5;
const double long pi = acos(-1);
const double long eps = 1e-12;
const int sq = 700;

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
/* ll vs int*/

const int N = 1e6 + 5;

map<pr<int, int>, bool> have;
bool valid[2501][2501];
ll count_rectangles(vt<vt<int>> a) {
  int n = sz(a);
  int m = sz(a[0]);
  vt<vt<int>> L(n, vt<int> (m, -1)), R(n, vt<int> (m, -1));
  vt<vt<int>> L1(n, vt<int> (m, -1)), R1(n, vt<int> (m, -1));
  for(int i = 0; i < n; ++i) {
    vt<pr<int, int> > st;
    for(int j = 0; j < m; ++j) {
      valid[i][j] = 1;
      while(!st.empty() && st.back().ff <= a[i][j]) st.pop_back();
      if(!st.empty()) L[i][j] = st.back().ss;
      else L[i][j] = -1;
      st.push_back({a[i][j], j});
    }
    st.clear();
    for(int j = m - 1; j >= 0; --j) {
      while(!st.empty() && st.back().ff <= a[i][j]) st.pop_back();
      if(!st.empty()) R[i][j] = st.back().ss;
      else R[i][j] = m;
      st.push_back({a[i][j], j});
    }
  }
  for(int i = 0; i < m; ++i) {
    vt<pr<int, int> > st;
    for(int j = 0; j < n; ++j) {
      while(!st.empty() && st.back().ff <= a[j][i]) st.pop_back();
      if(!st.empty()) L1[j][i] = st.back().ss;
      else L1[j][i] = -1;
      st.push_back({a[j][i], j});
    }
    st.clear();
    for(int j = n - 1; j >= 0; --j) {
      while(!st.empty() && st.back().ff <= a[j][i]) st.pop_back();
      if(!st.empty()) R1[j][i] = st.back().ss;
      else R1[j][i] = n;
      st.push_back({a[j][i], j});
    }
  }
  set<pr<int, int>> last;
  for(int i = 0; i < n; ++i) {
    set<pr<int, int>> nxt;
    for(int j = 0; j < m; ++j) {
      int limL = L1[i][j], limR = i;
     // print(i, j, limL, limR);
      if(limL == -1) {
        valid[i][j] = 0;
        continue;
      }
      int curL = L[i][j], curR = R[i][j];
     // print(curL, curR);
      if(curL == -1 || curR == m) {
        valid[i][j] = 0;
        continue;
      }
      curL *= n;
      curL += curR;
      auto fnd = last.lower_bound({curL, 0});
      int cur = 0;
      if(fnd != last.end() && (*fnd).ff == curL) {
        cur = (*fnd).ss;
      }
      cur++;
      //print(cur);
      if(cur < (limR - limL)) {
        valid[i][j] = 0;
      }
      //print(valid[i][j]);
      nxt.insert({curL, cur});
    }
    swap(last, nxt);
  }
  last.clear();
  for(int i = n - 1; i >= 0; --i) {
    set<pr<int, int>> nxt;
    for(int j = 0; j < m; ++j) {
      int limL = i, limR = R1[i][j];
      //print(i, j, limL, limR);
      if(limR == n) {
        valid[i][j] = 0;
        continue;
      }
      int curL = L[i][j], curR = R[i][j];
     // print(curL, curR);
      if(curL == -1 || curR == m) {
        valid[i][j] = 0;
        continue;
      }
      curL *= n;
      curL += curR;
      auto fnd = last.lower_bound({curL, 0});
      int cur = 0;
      if(fnd != last.end() && (*fnd).ff == curL) {
        cur = (*fnd).ss;
      }
      cur++;
      //print(cur);
      if(cur < (limR - limL)) {
        valid[i][j] = 0;
      }
      //print(valid[i][j]);
      nxt.insert({curL, cur});
    }
    swap(last, nxt);
  }
  last.clear();
  for(int i = 0; i < m; ++i) {
    set<pr<int, int>> nxt;
    for(int j = 0; j < n; ++j) {
      int limL = L[j][i], limR = i;
      //print(j, i, limL, limR);
      if(limL == -1) {
        valid[j][i] = 0;
        continue;
      }
      int curL = L1[j][i], curR = R1[j][i];
     // print(curL, curR);
      if(curL == -1 || curR == n) {
        valid[j][i] = 0;
        continue;
      }
      curL *= m;
      curL += curR;
      auto fnd = last.lower_bound({curL, 0});
      int cur = 0;
      if(fnd != last.end() && (*fnd).ff == curL) {
        cur = (*fnd).ss;
      }
      cur++;
      //print(cur);
      if(cur < (limR - limL)) {
        valid[j][i] = 0;
      }
      //print(valid[j][i]);
      nxt.insert({curL, cur});
    }
    swap(last, nxt);
  }
  last.clear();
  for(int i = m - 1; i >= 0; --i) {
    set<pr<int, int>> nxt;
    for(int j = 0; j < n; ++j) {
      int limL = i, limR = R[j][i];
      if(limR == m) {
        valid[j][i] = 0;
        continue;
      }
      int curL = L1[j][i], curR = R1[j][i];
      if(curL == -1 || curR == n) {
        valid[j][i] = 0;
        continue;
      }
      curL *= m;
      curL += curR;
      auto fnd = last.lower_bound({curL, 0});
      int cur = 0;
      if(fnd != last.end() && (*fnd).ff == curL) {
        cur = (*fnd).ss;
      }
      cur++;
      if(cur < (limR - limL)) {
        valid[j][i] = 0;
      }
      nxt.insert({curL, cur});
    }
    swap(last, nxt);
  }
  //vt<pr<int, int>> ord

  int ans = 0;
  if(n <= 1000 && m <= 1000) {
  for(int i = 1; i < n; ++i) {
    for(int j = 1; j < m; ++j) {
      /*if(L[i][j] != L[i - 1][j] && R[i][j] != R[i - 1][j] && L1[i - 1][j] != L1[i][j] && R1[i][j] != R[i - 1][j] && L[i][j] != L[i][j - 1] && R[i][j] != R[i][j - 1] && L1[i][j - 1] != L1[i][j] && R1[i][j] != R[i][j - 1]) {
        ans += valid[i][j];
      }*/
      if(!have[{L[i][j] * n + R[i][j], L1[i][j] * m + R1[i][j]}]) ans += valid[i][j];
      have[{L[i][j] * n + R[i][j], L1[i][j] * m + R1[i][j]}] = 1;
    }
  }
  }
  /*int ans = 0;
  for(auto cur : ord) ans += valid[cur.ff][cur.ss];*/
  return ans;
}

/*
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  //freop("");
  int t = 1;
  //read(t);
  for (int i = 1; i <= t; ++i) {
    //write("Case #" + to_string(i) + ": ");
    solve();
  }
  return 0;
}*/

Compilation message

rect.cpp: In function 'void freop(std::string)':
rect.cpp:64:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   64 |   freopen((s + ".in").c_str(), "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
rect.cpp:65:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   65 |   freopen((s + ".out").c_str(), "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 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 1 ms 348 KB Output is correct
8 Correct 1 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 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 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 1 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 1 ms 348 KB Output is correct
8 Correct 1 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 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 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 1 ms 348 KB Output is correct
22 Correct 4 ms 3164 KB Output is correct
23 Correct 4 ms 3164 KB Output is correct
24 Correct 4 ms 3164 KB Output is correct
25 Correct 4 ms 2908 KB Output is correct
26 Correct 5 ms 3164 KB Output is correct
27 Correct 5 ms 3164 KB Output is correct
28 Correct 5 ms 3164 KB Output is correct
29 Correct 2 ms 2652 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 1 ms 348 KB Output is correct
8 Correct 1 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 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 4 ms 3164 KB Output is correct
18 Correct 4 ms 3164 KB Output is correct
19 Correct 4 ms 3164 KB Output is correct
20 Correct 4 ms 2908 KB Output is correct
21 Correct 5 ms 3164 KB Output is correct
22 Correct 5 ms 3164 KB Output is correct
23 Correct 5 ms 3164 KB Output is correct
24 Correct 2 ms 2652 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 1 ms 348 KB Output is correct
30 Correct 22 ms 5980 KB Output is correct
31 Correct 20 ms 5984 KB Output is correct
32 Correct 20 ms 5980 KB Output is correct
33 Correct 21 ms 4956 KB Output is correct
34 Correct 39 ms 5968 KB Output is correct
35 Correct 33 ms 5972 KB Output is correct
36 Correct 31 ms 5724 KB Output is correct
37 Correct 31 ms 5664 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 1 ms 348 KB Output is correct
8 Correct 1 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 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 4 ms 3164 KB Output is correct
18 Correct 4 ms 3164 KB Output is correct
19 Correct 4 ms 3164 KB Output is correct
20 Correct 4 ms 2908 KB Output is correct
21 Correct 5 ms 3164 KB Output is correct
22 Correct 5 ms 3164 KB Output is correct
23 Correct 5 ms 3164 KB Output is correct
24 Correct 2 ms 2652 KB Output is correct
25 Correct 22 ms 5980 KB Output is correct
26 Correct 20 ms 5984 KB Output is correct
27 Correct 20 ms 5980 KB Output is correct
28 Correct 21 ms 4956 KB Output is correct
29 Correct 39 ms 5968 KB Output is correct
30 Correct 33 ms 5972 KB Output is correct
31 Correct 31 ms 5724 KB Output is correct
32 Correct 31 ms 5664 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 1 ms 348 KB Output is correct
38 Correct 228 ms 44792 KB Output is correct
39 Correct 183 ms 44884 KB Output is correct
40 Correct 244 ms 44784 KB Output is correct
41 Correct 243 ms 44660 KB Output is correct
42 Correct 327 ms 44884 KB Output is correct
43 Correct 329 ms 44780 KB Output is correct
44 Correct 324 ms 44880 KB Output is correct
45 Correct 316 ms 42012 KB Output is correct
46 Correct 226 ms 26708 KB Output is correct
47 Correct 323 ms 31988 KB Output is correct
48 Correct 552 ms 44696 KB Output is correct
49 Correct 584 ms 44752 KB Output is correct
50 Correct 256 ms 23680 KB Output is correct
51 Correct 245 ms 23620 KB Output is correct
52 Correct 494 ms 39072 KB Output is correct
53 Correct 490 ms 39320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 600 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 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 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Incorrect 930 ms 72244 KB Output isn't correct
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 1 ms 348 KB Output is correct
8 Correct 1 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 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 4 ms 3164 KB Output is correct
18 Correct 4 ms 3164 KB Output is correct
19 Correct 4 ms 3164 KB Output is correct
20 Correct 4 ms 2908 KB Output is correct
21 Correct 5 ms 3164 KB Output is correct
22 Correct 5 ms 3164 KB Output is correct
23 Correct 5 ms 3164 KB Output is correct
24 Correct 2 ms 2652 KB Output is correct
25 Correct 22 ms 5980 KB Output is correct
26 Correct 20 ms 5984 KB Output is correct
27 Correct 20 ms 5980 KB Output is correct
28 Correct 21 ms 4956 KB Output is correct
29 Correct 39 ms 5968 KB Output is correct
30 Correct 33 ms 5972 KB Output is correct
31 Correct 31 ms 5724 KB Output is correct
32 Correct 31 ms 5664 KB Output is correct
33 Correct 228 ms 44792 KB Output is correct
34 Correct 183 ms 44884 KB Output is correct
35 Correct 244 ms 44784 KB Output is correct
36 Correct 243 ms 44660 KB Output is correct
37 Correct 327 ms 44884 KB Output is correct
38 Correct 329 ms 44780 KB Output is correct
39 Correct 324 ms 44880 KB Output is correct
40 Correct 316 ms 42012 KB Output is correct
41 Correct 226 ms 26708 KB Output is correct
42 Correct 323 ms 31988 KB Output is correct
43 Correct 552 ms 44696 KB Output is correct
44 Correct 584 ms 44752 KB Output is correct
45 Correct 256 ms 23680 KB Output is correct
46 Correct 245 ms 23620 KB Output is correct
47 Correct 494 ms 39072 KB Output is correct
48 Correct 490 ms 39320 KB Output is correct
49 Incorrect 2 ms 600 KB Output isn't correct
50 Halted 0 ms 0 KB -