#include <bits/stdc++.h> #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define st first #define nd second using namespace std; #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " using ll = long long; using pii = pair<int,int>; using pll = pair<ll,ll>; using vi = vector<int>; using vll = vector<ll>; const int kSizeTab = 11; struct SegTree { struct Node { int min_elem, add_push; int cnts[kSizeTab]; Node() : min_elem{0}, add_push{0} { memset(cnts, 0, sizeof(cnts)); } //~ void Dbg() const { //~ debug() << "dbg push=" << add_push << " " << min_elem << " " << range(cnts, cnts + kSizeTab); //~ } Node Merge(const Node &rhs) const { Node ans; ans.min_elem = min(min_elem, rhs.min_elem); for (int i = 0; i < kSizeTab; ++i) { const int src_lhs = min_elem - ans.min_elem + i; if (src_lhs < kSizeTab) { ans.cnts[src_lhs] += cnts[i]; } const int src_rhs = rhs.min_elem - ans.min_elem + i; if (src_rhs < kSizeTab) { ans.cnts[src_rhs] += rhs.cnts[i]; } } //~ debug() << "merge"; //~ Dbg(); //~ rhs.Dbg(); //~ ans.Dbg(); return ans; } }; vector<Node> nodes; int base; int lq, rq, elemq; SegTree(int n) : base{1} { while (base < n + 2) { base *= 2; } nodes.resize(base * 2); for (int i = base; i < 2 * base; ++i) { nodes[i].cnts[0] = 1; } for (int i = base - 1; i > 0; --i) { nodes[i].cnts[0] = nodes[2*i].cnts[0] + nodes[2*i+1].cnts[0]; } } //~ void DbgTree() { //~ for (int i = 1; i < base * 2; ++i) { //~ cerr << i << ": "; //~ nodes[i].Dbg(); //~ } //~ } void Push(int v) { if (v < base) { for (int s : {v * 2, v * 2 + 1}) { nodes[s].min_elem += nodes[v].add_push; nodes[s].add_push += nodes[v].add_push; } } nodes[v].add_push = 0; } void AddIntvRec(int lb, int rb, int v) { if (lb >= rq || rb <= lq) { return; } if (lq <= lb && rb <= rq) { nodes[v].min_elem += elemq; nodes[v].add_push += elemq; return; } Push(v); const int midb = (lb + rb) / 2; AddIntvRec(lb, midb, v * 2); AddIntvRec(midb, rb, v * 2 + 1); nodes[v] = nodes[v * 2].Merge(nodes[v * 2 + 1]); } void AddIntv(int L, int R, int what) { debug() << "add" << imie(L) << imie(R) << imie(what); lq = L; rq = R; elemq = what; AddIntvRec(0, base, 1); } Node GetIntvRec(int lb, int rb, int v) { if (lb >= rq || rb <= lq) { return Node(); } if (lq <= lb && rb <= rq) { return nodes[v]; } Push(v); const int midb = (lb + rb) / 2; auto lhs = GetIntvRec(lb, midb, v * 2); auto rhs = GetIntvRec(midb, rb, v * 2 + 1); return lhs.Merge(rhs); } Node GetIntv(int L, int R) { lq = L; rq = R; return Node{}.Merge(GetIntvRec(0, base, 1)); } }; struct Event { int L, R, value; }; int n, k; vector<vi> board; vector<vector<Event>> events; void AddRectangle(int x1, int x2, int y1, int y2) { debug() << imie(x1) << imie(x2) << imie(y1) << imie(y2); events[x1].push_back(Event{y1, y2 + 1, +1}); events[x2 + 1].push_back(Event{y1, y2 + 1, -1}); } // add a rectangle containing all points (L,R) where [L,R] contains // all points from X and no points from Y void AddRectangleFromPoints(vi X, vi Y) { assert(!X.empty()); const int min_x = *min_element(ALL(X)); const int max_x = *max_element(ALL(X)); int y1 = 0, y2 = 2 * n + 1; for (int y : Y) { if (min_x <= y && y <= max_x) { return; } else if (y < min_x) { y1 = max(y1, y); } else if (y > max_x) { y2 = min(y2, y); } } AddRectangle(y1 + 1, min_x, max_x, y2 - 1); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(11); cerr << fixed << setprecision(6); cin >> n >> k; board.resize(2, vi(n)); for (auto &row : board) { for (int &x : row) { cin >> x; } } events.resize(2 * n + 5); for (int col = 0; col < n; ++col) { const int ncol = (col + 1) % n; AddRectangleFromPoints({board[0][col]}, {board[1][col], board[0][ncol]}); AddRectangleFromPoints({board[1][col]}, {board[0][col], board[1][ncol]}); AddRectangleFromPoints({board[0][col], board[1][col]}, {board[0][ncol], board[1][ncol]}); } SegTree st(2 * n + 1); vll answers(k + 1); for (int x = 1; x <= 2 * n; ++x) { for (const auto &event : events[x]) { st.AddIntv(event.L, event.R, event.value); } //~ st.DbgTree(); auto info = st.GetIntv(x, 2 * n + 1); //~ info.Dbg(); assert(info.min_elem == 0); for (int i = 0; i <= k; ++i) { answers[i] += info.cnts[i]; } } answers[1] += answers[0]; for (int i = 1; i <= k; ++i) { cout << answers[i] << " "; } cout << "\n"; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 | #include <bits/stdc++.h> #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define st first #define nd second using namespace std; #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " using ll = long long; using pii = pair<int,int>; using pll = pair<ll,ll>; using vi = vector<int>; using vll = vector<ll>; const int kSizeTab = 11; struct SegTree { struct Node { int min_elem, add_push; int cnts[kSizeTab]; Node() : min_elem{0}, add_push{0} { memset(cnts, 0, sizeof(cnts)); } //~ void Dbg() const { //~ debug() << "dbg push=" << add_push << " " << min_elem << " " << range(cnts, cnts + kSizeTab); //~ } Node Merge(const Node &rhs) const { Node ans; ans.min_elem = min(min_elem, rhs.min_elem); for (int i = 0; i < kSizeTab; ++i) { const int src_lhs = min_elem - ans.min_elem + i; if (src_lhs < kSizeTab) { ans.cnts[src_lhs] += cnts[i]; } const int src_rhs = rhs.min_elem - ans.min_elem + i; if (src_rhs < kSizeTab) { ans.cnts[src_rhs] += rhs.cnts[i]; } } //~ debug() << "merge"; //~ Dbg(); //~ rhs.Dbg(); //~ ans.Dbg(); return ans; } }; vector<Node> nodes; int base; int lq, rq, elemq; SegTree(int n) : base{1} { while (base < n + 2) { base *= 2; } nodes.resize(base * 2); for (int i = base; i < 2 * base; ++i) { nodes[i].cnts[0] = 1; } for (int i = base - 1; i > 0; --i) { nodes[i].cnts[0] = nodes[2*i].cnts[0] + nodes[2*i+1].cnts[0]; } } //~ void DbgTree() { //~ for (int i = 1; i < base * 2; ++i) { //~ cerr << i << ": "; //~ nodes[i].Dbg(); //~ } //~ } void Push(int v) { if (v < base) { for (int s : {v * 2, v * 2 + 1}) { nodes[s].min_elem += nodes[v].add_push; nodes[s].add_push += nodes[v].add_push; } } nodes[v].add_push = 0; } void AddIntvRec(int lb, int rb, int v) { if (lb >= rq || rb <= lq) { return; } if (lq <= lb && rb <= rq) { nodes[v].min_elem += elemq; nodes[v].add_push += elemq; return; } Push(v); const int midb = (lb + rb) / 2; AddIntvRec(lb, midb, v * 2); AddIntvRec(midb, rb, v * 2 + 1); nodes[v] = nodes[v * 2].Merge(nodes[v * 2 + 1]); } void AddIntv(int L, int R, int what) { debug() << "add" << imie(L) << imie(R) << imie(what); lq = L; rq = R; elemq = what; AddIntvRec(0, base, 1); } Node GetIntvRec(int lb, int rb, int v) { if (lb >= rq || rb <= lq) { return Node(); } if (lq <= lb && rb <= rq) { return nodes[v]; } Push(v); const int midb = (lb + rb) / 2; auto lhs = GetIntvRec(lb, midb, v * 2); auto rhs = GetIntvRec(midb, rb, v * 2 + 1); return lhs.Merge(rhs); } Node GetIntv(int L, int R) { lq = L; rq = R; return Node{}.Merge(GetIntvRec(0, base, 1)); } }; struct Event { int L, R, value; }; int n, k; vector<vi> board; vector<vector<Event>> events; void AddRectangle(int x1, int x2, int y1, int y2) { debug() << imie(x1) << imie(x2) << imie(y1) << imie(y2); events[x1].push_back(Event{y1, y2 + 1, +1}); events[x2 + 1].push_back(Event{y1, y2 + 1, -1}); } // add a rectangle containing all points (L,R) where [L,R] contains // all points from X and no points from Y void AddRectangleFromPoints(vi X, vi Y) { assert(!X.empty()); const int min_x = *min_element(ALL(X)); const int max_x = *max_element(ALL(X)); int y1 = 0, y2 = 2 * n + 1; for (int y : Y) { if (min_x <= y && y <= max_x) { return; } else if (y < min_x) { y1 = max(y1, y); } else if (y > max_x) { y2 = min(y2, y); } } AddRectangle(y1 + 1, min_x, max_x, y2 - 1); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(11); cerr << fixed << setprecision(6); cin >> n >> k; board.resize(2, vi(n)); for (auto &row : board) { for (int &x : row) { cin >> x; } } events.resize(2 * n + 5); for (int col = 0; col < n; ++col) { const int ncol = (col + 1) % n; AddRectangleFromPoints({board[0][col]}, {board[1][col], board[0][ncol]}); AddRectangleFromPoints({board[1][col]}, {board[0][col], board[1][ncol]}); AddRectangleFromPoints({board[0][col], board[1][col]}, {board[0][ncol], board[1][ncol]}); } SegTree st(2 * n + 1); vll answers(k + 1); for (int x = 1; x <= 2 * n; ++x) { for (const auto &event : events[x]) { st.AddIntv(event.L, event.R, event.value); } //~ st.DbgTree(); auto info = st.GetIntv(x, 2 * n + 1); //~ info.Dbg(); assert(info.min_elem == 0); for (int i = 0; i <= k; ++i) { answers[i] += info.cnts[i]; } } answers[1] += answers[0]; for (int i = 1; i <= k; ++i) { cout << answers[i] << " "; } cout << "\n"; } |