Search is not available for this dataset
name
stringlengths
2
88
description
stringlengths
31
8.62k
public_tests
dict
private_tests
dict
solution_type
stringclasses
2 values
programming_language
stringclasses
5 values
solution
stringlengths
1
983k
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const long long LINF = 0x3f3f3f3f3f3f3f3fLL; const double PI = acos((long double)-1.0); const double EPS = 1e-10; const int MOD = 1e9 + 7; template <typename T> void cmin(T &x, T y) { if (y < x) x = y; } template <typename T> void cmax(T &x, T y) { if (y > x) x = y; } long long qpow(long long x, long long n, long long mod = MOD) { if (n < 0) return 0; long long res = 1; while (n) { if (n & 1) res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } long long sq(long long x) { return x * x; } long long read() { long long res = 0; char c = getchar(); while (!isdigit(c)) c = getchar(); while (isdigit(c)) { res = res * 10 + (c - '0'); c = getchar(); } return res; } const int MAXN = 200000 + 10; int n; int a[MAXN]; int pos[MAXN]; struct SegmentTree { int cnt[MAXN << 2]; long long sum[MAXN << 2]; int add[MAXN << 2]; void PushUp(int o) { cnt[o] = cnt[(o << 1)] + cnt[(o << 1 | 1)]; sum[o] = sum[(o << 1)] + sum[(o << 1 | 1)]; } void PushDown(int o, int l, int r) { if (add[o]) { int m = (l + r) >> 1; sum[(o << 1)] += 1LL * cnt[(o << 1)] * add[o]; sum[(o << 1 | 1)] += 1LL * cnt[(o << 1 | 1)] * add[o]; add[(o << 1)] += add[o]; add[(o << 1 | 1)] += add[o]; add[o] = 0; } } void Build(int o, int l, int r) { add[o] = 0; if (l == r) { cnt[o] = 1; sum[o] = 0; return; } int m = (l + r) >> 1; Build((o << 1), l, m); Build((o << 1 | 1), m + 1, r); PushUp(o); } void Add(int o, int l, int r, int ql, int qr, int v) { if (ql <= l && r <= qr) { sum[o] += 1LL * cnt[o] * v; add[o] += v; return; } PushDown(o, l, r); int m = (l + r) >> 1; if (ql <= m) Add((o << 1), l, m, ql, qr, v); if (qr >= m + 1) Add((o << 1 | 1), m + 1, r, ql, qr, v); PushUp(o); } void SetCnt0(int o, int l, int r, int p) { if (l == r) { cnt[o] = 0; sum[o] = 0; return; } PushDown(o, l, r); int m = (l + r) >> 1; if (p <= m) SetCnt0((o << 1), l, m, p); if (p >= m + 1) SetCnt0((o << 1 | 1), m + 1, r, p); PushUp(o); } int Cnt(int o, int l, int r, int ql, int qr) { if (ql <= l && r <= qr) return cnt[o]; PushDown(o, l, r); int m = (l + r) >> 1; int res = 0; if (ql <= m) res = res + Cnt((o << 1), l, m, ql, qr); if (qr >= m + 1) res = res + Cnt((o << 1 | 1), m + 1, r, ql, qr); return res; } long long Sum(int o, int l, int r, int ql, int qr) { if (ql <= l && r <= qr) return sum[o]; PushDown(o, l, r); int m = (l + r) >> 1; long long res = 0; if (ql <= m) res = res + Sum((o << 1), l, m, ql, qr); if (qr >= m + 1) res = res + Sum((o << 1 | 1), m + 1, r, ql, qr); return res; } int Pos(int o, int l, int r, int k) { if (l == r) return l; PushDown(o, l, r); int m = (l + r) >> 1; int cnt0ls = (m - l + 1) - cnt[(o << 1)]; if (cnt0ls >= k) return Pos((o << 1), l, m, k); return Pos((o << 1 | 1), m + 1, r, k - cnt0ls); } int Lmid() { int cnt0 = n - cnt[1]; return Pos(1, 1, n, (cnt0 + 1) / 2); } int Rmid() { int cnt0 = n - cnt[1]; return Pos(1, 1, n, cnt0 / 2 + 1); } } st; namespace Solver { void InitOnce() { int t; } void Read() { int res = scanf("%d", &n); if (res == -1) exit(0); for (int i = 1; i <= n; ++i) { scanf("%d", &a[i]); pos[a[i]] = i; } } long long ans[200005]; void Solve() { st.Build(1, 1, n); long long cur = 0; int Lmost = n, Rmost = 1; for (int i = 1; i <= n; ++i) { int p = pos[i]; cmin(Lmost, p); cmax(Rmost, p); cur += (n - p + 1) - st.Cnt(1, 1, n, p, n); if (i >= 2) { int Lmid = st.Lmid(); int Rmid = st.Rmid(); if (p < Lmid) st.Add(1, 1, n, p + 1, Lmid - 1, 1); if (p > Rmid) st.Add(1, 1, n, Rmid + 1, p - 1, 1); } st.SetCnt0(1, 1, n, p); ans[i] = cur + st.Sum(1, 1, n, Lmost, Rmost); printf("%lld%c", ans[i], " \n"[i == n]); } } } // namespace Solver int main() { Solver::InitOnce(); while (true) { Solver::Read(); Solver::Solve(); } return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
python3
n = int(input()) a = [0] + list(map(int, input().split())) pos, pb, ps = [[0] * (n + 1) for x in range(3)] def add(bit, i, val): while i <= n: bit[i] += val i += i & -i def sum(bit, i): res = 0 while i > 0: res += bit[i] i -= i & -i return res def find(bit, sum): i, t = 0, 0 if sum == 0: return 0 for k in range(17, -1, -1): i += 1 << k if i <= n and t + bit[i] < sum: t += bit[i] else: i -= 1 << k return i + 1 for i in range(1, n + 1): pos[a[i]] = i invSum = 0 totalSum = 0 for i in range(1, n + 1): totalSum += pos[i] invSum += i - sum(pb, pos[i]) - 1 add(pb, pos[i], 1) add(ps, pos[i], pos[i]) mid = find(pb, i // 2) if i % 2 == 1: mid2 = find(pb, i // 2 + 1) seqSum = (i + 1) * (i // 2) // 2 else: mid2 = mid seqSum = i * (i // 2) // 2 leftSum = sum(ps, mid) rightSum = totalSum - sum(ps, mid2) print(rightSum - leftSum - seqSum + invSum, end=" ")
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 5; int n; int a[MAXN]; int pos[MAXN]; struct BIT { long long tree[MAXN]; BIT() {} int LB(int x) { return x & -x; } void Modify(int p, long long k) { while (p <= n) { tree[p] += k; p += LB(p); } } long long Query(int p) { long long res = 0; while (p >= 1) { res += tree[p]; p -= LB(p); } return res; } int GetPos(long long sum) { int l = 1, r = n, res = 0; while (l <= r) { int mid = (l + r) >> 1; if (Query(mid) <= sum) { res = mid; l = mid + 1; } else { r = mid - 1; } } return res; } } T1, T2; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; pos[a[i]] = i; } long long cnt = 0; for (int i = 1; i <= n; i++) { int p = pos[i]; T1.Modify(p, 1); cnt += (long long)i - T1.Query(p); T2.Modify(p, p); int midp = T1.GetPos(i >> 1) + 1; long long tot = 0; long long l = i / 2, r = i - i / 2 - 1; tot += l * midp - l * (l + 1) / 2 - T2.Query(midp - 1); tot += (T2.Query(n) - T2.Query(midp)) - r * midp - r * (r + 1) / 2; cout << tot + cnt << ' '; } return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = 1061109567; const long long LINF = 4557430888798830399ll; const int MOD = 1000000007; long long qpow(long long x, long long n) { long long res = 1; while (n) { if (n & 1) res = res * x % MOD; x = x * x % MOD; n >>= 1; } return res; } struct Treap { static const int MAXN = 200000 + 5; int ch[MAXN][2], dat[MAXN]; pair<int, int> val[MAXN]; int cnt[MAXN]; int siz[MAXN]; int tot, root; inline void Init() { tot = 0; root = 0; } inline int NewNode(pair<int, int> v, int num) { int id = ++tot; ch[id][0] = ch[id][1] = 0; dat[id] = rand(); val[id] = v; cnt[id] = num; siz[id] = num; return id; } inline void PushUp(int id) { siz[id] = siz[ch[id][0]] + siz[ch[id][1]] + cnt[id]; } inline void Rotate(int &id, int d) { int temp = ch[id][d ^ 1]; ch[id][d ^ 1] = ch[temp][d]; ch[temp][d] = id; id = temp; PushUp(ch[id][d]); PushUp(id); } inline void Insert(int &id, pair<int, int> v, int num) { if (!id) id = NewNode(v, num); else { if (v == val[id]) cnt[id] += num; else { int d = val[id] > v ? 0 : 1; Insert(ch[id][d], v, num); if (dat[id] < dat[ch[id][d]]) Rotate(id, d ^ 1); } PushUp(id); } } void Remove(int &id, pair<int, int> v, int num) { if (!id) return; else { if (v == val[id]) { if (cnt[id] > num) { cnt[id] -= num; PushUp(id); } else if (ch[id][0] || ch[id][1]) { if (!ch[id][1] || dat[ch[id][0]] > dat[ch[id][1]]) Rotate(id, 1), Remove(ch[id][1], v, num); else Rotate(id, 0), Remove(ch[id][0], v, num); PushUp(id); } else id = 0; } else { val[id] > v ? Remove(ch[id][0], v, num) : Remove(ch[id][1], v, num); PushUp(id); } } } int GetRank(int id, pair<int, int> v) { int res = 1; while (id) { if (val[id] > v) id = ch[id][0]; else if (val[id] == v) { res += siz[ch[id][0]]; break; } else { res += siz[ch[id][0]] + cnt[id]; id = ch[id][1]; } } return res; } pair<int, int> GetValue(int id, int rk) { pair<int, int> res = {INF, INF}; while (id) { if (siz[ch[id][0]] >= rk) id = ch[id][0]; else if (siz[ch[id][0]] + cnt[id] >= rk) { res = val[id]; break; } else { rk -= siz[ch[id][0]] + cnt[id]; id = ch[id][1]; } } return res; } } treap1, treap2; int n; int a[200005]; int p[200005]; long long ans[200005]; void test_case() { scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", &a[i]); p[a[i]] = i; } treap1.Init(); treap2.Init(); int Lmost = p[1], Rmost = p[1]; long long cnt1 = 0, cnt2 = 0; treap1.Insert(treap1.root, {p[1], 1}, 1); treap2.Insert(treap2.root, {p[1], 1}, 1); pair<int, int> mid1, mid2; mid1 = mid2 = {p[1], 1}; ans[1] = 0; for (int i = 2; i <= n; ++i) { treap1.Insert(treap1.root, {p[i], i}, 1); if (i % 2 == 0) { mid1 = treap1.GetValue(treap1.root, i / 2); mid2 = treap1.GetValue(treap1.root, i / 2 + 1); } else mid1 = mid2 = treap1.GetValue(treap1.root, (i + 1) / 2); int rk1 = treap1.GetRank(treap1.root, {p[i], i}); if (p[i] < Lmost) { for (int j = Lmost - 1; j >= p[i]; --j) treap2.Insert(treap2.root, {j, a[j]}, 1); Lmost = p[i]; cnt2 += treap2.GetRank(treap2.root, mid2) - 1 - i / 2; } else if (p[i] > Rmost) { for (int j = Rmost + 1; j <= p[i]; ++j) treap2.Insert(treap2.root, {j, a[j]}, 1); Rmost = p[i]; cnt2 += treap2.siz[treap2.root] - treap2.GetRank(treap2.root, mid1) - i / 2; } else { if (p[i] >= mid2.first) { cnt2 -= i - rk1; cnt2 -= treap2.siz[treap2.root] - treap2.GetRank(treap2.root, {p[i], i}) - (i - rk1); cnt2 += treap2.siz[treap2.root] - treap2.GetRank(treap2.root, mid1) - i / 2; } else if (p[i] <= mid1.first) { cnt2 -= rk1 - 1; cnt2 -= treap2.GetRank(treap2.root, {p[i], i}) - (rk1 - 1); cnt2 += treap2.GetRank(treap2.root, mid2) - i / 2; } } cnt1 += i - rk1; ans[i] = cnt1 + cnt2; } for (int i = 1; i <= n; ++i) printf("%lld%c", ans[i], " \n"[i == n]); } int main() { int t = 1; for (int ti = 1; ti <= t; ++ti) { test_case(); } }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = 1061109567; const long long LINF = 4557430888798830399ll; const int MOD = 1000000007; long long qpow(long long x, long long n) { long long res = 1; while (n) { if (n & 1) res = res * x % MOD; x = x * x % MOD; n >>= 1; } return res; } struct TreapNode { int val1, val2; TreapNode() {} TreapNode(int val1, int val2) : val1(val1), val2(val2) {} bool operator<(const TreapNode& tn) const { return val1 < tn.val1; } bool operator<=(const TreapNode& tn) const { return val1 <= tn.val1; } bool operator==(const TreapNode& tn) const { return val1 == tn.val1; } bool operator>=(const TreapNode& tn) const { return val1 >= tn.val1; } bool operator>(const TreapNode& tn) const { return val1 > tn.val1; } } TNINF(INF, INF); struct Treap { static const int MAXN = 200000; int ch[MAXN + 5][2], dat[MAXN + 5]; TreapNode val[MAXN + 5]; int cnt[MAXN + 5]; int siz[MAXN + 5]; int tot, root; void Init() { tot = 0; root = 0; } int NewNode(TreapNode v, int num) { int id = ++tot; ch[id][0] = ch[id][1] = 0; dat[id] = rand(); val[id] = v; cnt[id] = num; siz[id] = num; return id; } void PushUp(int id) { siz[id] = siz[ch[id][0]] + siz[ch[id][1]] + cnt[id]; } void Rotate(int& id, int d) { int temp = ch[id][d ^ 1]; ch[id][d ^ 1] = ch[temp][d]; ch[temp][d] = id; id = temp; PushUp(ch[id][d]); PushUp(id); } void _Insert(int& id, TreapNode v, int num) { if (!id) id = NewNode(v, num); else { if (v == val[id]) cnt[id] += num; else { int d = val[id] > v ? 0 : 1; _Insert(ch[id][d], v, num); if (dat[id] < dat[ch[id][d]]) Rotate(id, d ^ 1); } PushUp(id); } } void _Remove(int& id, TreapNode v, int num) { if (!id) return; else { if (v == val[id]) { if (cnt[id] > num) { cnt[id] -= num; PushUp(id); } else if (ch[id][0] || ch[id][1]) { if (!ch[id][1] || dat[ch[id][0]] > dat[ch[id][1]]) Rotate(id, 1), _Remove(ch[id][1], v, num); else Rotate(id, 0), _Remove(ch[id][0], v, num); PushUp(id); } else id = 0; } else { val[id] > v ? _Remove(ch[id][0], v, num) : _Remove(ch[id][1], v, num); PushUp(id); } } } int _GetRank(int id, TreapNode v) { int res = 1; while (id) { if (val[id] > v) id = ch[id][0]; else if (val[id] == v) { res += siz[ch[id][0]]; break; } else { res += siz[ch[id][0]] + cnt[id]; id = ch[id][1]; } } return res; } TreapNode _GetValue(int id, int rk) { TreapNode res = TNINF; while (id) { if (siz[ch[id][0]] >= rk) id = ch[id][0]; else if (siz[ch[id][0]] + cnt[id] >= rk) { res = val[id]; break; } else { rk -= siz[ch[id][0]] + cnt[id]; id = ch[id][1]; } } return res; } int Size() { return siz[root]; } void Insert(TreapNode v, int num = 1) { _Insert(root, v, num); } void Remove(TreapNode v, int num = INF) { _Remove(root, v, num); } int GetRank(TreapNode v) { return _GetRank(root, v); } TreapNode GetValue(int rk) { return _GetValue(root, rk); } } treap1, treap2; int n; int a[200005]; int p[200005]; long long ans[200005]; void test_case() { scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", &a[i]); p[a[i]] = i; } treap1.Init(); treap2.Init(); int Lmost = p[1], Rmost = p[1]; long long cnt1 = 0, cnt2 = 0; TreapNode cur(p[1], 1); treap1.Insert(cur); treap2.Insert(cur); TreapNode mid1, mid2; mid1 = mid2 = cur; ans[1] = 0; for (int i = 2; i <= n; ++i) { cur = TreapNode(p[i], i); treap1.Insert(cur, 1); mid1 = treap1.GetValue((i + 1) / 2); mid2 = treap1.GetValue(i / 2 + 1); if (p[i] < Lmost) { for (int j = Lmost - 1; j >= p[i]; --j) treap2.Insert(TreapNode(j, a[j])); Lmost = p[i]; } else if (p[i] > Rmost) { for (int j = Rmost + 1; j <= p[i]; ++j) treap2.Insert(TreapNode(j, a[j])); Rmost = p[i]; } cnt1 += i - treap1.GetRank(cur); if (p[i] >= mid2.val1) { cnt2 -= treap2.Size() - treap2.GetRank(cur); cnt2 += treap2.Size() - treap2.GetRank(mid1) - i / 2; } else if (p[i] <= mid1.val1) { cnt2 -= treap2.GetRank(cur); cnt2 += treap2.GetRank(mid2) - i / 2; } ans[i] = cnt1 + cnt2; } for (int i = 1; i <= n; ++i) printf("%lld%c", ans[i], " \n"[i == n]); } int main() { int t = 1; for (int ti = 1; ti <= t; ++ti) { test_case(); } }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const bool debug = true; int p[200001]; int pos[200001]; class seg_tree { int* st; int n; public: seg_tree(int a) { st = new int[2 * a]; for (int i = 0; i < 2 * a; i++) st[i] = 0; n = a; } void set(int x) { x += n; st[x] = 1; x /= 2; while (x) { st[x] = st[2 * x] + st[2 * x + 1]; x /= 2; } } int query(int l, int r) { l += n; r += n; int ans = 0; while (l < r) { if (l & 1) { ans += st[l++]; } if (r & 1) { ans += st[--r]; } l >>= 1; r >>= 1; } return ans; } }; class median { priority_queue<int, vector<int>, greater<int>> higher; priority_queue<int> lower; long long int suml, sumr; public: median() { suml = 0; sumr = 0; } void insert(int n) { if ((lower.empty() && higher.empty()) || n < lower.top()) { lower.push(n); suml += n; if (lower.size() > higher.size() + 1) { higher.push(lower.top()); sumr += lower.top(); suml -= lower.top(); lower.pop(); } } else { higher.push(n); sumr += n; if (higher.size() > lower.size()) { lower.push(higher.top()); suml += higher.top(); sumr -= higher.top(); higher.pop(); } } } int get_median() { return lower.top(); } long long get_swaps() { long long int n = lower.size(); long long int l = lower.top() * n - suml - ((n) * (n - 1)) / 2; n = higher.size(); long long r = sumr - lower.top() * n - ((n) * (n + 1)) / 2; return l + r; } }; int main() { int n; cin >> n; for (int z = 1; z < n + 1; z++) { cin >> p[z]; pos[p[z]] = z; } seg_tree cind(n + 1); median med; long long inv = 0; for (int z = 1; z < n + 1; z++) { inv += cind.query(pos[z], n + 1); cind.set(pos[z]); med.insert(pos[z]); cout << inv + med.get_swaps() << " "; } cout << endl; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 1, base = (1 << 19); int n, p[N], inv_t[2 * base + 1], where[N], pref; long long pos_t[2 * base + 1]; void add(int pos) { pos += base; while (pos) { inv_t[pos]++; pos /= 2; } } int sum(int l, int r) { l += base; r += base; int res = inv_t[l]; if (l != r) { res += inv_t[r]; } while (l / 2 != r / 2) { if (l % 2 == 0) { res += inv_t[l + 1]; } if (r % 2 == 1) { res += inv_t[r - 1]; } l /= 2; r /= 2; } return res; } int find_med(int id, int k) { if (id >= base) { return id - base; } if (inv_t[id * 2] + pref >= k) { return find_med(id * 2, k); } pref += inv_t[id * 2]; return find_med(id * 2 + 1, k); } void ins(int pos, long long val) { pos += base; while (pos) { pos_t[pos] += val; pos /= 2; } } long long pos_q(int l, int r) { l += base; r += base; long long res = pos_t[l]; if (l != r) { res += pos_t[r]; } while (l / 2 != r / 2) { if (l % 2 == 0) { res += pos_t[l + 1]; } if (r % 2 == 1) { res += pos_t[r - 1]; } l /= 2; r /= 2; } return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i = 0; i < n; ++i) { cin >> p[i]; where[p[i]] = i; } long long inv = 0; for (int i = 1; i <= n; ++i) { long long l = sum(0, where[i]); long long r = sum(where[i], n - 1); inv += r; add(where[i]); ins(where[i], where[i]); int med = find_med(1, (i - 1) / 2 + 1); pref = 0; l = sum(0, med) - 1; r = sum(med, n - 1) - 1; long long cost = -pos_q(0, med) + pos_q(med, n - 1) + (long long)med * (l - r) - r * (r + 1LL) / 2LL - l * (l + 1LL) / 2LL; cout << inv + cost << ' '; } }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MAX_N = 2e5 + 10; int bit[MAX_N], n, pos[MAX_N], x; long long ans = 0; int lowbit(int x) { return x & (-x); } int sum(int x) { int res = 0; while (x < MAX_N) { res += bit[x]; x += lowbit(x); } return res; } void update(int x) { while (x) { bit[x] += 1; x -= lowbit(x); } } int get(int d) { int l = 1, r = n; while (l <= r) { int mid = (l + r) >> 1; if (sum(mid) >= d) { l = mid + 1; } else { r = mid - 1; } } return r; } int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> x; pos[x] = i; } for (int i = 1; i <= n; i++) { int mid = (i + 1) / 2; if (i & 1) { update(pos[i]); int k = get(mid); int dis = abs(pos[i] - k) - abs(sum(pos[i]) - sum(k)); if (pos[i] < k) { ans += -(sum(1) - sum(pos[i])); } else { ans += -sum(pos[i] + 1); } ans += dis + sum(pos[i] + 1); } else { int k = get(mid); int dis = abs(pos[i] - k) - abs(sum(pos[i]) + (pos[i] < k ? 1 : 0) - sum(k)); if (pos[i] < k) { ans += -(sum(1) - sum(pos[i])); } else { ans += -sum(pos[i] + 1); } ans += dis + sum(pos[i] + 1); update(pos[i]); } printf("%I64d ", ans); } return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MAX = 2e5 + 5; int a[MAX], p[MAX], n; long long c1[MAX], c2[MAX]; set<int> st; void add(long long *c, int x, int k) { for (; x <= n; x += x & -x) c[x] += k; } long long query(long long *c, int x) { long long sum = 0; for (; x; x -= x & -x) sum += c[x]; return sum; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]), p[a[i]] = i; st.insert(p[1]); auto it = st.begin(); printf("0 "); add(c1, p[1], 1); add(c2, p[1], p[1]); long long res = 0; for (int i = 2; i <= n; i++) { st.insert(p[i]); if (p[i] < (*it) && i % 2 == 0) it--; if (p[i] > (*it) && i % 2 == 1) it++; add(c1, p[i], 1); res += (i * 1ll - query(c1, p[i])); add(c2, p[i], p[i]); int midpos = *it; long long sum = 0, k = i / 2; sum += i & 1 ? k * (midpos - 1 + midpos - k) / 2 : (k - 1) * (midpos + midpos - k) / 2; sum -= k * (midpos + 1 + midpos + k) / 2; sum -= query(c2, midpos - 1); sum += query(c2, n) - query(c2, midpos); printf("%I64d ", res + sum); } }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = 1061109567; const int MAXN = 201010; int n, median; int v[MAXN]; int ind[MAXN]; long long st[4 * MAXN]; long long adjust[MAXN]; long long inversions[MAXN]; priority_queue<int> maxheap; priority_queue<int, vector<int>, greater<int> > minheap; void update(int nd, int l, int r, int pos, int x) { if (l == r) { st[nd] = x; return; } if (pos < l || pos > r) return; int mid = (l + r) >> 1; int nxt = nd << 1; if (pos <= mid) update(nxt, l, mid, pos, x); if (pos > mid) update(nxt + 1, mid + 1, r, pos, x); st[nd] = st[nxt] + st[nxt + 1]; } long long query(int nd, int l, int r, int lq, int rq) { if (r < lq || rq < l) return 0; if (lq <= l && r <= rq) return st[nd]; int mid = (l + r) >> 1; int nxt = nd << 1; return query(nxt, l, mid, lq, rq) + query(nxt + 1, mid + 1, r, lq, rq); } long long gather(int k, int med_pos) { long long rh = k - (k / 2); long long lh = k - rh; long long r_tar_sum = ((med_pos + (med_pos + rh - 1)) * rh) / 2; long long l_tar_sum = ((med_pos - 1 + (med_pos - lh)) * lh) / 2; long long r_pos_sum = query(1, 0, n, med_pos, n); long long l_pos_sum = query(1, 0, n, 0, max(0, med_pos - 1)); long long ret_val = (l_tar_sum - l_pos_sum) + (r_pos_sum - r_tar_sum); return ret_val; } void reset_seg_tree() { for (int i = 1; i <= n; i++) update(1, 0, n, i, 0); return; } void heap_insert(int x) { if (maxheap.size() > minheap.size()) { if (x < median) { minheap.push(maxheap.top()); maxheap.pop(); maxheap.push(x); } else { minheap.push(x); } median = minheap.top(); } else if (maxheap.size() == minheap.size()) { if (x < median) { maxheap.push(x); median = maxheap.top(); } else { minheap.push(x); median = minheap.top(); } } else { if (x > median) { maxheap.push(minheap.top()); minheap.pop(); minheap.push(x); } else { maxheap.push(x); } median = minheap.top(); } return; } int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> v[i]; ind[v[i]] = i; } for (int k = 1; k <= n; k++) { heap_insert(ind[k]); update(1, 0, n, ind[k], ind[k]); adjust[k] = gather(k, median); } reset_seg_tree(); inversions[0] = 0; for (int k = 1; k <= n; k++) { inversions[k] = inversions[k - 1] + query(1, 0, n, ind[k] + 1, n - 1); update(1, 0, n, ind[k], 1); } for (int i = 1; i <= n; i++) cout << (inversions[i] + adjust[i]) << " \n"[i == n]; return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; long long bit[N] = {}; int idx[N] = {}; int n; priority_queue<int> A; priority_queue<int, vector<int>, greater<int>> B; inline long long __abs(int x) { return (x ^ (x >> 31)) - (x >> 31); } long long sum(int x) { int ret = 0; while (x) { ret += bit[x]; x -= (x & -x); } return ret; } void add(int x, int val) { while (x <= n) { bit[x] += val; x += (x & -x); } } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n; long long x, now = 0, d = 0, t, f, p; for (int i = 1; i <= n; i++) { cin >> x; idx[x] = i; } x = idx[1]; for (long long i = 1; i <= n; i++) { B.emplace(idx[i]); A.emplace(B.top()); B.pop(); while (A.size() > ((i + 1) >> 1)) { B.emplace(A.top()); A.pop(); } while (A.size() < B.size()) { A.emplace(B.top()); B.pop(); } f = A.top(), p = (i / 2) * (i / 2 + 1) - (i / 2) * ((i & 1) == 0), t = A.size() - (f > x); now += __abs(idx[i] - x) + (f - x) * (t + t - i); x = f; add(idx[i], 1); d += i - sum(idx[i]); cout << now - p + d << " \n"[i == n]; } return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> const int inf = (1 << 29) - 1; const int maxn = (int)2e5 + 10; const int mod = (int)1e9 + 7; using namespace std; int n; int p[maxn]; long long t[2][maxn]; int rp[maxn]; void upd(int id, int pos, int val) { for (int i = pos; i <= n; i |= i + 1) t[id][i] += val; } long long get(int id, int pos) { long long res = 0; while (pos >= 0) { res += t[id][pos]; pos = (pos & (pos + 1)) - 1; } return res; } set<int> s1, s2; long long inv; long long add(int p, int k) { inv += get(0, n) - get(0, p); upd(0, p, 1); upd(1, p, p); long long ans = inv; s2.insert(p); s1.insert(*s2.begin()); s2.erase(s2.begin()); if (s2.size() + 2 <= s1.size()) { s2.insert(*(--s1.end())); s1.erase(--s1.end()); } long long m = *(--s1.end()); long long F = s1.size() - 1; long long S = s2.size(); ans += F * m - get(1, m - 1) - F * (F + 1ll) / 2; ans += get(1, n) - get(1, m) - m * S - S * (S + 1ll) / 2; return ans; } void solve() { cin >> n; for (int i = 1; i <= n; i++) { cin >> p[i]; rp[p[i]] = i; } for (int i = 1; i <= n; i++) { int pos = rp[i]; cout << add(pos, i) << "\n"; } } int main() { int t = 1; for (int i = 1; i <= t; i++) { solve(); } return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 9; long long mid[N], a[N], mark[N], k, f1[N][2], n, g[N], h[N], p, ct, cn, tvn = 1, tedad[N * 4]; string s, s2; long long saghf(long long x) { return (x / 2) + (x % 2); } string binary(int m, int k) { string g = ""; for (long long i = 0; i < k; i++) { g += char((m % 2) + '0'); m = m / 2; } return g; } int main() { cin >> n; cn = n; for (long long i = 1; i < n + 1; i++) cin >> a[i], mark[a[i]] = i, f1[i][0] = i - 1, f1[i][1] = i + 1; k = saghf(n); for (long long i = n; i >= 1; i--) { mid[i] = k; if (i % 2 == 0) { if (mark[i] <= k) k = f1[k][1]; } else { if (mark[i] >= k) k = f1[k][0]; } f1[f1[mark[i]][0]][1] = f1[mark[i]][1]; f1[f1[mark[i]][1]][0] = f1[mark[i]][0]; } k = 0; for (long long i = 2; i < n; i++) { if (i % 2 == 0) { if (mid[i] == mid[i - 1]) g[i] = g[i - 1] + (mark[i] - mid[i]) - (i / 2); else g[i] = g[i - 1] + (mid[i - 1] - mid[i]) + (mid[i] - mark[i]) - ((i / 2)); } else { if (mid[i] == mid[i - 1]) g[i] = g[i - 1] + (mid[i] - mark[i]) - (i / 2); else g[i] = g[i - 1] + (mark[i] - mid[i]) - (i / 2); } } for (long long i = 1; i < (n + 3) / 2; i++) { swap(a[i], a[n + 1 - i]); a[i] = n + 1 - a[i]; a[n + 1 - i] = n + 1 - a[n + 1 - i]; } if (n % 2 == 1) a[(n + 1) / 2] = n + 1 - a[(n + 1) / 2]; while (cn) { k++; cn /= 2; tvn *= 2; } tvn /= 2; for (long long i = 1; i < n + 1; i++) { s = binary(a[i], k); p = 0; ct = tvn; long long t = 0, m = tvn * 2 - a[i], p1 = tvn * 2; s2 = binary(m, k); for (long long j = k - 1; j >= 0; j--) { if (s2[j] == '1') { p1 -= ct; t += tedad[p1]; } ct /= 2; } h[n + 1 - a[i]] = t; ct = tvn; for (long long j = k - 1; j >= 0; j--) { if (s[j] == '1') { p += ct; tedad[p]++; } ct /= 2; } } for (long long i = 1; i < n + 1; i++) { h[i] += h[i - 1]; cout << h[i] + g[i] << " "; } }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; long long bit[N] = {}; int idx[N] = {}; int n; priority_queue<int> A; priority_queue<int, vector<int>, greater<int>> B; long long sum(int x) { int ret = 0; while (x) { ret += bit[x]; x -= (x & -x); } return ret; } void add(int x, int val) { while (x <= n) { bit[x] += val; x += (x & -x); } } int main() { cin >> n; long long x; for (int i = 1; i <= n; i++) { cin >> x; idx[x] = i; } long long now = 0, d = 0, t; x = idx[1]; for (int i = 1; i <= n; i++) { B.emplace(idx[i]); A.emplace(B.top()); B.pop(); while (A.size() > ((i + 1) >> 1)) { B.emplace(A.top()); A.pop(); } while (A.size() < B.size()) { A.emplace(B.top()); B.pop(); } long long f = A.top(), p = (long long)(i / 2) * (i / 2 + 1) - (i / 2) * ((i & 1) == 0), t = (long long)A.size() - (f >= x); now += abs(idx[i] - x) + (f - x) * (t + t - i); x = f; add(idx[i], 1); d += i - sum(idx[i]); cout << now - p + d << " \n"[i == n]; } return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; int n; int pos[maxn]; long long sum1[maxn], sum2[maxn]; int lowbit(int x) { return x & -x; } void add(long long *sum, int x, long long v) { while (x <= n) { sum[x] += v; x += lowbit(x); } } long long query(long long *sum, int x) { long long ans = 0; while (x > 0) { ans += sum[x]; x -= lowbit(x); } return ans; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { int p; scanf("%d", &p); pos[p] = i; } long long rev = 0; for (int i = 1; i <= n; i++) { add(sum1, pos[i], 1); add(sum2, pos[i], pos[i]); rev += i - query(sum1, pos[i]); int l = 1, r = n, mid = (l + r) >> 1; while (l < r) { if (query(sum1, mid) <= i / 2) l = mid + 1; else r = mid; mid = (l + r) >> 1; } long long cntl = query(sum1, mid) - 1, cntr = i - cntl - 1; long long ans = 1LL * mid * (cntl - cntr + 1) + query(sum2, n) - 2 * query(sum2, mid) - (cntl + 1) * cntl / 2 - (cntr + 1) * cntr / 2; ans += rev; printf("%lld ", ans); } return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long ans, n, a[210000], x, sum1, sum2, las; int rd[450000], siz[450000], son[450000][2], v[450000], root, num[450000], cnt; priority_queue<int> q1; priority_queue<int, vector<int>, greater<int> > q2; inline void update(int x) { if (!q2.size() || x > q2.top()) q2.push(x), sum2 += x; else q1.push(x), sum1 += x; if (q1.size() > q2.size() + 1) { sum1 -= q1.top(), sum2 += q1.top(); q2.push(q1.top()); q1.pop(); } if (q2.size() > q1.size() + 1) { sum2 -= q2.top(), sum1 += q2.top(); q1.push(q2.top()); q2.pop(); } return; } inline int ask() { if (q1.size() > q2.size()) return q1.top(); return q2.top(); } int judge(int id) { int ans = 1; if (son[id][0]) ans++; if (son[id][1]) ans += 2; return ans; } void push_up(int id) { siz[id] = siz[son[id][0]] + siz[son[id][1]] + num[id]; return; } void rotate(int &p, int d) { int r = son[p][d ^ 1]; son[p][d ^ 1] = son[r][d]; son[r][d] = p; push_up(p); push_up(r); p = r; return; } void ins(int &p, int d) { if (!p) { p = ++cnt; v[p] = d; num[p] = siz[p] = 1; rd[p] = rand(); return; } if (v[p] == d) { num[p]++; siz[p]++; return; } int t = 0; if (v[p] < d) t = 1; ins(son[p][t], d); if (rd[p] < rd[son[p][t]]) rotate(p, t ^ 1); push_up(p); return; } int rk(int p, int x) { if (!p) return 0; if (v[p] == x) return siz[son[p][0]] + 1; if (v[p] < x) return siz[son[p][0]] + rk(son[p][1], x) + num[p]; if (v[p] > x) return rk(son[p][0], x); } int find(int p, int x) { if (!p) return 0; if (siz[son[p][0]] >= x) return find(son[p][0], x); if (siz[son[p][0]] + num[p] >= x) return v[p]; return find(son[p][1], x - (siz[son[p][0]] + num[p])); } int main() { srand((unsigned)time(NULL)); scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &x); a[x] = i; } for (int i = 1; i <= n; i++) { update(a[i]); int x = ask(); if (q2.size() >= q1.size()) { ans = sum2 - (1ll * q2.size() * x) - 1ll * (q2.size() - 1) * q2.size() / 2; ans += (1ll * q1.size() * x) - sum1 - 1ll * (q1.size() + 1) * q1.size() / 2; } else { ans = sum2 - (1ll * q2.size() * x) - 1ll * (q2.size() + 1) * q2.size() / 2; ans += (1ll * q1.size() * x) - sum1 - 1ll * (q1.size() - 1) * q1.size() / 2; } ins(root, a[i]); long long rr = rk(root, a[i]); las += i - rr; ans += las; printf("%lld ", ans); } return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> const long long maxn = 200000 + 10; class BIT { private: long long n; long long bit[maxn]; public: BIT(long long size) { n = size; std::fill(bit, bit + maxn, 0); } void add(long long x, long long v) { while (x <= this->n) { bit[x] += v; x += ((x) & (-x)); } } long long query(long long x) { long long ans = 0; while (x) { ans += bit[x]; x -= ((x) & (-x)); } return ans; } long long find_k(long long k) { long long bits = 0; long long n = this->n; while (n) { n >>= 1; bits++; } long long mask = 0; long long cnt = 0; for (long long i = bits; i >= 0; i--) { mask += 1 << i; if (mask > this->n || cnt + bit[mask] >= k) mask -= (1 << i); else cnt += bit[mask]; } return mask + 1; } }; long long n, pos[maxn]; struct TreeNode { long long zeros, preadd, sufadd; long long presum, sufsum; }; TreeNode seg[1111111]; void build_tree(long long node, long long L, long long R) { seg[node].zeros = R - L + 1; if (L == R) return; long long M = L + (R - L) / 2; build_tree(node << 1, L, M); build_tree(node << 1 | 1, M + 1, R); } void push_down(long long node, long long L, long long R) { long long lch = node << 1; long long rch = node << 1 | 1; if (seg[node].preadd) { seg[lch].preadd += seg[node].preadd; seg[rch].preadd += seg[node].preadd; seg[lch].presum += seg[lch].zeros * 1LL * seg[node].preadd; seg[rch].presum += seg[rch].zeros * 1LL * seg[node].preadd; seg[node].preadd = 0; } if (seg[node].sufadd) { seg[lch].sufadd += seg[node].sufadd; seg[rch].sufadd += seg[node].sufadd; seg[lch].sufsum += seg[lch].zeros * 1LL * seg[node].sufadd; seg[rch].sufsum += seg[rch].zeros * 1LL * seg[node].sufadd; seg[node].sufadd = 0; } } long long qL, qR; void add(long long node, long long L, long long R, long long type) { if (qL <= L && R <= qR) { if (type == 0) { seg[node].preadd += 1; seg[node].presum += (long long)seg[node].zeros; } else { seg[node].sufadd += 1; seg[node].sufsum += (long long)seg[node].zeros; } return; } push_down(node, L, R); long long M = L + (R - L) / 2; if (qL <= M) add(node << 1, L, M, type); if (qR > M) add(node << 1 | 1, M + 1, R, type); long long lch = node << 1; long long rch = node << 1 | 1; seg[node].zeros = seg[lch].zeros + seg[rch].zeros; seg[node].presum = seg[lch].presum + seg[rch].presum; seg[node].sufsum = seg[lch].sufsum + seg[rch].sufsum; } void erase(long long node, long long L, long long R, long long p) { if (L == R) { memset(seg + node, 0, sizeof(TreeNode)); return; } long long M = L + (R - L) / 2; long long lch = node << 1; long long rch = node << 1 | 1; push_down(node, L, R); if (p <= M) erase(lch, L, M, p); else erase(rch, M + 1, R, p); seg[node].zeros = seg[lch].zeros + seg[rch].zeros; seg[node].presum = seg[lch].presum + seg[rch].presum; seg[node].sufsum = seg[lch].sufsum + seg[rch].sufsum; } long long query(long long node, long long L, long long R, long long type) { if (qL <= L && R <= qR) { if (type == 0) return seg[node].presum; else return seg[node].sufsum; } push_down(node, L, R); long long ans = 0; long long M = L + (R - L) / 2; if (qL <= M) ans += query(node << 1, L, M, type); if (qR > M) ans += query(node << 1 | 1, M + 1, R, type); return ans; } int main() { scanf("%lld", &n); BIT bit(n); for (long long i = 1; i <= n; i++) { long long a; scanf("%lld", &a); pos[a] = i; } build_tree(1, 1, n); long long inversion = 0; for (long long i = 1; i <= n; i++) { const long long &p = pos[i]; inversion += i - 1 - bit.query(p); bit.add(p, 1); qL = p + 1; qR = n; if (qL <= qR) add(1, 1, n, 0); qL = 1; qR = p - 1; if (qL <= qR) add(1, 1, n, 1); erase(1, 1, n, p); long long sum = 0; long long median = bit.find_k((i + 1) / 2); qL = 1; qR = median - 1; if (qL <= qR) { sum += query(1, 1, n, 0); } qL = median + 1; qR = n; if (qL <= qR) { sum += query(1, 1, n, 1); } long long ans = inversion + sum; printf("%lld ", ans); } printf("\n"); return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
java
import java.util.Scanner; public class KIntegers { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] p = new int[n + 1]; int[] q = new int[n + 1]; for (int j = 1; j <= n; j++) { p[j] = in.nextInt(); q[p[j]] = j; } BinaryIndexTree bit1 = new BinaryIndexTree(1, n); BinaryIndexTree bit2 = new BinaryIndexTree(1, n); StringBuilder builder = new StringBuilder(); long curr = 0; for (int j = 1; j <= n; j++) { curr += bit1.query(q[j] + 1, n); //System.out.println("j = " + j + ", curr = " + curr); bit1.update(q[j], 1L); bit2.update(q[j], q[j]); int lower = 1; int upper = n; while (upper > lower) { int mid = (lower + upper + 1) / 2; if (bit1.query(1, mid - 1) > j / 2) { upper = mid - 1; } else { lower = mid; } } long above = bit1.query(upper + 1, n); long add1 = bit2.query(upper + 1, n) - (((long) upper) * above) - ((above * (above + 1)) / 2L); long below = bit1.query(1, upper - 1); long add2 = (((long) upper) * below) - ((below * (below + 1)) / 2L) - bit2.query(1, upper - 1); //System.out.println("upper = " + upper); //System.out.println("above = " + above + ", below = " + below); //System.out.println("add1 = " + add1 + ", add2 = " + add2); builder.append(curr + add1 + add2).append(' '); } System.out.println(builder); } static class BinaryIndexTree { final long[] val; final int treeFrom; BinaryIndexTree(int treeFrom, int treeTo) { val = new long[treeTo - treeFrom + 2]; this.treeFrom = treeFrom; } void update(int index, long delta) { for (int i = index + 1 - treeFrom; i < val.length; i += i & -i) { val[i] += delta; } } long query(int to) { long res = 0; for (int i = to + 1 - treeFrom; i > 0; i -= i & -i) { res += val[i]; } return res; } long query(int from, int to) { if (to < from) { return 0; } return query(to) - query(from - 1); } } }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long MAXN = 200000 + 10; long long BIT1[MAXN], BIT2[MAXN]; long long N; void update(long long p, long long num, long long *BIT) { for (; p <= N; p += p & -p) { BIT[p] += num; } } long long query(long long p, long long *BIT) { long long sum = 0; for (; p > 0; p -= p & -p) { sum += BIT[p]; } return sum; } long long arr_num[MAXN], arr_pos[MAXN]; signed main(void) { cin >> N; for (long long i = 1; i <= N; i++) { cin >> arr_num[i]; arr_pos[arr_num[i]] = i; } long long ans = 0; for (long long i = 1; i <= N; i++) { ans += i - query(arr_pos[i], BIT1) - 1; update(arr_pos[i], 1, BIT1); update(arr_pos[i], arr_pos[i], BIT2); long long lb = 1, ub = N; while (ub > lb) { long long mid = (lb + ub + 1) >> 1; if (query(mid - 1, BIT1) > i / 2) { ub = mid - 1; } else { lb = mid; } } long long upper = query(N, BIT1) - query(ub, BIT1); long long add1 = query(N, BIT2) - query(ub, BIT2) - ub * upper - (1 + upper) * upper / 2; long long lower = query(ub - 1, BIT1); long long add2 = ub * lower - query(ub - 1, BIT2) - (1 + lower) * lower / 2; cout << ans + add1 + add2 << " "; } cout << endl; return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 5; long long sum1[MAXN], sum2[MAXN], a[MAXN], pos[MAXN]; void add1(long long x, long long y) { if (!x) return; for (; x < MAXN; x += x & -x) { sum1[x] += y; } } long long query1(long long x) { long long ans = 0; for (; x; x -= x & -x) { ans += sum1[x]; } return ans; } void add2(long long x, long long y) { if (!x) return; for (; x < MAXN; x += x & -x) { sum2[x] += y; } } long long query2(long long x) { long long ans = 0; for (; x; x -= x & -x) { ans += sum2[x]; } return ans; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n; while (cin >> n) { memset(sum1, 0, sizeof(sum1)); memset(sum2, 0, sizeof(sum2)); long long ans = 0; for (int i = 1; i <= n; i++) cin >> a[i], pos[a[i]] = i; for (int i = 1; i <= n; i++) { int p = pos[i]; add1(p, 1); add2(p, p); ans += i - query1(p); long long pp = ans; long long l = 1, r = n, temp; while (l <= r) { int mid = (l + r) >> 1; if (query1(mid) * 2 > i) { temp = mid; r = mid - 1; } else l = mid + 1; } long long x = query1(temp); pp += (temp - x + 1 + temp) * x / 2 - query2(temp); x = i - x; pp += query2(n) - query2(temp) - ((temp + 1 + temp + x) * x / 2); cout << pp << " "; } cout << endl; } return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 3e5 + 5; const int INF = 0x3f3f3f3f; const int mod = 1e9 + 7; const double Pi = acos(-1); long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } double dpow(double a, long long b) { double ans = 1.0; while (b) { if (b % 2) ans = ans * a; a = a * a; b /= 2; } return ans; } long long quick_pow(long long x, long long y) { long long ans = 1; while (y) { if (y & 1) { ans = ans * x % mod; } x = x * x % mod; y >>= 1; } return ans; } int n; int a[maxn], c[maxn]; int lowbit(int x) { return x & (-x); } long long bit1[maxn], bit2[maxn]; void add(long long *bit, int pos, int val) { while (pos < maxn) { bit[pos] += val; pos += lowbit(pos); } } long long query(long long *bit, int pos) { long long ans = 0; while (pos) { ans += bit[pos]; pos -= lowbit(pos); } return ans; } int bs(long long *bit, int val) { int i = 0; for (int j = 19; j >= 0; j--) { if ((i | 1 << j) < maxn) { if (bit[i | 1 << j] <= val) { val -= bit[i |= 1 << j]; } } } return i; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); c[a[i]] = i; } long long cnt = 0; for (int i = 1; i <= n; i++) { int p = c[i]; add(bit1, p, 1); cnt += i - query(bit1, p); add(bit2, p, p); int pos = bs(bit1, i / 2) + 1; long long sum = 0; long long aa = i / 2, bb = i - i / 2 - 1; sum += (long long)aa * pos - (long long)aa * (aa + 1) / 2 - query(bit2, pos - 1); sum += (query(bit2, maxn) - query(bit2, pos)) - (long long)bb * pos - (long long)bb * (bb + 1) / 2; printf("%lld\n", sum + cnt); } return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
python2
n = input() arr = map(int,raw_input().split(" ")) trees = [0]*(1+n) dic = [0]*(n+1) ans = [0]*n def update(t,i,v): while i < len(t): t[i] += v i += lowbit(i) def lowbit(x): return x&-x def sum(t,i): ans = 0 while i>0: ans += t[i] i -= lowbit(i) return ans def getmid(arr,l1,flag): low,high = 1,n if l1%2 == 0 and flag: midv = l1/2 else: midv = l1/2+1 while low <= high: mid = (low+high)/2 ret = sum(arr,mid) if ret >= midv: high = mid-1 else: low = mid+1 return low for i in range(n): dic[arr[i]]=i+1 for i in range(1,n+1): ans[i-1] += sum(trees,n)-sum(trees,dic[i]) if i>=2: ans[i-1] += ans[i-2] update(trees,dic[i],1) visited = [0]*(1+n) mid = 0 last = 0 for i in range(1,n+1): update(visited,dic[i],1) mid = getmid(visited,i,dic[i]>mid) tt = sum(visited,dic[i]) minus = min(tt-1,i-tt) tmp = abs(dic[i]-mid-(tt-sum(visited,mid)))- minus ans[i-1] += tmp+last last = tmp+last print " ".join(map(str,ans))
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int s[200005], sum[200005], ara[200005], p[200005], seg[4 * 200005]; void add(int x, int n) { while (x <= n) { sum[x]++; x += (x & -x); } return; } int query(int x) { int ret = 0; while (x) { ret += sum[x]; x -= (x & -x); } return ret; } void update(int n, int s, int e, int indx) { if (s == indx && e == indx) { seg[n]++; return; } if (s > indx || e < indx) return; int mid = (s + e) / 2; update(n * 2, s, mid, indx); update(n * 2 + 1, mid + 1, e, indx); seg[n] = seg[n * 2] + seg[n * 2 + 1]; return; } int query(int n, int s, int e, int k) { if (s == e && k == 1) return s; int mid = (s + e) / 2; if (seg[n * 2] < k) return query(n * 2 + 1, mid + 1, e, k - seg[n * 2]); else return query(n * 2, s, mid, k); } int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &ara[i]), p[ara[i]] = i; for (int i = 1; i <= n; i++) s[i] = query(n) - query(p[i]), add(p[i], n); printf("0"); long long res = 0; memset(sum, 0, sizeof sum); add(p[1], n); update(1, 1, n, p[1]); int mn = p[1], mx = p[1]; for (int i = 2; i <= n; i++) { res += s[i]; update(1, 1, n, p[i]); add(p[i], n); if (p[i] < mn || p[i] > mx) mn = min(mn, p[i]), mx = max(mx, p[i]); else { int x1 = query(p[i] - 1); int x2 = query(n) - x1 - 1; res -= min(x1, x2); } int l = p[i], r = query(1, 1, n, (i + 1) / 2); if (!(i & 1) && p[i] <= r) r = query(1, 1, n, (i + 2) / 2); if (l > r) swap(l, r); if (l < r) res += (r - l + 1) - (query(r) - query(l - 1)); printf(" %lld", res); } puts(""); return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
python3
from bisect import bisect_right, bisect_left # instead of AVLTree class BITbisect(): def __init__(self, InputProbNumbers): # 座圧 self.ind_to_co = [-10**18] self.co_to_ind = {} for ind, num in enumerate(sorted(list(set(InputProbNumbers)))): self.ind_to_co.append(num) self.co_to_ind[num] = ind+1 self.max = len(self.co_to_ind) self.data = [0]*(self.max+1) def __str__(self): retList = [] for i in range(1, self.max+1): x = self.ind_to_co[i] if self.count(x): c = self.count(x) for _ in range(c): retList.append(x) return "[" + ", ".join([str(a) for a in retList]) + "]" def __getitem__(self, key): key += 1 s = 0 ind = 0 l = self.max.bit_length() for i in reversed(range(l)): if ind + (1<<i) <= self.max: if s + self.data[ind+(1<<i)] < key: s += self.data[ind+(1<<i)] ind += (1<<i) if ind == self.max or key < 0: raise IndexError("BIT index out of range") return self.ind_to_co[ind+1] def __len__(self): return self._query_sum(self.max) def __contains__(self, num): if not num in self.co_to_ind: return False return self.count(num) > 0 # 0からiまでの区間和 # 左に進んでいく def _query_sum(self, i): s = 0 while i > 0: s += self.data[i] i -= i & -i return s # i番目の要素にxを足す # 上に登っていく def _add(self, i, x): while i <= self.max: self.data[i] += x i += i & -i # 値xを挿入 def push(self, x): if not x in self.co_to_ind: raise KeyError("The pushing number didnt initialized") self._add(self.co_to_ind[x], 1) # 値xを削除 def delete(self, x): if not x in self.co_to_ind: raise KeyError("The deleting number didnt initialized") if self.count(x) <= 0: raise ValueError("The deleting number doesnt exist") self._add(self.co_to_ind[x], -1) # 要素xの個数 def count(self, x): return self._query_sum(self.co_to_ind[x]) - self._query_sum(self.co_to_ind[x]-1) # 値xを超える最低ind def bisect_right(self, x): if x in self.co_to_ind: i = self.co_to_ind[x] else: i = bisect_right(self.ind_to_co, x) - 1 return self._query_sum(i) # 値xを下回る最低ind def bisect_left(self, x): if x in self.co_to_ind: i = self.co_to_ind[x] else: i = bisect_left(self.ind_to_co, x) if i == 1: return 0 return self._query_sum(i-1) import sys input = sys.stdin.readline N = int(input()) A = list(map(int, input().split())) Ind = [0]*(N+1) for i, a in enumerate(A): Ind[a] = i+1 Bit = BITbisect(list(range(N+1))) ans = [0] Bit.push(Ind[1]) a = 0 for n in range(2, N+1): ind = Ind[n] f = Bit.bisect_left(ind) #print(Bit) l = len(Bit) if l%2 == 0: if f == l//2: a += l//2-l//2 elif f < l//2: p1 = Bit[l//2-1] a += (p1-ind-1) - (l//2-1) + l-f else: p2 = Bit[l//2] a += (ind-p2-1) - (l//2-1) + l-f else: p1 = Bit[l//2] #print(f, p1, ind, l) if f <= l//2: a += (p1-ind-1) - l//2 + l-f else: a += (ind-p1-1) - l//2 + l-f ans.append(a) Bit.push(ind) print(*ans, sep=" ")
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 7; int n, sum[maxn], r[maxn]; void add(int i) { while (i <= n) { sum[i]++; i += i & (-i); } } int getsum(int i) { int ans = 0; while (i) { ans += sum[i]; i -= i & (-i); } return ans; } int bs(int s, int e, int t) { int mid = (s + e) / 2; if (s >= e) return mid; if (getsum(mid) >= t) return bs(s, mid, t); else return bs(mid + 1, e, t); } int main() { cin >> n; for (int i = 1; i <= n; i++) { int x; cin >> x; r[x] = i; } long long ans = 0; for (int i = 1; i <= n; i++) { ans += i - 1 - getsum(r[i]); int s, e; s = bs(1, n, i / 2); if (i % 2 == 1) e = bs(1, n, i / 2 + 1); else e = s; if (!(s <= r[i] && r[i] <= e)) { if (r[i] < s) ans += s - r[i] - 1 - getsum(s - 1); else ans += r[i] - e + getsum(e) - i; } else ans -= i / 2; cout << ans << " "; add(r[i]); } cout << '\n'; return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
java
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.StringTokenizer; import java.io.BufferedReader; import java.util.Comparator; import java.util.Collections; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); KIntegers solver = new KIntegers(); solver.solve(1, in, out); out.close(); } static class KIntegers { public void solve(int testNumber, InputReader in, PrintWriter out) { int N = in.nextInt(); ArrayList<Integer> arr = new ArrayList<>(); int[] p = new int[N]; FenwickTree invert = new FenwickTree(N); for (int i = 0; i < N; i++) { p[i] = in.nextInt(); arr.add(i); } Collections.sort(arr, new Comparator<Integer>() { public int compare(Integer integer, Integer t1) { return Integer.compare(p[integer], p[t1]); } }); long res = 0; FenwickTree sum = new FenwickTree(N); for (long i = 1; i <= N; i++) { int nextInd = arr.get((int) (i - 1)); res += invert.sum(nextInd, N - 1); sum.add(nextInd, nextInd); invert.add(nextInd, 1); int mid = getMid(invert); if ((i) % 2 == 1) { out.println(res - sum.sum(0, mid - 1) + sum.sum(mid + 1, N - 1) - ((i / 2) * (i / 2 + 1))); } else { out.println(res + mid * (i / 2) - sum.sum(0, mid - 1) + sum.sum(mid + 1, N - 1) - mid * ((i - 1) / 2) - ((i / 2) * (i / 2 + 1)) / 2 - (((i - 1) / 2) * ((i - 1) / 2 + 1)) / 2); } } } public int getMid(FenwickTree tree) { int low = 0; int high = tree.size - 1; while (low < high) { int mid = (low + high + 1) / 2; if (tree.sum(mid - 1) <= tree.sum(tree.size - 1) / 2) { low = mid; } else { high = mid - 1; } } return high; } public class FenwickTree { public long[] BIT; public int size = 0; public FenwickTree(int N) { BIT = new long[N]; size = N; } public void add(int id, int add) { for (int i = id; i < size; i |= i + 1) { BIT[i] += add; } } public long sum(int l, int r) { return sum(r) - sum(l - 1); } public long sum(int r) { if (r < 0 || r >= size) { return 0; } long res = 0; for (int i = r; i >= 0; i = ((i) & (i + 1)) - 1) { res += BIT[i]; } return res; } } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using dd = double; using pll = pair<ll, ll>; using tll = tuple<ll, ll, ll>; using vll = vector<ll>; using vdd = vector<dd>; using vpll = vector<pll>; using vtll = vector<tll>; using vvll = vector<vll>; using vvdd = vector<vdd>; using vvpll = vector<vpll>; using vvtll = vector<vtll>; using vvvll = vector<vvll>; using vvvdd = vector<vvdd>; using vvvpll = vector<vvpll>; using vvvtll = vector<vvtll>; using vvvvll = vector<vvvll>; using vvvvdd = vector<vvvdd>; using vvvvpll = vector<vvvpll>; using vvvvtll = vector<vvvtll>; constexpr ll INF = 1LL << 60; constexpr dd EPS = 1e-11; struct Fast { Fast() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(std::numeric_limits<double>::max_digits10); } } fast; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline T MaxE(vector<T> &v, ll S, ll E) { T m = v[S]; for (ll i = (S); i <= (E); i++) chmax(m, v[i]); return m; } template <class T> inline T MinE(vector<T> &v, ll S, ll E) { T m = v[S]; for (ll i = (S); i <= (E); i++) chmin(m, v[i]); return m; } template <class T> inline T MaxE(vector<T> &v, ll N) { return MaxE(v, 0, N - 1); } template <class T> inline T MinE(vector<T> &v, ll N) { return MinE(v, 0, N - 1); } template <class T> inline T MaxE(vector<T> &v) { return MaxE(v, (ll)v.size()); } template <class T> inline T MinE(vector<T> &v) { return MinE(v, (ll)v.size()); } template <class T> inline ll MaxI(vector<T> &v, ll S, ll E) { ll m = S; for (ll i = (S); i <= (E); i++) { if (v[i] > v[m]) m = i; } return m; } template <class T> inline ll MinI(vector<T> &v, ll S, ll E) { ll m = S; for (ll i = (S); i <= (E); i++) { if (v[i] < v[m]) m = i; } return m; } template <class T> inline ll MaxI(vector<T> &v, ll N) { return MaxI(v, 0, N - 1); } template <class T> inline ll MinI(vector<T> &v, ll N) { return MinI(v, 0, N - 1); } template <class T> inline ll MaxI(vector<T> &v) { return MaxI(v, (ll)v.size()); } template <class T> inline ll MinI(vector<T> &v) { return MinI(v, (ll)v.size()); } template <class T> inline T Sum(vector<T> &v, ll S, ll E) { T s = v[S]; for (ll i = (S + 1); i <= (E); i++) s += v[i]; return s; } template <class T> inline T Sum(vector<T> &v, ll N) { return Sum(v, 0, N - 1); } template <class T> inline T Sum(vector<T> &v) { return Sum(v, v.size()); } template <class T> inline ll Size(T &v) { return (ll)v.size(); } template <class T> inline T POW(T a, ll n) { T r = 1; for (; n > 0; n >>= 1, a *= a) { if (n & 1) r *= a; } return r; } inline ll POW(int a, ll n) { return POW((ll)a, n); } inline ll MSB(ll a) { for (ll o = 63, x = -1;;) { ll m = (o + x) / 2; if (a < (1LL << m)) o = m; else x = m; if (o - x == 1) return x; } } inline ll CEIL(ll a, ll b) { return (a + b - 1) / b; } ll Gcd(ll a, ll b) { return (b == 0) ? a : Gcd(b, a % b); } ll Lcm(ll a, ll b) { return a * b / Gcd(a, b); } inline vll Bit2Idx(ll m) { vll v; for (ll i = 0; m; m >>= 1, i++) if (m & 1) v.push_back(i); return move(v); } inline ll BitNum(ll m) { for (ll c = 0;; m >>= 1) { c += m & 1; if (!m) return c; } } inline ll Bit(ll s, ll i) { return (s >> i) & 1; } inline ll BitOn(ll s, ll i) { return s | (1LL << i); } inline ll BitOff(ll s, ll i) { return s & ~(1LL << i); } template <class T> inline ll UnderNumOf(vector<T> &v, T x) { return upper_bound(v.begin(), v.end(), x) - v.begin(); } template <class T, class Pr> inline ll UnderNumOf(vector<T> &v, T x, Pr pred) { return upper_bound(v.begin(), v.end(), x, pred) - v.begin(); } template <class T> inline ll OverNumOf(vector<T> &v, T x) { return (ll)v.size() - (lower_bound(v.begin(), v.end(), x) - v.begin()); } template <class T, class Pr> inline ll OverNumOf(vector<T> &v, T x, Pr pred) { return (ll)v.size() - (lower_bound(v.begin(), v.end(), x, pred) - v.begin()); } template <class T = ll> inline vector<T> cinv(ll N) { vector<T> v(N); for (ll i = (0); i <= ((N)-1); i++) cin >> v[i]; return move(v); } template <class T = ll, class S = ll> inline vector<pair<T, S>> cinv2(ll N) { vector<pair<T, S>> v(N); for (ll i = (0); i <= ((N)-1); i++) { cin >> v[i].first >> v[i].second; } return move(v); } template <class T = ll, class S = ll, class R = ll> inline vector<tuple<T, S, R>> cinv3(ll N) { vector<tuple<T, S, R>> v(N); for (ll i = (0); i <= ((N)-1); i++) { cin >> get<0>(v[i]) >> get<1>(v[i]) >> get<2>(v[i]); } return move(v); } template <class T = ll, class S = ll, class R = ll, class Q = ll> inline vector<tuple<T, S, R, Q>> cinv4(ll N) { vector<tuple<T, S, R, Q>> v(N); for (ll i = (0); i <= ((N)-1); i++) { cin >> get<0>(v[i]) >> get<1>(v[i]) >> get<2>(v[i]) >> get<3>(v[i]); } return move(v); } template <class T = ll> inline vector<vector<T>> cinvv(ll N, ll M) { vector<vector<T>> vv(N); for (ll i = (0); i <= ((N)-1); i++) vv[i] = cinv(M); return move(vv); } template <class T> inline void coutv(vector<T> &v, char deli = ' ') { ll N = (ll)v.size(); for (ll i = (0); i <= ((N)-1); i++) { cout << v[i] << ((i == N - 1) ? '\n' : deli); } } template <class T> void bye(T a) { cout << a << '\n'; exit(0); } template <class T> void Dump(vector<T> &v) {} template <class T> void Dump(vector<vector<T>> &v) {} template <class T> void Dump(vector<vector<vector<T>>> &v) {} template <class T> void Dump(vector<vector<vector<vector<T>>>> &v) {} template <class T> void Dump(vector<vector<vector<vector<vector<T>>>>> &v) {} template <ll MOD> struct mll_ { ll val; mll_(ll v = 0) : val(v % MOD) { if (val < 0) val += MOD; } mll_ operator-() const { return -val; } mll_ operator+(const mll_ &b) const { return val + b.val; } mll_ operator-(const mll_ &b) const { return val - b.val; } mll_ operator*(const mll_ &b) const { return val * b.val; } mll_ operator/(const mll_ &b) const { return mll_(*this) /= b; } mll_ operator+(ll b) const { return *this + mll_(b); } mll_ operator-(ll b) const { return *this - mll_(b); } mll_ operator*(ll b) const { return *this * mll_(b); } friend mll_ operator+(ll a, const mll_ &b) { return b + a; } friend mll_ operator-(ll a, const mll_ &b) { return -b + a; } friend mll_ operator*(ll a, const mll_ &b) { return b * a; } friend mll_ operator/(ll a, const mll_ &b) { return mll_(a) / b; } mll_ &operator+=(const mll_ &b) { val = (val + b.val) % MOD; return *this; } mll_ &operator-=(const mll_ &b) { val = (val + MOD - b.val) % MOD; return *this; } mll_ &operator*=(const mll_ &b) { val = (val * b.val) % MOD; return *this; } mll_ &operator/=(const mll_ &b) { ll c = b.val, d = MOD, u = 1, v = 0; while (d) { ll t = c / d; c -= t * d; swap(c, d); u -= t * v; swap(u, v); } val = val * u % MOD; if (val < 0) val += MOD; return *this; } mll_ &operator+=(ll b) { return *this += mll_(b); } mll_ &operator-=(ll b) { return *this -= mll_(b); } mll_ &operator*=(ll b) { return *this *= mll_(b); } mll_ &operator/=(ll b) { return *this /= mll_(b); } bool operator==(const mll_ &b) { return val == b.val; } bool operator!=(const mll_ &b) { return val != b.val; } bool operator==(ll b) { return *this == mll_(b); } bool operator!=(ll b) { return *this != mll_(b); } friend bool operator==(ll a, const mll_ &b) { return mll_(a) == b.val; } friend bool operator!=(ll a, const mll_ &b) { return mll_(a) != b.val; } friend ostream &operator<<(ostream &os, const mll_ &a) { return os << a.val; } friend istream &operator>>(istream &is, mll_ &a) { return is >> a.val; } static mll_ Combination(ll a, ll b) { chmin(b, a - b); if (b < 0) return mll_(0); mll_ c = 1; for (ll i = (0); i <= ((b)-1); i++) c *= a - i; for (ll i = (0); i <= ((b)-1); i++) c /= i + 1; return c; } }; using mll = mll_<1000000007LL>; using vmll = std::vector<mll>; using vvmll = std::vector<vmll>; using vvvmll = std::vector<vvmll>; using vvvvmll = std::vector<vvvmll>; using vvvvvmll = std::vector<vvvvmll>; template <class T> struct SegmentTree { using F = function<T(T, T)>; vector<T> dat; ll n; F f; T ti; SegmentTree() {} SegmentTree(vector<T> &v, F f, T ti) { Init(v, f, ti); } void Init(vector<T> &v, F f, T ti) { this->f = f; this->ti = ti; for (n = 1; n < (ll)v.size(); n *= 2) ; dat.resize(2 * n - 1, ti); for (ll i = (0); i <= (((ll)v.size()) - 1); i++) { dat[i + n - 1] = v[i]; } for (ll i = ((n - 1) - 1); i >= (0); i--) { dat[i] = f(dat[2 * i + 1], dat[2 * i + 2]); } } void Set(ll i, T x) { i += n - 1; dat[i] = x; while (i > 0) { i = (i - 1) / 2; dat[i] = f(dat[2 * i + 1], dat[2 * i + 2]); } } T Range(ll a, ll b) { return range(a, b, 0, 0, n); } T operator[](ll i) { return dat[i + n - 1]; } void Dump(ll w = 5) { for (ll i = (0); i <= ((n)-1); i++) for (ll k = i + n - 1, m = 1, p = 1; k >= 0; p = m, m *= k % 2, k = (k == 0) ? -1 : (k - 1) / 2) { if (m) cerr << ((k < n - 1) ? " ┬ " : "") << setw(w) << dat[k]; else cerr << ((p & ~m) ? " ┘ " : ""); if (k == 0) cerr << '\n'; } cerr << '\n'; } T range(ll a, ll b, ll k, ll l, ll r) { if (r <= a || b <= l) return ti; if (a <= l && r <= b) return dat[k]; return f(range(a, b, k * 2 + 1, l, (l + r) / 2), range(a, b, k * 2 + 2, (l + r) / 2, r)); } }; void solve() { ll n; cin >> n; vector<ll> P = cinv<ll>(n); vll loc(n + 1); for (ll i = (0); i <= (n - 1); i++) { loc[P[i]] = i; } vll ans; ans.push_back(0); set<ll> buf; buf.insert(loc[1]); ll x = 0; ll y = 0; auto it = buf.begin(); vll ini(n, 0); ini[loc[1]] = 1; SegmentTree<ll> sgt( ini, [](ll x, ll y) { return x + y; }, 0); ll T = 0; ll S = 0; for (ll k = (1); k <= (n - 1); k++) { ll p = loc[k + 1]; if (k % 2 == 0) { auto iit = it; iit++; if (p < *it) { ll inter = *it - p - 1; inter -= sgt.Range(p + 1, (*it)); T += inter; T -= sgt.Range(0, p); } else if (*iit < p) { ll inter = p - *iit - 1; inter -= sgt.Range((*iit) + 1, p); T += inter; T -= sgt.Range(p + 1, n); } else { T -= sgt.Range(p + 1, n); } } else { if (p < *it) { ll inter = *it - p - 1; inter -= sgt.Range(p + 1, (*it)); T += inter; T -= sgt.Range(0, p); } else { ll inter = p - *it - 1; inter -= sgt.Range((*it) + 1, p); T += inter; T -= sgt.Range(p + 1, n); } } S += sgt.Range(p + 1, n); ans.push_back(T + S); if (p < *it) x++; else y++; buf.insert(p); if (x - 1 == y) { it--; x--; y++; } else if (x + 2 == y) { it++; x++; y--; } sgt.Set(p, 1); } coutv(ans); } int main() { solve(); return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
java
/* If you want to aim high, aim high Don't let that studying and grades consume you Just live life young ****************************** If I'm the sun, you're the moon Because when I go up, you go down ******************************* I'm working for the day I will surpass you https://www.a2oj.com/Ladder16.html */ import java.util.*; import java.io.*; import java.math.*; public class x1269E { public static void main(String omkar[]) throws Exception { BufferedReader infile = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(infile.readLine()); int N = Integer.parseInt(st.nextToken()); int[] arr = new int[N]; int[] map = new int[N+1]; st = new StringTokenizer(infile.readLine()); for(int i=0; i < N; i++) { arr[i] = Integer.parseInt(st.nextToken()); map[arr[i]] = i; } //brain tumor StringBuilder sb = new StringBuilder(); FenwickTree bit = new FenwickTree(N+1); FenwickTree pos = new FenwickTree(N+1); //ArrayList<Integer> pos = new ArrayList<Integer>(); long res = 0L; for(int i=1; i <= N; i++) { int dex = map[i]+1; res += bit.find(dex, N); bit.add(dex, 1); pos.add(dex, dex); //has pos.size()/2 elements to the left int low = 1; int high = N; while(low != high) { int mid = (low+high+1)/2; if(bit.find(mid-1) > i/2) high = mid-1; else low = mid; } //fft (fuck fenwick trees) long left_count = bit.find(low-1); long temp = -1*pos.find(low-1); temp -= left_count*(left_count+1)/2; temp += left_count*(long)low; long right_count = bit.find(low+1, N); long temp2 = pos.find(low+1, N); temp2 -= right_count*(right_count+1)/2; temp2 -= right_count*(long)low; sb.append(res+temp+temp2+" "); } System.out.println(sb); } } class FenwickTree { //1 indexed public long[] tree; public int size; public FenwickTree(int size) { this.size = size; tree = new long[size+5]; } public void add(int i, int v) { while(i <= size) { tree[i] += v; i += i&-i; } } public long find(int i) { long res = 0L; while(i >= 1) { res += tree[i]; i -= i&-i; } return res; } public long find(int l, int r) { return find(r)-find(l-1); } }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MX = 2e5 + 5, MXX = 23; const long long mod = 1e9 + 7, inf = 1e18 + 6; int n, a[MX], ind[MX], ver[MX]; void add(int l) { for (int i = l + 1; i < MX; i += i & -i) ver[i]++; } int ask(int pn) { int ans = 0; for (pn++; pn; pn -= pn & -pn) ans += ver[pn]; return ans; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << fixed << setprecision(9); cin >> n; for (int i = 0; i < n; ++i) cin >> a[i]; for (int i = 0; i < n; ++i) ind[a[i]] = i; set<int> nw; long long ans = 0; int md = ind[1]; for (int i = 1; i < n + 1; ++i) { nw.insert(ind[i]); auto v = nw.find(ind[i]); add(ind[i]); int ver = ask(ind[i]); if (i & 1) { if (ind[i] > md) md = *next(nw.find(md), 1); } else { if (ind[i] < md) md = *prev(nw.find(md), 1); } if (md != ind[i]) { int ls = ((i % 2 == 0) && md > ind[i] ? *next(nw.find(md), 1) : md); int dis = ls - ind[i]; dis = max(dis, -dis); dis--; int fr = ((i % 2 == 0) && md > ind[i] ? (i / 2) : ((i - 1) / 2)); fr++; int bs = ver - fr; bs = max(bs, -bs); bs--; ans += (long long)(dis - bs); } if (md == ind[i] && (i % 2 == 0)) { ans += *next(nw.find(md), 1) - md - 1; } ans -= (min(i - ver, ver - 1)); ans += i - ver; cout << ans << ' '; } cout << "\n"; return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> int size = 256 * 1024; int Count(std::vector<int>& tree, int l, int r) { l += size; r += size; int ans = 0; while (l <= r) { if (l % 2) { ans += tree[l]; ++l; } if (r % 2 == 0) { ans += tree[r]; --r; } if (l > r) break; r >>= 1; l >>= 1; } return ans; } void Add(std::vector<int>& tree, int ind) { ind += size; while (ind) { ++tree[ind]; ind >>= 1; } } int main() { int64_t own = 0; int n; std::cin >> n; std::vector<int> array(n); std::vector<int> where(n); std::vector<int> add(n); for (int i = 0; i < n; ++i) { std::cin >> array[i]; --array[i]; where[array[i]] = i; } { std::vector<int> tree(size * 2, 0); for (int i = 0; i < n; ++i) { add[i] = Count(tree, where[i], n - 1); Add(tree, where[i]); } } std::cout << 0 << ' '; std::set<int> pos; pos.insert(where[0]); int median = where[0]; int64_t dist = 0; std::vector<int> tree(size * 2, 0); Add(tree, where[0]); for (int i = 1; i < n; ++i) { own += add[i]; if (where[i] >= median) { dist -= Count(tree, where[i], n - 1); } else { dist -= Count(tree, 0, where[i]); } pos.insert(where[i]); if (where[i] > median && (i % 2 == 0)) { median = *pos.upper_bound(median); } else if (where[i] < median && (i % 2 == 1)) { int n_median = *(--pos.lower_bound(median)); dist += (median - n_median - 1); median = n_median; } if (where[i] > median) { dist += (where[i] - median - Count(tree, median, where[i] - 1)); } else { dist += (median - where[i] - Count(tree, where[i] + 1, median)); } std::cout << own + dist << ' '; Add(tree, where[i]); } return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long N = 200010; long long tr1[N], tr2[N]; long long n; long long c[N]; long long lowbit(long long x) { return x & -x; } void ud(long long tr[], long long x, long long d) { for (long long i = x; i <= n; i += lowbit(i)) { tr[i] += d; } } long long qur(long long tr[], long long x) { long long ans = 0; for (long long i = x; i; i -= lowbit(i)) { ans += tr[i]; } return ans; } long long get_pos(long long tr[], long long x) { long long l = 0, r = n; while (l < r) { long long mid = (l + r) >> 1; if (qur(tr, mid) >= x) r = mid; else l = mid + 1; } return l; } int main() { cin >> n; for (long long i = 1; i <= n; i++) { long long x; cin >> x; c[x] = i; } long long d = 0; for (long long i = 1; i <= n; i++) { ud(tr1, c[i], 1); ud(tr2, c[i], c[i]); d += (i - qur(tr1, c[i])); long long pos = get_pos(tr1, (i + 1) / 2); long long aa = (i - 1) / 2; long long bb = i / 2; long long t1 = (long long)aa * pos - (1 + aa) * aa / 2; long long t2 = (1ll + bb) * bb / 2 + bb * pos; long long q1 = qur(tr2, pos - 1); long long q2 = qur(tr2, n) - qur(tr2, pos); long long ans = q2 - t2 + t1 - q1; cout << (ans + d) << ' '; } }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> const int maxn = 2e5 + 10; const int maxm = 1e5 + 10; const long long int mod = 1e9 + 7; const long long int INF = 1e18 + 100; const int inf = 0x3f3f3f3f; const double pi = acos(-1.0); const double eps = 1e-8; using namespace std; int n, m; int cas, tol, T; set<int> st; int a[maxn], p[maxn]; long long int sum[maxn]; void update(int x) { for (int i = x; i; i -= i & (-i)) sum[i]++; } long long int query(int x) { if (x == 0) return 0; int ans = 0; for (int i = x; i <= n; i += i & (-i)) ans += sum[i]; return ans; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]), p[a[i]] = i; long long int ans = 0, k = 0; printf("0 "); update(p[1]); st.insert(p[1]); auto it = st.begin(); for (int i = 2; i <= n; i++) { ans -= min(query(p[i] + 1), i - 1 - query(p[i] + 1)); ans += query(p[i] + 1); update(p[i]), st.insert(p[i]); if (i % 2 == 0 && p[i] < (*it)) ans += abs((*it) - (*(--it))) - 1; if (i % 2 == 1 && p[i] > (*it)) it++; if (p[i] > (*it)) ans += p[i] - (*it) + 1 - query((*it)) + query(p[i] + 1); else ans += (*it) - p[i] + 1 - query(p[i]) + query((*it) + 1); printf("%lld ", ans); } return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 3e5 + 5; const int INF = 0x3f3f3f3f; const int mod = 1e9 + 7; const double Pi = acos(-1); long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } double dpow(double a, long long b) { double ans = 1.0; while (b) { if (b % 2) ans = ans * a; a = a * a; b /= 2; } return ans; } long long quick_pow(long long x, long long y) { long long ans = 1; while (y) { if (y & 1) { ans = ans * x % mod; } x = x * x % mod; y >>= 1; } return ans; } int n; int a[maxn], c[maxn]; int lowbit(int x) { return x & (-x); } long long bit1[maxn], bit2[maxn]; void add(long long *bit, int pos, int val) { while (pos < maxn) { bit[pos] += val; pos += lowbit(pos); } } long long query(long long *bit, int pos) { long long ans = 0; while (pos) { ans += bit[pos]; pos -= lowbit(pos); } return ans; } int bs(long long *bit, int val) { int i = 0; for (int j = 19; j >= 0; j--) { if ((i | 1 << j) < maxn) { if (bit[i | 1 << j] <= val) { val -= bit[i |= 1 << j]; } } } return i; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); c[a[i]] = i; } long long cnt = 0; for (int i = 1; i <= n; i++) { int p = c[i]; add(bit1, p, 1); cnt += i - query(bit1, p); add(bit2, p, p); int pos = bs(bit1, i / 2) + 1; long long sum = 0; long long aa = i / 2; long long bb = i - i / 2 - 1; sum += (long long)aa * pos - (long long)aa * (aa + 1) / 2 - query(bit2, pos - 1); sum += (query(bit2, maxn) - query(bit2, pos)) - (long long)bb * pos - (long long)bb * (bb + 1) / 2; printf("%lld\n", sum + cnt); } return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct fenwick { int N; vector<int> bit; fenwick(int n = 1e5) { N = n + 5; bit.assign(N, 0); } void resize(int n) { N = n + 5; bit.assign(N, 0); } void update(int x, int val) { while (x < N) { bit[x] += val; x += x & -x; } } int sum(int x) { int ret = 0; while (x > 0) { ret += bit[x]; x -= x & -x; } return ret; } }; int main() { ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0); int n; cin >> n; vector<int> idx(n + 5); for (int i = 1; i <= n; ++i) { int x; cin >> x; idx[x] = i; } fenwick bit(n); set<int, greater<int>> l; set<int> r; long long lsum = 0, rsum = 0, ans = 0; for (int i = 1; i <= n; ++i) { ans += i - 1 - bit.sum(idx[i]); bit.update(idx[i], 1); if (l.size() == 0 || idx[i] < *l.begin()) l.insert(idx[i]), lsum += idx[i]; else r.insert(idx[i]), rsum += idx[i]; if (l.size() > r.size() + 1) { int x = *l.begin(); lsum -= x; rsum += x; l.erase(l.begin()); r.insert(x); } else if (r.size() > l.size()) { int x = *r.begin(); lsum += x; rsum -= x; l.insert(x); r.erase(r.begin()); } long long lcnt = (l.size() * (*l.begin())) - lsum - (l.size() * (l.size() - 1)) / 2; long long rcnt = (rsum - r.size() * (*l.begin())) - (r.size() * (r.size() + 1)) / 2; cout << ans + lcnt + rcnt << ' '; } }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int maxn = 2e5 + 233; int ch[maxn]; int a[maxn], n, p[maxn]; long long ans[maxn], sum[maxn]; void add(int x, int v) { for (int i = x; i <= n; i += i & (-i)) ch[i] += v; } int query(int x) { int sum = 0; for (int i = x; i; i -= i & (-i)) sum += ch[i]; return sum; } void add_sum(int x, int v) { for (int i = x; i <= n; i += i & (-i)) sum[i] += v; } int query_sum(int x) { long long res = 0; for (int i = x; i; i -= i & (-i)) res += sum[i]; return res; } int cal(int n, int x) { int t = query(x); return min(t, n - t); } int find(int k) { k--; int x = 0; for (int i = 20; i >= 0; i--) if (x + (1 << i) <= n && ch[x + (1 << i)] <= k) { x += 1 << i; k -= ch[x]; } return x + 1; } inline long long ari(int n) { return 1ll * n * (n + 1) / 2; } int main() { cin >> n; for (int i = 1; i <= n; i++) scanf("%d", &a[i]), p[a[i]] = i; for (int i = 1; i <= n; i++) { int x = p[i]; ans[i] = ans[i - 1] + (i - 1 - query(x)); add(x, 1); } memset(ch, 0, sizeof(ch)); long long sum = 0; for (int i = 1; i <= n; i++) { int x = p[i]; add(x, 1); add_sum(x, x); sum += x; int k = (i + 1) / 2; long long c = find(k); long long tmp = query_sum(c); long long a = k * c - tmp - ari(k - 1); long long b = (sum - tmp) - (i - k) * c - ari(i - k); ans[i] += a + b; } for (int i = 1; i <= n; i++) printf("%I64d ", ans[i]); return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 9; long long mid[N], a[N], mark[N], k, f1[N][2], n, g[N], h[N], p, ct, cn, tvn = 1, tedad[N]; string s, s2; long long saghf(long long x) { return (x / 2) + (x % 2); } string binary(int m, int k) { string g = ""; for (long long i = 0; i < k; i++) { g += char((m % 2) + '0'); m = m / 2; } return g; } int main() { cin >> n; cn = n; for (long long i = 1; i < n + 1; i++) cin >> a[i], mark[a[i]] = i, f1[i][0] = i - 1, f1[i][1] = i + 1; k = saghf(n); for (long long i = n; i >= 1; i--) { mid[i] = k; if (i % 2 == 0) { if (mark[i] <= k) k = f1[k][1]; } else { if (mark[i] >= k) k = f1[k][0]; } f1[f1[mark[i]][0]][1] = f1[mark[i]][1]; f1[f1[mark[i]][1]][0] = f1[mark[i]][0]; } k = 0; for (long long i = 2; i < n; i++) { if (i % 2 == 0) { if (mid[i] == mid[i - 1]) g[i] = g[i - 1] + (mark[i] - mid[i]) - (i / 2); else g[i] = g[i - 1] + (mid[i - 1] - mid[i]) + (mid[i] - mark[i]) - ((i / 2)); } else { if (mid[i] == mid[i - 1]) g[i] = g[i - 1] + (mid[i] - mark[i]) - (i / 2); else g[i] = g[i - 1] + (mark[i] - mid[i]) - (i / 2); } } for (long long i = 1; i < (n + 3) / 2; i++) { swap(a[i], a[n + 1 - i]); a[i] = n + 1 - a[i]; a[n + 1 - i] = n + 1 - a[n + 1 - i]; } if (n % 2 == 1) a[(n + 1) / 2] = n + 1 - a[(n + 1) / 2]; while (cn) { k++; cn /= 2; tvn *= 2; } tvn /= 2; for (long long i = 1; i < n + 1; i++) { s = binary(a[i], k); p = 0; ct = tvn; long long t = 0, m = tvn * 2 - a[i], p1 = tvn * 2; s2 = binary(m, k); for (long long j = k - 1; j >= 0; j--) { if (s2[j] == '1') { p1 -= ct; t += tedad[p1]; } ct /= 2; } h[n + 1 - a[i]] = t; ct = tvn; for (long long j = k - 1; j >= 0; j--) { if (s[j] == '1') { p += ct; tedad[p]++; } ct /= 2; } } for (long long i = 1; i < n + 1; i++) { h[i] += h[i - 1]; cout << h[i] + g[i] << " "; } }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); typedef long long ll; typedef long double ld; #define pb push_back #define mp make_pair #define ff first #define ss second #define mod 1000000007 #define autoit(x,it) for(auto it = x.begin(); it != x.end(); it++) #define rep(n) for(ll i = 0; i < n; i++) #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; #define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update> using namespace std; ll que(ll st[], ll l, ll r, ll l1, ll r1, ll i) { if(l>r1 || l1>r) return 0; if(l>=l1 && r<=r1) return st[i]; ll mid = (l+r)/2; return que(st,l,mid,l1,r1,2*i+1)+que(st,mid+1,r,l1,r1,2*i+2); } void upd(ll st[], ll l, ll r, ll l1, ll i, ll val) { if(l>l1||r<l1) return; if(l == r) { st[i] = val; return; } ll mid = (l+r)/2; upd(st,l,mid,l1,2*i+1,val); upd(st,mid+1,r,l1,2*i+2,val); st[i] = st[2*i+1] + st[2*i+2]; } int main() { FAST/**/ ll n; cin>>n; ll arr[n]; for(ll i=0;i<n;i++) cin>>arr[i]; ll pos[n+1]; for(ll i=0;i<n;i++) pos[arr[i]] = i; ordered_set s1; ll cnt[n+1]; for(ll i=0;i<n;i++) { cnt[arr[i]] = s1.order_of_key(arr[i]); s1.insert(arr[i]); } ll ht = ceil(log2(n)); ht = (1ll<<(ht + 1)) - 1; vector<ll> ans; ans.pb(0); ll st[ht]; memset(st,0,sizeof(st)); s1.clear(); s1.insert(pos[1]); upd(st,0,n-1,pos[1],0,pos[1]); ll icnt = 0; for(ll i=2;i<=n;i++) { s1.insert(pos[i]); upd(st,0,n-1,pos[i],0,pos[i]); ll curr = *s1.find_by_order((i)/2-1); icnt+= i-1-cnt[i]; ll leftsum = que(st,0,n-1,0,curr-1,0); ll rightsum = que(st,0,n-1,curr+1,n-1,0); ll cleft = i/2 - 1; ll cright = i-1-cleft; ll addleft = cleft*(curr) - (cleft*(cleft+1))/2 - leftsum; ll addright = -cright*curr - (cright*(cright+1))/2 + rightsum; ans.pb(icnt+addleft+addright); } rep(n) cout<<ans[i]<<" "; return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#pragma GCC optimize("Ofast") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; #define uid uniform_int_distribution mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); #define bpop(x) __builtin_popcountll((ulli)x) #define blead(x) __builtin_clzll((ulli)x) #define btrail(x) __builtin_ctzll((ulli)x) const double PI = acos(-1); template<class my> using ind_set = tree<my,null_type,less<my>,rb_tree_tag,tree_order_statistics_node_update>; #define ff first #define ss second #define pb push_back #define pf push_front #define ppb pop_back() #define ppf pop_front() #define all(vec) vec.begin(), vec.end() #define fol(i,a,b) for(int i=a;i<b;i++) #define loop(i,a,b) for(int i=a;i>=b;i--) #define forr(x,arr) for(auto& x:arr) #define mod 1000000007 #define INF 0x3f3f3f3f3f3f3f3f #define EPS 1e-7 #define sz(x) (int)(x).size() using lli = long long; using ulli = unsigned long long int; using pll = pair<lli, lli>; using ttt = pair<lli, pll>; using vttt = vector<ttt>; using vll = vector<pll>; using vl = vector<lli>; using vi = vector<int>; using vvi = vector<vector<int>>; using cd = complex<long double>; #ifdef tejasp template<typename T> void __p(T a) { cout << a << " "; } template<typename T, typename F> void __p(pair<T, F> a) { cout << "{ "; __p(a.ff); __p(a.ss); cout << "} "; } template<typename Arg1> void __f(const char *name, Arg1 &&arg1) { cout<<name<<" : ";__p(arg1); cout<<endl; } template<typename Arg1> void __t(const char *name, Arg1 &&arg1) { cout<<name<<" : { "; for (auto p : arg1) __p(p); cout<<"}"<<endl; } template<typename Arg1, typename ... Args> void __f(const char *names, Arg1 &&arg1, Args &&... args) { int bracket=0,i=0; for(; ;i++) if(names[i]==','&&bracket==0) break; else if(names[i]=='(') bracket++; else if(names[i]==')') bracket--; cout.write(names,i)<<" : "; __p(arg1); cout<<"| "; __f(names+i+1,args...); } template<typename Arg1, typename Arg2> void __f(const char *names, Arg1 arg1[], Arg2 &&arg2){ int i=0; for(; ;i++) if(names[i]==',') break; cout.write(names,i)<<" : { "; fol(i,0,arg2) __p(arg1[i]); cout << "} "<<endl; } #define trace(...) { cout<<"Line:"<<__LINE__<<" | "; __f(#__VA_ARGS__, __VA_ARGS__); } #define cotra(...) { cout<<"Line:"<<__LINE__<<" | "; __t(#__VA_ARGS__, __VA_ARGS__); } #else #define endl '\n' #define trace(...) #define cotra(...) #endif void tejas_919(int kkkk) { lli n, m, k, q, u, v, temp=0; ind_set<int> se[2]; cin>>n; int arr[n+5], ans[n+5]={}, exis[n+5]={}; fol(i,0,n) { cin>>u; arr[u]=i; if(u==1) temp++; else { se[temp].insert(i); exis[u]=temp; } } fol(i,2,n+1) { if(exis[i]==0) { temp=se[0].order_of_key(arr[i])+1; temp=sz(se[0])-temp; temp+=i-1; ans[i]=ans[i-1]+temp; se[0].erase(arr[i]); } else { temp=se[1].order_of_key(arr[i]); ans[i]=ans[i-1]+temp; se[1].erase(arr[i]); } cotra(se[0]) cotra(se[1]) } fol(i,1,n+1) cout<<ans[i]<<" "; cout<<endl; } int main() { #ifndef tejasp ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); #endif cout << fixed << setprecision(10); int t=1; //cin>>t; fol(i,0,t) { tejas_919(i+1); } }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
java
import java.io.*; import java.util.*; public class Contest1 { //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////// ///////// //////// ///////// //////// HHHH HHHH EEEEEEEEEEEEE MMMM MMMM OOOOOO SSSSSSS EEEEEEEEEEEEE ///////// //////// HHHH HHHH EEEEEEEEEEEEE MMMMMM MMMMMM OOO OOO SSSS SSS EEEEEEEEEEEEE ///////// //////// HHHH HHHH EEEEE MMMM MMM MMM MMMM OOO OOO SSSS SSS EEEEE ///////// //////// HHHH HHHH EEEEE MMMM MMMMMM MMMM OOO OOO SSSS EEEEE ///////// //////// HHHH HHHH EEEEE MMMM MMMM OOO OOO SSSSSSS EEEEE ///////// //////// HHHHHHHHHHHHHHHH EEEEEEEEEEE MMMM MMMM OOO OOO SSSSSS EEEEEEEEEEE ///////// //////// HHHHHHHHHHHHHHHH EEEEEEEEEEE MMMM MMMM OOO OOO SSSSSSS EEEEEEEEEEE ///////// //////// HHHH HHHH EEEEE MMMM MMMM OOO OOO SSSS EEEEE ///////// //////// HHHH HHHH EEEEE MMMM MMMM OOO OOO SSS SSSS EEEEE ///////// //////// HHHH HHHH EEEEEEEEEEEEE MMMM MMMM OOO OOO SSS SSSS EEEEEEEEEEEEE ///////// //////// HHHH HHHH EEEEEEEEEEEEE MMMM MMMM OOOOOO SSSSSSS EEEEEEEEEEEEE ///////// //////// ///////// //////// ///////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public static void main(String[] args) throws IOException, InterruptedException { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int n = sc.nextInt(); int[] a = new int[n]; int[] pos = new int[n+1]; for (int i =0;i<n;i++){ a[i]=sc.nextInt(); pos[a[i]]=i+1; } FenwickTree fn = new FenwickTree(n); long[] ans = new long[n+1]; //count inversions for (int i =n-1;i>=0;i--){ ans[a[i]]+=fn.rsq(a[i]); fn.point_update(a[i],1); } for (int i =1;i<=n;i++)ans[i]+=ans[i-1]; // pw.println(Arrays.toString(ans)); // fn= new FenwickTree(n); FenwickTree fn2 = new FenwickTree(n); fn.point_update(pos[1],1); fn2.point_update(pos[1],pos[1]); for (int i =2;i<=n;i++){ fn.point_update(pos[i],1); fn2.point_update(pos[i],pos[i]); int low = 1; int hi = n; int idx=0; while (low<=hi){ int mid = low+hi >>1; if (fn.rsq(mid)>=(i+1)/2){ idx= mid; hi = mid-1; } else low=mid+1; } long pre= +sumrange(idx-(i+1)/2+1,idx-1); pre-=fn2.rsq(idx-1); if (fn.rsq(idx)==1)pre=0; int add=i-(i+1)/2; long after=fn2.rsq(idx+1,n)-sumrange(idx+1,idx+add); if (add==0)after=0; ans[i]+=pre+after; } for (int i =1;i<=n;i++) pw.print(ans[i]+" "); pw.println(); pw.flush(); } static long sumrange(int a, int b){ return -1l*a*(a-1)/2 + 1l*b*(b+1)/2; } static class FenwickTree { // one-based DS int n; int[] ft; FenwickTree(int size) { n = size; ft = new int[n+1]; } int rsq(int b) //O(log n) { int sum = 0; while(b > 0) { sum += ft[b]; b -= b & -b;} //min? return sum; } int rsq(int a, int b) { return rsq(b) - rsq(a-1); } void point_update(int k, int val) //O(log n), update = increment { while(k <= n) { ft[k] += val; k += k & -k; } //min? } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(FileReader r) { br = new BufferedReader(r); } public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public boolean ready() throws IOException { return br.ready(); } } }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #include <cassert> #include <numeric> #include <type_traits> namespace atcoder { namespace internal { #ifndef _MSC_VER template <class T> using is_signed_int128 = typename std::conditional<std::is_same<T, __int128_t>::value || std::is_same<T, __int128>::value, std::true_type, std::false_type>::type; template <class T> using is_unsigned_int128 = typename std::conditional<std::is_same<T, __uint128_t>::value || std::is_same<T, unsigned __int128>::value, std::true_type, std::false_type>::type; template <class T> using make_unsigned_int128 = typename std::conditional<std::is_same<T, __int128_t>::value, __uint128_t, unsigned __int128>; template <class T> using is_integral = typename std::conditional<std::is_integral<T>::value || is_signed_int128<T>::value || is_unsigned_int128<T>::value, std::true_type, std::false_type>::type; template <class T> using is_signed_int = typename std::conditional<(is_integral<T>::value && std::is_signed<T>::value) || is_signed_int128<T>::value, std::true_type, std::false_type>::type; template <class T> using is_unsigned_int = typename std::conditional<(is_integral<T>::value && std::is_unsigned<T>::value) || is_unsigned_int128<T>::value, std::true_type, std::false_type>::type; template <class T> using to_unsigned = typename std::conditional< is_signed_int128<T>::value, make_unsigned_int128<T>, typename std::conditional<std::is_signed<T>::value, std::make_unsigned<T>, std::common_type<T>>::type>::type; #else template <class T> using is_integral = typename std::is_integral<T>; template <class T> using is_signed_int = typename std::conditional<is_integral<T>::value && std::is_signed<T>::value, std::true_type, std::false_type>::type; template <class T> using is_unsigned_int = typename std::conditional<is_integral<T>::value && std::is_unsigned<T>::value, std::true_type, std::false_type>::type; template <class T> using to_unsigned = typename std::conditional<is_signed_int<T>::value, std::make_unsigned<T>, std::common_type<T>>::type; #endif template <class T> using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>; template <class T> using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>; template <class T> using to_unsigned_t = typename to_unsigned<T>::type; } // namespace internal } // namespace atcoder #include <cassert> #include <vector> namespace atcoder { // Reference: https://en.wikipedia.org/wiki/Fenwick_tree template <class T> struct fenwick_tree { using U = internal::to_unsigned_t<T>; public: fenwick_tree() : _n(0) {} fenwick_tree(int n) : _n(n), data(n) {} void add(int p, T x) { assert(0 <= p && p < _n); p++; while (p <= _n) { data[p - 1] += U(x); p += p & -p; } } T sum(int l, int r) { assert(0 <= l && l <= r && r <= _n); return sum(r) - sum(l); } private: int _n; std::vector<U> data; U sum(int r) { U s = 0; while (r > 0) { s += data[r - 1]; r -= r & -r; } return s; } }; } // namespace atcoder typedef struct _median_manage { // vaiables atcoder::fenwick_tree<int> fw; int nmax, real_size; // construct _median_manage(int _n) : nmax(_n), real_size(_n+10) { fw = atcoder::fenwick_tree<int>(real_size); } bool insert(int i) { if(fw.sum(i, i+1) == 1) return false; fw.add(i, 1); return true; } bool erase(int i) { if(fw.sum(i, i+1) == 0) return false; fw.add(i, -1); return true; } long long get_median() { int sum = fw.sum(0, real_size); assert(sum > 0); int l = -1, r = real_size; while(r - l > 1) { int m = (l + r) / 2; if(fw.sum(0, m+1) < (sum+1) / 2) l = m; else r = m; } return r; } long long get_lnum() { int sum = fw.sum(0, real_size); return (sum+1) / 2; } long long get_rnum() { int sum = fw.sum(0, real_size); return sum / 2; } long long get_sum() { return fw.sum(0, real_size); } } medi; using namespace std; int main() { int n; cin >> n; vector<int> p(n), q(n); for(int i = 0; i < n; i++) { cin >> p[i]; p[i]--; q[p[i]] = i; } medi md(n); int rmax = n+10; atcoder::fenwick_tree<int> inv(rmax), idx(rmax); long long ninv = 0; for(int i = 0; i < n; i++) { // update inv.add(q[i], 1); idx.add(q[i], q[i]); md.insert(q[i]); // count inversion ninv += inv.sum(q[i]+1, rmax); // print ans long long med = md.get_median(); long long lnum = md.get_lnum(); long long rnum = md.get_rnum(); long long l = med * lnum - idx.sum(0, med+1) - lnum * (lnum-1) / 2; long long r = idx.sum(med+1, rmax) - med * (rnum) - (rnum) * (rnum+1) / 2; cout << ninv + l + r << (i == n-1 ? "\n" : " "); } return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> const int maxn = 200000 + 10; class BIT { private: int n; int bit[maxn]; public: BIT(int size) { n = size; std::fill(bit, bit + size, 0); } void add(int x, int v) { while (x <= this->n) { bit[x] += v; x += ((x) & (-x)); } } int query(int x) { int ans = 0; while (x) { ans += bit[x]; x -= ((x) & (-x)); } return ans; } }; int n, pos[maxn]; struct TreeNode { int zeros, preadd, sufadd; long long presum, sufsum; }; TreeNode seg[maxn * 4]; void build_tree(int node, int L, int R) { seg[node].zeros = R - L + 1; if (L == R) return; int M = L + (R - L) / 2; build_tree(node << 1, L, M); build_tree(node << 1 | 1, M + 1, R); } void push_down(int node, int L, int R) { int lch = node << 1; int rch = node << 1 | 1; int M = L + (R - L) / 2; if (seg[node].preadd) { seg[lch].preadd += seg[node].preadd; seg[rch].preadd += seg[node].preadd; seg[lch].presum += 1LL * seg[lch].zeros * seg[lch].preadd; seg[rch].presum += 1LL * seg[rch].zeros * seg[rch].preadd; seg[node].preadd = 0; } if (seg[node].sufadd) { seg[lch].sufadd += seg[node].sufadd; seg[rch].sufadd += seg[node].sufadd; seg[lch].sufsum += 1LL * seg[lch].zeros * seg[lch].sufadd; seg[rch].sufsum += 1LL * seg[rch].zeros * seg[rch].sufadd; seg[node].sufadd = 0; } } int qL, qR; void add(int node, int L, int R, int type) { if (qL <= L && R <= qR) { if (type == 0) { seg[node].preadd += 1; seg[node].presum += seg[node].zeros; } else { seg[node].sufadd += 1; seg[node].sufsum += seg[node].zeros; } return; } push_down(node, L, R); int M = L + (R - L) / 2; if (qL <= M) add(node << 1, L, M, type); if (qR > M) add(node << 1 | 1, M + 1, R, type); int lch = node << 1; int rch = node << 1 | 1; seg[node].zeros = seg[lch].zeros + seg[rch].zeros; seg[node].presum = seg[lch].presum + seg[rch].presum; seg[node].sufsum = seg[lch].sufsum + seg[rch].sufsum; } void erase(int node, int L, int R, int p) { if (L == R) { memset(seg + node, 0, sizeof(TreeNode)); return; } int M = L + (R - L) / 2; int lch = node << 1; int rch = node << 1 | 1; push_down(node, L, R); if (p <= M) erase(lch, L, M, p); else erase(rch, M + 1, R, p); seg[node].zeros = seg[lch].zeros + seg[rch].zeros; seg[node].presum = seg[lch].presum + seg[rch].presum; seg[node].sufsum = seg[lch].sufsum + seg[rch].sufsum; } long long query(int node, int L, int R, int type) { if (qL <= L && R <= qR) { if (type == 0) return seg[node].presum; else return seg[node].sufsum; } push_down(node, L, R); long long ans = 0; int M = L + (R - L) / 2; if (qL <= M) ans += query(node << 1, L, M, type); if (qR > M) ans += query(node << 1 | 1, M + 1, R, type); return ans; } int main() { scanf("%d", &n); BIT bit(n); for (int i = 1; i <= n; i++) { int a; scanf("%d", &a); pos[a] = i; } build_tree(1, 1, n); long long inversion = 0; for (int i = 1; i <= n; i++) { const int& p = pos[i]; inversion += i - 1 - bit.query(p); bit.add(p, 1); qL = p + 1; qR = n; if (qL <= qR) add(1, 1, n, 0); qL = 1; qR = p - 1; if (qL <= qR) add(1, 1, n, 1); erase(1, 1, n, p); long long ans = 0; const int& median = pos[(i + 1) / 2]; qL = 1; qR = median - 1; if (qL <= qR) ans += query(1, 1, n, 0); qL = median + 1; qR = n; if (qL <= qR) ans += query(1, 1, n, 1); printf("%lld ", inversion + ans); } printf("\n"); return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; int n; int pos[maxn]; int sum1[maxn], sum2[maxn]; int lowbit(int x) { return x & -x; } void add(int *sum, int x, int v) { while (x <= n) { sum[x] += v; x += lowbit(x); } } int query(int *sum, int x) { int ans = 0; while (x > 0) { ans += sum[x]; x -= lowbit(x); } return ans; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { int p; scanf("%d", &p); pos[p] = i; } int rev = 0; for (int i = 1; i <= n; i++) { add(sum1, pos[i], 1); add(sum2, pos[i], pos[i]); rev += i - query(sum1, pos[i]); int l = 1, r = n, mid; while (l < r) { mid = (l + r) >> 1; if (query(sum1, mid) <= i / 2) l = mid + 1; else r = mid; } int cntl = query(sum1, mid) - 1, cntr = i - cntl - 1; int ans = mid * (cntl - cntr + 1) + query(sum2, n) - 2 * query(sum2, mid) - (cntl + 1) * cntl / 2 - (cntr + 1) * cntr / 2; ans += rev; printf("%d ", ans); } return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #ifndef ONLINE_JUDGE #include <custom/prettyprint.hpp> #endif using namespace std; #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; typedef long long ll; #define FOR(i,a,b) for (int i = (a); i < (b); ++i) #define sz(x) (int)x.size() #define dbg(x) cerr << #x << " = " << x << '\n' const int MOD = 1e9+7; //998244353; const int inf = 1e9+3; const ll INF = ll(3e18)+3; void setIO(const string& s) { ios_base::sync_with_stdio(false); cin.tie(nullptr); #ifndef ONLINE_JUDGE freopen((s+".in").c_str(),"r",stdin); // freopen((s+".out").c_str(),"w",stdout); #endif } template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; const int N = 200010; struct BIT { int n; vector<int> arr; BIT(int n) : n(n) { arr = vector<int>(n+1); } void upd(int i, int x) { for(; i<=n; i+=i&-i) arr[i] += x; } int sum(int i) { int res = 0; for(; i>0; i-=i&-i) res += arr[i]; return res; } int rsum(int l, int r) { return sum(r) - sum(l-1); } }; int arr[N], pos[N]; signed main() { setIO("input"); int n; cin >> n; for(int i=1; i<=n; ++i) { cin >> arr[i]; pos[arr[i]] = i; } BIT inv(n), ones(n); ordered_set<int> cur; int inversions = 0; for(int i=1; i<=n; ++i) { inversions += inv.rsum(pos[i]+1, n); inv.upd(pos[i], 1); cur.insert(pos[i]); ones.upd(pos[i], pos[i]); int half = (i-1)/2; int median = *cur.find_by_order(half); int left = ones.rsum(1, median-1); int right = ones.rsum(median+1, n); // |p_i - median| cancels out when breaking into left and right int ans = right - left; ans -= 2 * ((1+half)*half / 2); if(i%2 == 0) { ans -= median; ans -= half+1; } ans += inversions; cout << ans << ' '; } cout << '\n'; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 200000; int a[200005], pos[200005]; long long sum1[200005], sum2[200005]; void add(long long *sum1, int x, int val) { while (x <= maxn) { sum1[x] += val; x += x & (-x); } } int sum(long long *sum1, int pos) { int res = 0; while (pos) { res += sum1[pos]; pos -= pos & (-pos); } return res; } int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; pos[a[i]] = i; } int ans1 = 0, ans2 = 0; for (int i = 1; i <= n; i++) { ans1 += i - 1 - sum(sum1, pos[i]); add(sum1, pos[i], 1); add(sum2, pos[i], pos[i]); int l = 1, r = n; while (l < r) { int mid = l + r + 1 >> 1; if (sum(sum1, mid) * 2 <= i) { l = mid; } else r = mid - 1; } int cnt = sum(sum1, l); long long sum3 = sum(sum2, l); ans2 = 0; ans2 += cnt * l - sum3 - cnt * (cnt - 1) / 2; cnt = i - cnt; sum3 = sum(sum2, n) - sum(sum2, l); ans2 += sum3 - cnt * (l + 1) - cnt * (cnt - 1) / 2; cout << ans1 + ans2 << " "; } cout << endl; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = 1061109567; const int MAXN = 201010; int n, median; int v[MAXN]; int ind[MAXN]; int st[4 * MAXN]; long long adjust[MAXN]; long long inversions[MAXN]; priority_queue<int> maxheap; priority_queue<int, vector<int>, greater<int> > minheap; void update(int nd, int l, int r, int pos, int x) { if (l == r) { st[nd] = x; return; } if (pos < l || pos > r) return; int mid = (l + r) >> 1; int nxt = nd << 1; if (pos <= mid) update(nxt, l, mid, pos, x); if (pos > mid) update(nxt + 1, mid + 1, r, pos, x); st[nd] = st[nxt] + st[nxt + 1]; } int query(int nd, int l, int r, int lq, int rq) { if (r < lq || rq < l) return 0; if (lq <= l && r <= rq) return st[nd]; int mid = (l + r) >> 1; int nxt = nd << 1; return query(nxt, l, mid, lq, rq) + query(nxt + 1, mid + 1, r, lq, rq); } long long gather(int k, int med_pos) { long long rh = k - (k / 2); long long lh = k - rh; long long r_tar_sum = ((med_pos + (med_pos + rh - 1)) * rh) / 2; long long l_tar_sum = ((med_pos - 1 + (med_pos - lh)) * lh) / 2; long long r_pos_sum = query(1, 0, n, med_pos, n); long long l_pos_sum = query(1, 0, n, 0, max(0, med_pos - 1)); long long ret_val = (l_tar_sum - l_pos_sum) + (r_pos_sum - r_tar_sum); return ret_val; } void reset_seg_tree() { for (int i = 1; i <= n; i++) update(1, 0, n, i, 0); return; } void heap_insert(int x) { if (maxheap.size() > minheap.size()) { if (x < median) { minheap.push(maxheap.top()); maxheap.pop(); maxheap.push(x); } else { minheap.push(x); } median = minheap.top(); } else if (maxheap.size() == minheap.size()) { if (x < median) { maxheap.push(x); median = maxheap.top(); } else { minheap.push(x); median = minheap.top(); } } else { if (x > median) { maxheap.push(minheap.top()); minheap.pop(); minheap.push(x); } else { maxheap.push(x); } median = minheap.top(); } return; } int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> v[i]; ind[v[i]] = i; } for (int k = 1; k <= n; k++) { heap_insert(ind[k]); update(1, 0, n, ind[k], ind[k]); adjust[k] = gather(k, median); } reset_seg_tree(); inversions[0] = 0; for (int k = 1; k <= n; k++) { inversions[k] = inversions[k - 1] + query(1, 0, n, ind[k] + 1, n - 1); update(1, 0, n, ind[k], 1); } for (int i = 1; i <= n; i++) cout << (inversions[i] + adjust[i]) << " \n"[i == n]; return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; long long bit[N] = {}; int idx[N] = {}; int n; priority_queue<int> A; priority_queue<int, vector<int>, greater<int>> B; long long sum(int x) { int ret = 0; while (x) { ret += bit[x]; x -= (x & -x); } return ret; } void add(int x, int val) { while (x <= n) { bit[x] += val; x += (x & -x); } } int main() { cin >> n; long long x; for (int i = 1; i <= n; i++) { cin >> x; idx[x] = i; } long long now = 0, d = 0, t; x = idx[1]; for (int i = 1; i <= n; i++) { B.emplace(idx[i]); A.emplace(B.top()); B.pop(); while (A.size() > ((i + 1) >> 1)) { B.emplace(A.top()); A.pop(); } while (A.size() < B.size()) { A.emplace(B.top()); B.pop(); } long long f = A.top(), p = (i / 2) * (i / 2 + 1) - (i / 2) * ((i & 1) == 0), t = A.size() - (f > x); now += abs(idx[i] - x) + (f - x) * (t + t - i); x = f; add(idx[i], 1); d += i - sum(idx[i]); cout << now - p + d << " \n"[i == n]; } return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> const int maxn = 200000 + 10; class BIT { private: int n; int bit[maxn]; public: BIT(int size) { n = size; std::fill(bit, bit + size, 0); } void add(int x, int v) { while (x <= this->n) { bit[x] += v; x += ((x) & (-x)); } } int query(int x) { int ans = 0; while (x) { ans += bit[x]; x -= ((x) & (-x)); } return ans; } int find_k(int k) { int bits = 0; int n = this->n; while (n) { n >>= 1; bits++; } int mask = 0; int cnt = 0; for (int i = bits - 1; i >= 0; i--) { mask += 1 << i; if (mask > this->n || cnt + bit[mask] >= k) mask -= (1 << i); else cnt += bit[mask]; } return mask + 1; } }; int n, pos[maxn]; struct TreeNode { int zeros, preadd, sufadd; long long presum, sufsum; }; TreeNode seg[maxn * 4]; void build_tree(int node, int L, int R) { seg[node].zeros = R - L + 1; if (L == R) return; int M = L + (R - L) / 2; build_tree(node << 1, L, M); build_tree(node << 1 | 1, M + 1, R); } void push_down(int node, int L, int R) { int lch = node << 1; int rch = node << 1 | 1; int M = L + (R - L) / 2; if (seg[node].preadd) { seg[lch].preadd += seg[node].preadd; seg[rch].preadd += seg[node].preadd; seg[lch].presum += 1LL * seg[lch].zeros * seg[node].preadd; seg[rch].presum += 1LL * seg[rch].zeros * seg[node].preadd; seg[node].preadd = 0; } if (seg[node].sufadd) { seg[lch].sufadd += seg[node].sufadd; seg[rch].sufadd += seg[node].sufadd; seg[lch].sufsum += 1LL * seg[lch].zeros * seg[node].sufadd; seg[rch].sufsum += 1LL * seg[rch].zeros * seg[node].sufadd; seg[node].sufadd = 0; } } int qL, qR; void add(int node, int L, int R, int type) { if (qL <= L && R <= qR) { if (type == 0) { seg[node].preadd += 1; seg[node].presum += seg[node].zeros; } else { seg[node].sufadd += 1; seg[node].sufsum += seg[node].zeros; } return; } push_down(node, L, R); int M = L + (R - L) / 2; if (qL <= M) add(node << 1, L, M, type); if (qR > M) add(node << 1 | 1, M + 1, R, type); int lch = node << 1; int rch = node << 1 | 1; seg[node].zeros = seg[lch].zeros + seg[rch].zeros; seg[node].presum = seg[lch].presum + seg[rch].presum; seg[node].sufsum = seg[lch].sufsum + seg[rch].sufsum; } void erase(int node, int L, int R, int p) { if (L == R) { memset(seg + node, 0, sizeof(TreeNode)); return; } int M = L + (R - L) / 2; int lch = node << 1; int rch = node << 1 | 1; push_down(node, L, R); if (p <= M) erase(lch, L, M, p); else erase(rch, M + 1, R, p); seg[node].zeros = seg[lch].zeros + seg[rch].zeros; seg[node].presum = seg[lch].presum + seg[rch].presum; seg[node].sufsum = seg[lch].sufsum + seg[rch].sufsum; } long long query(int node, int L, int R, int type) { if (qL <= L && R <= qR) { if (type == 0) return seg[node].presum; else return seg[node].sufsum; } push_down(node, L, R); long long ans = 0; int M = L + (R - L) / 2; if (qL <= M) ans += query(node << 1, L, M, type); if (qR > M) ans += query(node << 1 | 1, M + 1, R, type); return ans; } int main() { scanf("%d", &n); BIT bit(n); for (int i = 1; i <= n; i++) { int a; scanf("%d", &a); pos[a] = i; } build_tree(1, 1, n); long long inversion = 0; for (int i = 1; i <= n; i++) { const int &p = pos[i]; inversion += i - 1 - bit.query(p); bit.add(p, 1); qL = p + 1; qR = n; if (qL <= qR) add(1, 1, n, 0); qL = 1; qR = p - 1; if (qL <= qR) add(1, 1, n, 1); erase(1, 1, n, p); long long ans = 0; int median = bit.find_k((i + 1) / 2); qL = 1; qR = median - 1; if (qL <= qR) { ans += query(1, 1, n, 0); } qL = median + 1; qR = n; if (qL <= qR) { ans += query(1, 1, n, 1); } printf("%lld ", inversion + ans); } printf("\n"); return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> int main() { int i,j,n,k; cin>>n; vector<int>a(n),pos(n+1); for(i=0;i<n;i++){ cin>>a[i]; pos[a[i]]=i; } ordered_set s; int l=0,r=0,ele=pos[1],ans=0; for(i=1;i<=n;i++){ if(i==1){ s.insert(pos[i]); cout<<ans<<" "; } else{ s.insert(pos[i]); int rank=s.order_of_key(pos[i]); if(ele>pos[i]) l++; else r++; if(l==r || l==r+1 || l+1==r){ if(*(s.find_by_order(0))>pos[i]){ ans+=(ele-pos[i]-l)+i-1; }else if(*(s.find_by_order(i-1))<pos[i]){ ans+=(pos[i]-ele-r); }else if(ele>pos[i]){ ans+=(ele-pos[i]-(l-rank))-rank + i-rank-1; }else{ rank=i-rank-1; int waste=pos[i]-ele-1-(r-rank-1); ans+=waste; } }else{ if(l==r+2){ ele=*(s.find_by_order(s.order_of_key(ele)-1)); l--;r++; if(ele==a[i])ans=ans-l; else{ if(*(s.find_by_order(0))>pos[i]){ ans+=(ele-pos[i]-l)+i-1; }else if(*(s.find_by_order(i-1))<pos[i]){ ans+=(pos[i]-ele-r); }else if(ele>pos[i]){ ans+=(ele-pos[i]-(l-rank))-rank + i-rank-1; }else{ rank=i-rank-1; int waste=pos[i]-ele-1-(r-rank-1); ans+=waste; } } }else{ ele=*(s.find_by_order(s.order_of_key(ele)+1)); l++;r--; if(ele==a[i])ans=ans-l; else{ if(*(s.find_by_order(0))>pos[i]){ ans+=(ele-pos[i]-l)+i-1; }else if(*(s.find_by_order(i-1))<pos[i]){ ans+=(pos[i]-ele-r); }else if(ele>pos[i]){ ans+=(ele-pos[i]-(l-rank))-rank + i-rank-1; }else{ rank=i-rank-1; int waste=pos[i]-ele-1-(r-rank-1); ans+=waste; } } } } cout<<ans<<" "; } } cout<<endl; return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; class BinTree : vector<long long> { public: explicit BinTree(long long k = 0) { assign(k + 1, 0); } long long lowbit(long long k) { return k & -k; } long long sum(long long k) { return k > 0 ? sum(k - lowbit(k)) + (*this)[k] : 0; } long long last() { return size() - 1; } void add(long long k, long long w) { if (k > last()) return; (*this)[k] += w; add(k + lowbit(k), w); } }; long long n; vector<long long> ps; vector<long long> mp; BinTree bt0, bt1; void solve() { bt0 = BinTree(n); bt1 = BinTree(n); long long res0 = 0; long long s = 0; for (long long p = 1; p <= n; p++) { long long res = res0; long long pi = mp[p]; s += pi; long long mi0 = s / p; bt0.add(pi, 1); bt1.add(pi, pi); long long l = 1, r = n; while (l <= r) { long long m = l + (r - l) / 2; long long nl = bt0.sum(m); if (nl > (p / 2)) r = m - 1; else l = m + 1; } mi0 = l; long long resf = INT_MAX; for (long long mi = mi0 - 2; mi <= mi0 + 2; mi++) { if (mi <= 0 or mi > n) continue; long long res = res0; if (ps[mi] > p) { long long cn = bt0.sum(mi - 1); long long sn = bt1.sum(mi - 1); res += cn * mi - sn - (cn - 1) * cn / 2; cn = bt0.sum(n) - bt0.sum(mi); sn = bt1.sum(n) - bt1.sum(mi); res += sn - cn * mi - cn - (cn - 1) * cn / 2; res += bt0.sum(n) - bt0.sum(pi); } else { long long cn = bt0.sum(mi - 1); long long sn = bt1.sum(mi - 1); res += cn * mi - sn - cn - (cn - 1) * cn / 2; cn = bt0.sum(n) - bt0.sum(mi); sn = bt1.sum(n) - bt1.sum(mi); res += sn - cn * mi - cn - (cn - 1) * cn / 2; res += bt0.sum(n) - bt0.sum(pi); } resf = min(resf, res); } res0 += bt0.sum(n) - bt0.sum(pi); cout << resf << " "; } cout << endl; } int main() { scanf("%d", &n); ps = vector<long long>(n + 1, 0); mp = vector<long long>(n + 1, 0); for (long long i = 1; i <= n; i++) { scanf("%d", &ps[i]); mp[ps[i]] = i; } solve(); return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
python3
from bisect import bisect_right, bisect_left # instead of AVLTree class BITbisect(): def __init__(self, InputProbNumbers): # 座圧 self.ind_to_co = [-10**18] self.co_to_ind = {} for ind, num in enumerate(sorted(list(set(InputProbNumbers)))): self.ind_to_co.append(num) self.co_to_ind[num] = ind+1 self.max = len(self.co_to_ind) self.data = [0]*(self.max+1) def __str__(self): retList = [] for i in range(1, self.max+1): x = self.ind_to_co[i] if self.count(x): c = self.count(x) for _ in range(c): retList.append(x) return "[" + ", ".join([str(a) for a in retList]) + "]" def __getitem__(self, key): key += 1 s = 0 ind = 0 l = self.max.bit_length() for i in reversed(range(l)): if ind + (1<<i) <= self.max: if s + self.data[ind+(1<<i)] < key: s += self.data[ind+(1<<i)] ind += (1<<i) if ind == self.max or key < 0: raise IndexError("BIT index out of range") return self.ind_to_co[ind+1] def __len__(self): return self._query_sum(self.max) def __contains__(self, num): if not num in self.co_to_ind: return False return self.count(num) > 0 # 0からiまでの区間和 # 左に進んでいく def _query_sum(self, i): s = 0 while i > 0: s += self.data[i] i -= i & -i return s # i番目の要素にxを足す # 上に登っていく def _add(self, i, x): while i <= self.max: self.data[i] += x i += i & -i # 値xを挿入 def push(self, x): if not x in self.co_to_ind: raise KeyError("The pushing number didnt initialized") self._add(self.co_to_ind[x], 1) # 値xを削除 def delete(self, x): if not x in self.co_to_ind: raise KeyError("The deleting number didnt initialized") if self.count(x) <= 0: raise ValueError("The deleting number doesnt exist") self._add(self.co_to_ind[x], -1) # 要素xの個数 def count(self, x): return self._query_sum(self.co_to_ind[x]) - self._query_sum(self.co_to_ind[x]-1) # 値xを超える最低ind def bisect_right(self, x): if x in self.co_to_ind: i = self.co_to_ind[x] else: i = bisect_right(self.ind_to_co, x) - 1 return self._query_sum(i) # 値xを下回る最低ind def bisect_left(self, x): if x in self.co_to_ind: i = self.co_to_ind[x] else: i = bisect_left(self.ind_to_co, x) if i == 1: return 0 return self._query_sum(i-1) import sys input = sys.stdin.readline N = int(input()) A = list(map(int, input().split())) Ind = [0]*(N+1) for i, a in enumerate(A): Ind[a] = i+1 Bit = BITbisect(list(range(N+1))) ans = [0] Bit.push(Ind[1]) a = 0 for n in range(2, N+1): ind = Ind[n] f = Bit.bisect_left(ind) #print(Bit) l = len(Bit) if l%2 == 0: if f == l//2: a += l//2 elif f < l//2: p1 = Bit[l//2-1] a += (p1-ind-1) - (l//2-1) + l-f else: p2 = Bit[l//2] a += (ind-p2-1) - (l//2-1) + l-f else: p1 = Bit[l//2] #print(f, p1, ind, l) if f <= l//2: a += (p1-ind-1) - l//2 + l-f else: a += (ind-p1-1) - l//2 + l-f ans.append(a) Bit.push(ind) print(*ans, sep=" ")
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #include <cassert> #include <numeric> #include <type_traits> namespace atcoder { namespace internal { #ifndef _MSC_VER template <class T> using is_signed_int128 = typename std::conditional<std::is_same<T, __int128_t>::value || std::is_same<T, __int128>::value, std::true_type, std::false_type>::type; template <class T> using is_unsigned_int128 = typename std::conditional<std::is_same<T, __uint128_t>::value || std::is_same<T, unsigned __int128>::value, std::true_type, std::false_type>::type; template <class T> using make_unsigned_int128 = typename std::conditional<std::is_same<T, __int128_t>::value, __uint128_t, unsigned __int128>; template <class T> using is_integral = typename std::conditional<std::is_integral<T>::value || is_signed_int128<T>::value || is_unsigned_int128<T>::value, std::true_type, std::false_type>::type; template <class T> using is_signed_int = typename std::conditional<(is_integral<T>::value && std::is_signed<T>::value) || is_signed_int128<T>::value, std::true_type, std::false_type>::type; template <class T> using is_unsigned_int = typename std::conditional<(is_integral<T>::value && std::is_unsigned<T>::value) || is_unsigned_int128<T>::value, std::true_type, std::false_type>::type; template <class T> using to_unsigned = typename std::conditional< is_signed_int128<T>::value, make_unsigned_int128<T>, typename std::conditional<std::is_signed<T>::value, std::make_unsigned<T>, std::common_type<T>>::type>::type; #else template <class T> using is_integral = typename std::is_integral<T>; template <class T> using is_signed_int = typename std::conditional<is_integral<T>::value && std::is_signed<T>::value, std::true_type, std::false_type>::type; template <class T> using is_unsigned_int = typename std::conditional<is_integral<T>::value && std::is_unsigned<T>::value, std::true_type, std::false_type>::type; template <class T> using to_unsigned = typename std::conditional<is_signed_int<T>::value, std::make_unsigned<T>, std::common_type<T>>::type; #endif template <class T> using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>; template <class T> using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>; template <class T> using to_unsigned_t = typename to_unsigned<T>::type; } // namespace internal } // namespace atcoder #include <cassert> #include <vector> namespace atcoder { // Reference: https://en.wikipedia.org/wiki/Fenwick_tree template <class T> struct fenwick_tree { using U = internal::to_unsigned_t<T>; public: fenwick_tree() : _n(0) {} fenwick_tree(int n) : _n(n), data(n) {} void add(int p, T x) { assert(0 <= p && p < _n); p++; while (p <= _n) { data[p - 1] += U(x); p += p & -p; } } T sum(int l, int r) { assert(0 <= l && l <= r && r <= _n); return sum(r) - sum(l); } private: int _n; std::vector<U> data; U sum(int r) { U s = 0; while (r > 0) { s += data[r - 1]; r -= r & -r; } return s; } }; } // namespace atcoder typedef struct _median_manage { // vaiables atcoder::fenwick_tree<int> fw; int nmax, real_size; // construct _median_manage(int _n) : nmax(_n), real_size(_n+10) { fw = atcoder::fenwick_tree<int>(real_size); } bool insert(int i) { if(fw.sum(i, i+1) == 1) return false; fw.add(i, 1); return true; } bool erase(int i) { if(fw.sum(i, i+1) == 0) return false; fw.add(i, -1); return true; } int get_median() { int sum = fw.sum(0, real_size); assert(sum > 0); int l = -1, r = real_size; while(r - l > 1) { int m = (l + r) / 2; if(fw.sum(0, m+1) < (sum+1) / 2) l = m; else r = m; } return r; } int get_lnum() { int sum = fw.sum(0, real_size); return (sum+1) / 2; } int get_rnum() { int sum = fw.sum(0, real_size); return sum / 2; } int get_sum() { return fw.sum(0, real_size); } } medi; using namespace std; int main() { int n; cin >> n; vector<int> p(n), q(n); for(int i = 0; i < n; i++) { cin >> p[i]; p[i]--; q[p[i]] = i; } medi md(n); int rmax = n+10; atcoder::fenwick_tree<long long> inv(rmax), idx(rmax); long long ninv = 0; for(int i = 0; i < n; i++) { // update inv.add(q[i], 1); idx.add(q[i], q[i]); assert(md.insert(q[i])); // count inversion ninv += inv.sum(q[i]+1, rmax); // print ans int med = md.get_median(); int lnum = md.get_lnum(); int rnum = md.get_rnum(); long long l = med * lnum - idx.sum(0, med+1) - lnum * (lnum-1) / 2; long long r = idx.sum(med+1, rmax) - med * (rnum) - (rnum) * (rnum+1) / 2; cout << ninv + l + r << (i == n-1 ? "\n" : " "); } return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct fenwick { int N; vector<int> bit; fenwick(int n = 1e5) { N = n + 5; bit.assign(N, 0); } void resize(int n) { N = n + 5; bit.assign(N, 0); } void update(int x, int val) { while (x < N) { bit[x] += val; x += x & -x; } } int sum(int x) { int ret = 0; while (x > 0) { ret += bit[x]; x -= x & -x; } return ret; } }; int main() { ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0); int n; cin >> n; vector<int> idx(n + 5); for (int i = 1; i <= n; ++i) { int x; cin >> x; idx[x] = i; } fenwick bit(n); set<int, greater<int>> l; set<int> r; long long lsum = 0, rsum = 0, ans = 0; for (int i = 1; i <= n; ++i) { ans += i - 1 - bit.sum(idx[i]); bit.update(idx[i], 1); if (l.size() == 0 || idx[i] < *l.begin()) l.insert(idx[i]), lsum += idx[i]; else r.insert(idx[i]), rsum += idx[i]; if (l.size() > r.size() + 1) { int x = *l.begin(); lsum -= x; rsum += x; l.erase(l.begin()); r.insert(x); } else if (r.size() > l.size() + 1) { int x = *r.begin(); lsum += x; rsum -= x; l.insert(x); r.erase(r.begin()); } long long lcnt = (l.size() * (*l.begin())) - lsum - (l.size() * (l.size() - 1)) / 2; long long rcnt = (rsum - r.size() * (*l.begin())) - (r.size() * (r.size() + 1)) / 2; cout << ans + lcnt + rcnt << ' '; } }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> const int maxn = 2e5 + 10; const int maxm = 1e5 + 10; const long long int mod = 1e9 + 7; const long long int INF = 1e18 + 100; const int inf = 0x3f3f3f3f; const double pi = acos(-1.0); const double eps = 1e-8; using namespace std; int n, m; int cas, tol, T; set<int> st; int a[maxn], p[maxn]; int sum[maxn]; void update(int x) { for (int i = x; i; i -= i & (-i)) sum[i]++; } int query(int x) { if (x == 0) return 0; int ans = 0; for (int i = x; i <= n; i += i & (-i)) ans += sum[i]; return ans; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]), p[a[i]] = i; int ans = 0, k = 0; printf("0 "); update(p[1]); st.insert(p[1]); auto it = st.begin(); for (int i = 2; i <= n; i++) { ans -= min(query(p[i] + 1), i - 1 - query(p[i] + 1)); ans += query(p[i] + 1); update(p[i]); st.insert(p[i]); if (i % 2 == 0 && p[i] < (*it)) ans += abs((*it) - (*(--it))) - 1; if (i % 2 == 1 && p[i] > (*it)) it++; int x = *it; if (p[i] > x) ans += p[i] - x + 1 - query(x) + query(p[i] + 1); else ans += x - p[i] + 1 - query(p[i]) + query(x + 1); printf("%d ", ans); } return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int s[200005], sum[200005], ara[200005], p[200005], seg[4 * 200005]; void add(int x, int n) { while (x <= n) { sum[x]++; x += (x & -x); } return; } int query(int x) { int ret = 0; while (x) { ret += sum[x]; x -= (x & -x); } return ret; } void update(int n, int s, int e, int indx) { if (s == indx && e == indx) { seg[n]++; return; } if (s > indx || e < indx) return; int mid = (s + e) / 2; update(n * 2, s, mid, indx); update(n * 2 + 1, mid + 1, e, indx); seg[n] = seg[n * 2] + seg[n * 2 + 1]; return; } int query(int n, int s, int e, int k) { if (s == e && k == 1) return s; int mid = (s + e) / 2; if (seg[n * 2] < k) return query(n * 2 + 1, mid + 1, e, k - seg[n * 2]); else return query(n * 2, s, mid, k); } int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &ara[i]), p[ara[i]] = i; for (int i = 1; i <= n; i++) s[i] = query(n) - query(p[i]), add(p[i], n); printf("0"); long long res = 0; memset(sum, 0, sizeof sum); add(p[1], n); update(1, 1, n, p[1]); int mn = p[1], mx = p[1]; for (int i = 2; i <= n; i++) { res += s[i]; update(1, 1, n, p[i]); add(p[i], n); if (p[i] < mn || p[i] > mx) mn = min(mn, p[i]), mx = max(mx, p[i]); else { int x1 = query(p[i] - 1); int x2 = query(n) - x1 - 1; res -= min(x1, x2); } if (i == 2) { res += (mx - mn + 1) - 2; printf(" %lld", res); continue; } int l = p[i]; int r = query(1, 1, n, (i + 1) / 2); if (l > r) swap(l, r); if (l < r) res += (r - l + 1) - (query(r) - query(l - 1)); printf(" %lld", res); } puts(""); return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MAXN = 200000 + 10; int BIT1[MAXN], BIT2[MAXN]; int N; void update(int p, int num, int *BIT) { for (; p < N; p += p & -p) { BIT[p] += num; } } int query(int p, int *BIT) { int sum = 0; for (; p > 0; p -= p & -p) { sum += BIT[p]; } return sum; } int arr_num[MAXN], arr_pos[MAXN]; int main(void) { cin >> N; for (int i = 1; i <= N; i++) { cin >> arr_num[i]; arr_pos[arr_num[i]] = i; } int ans = 0; for (int i = 1; i <= N; i++) { ans += i - query(arr_pos[i], BIT1) - 1; update(arr_pos[i], 1, BIT1); update(arr_pos[i], arr_pos[i], BIT2); int lb = 1, ub = N; while (ub > lb) { int mid = (lb + ub + 1) >> 1; if (query(mid - 1, BIT1) > i / 2) { ub = mid - 1; } else { lb = mid; } } int upper = query(N, BIT1) - query(ub, BIT1); int add1 = query(N, BIT2) - query(ub, BIT2) - ub * upper - (1 + upper) * upper / 2; int lower = query(ub - 1, BIT1); int add2 = ub * lower - query(ub - 1, BIT2) - (1 + lower) * lower / 2; cout << ans + add1 + add2 << " "; } cout << endl; return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #define LL long long #define PI pair<int,int> #define PL pair<LL,LL> #define st first #define nd second #define all(x) x.begin(),x.end() using namespace __gnu_pbds; using namespace std; typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set; const int MX = 2e5 + 5; const int INF = 1e9 + 9; int n; LL Bit[MX]; LL Idx[MX]; LL Inv[MX]; LL Pos[MX]; vector<int> A; void Add_Bit(int x,int val){ for( ; x <= n + 1 ; x += x&-x) Bit[x] += val; } void Add_Idx(int x,int val){ for( ; x <= n + 1 ; x += x&-x) Idx[x] += val; } LL Get_Bit(int x){ LL res = 0; for( ; x > 0 ; x -= x&-x) res += Bit[x]; return res; } LL Get_Idx(int x){ LL res = 0; for( ; x > 0 ; x -= x&-x) res += Idx[x]; return res; } void prep(){ for(int i = 0 ; i <= n + 1; ++i){ Bit[i] = 0; Idx[i] = 0; Pos[i] = 0; } } int main(){ scanf("%d",&n); prep(); for(int i = 0 ; i < n ; ++i){ int x; scanf("%d",&x); A.push_back(x); Pos[x] = i + 1; } for(int i = n - 1 ; i >= 0 ; --i){ Inv[A[i]] = Get_Bit(A[i]); Add_Bit(A[i],1); } for(int i = 0 ; i <= n ; ++i) { Bit[i] = 0; } LL ans = 0; LL sum = 0; ordered_set S; for(int i = 1 ; i <= n ; ++i){ sum += Inv[i]; S.insert(Pos[i]); Add_Bit(Pos[i],1); Add_Idx(Pos[i],Pos[i]); int m = *S.find_by_order(i / 2); int Ri = Get_Bit(n) - Get_Bit(m); int Le = Get_Bit(m - 1); ans = 0; ans += Get_Idx(n) - Get_Idx(m) - Ri * (m + 1); ans += Le * (m - 1) - Get_Idx(m - 1); if(Get_Bit(m) - Get_Bit(m - 1) == 0) ans += min(Le,Ri); Ri--;Le--; ans -= Ri * (Ri + 1) / 2; ans -= Le * (Le + 1) / 2; cout << ans + sum << " "; } puts(""); }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int maxn = 2e5 + 233; int ch[maxn]; int a[maxn], n, p[maxn]; long long ans[maxn], sum[maxn]; void add(int x, int v) { for (int i = x; i <= n; i += i & (-i)) ch[i] += v; } int query(int x) { int sum = 0; for (int i = x; i; i -= i & (-i)) sum += ch[i]; return sum; } void add_sum(int x, int v) { for (int i = x; i <= n; i += i & (-i)) sum[i] += v; } int query_sum(int x) { long long res = 0; for (int i = x; i; i -= i & (-i)) res += sum[i]; return res; } int cal(int n, int x) { int t = query(x); return min(t, n - t); } int find(int k) { k--; int x = 0; for (int i = 20; i >= 0; i--) if (x + (1 << i) <= n && ch[x + (1 << i)] <= k) { x += 1 << i; k -= ch[x]; } return x + 1; } inline long long ari(int n) { return 1ll * n * (n + 1) / 2; } int main() { cin >> n; for (int i = 1; i <= n; i++) scanf("%d", &a[i]), p[a[i]] = i; for (int i = 1; i <= n; i++) { int x = p[i]; ans[i] = ans[i - 1] + (i - 1 - query(x)); add(x, 1); } memset(ch, 0, sizeof(ch)); long long sum = 0; for (int i = 1; i <= n; i++) { int x = p[i]; add(x, 1); add_sum(x, x); sum += x; int k = (i + 1) / 2; int c = find(k); long long tmp = query_sum(c); long long a = k * c - tmp - ari(k - 1); long long b = (sum - tmp) - (i - k) * c - ari(i - k); ans[i] += a + b; } for (int i = 1; i <= n; i++) printf("%I64d ", ans[i]); return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; int N; int p[maxn], ip[maxn]; long long sum[maxn << 2][2]; void update(int ver, int pos, int val) { while (pos <= N) { sum[pos][ver] += val; pos += pos & (-pos); } } long long ask(int ver, int pos) { int ret = 0; while (pos) { ret += sum[pos][ver]; pos -= pos & (-pos); } return ret; } int query(int ver, int l, int r) { return ask(ver, r) - ask(ver, l - 1); } int find_mid(int x) { int l = 1, r = N, ret = -1; while (l <= r) { int mid = (l + r) >> 1; if (ask(0, mid) >= x) { ret = mid; r = mid - 1; } else { l = mid + 1; } } return ret; } int main() { scanf("%d", &N); for (int i = 1; i <= N; ++i) { scanf("%d", &p[i]); ip[p[i]] = i; } long long cnt = 0; for (int i = 1; i <= N; ++i) { cnt += query(0, ip[i], N); update(0, ip[i], 1); update(1, ip[i], ip[i]); long long ret = 0; int mid = find_mid(i / 2 + 1); long long l = i / 2, r = i - l - 1; ret += (long long)mid * l - query(1, 1, mid - 1) - l * (l + 1) / 2; ret += -(long long)mid * r + query(1, mid + 1, N) - r * (r + 1) / 2; printf("%lld ", ret + cnt); } printf("\n"); }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int s[200005], sum[200005], ara[200005], p[200005], seg[4 * 200005]; void add(int x, int n) { while (x <= n) { sum[x]++; x += (x & -x); } return; } int query(int x) { int ret = 0; while (x) { ret += sum[x]; x -= (x & -x); } return ret; } void update(int n, int s, int e, int indx) { if (s == indx && e == indx) { seg[n]++; return; } if (s > indx || e < indx) return; int mid = (s + e) / 2; update(n * 2, s, mid, indx); update(n * 2 + 1, mid + 1, e, indx); seg[n] = seg[n * 2] + seg[n * 2 + 1]; return; } int query(int n, int s, int e, int k) { if (s == e && k == 1) return s; int mid = (s + e) / 2; if (seg[n * 2] < k) return query(n * 2 + 1, mid + 1, e, k - seg[n * 2]); else return query(n * 2, s, mid, k); } int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &ara[i]), p[ara[i]] = i; for (int i = 1; i <= n; i++) s[i] = query(n) - query(p[i]), add(p[i], n); printf("0"); long long res = 0; memset(sum, 0, sizeof sum); add(p[1], n); update(1, 1, n, p[1]); int mn = p[1], mx = p[1]; for (int i = 2; i <= n; i++) { res += s[i]; update(1, 1, n, p[i]); add(p[i], n); if (p[i] < mn || p[i] > mx) mn = min(mn, p[i]), mx = max(mx, p[i]); else { int x1 = query(p[i] - 1); int x2 = query(n) - x1 - 1; res -= min(x1, x2); } int l = p[i]; int r = query(1, 1, n, (i + 1) / 2); if (l > r) swap(l, r); if (l < r) res += (r - l + 1) - (query(r) - query(l - 1)); printf(" %lld", res); } puts(""); return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> int main() { int i,j,n,k; cin>>n; vector<int>a(n),pos(n+1); for(i=0;i<n;i++){ cin>>a[i]; pos[a[i]]=i; } ordered_set s; int l=0,r=0,ele=pos[1];long long ans=0; for(i=1;i<=n;i++){ if(i==1){ s.insert(pos[i]); cout<<ans<<" "; } else{ s.insert(pos[i]); int rank=s.order_of_key(pos[i]); if(ele>pos[i]) l++; else r++; if(l==r || l==r+1 || l+1==r){ if(*(s.find_by_order(0))>pos[i]){ ans+=(ele-pos[i]-l)+i-1; }else if(*(s.find_by_order(i-1))<pos[i]){ ans+=(pos[i]-ele-r); }else if(ele>pos[i]){ ans+=(ele-pos[i]-(l-rank))-rank + i-rank-1; }else{ rank=i-rank-1; int waste=pos[i]-ele-1-(r-rank-1); ans+=waste; } }else{ if(l==r+2){ ele=*(s.find_by_order(s.order_of_key(ele)-1)); l--;r++; if(ele==a[i])ans=ans-l+r; else{ if(*(s.find_by_order(0))>pos[i]){ ans+=(ele-pos[i]-l)+i-1; }else if(*(s.find_by_order(i-1))<pos[i]){ ans+=(pos[i]-ele-r); }else if(ele>pos[i]){ ans+=(ele-pos[i]-(l-rank))-rank + i-rank-1; }else{ rank=i-rank-1; int waste=pos[i]-ele-1-(r-rank-1); ans+=waste; } } }else{ ele=*(s.find_by_order(s.order_of_key(ele)+1)); l++;r--; if(ele==a[i])ans=ans-l+r; else{ if(*(s.find_by_order(0))>pos[i]){ ans+=(ele-pos[i]-l)+i-1; }else if(*(s.find_by_order(i-1))<pos[i]){ ans+=(pos[i]-ele-r); }else if(ele>pos[i]){ ans+=(ele-pos[i]-(l-rank))-rank + i-rank-1; }else{ rank=i-rank-1; int waste=pos[i]-ele-1-(r-rank-1); ans+=waste; } } } } cout<<ans<<" "; } } cout<<endl; return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int mod = 1000000007; int n, a[200010], p[200010], c[200010]; long long inv[200010]; set<int> st; set<int>::iterator it; int Lowbit(int x) { return x & (-x); } void Update(int x, int d) { while (x <= n) { c[x] += d; x += Lowbit(x); } } int Getsum(int x) { int res = 0; while (x) { res += c[x]; x -= Lowbit(x); } return res; } int main() { ios_base::sync_with_stdio(false); cin >> n; for (int i = 1; i <= n; ++i) { cin >> a[i]; p[a[i]] = i; } cout << "0 "; int center = p[1], lnum = 0, rnum = 0; long long sum = 0; st.insert(p[1]); Update(p[1], 1); for (int i = 2; i <= n; ++i) { inv[i] = Getsum(n) - Getsum(p[i]); Update(p[i], 1); st.insert(p[i]); long long nxt_sum = sum + inv[i] + abs(p[i] - center) - abs(Getsum(center) - Getsum(p[i])); if (center < p[i]) nxt_sum -= inv[i]; else nxt_sum -= Getsum(p[i] - 1); if (p[i] > center) rnum++; else lnum++; if (lnum == rnum + 1) { it = st.find(center); it--; center = (*it); lnum--; rnum++; } if (rnum == lnum + 2) { it = st.find(center); it++; nxt_sum -= 2LL * ((*it) - center - 1); center = (*it); lnum++; rnum--; } sum = nxt_sum; cout << sum << " "; } cout << endl; return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MAXN = 200100; const long long INF = 0x3f3f3f3f3f3f3f3f; struct seg { seg *fe, *fd; long long meio, soma, l, r; seg(long long x, long long y) { l = x, r = y; meio = (x + y) / 2; soma = 0; if (l == r) return; fe = new seg(x, meio); fd = new seg(meio + 1, y); } void up(long long pos, long long novo) { if (l == r) { soma = novo; return; } if (pos <= meio) fe->up(pos, novo); else fd->up(pos, novo); soma = fe->soma + fd->soma; } long long get(long long a, long long b) { if (l >= a && r <= b) return soma; if (l > b || r < a) return 0; return fe->get(a, b) + fd->get(a, b); } long long busca(long long k) { if (l == r) return l; if (fe->soma < k) return fd->busca(k - fe->soma); return fe->busca(k); } }; long long v[MAXN], p[MAXN]; seg st1(0, MAXN), st2(0, MAXN); signed main() { long long n; cin >> n; for (long long i = 1; i <= n; i++) cin >> v[i], p[v[i]] = i; long long inv = 0; for (long long i = 1; i <= n; i++) { st1.up(p[i], 1); st2.up(p[i], p[i]); long long mid = (i + 1) / 2; long long g = st1.busca(mid); inv += st1.get(p[i] + 1, n); long long l = st1.get(0, p[i] - 1), r = st1.get(p[i] + 1, n + 1); long long sl = st2.get(0, p[i] - 1), sr = st2.get(p[i] + 1, n + 1); long long s = (p[i] * l - sl) - ((l) * (l + 1)) / 2; s += (sr - p[i] * r) - ((r) * (r + 1)) / 2; cout << s + inv << " "; } }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MX = 2e5 + 5, MXX = 23; const long long mod = 1e9 + 7, inf = 1e18 + 6; int n, a[MX], ind[MX], ver[MX]; long long seg[4 * MX], rm[4 * MX]; void add(int l) { for (int i = l + 1; i < MX; i += i & -i) ver[i]++; } int ask(int pn) { int ans = 0; for (pn++; pn; pn -= pn & -pn) ans += ver[pn]; return ans; } void clean(int u, pair<int, int> bz) { if (!rm[u]) return; seg[u] += rm[u]; if (bz.first + 1 < bz.second) { rm[u << 1] += rm[u]; rm[u << 1 | 1] += rm[u]; } rm[u] = 0; } void upd(int u, pair<int, int> bz, int l, int r, long long x) { clean(u, bz); if (bz.first == l && bz.second == r) { rm[u] = x; return; } int md = (bz.first + bz.second) / 2; if (l < md) upd(u << 1, make_pair(bz.first, md), l, min(md, r), x); if (r > md) upd(u << 1 | 1, make_pair(md, bz.second), max(md, l), r, x); clean(u << 1, make_pair(bz.first, md)); clean(u << 1 | 1, make_pair(md, bz.second)); seg[u] = min(seg[u << 1], seg[u << 1 | 1]); } long long ask(int u, pair<int, int> bz, int pn) { clean(u, bz); if (bz.first + 1 == bz.second) return seg[u]; int md = (bz.first + bz.second) / 2; if (pn < md) return ask(u << 1, make_pair(bz.first, md), pn); return ask(u << 1 | 1, make_pair(md, bz.second), pn); } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << fixed << setprecision(9); memset(seg, 63, sizeof seg); cin >> n; for (int i = 0; i < n; ++i) cin >> a[i]; for (int i = 0; i < n; ++i) ind[a[i]] = i; set<int> nw; int val = 0; for (int i = 1; i < n + 1; ++i) { add(ind[i]); int ver = ask(ind[i]); nw.insert(ind[i]); auto v = nw.find(ind[i]); int pay = 0, dis = 0, bc = 0; bool its = false; if (v != nw.begin()) { upd(1, make_pair(0, MX), 0, ind[i], ind[i] - (*prev(v, 1)) - 1 - i + ver); pay = ask(1, make_pair(0, MX), (*prev(v, 1))); dis = ind[i] - (*prev(v, 1)) - 1; bc = ver - 1; its = true; } if (next(v, 1) != nw.end()) { upd(1, make_pair(0, MX), ind[i] + 1, MX, (*next(v, 1)) - ind[i] - 1 - ver + 1); if (!its) { pay = ask(1, make_pair(0, MX), (*next(v, 1))); dis = (*next(v, 1)) - ind[i] - 1; bc = i - ver; } } long long tmp = ask(1, make_pair(0, MX), ind[i]); pay += (long long)bc * dis; pay -= (long long)(i - bc - 1) * (dis + 1); upd(1, make_pair(0, MX), ind[i], ind[i] + 1, pay - tmp); val += i - ver; cout << seg[1] + val << ' '; } cout << "\n"; return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include<bits/stdc++.h> #define ll long long using namespace std; struct SEG{ struct node{ int l,r; ll req; }tree[1<<22]; int tn=1; void make(int a){ if(a>=tn) return; make(2*a); make(2*a+1); tree[a].l=tree[2*a].l; tree[a].r=tree[2*a+1].r; tree[a].req=tree[2*a].req+tree[2*a+1].req; } void init(int n){ while(tn<n) tn*=2; for(int i=tn;i<2*tn;i++) tree[i].l=tree[i].r=i-tn+1, tree[i].req=0; make(1); } void update(int a, int s, ll d){ if(s<tree[a].l || tree[a].r<s); else if(s<=tree[a].l && tree[a].r<=s) tree[a].req=d; else{ update(2*a,s,d); update(2*a+1,s,d); tree[a].req=tree[2*a].req+tree[2*a+1].req; } } ll query(int a, int s, int d){ if(d<tree[a].l || tree[a].r<s) return 0; else if(s<=tree[a].l && tree[a].r<=d) return tree[a].req; else return query(2*a,s,d)+query(2*a+1,s,d); } }seg; int N, arr[200001]; int where[200001]; int main(){ cin>>N; for(int i=1;i<=N;i++){ cin>>arr[i]; where[arr[i]]=i; } seg.init(N); int ans=0; cout<<0<<' '; int wo=where[1]; seg.update(1,where[1],1); for(int i=2;i<=N;i++){ if(where[i]<wo){ ans+=wo-where[i]-1-seg.query(1,where[i],wo-1)+i-1; } else { ans+=where[i]-wo-1-seg.query(1,wo+1,where[i]); } seg.update(1,where[i],1); cout<<ans<<' '; } }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) { return os << "(" << P.first << "," << P.second << ")"; } template <class T> ostream &operator<<(ostream &os, vector<T> V) { os << "["; for (auto vv : V) os << vv << ","; return os << "]"; } template <class TH> void _dbg(const char *sdbg, TH h) { cerr << sdbg << '=' << h << '\n'; } template <class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while (*sdbg != ',') cerr << *sdbg++; cerr << '=' << h << ','; _dbg(sdbg + 1, a...); } const long long N = 2e5 + 7; long long n, p[N], pos[N]; struct BIT { vector<long long> bit; long long n; BIT(long long n) : n(n + 1) { bit.assign(this->n, 0); } void add(long long idx, long long val) { for (++idx; idx < n; idx += idx & -idx) bit[idx] += val; } long long sum(long long idx) { long long ret = 0; for (++idx; idx > 0; idx -= idx & -idx) ret += bit[idx]; return ret; } long long sum(long long l, long long r) { return sum(r) - sum(l - 1); } }; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; cin >> n; for (long long i = 1; i <= n; i++) cin >> p[i], pos[p[i]] = i; BIT bt1(n + 2), lft(n + 2), rht(n + 2), cnt(n + 2); for (long long i = 1; i <= n; i++) lft.add(i, i), cnt.add(i, 1); long long l = pos[1], r = pos[1]; long long totinv = 0; for (long long i = 1; i <= n; i++) { long long idx = pos[i]; l = min(l, idx), r = max(r, idx); cnt.add(idx, -1); lft.add(idx, -idx); bt1.add(idx, 1); long long inv = bt1.sum(idx + 1, n); totinv += inv; long long mid = (l + r) / 2; long long lsum = lft.sum(l, mid), lcnt = cnt.sum(l, mid); long long tl = lsum - lcnt * l; long long rsum = lft.sum(mid + 1, r), rcnt = cnt.sum(mid + 1, r); long long tr = rcnt * r - rsum; long long ans = totinv + tl + tr; cout << ans << " "; } return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; #include <bits/extc++.h> /** keep-include */ using namespace __gnu_pbds; template<class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; void __print(int x) {cerr << x;} void __print(long x) {cerr << x;} void __print(long long x) {cerr << x;} void __print(unsigned x) {cerr << x;} void __print(unsigned long x) {cerr << x;} void __print(unsigned long long x) {cerr << x;} void __print(float x) {cerr << x;} void __print(double x) {cerr << x;} void __print(long double x) {cerr << x;} void __print(char x) {cerr << '\'' << x << '\'';} void __print(const char *x) {cerr << '\"' << x << '\"';} void __print(const string &x) {cerr << '\"' << x << '\"';} void __print(bool x) {cerr << (x ? "true" : "false");} template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';} template<typename T> void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";} void _print() {cerr << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);} #ifndef ONLINE_JUDGE #define debug(x...) cerr << "[" << #x << "] = ["; _print(x) #else #define debug(x...) #endif #define rep(i, n) for(int i = 0; i < (n); ++i) #define repA(i, a, n) for(int i = a; i <= (n); ++i) #define repD(i, a, n) for(int i = a; i >= (n); --i) #define trav(a, x) for(auto& a : x) #define all(x) x.begin(), x.end() #define sz(x) (int)(x).size() #define fill(a) memset(a, 0, sizeof (a)) #define fst first #define snd second #define mp make_pair #define pb push_back typedef long double ld; typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; void pre(){ } void solve(){ } const ll inf = 1e9; struct Node { Node *l = 0, *r = 0; int lo, hi; ll val = 0; Node(int lo,int hi): lo(lo),hi(hi){} // Large interval of -inf Node(vi& v, int lo, int hi) : lo(lo), hi(hi) { if (lo + 1 < hi) { int mid = lo + (hi - lo)/2; l = new Node(v, lo, mid); r = new Node(v, mid, hi); } } ll query(int L, int R) { if (R <= lo || hi <= L) return 0; if (L <= lo && hi <= R) return val; return l->query(L, R)+r->query(L, R); } void add(int L, int R, ll x) { if (R <= lo || hi <= L) return; if (L <= lo && hi <= R) val = x; else { l->add(L, R, x), r->add(L, R, x); val = l->val+r->val; } } }; ll C2(ll x){ return x*(x+1)/2; } int main() { cin.sync_with_stdio(0); cin.tie(0); cin.exceptions(cin.failbit); pre(); int n;cin>>n; vi p(n+1,0), pos(n+1,0); repA(i,1,n)cin>>p[i], pos[p[i]]=i; Tree<int > T; vi v; Node N(v,0,n+10); ll ans=0; repA(i,1,n){ int j = pos[i]; ans+=i-1-T.order_of_key(j); T.insert(j); N.add(j,j+1,j); int m=*T.find_by_order(i/2); int r=(i-1)/2, l=i/2; ll sr=N.query(m+1,n+1); ll sl=N.query(0,m); ll cur = sr-r*m-C2(r)+l*m-sl-C2(l)+ans; cout<<cur<<'\n'; } return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using namespace std; class Median_heap { private: priority_queue<int> min_heap; priority_queue<int> max_heap; long long sml = 0; long long big = 0; public: Median_heap(){}; void update(int x) { if (min_heap.empty()) { min_heap.push(-x); big += x; return; } int t = -min_heap.top(); if (x >= t) min_heap.push(-x), big += x; else max_heap.push(x), sml += x; while ((int)(min_heap.size() - max_heap.size()) > 1) { int t = -min_heap.top(); min_heap.pop(); big -= t; max_heap.push(-t); sml += t; } while ((int)(max_heap.size() - min_heap.size()) > 0) { int t = max_heap.top(); max_heap.pop(); sml -= t; min_heap.push(-t); big += t; } } int find_median() { return -min_heap.top(); } long long query() { long long m = -min_heap.top(); long long sm = max_heap.size(); long long bg = min_heap.size(); return big - ((2 * m + bg - 1) * bg) / 2 + ((2 * m - sm - 1) * sm) / 2 - sml; } }; class Segment_Tree { private: vector<int> seg; int n; public: Segment_Tree(int n) { this->n = n; seg.resize(4 * n, 0); } void update(int idx, int l, int r, int f) { if (l > f || r < f) return; seg[idx]++; if (l == r) return; update(2 * idx, l, (l + r) / 2, f); update(2 * idx + 1, (l + r) / 2 + 1, r, f); } int find(int idx, int l, int r, int fl, int fr) { if (l > fr || r < fl) return 0; if (fl <= l && r <= fr) return seg[idx]; return find(2 * idx, l, (l + r) / 2, fl, fr) + find(2 * idx + 1, (l + r) / 2 + 1, r, fl, fr); } }; int main(void) { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; Median_heap mh = Median_heap(); Segment_Tree sg = Segment_Tree(n); vector<int> arr(n + 5, 0); vector<int> idx(n + 5, 0); for (int i = 1; i <= n; i++) { int x; cin >> x; arr[i] = x; idx[x] = i; } vector<long long> swap(n + 5, 0); vector<long long> move(n + 5, 0); for (int i = 1; i <= n; i++) { swap[i] = swap[i - 1] + sg.find(1, 1, n, idx[i] + 1, n); sg.update(1, 1, n, idx[i]); } for (int i = 1; i <= n; i++) { mh.update(idx[i]); move[i] = mh.query(); } for (int i = 1; i <= n; i++) cout << swap[i] + move[i] << " "; return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #include <cassert> #include <numeric> #include <type_traits> namespace atcoder { namespace internal { #ifndef _MSC_VER template <class T> using is_signed_int128 = typename std::conditional<std::is_same<T, __int128_t>::value || std::is_same<T, __int128>::value, std::true_type, std::false_type>::type; template <class T> using is_unsigned_int128 = typename std::conditional<std::is_same<T, __uint128_t>::value || std::is_same<T, unsigned __int128>::value, std::true_type, std::false_type>::type; template <class T> using make_unsigned_int128 = typename std::conditional<std::is_same<T, __int128_t>::value, __uint128_t, unsigned __int128>; template <class T> using is_integral = typename std::conditional<std::is_integral<T>::value || is_signed_int128<T>::value || is_unsigned_int128<T>::value, std::true_type, std::false_type>::type; template <class T> using is_signed_int = typename std::conditional<(is_integral<T>::value && std::is_signed<T>::value) || is_signed_int128<T>::value, std::true_type, std::false_type>::type; template <class T> using is_unsigned_int = typename std::conditional<(is_integral<T>::value && std::is_unsigned<T>::value) || is_unsigned_int128<T>::value, std::true_type, std::false_type>::type; template <class T> using to_unsigned = typename std::conditional< is_signed_int128<T>::value, make_unsigned_int128<T>, typename std::conditional<std::is_signed<T>::value, std::make_unsigned<T>, std::common_type<T>>::type>::type; #else template <class T> using is_integral = typename std::is_integral<T>; template <class T> using is_signed_int = typename std::conditional<is_integral<T>::value && std::is_signed<T>::value, std::true_type, std::false_type>::type; template <class T> using is_unsigned_int = typename std::conditional<is_integral<T>::value && std::is_unsigned<T>::value, std::true_type, std::false_type>::type; template <class T> using to_unsigned = typename std::conditional<is_signed_int<T>::value, std::make_unsigned<T>, std::common_type<T>>::type; #endif template <class T> using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>; template <class T> using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>; template <class T> using to_unsigned_t = typename to_unsigned<T>::type; } // namespace internal } // namespace atcoder #include <cassert> #include <vector> namespace atcoder { // Reference: https://en.wikipedia.org/wiki/Fenwick_tree template <class T> struct fenwick_tree { using U = internal::to_unsigned_t<T>; public: fenwick_tree() : _n(0) {} fenwick_tree(int n) : _n(n), data(n) {} void add(int p, T x) { assert(0 <= p && p < _n); p++; while (p <= _n) { data[p - 1] += U(x); p += p & -p; } } T sum(int l, int r) { assert(0 <= l && l <= r && r <= _n); return sum(r) - sum(l); } private: int _n; std::vector<U> data; U sum(int r) { U s = 0; while (r > 0) { s += data[r - 1]; r -= r & -r; } return s; } }; } // namespace atcoder typedef struct _median_manage { // vaiables atcoder::fenwick_tree<int> fw; int nmax, real_size; // construct _median_manage(int _n) : nmax(_n), real_size(_n+10) { fw = atcoder::fenwick_tree<int>(real_size); } bool insert(int i) { if(fw.sum(i, i+1) == 1) return false; fw.add(i, 1); return true; } bool erase(int i) { if(fw.sum(i, i+1) == 0) return false; fw.add(i, -1); return true; } int get_median() { int sum = fw.sum(0, real_size); assert(sum > 0); int l = -1, r = real_size; while(r - l > 1) { int m = (l + r) / 2; if(fw.sum(0, m+1) < (sum+1) / 2) l = m; else r = m; } return r; } int get_lnum() { int sum = fw.sum(0, real_size); return (sum+1) / 2; } int get_rnum() { int sum = fw.sum(0, real_size); return sum / 2; } int get_sum() { return fw.sum(0, real_size); } } medi; using namespace std; int main() { int n; cin >> n; vector<int> p(n), q(n); for(int i = 0; i < n; i++) { cin >> p[i]; p[i]--; q[p[i]] = i; } medi md(n); int rmax = n+10; atcoder::fenwick_tree<int> inv(rmax), idx(rmax); long long ninv = 0; for(int i = 0; i < n; i++) { // update inv.add(q[i], 1); idx.add(q[i], q[i]); assert(md.insert(q[i])); // count inversion ninv += inv.sum(q[i]+1, rmax); // print ans int med = md.get_median(); int lnum = md.get_lnum(); int rnum = md.get_rnum(); long long l = med * lnum - idx.sum(0, med+1) - lnum * (lnum-1) / 2; long long r = idx.sum(med+1, rmax) - med * (rnum) - (rnum) * (rnum+1) / 2; cout << ninv + l + r << (i == n-1 ? "\n" : " "); } return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 200010; int tr1[N], tr2[N]; int n; int c[N]; int lowbit(int x) { return x & -x; } void ud(int tr[], int x, int d) { for (int i = x; i <= n; i += lowbit(i)) { tr[i] += d; } } int qur(int tr[], int x) { int ans = 0; for (int i = x; i; i -= lowbit(i)) { ans += tr[i]; } return ans; } int get_pos(int tr[], int x) { int l = 0, r = n; while (l < r) { int mid = (l + r) >> 1; if (qur(tr, mid) >= x) r = mid; else l = mid + 1; } return l; } int main() { cin >> n; for (int i = 1; i <= n; i++) { int x; cin >> x; c[x] = i; } int d = 0; for (int i = 1; i <= n; i++) { ud(tr1, c[i], 1); ud(tr2, c[i], c[i]); d += (i - qur(tr1, c[i])); int pos = get_pos(tr1, (i + 1) / 2); int aa = (i - 1) / 2; int bb = i / 2; int t1 = (long long)aa * pos - (1 + aa) * aa / 2; int t2 = (1ll + bb) * bb / 2 + bb * pos; int q1 = qur(tr2, pos - 1); int q2 = qur(tr2, n) - qur(tr2, pos); long long ans = q2 - t2 + t1 - q1; cout << (ans + d) << ' '; } }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#pragma GCC optimize(2) #include <cstdio> #include <queue> #include <string> #include <cstring> #include <algorithm> #include <cstdlib> #include <iostream> #include <iomanip> #include <cmath> #include <vector> #include <set> #include <map> #include <fstream> #include <cassert> #include <complex> #include <ctime> #include <tr1/unordered_map> using namespace std; using namespace tr1; typedef long long ll; const int maxn = 2e5 + 10; int n; int aa[maxn]; int inv[maxn]; int sum[maxn * 4]; ll in[maxn]; void build(int rt,int l,int r) { if(l == r) { sum[rt] = 0; return ; } int mid = (l + r) / 2; build(rt * 2,l,mid); build(rt * 2 + 1,mid + 1,r); sum[rt] = sum[rt * 2] + sum[rt * 2 + 1]; } void update(int rt,int x,int al,int ar,int k) { if(x < al || x > ar) return ; if(al == ar) { sum[rt] += k; return ; } int mid = (al + ar) / 2; update(rt * 2,x,al,mid,k); update(rt * 2 + 1,x,mid + 1,ar,k); sum[rt] = sum[rt * 2] + sum[rt * 2 + 1]; } int query(int rt,int l,int r,int al,int ar) { if(al > r || ar < l) return 0; if(al >= l && ar <= r) return sum[rt]; int mid = (al + ar) / 2; return query(rt * 2,l,r,al,mid) + query(rt * 2 + 1,l,r,mid + 1,ar); } int query_rank(int rt,int al,int ar,int rk) { if(al == ar) return al; int mid = (al + ar) / 2; if(sum[rt * 2] >= rk) return query_rank(rt * 2,al,mid,rk); return query_rank(rt * 2 + 1,mid + 1,ar,rk - sum[rt * 2]); } int main() { scanf("%d",&n); for(int i = 1; i <= n; i++) scanf("%d",&aa[i]); for(int i = 1; i <= n; i++) inv[aa[i]] = i; in[0] = 0; for(int i = 1; i <= n; i++) { in[i] = in[i - 1] + query(1,inv[i],n,1,n); update(1,inv[i],1,n,1); } build(1,1,n); update(1,inv[1],1,n,1); int tmp1,tmp2,tmp3; tmp3 = query_rank(1,1,n,1); ll ans = 0; for(int i = 2; i <= n; i++) { ans = 0; if(i % 2 == 0) { int temp = query(1,1,inv[i],1,n); temp = min(i - 1 - temp,temp); ans -= temp; update(1,inv[i],1,n,1); if(inv[i] > tmp3) temp = inv[i] - tmp3 + 1 - query(1,tmp3,inv[i],1,n); else temp = tmp3 - inv[i] + 1 - query(1,inv[i],tmp3,1,n); ans += temp; tmp1 = query_rank(1,1,n,i / 2); tmp2 = query_rank(1,1,n,i / 2 + 1); } else { int temp = query(1,1,inv[i],1,n); temp = min(i - 1 - temp,temp); ans -= temp; update(1,inv[i],1,n,1); if(inv[i] < tmp1) temp = tmp1 - inv[i] + 1 - query(1,inv[i],tmp1,1,n); else if(inv[i] > tmp2) temp = inv[i] - tmp2 + 1 - query(1,tmp2,inv[i],1,n); ans += temp; tmp3 = query_rank(1,1,n,i / 2 + 1); } in[i] += ans; } for(int i = 1; i <= n; i++) { if(i != 1) printf(" "); printf("%lld",in[i]); } }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 200000; int a[200005], pos[200005]; long long sum1[200005], sum2[200005]; void add(long long *sum1, int x, int val) { while (x <= maxn) { sum1[x] += val; x += x & (-x); } } long long sum(long long *sum1, int pos) { long long res = 0; while (pos) { res += sum1[pos]; pos -= pos & (-pos); } return res; } int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; pos[a[i]] = i; } int ans1 = 0, ans2 = 0; for (int i = 1; i <= n; i++) { ans1 += i - 1 - sum(sum1, pos[i]); add(sum1, pos[i], 1); add(sum2, pos[i], pos[i]); int l = 1, r = n; while (l < r) { int mid = l + r + 1 >> 1; if (sum(sum1, mid) * 2 <= i) { l = mid; } else r = mid - 1; } int cnt = sum(sum1, l); long long sum3 = sum(sum2, l); ans2 = 0; ans2 += cnt * l - sum3 - cnt * (cnt - 1) / 2; cnt = i - cnt; sum3 = sum(sum2, n) - sum(sum2, l); ans2 += sum3 - cnt * (l + 1) - cnt * (cnt - 1) / 2; cout << ans1 + ans2 << " "; } cout << endl; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = 1061109567; const long long LINF = 4557430888798830399ll; const int MOD = 1000000007; long long qpow(long long x, long long n) { long long res = 1; while (n) { if (n & 1) res = res * x % MOD; x = x * x % MOD; n >>= 1; } return res; } struct SegmentTree { static const int MAXN = 200000; int st[(MAXN << 2) + 5]; void PushUp(int o) { st[o] = st[(o << 1)] + st[(o << 1 | 1)]; } void Build(int o, int l, int r) { if (l == r) st[o] = 0; else { int m = l + r >> 1; Build((o << 1), l, m); Build((o << 1 | 1), m + 1, r); PushUp(o); } } void Update(int o, int l, int r, int p, int v) { if (l == r) { st[o] += v; return; } else { int m = l + r >> 1; if (p <= m) Update((o << 1), l, m, p, v); if (p >= m + 1) Update((o << 1 | 1), m + 1, r, p, v); PushUp(o); } } int Query(int o, int l, int r, int ql, int qr) { if (ql <= l && r <= qr) { return st[o]; } else { int m = l + r >> 1; int res = 0; if (ql <= m) res = res + Query((o << 1), l, m, ql, qr); if (qr >= m + 1) res = res + Query((o << 1 | 1), m + 1, r, ql, qr); return res; } } } st; int n; int a[200005]; int p[200005]; long long ans[200005]; void test_case() { scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", &a[i]); p[a[i]] = i; } st.Build(1, 1, n); int Lmost = p[1], Rmost = p[1]; long long cnt = 0; st.Update(1, 1, n, p[1], 1); ans[1] = 0; for (int i = 2; i <= n; ++i) { if (p[i] < Lmost) Lmost = p[i]; if (p[i] > Rmost) Rmost = p[i]; cnt += st.Query(1, 1, n, p[i], n); st.Update(1, 1, n, p[i], 1); ans[i] = cnt + (Rmost - Lmost + 1 - i); } for (int i = 1; i <= n; ++i) printf("%lld%c", ans[i], " \n"[i == n]); } int main() { int t = 1; for (int ti = 1; ti <= t; ++ti) { test_case(); } }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#pragma GCC optimize ("-O3") #include <bits/stdc++.h> #include <complex> #include <queue> #include <set> #include <unordered_set> #include <list> #include <chrono> #include <random> #include <iostream> #include <algorithm> #include <cmath> #include <string> #include <vector> #include <map> #include <unordered_map> #include <stack> #include <iomanip> #include <fstream> using namespace std; typedef long long ll; typedef long double ld; typedef pair<ll,ll> p32; typedef pair<ll,ll> p64; typedef pair<double,double> pdd; typedef vector<ll> v64; typedef vector<ll> v32; typedef vector<vector<ll> > vv32; typedef vector<vector<ll> > vv64; typedef vector<vector<p64> > vvp64; typedef vector<p64> vp64; typedef vector<p32> vp32; ll MOD = 1e9+7; #define forn(i,e) for(ll i = 0; i < e; i++) #define forsn(i,s,e) for(ll i = s; i < e; i++) #define rforn(i,s) for(ll i = s; i >= 0; i--) #define rforsn(i,s,e) for(ll i = s; i >= e; i--) #define ln "\n" #define dbg(x) cout<<#x<<" = "<<x<<ln #define mp make_pair #define pb push_back #define fi first #define se second #define INF 1e18 #define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL) #define all(x) (x).begin(), (x).end() #define sz(x) ((ll)(x).size()) #define vvv64 vector<vv32> #define vvvv64 vector<vvv32> #include <ext/pb_ds/assoc_container.hpp> // Common file #include <ext/pb_ds/tree_policy.hpp> #include <bits/stdc++.h> using namespace __gnu_pbds; using namespace std; // a new data structure defined. Please refer below // GNU link : https://goo.gl/WVDL6g typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> tr; void solve() { int n; cin>>n; v64 pos(n+1); for (int i=1; i<=n; i++) { int temp; cin>>temp; pos[temp]=i; } ll count=0; tr tree; ll mini=1e9; ll maxi=0; for (int i=1; i<=n; i++) { count+=i-1-tree.order_of_key(pos[i]); tree.insert(pos[i]); mini=min(mini,pos[i]); maxi=max(maxi,pos[i]); ll ans=maxi-mini+1-i+count; cout<<ans<<" "; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll t=1; // cin>>t; while (t--) { solve(); } return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long const inf = 1e9; long long const mod = 1e9 + 7; long double const eps = 1e-9; int bit[200005]; int bit2[200005]; long long sumbit[200005]; int n; void update(int k) { while (k <= 200000) { bit[k] += 1; k += k & -k; } } int sum(int k) { int s = 0; while (k > 0) { s += bit[k]; k -= k & -k; } return s; } void update2(int k) { while (k <= 200000) { bit2[k] += 1; k += k & -k; } } int sum2(int k) { int s = 0; while (k > 0) { s += bit2[k]; k -= k & -k; } return s; } void update3(int k, int val) { while (k <= 200000) { sumbit[k] += val; k += k & -k; } } long long sum3(int k) { long long s = 0; while (k > 0) { s += sumbit[k]; k -= k & -k; } return s; } int cinv(int pos, int num) { int SUM = num - 1; SUM -= sum2(pos); update2(pos); return SUM; } int main() { cin >> n; int a[n]; int pos[n + 1]; for (int i = (0); i < (n); i++) { cin >> a[i]; pos[a[i]] = i + 1; } long long inversions = 0; for (int i = (1); i < (n + 1); i++) { inversions += cinv(pos[i], i); update(pos[i]); update3(pos[i], pos[i]); int k = 0; for (int b = n / 2; b >= 1; b /= 2) { while (k + b <= n && sum(k + b) <= i / 2) k += b; } k++; int l = sum(k - 1); int r = sum(n) - sum(k); long long suml = sum3(k - 1); long long sumr = sum3(n) - sum3(k); long long ans = inversions + (long long)l * k - suml + sumr - (long long)r * k - (long long)(l * (l + 1)) / 2 - (long long)(r * (r + 1)) / 2; cout << ans << endl; } return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const bool debug = true; int p[200001]; int pos[200001]; class seg_tree { int* st; int n; public: seg_tree(int a) { st = new int[2 * a]; for (int i = 0; i < 2 * a; i++) st[i] = 0; n = a; } void set(int x) { x += n; st[x] = 1; x /= 2; while (x) { st[x] = st[2 * x] + st[2 * x + 1]; x /= 2; } } int query(int l, int r) { l += n; r += n; int ans = 0; while (l < r) { if (l & 1) { ans += st[l++]; } if (r & 1) { ans += st[--r]; } l >>= 1; r >>= 1; } return ans; } }; class median { priority_queue<int, vector<int>, greater<int>> higher; priority_queue<int> lower; int suml, sumr; public: median() { suml = 0; sumr = 0; } void insert(int n) { if ((lower.empty() && higher.empty()) || n < lower.top()) { lower.push(n); suml += n; if (lower.size() > higher.size() + 1) { higher.push(lower.top()); sumr += lower.top(); suml -= lower.top(); lower.pop(); } } else { higher.push(n); sumr += n; if (higher.size() > lower.size()) { lower.push(higher.top()); suml += higher.top(); sumr -= higher.top(); higher.pop(); } } } int get_median() { return lower.top(); } int get_swaps() { int n = lower.size(); int l = lower.top() * n - suml - ((n) * (n - 1)) / 2; n = higher.size(); int r = sumr - lower.top() * n - ((n) * (n + 1)) / 2; return l + r; } }; int main() { int n; cin >> n; for (int z = 1; z < n + 1; z++) { cin >> p[z]; pos[p[z]] = z; } seg_tree cind(n + 1); median med; int inv = 0; for (int z = 1; z < n + 1; z++) { inv += cind.query(pos[z], n + 1); cind.set(pos[z]); med.insert(pos[z]); cout << inv + med.get_swaps() << " "; } cout << endl; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MX = 2e5 + 5; const int INF = 1e9 + 9; int n; long long Bit[MX]; long long Idx[MX]; long long Inv[MX]; long long Pos[MX]; vector<int> A; void Add_Bit(int x, int val) { for (; x <= n + 1; x += x & -x) Bit[x] += val; } void Add_Idx(int x, int val) { for (; x <= n + 1; x += x & -x) Idx[x] += val; } long long Get_Bit(int x) { long long res = 0; for (; x > 0; x -= x & -x) res += Bit[x]; return res; } long long Get_Idx(int x) { long long res = 0; for (; x > 0; x -= x & -x) res += Idx[x]; return res; } void prep() { for (int i = 0; i <= n; ++i) { Bit[i] = 0; Idx[i] = 0; Pos[i] = 0; } } int main() { scanf("%d", &n); prep(); for (int i = 0; i < n; ++i) { int x; scanf("%d", &x); A.push_back(x); Pos[x] = i + 1; } for (int i = n - 1; i >= 0; --i) { Inv[A[i]] = Get_Bit(A[i]); Add_Bit(A[i], 1); } for (int i = 1; i <= n; ++i) { Bit[i] = 0; } long long ans = 0; long long L = INF; long long R = -1; long long sum = 0; for (int i = 1; i <= n; ++i) { sum += Inv[i]; L = min(L, Pos[i]); R = max(R, Pos[i]); Add_Bit(Pos[i], 1); Add_Idx(Pos[i], Pos[i]); int m = (L + R) >> 1; int Ri = Get_Bit(n) - Get_Bit(m); int Le = Get_Bit(m - 1); ans = 0; ans += Get_Idx(n) - Get_Idx(m) - Ri * (m + 1); ans += Le * (m - 1) - Get_Idx(m - 1); if (Get_Bit(m) - Get_Bit(m - 1) == 0) ans += min(Le, Ri); Ri--; Le--; ans -= Ri * (Ri + 1) / 2; ans -= Le * (Le + 1) / 2; cout << ans + sum << " "; } puts(""); }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #include <cassert> #include <numeric> #include <type_traits> namespace atcoder { namespace internal { #ifndef _MSC_VER template <class T> using is_signed_int128 = typename std::conditional<std::is_same<T, __int128_t>::value || std::is_same<T, __int128>::value, std::true_type, std::false_type>::type; template <class T> using is_unsigned_int128 = typename std::conditional<std::is_same<T, __uint128_t>::value || std::is_same<T, unsigned __int128>::value, std::true_type, std::false_type>::type; template <class T> using make_unsigned_int128 = typename std::conditional<std::is_same<T, __int128_t>::value, __uint128_t, unsigned __int128>; template <class T> using is_integral = typename std::conditional<std::is_integral<T>::value || is_signed_int128<T>::value || is_unsigned_int128<T>::value, std::true_type, std::false_type>::type; template <class T> using is_signed_int = typename std::conditional<(is_integral<T>::value && std::is_signed<T>::value) || is_signed_int128<T>::value, std::true_type, std::false_type>::type; template <class T> using is_unsigned_int = typename std::conditional<(is_integral<T>::value && std::is_unsigned<T>::value) || is_unsigned_int128<T>::value, std::true_type, std::false_type>::type; template <class T> using to_unsigned = typename std::conditional< is_signed_int128<T>::value, make_unsigned_int128<T>, typename std::conditional<std::is_signed<T>::value, std::make_unsigned<T>, std::common_type<T>>::type>::type; #else template <class T> using is_integral = typename std::is_integral<T>; template <class T> using is_signed_int = typename std::conditional<is_integral<T>::value && std::is_signed<T>::value, std::true_type, std::false_type>::type; template <class T> using is_unsigned_int = typename std::conditional<is_integral<T>::value && std::is_unsigned<T>::value, std::true_type, std::false_type>::type; template <class T> using to_unsigned = typename std::conditional<is_signed_int<T>::value, std::make_unsigned<T>, std::common_type<T>>::type; #endif template <class T> using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>; template <class T> using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>; template <class T> using to_unsigned_t = typename to_unsigned<T>::type; } // namespace internal } // namespace atcoder #include <cassert> #include <vector> namespace atcoder { // Reference: https://en.wikipedia.org/wiki/Fenwick_tree template <class T> struct fenwick_tree { using U = internal::to_unsigned_t<T>; public: fenwick_tree() : _n(0) {} fenwick_tree(int n) : _n(n), data(n) {} void add(int p, T x) { assert(0 <= p && p < _n); p++; while (p <= _n) { data[p - 1] += U(x); p += p & -p; } } T sum(int l, int r) { assert(0 <= l && l <= r && r <= _n); return sum(r) - sum(l); } private: int _n; std::vector<U> data; U sum(int r) { U s = 0; while (r > 0) { s += data[r - 1]; r -= r & -r; } return s; } }; } // namespace atcoder typedef struct _median_manage { // vaiables atcoder::fenwick_tree<int> fw; int nmax, real_size; // construct _median_manage(int _n) : nmax(_n), real_size(_n+10) { fw = atcoder::fenwick_tree<int>(real_size); } bool insert(int i) { if(fw.sum(i, i+1) == 1) return false; fw.add(i, 1); return true; } bool erase(int i) { if(fw.sum(i, i+1) == 0) return false; fw.add(i, -1); return true; } long long get_median() { int sum = fw.sum(0, real_size); assert(sum > 0); int l = -1, r = real_size; while(r - l > 1) { int m = (l + r) / 2; if(fw.sum(0, m+1) < (sum+1) / 2) l = m; else r = m; } return r; } long long get_lnum() { int sum = fw.sum(0, real_size); return (sum+1) / 2; } long long get_rnum() { int sum = fw.sum(0, real_size); return sum / 2; } long long get_sum() { return fw.sum(0, real_size); } } medi; using namespace std; int main() { int n; cin >> n; vector<int> p(n), q(n); for(int i = 0; i < n; i++) { cin >> p[i]; p[i]--; q[p[i]] = i; } medi md(n); int rmax = n+10; atcoder::fenwick_tree<int> inv(rmax), idx(rmax); long long ninv = 0; for(int i = 0; i < n; i++) { // update inv.add(q[i], 1); idx.add(q[i], q[i]); assert(md.insert(q[i])); // count inversion ninv += inv.sum(q[i]+1, rmax); // print ans auto med = md.get_median(); auto lnum = md.get_lnum(); auto rnum = md.get_rnum(); long long l = med * lnum - idx.sum(0, med+1) - lnum * (lnum-1) / 2; long long r = idx.sum(med+1, rmax) - med * (rnum) - (rnum) * (rnum+1) / 2; cout << ninv + l + r << (i == n-1 ? "\n" : " "); } return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using dd = double; using pll = pair<ll, ll>; using tll = tuple<ll, ll, ll>; using vll = vector<ll>; using vdd = vector<dd>; using vpll = vector<pll>; using vtll = vector<tll>; using vvll = vector<vll>; using vvdd = vector<vdd>; using vvpll = vector<vpll>; using vvtll = vector<vtll>; using vvvll = vector<vvll>; using vvvdd = vector<vvdd>; using vvvpll = vector<vvpll>; using vvvtll = vector<vvtll>; using vvvvll = vector<vvvll>; using vvvvdd = vector<vvvdd>; using vvvvpll = vector<vvvpll>; using vvvvtll = vector<vvvtll>; constexpr ll INF = 1LL << 60; constexpr dd EPS = 1e-11; struct Fast { Fast() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(std::numeric_limits<double>::max_digits10); } } fast; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline T MaxE(vector<T> &v, ll S, ll E) { T m = v[S]; for (ll i = (S); i <= (E); i++) chmax(m, v[i]); return m; } template <class T> inline T MinE(vector<T> &v, ll S, ll E) { T m = v[S]; for (ll i = (S); i <= (E); i++) chmin(m, v[i]); return m; } template <class T> inline T MaxE(vector<T> &v, ll N) { return MaxE(v, 0, N - 1); } template <class T> inline T MinE(vector<T> &v, ll N) { return MinE(v, 0, N - 1); } template <class T> inline T MaxE(vector<T> &v) { return MaxE(v, (ll)v.size()); } template <class T> inline T MinE(vector<T> &v) { return MinE(v, (ll)v.size()); } template <class T> inline ll MaxI(vector<T> &v, ll S, ll E) { ll m = S; for (ll i = (S); i <= (E); i++) { if (v[i] > v[m]) m = i; } return m; } template <class T> inline ll MinI(vector<T> &v, ll S, ll E) { ll m = S; for (ll i = (S); i <= (E); i++) { if (v[i] < v[m]) m = i; } return m; } template <class T> inline ll MaxI(vector<T> &v, ll N) { return MaxI(v, 0, N - 1); } template <class T> inline ll MinI(vector<T> &v, ll N) { return MinI(v, 0, N - 1); } template <class T> inline ll MaxI(vector<T> &v) { return MaxI(v, (ll)v.size()); } template <class T> inline ll MinI(vector<T> &v) { return MinI(v, (ll)v.size()); } template <class T> inline T Sum(vector<T> &v, ll S, ll E) { T s = v[S]; for (ll i = (S + 1); i <= (E); i++) s += v[i]; return s; } template <class T> inline T Sum(vector<T> &v, ll N) { return Sum(v, 0, N - 1); } template <class T> inline T Sum(vector<T> &v) { return Sum(v, v.size()); } template <class T> inline ll Size(T &v) { return (ll)v.size(); } template <class T> inline T POW(T a, ll n) { T r = 1; for (; n > 0; n >>= 1, a *= a) { if (n & 1) r *= a; } return r; } inline ll POW(int a, ll n) { return POW((ll)a, n); } inline ll MSB(ll a) { for (ll o = 63, x = -1;;) { ll m = (o + x) / 2; if (a < (1LL << m)) o = m; else x = m; if (o - x == 1) return x; } } inline ll CEIL(ll a, ll b) { return (a + b - 1) / b; } ll Gcd(ll a, ll b) { return (b == 0) ? a : Gcd(b, a % b); } ll Lcm(ll a, ll b) { return a * b / Gcd(a, b); } inline vll Bit2Idx(ll m) { vll v; for (ll i = 0; m; m >>= 1, i++) if (m & 1) v.push_back(i); return move(v); } inline ll BitNum(ll m) { for (ll c = 0;; m >>= 1) { c += m & 1; if (!m) return c; } } inline ll Bit(ll s, ll i) { return (s >> i) & 1; } inline ll BitOn(ll s, ll i) { return s | (1LL << i); } inline ll BitOff(ll s, ll i) { return s & ~(1LL << i); } template <class T> inline ll UnderNumOf(vector<T> &v, T x) { return upper_bound(v.begin(), v.end(), x) - v.begin(); } template <class T, class Pr> inline ll UnderNumOf(vector<T> &v, T x, Pr pred) { return upper_bound(v.begin(), v.end(), x, pred) - v.begin(); } template <class T> inline ll OverNumOf(vector<T> &v, T x) { return (ll)v.size() - (lower_bound(v.begin(), v.end(), x) - v.begin()); } template <class T, class Pr> inline ll OverNumOf(vector<T> &v, T x, Pr pred) { return (ll)v.size() - (lower_bound(v.begin(), v.end(), x, pred) - v.begin()); } template <class T = ll> inline vector<T> cinv(ll N) { vector<T> v(N); for (ll i = (0); i <= ((N)-1); i++) cin >> v[i]; return move(v); } template <class T = ll, class S = ll> inline vector<pair<T, S>> cinv2(ll N) { vector<pair<T, S>> v(N); for (ll i = (0); i <= ((N)-1); i++) { cin >> v[i].first >> v[i].second; } return move(v); } template <class T = ll, class S = ll, class R = ll> inline vector<tuple<T, S, R>> cinv3(ll N) { vector<tuple<T, S, R>> v(N); for (ll i = (0); i <= ((N)-1); i++) { cin >> get<0>(v[i]) >> get<1>(v[i]) >> get<2>(v[i]); } return move(v); } template <class T = ll, class S = ll, class R = ll, class Q = ll> inline vector<tuple<T, S, R, Q>> cinv4(ll N) { vector<tuple<T, S, R, Q>> v(N); for (ll i = (0); i <= ((N)-1); i++) { cin >> get<0>(v[i]) >> get<1>(v[i]) >> get<2>(v[i]) >> get<3>(v[i]); } return move(v); } template <class T = ll> inline vector<vector<T>> cinvv(ll N, ll M) { vector<vector<T>> vv(N); for (ll i = (0); i <= ((N)-1); i++) vv[i] = cinv(M); return move(vv); } template <class T> inline void coutv(vector<T> &v, char deli = ' ') { ll N = (ll)v.size(); for (ll i = (0); i <= ((N)-1); i++) { cout << v[i] << ((i == N - 1) ? '\n' : deli); } } template <class T> void bye(T a) { cout << a << '\n'; exit(0); } template <class T> void Dump(vector<T> &v) {} template <class T> void Dump(vector<vector<T>> &v) {} template <class T> void Dump(vector<vector<vector<T>>> &v) {} template <class T> void Dump(vector<vector<vector<vector<T>>>> &v) {} template <class T> void Dump(vector<vector<vector<vector<vector<T>>>>> &v) {} template <ll MOD> struct mll_ { ll val; mll_(ll v = 0) : val(v % MOD) { if (val < 0) val += MOD; } mll_ operator-() const { return -val; } mll_ operator+(const mll_ &b) const { return val + b.val; } mll_ operator-(const mll_ &b) const { return val - b.val; } mll_ operator*(const mll_ &b) const { return val * b.val; } mll_ operator/(const mll_ &b) const { return mll_(*this) /= b; } mll_ operator+(ll b) const { return *this + mll_(b); } mll_ operator-(ll b) const { return *this - mll_(b); } mll_ operator*(ll b) const { return *this * mll_(b); } friend mll_ operator+(ll a, const mll_ &b) { return b + a; } friend mll_ operator-(ll a, const mll_ &b) { return -b + a; } friend mll_ operator*(ll a, const mll_ &b) { return b * a; } friend mll_ operator/(ll a, const mll_ &b) { return mll_(a) / b; } mll_ &operator+=(const mll_ &b) { val = (val + b.val) % MOD; return *this; } mll_ &operator-=(const mll_ &b) { val = (val + MOD - b.val) % MOD; return *this; } mll_ &operator*=(const mll_ &b) { val = (val * b.val) % MOD; return *this; } mll_ &operator/=(const mll_ &b) { ll c = b.val, d = MOD, u = 1, v = 0; while (d) { ll t = c / d; c -= t * d; swap(c, d); u -= t * v; swap(u, v); } val = val * u % MOD; if (val < 0) val += MOD; return *this; } mll_ &operator+=(ll b) { return *this += mll_(b); } mll_ &operator-=(ll b) { return *this -= mll_(b); } mll_ &operator*=(ll b) { return *this *= mll_(b); } mll_ &operator/=(ll b) { return *this /= mll_(b); } bool operator==(const mll_ &b) { return val == b.val; } bool operator!=(const mll_ &b) { return val != b.val; } bool operator==(ll b) { return *this == mll_(b); } bool operator!=(ll b) { return *this != mll_(b); } friend bool operator==(ll a, const mll_ &b) { return mll_(a) == b.val; } friend bool operator!=(ll a, const mll_ &b) { return mll_(a) != b.val; } friend ostream &operator<<(ostream &os, const mll_ &a) { return os << a.val; } friend istream &operator>>(istream &is, mll_ &a) { return is >> a.val; } static mll_ Combination(ll a, ll b) { chmin(b, a - b); if (b < 0) return mll_(0); mll_ c = 1; for (ll i = (0); i <= ((b)-1); i++) c *= a - i; for (ll i = (0); i <= ((b)-1); i++) c /= i + 1; return c; } }; using mll = mll_<1000000007LL>; using vmll = std::vector<mll>; using vvmll = std::vector<vmll>; using vvvmll = std::vector<vvmll>; using vvvvmll = std::vector<vvvmll>; using vvvvvmll = std::vector<vvvvmll>; template <class T> struct SegmentTree { using F = function<T(T, T)>; vector<T> dat; ll n; F f; T ti; SegmentTree() {} SegmentTree(vector<T> &v, F f, T ti) { Init(v, f, ti); } void Init(vector<T> &v, F f, T ti) { this->f = f; this->ti = ti; for (n = 1; n < (ll)v.size(); n *= 2) ; dat.resize(2 * n - 1, ti); for (ll i = (0); i <= (((ll)v.size()) - 1); i++) { dat[i + n - 1] = v[i]; } for (ll i = ((n - 1) - 1); i >= (0); i--) { dat[i] = f(dat[2 * i + 1], dat[2 * i + 2]); } } void Set(ll i, T x) { i += n - 1; dat[i] = x; while (i > 0) { i = (i - 1) / 2; dat[i] = f(dat[2 * i + 1], dat[2 * i + 2]); } } T Range(ll a, ll b) { return range(a, b, 0, 0, n); } T operator[](ll i) { return dat[i + n - 1]; } void Dump(ll w = 5) { for (ll i = (0); i <= ((n)-1); i++) for (ll k = i + n - 1, m = 1, p = 1; k >= 0; p = m, m *= k % 2, k = (k == 0) ? -1 : (k - 1) / 2) { if (m) cerr << ((k < n - 1) ? " ┬ " : "") << setw(w) << dat[k]; else cerr << ((p & ~m) ? " ┘ " : ""); if (k == 0) cerr << '\n'; } cerr << '\n'; } T range(ll a, ll b, ll k, ll l, ll r) { if (r <= a || b <= l) return ti; if (a <= l && r <= b) return dat[k]; return f(range(a, b, k * 2 + 1, l, (l + r) / 2), range(a, b, k * 2 + 2, (l + r) / 2, r)); } }; void solve() { ll n; cin >> n; vector<ll> P = cinv<ll>(n); vll loc(n + 1); for (ll i = (0); i <= (n - 1); i++) { loc[P[i]] = i; } vll ans; ans.push_back(0); set<ll> buf; buf.insert(loc[1]); ll x = 0; ll y = 0; auto it = buf.begin(); vll ini(n, 0); ini[loc[1]] = 1; SegmentTree<ll> sgt( ini, [](ll x, ll y) { return x + y; }, 0); ll T = 0; ll S = 0; for (ll k = (1); k <= (n - 1); k++) { ll p = loc[k + 1]; if (k % 2 == 0) { auto iit = it; iit++; if (p < *it) { ll inter = *it - p - 1; inter -= sgt.Range(p + 1, (*it)); T += inter; T -= sgt.Range(0, p); } else if (*iit < p) { ll inter = p - *iit - 1; inter -= sgt.Range((*iit) + 1, p); T += inter; T -= sgt.Range(p + 1, n); } else { } } else { if (p < *it) { ll inter = *it - p - 1; inter -= sgt.Range(p + 1, (*it)); T += inter; T -= sgt.Range(0, p); } else { ll inter = p - *it - 1; inter -= sgt.Range((*it) + 1, p); T += inter; T -= sgt.Range(p + 1, n); } } S += sgt.Range(p + 1, n); ans.push_back(T + S); if (p < *it) x++; else y++; buf.insert(p); if (x - 1 == y) { it--; x--; y++; } else if (x + 2 == y) { it++; x++; y--; } sgt.Set(p, 1); } coutv(ans); } int main() { solve(); return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int idx[N] = {}; int bit[N] = {}; long long ans[N] = {}; int n; int sum(int x) { int ret = 0; while (x) { ret += bit[x]; x -= (x & -x); } return ret; } void add(int x, int val) { while (x <= n) { bit[x] += val; x += (x & -x); } } void init() { for (int i = 1; i <= n; i++) bit[i] = (i & -i); } int main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n; init(); int x; for (int i = 1; i <= n; i++) { cin >> x; idx[x] = i; } for (int i = 2; i <= n; i++) { if (idx[i] > idx[1]) ans[i] = sum(idx[i] - 1) - sum(idx[1]); else ans[i] = sum(idx[1]) - sum(idx[i]) + i - 2; ans[i] += ans[i - 1]; add(idx[i], -1); } for (int i = 1; i <= n; i++) cout << ans[i] << " \n"[i == n]; return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxd = 2e5 + 10; int a[maxd], t[maxd]; void add(int x) { for (; x <= maxd; x += (x & -(x))) t[x]++; } int getsum(int x) { int sum = 0; for (; x; x -= (x & (-x))) sum += t[x]; return sum; } int main() { int n, k; scanf("%d", &n); int ans = 0; for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); int k = getsum(a[i]); int now = i - k - 1; ans += now; add(a[i]); printf("%d ", ans); } return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
/* ID : omarmuh1 PROG : test LANG : C++11 */ #include "bits/stdc++.h" #include "ext/pb_ds/assoc_container.hpp" #define mp make_pair #define pb push_back #define ppb pop_back #define db puts("*****") #define mid(x , y) ((x+y)>>1) #define ff first #define ss second #define all(x) x.begin(),x.end() #define ll long long #define sqr(x) ((x)*(x)) #define pii pair <int , int> #define sz(x) int(x.size()) #define tr(it , c) for(__typeof(c.begin()) it = (c.begin()); it != (c.end()); it++) #define y1 you_made_my_day using namespace std; using namespace __gnu_pbds; const int N = 2e5+7; const int MOD = 1e9+7; template<class T> bool umin(T& a, T b) { if(a > b){ a = b;return 1;}return 0;} template<class T> bool umax(T& a, T b) { if(a < b){ a = b;return 1;}return 0;} template<class T> bool umod(T& a) { while(a < 0) a += MOD; a %= MOD; return 1;} typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> omar; // freopen("file.in" , "r" , stdin); // freopen("file.out" , "w" , stdout); int T[N<<2], pos[N], n, m, p[N], arr[N], middle; set<int> setik; ll ans[N], S[N<<2]; void updT(int x, int l, int r, int v){ if(l == r){ T[v] = 1; return; } if(x <= mid(l, r)) updT(x, l, mid(l, r), v<<1); else updT(x, mid(l, r)+1, r, v<<1|1); T[v] = T[v<<1] + T[v<<1|1]; } int getT(int x, int y, int l, int r, int v){ if(y < l || r < x) return 0; if(x <= l && r <= y) return T[v]; return getT(x, y, l, mid(l, r), v<<1) + getT(x, y, mid(l, r)+1, r, v<<1|1); } void updS(int x, int val, int l, int r, int v){ if(l == r){ S[v] = val*l; return; } if(x <= mid(l, r)) updS(x, val, l, mid(l, r), v<<1); else updS(x, val, mid(l, r)+1, r, v<<1|1); S[v] = S[v<<1] + S[v<<1|1]; } ll getS(int x, int y, int l, int r, int v){ if(y < l || r < x) return 0LL; if(x <= l && r <= y) return S[v]; return getS(x, y, l, mid(l, r), v<<1) + getS(x, y, mid(l, r)+1, r, v<<1|1); } ll cnt(int x, int y){ return (1LL*(y+x)*(y-x+1))/2LL; } int main(){/* #ifndef LOCAL_DEFINE freopen("input.txt", "r", stdin); #endif*/ scanf("%d", &n); for(int i=1; i<=n; i++) scanf("%d", &arr[i]), pos[arr[i]] = i;// printf("%d ", arr[i]); puts(""); for(int i=n; i>=1; i--){ updT(arr[i], 1, n, 1); if(arr[i] > 1) p[arr[i]] = getT(1, arr[i]-1, 1, n, 1); } for(int i=1; i<=n; i++) p[i] += p[i-1]; setik.insert(pos[1]); middle = pos[1]; updS(pos[1], -1, 1, n, 1); for(int step=2; step<=n; step++){ // printf("%d %d\n", step, middle); updS(pos[step], pos[step] > middle ? 1 : -1, 1, n, 1); setik.insert(pos[step]); if((step-1)%2 == 1 && pos[step] < middle){//puts("first"); updS(middle, 1, 1, n, 1); middle = *(--setik.lower_bound(middle)); } else if((step-1)%2 == 0 && pos[step] > middle){//puts("second"); middle = *(setik.upper_bound(middle)); updS(middle, -1, 1, n, 1); } // printf("middle: %d\n", middle); int half = step / 2 + (step%2); if(step % 2 == 1){ ans[step] = getS(1, middle-1, 1, n, 1) + getS(middle+1, n, 1, n, 1); // printf("%d %d\n", getS(1, middle-1, 1, n, 1), getS(middle+1, n, 1, n, 1)); ans[step] += cnt(1, half-1) - cnt(half+1, step); // printf("%d %d\n", cnt(1, half-1), cnt(half+1, step)); } else{ ans[step] = S[1] + cnt(1, half) - cnt(half+1, step); // printf("%d\n", S[1]); // printf("%d %d\n", cnt(1, half), cnt(half+1, step)); } ans[step] += p[step]; } for(int i=1; i<=n; i++) printf("%lld ", ans[i]); puts(""); /* #ifndef LOCAL_DEFINE cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"; #endif*/ return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> const int maxn = 2e5 + 10; const int maxm = 1e5 + 10; const long long int mod = 1e9 + 7; const long long int INF = 1e18 + 100; const int inf = 0x3f3f3f3f; const double pi = acos(-1.0); const double eps = 1e-8; using namespace std; int n, m; int cas, tol, T; set<int> st; int a[maxn], p[maxn]; int sum[maxn]; void update(int x) { for (int i = x; i; i -= i & (-i)) sum[i]++; } int query(int x) { if (x == 0) return 0; int ans = 0; for (int i = x; i <= n; i += i & (-i)) ans += sum[i]; return ans; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]), p[a[i]] = i; int ans = 0, k = 0; printf("0 "); update(p[1]); st.insert(p[1]); auto it = st.begin(); for (int i = 2; i <= n; i++) { ans -= min(query(p[i] + 1), i - 1 - query(p[i] + 1)); ans += query(p[i] + 1); update(p[i]); st.insert(p[i]); if (i % 2 == 0 && p[i] < (*it)) it--; if (i % 2 == 1 && p[i] > (*it)) it++; int x = *it; if (p[i] > x) ans += p[i] - x + 1 - query(x) + query(p[i] + 1); else ans += x - p[i] + 1 - query(p[i]) + query(x + 1); printf("%d ", ans); } return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> const int maxn = 200000 + 10; class BIT { private: int n; int bit[maxn]; public: BIT(int size) { n = size; std::fill(bit, bit + size, 0); } void add(int x, int v) { while (x <= this->n) { bit[x] += v; x += ((x) & (-x)); } } int query(int x) { int ans = 0; while (x) { ans += bit[x]; x -= ((x) & (-x)); } return ans; } int find_k(int k) { int bits = 0; int n = this->n; while (n) { n >>= 1; bits++; } int mask = 0; int cnt = 0; for (int i = bits; i >= 0; i--) { mask += 1 << i; if (mask > this->n || cnt + bit[mask] >= k) mask -= (1 << i); else cnt += bit[mask]; } return mask + 1; } }; int n, pos[maxn]; struct TreeNode { int zeros, preadd, sufadd; long long presum, sufsum; }; TreeNode seg[maxn * 4]; void build_tree(int node, int L, int R) { seg[node].zeros = R - L + 1; if (L == R) return; int M = L + (R - L) / 2; build_tree(node << 1, L, M); build_tree(node << 1 | 1, M + 1, R); } void push_down(int node, int L, int R) { int lch = node << 1; int rch = node << 1 | 1; int M = L + (R - L) / 2; if (seg[node].preadd) { seg[lch].preadd += seg[node].preadd; seg[rch].preadd += seg[node].preadd; seg[lch].presum += 1LL * seg[lch].zeros * seg[node].preadd; seg[rch].presum += 1LL * seg[rch].zeros * seg[node].preadd; seg[node].preadd = 0; } if (seg[node].sufadd) { seg[lch].sufadd += seg[node].sufadd; seg[rch].sufadd += seg[node].sufadd; seg[lch].sufsum += 1LL * seg[lch].zeros * seg[node].sufadd; seg[rch].sufsum += 1LL * seg[rch].zeros * seg[node].sufadd; seg[node].sufadd = 0; } } int qL, qR; void add(int node, int L, int R, int type) { if (qL <= L && R <= qR) { if (type == 0) { seg[node].preadd += 1; seg[node].presum += seg[node].zeros; } else { seg[node].sufadd += 1; seg[node].sufsum += seg[node].zeros; } return; } push_down(node, L, R); int M = L + (R - L) / 2; if (qL <= M) add(node << 1, L, M, type); if (qR > M) add(node << 1 | 1, M + 1, R, type); int lch = node << 1; int rch = node << 1 | 1; seg[node].zeros = seg[lch].zeros + seg[rch].zeros; seg[node].presum = seg[lch].presum + seg[rch].presum; seg[node].sufsum = seg[lch].sufsum + seg[rch].sufsum; } void erase(int node, int L, int R, int p) { if (L == R) { memset(seg + node, 0, sizeof(TreeNode)); return; } int M = L + (R - L) / 2; int lch = node << 1; int rch = node << 1 | 1; push_down(node, L, R); if (p <= M) erase(lch, L, M, p); else erase(rch, M + 1, R, p); seg[node].zeros = seg[lch].zeros + seg[rch].zeros; seg[node].presum = seg[lch].presum + seg[rch].presum; seg[node].sufsum = seg[lch].sufsum + seg[rch].sufsum; } long long query(int node, int L, int R, int type) { if (qL <= L && R <= qR) { if (type == 0) return seg[node].presum; else return seg[node].sufsum; } push_down(node, L, R); long long ans = 0; int M = L + (R - L) / 2; if (qL <= M) ans += query(node << 1, L, M, type); if (qR > M) ans += query(node << 1 | 1, M + 1, R, type); return ans; } int main() { scanf("%d", &n); BIT bit(n); for (int i = 1; i <= n; i++) { int a; scanf("%d", &a); pos[a] = i; } build_tree(1, 1, n); long long inversion = 0; for (int i = 1; i <= n; i++) { const int &p = pos[i]; inversion += i - 1 - bit.query(p); bit.add(p, 1); qL = p + 1; qR = n; if (qL <= qR) add(1, 1, n, 0); qL = 1; qR = p - 1; if (qL <= qR) add(1, 1, n, 1); erase(1, 1, n, p); long long ans = 0; int median = bit.find_k((i + 1) / 2); qL = 1; qR = median - 1; if (qL <= qR) { ans += query(1, 1, n, 0); } qL = median + 1; qR = n; if (qL <= qR) { ans += query(1, 1, n, 1); } printf("%lld ", inversion + ans); } printf("\n"); return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 200010; int tr1[N], tr2[N]; int n; int c[N]; int lowbit(int x) { return x & -x; } void ud(int tr[], int x, int d) { for (int i = x; i <= n; i += lowbit(i)) { tr[i] += d; } } int qur(int tr[], int x) { int ans = 0; for (int i = x; i; i -= lowbit(i)) { ans += tr[i]; } return ans; } int get_pos(int tr[], int x) { int l = 0, r = n; while (l < r) { int mid = (l + r) >> 1; if (qur(tr, mid) >= x) r = mid; else l = mid + 1; } return l; } int main() { cin >> n; for (int i = 1; i <= n; i++) { int x; cin >> x; c[x] = i; } long long d = 0; for (int i = 1; i <= n; i++) { ud(tr1, c[i], 1); ud(tr2, c[i], c[i]); d += (i - qur(tr1, c[i])); int pos = get_pos(tr1, (i + 1) / 2); int aa = (i - 1) / 2; int bb = i / 2; long long t1 = (long long)aa * pos - (1 + aa) * aa / 2; long long t2 = (1ll + bb) * bb / 2 + bb * pos; long long q1 = qur(tr2, pos - 1); long long q2 = qur(tr2, n) - qur(tr2, pos); long long ans = q2 - t2 + t1 - q1; cout << (ans + d) << ' '; } }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 200010; int tr1[N], tr2[N]; int n; int c[N]; int lowbit(int x) { return x & -x; } void ud(int tr[], int x, int d) { for (int i = x; i <= n; i += lowbit(i)) { tr[i] += d; } } int qur(int tr[], int x) { int ans = 0; for (int i = x; i; i -= lowbit(i)) { ans += tr[i]; } return ans; } int get_pos(int tr[], int x) { int l = 0, r = n; while (l < r) { int mid = (l + r) >> 1; if (qur(tr, mid) >= x) r = mid; else l = mid + 1; } return l; } int main() { cin >> n; for (int i = 1; i <= n; i++) { int x; cin >> x; c[x] = i; } int d = 0; for (int i = 1; i <= n; i++) { ud(tr1, c[i], 1); ud(tr2, c[i], c[i]); d += (i - qur(tr1, c[i])); cout << d << endl; int pos = get_pos(tr1, (i + 1) / 2); int aa = (i - 1) / 2; int bb = i / 2; int t1 = (long long)aa * pos - (1 + aa) * aa / 2; int t2 = (1ll + bb) * bb / 2 + bb * pos; int q1 = qur(tr2, pos - 1); int q2 = qur(tr2, n) - qur(tr2, pos); long long ans = q1 - t1 + t2 - q2; } }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #define pii pair<int,int> #define pb push_back #define mp make_pair #define mt make_tuple #define DEBUG 1 #define cerr if (DEBUG) cerr #define fprintf if (DEBUG) fprintf #define local freopen("in.txt","r",stdin); #define test cerr<<"hi\n"; #define tr(x) cerr<<#x" "<<x<<'\n'; #define fi first #define se second #define HH1 402653189 #define HH2 1610612741 #define INF 0x3f3f3f3f #define tii tuple<int,int,int> #define npos string::npos using namespace std; #define MOD 1000000007LL #define MAXN 200005 int n, a[MAXN], pos[MAXN]; int T1[MAXN], T2[MAXN]; using namespace __gnu_pbds; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> OST; OST s; long long ask1(int x) { long long ret = 0; for (int p = x; p; p -= p & -p) ret += T1[p]; return ret; } void add1(int x) { for (int p = x; p < MAXN; p += p & -p) T1[p]++; } long long ask2(int x) { long long ret = 0; for (int p = x; p; p -= p & -p) ret += T2[p]; return ret; } void add2(int x) { for (int p = x; p < MAXN; p += p & -p) T2[p] += x; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; pos[a[i]] = i; } long long inv = 0; vector<long long> out; for (int i = 1; i <= n; i++) { long long ans = 0; inv += i-1-ask1(pos[i]); add1(pos[i]); s.insert(pos[i]); add2(pos[i]); long long m = *s.find_by_order(s.size() / 2); long long lx = ask2(m); long long rx = ask2(n) - lx; // cerr << "lx rx " << m << ' ' << lx << ' ' << rx << '\n'; // cerr << "sz " << (s.size() / 2 + 1) << ' ' << (s.size() - (s.size() / 2 + 1)) << '\n'; long long ls = (s.size() / 2 + 1), rs = s.size() - ls; ans += m * ls - lx - (0 + ls-1) * ls / 2 + rx - m * rs - (1 + rs) * rs / 2; ans += inv; out.pb(ans); } for (int i = 0; i < n; i++) { cout << out[i] << (i + 1 == n ? '\n' : ' '); } }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> int main() { int i,j,n,k; cin>>n; vector<int>a(n),pos(n+1); for(i=0;i<n;i++){ cin>>a[i]; pos[a[i]]=i; } ordered_set s; int l=0,r=0,ele=pos[1],ans=0; for(i=1;i<=n;i++){ if(i==1){ s.insert(pos[i]); cout<<ans<<" "; } else{ s.insert(pos[i]); int rank=s.order_of_key(pos[i]); if(ele>pos[i]) l++; else r++; if(l==r || l==r+1 || l+1==r){ if(*(s.find_by_order(0))>pos[i]){ ans+=(ele-pos[i]-l)+i-1; }else if(*(s.find_by_order(i-1))<pos[i]){ ans+=(pos[i]-pos[ele]-r); }else if(ele>pos[i]){ ans+=(ele-pos[i]-(l-rank))-rank + i-rank-1; }else{ rank=i-rank-1; ans+=(pos[i]-ele-(r-rank)); } }else{ if(l==r+2){ ele=*(s.find_by_order(s.order_of_key(ele)-1)); l--;r++; if(*(s.find_by_order(0))>pos[i]){ ans+=(ele-pos[i]-l)+i-1; }else if(*(s.find_by_order(i-1))<pos[i]){ ans+=(pos[i]-pos[ele]-r); }else if(ele>pos[i]){ ans+=(ele-pos[i]-(l-rank))-rank + i-rank-1; }else{ rank=i-rank-1; ans+=(pos[i]-ele-(r-rank)); } }else{ ele=*(s.find_by_order(s.order_of_key(ele)+1)); l++;r--; if(ele==a[i])ans=ans-l; else{ if(*(s.find_by_order(0))>pos[i]){ ans+=(ele-pos[i]-l)+i-1; }else if(*(s.find_by_order(i-1))<pos[i]){ ans+=(pos[i]-pos[ele]-r); }else if(ele>pos[i]){ ans+=(ele-pos[i]-(l-rank))-rank + i-rank-1; }else{ rank=i-rank-1; ans+=(pos[i]-ele-(r-rank)); } } } } cout<<ans<<" "; } } cout<<endl; return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> const int maxn = 200000 + 10; class BIT { private: int n; int bit[maxn]; public: BIT(int size) { n = size; std::fill(bit, bit + size, 0); } void add(int x, int v) { while (x <= this->n) { bit[x] += v; x += ((x) & (-x)); } } int query(int x) { int ans = 0; while (x) { ans += bit[x]; x -= ((x) & (-x)); } return ans; } int find_k(int k) { int bits = 0; int n = this->n; while (n) { n >>= 1; bits++; } int mask = 0; int cnt = 0; for (int i = bits - 1; i >= 0; i--) { mask += 1 << i; if (mask > this->n || cnt + bit[mask] >= k) mask -= (1 << i); else cnt += bit[mask]; } return mask + 1; } }; int n, pos[maxn]; struct TreeNode { int zeros, preadd, sufadd; long long presum, sufsum; }; TreeNode seg[1111111]; void build_tree(int node, int L, int R) { seg[node].zeros = R - L + 1; if (L == R) return; int M = L + (R - L) / 2; build_tree(node << 1, L, M); build_tree(node << 1 | 1, M + 1, R); } void push_down(int node, int L, int R) { int lch = node << 1; int rch = node << 1 | 1; int M = L + (R - L) / 2; if (seg[node].preadd) { seg[lch].preadd += seg[node].preadd; seg[rch].preadd += seg[node].preadd; seg[lch].presum += 1LL * seg[lch].zeros * seg[node].preadd; seg[rch].presum += 1LL * seg[rch].zeros * seg[node].preadd; seg[node].preadd = 0; } if (seg[node].sufadd) { seg[lch].sufadd += seg[node].sufadd; seg[rch].sufadd += seg[node].sufadd; seg[lch].sufsum += 1LL * seg[lch].zeros * seg[node].sufadd; seg[rch].sufsum += 1LL * seg[rch].zeros * seg[node].sufadd; seg[node].sufadd = 0; } } int qL, qR; void add(int node, int L, int R, int type) { if (qL <= L && R <= qR) { if (type == 0) { seg[node].preadd += 1; seg[node].presum += seg[node].zeros; } else { seg[node].sufadd += 1; seg[node].sufsum += seg[node].zeros; } return; } push_down(node, L, R); int M = L + (R - L) / 2; if (qL <= M) add(node << 1, L, M, type); if (qR > M) add(node << 1 | 1, M + 1, R, type); int lch = node << 1; int rch = node << 1 | 1; seg[node].zeros = seg[lch].zeros + seg[rch].zeros; seg[node].presum = seg[lch].presum + seg[rch].presum; seg[node].sufsum = seg[lch].sufsum + seg[rch].sufsum; } void erase(int node, int L, int R, int p) { if (L == R) { memset(seg + node, 0, sizeof(TreeNode)); return; } int M = L + (R - L) / 2; int lch = node << 1; int rch = node << 1 | 1; push_down(node, L, R); if (p <= M) erase(lch, L, M, p); else erase(rch, M + 1, R, p); seg[node].zeros = seg[lch].zeros + seg[rch].zeros; seg[node].presum = seg[lch].presum + seg[rch].presum; seg[node].sufsum = seg[lch].sufsum + seg[rch].sufsum; } long long query(int node, int L, int R, int type) { if (qL <= L && R <= qR) { if (type == 0) return seg[node].presum; else return seg[node].sufsum; } push_down(node, L, R); long long ans = 0; int M = L + (R - L) / 2; if (qL <= M) ans += query(node << 1, L, M, type); if (qR > M) ans += query(node << 1 | 1, M + 1, R, type); return ans; } int main() { scanf("%d", &n); BIT bit(n); for (int i = 1; i <= n; i++) { int a; scanf("%d", &a); pos[a] = i; } build_tree(1, 1, n); long long inversion = 0; for (int i = 1; i <= n; i++) { const int &p = pos[i]; inversion += i - 1 - bit.query(p); bit.add(p, 1); qL = p + 1; qR = n; if (qL <= qR) add(1, 1, n, 0); qL = 1; qR = p - 1; if (qL <= qR) add(1, 1, n, 1); erase(1, 1, n, p); long long ans = 0; int median = bit.find_k((i + 1) / 2); qL = 1; qR = median - 1; if (qL <= qR) { ans += query(1, 1, n, 0); } qL = median + 1; qR = n; if (qL <= qR) { ans += query(1, 1, n, 1); } printf("%lld ", inversion + ans); } printf("\n"); return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#pragma GCC optimize(2) #include <cstdio> #include <queue> #include <string> #include <cstring> #include <algorithm> #include <cstdlib> #include <iostream> #include <iomanip> #include <cmath> #include <vector> #include <set> #include <map> #include <fstream> #include <cassert> #include <complex> #include <ctime> #include <tr1/unordered_map> using namespace std; using namespace tr1; typedef long long ll; const int maxn = 2e5 + 10; int n; int aa[maxn]; int inv[maxn]; int sum[maxn * 4]; ll in[maxn]; void build(int rt,int l,int r) { if(l == r) { sum[rt] = 1; return ; } int mid = (l + r) / 2; build(rt * 2,l,mid); build(rt * 2 + 1,mid + 1,r); sum[rt] = sum[rt * 2] + sum[rt * 2 + 1]; } void update(int rt,int x,int al,int ar,int k) { if(x < al || x > ar) return ; if(al == ar) { sum[rt] += k; return ; } int mid = (al + ar) / 2; update(rt * 2,x,al,mid,k); update(rt * 2 + 1,x,mid + 1,ar,k); sum[rt] = sum[rt * 2] + sum[rt * 2 + 1]; } int query(int rt,int l,int r,int al,int ar) { if(al > r || ar < l) return 0; if(al >= l && ar <= r) return sum[rt]; int mid = (al + ar) / 2; return query(rt * 2,l,r,al,mid) + query(rt * 2 + 1,l,r,mid + 1,ar); } int query_rank(int rt,int al,int ar,int rk) { if(al == ar) return al; int mid = (al + ar) / 2; if(sum[rt * 2] >= rk) return query_rank(rt * 2,al,mid,rk); return query_rank(rt * 2 + 1,mid + 1,ar,rk - sum[rt * 2]); } int main() { scanf("%d",&n); for(int i = 1; i <= n; i++) scanf("%d",&aa[i]); for(int i = 1; i <= n; i++) inv[aa[i]] = i; in[0] = 0; for(int i = 1; i <= n; i++) { in[i] = in[i - 1] + query(1,inv[i],n,1,n); update(1,inv[i],1,n,1); } build(1,1,n); ll ans = 0; for(int i = n; i >= 1; i--) { in[i] += ans; update(1,inv[i],1,n,-1); int tmp = query_rank(1,1,n,(i - 1) / 2 + 1); //printf("temp%d\n",tmp); int temp; int u = query(1,1,inv[i],1,n); int v = query(1,inv[i],n,1,n); temp = min(u,v); ans += temp; if(inv[i] > tmp) ans -= (ll)(inv[i] - tmp - query(1,tmp,inv[i],1,n)); else ans -= (ll)(tmp - inv[i] - query(1,inv[i],tmp,1,n)); } for(int i = 1; i <= n; i++) { if(i != 1) printf(" "); printf("%lld",in[i]); } }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#pragma GCC optimize("Ofast") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; #define uid uniform_int_distribution mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); #define bpop(x) __builtin_popcountll((ulli)x) #define blead(x) __builtin_clzll((ulli)x) #define btrail(x) __builtin_ctzll((ulli)x) const double PI = acos(-1); template<class my> using ind_set = tree<my,null_type,less<my>,rb_tree_tag,tree_order_statistics_node_update>; #define ff first #define ss second #define pb push_back #define pf push_front #define ppb pop_back() #define ppf pop_front() #define all(vec) vec.begin(), vec.end() #define fol(i,a,b) for(int i=a;i<b;i++) #define loop(i,a,b) for(int i=a;i>=b;i--) #define forr(x,arr) for(auto& x:arr) #define mod 1000000007 #define INF 0x3f3f3f3f3f3f3f3f #define EPS 1e-7 #define sz(x) (int)(x).size() using lli = long long; using ulli = unsigned long long int; using pll = pair<lli, lli>; using ttt = pair<lli, pll>; using vttt = vector<ttt>; using vll = vector<pll>; using vl = vector<lli>; using vi = vector<int>; using vvi = vector<vector<int>>; using cd = complex<long double>; #ifdef tejasp template<typename T> void __p(T a) { cout << a << " "; } template<typename T, typename F> void __p(pair<T, F> a) { cout << "{ "; __p(a.ff); __p(a.ss); cout << "} "; } template<typename Arg1> void __f(const char *name, Arg1 &&arg1) { cout<<name<<" : ";__p(arg1); cout<<endl; } template<typename Arg1> void __t(const char *name, Arg1 &&arg1) { cout<<name<<" : { "; for (auto p : arg1) __p(p); cout<<"}"<<endl; } template<typename Arg1, typename ... Args> void __f(const char *names, Arg1 &&arg1, Args &&... args) { int bracket=0,i=0; for(; ;i++) if(names[i]==','&&bracket==0) break; else if(names[i]=='(') bracket++; else if(names[i]==')') bracket--; cout.write(names,i)<<" : "; __p(arg1); cout<<"| "; __f(names+i+1,args...); } template<typename Arg1, typename Arg2> void __f(const char *names, Arg1 arg1[], Arg2 &&arg2){ int i=0; for(; ;i++) if(names[i]==',') break; cout.write(names,i)<<" : { "; fol(i,0,arg2) __p(arg1[i]); cout << "} "<<endl; } #define trace(...) { cout<<"Line:"<<__LINE__<<" | "; __f(#__VA_ARGS__, __VA_ARGS__); } #define cotra(...) { cout<<"Line:"<<__LINE__<<" | "; __t(#__VA_ARGS__, __VA_ARGS__); } #else #define endl '\n' #define trace(...) #define cotra(...) #endif void tejas_919(int kkkk) { lli n, m, k, q, u, v, temp=0; cin>>n; int arr[n+5], ans[n+5]={}, loc[n+5]={}; fol(i,0,n) { cin>>u; loc[u]=i+1; } { ind_set<int> se; se.insert(loc[1]); fol(i,2,n+1) { if(i%2) { se.insert(loc[i]); temp=i/2; temp=*se.find_by_order(temp); trace(i,loc[i],temp) ans[i]=ans[i-1]+abs(temp-loc[i]); } else { temp=i/2; temp--; temp=*se.find_by_order(temp); trace(i,loc[i],temp) ans[i]=ans[i-1]+abs(temp-loc[i]); se.insert(loc[i]); } cotra(se) temp=se.order_of_key(loc[i])+1; ans[i]+=(i-temp); } fol(i,1,n+5) { temp=i/2; temp*=temp+1; if(i%2==0) temp-=i/2; ans[i]-=temp; } } fol(i,1,n+1) cout<<ans[i]<<" "; cout<<endl; } int main() { #ifndef tejasp ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); #endif cout << fixed << setprecision(10); int t=1; //cin>>t; fol(i,0,t) { tejas_919(i+1); } }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
python3
n = int(input()) a = [0] + list(map(int, input().split())) pos, bit = [[0] * (n+1), [0] * (n+1)] def add(i): while i <= n: bit[i] += 1 i += i & -i def sum(i): res = 0 while i > 0: res += bit[i] i -= i & -i return res for i in range(1, n+1): pos[a[i]] = i Max, Min, invSum = [0, n, 0] for i in range(1, n+1): Max = max(Max, pos[i]) Min = min(Min, pos[i]) invSum += i - sum(pos[i]) - 1 print(Max - Min - i + invSum + 1, end=' ') add(pos[i])
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; #define endl '\n' #define fast_io \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); typedef long long int ll; #define forn(i, n) for (int i = 0; i < int(n); i++) ll modExp(int a, int k, ll MOD) { ll r = 1, t = a; while (k) { if (k & 1) r = r * t % MOD; t = t * t % MOD; k >>= 1; } return r; } ll modInverse(ll a, ll m) { ll m0 = m; ll y = 0, x = 1; if (m == 1) return 0; while (a > 1) { ll q = a / m; ll t = m; m = a % m, a = t; t = y; y = x - q * y; x = t; } if (x < 0) x += m0; return x; } //use to find kth smallest element in int set, insert with ob.insert(int), find rank with *ob.find_by_order(k-1) typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; void print(int ar[], int n) { for (int i = 0; i < n;i++) { cout << ar[i]; } } int main() { fast_io int n; cin >> n; int ar[n + 1]; int rev[n + 1]; ar[0] = 0, rev[0] = 0; for (int i = 1; i <= n;i++) { cin >> ar[i]; rev[ar[i]] = i; } auto centre=rev[1]; ll num1 = 0; ll num2 = 0; ordered_set work; work.insert(rev[1]); cout << num1 + num2 << " "; for (int i = 2; i <= n; i++) { if(work.size()%2==1) { work.insert(rev[i]); num1 += (ll)(abs(rev[i] - centre) - 1 - work.size() / 2 +1); num2 += (ll)(work.size() - (work.order_of_key(rev[i]) + 1)); cout << num1 + num2 << " "; } else { work.insert(rev[i]); int posc = work.order_of_key(centre) + 1; int posi = work.order_of_key(rev[i])+1; if (2*posc-1 != work.size()) { auto iter = work.find(centre); if (posi < posc) { centre = *(--iter); } else { centre = *(++iter); } } num1 += (ll)(abs(rev[i] - centre) - 1 - (work.size() - 1) / 2 + 1); num2 += (ll)(work.size() - (posi)); cout << num1 + num2 << " "; } cout << endl; } }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
java
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.StringTokenizer; import java.io.BufferedReader; import java.util.Comparator; import java.util.Collections; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); KIntegers solver = new KIntegers(); solver.solve(1, in, out); out.close(); } static class KIntegers { public void solve(int testNumber, InputReader in, PrintWriter out) { int N = in.nextInt(); ArrayList<Integer> arr = new ArrayList<>(); int[] p = new int[N]; FenwickTree invert = new FenwickTree(N); for (int i = 0; i < N; i++) { p[i] = in.nextInt(); arr.add(i); } Collections.sort(arr, new Comparator<Integer>() { public int compare(Integer integer, Integer t1) { return Integer.compare(p[integer], p[t1]); } }); int res = 0; for (int i = 0; i < N; i++) { int nextInd = arr.get(i); res += invert.sum(nextInd, N - 1); invert.add(nextInd, 1); out.println(res); } } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } static class FenwickTree { public int[] BIT; public int size = 0; public FenwickTree(int N) { BIT = new int[N]; size = N; } public FenwickTree(int[] arr) { size = arr.length; BIT = new int[arr.length]; for (int i = 0; i < size; i++) { add(i, arr[i]); } } public void add(int id, int add) { for (int i = id; i < size; i |= i + 1) { BIT[i] += add; } } public int sum(int l, int r) { return sum(r) - sum(l - 1); } public int sum(int r) { if (r < 0 || r >= size) { return 0; } int res = 0; for (int i = r; i >= 0; i = ((i) & (i + 1)) - 1) { res += BIT[i]; } return res; } } }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#pragma GCC optimize(2) #include <cstdio> #include <queue> #include <string> #include <cstring> #include <algorithm> #include <cstdlib> #include <iostream> #include <iomanip> #include <cmath> #include <vector> #include <set> #include <map> #include <fstream> #include <cassert> #include <complex> #include <ctime> #include <tr1/unordered_map> using namespace std; using namespace tr1; typedef long long ll; const int maxn = 2e5 + 10; int n; int aa[maxn]; int inv[maxn]; int sum[maxn * 4]; ll in[maxn]; void build(int rt,int l,int r) { if(l == r) { sum[rt] = 0; return ; } int mid = (l + r) / 2; build(rt * 2,l,mid); build(rt * 2 + 1,mid + 1,r); sum[rt] = sum[rt * 2] + sum[rt * 2 + 1]; } void update(int rt,int x,int al,int ar,int k) { if(x < al || x > ar) return ; if(al == ar) { sum[rt] += k; return ; } int mid = (al + ar) / 2; update(rt * 2,x,al,mid,k); update(rt * 2 + 1,x,mid + 1,ar,k); sum[rt] = sum[rt * 2] + sum[rt * 2 + 1]; } int query(int rt,int l,int r,int al,int ar) { if(al > r || ar < l) return 0; if(al >= l && ar <= r) return sum[rt]; int mid = (al + ar) / 2; return query(rt * 2,l,r,al,mid) + query(rt * 2 + 1,l,r,mid + 1,ar); } int query_rank(int rt,int al,int ar,int rk) { if(al == ar) return al; int mid = (al + ar) / 2; if(sum[rt * 2] >= rk) return query_rank(rt * 2,al,mid,rk); return query_rank(rt * 2 + 1,mid + 1,ar,rk - sum[rt * 2]); } int main() { scanf("%d",&n); for(int i = 1; i <= n; i++) scanf("%d",&aa[i]); for(int i = 1; i <= n; i++) inv[aa[i]] = i; in[0] = 0; for(int i = 1; i <= n; i++) { in[i] = in[i - 1] + query(1,inv[i],n,1,n); update(1,inv[i],1,n,1); } build(1,1,n); update(1,inv[1],1,n,1); int tmp1,tmp2,tmp3; tmp3 = query_rank(1,1,n,1); ll ans = 0; for(int i = 2; i <= n; i++) { if(i % 2 == 0) { int temp = query(1,1,inv[i],1,n); temp = min(i - 1 - temp,temp); ans -= temp; update(1,inv[i],1,n,1); if(inv[i] > tmp3) temp = inv[i] - tmp3 + 1 - query(1,tmp3,inv[i],1,n); else temp = tmp3 - inv[i] + 1 - query(1,inv[i],tmp3,1,n); ans += temp; tmp1 = query_rank(1,1,n,i / 2); tmp2 = query_rank(1,1,n,i / 2 + 1); } else { int temp = query(1,1,inv[i],1,n); temp = min(i - 1 - temp,temp); ans -= temp; update(1,inv[i],1,n,1); if(inv[i] < tmp1) temp = tmp1 - inv[i] + 1 - query(1,inv[i],tmp1,1,n); else if(inv[i] > tmp2) temp = inv[i] - tmp2 + 1 - query(1,tmp2,inv[i],1,n); ans += temp; tmp3 = query_rank(1,1,n,i / 2 + 1); } in[i] += ans; } for(int i = 1; i <= n; i++) { if(i != 1) printf(" "); printf("%lld",in[i]); } }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MAX = 2e5 + 5; int a[MAX], p[MAX], c1[MAX], c2[MAX], n; set<int> st; void add(int *c, int x, int k) { for (; x <= n; x += x & -x) c[x] += k; } long long query(int *c, int x) { long long sum = 0; for (; x; x -= x & -x) sum += c[x]; return sum; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]), p[a[i]] = i; st.insert(p[1]); auto it = st.begin(); printf("0 "); add(c1, p[1], 1); add(c2, p[1], p[1]); long long res = 0; for (int i = 2; i <= n; i++) { st.insert(p[i]); if (p[i] < (*it) && i % 2 == 0) it--; if (p[i] > (*it) && i % 2 == 1) it++; add(c1, p[i], 1); res += (i * 1ll - query(c1, p[i])); add(c2, p[i], p[i]); int midpos = *it; long long sum = 0, k = i / 2; sum += i & 1 ? k * 1ll * (midpos - 1 + midpos - k) / 2 : (k - 1) * 1ll * (midpos + midpos - k) / 2; sum -= k * 1ll * (midpos + 1 + midpos + k) / 2; sum -= query(c2, midpos - 1); sum += query(c2, n) - query(c2, midpos); printf("%I64d ", res + sum); } }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 3e5 + 5; const int INF = 0x3f3f3f3f; const int mod = 1e9 + 7; const double Pi = acos(-1); long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } double dpow(double a, long long b) { double ans = 1.0; while (b) { if (b % 2) ans = ans * a; a = a * a; b /= 2; } return ans; } long long quick_pow(long long x, long long y) { long long ans = 1; while (y) { if (y & 1) { ans = ans * x % mod; } x = x * x % mod; y >>= 1; } return ans; } int n; int a[maxn], c[maxn]; int lowbit(int x) { return x & (-x); } int bit1[maxn], bit2[maxn]; void add(int *bit, int pos, int val) { while (pos < maxn) { bit[pos] += val; pos += lowbit(pos); } } long long query(int *bit, int pos) { long long ans = 0; while (pos) { ans += bit[pos]; pos -= lowbit(pos); } return ans; } int bs(int *bit, int val) { int i = 0; for (int j = 19; j >= 0; j--) { if ((i | 1 << j) < maxn) { if (bit[i | 1 << j] <= val) { val -= bit[i |= 1 << j]; } } } return i; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); c[a[i]] = i; } long long cnt = 0; for (int i = 1; i <= n; i++) { int p = c[i]; add(bit1, p, 1); cnt += i - query(bit1, p); add(bit2, p, p); int pos = bs(bit1, i / 2) + 1; long long sum = 0; long long aa = i / 2, bb = i - i / 2 - 1; sum += (long long)aa * pos - (long long)aa * (aa + 1) / 2 - query(bit2, pos - 1); sum += (query(bit2, maxn) - query(bit2, pos)) - (long long)bb * pos - (long long)bb * (bb + 1) / 2; printf("%lld\n", sum + cnt); } return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long maxn = 1e6 + 10; const long long mod = 1e9 + 7; const long long N = 1e6 + 10; long long n, m; long long fen[maxn]; void add(long long pos) { pos--; for (; pos < n; pos = (pos | (pos + 1))) fen[pos]++; } long long qu(long long r) { long long sm = 0; r--; for (; r >= 0; r = (r & (r + 1)) - 1) sm += fen[r]; return sm; } long long sm(long long l, long long r) { return max(qu(r) - qu(l - 1), 0ll); } long long pos[maxn]; signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; for (long long i = 1; i <= n; i++) { long long x; cin >> x; pos[x] = i; } long long ans = 0; for (long long i = 1; i <= n; i++) { add(pos[i]); ans -= min(pos[i] - 1 - qu(pos[i] - 1), n - pos[i] - sm(pos[i] + 1, n)); long long hi = n, lo = pos[i]; while (hi - lo > 1) { long long tm = (hi + lo) >> 1; if (sm(1, tm - 1) >= sm(tm + 1, n)) hi = tm; else lo = tm; } hi--; ans += (hi - pos[i] + 1) - sm(pos[i], hi); ans += sm(pos[i] + 1, n); lo = 1, hi = pos[i] + 1; while (hi - lo > 1) { long long tm = (hi + lo) >> 1; if (sm(1, tm - 1) > sm(tm + 1, n)) hi = tm; else lo = tm; } ans += (pos[i] - hi + 1) - sm(hi, pos[i]); cout << ans << " "; } }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; class BinTree : vector<long long> { public: explicit BinTree(long long k = 0) { assign(k + 1, 0); } long long lowbit(long long k) { return k & -k; } long long sum(long long k) { return k > 0 ? sum(k - lowbit(k)) + (*this)[k] : 0; } long long last() { return size() - 1; } void add(long long k, long long w) { if (k > last()) return; (*this)[k] += w; add(k + lowbit(k), w); } }; long long n; vector<long long> ps; vector<long long> mp; vector<long long> ress; BinTree bt0, bt1; void solve() { bt0 = BinTree(n); bt1 = BinTree(n); ress = vector<long long>(n + 1, 0); long long res0 = 0; long long s = 0; for (long long p = 1; p <= n; p++) { long long res = res0; long long pi = mp[p]; s += pi; long long mi = s / p; bt0.add(pi, 1); bt1.add(pi, pi); if (ps[mi] > p) { long long cn = bt0.sum(mi - 1); long long sn = bt1.sum(mi - 1); res += cn * mi - sn - (cn - 1) * cn / 2; cn = bt0.sum(n) - bt0.sum(mi); sn = bt1.sum(n) - bt1.sum(mi); res += sn - cn * mi - cn - (cn - 1) * cn / 2; res += bt0.sum(n) - bt0.sum(pi); } else { long long cn = bt0.sum(mi - 1); long long sn = bt1.sum(mi - 1); res += cn * mi - sn - cn - (cn - 1) * cn / 2; cn = bt0.sum(n) - bt0.sum(mi); sn = bt1.sum(n) - bt1.sum(mi); res += sn - cn * mi - cn - (cn - 1) * cn / 2; res += bt0.sum(n) - bt0.sum(pi); } res0 += bt0.sum(n) - bt0.sum(pi); cout << res << " "; } cout << endl; } int main() { scanf("%d", &n); ps = vector<long long>(n + 1, 0); mp = vector<long long>(n + 1, 0); for (long long i = 1; i <= n; i++) { scanf("%d", &ps[i]); mp[ps[i]] = i; } solve(); return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> const int maxn = 2e5 + 10; const int maxm = 1e5 + 10; const long long int mod = 1e9 + 7; const long long int INF = 1e18 + 100; const int inf = 0x3f3f3f3f; const double pi = acos(-1.0); const double eps = 1e-8; using namespace std; int n, m; int cas, tol, T; set<int> st; int a[maxn], p[maxn]; long long int sum[maxn]; void update(int x) { for (int i = x; i; i -= i & (-i)) sum[i]++; } long long int query(int x) { if (x == 0) return 0; int ans = 0; for (int i = x; i <= n; i += i & (-i)) ans += sum[i]; return ans; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]), p[a[i]] = i; int ans = 0, k = 0; printf("0 "); update(p[1]); st.insert(p[1]); auto it = st.begin(); for (int i = 2; i <= n; i++) { ans -= min(query(p[i] + 1), i - 1 - query(p[i] + 1)); ans += query(p[i] + 1); update(p[i]), st.insert(p[i]); if (i % 2 == 0 && p[i] < (*it)) ans += abs((*it) - (*(--it))) - 1; if (i % 2 == 1 && p[i] > (*it)) it++; if (p[i] > (*it)) ans += p[i] - (*it) + 1 - query((*it)) + query(p[i] + 1); else ans += (*it) - p[i] + 1 - query(p[i]) + query((*it) + 1); printf("%d ", ans); } return 0; }
1269_E. K Integers
You are given a permutation p_1, p_2, …, p_n. In one move you can swap two adjacent values. You want to perform a minimum number of moves, such that in the end there will exist a subsegment 1,2,…, k, in other words in the end there should be an integer i, 1 ≤ i ≤ n-k+1 such that p_i = 1, p_{i+1} = 2, …, p_{i+k-1}=k. Let f(k) be the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation. You need to find f(1), f(2), …, f(n). Input The first line of input contains one integer n (1 ≤ n ≤ 200 000): the number of elements in the permutation. The next line of input contains n integers p_1, p_2, …, p_n: given permutation (1 ≤ p_i ≤ n). Output Print n integers, the minimum number of moves that you need to make a subsegment with values 1,2,…,k appear in the permutation, for k=1, 2, …, n. Examples Input 5 5 4 3 2 1 Output 0 1 3 6 10 Input 3 1 2 3 Output 0 0 0
{ "input": [ "3\n1 2 3\n", "5\n5 4 3 2 1\n" ], "output": [ "0 0 0\n", "0 1 3 6 10\n" ] }
{ "input": [ "1\n1\n", "100\n98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39\n", "10\n5 1 6 2 8 3 4 10 9 7\n" ], "output": [ "0\n", "0 42 52 101 101 117 146 166 166 188 194 197 249 258 294 298 345 415 445 492 522 529 540 562 569 628 628 644 684 699 765 766 768 774 791 812 828 844 863 931 996 1011 1036 1040 1105 1166 1175 1232 1237 1251 1282 1364 1377 1409 1445 1455 1461 1534 1553 1565 1572 1581 1664 1706 1715 1779 1787 1837 1841 1847 1909 1919 1973 1976 2010 2060 2063 2087 2125 2133 2192 2193 2196 2276 2305 2305 2324 2327 2352 2361 2417 2418 2467 2468 2510 2598 2599 2697 2697 2770\n", "0 1 2 3 8 9 12 12 13 13\n" ] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <ext/pb_ds/detail/standard_policies.hpp> using namespace __gnu_pbds; using namespace std; #define ll long long #define pb push_back #define ff first #define ss second #define all(v) v.begin(),v.end() #define pll pair<ll,ll> #define sqr(x) (x)*(x) #define mod 1000000007 #define maxn 200002 typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; ll n,k; ll pos[maxn],Tree[4*maxn]; void update(ll pos, ll l, ll r, ll idx, ll value){ if(l==r && l==idx){ Tree[pos]=value; return; } ll mid=(l+r)/2; if(idx>=l && idx<=mid) update(pos*2,l,mid,idx,value); if(idx>=(mid+1) && idx<=r) update(pos*2+1,mid+1,r,idx,value); Tree[pos]=Tree[pos*2]+Tree[pos*2+1]; } ll query(ll pos, ll l, ll r, ll L, ll R){ if(l>=L && r<=R) return Tree[pos]; if(r<L || l>R) return 0; ll mid=(l+r)/2; ll q1=query(pos*2,l,mid,L,R); ll q2=query(pos*2+1,mid+1,r,L,R); return q1+q2; } int main() { ios::sync_with_stdio(0); cin.tie(),cout.tie(); cin>>n; for(int i=0;i<n;i++){ ll x; cin>>x; pos[--x]=i; } ordered_set s; ll ans=0,inversion=0; for(int i=0;i<n;i++){ inversion+=(int)s.size()-s.order_of_key(pos[i]); s.insert(pos[i]); update(1,0,n-1,pos[i],pos[i]); ll mid=s.size()/2; ll val=*s.find_by_order(mid); ans+=query(1,0,n-1,val+1,n-1); ll left=s.size()-mid-1; ans-=(left*val)+(left*(left+1))/2; ans-=query(1,0,n-1,0,val-1); ll right=mid; ans+=(right*val)-(right*(right+1))/2; cout<<ans+inversion<<" "; } cout<<endl; return 0; }