#include <bits/stdc++.h> using namespace std; #define REP(i,a,b) for (int i = (a); i <= (b); ++i) #define REPD(i,a,b) for (int i = (a); i >= (b); --i) #define FORI(i,n) REP(i,1,n) #define FOR(i,n) REP(i,0,int(n)-1) #define mp make_pair #define pb push_back #define pii pair<int,int> #define vi vector<int> #define ll long long #define SZ(x) int((x).size()) #define DBG(v) cerr << #v << " = " << (v) << endl; #define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define fi first #define se second const int dx[4] = {0, 1, 0, -1}; const int dy[4] = {1, 0, -1, 0}; const int px[6] = { 0, 1, 0, 1, 0, 1}; const int py[6] = {-1, -1, 0, 0, 1, 1}; int p[11]; int fin(int x) { return p[x]==x ? x : p[x]=fin(p[x]); } bool uni(int x, int y) { fin(x); fin(y); if (p[x]==p[y]) return false; p[p[x]]=p[y]; return true; } const int N = 100100; int n,k; int a[2][N]; int nei[11], vis[11][11]; vi eve[N*7]; int en, ep; int val[N*2]; ll cnt[555]; int tot[N], upd[N]; ll cnts[555][3030]; int tmp[N*2]; int main() { scanf("%d%d", &n, &k); FOR(i,2) FOR(j,n) scanf("%d", &a[i][j]); //n=100000; k=10; //FOR(i,n*2) tmp[i]=i+1; //random_shuffle(tmp,tmp+n); //FOR(i,2) FOR(j,n) a[i][j]=tmp[i*n+j]; a[0][n] = a[0][0]; a[1][n] = a[1][0]; a[0][n+1] = a[0][1]; a[1][n+1] = a[1][1]; FOR(i,2) FORI(j,n) { FOR(c,6) nei[c] = a[px[c]][j+py[c]]; sort(nei, nei+6); int prv = -1; FOR(c,6) { // prv < L <= nei[c] => a[] <= prv off, prv < a[] < a[i][j] on, a[] > a[i][j] off FOR(w,6) vis[px[w]][py[w]+1] = 0; FOR(w,6) if (prv < a[px[w]][j+py[w]] && a[px[w]][j+py[w]] < a[i][j]) vis[px[w]][py[w]+1] = 1; int nv = 0; FOR(w,6) p[w]=w; //printf("i=%d, j=%d, range = (%d, %d]\n", i, j, prv, nei[c]); //FOR(ii,2) { // FOR(jj,3) printf("%d",vis[ii][jj]); // printf("\n"); //} FOR(w,6) if(vis[px[w]][py[w]+1]) { nv++; FOR(kk,4) { int x = px[w]+dx[kk], y = py[w]+dy[kk]+1; if (x<0 || x>1 || y<0 || y>2) continue; int w2 = y*2+x; if (vis[x][y]) nv -= uni(w,w2); } } //printf("p = "); //FOR(ii,6) printf("%d ", p[ii]); //printf("\nnv = %d\n", nv); int pv = nv; nv++; FOR(kk,4) { int x = i+dx[kk], y = dy[kk]+1; if (x<0 || x>1 || y<0 || y>2) continue; int w2 = y*2+x; //printf("kk=%d, join %d %d\n", kk, w2, 2+i); if (vis[x][y]) nv -= uni(2+i,w2); } //printf("p = "); //FOR(ii,6) printf("%d ", fin(p[ii])); //printf("\nnv = %d\n", nv); // prv < L <= nei[c] => a[i][j] is worth nv-pv //printf("%d < L <= %d => a[%d][%d] is worth %d\n", prv, nei[c], i, j, nv-pv); eve[en++] = {prv+1, i, j, nv-pv}; if (nei[c] == a[i][j]) break; prv = nei[c]; } } sort(eve, eve+en); int K = sqrt(n*2)+1, K2 = K*2+3, M = n*2/K+1; cerr << K << " " << M << " " << K*M << " " << n*2 << "\n"; FORI(L,n*2) { FOR(i,M) upd[i] = 0; while (ep < en && eve[ep][0] <= L) { int chgpos = a[eve[ep][1]][eve[ep][2]]; val[chgpos] = eve[ep][3]; upd[chgpos/K] = true; ep++; } val[L-1] = 0; upd[(L-1)/K] = true; //FOR(i,n*2+1) { // if (i==L) printf(" | "); // printf("%d ", val[i]); //} //printf("\n"); FOR(i,M) if (upd[i]) { FOR(j,K2*2+1) cnts[i][j] = 0; int cur = 0; FOR(j,K) if (L <= i*K+j && i*K+j <= n*2) { cur += val[i*K+j]; cnts[i][cur+K2]++; } tot[i] = cur; //printf("updated %d (%d to %d) - tot = %d\n", i, i*K, i*K+K-1, tot[i]); //for (int j = -10; j <= 10; j++) { // if (j==0) printf(" | "); // printf("%lld ", cnts[i][j+K2]); // if (j==0) printf(" | "); //} //printf("\n"); } int cur = 0; FOR(i,M) { FOR(j,k+1) { if (0 <= j+K2-cur && j+K2-cur < K2*2) cnt[j] += cnts[i][j+K2-cur]; //printf("cnt[%d] upd by %lld\n", j, cnts[i][j+K2-cur]); } cur += tot[i]; } //printf("\n"); } cnt[1] += cnt[0]; FORI(i,k) printf("%lld ", cnt[i]); printf("\n"); return 0; }
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 | #include <bits/stdc++.h> using namespace std; #define REP(i,a,b) for (int i = (a); i <= (b); ++i) #define REPD(i,a,b) for (int i = (a); i >= (b); --i) #define FORI(i,n) REP(i,1,n) #define FOR(i,n) REP(i,0,int(n)-1) #define mp make_pair #define pb push_back #define pii pair<int,int> #define vi vector<int> #define ll long long #define SZ(x) int((x).size()) #define DBG(v) cerr << #v << " = " << (v) << endl; #define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define fi first #define se second const int dx[4] = {0, 1, 0, -1}; const int dy[4] = {1, 0, -1, 0}; const int px[6] = { 0, 1, 0, 1, 0, 1}; const int py[6] = {-1, -1, 0, 0, 1, 1}; int p[11]; int fin(int x) { return p[x]==x ? x : p[x]=fin(p[x]); } bool uni(int x, int y) { fin(x); fin(y); if (p[x]==p[y]) return false; p[p[x]]=p[y]; return true; } const int N = 100100; int n,k; int a[2][N]; int nei[11], vis[11][11]; vi eve[N*7]; int en, ep; int val[N*2]; ll cnt[555]; int tot[N], upd[N]; ll cnts[555][3030]; int tmp[N*2]; int main() { scanf("%d%d", &n, &k); FOR(i,2) FOR(j,n) scanf("%d", &a[i][j]); //n=100000; k=10; //FOR(i,n*2) tmp[i]=i+1; //random_shuffle(tmp,tmp+n); //FOR(i,2) FOR(j,n) a[i][j]=tmp[i*n+j]; a[0][n] = a[0][0]; a[1][n] = a[1][0]; a[0][n+1] = a[0][1]; a[1][n+1] = a[1][1]; FOR(i,2) FORI(j,n) { FOR(c,6) nei[c] = a[px[c]][j+py[c]]; sort(nei, nei+6); int prv = -1; FOR(c,6) { // prv < L <= nei[c] => a[] <= prv off, prv < a[] < a[i][j] on, a[] > a[i][j] off FOR(w,6) vis[px[w]][py[w]+1] = 0; FOR(w,6) if (prv < a[px[w]][j+py[w]] && a[px[w]][j+py[w]] < a[i][j]) vis[px[w]][py[w]+1] = 1; int nv = 0; FOR(w,6) p[w]=w; //printf("i=%d, j=%d, range = (%d, %d]\n", i, j, prv, nei[c]); //FOR(ii,2) { // FOR(jj,3) printf("%d",vis[ii][jj]); // printf("\n"); //} FOR(w,6) if(vis[px[w]][py[w]+1]) { nv++; FOR(kk,4) { int x = px[w]+dx[kk], y = py[w]+dy[kk]+1; if (x<0 || x>1 || y<0 || y>2) continue; int w2 = y*2+x; if (vis[x][y]) nv -= uni(w,w2); } } //printf("p = "); //FOR(ii,6) printf("%d ", p[ii]); //printf("\nnv = %d\n", nv); int pv = nv; nv++; FOR(kk,4) { int x = i+dx[kk], y = dy[kk]+1; if (x<0 || x>1 || y<0 || y>2) continue; int w2 = y*2+x; //printf("kk=%d, join %d %d\n", kk, w2, 2+i); if (vis[x][y]) nv -= uni(2+i,w2); } //printf("p = "); //FOR(ii,6) printf("%d ", fin(p[ii])); //printf("\nnv = %d\n", nv); // prv < L <= nei[c] => a[i][j] is worth nv-pv //printf("%d < L <= %d => a[%d][%d] is worth %d\n", prv, nei[c], i, j, nv-pv); eve[en++] = {prv+1, i, j, nv-pv}; if (nei[c] == a[i][j]) break; prv = nei[c]; } } sort(eve, eve+en); int K = sqrt(n*2)+1, K2 = K*2+3, M = n*2/K+1; cerr << K << " " << M << " " << K*M << " " << n*2 << "\n"; FORI(L,n*2) { FOR(i,M) upd[i] = 0; while (ep < en && eve[ep][0] <= L) { int chgpos = a[eve[ep][1]][eve[ep][2]]; val[chgpos] = eve[ep][3]; upd[chgpos/K] = true; ep++; } val[L-1] = 0; upd[(L-1)/K] = true; //FOR(i,n*2+1) { // if (i==L) printf(" | "); // printf("%d ", val[i]); //} //printf("\n"); FOR(i,M) if (upd[i]) { FOR(j,K2*2+1) cnts[i][j] = 0; int cur = 0; FOR(j,K) if (L <= i*K+j && i*K+j <= n*2) { cur += val[i*K+j]; cnts[i][cur+K2]++; } tot[i] = cur; //printf("updated %d (%d to %d) - tot = %d\n", i, i*K, i*K+K-1, tot[i]); //for (int j = -10; j <= 10; j++) { // if (j==0) printf(" | "); // printf("%lld ", cnts[i][j+K2]); // if (j==0) printf(" | "); //} //printf("\n"); } int cur = 0; FOR(i,M) { FOR(j,k+1) { if (0 <= j+K2-cur && j+K2-cur < K2*2) cnt[j] += cnts[i][j+K2-cur]; //printf("cnt[%d] upd by %lld\n", j, cnts[i][j+K2-cur]); } cur += tot[i]; } //printf("\n"); } cnt[1] += cnt[0]; FORI(i,k) printf("%lld ", cnt[i]); printf("\n"); return 0; } |