Submission #153570

#TimeUsernameProblemLanguageResultExecution timeMemory
153570myungwooRectangles (IOI19_rect)C++14
72 / 100
5086 ms335584 KiB
#include <bits/stdc++.h> #include "rect.h" using namespace std; #define fr first #define sc second #define pb push_back #define sz(v) ((int)(v).size()) #define all(v) (v).begin(), (v).end() typedef long long lld; typedef pair<int, int> pii; int N, M; int A[2502][2502]; pii ver[2502][2502], hor[2502][2502]; struct SEGMENT { short a, b, t, dp; bool operator < (const SEGMENT &ot)const{ if (t != ot.t) return t < ot.t; if (a != ot.a) return a < ot.a; return b < ot.b; } bool operator == (const SEGMENT &ot)const{ return a == ot.a && b == ot.b && t == ot.t; } bool operator != (const SEGMENT &ot)const{ return a != ot.a || b != ot.b || t != ot.t; } }; struct RECT { short a, b, c, d; bool operator < (const RECT &ot)const{ if (a != ot.a) return a < ot.a; if (b != ot.b) return b < ot.b; if (c != ot.c) return c < ot.c; return d < ot.d; } bool operator != (const RECT &ot)const{ return a != ot.a || b != ot.b || c != ot.c || d != ot.d; } }; lld count_rectangles(vector<vector<int>> a) { N = sz(a); M = sz(a[0]); for (int i=1;i<=N;i++) for (int j=1;j<=M;j++) A[i][j] = a[i-1][j-1]; vector <SEGMENT> vertical, horizontal; for (int i=1;i<=N;i++){ { stack <int> stk; for (int j=1;j<=M;j++){ while (!stk.empty() && A[i][stk.top()] <= A[i][j]) stk.pop(); hor[i][j].fr = stk.empty() ? 1 : stk.top()+1; stk.push(j); } } { stack <int> stk; for (int j=M;j;j--){ while (!stk.empty() && A[i][stk.top()] <= A[i][j]) stk.pop(); hor[i][j].sc = stk.empty() ? M : stk.top()-1; stk.push(j); } } } for (int j=1;j<=M;j++){ { stack <int> stk; for (int i=1;i<=N;i++){ while (!stk.empty() && A[stk.top()][j] <= A[i][j]) stk.pop(); ver[i][j].fr = stk.empty() ? 1 : stk.top()+1; stk.push(i); } } { stack <int> stk; for (int i=N;i;i--){ while (!stk.empty() && A[stk.top()][j] <= A[i][j]) stk.pop(); ver[i][j].sc = stk.empty() ? N : stk.top()-1; stk.push(i); } } } for (int i=1;i<=N;i++) for (int j=1;j<=M;j++){ horizontal.pb({hor[i][j].fr, hor[i][j].sc, i}); vertical.pb({ver[i][j].fr, ver[i][j].sc, j}); } sort(all(horizontal)); sort(all(vertical)); for (auto i=horizontal.begin(),j=i;i!=horizontal.end();i++){ while (*j < SEGMENT{i->a, i->b, i->t-1}) j++; if (*j == SEGMENT{i->a, i->b, i->t-1}) i->dp = j->dp+1; else i->dp = 1; } for (auto i=vertical.begin(),j=i;i!=vertical.end();i++){ while (*j < SEGMENT{i->a, i->b, i->t-1}) j++; if (*j == SEGMENT{i->a, i->b, i->t-1}) i->dp = j->dp+1; else i->dp = 1; } vector <RECT> rects; for (int i=1;i<=N;i++) for (int j=1;j<=M;j++){ int sy = ver[i][j].fr, ey = ver[i][j].sc; int sx = hor[i][j].fr, ex = hor[i][j].sc; if (sy == 1 || sx == 1 || ey == N || ex == M) continue; { auto it = lower_bound(all(vertical), SEGMENT{sy, ey, ex}); if (it == vertical.end() || *it != SEGMENT{sy, ey, ex} || it->dp <= ex-sx) continue; } { auto it = lower_bound(all(horizontal), SEGMENT{sx, ex, ey}); if (it == horizontal.end() || *it != SEGMENT{sx, ex, ey} || it->dp <= ey-sy) continue; } rects.pb({sy, sx, ey, ex}); } sort(all(rects)); int ans = 0; for (int i=0;i<sz(rects);i++) if (i == 0 || rects[i-1] != rects[i]) ans++; return ans; }

Compilation message (stderr)

rect.cpp: In function 'lld count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:5:12: warning: narrowing conversion of 'hor[i][j].std::pair<int, int>::first' from 'int' to 'short int' inside { } [-Wnarrowing]
 #define fr first
            ^
rect.cpp:88:28: note: in expansion of macro 'fr'
   horizontal.pb({hor[i][j].fr, hor[i][j].sc, i});
                            ^~
rect.cpp:6:12: warning: narrowing conversion of 'hor[i][j].std::pair<int, int>::second' from 'int' to 'short int' inside { } [-Wnarrowing]
 #define sc second
            ^
rect.cpp:88:42: note: in expansion of macro 'sc'
   horizontal.pb({hor[i][j].fr, hor[i][j].sc, i});
                                          ^~
rect.cpp:88:48: warning: narrowing conversion of 'i' from 'int' to 'short int' inside { } [-Wnarrowing]
   horizontal.pb({hor[i][j].fr, hor[i][j].sc, i});
                                                ^
