Submission #725127

#TimeUsernameProblemLanguageResultExecution timeMemory
725127QwertyPiRectangles (IOI19_rect)C++14
72 / 100
4214 ms687336 KiB
#include "rect.h" #include <bits/stdc++.h> #pragma GCC optimize("unroll-loops") #pragma GCC target("avx2") #define fi first #define se second #define pii pair<int, int> using namespace std; const int N = 2500 + 11; struct DSU{ int dsu[N], l[N], r[N]; void init(int n){ for(int i = 0; i <= n + 2; i++) dsu[i] = l[i] = r[i] = i; } int root(int x){ if(x == dsu[x]) return x; return dsu[x] = root(dsu[x]); } void join(int x, int y){ x = root(x), y = root(y); if(x == y) return; dsu[y] = x; l[x] = min(l[x], l[y]); r[x] = max(r[x], r[y]); } } dsu; int at(const vector<int>& v, int idx){ if(idx < 0 || idx >= (int) v.size()) return -(1 << 30); return v[idx]; } vector<pii> build(const vector<int>& v){ vector<pii> res; vector<pii> vp; int n = v.size(); for(int i = 0; i < n; i++){ vp.push_back({v[i], i}); } sort(vp.begin(), vp.end()); dsu.init(n); for(auto i : vp){ dsu.join(i.se, i.se + 1); int rt = dsu.root(i.se); int b_l = dsu.l[rt], b_r = dsu.r[rt] - 1; if(at(v, b_l - 1) > i.fi && at(v, b_r + 1) > i.fi){ res.push_back({b_l, b_r}); } } return res; } struct Rect{ int16_t x1, x2, y1, y2; void pr() { printf("%d %d %d %d\n", x1, x2, y1, y2); } friend Rect operator+ (const Rect& a, const Rect& b){ return {max(a.x1, b.x1), min(a.x2, b.x2), max(a.y1, b.y1), min(a.y2, b.y2)}; } }; void split(const vector<Rect>& a, vector<Rect>& al, vector<Rect>& ar, int xm){ for(auto r : a) { if(r.x1 <= xm) al.push_back(r); else ar.push_back(r); } } long long ans = 0; struct BIT2D{ int bit[N][N]; void add(int x, int y, int v){ x++; y++; for(int i = x; i < N; i += i & -i){ for(int j = y; j < N; j += j & -j){ bit[i][j] += v; } } } void qry(int x, int y){ x++; y++; for(int i = x; i; i -= i & -i){ for(int j = y; j; j -= j & -j){ ans += bit[i][j]; } } } } bit2; void calc(const vector<Rect>& ad, const vector<Rect>& qr){ int l = 0, r = 0; while(l + r < (int) ad.size() + (int) qr.size()){ if(r == (int) qr.size() || (l != (int) ad.size() && ad[l].x2 <= qr[r].x2)){ bit2.add(ad[l].y1, ad[l].y2, 1); l++; }else{ bit2.qry(qr[r].y1, qr[r].y2); r++; } } for(int l = 0; l < (int) ad.size(); l++){ bit2.add(ad[l].y1, ad[l].y2, -1); } } void solve(int xl, int xr, vector<Rect>& A, vector<Rect>& B) { if(xl == xr){ calc(A, B); }else{ int xm = (xl + xr) / 2; vector<Rect> AL, AR, BL, BR; split(A, AL, AR, xm), split(B, BL, BR, xm); solve(xl, xm, AL, BL), solve(xm + 1, xr, AR, BR), calc(AL, BR); } } vector<int> xs[N * N], ys[N * N]; vector<Rect> xr, yr; vector<pii> x[N], y[N]; void counting_sort(vector<Rect>& a){ vector<Rect> v[N]; for(auto& r : a) v[r.x2].push_back(r); a.clear(); for(int i = 0; i < N; i++){ for(auto& r : v[i]) a.push_back(r); } } long long count_rectangles(std::vector<std::vector<int>> a) { int n = a.size(), m = a[0].size(), mx = 0; for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) mx = max(mx, a[i][j]); for(int i = 0; i < n; i++){ vector<int> v; for(int j = 0; j < m; j++) v.push_back(a[i][j]); x[i] = build(v); for(pii p : x[i]){ xs[p.fi * m + p.se].push_back(i); } } for(int i = 0; i < m * m; i++){ int x1 = i / m, x2 = i % m; if(x1 > x2) continue; int y1 = -2, y2 = -2; for(auto j : xs[i]){ if(j == y2 + 1){ y2++; }else{ if(y1 != -2) xr.push_back({x1, x2, y1, y2}); y1 = y2 = j; } } if(y1 != -2) xr.push_back({x1, x2, y1, y2}); } for(int j = 0; j < m; j++){ vector<int> v; for(int i = 0; i < n; i++) v.push_back(a[i][j]); y[j] = build(v); for(pii p : y[j]){ ys[p.fi * n + p.se].push_back(j); } } for(int i = 0; i < n * n; i++){ int y1 = i / n, y2 = i % n; if(y1 > y2) continue; int x1 = -2, x2 = -2; for(auto j : ys[i]){ if(j == x2 + 1){ x2++; }else{ if(x1 != -2) yr.push_back({x1, x2, y1, y2}); x1 = x2 = j; } } if(x1 != -2) yr.push_back({x1, x2, y1, y2}); } for(auto& r1 : xr) r1.x1 = m - 1 - r1.x1, r1.y2 = n - 1 - r1.y2; for(auto& r2 : yr) r2.x1 = m - 1 - r2.x1, r2.y2 = n - 1 - r2.y2; counting_sort(xr); counting_sort(yr); if(n <= 1000 || mx <= 1) solve(0, m - 1, xr, yr); return ans; }

