#pragma GCC optimize("O3") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; using namespace std; #define PB push_back #define MP make_pair #define LL long long #define int LL #define FOR(i,a,b) for(int32_t i = (a); i <= (b); i++) #define RE(i,n) FOR(i,1,n) #define REP(i,n) FOR(i,0,(int)(n)-1) #define R(i,n) REP(i,n) #define VI vector<int> #define PII pair<int,int> #define LD long double #define FI first #define SE second #define st FI #define nd SE #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) template<class C> void mini(C &a4, C b4) { a4 = min(a4, b4); } template<class C> void maxi(C &a4, C b4) { a4 = max(a4, b4); } template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; } template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while(*sdbg!=',')cerr<<*sdbg++; cerr<<'='<<h<<','; _dbg(sdbg+1, a...); } template<class T> ostream &operator<<(ostream& os, vector<T> V) { os << "["; for (auto vv : V) os << vv << ","; return os << "]"; } template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) { return os << "(" << P.st << "," << P.nd << ")"; } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) (__VA_ARGS__) #define cerr if(0)cout #endif void zasiegi(vector<int32_t>& t, vector<int32_t>& a, vector<int32_t> &z){ int32_t ma = -1; int32_t n = SZ(t); z.resize(n); R(i,n){ if(ma < t[i]){ ma = t[i]; a.PB(i); } z[i] = SZ(a) - 1; } } vector<int32_t> t; struct Sol{ int32_t n; vector<int32_t> a; vector<int32_t> b; vector<int32_t> za; vector<int32_t> zb; vector<int32_t> l; vector<int32_t> r; vector<int32_t> val; int32_t nodes = 1; int32_t nn; int32_t gd; int32_t add(int32_t v, int32_t po, int32_t ko){ int32_t ak = nodes; nodes++; if(v != -1){ val[ak] = val[v]; l[ak] = l[v]; r[ak] = r[v]; } val[ak]++; if(po + 1 == ko)return ak; int32_t m = (po + ko) / 2; if(gd < m) l[ak] = add(l[ak], po, (po + ko)/2); else r[ak] = add(r[ak], (po + ko)/2, ko); return ak; } int32_t sum(int32_t v){ //sumuje ostro wieksze niz gd int32_t res = 0; int32_t po = 0; int32_t ko = nn; //po <= gd < ko while(v != -1){ int32_t m = (po + ko) >> 1; if(m <= gd){ po = m; v = r[v]; }else{ ko = m; if(r[v] != -1) res += val[r[v]]; v = l[v]; } } return res; } vector<int> pamy; int sumujy(int32_t y){ int32_t x = za[b[y]]; if(x == SZ(a)-1 || y == SZ(b)-1)return 0; int &res = pamy[y]; if(res != -1)return res; res = sumujx(x); gd = t[a[x]]; res += sum(roots[n-b[y]-1]); return res; } vector<int> pamx; int sumujx(int32_t x){ int32_t y = zb[a[x]]; if(x == SZ(a)-1 || y == SZ(b)-1)return 0; int &res = pamx[x]; if(res != -1)return res; res = sumujy(y); gd = t[a[x]]; res += sum(roots[n-b[y]-1]); return res; } vector<gp_hash_table<int32_t, int>> mapka; int sumuj(int32_t x,int32_t y){ if(x == SZ(a)-1 || y == SZ(b)-1)return 0; int &res = mapka[x][y]; if(res)return res; gd = t[a[x]]; res = sum(roots[n-b[y]-1]); if(za[b[y]] == x){ res += sumujx(x); }else if(zb[a[x]] == y){ res += sumujy(y); }else res += sumuj(za[b[y]],zb[a[x]]); return res; } vector<int32_t> roots; vector<int> run(){ n = SZ(t); pamx.resize(n,-1); pamy.resize(n,-1); if(n == 1)return vector<int>(1); mapka.resize(n); vector<int32_t> trev(n); zasiegi(t, a, za); R(i,n)trev[t[i]] = i; vector<int32_t> zpom; zasiegi(trev, b, zpom); R(i,SZ(b))b[i] = trev[b[i]]; zb.resize(n); R(i,n)zb[trev[i]] = zpom[i]; nn = 1; while(nn <= n)nn *= 2; roots.PB(0); val.resize(n * 20); l.resize(n * 20,-1); r.resize(n * 20,-1); vector<int> res(n); for(int32_t i = n-1; i >= 0; i--){ gd = t[i]; res[i] = sum(roots.back()); roots.PB(add(roots.back(), 0, nn)); } R(i,n){ res[i] += sumuj(za[i], zb[i]); } return res; } }; vector<int> run(){ Sol sol; return sol.run(); } // vector<int> generuj(){ // int n = 100000; // int kw = 182; // int id = 0; // vector<int> x(n),y(n); // R(i,kw)R(j,kw){ // x[id] = 3 * i + 2; // y[id] = 3 * j + 2; // id++; // } // int ak = 0; // while(id != n){ // x[id] = y[id + 1] = ak; // y[id] = x[id + 1] = ak + kw * 3 + 1; // ak += 3; // id+=2; // } // vector<int> p(n),q(n),prev(n),qrev(n); // R(i,n)p[i] = i; // random_shuffle(ALL(p)); // sort(ALL(p),[&](int a,int b){return y[a] < y[b];}); // R(i,n)prev[p[i]] = i; // R(i,n)q[i] = i; // random_shuffle(ALL(q)); // sort(ALL(q),[&](int a,int b){return x[a] < x[b];}); // R(i,n)qrev[q[i]] = i; // vector<int> res(n); // R(i,n){ // res[prev[i]] = qrev[i]; // } // return res; // } int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(11); cerr << fixed << setprecision(6); int n; cin >> n; // vector<int> x = generuj(); // n = SZ(x); int offset = 0; int ma = 0; R(i,n){ int pom; cin >> pom; pom--; // pom = x[i]; maxi(ma,pom); t.PB(pom-offset); if(ma == i){ int m = SZ(t); vector<int> res1 = run(); reverse(ALL(t)); R(j,m){ t[j] = m - t[j] - 1; } vector<int> res2 = run(); R(j,m){ cout << res1[j] + res2[m - j - 1] + m - 1 << " "; } cerr << "\n"; offset += SZ(t); t.clear(); } } 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 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | #pragma GCC optimize("O3") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; using namespace std; #define PB push_back #define MP make_pair #define LL long long #define int LL #define FOR(i,a,b) for(int32_t i = (a); i <= (b); i++) #define RE(i,n) FOR(i,1,n) #define REP(i,n) FOR(i,0,(int)(n)-1) #define R(i,n) REP(i,n) #define VI vector<int> #define PII pair<int,int> #define LD long double #define FI first #define SE second #define st FI #define nd SE #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) template<class C> void mini(C &a4, C b4) { a4 = min(a4, b4); } template<class C> void maxi(C &a4, C b4) { a4 = max(a4, b4); } template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; } template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while(*sdbg!=',')cerr<<*sdbg++; cerr<<'='<<h<<','; _dbg(sdbg+1, a...); } template<class T> ostream &operator<<(ostream& os, vector<T> V) { os << "["; for (auto vv : V) os << vv << ","; return os << "]"; } template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) { return os << "(" << P.st << "," << P.nd << ")"; } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) (__VA_ARGS__) #define cerr if(0)cout #endif void zasiegi(vector<int32_t>& t, vector<int32_t>& a, vector<int32_t> &z){ int32_t ma = -1; int32_t n = SZ(t); z.resize(n); R(i,n){ if(ma < t[i]){ ma = t[i]; a.PB(i); } z[i] = SZ(a) - 1; } } vector<int32_t> t; struct Sol{ int32_t n; vector<int32_t> a; vector<int32_t> b; vector<int32_t> za; vector<int32_t> zb; vector<int32_t> l; vector<int32_t> r; vector<int32_t> val; int32_t nodes = 1; int32_t nn; int32_t gd; int32_t add(int32_t v, int32_t po, int32_t ko){ int32_t ak = nodes; nodes++; if(v != -1){ val[ak] = val[v]; l[ak] = l[v]; r[ak] = r[v]; } val[ak]++; if(po + 1 == ko)return ak; int32_t m = (po + ko) / 2; if(gd < m) l[ak] = add(l[ak], po, (po + ko)/2); else r[ak] = add(r[ak], (po + ko)/2, ko); return ak; } int32_t sum(int32_t v){ //sumuje ostro wieksze niz gd int32_t res = 0; int32_t po = 0; int32_t ko = nn; //po <= gd < ko while(v != -1){ int32_t m = (po + ko) >> 1; if(m <= gd){ po = m; v = r[v]; }else{ ko = m; if(r[v] != -1) res += val[r[v]]; v = l[v]; } } return res; } vector<int> pamy; int sumujy(int32_t y){ int32_t x = za[b[y]]; if(x == SZ(a)-1 || y == SZ(b)-1)return 0; int &res = pamy[y]; if(res != -1)return res; res = sumujx(x); gd = t[a[x]]; res += sum(roots[n-b[y]-1]); return res; } vector<int> pamx; int sumujx(int32_t x){ int32_t y = zb[a[x]]; if(x == SZ(a)-1 || y == SZ(b)-1)return 0; int &res = pamx[x]; if(res != -1)return res; res = sumujy(y); gd = t[a[x]]; res += sum(roots[n-b[y]-1]); return res; } vector<gp_hash_table<int32_t, int>> mapka; int sumuj(int32_t x,int32_t y){ if(x == SZ(a)-1 || y == SZ(b)-1)return 0; int &res = mapka[x][y]; if(res)return res; gd = t[a[x]]; res = sum(roots[n-b[y]-1]); if(za[b[y]] == x){ res += sumujx(x); }else if(zb[a[x]] == y){ res += sumujy(y); }else res += sumuj(za[b[y]],zb[a[x]]); return res; } vector<int32_t> roots; vector<int> run(){ n = SZ(t); pamx.resize(n,-1); pamy.resize(n,-1); if(n == 1)return vector<int>(1); mapka.resize(n); vector<int32_t> trev(n); zasiegi(t, a, za); R(i,n)trev[t[i]] = i; vector<int32_t> zpom; zasiegi(trev, b, zpom); R(i,SZ(b))b[i] = trev[b[i]]; zb.resize(n); R(i,n)zb[trev[i]] = zpom[i]; nn = 1; while(nn <= n)nn *= 2; roots.PB(0); val.resize(n * 20); l.resize(n * 20,-1); r.resize(n * 20,-1); vector<int> res(n); for(int32_t i = n-1; i >= 0; i--){ gd = t[i]; res[i] = sum(roots.back()); roots.PB(add(roots.back(), 0, nn)); } R(i,n){ res[i] += sumuj(za[i], zb[i]); } return res; } }; vector<int> run(){ Sol sol; return sol.run(); } // vector<int> generuj(){ // int n = 100000; // int kw = 182; // int id = 0; // vector<int> x(n),y(n); // R(i,kw)R(j,kw){ // x[id] = 3 * i + 2; // y[id] = 3 * j + 2; // id++; // } // int ak = 0; // while(id != n){ // x[id] = y[id + 1] = ak; // y[id] = x[id + 1] = ak + kw * 3 + 1; // ak += 3; // id+=2; // } // vector<int> p(n),q(n),prev(n),qrev(n); // R(i,n)p[i] = i; // random_shuffle(ALL(p)); // sort(ALL(p),[&](int a,int b){return y[a] < y[b];}); // R(i,n)prev[p[i]] = i; // R(i,n)q[i] = i; // random_shuffle(ALL(q)); // sort(ALL(q),[&](int a,int b){return x[a] < x[b];}); // R(i,n)qrev[q[i]] = i; // vector<int> res(n); // R(i,n){ // res[prev[i]] = qrev[i]; // } // return res; // } int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(11); cerr << fixed << setprecision(6); int n; cin >> n; // vector<int> x = generuj(); // n = SZ(x); int offset = 0; int ma = 0; R(i,n){ int pom; cin >> pom; pom--; // pom = x[i]; maxi(ma,pom); t.PB(pom-offset); if(ma == i){ int m = SZ(t); vector<int> res1 = run(); reverse(ALL(t)); R(j,m){ t[j] = m - t[j] - 1; } vector<int> res2 = run(); R(j,m){ cout << res1[j] + res2[m - j - 1] + m - 1 << " "; } cerr << "\n"; offset += SZ(t); t.clear(); } } cout << "\n"; } |