#pragma GCC optimize("O3") #include <bits/stdc++.h> // #include <ext/pb_ds/assoc_container.hpp> // using namespace __gnu_pbds; // gp_hash_table<int, int> mapka; using namespace std; #define PB push_back #define MP make_pair #define LL long long #define int LL #define FOR(i,a,b) for(int 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 int n,k; vector<int> x,y; int miedzy = 0; vector<int> ilemie; vector<int> ak[2]; vector<int> t[2]; void dodaj(int v){ int i = x[v]; int j = y[v]; if(ak[i][(j+1) % n]){ if(ilemie[j] == 0)miedzy++; ilemie[j]++; } int pop = (j + n -1) % n; if(ak[i][pop]){ if(ilemie[pop] == 0)miedzy++; ilemie[pop]++; } ak[i][j] = 1; } void us(int v){ int i = x[v]; int j = y[v]; if(ak[i][(j+1) % n]){ ilemie[j]--; if(ilemie[j] == 0)miedzy--; } int pop = (j + n -1) % n; if(ak[i][pop]){ ilemie[pop]--; if(ilemie[pop] == 0)miedzy--; } ak[i][j] = 0; } vector<int> sum; vector<int> mi; vector<vector<int>> ile; vector<vector<PII>> d; void add_edge(int a,int b,int val){ if(a > b)swap(a,b); debug("add_edge",a + 1,b + 1,val); d[a].PB({b,val}); } void akuj(int i){ sum[i] = sum[i * 2] + sum[i * 2 + 1]; int diff = mi[i * 2] - (mi[i*2 + 1] + sum[i * 2]); if(diff <= 0){ mi[i] = mi[i * 2]; ile[i] = ile[i * 2]; diff *= -1; R(j,k-diff) ile[i][j + diff] += ile[i * 2 + 1][j]; }else{ mi[i] = mi[i * 2 + 1] + sum[i * 2]; ile[i] = ile[i * 2 + 1]; R(j,k-diff){ ile[i][j + diff] += ile[i * 2][j]; } } } int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(11); cerr << fixed << setprecision(6); cin >> n >> k; vector<int> res(k); x.resize(2 * n); y.resize(2 * n); R(i,2){ t[i].resize(n); R(j,n){ int pom; cin >> pom; pom--; x[pom] = i; y[pom] = j; t[i][j] = pom; } } vector<int> kon(2 * n); { int j = 0; ilemie.resize(n); R(i,2)ak[i].resize(n); R(i,n * 2){ while(j != 2 * n && miedzy != n){ dodaj(j); j++; } if(miedzy == n){ debug(i,j); res[0] += 2 * n - j + 1; kon[i] = j-1; }else{ kon[i] = 2 * n; } us(i); } } debug(kon,res[0]); R(i,2)t[i].PB(t[i][0]); d.resize(2*n); debug(t[0],t[1]); R(i,n){ add_edge(t[0][i],t[1][i],-1); add_edge(t[0][i],t[0][i+1],-1); add_edge(t[1][i],t[1][i+1],-1); vector<int> kw; R(ii,2)R(jj,2){ kw.PB(t[ii][i+jj]); } sort(ALL(kw)); add_edge(kw[0],kw[3],1); } int nn = 1;while(nn <= 2 * n)nn *= 2; sum.resize(nn * 2); ile.resize(nn * 2, vector<int>(k)); mi.resize(nn * 2); R(i,nn){ ile[i+nn][0] = 1; sum[i+nn] = 1; mi[i+nn] = 0; } for(int i = nn-1;i;i--){ akuj(i); } for(int i = 2*n-1;i>=0;i--){ debug("--------------------", i); for(PII el:d[i]){ debug("kra",i,el); int v = el.FI + nn; sum[v] += el.SE; mi[v] += el.SE; while(v!=1){ v/=2; akuj(v); } } int bil = 0; function<void(int,int,int)> rec = [&](int v,int po,int ko){ if(i >= ko || po >= kon[i])return; if(i <= po && ko <= kon[i]){ int pom = bil + mi[v]; assert(pom >= 0); R(j,k-pom) res[j + pom] += ile[v][j]; bil += sum[v]; return; } rec(v * 2, po, (po + ko)/2); rec(v * 2 + 1, (po + ko)/2, ko); }; rec(1,0,nn); debug(res); } R(i,k){ cout << res[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 | #pragma GCC optimize("O3") #include <bits/stdc++.h> // #include <ext/pb_ds/assoc_container.hpp> // using namespace __gnu_pbds; // gp_hash_table<int, int> mapka; using namespace std; #define PB push_back #define MP make_pair #define LL long long #define int LL #define FOR(i,a,b) for(int 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 int n,k; vector<int> x,y; int miedzy = 0; vector<int> ilemie; vector<int> ak[2]; vector<int> t[2]; void dodaj(int v){ int i = x[v]; int j = y[v]; if(ak[i][(j+1) % n]){ if(ilemie[j] == 0)miedzy++; ilemie[j]++; } int pop = (j + n -1) % n; if(ak[i][pop]){ if(ilemie[pop] == 0)miedzy++; ilemie[pop]++; } ak[i][j] = 1; } void us(int v){ int i = x[v]; int j = y[v]; if(ak[i][(j+1) % n]){ ilemie[j]--; if(ilemie[j] == 0)miedzy--; } int pop = (j + n -1) % n; if(ak[i][pop]){ ilemie[pop]--; if(ilemie[pop] == 0)miedzy--; } ak[i][j] = 0; } vector<int> sum; vector<int> mi; vector<vector<int>> ile; vector<vector<PII>> d; void add_edge(int a,int b,int val){ if(a > b)swap(a,b); debug("add_edge",a + 1,b + 1,val); d[a].PB({b,val}); } void akuj(int i){ sum[i] = sum[i * 2] + sum[i * 2 + 1]; int diff = mi[i * 2] - (mi[i*2 + 1] + sum[i * 2]); if(diff <= 0){ mi[i] = mi[i * 2]; ile[i] = ile[i * 2]; diff *= -1; R(j,k-diff) ile[i][j + diff] += ile[i * 2 + 1][j]; }else{ mi[i] = mi[i * 2 + 1] + sum[i * 2]; ile[i] = ile[i * 2 + 1]; R(j,k-diff){ ile[i][j + diff] += ile[i * 2][j]; } } } int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(11); cerr << fixed << setprecision(6); cin >> n >> k; vector<int> res(k); x.resize(2 * n); y.resize(2 * n); R(i,2){ t[i].resize(n); R(j,n){ int pom; cin >> pom; pom--; x[pom] = i; y[pom] = j; t[i][j] = pom; } } vector<int> kon(2 * n); { int j = 0; ilemie.resize(n); R(i,2)ak[i].resize(n); R(i,n * 2){ while(j != 2 * n && miedzy != n){ dodaj(j); j++; } if(miedzy == n){ debug(i,j); res[0] += 2 * n - j + 1; kon[i] = j-1; }else{ kon[i] = 2 * n; } us(i); } } debug(kon,res[0]); R(i,2)t[i].PB(t[i][0]); d.resize(2*n); debug(t[0],t[1]); R(i,n){ add_edge(t[0][i],t[1][i],-1); add_edge(t[0][i],t[0][i+1],-1); add_edge(t[1][i],t[1][i+1],-1); vector<int> kw; R(ii,2)R(jj,2){ kw.PB(t[ii][i+jj]); } sort(ALL(kw)); add_edge(kw[0],kw[3],1); } int nn = 1;while(nn <= 2 * n)nn *= 2; sum.resize(nn * 2); ile.resize(nn * 2, vector<int>(k)); mi.resize(nn * 2); R(i,nn){ ile[i+nn][0] = 1; sum[i+nn] = 1; mi[i+nn] = 0; } for(int i = nn-1;i;i--){ akuj(i); } for(int i = 2*n-1;i>=0;i--){ debug("--------------------", i); for(PII el:d[i]){ debug("kra",i,el); int v = el.FI + nn; sum[v] += el.SE; mi[v] += el.SE; while(v!=1){ v/=2; akuj(v); } } int bil = 0; function<void(int,int,int)> rec = [&](int v,int po,int ko){ if(i >= ko || po >= kon[i])return; if(i <= po && ko <= kon[i]){ int pom = bil + mi[v]; assert(pom >= 0); R(j,k-pom) res[j + pom] += ile[v][j]; bil += sum[v]; return; } rec(v * 2, po, (po + ko)/2); rec(v * 2 + 1, (po + ko)/2, ko); }; rec(1,0,nn); debug(res); } R(i,k){ cout << res[i] << " "; } cout << "\n"; } |