Compilation message (stderr)

rect.cpp: In function 'long long int count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:145:32: warning: narrowing conversion of 'x1' from 'int' to 'int16_t' {aka 'short int'} [-Wnarrowing]
  145 |     if(y1 != -2) xr.push_back({x1, x2, y1, y2});
      |                                ^~
rect.cpp:145:36: warning: narrowing conversion of 'x2' from 'int' to 'int16_t' {aka 'short int'} [-Wnarrowing]
  145 |     if(y1 != -2) xr.push_back({x1, x2, y1, y2});
      |                                    ^~
rect.cpp:145:40: warning: narrowing conversion of 'y1' from 'int' to 'int16_t' {aka 'short int'} [-Wnarrowing]
  145 |     if(y1 != -2) xr.push_back({x1, x2, y1, y2});
      |                                        ^~
rect.cpp:145:44: warning: narrowing conversion of 'y2' from 'int' to 'int16_t' {aka 'short int'} [-Wnarrowing]
  145 |     if(y1 != -2) xr.push_back({x1, x2, y1, y2});
      |                                            ^~
rect.cpp:149:30: warning: narrowing conversion of 'x1' from 'int' to 'int16_t' {aka 'short int'} [-Wnarrowing]
  149 |   if(y1 != -2) xr.push_back({x1, x2, y1, y2});
      |                              ^~
rect.cpp:149:34: warning: narrowing conversion of 'x2' from 'int' to 'int16_t' {aka 'short int'} [-Wnarrowing]
  149 |   if(y1 != -2) xr.push_back({x1, x2, y1, y2});
      |                                  ^~
rect.cpp:149:38: warning: narrowing conversion of 'y1' from 'int' to 'int16_t' {aka 'short int'} [-Wnarrowing]
  149 |   if(y1 != -2) xr.push_back({x1, x2, y1, y2});
      |                                      ^~
rect.cpp:149:42: warning: narrowing conversion of 'y2' from 'int' to 'int16_t' {aka 'short int'} [-Wnarrowing]
  149 |   if(y1 != -2) xr.push_back({x1, x2, y1, y2});
      |                                          ^~
rect.cpp:165:32: warning: narrowing conversion of 'x1' from 'int' to 'int16_t' {aka 'short int'} [-Wnarrowing]
  165 |     if(x1 != -2) yr.push_back({x1, x2, y1, y2});
      |                                ^~
rect.cpp:165:36: warning: narrowing conversion of 'x2' from 'int' to 'int16_t' {aka 'short int'} [-Wnarrowing]
  165 |     if(x1 != -2) yr.push_back({x1, x2, y1, y2});
      |                                    ^~
rect.cpp:165:40: warning: narrowing conversion of 'y1' from 'int' to 'int16_t' {aka 'short int'} [-Wnarrowing]
  165 |     if(x1 != -2) yr.push_back({x1, x2, y1, y2});
      |                                        ^~
rect.cpp:165:44: warning: narrowing conversion of 'y2' from 'int' to 'int16_t' {aka 'short int'} [-Wnarrowing]
  165 |     if(x1 != -2) yr.push_back({x1, x2, y1, y2});
      |                                            ^~
rect.cpp:169:30: warning: narrowing conversion of 'x1' from 'int' to 'int16_t' {aka 'short int'} [-Wnarrowing]
  169 |   if(x1 != -2) yr.push_back({x1, x2, y1, y2});
      |                              ^~
rect.cpp:169:34: warning: narrowing conversion of 'x2' from 'int' to 'int16_t' {aka 'short int'} [-Wnarrowing]
  169 |   if(x1 != -2) yr.push_back({x1, x2, y1, y2});
      |                                  ^~
rect.cpp:169:38: warning: narrowing conversion of 'y1' from 'int' to 'int16_t' {aka 'short int'} [-Wnarrowing]
  169 |   if(x1 != -2) yr.push_back({x1, x2, y1, y2});
      |                                      ^~
rect.cpp:169:42: warning: narrowing conversion of 'y2' from 'int' to 'int16_t' {aka 'short int'} [-Wnarrowing]
  169 |   if(x1 != -2) yr.push_back({x1, x2, y1, y2});
      |                                          ^~
#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...