rect.cpp:5:12: warning: narrowing conversion of 'ver[i][j].std::pair<int, int>::first' from 'int' to 'short int' inside { } [-Wnarrowing]
 #define fr first
            ^
rect.cpp:89:26: note: in expansion of macro 'fr'
   vertical.pb({ver[i][j].fr, ver[i][j].sc, j});
                          ^~
rect.cpp:6:12: warning: narrowing conversion of 'ver[i][j].std::pair<int, int>::second' from 'int' to 'short int' inside { } [-Wnarrowing]
 #define sc second
            ^
rect.cpp:89:40: note: in expansion of macro 'sc'
   vertical.pb({ver[i][j].fr, ver[i][j].sc, j});
                                        ^~
rect.cpp:89:46: warning: narrowing conversion of 'j' from 'int' to 'short int' inside { } [-Wnarrowing]
   vertical.pb({ver[i][j].fr, ver[i][j].sc, j});
                                              ^
rect.cpp:95:39: warning: narrowing conversion of '(((int)i.__gnu_cxx::__normal_iterator<SEGMENT*, std::vector<SEGMENT> >::operator->()->SEGMENT::t) - 1)' from 'int' to 'short int' inside { } [-Wnarrowing]
   while (*j < SEGMENT{i->a, i->b, i->t-1}) j++;
                                   ~~~~^~
rect.cpp:96:37: warning: narrowing conversion of '(((int)i.__gnu_cxx::__normal_iterator<SEGMENT*, std::vector<SEGMENT> >::operator->()->SEGMENT::t) - 1)' from 'int' to 'short int' inside { } [-Wnarrowing]
   if (*j == SEGMENT{i->a, i->b, i->t-1}) i->dp = j->dp+1;
                                 ~~~~^~
rect.cpp:100:39: warning: narrowing conversion of '(((int)i.__gnu_cxx::__normal_iterator<SEGMENT*, std::vector<SEGMENT> >::operator->()->SEGMENT::t) - 1)' from 'int' to 'short int' inside { } [-Wnarrowing]
   while (*j < SEGMENT{i->a, i->b, i->t-1}) j++;
                                   ~~~~^~
rect.cpp:101:37: warning: narrowing conversion of '(((int)i.__gnu_cxx::__normal_iterator<SEGMENT*, std::vector<SEGMENT> >::operator->()->SEGMENT::t) - 1)' from 'int' to 'short int' inside { } [-Wnarrowing]
   if (*j == SEGMENT{i->a, i->b, i->t-1}) i->dp = j->dp+1;
                                 ~~~~^~
rect.cpp:112:59: warning: narrowing conversion of 'sy' from 'int' to 'short int' inside { } [-Wnarrowing]
    auto it = lower_bound(all(vertical), SEGMENT{sy, ey, ex});
                                                           ^
rect.cpp:112:59: warning: narrowing conversion of 'ey' from 'int' to 'short int' inside { } [-Wnarrowing]
rect.cpp:112:59: warning: narrowing conversion of 'ex' from 'int' to 'short int' inside { } [-Wnarrowing]
rect.cpp:113:57: warning: narrowing conversion of 'sy' from 'int' to 'short int' inside { } [-Wnarrowing]
    if (it == vertical.end() || *it != SEGMENT{sy, ey, ex} || it->dp <= ex-sx) continue;
                                                         ^
rect.cpp:113:57: warning: narrowing conversion of 'ey' from 'int' to 'short int' inside { } [-Wnarrowing]
rect.cpp:113:57: warning: narrowing conversion of 'ex' from 'int' to 'short int' inside { } [-Wnarrowing]
rect.cpp:116:61: warning: narrowing conversion of 'sx' from 'int' to 'short int' inside { } [-Wnarrowing]
    auto it = lower_bound(all(horizontal), SEGMENT{sx, ex, ey});
                                                             ^
rect.cpp:116:61: warning: narrowing conversion of 'ex' from 'int' to 'short int' inside { } [-Wnarrowing]
rect.cpp:116:61: warning: narrowing conversion of 'ey' from 'int' to 'short int' inside { } [-Wnarrowing]
rect.cpp:117:59: warning: narrowing conversion of 'sx' from 'int' to 'short int' inside { } [-Wnarrowing]
    if (it == horizontal.end() || *it != SEGMENT{sx, ex, ey} || it->dp <= ey-sy) continue;
                                                           ^
rect.cpp:117:59: warning: narrowing conversion of 'ex' from 'int' to 'short int' inside { } [-Wnarrowing]
rect.cpp:117:59: warning: narrowing conversion of 'ey' from 'int' to 'short int' inside { } [-Wnarrowing]
rect.cpp:119:28: warning: narrowing conversion of 'sy' from 'int' to 'short int' inside { } [-Wnarrowing]
   rects.pb({sy, sx, ey, ex});
                            ^
rect.cpp:119:28: warning: narrowing conversion of 'sx' from 'int' to 'short int' inside { } [-Wnarrowing]
rect.cpp:119:28: warning: narrowing conversion of 'ey' from 'int' to 'short int' inside { } [-Wnarrowing]
rect.cpp:119:28: warning: narrowing conversion of 'ex' from 'int' to 'short int' inside { } [-Wnarrowing]
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...