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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#include <bits/stdc++.h>
#ifdef LOC
#include "debuglib.h"
#else
#define deb(...)
#define DBP(...)
#endif
using namespace std;
using   ll         = long long;
using   Vi         = vector<int>;
using   Pii        = pair<int, int>;
#define pb           push_back
#define mp           make_pair
#define x            first
#define y            second
#define rep(i, b, e) for (int i = (b); i < (e); i++)
#define each(a, x)   for (auto& a : (x))
#define all(x)       (x).begin(), (x).end()
#define sz(x)        int((x).size())

namespace brute {
	constexpr int MAX_N = 40;

	ll masks[MAX_N];

	int getInvs(ll mask) {
		int ret = 0;
		for (ll tmp = mask; tmp; tmp &= tmp-1) {
			int bit = __builtin_ctzll(tmp);
			ret += __builtin_popcountll(masks[bit] & mask);
		}
		return ret;
	}

	void solve(int n) {
		Vi perm(n);
		each(e, perm) cin >> e;

		rep(i, 0, n) {
			rep(j, 0, i) {
				if (perm[j] > perm[i]) {
					masks[i] |= 1LL << j;
				}
			}
		}

		Vi invs(n+1, 1e9), cnt(n+1);

		for (ll mask = 0; mask < (1LL<<n); mask++) {
			int s = __builtin_popcountll(mask);
			int k = getInvs(mask);

			if (k < invs[s]) {
				invs[s] = k;
				cnt[s] = 1;
			} else if (k == invs[s]) {
				cnt[s]++;
			}
		}

		rep(i, 1, n+1) {
			cout << invs[i] << ' ' << cnt[i] << '\n';
		}
	}
};

#define left left__
#define right right__

int nextSubset(int v) {
	int t = v | (v - 1);
	return (t + 1) | (((~t & -~t) - 1) >> (__builtin_ctz(v) + 1));  
}

struct Ans {
	int invs;
	ll cnt;
	DBP(invs, cnt);

	Ans& operator+=(const Ans& r) {
		if (r.invs < invs) {
			*this = r;
		} else if (r.invs == invs) {
			cnt += r.cnt;
		}
		return *this;
	}
};

struct State {
	int lMask, rMask;
	Ans ans;
	DBP(lMask, rMask, ans);

	ll key() const {
		return lMask | (ll(rMask) << 24);
	}
};

int n, h;
Vi perm, iLeft, iRight, minBadLeft;
vector<ll> lookup;
vector<int> left, right;
vector<State> states, statesNew;
vector<pair<ll, int>> sortedKeys;

int getInvs(ll mask) {
	int ret = 0;
	for (ll tmp = mask; tmp; tmp &= tmp-1) {
		int bit = __builtin_ctzll(tmp);
		ret += __builtin_popcountll(lookup[bit] & mask);
	}
	return ret;
}

void getHalf(vector<int>& out, Vi& ind) {
	int k = sz(ind);
	out.resize(1<<k);

	rep(mask, 0, 1<<k) {
		ll mapped = 0;
		for (int tmp = mask; tmp; tmp &= tmp-1) {
			int bit = __builtin_ctz(tmp);
			mapped |= 1LL << ind[bit];
		}
		out[mask] = getInvs(mapped);
	}
}

int normLeftMask(int mask) {
	int ret = 0;
	rep(i, 1, sz(minBadLeft)) {
		int begin = minBadLeft[i-1], end = minBadLeft[i];
		int k = begin;
		rep(j, begin, end) {
			if ((mask >> j) & 1) {
				ret |= 1 << k;
				k++;
			}
		}
	}
	return ret;
}

int lastCut;

void reduce(int cut) {
	int cutMask = ~((1 << cut)-1);
	sortedKeys.resize(sz(states));

	rep(i, 0, sz(states)) {
		states[i].lMask &= cutMask;
		sortedKeys[i].x = states[i].key();
		sortedKeys[i].y = i;
	}

	sort(all(sortedKeys));
	statesNew.clear();
	ll lastKey = -1;

	each(key, sortedKeys) {
		if (key.x != lastKey) {
			statesNew.pb(states[key.y]);
			lastKey = key.x;
		} else {
			statesNew.back().ans += states[key.y].ans;
		}
	}

	statesNew.swap(states);
	lastCut = cut;
}

void reduceFast(int cut) {
	if (cut <= lastCut) {
		return;
	}

	int cutMask = ~((1 << cut)-1);
	ll lastKey = -1;
	int j = -1;

	rep(i, 0, sz(states)) {
		states[i].lMask &= cutMask;
		ll key = states[i].key();

		if (key != lastKey) {
			lastKey = key;
			j++;
			states[j] = states[i];
		} else {
			states[j].ans += states[i].ans;
		}
	}

	states.resize(j+1);
	lastCut = cut;
}

int main() {
	cin.sync_with_stdio(0); cin.tie(0);
	cout << fixed << setprecision(18);

	cin >> n;

	if (n <= 22) {
		deb("brute");
		brute::solve(n);
		return 0;
	}

	perm.resize(n);
	each(e, perm) cin >> e;

	lookup.resize(n);

	rep(i, 0, n) {
		rep(j, 0, i) {
			if (perm[j] > perm[i]) {
				lookup[i] |= 1LL << j;
			}
		}
	}

	h = n/2 + 2;

	iLeft.resize(h);
	iRight.resize(n-h);
	iota(all(iLeft), 0);
	iota(all(iRight), h);

	sort(all(iLeft), [&](int l, int r) { return perm[l] < perm[r]; });
	sort(all(iRight), [&](int l, int r) { return perm[l] < perm[r]; });

	minBadLeft.resize(n-h);
	int j = 0;

	rep(i, 0, n-h) {
		while (j < h && perm[iLeft[j]] < perm[iRight[i]]) {
			j++;
		}
		minBadLeft[i] = j;
	}

	minBadLeft.pb(h);

	getHalf(left, iLeft);
	getHalf(right, iRight);

	int limMask = 1 << sz(iLeft);

	vector<Ans> ans(n+1, {INT_MAX, 0});

	rep(nBits, 0, sz(iLeft)+1) {
		states.clear();

		for (int i = (1<<nBits)-1; i < limMask; i = nextSubset(i)) {
			states.emplace_back();
			auto& st = states.back();
			st.lMask = normLeftMask(i);
			st.rMask = 0;
			st.ans.invs = left[i];
			st.ans.cnt = 1;
			if (i == 0) break;
		}

		reduce(minBadLeft[0]);

		rep(bit, 0, sz(iRight)) {
			int nStates = sz(states);
			states.resize(nStates*2);

			rep(i, 0, nStates) {
				auto& st = states[nStates+i] = states[i];
				st.rMask |= 1<<bit;
				st.ans.invs += __builtin_popcount(st.lMask);
			}

			reduceFast(minBadLeft[bit+1]);
		}

		each(st, states) {
			int k = nBits + __builtin_popcount(st.rMask);
			st.ans.invs += right[st.rMask];
			ans[k] += st.ans;
		}
	}

	rep(i, 1, n+1) {
		cout << ans[i].invs << ' ' << ans[i].cnt << '\n';
	}
	return 0;
}