답안 #918707

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
918707 2024-01-30T09:35:37 Z xynex Rectangles (IOI19_rect) C++17
72 / 100
5000 ms 287184 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;
const ll X = 3e3;
const ll Y = X * X * X;
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
 // set<pr<int, int>> have;
  int ans = 0;
  vt<pr<ll, int>> an;
  for(int i = 1; i < n - 1; ++i) {
    for(int j = 1; j < m - 1; ++j) {
      ll cur = L[i][j] * X + R[i][j];
      ll cur1 = L1[i][j] * X + R1[i][j];
      cur *= Y;
      cur += cur1;
      an.push_back({cur, valid[i][j]});
    }
  }
  sort(all(an));
  for(int i = 0; i < sz(an); ++i) {
    if(i && an[i].ff == an[i - 1].ff) continue;
    ans += an[i].ss;
  }
  /*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);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 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 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 524 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 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 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 1 ms 360 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 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 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 524 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 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 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 1 ms 360 KB Output is correct
22 Correct 3 ms 2908 KB Output is correct
23 Correct 3 ms 2908 KB Output is correct
24 Correct 3 ms 2908 KB Output is correct
25 Correct 3 ms 2908 KB Output is correct
26 Correct 4 ms 2904 KB Output is correct
27 Correct 4 ms 3160 KB Output is correct
28 Correct 4 ms 2908 KB Output is correct
29 Correct 2 ms 2652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 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 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 524 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 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 3 ms 2908 KB Output is correct
18 Correct 3 ms 2908 KB Output is correct
19 Correct 3 ms 2908 KB Output is correct
20 Correct 3 ms 2908 KB Output is correct
21 Correct 4 ms 2904 KB Output is correct
22 Correct 4 ms 3160 KB Output is correct
23 Correct 4 ms 2908 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 0 ms 348 KB Output is correct
28 Correct 1 ms 344 KB Output is correct
29 Correct 1 ms 360 KB Output is correct
30 Correct 16 ms 4800 KB Output is correct
31 Correct 16 ms 4560 KB Output is correct
32 Correct 17 ms 4796 KB Output is correct
33 Correct 15 ms 4568 KB Output is correct
34 Correct 24 ms 4568 KB Output is correct
35 Correct 24 ms 4560 KB Output is correct
36 Correct 24 ms 4792 KB Output is correct
37 Correct 23 ms 4556 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 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 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 524 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 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 3 ms 2908 KB Output is correct
18 Correct 3 ms 2908 KB Output is correct
19 Correct 3 ms 2908 KB Output is correct
20 Correct 3 ms 2908 KB Output is correct
21 Correct 4 ms 2904 KB Output is correct
22 Correct 4 ms 3160 KB Output is correct
23 Correct 4 ms 2908 KB Output is correct
24 Correct 2 ms 2652 KB Output is correct
25 Correct 16 ms 4800 KB Output is correct
26 Correct 16 ms 4560 KB Output is correct
27 Correct 17 ms 4796 KB Output is correct
28 Correct 15 ms 4568 KB Output is correct
29 Correct 24 ms 4568 KB Output is correct
30 Correct 24 ms 4560 KB Output is correct
31 Correct 24 ms 4792 KB Output is correct
32 Correct 23 ms 4556 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 1 ms 344 KB Output is correct
37 Correct 1 ms 360 KB Output is correct
38 Correct 119 ms 24144 KB Output is correct
39 Correct 116 ms 24400 KB Output is correct
40 Correct 115 ms 24192 KB Output is correct
41 Correct 102 ms 24000 KB Output is correct
42 Correct 259 ms 23484 KB Output is correct
43 Correct 251 ms 23424 KB Output is correct
44 Correct 251 ms 24204 KB Output is correct
45 Correct 238 ms 22692 KB Output is correct
46 Correct 155 ms 23384 KB Output is correct
47 Correct 200 ms 24156 KB Output is correct
48 Correct 310 ms 22724 KB Output is correct
49 Correct 322 ms 24204 KB Output is correct
50 Correct 172 ms 13780 KB Output is correct
51 Correct 156 ms 14280 KB Output is correct
52 Correct 300 ms 23688 KB Output is correct
53 Correct 304 ms 23436 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 600 KB Output is correct
2 Correct 3 ms 600 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 2 ms 716 KB Output is correct
6 Correct 2 ms 600 KB Output is correct
7 Correct 2 ms 600 KB Output is correct
8 Correct 2 ms 604 KB Output is correct
9 Correct 2 ms 604 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 2 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 360 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1202 ms 139532 KB Output is correct
8 Correct 2708 ms 284760 KB Output is correct
9 Correct 2719 ms 286104 KB Output is correct
10 Correct 2771 ms 286056 KB Output is correct
11 Correct 358 ms 143048 KB Output is correct
12 Correct 778 ms 276708 KB Output is correct
13 Correct 828 ms 287012 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 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 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 524 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 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 3 ms 2908 KB Output is correct
18 Correct 3 ms 2908 KB Output is correct
19 Correct 3 ms 2908 KB Output is correct
20 Correct 3 ms 2908 KB Output is correct
21 Correct 4 ms 2904 KB Output is correct
22 Correct 4 ms 3160 KB Output is correct
23 Correct 4 ms 2908 KB Output is correct
24 Correct 2 ms 2652 KB Output is correct
25 Correct 16 ms 4800 KB Output is correct
26 Correct 16 ms 4560 KB Output is correct
27 Correct 17 ms 4796 KB Output is correct
28 Correct 15 ms 4568 KB Output is correct
29 Correct 24 ms 4568 KB Output is correct
30 Correct 24 ms 4560 KB Output is correct
31 Correct 24 ms 4792 KB Output is correct
32 Correct 23 ms 4556 KB Output is correct
33 Correct 119 ms 24144 KB Output is correct
34 Correct 116 ms 24400 KB Output is correct
35 Correct 115 ms 24192 KB Output is correct
36 Correct 102 ms 24000 KB Output is correct
37 Correct 259 ms 23484 KB Output is correct
38 Correct 251 ms 23424 KB Output is correct
39 Correct 251 ms 24204 KB Output is correct
40 Correct 238 ms 22692 KB Output is correct
41 Correct 155 ms 23384 KB Output is correct
42 Correct 200 ms 24156 KB Output is correct
43 Correct 310 ms 22724 KB Output is correct
44 Correct 322 ms 24204 KB Output is correct
45 Correct 172 ms 13780 KB Output is correct
46 Correct 156 ms 14280 KB Output is correct
47 Correct 300 ms 23688 KB Output is correct
48 Correct 304 ms 23436 KB Output is correct
49 Correct 2 ms 600 KB Output is correct
50 Correct 3 ms 600 KB Output is correct
51 Correct 1 ms 604 KB Output is correct
52 Correct 0 ms 348 KB Output is correct
53 Correct 2 ms 716 KB Output is correct
54 Correct 2 ms 600 KB Output is correct
55 Correct 2 ms 600 KB Output is correct
56 Correct 2 ms 604 KB Output is correct
57 Correct 2 ms 604 KB Output is correct
58 Correct 1 ms 348 KB Output is correct
59 Correct 2 ms 604 KB Output is correct
60 Correct 1 ms 348 KB Output is correct
61 Correct 1202 ms 139532 KB Output is correct
62 Correct 2708 ms 284760 KB Output is correct
63 Correct 2719 ms 286104 KB Output is correct
64 Correct 2771 ms 286056 KB Output is correct
65 Correct 358 ms 143048 KB Output is correct
66 Correct 778 ms 276708 KB Output is correct
67 Correct 828 ms 287012 KB Output is correct
68 Correct 0 ms 348 KB Output is correct
69 Correct 0 ms 348 KB Output is correct
70 Correct 0 ms 348 KB Output is correct
71 Correct 1 ms 344 KB Output is correct
72 Correct 1 ms 360 KB Output is correct
73 Correct 2227 ms 287056 KB Output is correct
74 Correct 2016 ms 285516 KB Output is correct
75 Correct 2139 ms 287124 KB Output is correct
76 Correct 1875 ms 287184 KB Output is correct
77 Correct 4600 ms 286800 KB Output is correct
78 Correct 2996 ms 160712 KB Output is correct
79 Correct 2834 ms 166256 KB Output is correct
80 Execution timed out 5018 ms 286284 KB Time limit exceeded
81 Halted 0 ms 0 KB -