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"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5;
int a[N], p[N], n;
int f[N + 1];
int read(int idx) {
int sum = 0;
idx++;
while (idx > 0) {
sum += f[idx];
idx -= (idx & -idx);
}
return sum;
}
void update(int idx, int val) {
idx++;
while (idx <= n) {
f[idx] += val;
idx += (idx & -idx);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) p[a[i] - 1] = i;
long long c = 0;
cout << 0 << " ";
for (int i = 1; i < n; i++) {
if (p[i] < p[0]) {
c += i - 1 + p[0] - p[i] - read(p[0]) + read(p[i] - 1);
update(p[i], 1);
} else {
c += max(0, p[i] - (p[0] + i - 1) - 1);
}
cout << c << " ";
}
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 = 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;
}
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;
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);
int 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 N = 2e5 + 5;
int n;
long long a[N], id[N], ans;
pair<long long, long long> G[N];
void Upd(int x, int y) {
while (x <= n) {
G[x].first++;
G[x].second += y;
x += (x & -x);
}
}
pair<long long, long long> Get(int x) {
pair<long long, long long> res;
res.first = res.second = 0;
while (x > 0) {
res.first += G[x].first;
res.second += G[x].second;
x -= (x & -x);
}
return res;
}
int main() {
scanf("%lld", &n);
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
id[a[i]] = i;
}
for (int i = 1; i <= n; i++) {
long long idx = id[i];
ans += Get(n).first - Get(idx).first;
Upd(idx, idx);
long long l = 1, r = n, mid, res = -1;
while (l <= r) {
mid = (l + r) / 2;
if (Get(mid).first >= (i + 1) / 2) {
res = mid;
r = mid - 1;
} else
l = mid + 1;
}
long long totR = idx + Get(n).first - Get(idx).first;
ans += Get(n).second - Get(idx).second -
(totR * (totR + 1) / 2 - idx * (idx + 1) / 2);
long long totL = idx - Get(idx - 1).first;
ans += (idx - 1) * idx / 2 - (totL - 1) * totL / 2 - Get(idx - 1).second;
printf("%lld ", 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;
const int INF = 1061109567;
const int MAXN = 201010;
int n;
int v[MAXN];
long long ans[MAXN];
int pos_original[MAXN];
int lazy[4 * MAXN];
int st[4 * MAXN];
int query(int node, int l, int r, int pos) {
int nxt = node << 1;
int mid = (l + r) >> 1;
if (l == r) return st[node] + lazy[node];
if (lazy[node] != 0) {
lazy[nxt] += lazy[node];
lazy[nxt + 1] += lazy[node];
lazy[node] = 0;
}
if (pos <= mid) return query(nxt, l, mid, pos);
return query(nxt + 1, mid + 1, r, pos);
}
void update(int node, int l, int r, int lu, int ru, int x) {
if (r < l) return;
int nxt = node << 1;
int mid = (l + r) >> 1;
if (r < lu) return;
if (l > ru) return;
if (lu <= l && r <= ru) {
lazy[node] += x;
return;
}
update(nxt, l, mid, lu, ru, x);
update(nxt + 1, mid + 1, r, lu, ru, x);
return;
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> v[i];
pos_original[v[i]] = i;
}
ans[1] = 0;
for (int i = 2; i <= n; i++) {
ans[i] = ans[i - 1];
long long posicao = pos_original[i] + query(1, 0, n, pos_original[i]);
long long destino =
min(n - 1, pos_original[1] + query(1, 0, n, pos_original[1]) + (i - 1));
ans[i] += abs(posicao - destino);
if (destino > posicao) {
update(1, 0, n, posicao + 1, destino - 1, -1);
} else if (destino < posicao) {
update(1, 0, n, destino + 1, posicao - 1, 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>
#define ll long long
using namespace std;
int n;
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);
}
int kth(ll a){
int ret=n,l=1,r=n,mid;
ll p;
while(l<=r){
mid=(l+r)/2;
p=query(1,1,mid);
if(p>=a) ret=min(ret,mid),r=mid-1;
else l=mid+1;
}
return ret;
}
}seg,seg2;
ll arr[200001], pos[200001], pfx[200001];
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>n;
for(int i=1;i<=n;i++){ cin>>arr[i]; pos[arr[i]]=i; }
seg.init(n); seg2.init(n);
for(int i=1;i<=n;i++){
ll ans=pfx[i]=pfx[i-1]+seg.query(1,pos[i]+1,n);
seg.update(1,pos[i],1); seg2.update(1,pos[i],pos[i]);
int mid=seg.kth((i+1)/2);
ans+=seg2.query(1,mid+1,n)-seg2.query(1,1,mid-1)-mid*(1-i%2)-((i+1)/2-1)*((i+1)/2)/2-(i-(i+1)/2)*(i-(i+1)/2+1)/2;
cout<<ans<<'\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>
#include <ext/pb_ds/assoc_container.hpp> //required
#include <ext/pb_ds/tree_policy.hpp> //required
using namespace __gnu_pbds;
#define ll long long int
#define FASTIO std::ios::sync_with_stdio(false);
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define inf 1e18
#define g(x) cout<<x<<" "<<endl;
#define gg(x,y) cout<<x<<" & "<<y<<" "<<endl
#define ggg(x,y,z) cout<<x<<" & "<<y<<" & "<<z<<" "<<endl
#define all(x) x.begin(),x.end()
#define input_from_file freopen("input.txt", "r", stdin);
#define mod 1000000007ll
#define sz 200005
using namespace std;
ll a[sz];
ll pos[sz];
ll cost[sz];
ll res[sz];
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
ordered_set se;
ll get(ll x,ll val,ll len)
{
ll ro = x + val * len;
return ro;
}
int main()
{
#ifndef ONLINE_JUDGE
input_from_file
#endif
FASTIO
cin.tie(NULL);
ll n;
cin >> n;
for(ll i=1;i<=n;i++){
cin >> a[i];
pos[a[i]] = i;
}
for(ll i=n;i>=1;i--){
se.insert(a[i]);
cost[a[i]] = se.order_of_key(a[i]);
}
for(ll i=1;i<=n;i++)cost[i] += cost[i-1];
for(ll i=1;i<=n;i++){
res[i] += pos[i] + res[i-1];
}
cout <<0<<" ";
for(ll i=2;i<=n;i++){
ll cons = i * (i + 1) / 2;
ll lo = 0;
ll hi = n-i+1;
ll k = 100;
ll result = res[i] - get(cons,lo,i);
for(ll j=1;j<=k;j++){
ll mi = (hi + lo)/2;
ll curr = get(cons,mi,i);
if(curr > res[i]){
result = min(result,curr-res[i]);
hi = mi;
}
else
{
result = min(result,res[i]-curr);
lo = mi;
}
}
// gg(result,cost[i]);
result += cost[i];
cout<<result<<" ";
}
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 * 10);
bt1 = BinTree(n * 10);
long long res0 = 0;
for (long long p = 1; p <= n; p++) {
long long res = res0;
long long pi = mp[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;
}
long long mi0 = l;
long long resf = INT_MAX;
for (long long mi = mi0 - 10; mi <= mi0 + 10; 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
|
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 + size, 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[maxn * 4];
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"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace __gnu_pbds;
using namespace std;
using ll = long long;
typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
const int ac = 2e6 + 10, bc = (1 << 20);
int A[ac];
int T[bc * 2];
void update(int v)
{
v += bc;
T[v] = 1;
while (v)
{
v /= 2;
T[v] = T[v * 2] + T[v * 2 + 1];
}
}
int query(int a, int b)
{
if (a > b) return 0;
a += bc; b += bc;
int s = T[a];
if (a != b) s += T[b];
while (a / 2 != b / 2)
{
if (a % 2 == 0) s += T[a + 1];
if (b % 2 == 1) s += T[b - 1];
a /= 2; b /= 2;
}
return s;
}
int T2[bc * 2];
void update2(int v)
{
v += bc;
T2[v] = v - bc;
while (v)
{
v /= 2;
T2[v] = T2[v * 2] + T2[v * 2 + 1];
}
}
int query2(int a, int b)
{
if (a > b) return 0;
a += bc; b += bc;
int s = T2[a];
if (a != b) s += T2[b];
while (a / 2 != b / 2)
{
if (a % 2 == 0) s += T2[a + 1];
if (b % 2 == 1) s += T2[b - 1];
a /= 2; b /= 2;
}
return s;
}
ll n;
long long sum(long long a)
{
return a * (a + 1) / 2;
}
long long dist(ordered_set& S, int x)
{
S.insert(x);
update2(x);
auto v = *S.find_by_order(S.size() / 2);
ll s = query2(v + 1, n - 1) - sum(query(v + 1, n - 1)) - query(v + 1, n - 1) * v
+ query(0, v - 1) * v - query2(0, v - 1) - sum(query(0, v - 1));
return s;
}
bool cmp(int a, int b)
{
return A[a] <= A[b];
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; i++) cin >> A[i];
vector<int> O;
for (int i = 0; i < n; i++) O.push_back(i);
sort(O.begin(), O.end(), cmp);
long long inv = 0;
ordered_set S;
for (int i = 0; i < n; i++)
{
inv += query(O[i], n - 1);
update(O[i]);
auto d = dist(S, O[i]);
cout << inv + d << ' ';
}
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;
int s[200005], sum[200005], ara[200005], p[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;
}
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");
int mn = p[1], mx = p[1];
long long tot = 0;
for (int i = 2; i <= n; i++) {
tot += s[i];
mn = min(mn, p[i]);
mx = max(mx, p[i]);
tot += (mx - mn + 1 - i);
printf(" %lld", tot);
}
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],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(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));
}
}
}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
|
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector")
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/rope>
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define tr(it,a) for(auto it:a)
#define pob pop_back
#define pf push_front
#define pof pop_front
#define umin(x,y) (x=min(x,(y)))
#define umax(x,y) (x=max(x,(y)))
#define mid ((l+r)/2)
#define lch (idx*2+1)
#define rch (idx*2+2)
//
#include<bits/stdc++.h>
#define int int_fast64_t
using namespace std;
typedef pair<int,int> pii;
#define REP(i,j,k) for(int i=(j);i<(k);++i)
#define RREP(i,j,k) for(int i=int(j)-1;i>=(k);--i)
#define ALL(a) a.begin(),a.end()
#define pb push_back
#define f first
#define s second
#define endl '\n'
// #define __debug
#ifdef __debug
#define IOS (void)0
#define pr(...) cerr<<__VA_ARGS__
#define ar(a,s,t) {REP(zy,s,t)pr(a[zy]<<' ');pr(endl);}
#else
#define IOS cin.tie(0),cout.tie(0),ios_base::sync_with_stdio(false)
#define pr(...) (void)0
#define ar(...) (void)0
#endif
//
const int maxn=2e5+9;
int n,p[maxn],t[maxn];
set<int>s1,s2;
namespace fw{
int dat[maxn];
void init(){
memset(dat,0,sizeof dat);
}
void add(int i){
for(;i<=n;i+=i&-i){
++dat[i];
}
}
int sum(int i){
int re=0;
for(;i>0;i-=i&-i){
re+=dat[i];
}
return re;
}
}
main(){
IOS;
fw::init();
cin>>n;
REP(i,0,n){
cin>>p[i];
t[--p[i]]=i;
}
int all=0,rev=0;
REP(i,0,n){
if(s1.size()){
auto it=prev(s1.end());
//all+=abs(t[i]-*it);
if(t[i]<*it)all+=(*it-s1.size())-t[i],s1.insert(t[i]);
else all+=t[i]-(*it+s2.size()+1),s2.insert(t[i]);
while(s1.size()+1<s2.size()){
it=prev(s1.end());
all+=((int)s1.size()-(int)s2.size())*(*s2.begin()-*it-1);
s1.insert(*s2.begin());
s2.erase(s2.begin());
}
while(s2.size()+1<s1.size()){
it=prev(s1.end());
all+=((int)s2.size()-(int)s1.size()+2)*(*s2.begin()-*it-1);
s2.insert(*it);
s1.erase(it);
}
}else s1.insert(t[i]);
rev+=fw::sum(n-t[i]);
//cout<<all<<"-"<<(i+1)*i/2<<"+"<<rev<<endl;
cout<<all+rev<<" ";
fw::add(n-t[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 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>
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(); }
int 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;
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
|
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>
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];
int 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], 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] = (Rmost - Lmost + 1 - i) + cnt;
}
for (int i = 1; i <= n; ++i) printf("%d%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
|
#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 s = 0;
for (long long p = 1; p <= n; p++) {
long long res = ress[p - 1];
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);
}
ress[p] = res;
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>
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]));
printf("i %d, %lld\n", i, nxt_sum);
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 -= ((*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 = 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 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);
ans += (temp - x + 1 + temp) * x / 2 - query2(temp);
x = i - x;
ans += query2(n) - query2(temp) - ((temp + 1 + temp + x) * x / 2);
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
|
#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;
int LIM=2e5+100;
v64 t1(4*LIM);
v64 t2(4*LIM);
v64 l1(4*LIM);
v64 l2(4*LIM);
void updateRange(int node, int start, int end, int l, int r, int val,v64 &lazy,v64 &tree)
{
if(lazy[node] != 0)
{
// This node needs to be updated
tree[node] += (end - start + 1) * lazy[node]; // Update it
if(start != end)
{
lazy[node*2] += lazy[node]; // Mark child as lazy
lazy[node*2+1] += lazy[node]; // Mark child as lazy
}
lazy[node] = 0; // Reset it
}
if(start > end or start > r or end < l) // Current segment is not within range [l, r]
return;
if(start >= l and end <= r)
{
// Segment is fully within range
tree[node] += (end - start + 1) * val;
if(start != end)
{
// Not leaf node
lazy[node*2] += val;
lazy[node*2+1] += val;
}
return;
}
int mid = (start + end) / 2;
updateRange(node*2, start, mid, l, r, val,lazy,tree); // Updating left child
updateRange(node*2 + 1, mid + 1, end, l, r, val,lazy,tree); // Updating right child
tree[node] = tree[node*2] + tree[node*2+1]; // Updating root with max value
}
int queryRange(int node, int start, int end, int l, int r,v64 &lazy,v64 &tree)
{
if(start > end or start > r or end < l)
return 0; // Out of range
if(lazy[node] != 0)
{
// This node needs to be updated
tree[node] += (end - start + 1) * lazy[node]; // Update it
if(start != end)
{
lazy[node*2] += lazy[node]; // Mark child as lazy
lazy[node*2+1] += lazy[node]; // Mark child as lazy
}
lazy[node] = 0; // Reset it
}
if(start >= l and end <= r) // Current segment is totally within range [l, r]
return tree[node];
int mid = (start + end) / 2;
int p1 = queryRange(node*2, start, mid, l, r,lazy,tree); // Query left child
int p2 = queryRange(node*2 + 1, mid + 1, end, l, r,lazy,tree); // Query right child
return (p1 + p2);
}
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;
v64 zero(n,0);
for (int i=1; i<=n; i++)
{
updateRange(1,1,n,pos[i]+1,n,1,l1,t1);
updateRange(1,1,n,1,pos[i]-1,1,l2,t2);
count+=i-1-tree.order_of_key(pos[i]);
tree.insert(pos[i]);
mini=min(mini,pos[i]);
maxi=max(maxi,pos[i]);
int mid=*tree.find_by_order(i/2);
ll a=(i+1)/2;
ll b=i-a;
ll total=queryRange(1,1,n,mid,maxi,l2,t2) + queryRange(1,1,n,mini,mid-1,l1,t1) - (a*(a-1))/2 - (b*(b-1))/2;
ll ans=total+count;
cout<<ans<<" ";
}
// cout<<"a"<<endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll t=1;
// int n=5;
// v64 zero(n,0);
// LazySegmentTree l(zero),r(zero);
// l.inc(0,5,1);
// // l.inc(1,4,1);
// cout<<l.query(0,5)<<" "<<n<<endl;
// 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>
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;
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;
}
}
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 += (long long)seg[node].zeros;
} else {
seg[node].sufadd += 1;
seg[node].sufsum += (long long)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>
#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
using namespace std;
using namespace atcoder;
typedef long long ll;
#define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++)
constexpr ll mod = 1000000007;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll n;
cin >> n;
vector<ll> a(n), idx(n);
rep(i, 0, n) {
cin >> a[i];
a[i]--;
idx[a[i]] = i;
}
fenwick_tree<ll> fw(n);
ll ans = 0;
rep(i, 0, n) {
ans += fw.sum(idx[i], n);
cout << ans << (i == n-1 ? "\n" : " ");
fw.add(idx[i], 1);
}
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> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace __gnu_pbds;
using namespace std;
using ll = long long;
typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
const int ac = 2e6 + 10, bc = (1 << 20);
int A[ac];
int T[bc * 2];
void update(int v)
{
v += bc;
T[v] = 1;
while (v)
{
v /= 2;
T[v] = T[v * 2] + T[v * 2 + 1];
}
}
int query(int a, int b)
{
if (a > b) return 0;
a += bc; b += bc;
ll s = T[a];
if (a != b) s += T[b];
while (a / 2 != b / 2)
{
if (a % 2 == 0) s += T[a + 1];
if (b % 2 == 1) s += T[b - 1];
a /= 2; b /= 2;
}
return s;
}
ll T2[bc * 2];
void update2(int v)
{
v += bc;
T2[v] = v - bc;
while (v)
{
v /= 2;
T2[v] = T2[v * 2] + T2[v * 2 + 1];
}
}
ll query2(int a, int b)
{
if (a > b) return 0;
a += bc; b += bc;
ll s = T2[a];
if (a != b) s += T2[b];
while (a / 2 != b / 2)
{
if (a % 2 == 0) s += T2[a + 1];
if (b % 2 == 1) s += T2[b - 1];
a /= 2; b /= 2;
}
return s;
}
ll n;
long long sum(long long a)
{
return a * (a + 1) / 2;
}
long long dist(ordered_set& S, int x)
{
S.insert(x);
update2(x);
auto v = *S.find_by_order(S.size() / 2);
ll s = query2(v + 1, n - 1) - sum(query(v + 1, n - 1)) - query(v + 1, n - 1) * v
+ query(0, v - 1) * v - query2(0, v - 1) - sum(query(0, v - 1));
return s;
}
bool cmp(int a, int b)
{
return A[a] <= A[b];
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; i++) cin >> A[i];
vector<int> O;
for (int i = 0; i < n; i++) O.push_back(i);
sort(O.begin(), O.end(), cmp);
long long inv = 0;
ordered_set S;
for (int i = 0; i < n; i++)
{
inv += query(O[i], n - 1);
update(O[i]);
auto d = dist(S, O[i]);
cout << inv + d << ' ';
}
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 = 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;
}
long long 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;
long long mid = find_mid((i + 1) / 2);
long long l = ask(0, mid - 1), r = i - l - 1;
ret += mid * l - query(1, 1, mid - 1) - l * (l + 1) / 2;
ret += -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;
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];
}
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>
#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];
int num1 = 0;
int 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 += abs(rev[i] - centre) - 1 - work.size() / 2 +1;
num2 += 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 += abs(rev[i] - centre) - 1 - (work.size() - 1) / 2 + 1;
num2 += 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
|
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 resf = INT_MAX;
for (long long mi = mi0 - 1; mi <= mi0 + 1; 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
|
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 = (l + r) >> 1;
while (l < r) {
if (query(sum1, mid) <= i / 2)
l = mid + 1;
else
r = mid;
mid = (l + r) >> 1;
}
int cntl = query(sum1, mid) - 1, cntr = i - cntl - 1;
long long ans = 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"
]
}
|
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 resf = INT_MAX;
for (long long mi = mi0 - 10; mi <= mi0 + 10; 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
|
cpp
|
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pb push_back
#define all(v) (v).begin() , (v).end()
#define popcnt(x) __builtin_popcount(x)
#define inf 0x3f3f3f3f
#define watch(x) cout << (#x) << " is " << (x) << endl
#define rand() (rand() << 15 | rand())
using namespace std;
using namespace __gnu_pbds;
template<typename T>
using ordered_set =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long ll;
#define EPS 1e-9
#define PI acos(-1.0)
const int N = 5e5 + 44;
const int MOD = 1e9 + 7;
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);
int n;
cin >> n;
vector<int> a(n) , pos(n + 1);
for(int i = 0 ; i < n ; ++i){
cin >> a[i];
pos[a[i]] = i;
}
ordered_set<int> os;
set<int> s;
ll ans = 0;
for(int i = 1 ; i <= n ; ++i){
int to = os.order_of_key(pos[i]);
// cout << pos[i] << ' ' << to << "\n";
// cout << ans << ' ';
if(to < os.size())ans += (os.size() - to);
os.insert(pos[i]);
s.insert(pos[i]);
int add = *s.rbegin() - *s.begin() + 1 - (int)s.size();
// cout << i << ' ' << add << endl;
cout << ans + add << " ";
}
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 N = 2e5 + 7;
int a[N];
struct fenwick {
long long fen[N];
fenwick() { memset(fen, 0, sizeof fen); }
void add(int x, int d) {
for (int i = x + 1; i < N; i += i & -i) {
fen[i] += d;
}
}
long long sum(int x) {
long long ans = 0;
for (int i = x; i; i -= i & -i) {
ans += fen[i];
}
return ans;
}
long long sum(int l, int r) { return sum(r) - sum(l); }
int kth(int k) {
int x = 0;
for (int i = 20; ~i; i--) {
if ((x | 1 << i) < N && fen[x | 1 << i] <= k) {
x |= 1 << i;
k -= fen[x];
}
}
return x;
}
} t1, t2;
int solve() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
int x;
scanf("%d", &x);
a[x] = i + 1;
}
long long rev = 0;
for (int i = 0; i < n; i++) {
t1.add(a[i], 1);
t2.add(a[i], a[i]);
rev += t1.sum(a[i] + 1, 2e5 + 1);
int t = i >> 1;
int m = t1.kth(t);
long long mv = 0;
++t;
mv += t2.sum(m + 1, 2e5 + 1) - 1ll * (m + 1) * (i - t) -
1ll * (i - t) * (i - t - 1) / 2;
mv += 1ll * m * t - t2.sum(m + 1) - 1ll * t * (t - 1) / 2;
printf("%lld ", mv + rev);
}
puts("");
return 0;
}
signed 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;
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) - (q2.size() - 1) * q2.size() / 2;
ans += (1ll * q1.size() * x) - sum1 - (q1.size() + 1) * q1.size() / 2;
} else {
ans = sum2 - (1ll * q2.size() * x) - (q2.size() + 1) * q2.size() / 2;
ans += (1ll * q1.size() * x) - sum1 - (q1.size() - 1) * q1.size() / 2;
}
ins(root, a[i]);
int rr = rk(root, a[i]);
las += i - rr;
ans += las;
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;
const int maxn = 2e5 + 7;
int a[maxn];
class BIT {
public:
long long a[maxn];
void add(int x, long long c) {
for (int i = x; i < maxn; i += i & -i) a[i] += c;
}
long long sum(int x) {
long long res = 0;
for (int i = x; i > 0; i -= i & -i) res += a[i];
return res;
}
} b1, b2;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, x;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> x;
a[x] = i;
}
long long inv = 0;
for (int i = 1; i <= n; i++) {
b1.add(a[i], 1);
inv += i - b1.sum(a[i]);
b2.add(a[i], a[i]);
x = i / 2 + 1;
long long res = b2.sum(n);
int pos, l = 1, r = n;
while (l < r) {
int mid = (l + r) >> 1;
if (b1.sum(mid) >= x)
r = mid;
else
l = mid + 1;
}
pos = l;
long long s = b2.sum(pos);
long long lans = pos * (pos + 1) / 2 - s - (pos - x) * (pos - x + 1) / 2;
long long rans = res - s - (i - x) * (pos + 1 + i - x + pos) / 2;
cout << lans + rans + inv << ' ';
}
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>
#define ll long long
#define pb push_back
using namespace std;
const int N = 400005;
int a[N],pos[N],fen[5][N];
void upd(int tp,int idx,int val){
while(idx < N){
fen[tp][idx] += val;
idx+=(idx&(-idx));
}
}
ll get(int tp,int idx){
ll s = 0;
while(idx){
s += fen[tp][idx];
idx -= (idx&(-idx));
}
return s;
}
int find(int l,int r,int x){
int res=0;
while (l <= r){
int mid = (l + r) >> 1;
if (get(1,mid)*2 <= x) {
res = mid;
l = mid + 1;
}
else r = mid - 1;
}
return res;
}
main (){
ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
int n;
cin>>n;
for (int i=1;i<=n;i++)
cin>>a[i],pos[a[i]] = i;
int l = n+1,r = 0;
ll inversions = 0,ans = 0;
for (int k=1;k<=n;k++){
l = min(l,pos[k]),r = max(r,pos[k]);
inversions += k - 1 - get(1,pos[k]);
upd(1,pos[k],1),upd(2,pos[k],pos[k]);
if (k == 1) {
cout<<0<<" ";
continue;
}
int id = find(l,r,k);
int c1 = get(1,id),c2 = k - c1;
ll L = c1 * id - get(2,id) - c1*(c1 - 1)/2;
ll R = get(2,n) - get(2,id) - id*c2 - c2*(c2 + 1)/2;
ans = inversions + L + R;
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;
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 resf = INT_MAX;
for (long long mi = mi0 - 1; mi <= mi0 + 1; mi++) {
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
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int MX = 2e5 + 5;
int n;
int Bit[MX];
int Idx[MX];
int Inv[MX];
int 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;
}
Add_Bit(Pos[1], 1);
Add_Idx(Pos[1], Pos[1]);
long long ans = Inv[1];
int L = Pos[1];
int R = Pos[1];
cout << Inv[1] << " ";
for (int i = 2; i <= n; ++i) {
ans += 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 += Get_Idx(n) - Get_Idx(m) - (Ri - 1) * m;
ans += Le * m - Get_Idx(m);
ans -= Ri * (Ri + 1) / 2;
ans -= Le * (Le + 1) / 2;
cout << ans << " ";
}
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;
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 - 10; mi <= mi0 + 10; 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
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200000;
int a[200005], pos[200005];
int sum1[200005], sum2[200005];
void add(int *sum1, int x, int val) {
while (x <= maxn) {
sum1[x] += val;
x += x & (-x);
}
}
int sum(int *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);
int 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 N = 1e6 + 10;
const int mod = 1e9 + 7;
int head[N];
int dis[N], ecnt;
int fa[N];
int cat[2005][2005];
long long gcd(long long a, long long b) {
return a % b == 0 ? b : gcd(b, a % b);
}
long long qpow(long long base, long long n) {
long long ans = 1;
while (n) {
if (n & 1) ans = (ans * base) % mod;
n >>= 1;
base = base * base % mod;
}
return ans;
}
struct Node {
long long x, y;
} nd[N];
long long m, n, x, k, y;
struct EDGE {
int u, v, nxt;
long long w;
} e[N];
bool cmp(EDGE a, EDGE b) { return a.w < b.w; }
void add_edge(int u, int v, long long w) {
e[ecnt].u = u;
e[ecnt].v = v;
e[ecnt].w = w;
e[ecnt].nxt = head[u];
head[u] = ecnt++;
}
int fd(int x) { return -1 == fa[x] ? x : fa[x] = fd(fa[x]); }
int c[N];
int lowbit(int x) { return x & (-x); }
void add(long long *c, int x, int y) {
while (x <= n) {
c[x] += y;
x += lowbit(x);
}
}
int getsum(long long *c, int x) {
int res = 0;
while (x > 0) {
res += c[x];
x -= lowbit(x);
}
return res;
}
int vis[N];
int p[N];
long long ans;
int C[55][55];
int dp[150][150];
long long posar[N], smar[N];
struct H {
int p, s;
friend bool operator<(H a, H b) { return a.p < b.p; }
} he[N];
bool cmp(int a, int b) { return a > b; }
priority_queue<int> q;
vector<int> v[30];
map<vector<int>, int> mp;
int ar[N];
int br[N];
vector<int> vv[10];
int main() {
{
cin >> n;
for (int i = 1; i <= n; i++) {
scanf("%d", &ar[i]);
p[ar[i]] = i;
}
long long inv = 0;
for (int i = 1; i <= n; i++) {
inv += i - 1 - getsum(smar, p[i]);
add(smar, p[i], 1);
add(posar, p[i], p[i]);
int l = 1;
int r = n;
int mid;
while (l < r) {
mid = 1 + l + r >> 1;
if (getsum(smar, mid - 1) * 2 <= i)
l = mid;
else
r = mid - 1;
}
mid = l;
long long pre_cnt_sum = getsum(smar, mid),
pre_pos_sum = getsum(posar, mid);
long long mov =
pre_cnt_sum * mid - pre_pos_sum - pre_cnt_sum * (pre_cnt_sum - 1) / 2;
long long aft_cnt_sum = i - pre_cnt_sum;
mov += getsum(posar, n) - pre_pos_sum - aft_cnt_sum * mid -
aft_cnt_sum * (aft_cnt_sum + 1) / 2;
cout << inv + mov << " \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 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);
}
} 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);
st.SetCnt0(1, 1, n, p);
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);
}
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"
]
}
|
IN-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]));
}
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]));
}
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"
]
}
|
IN-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) > 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"
]
}
|
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 = (l + r) >> 1;
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>
using namespace std;
const int maxn = 2e5 + 5;
const int oo = 1e9 + 7;
int a[maxn], c[maxn], n;
int pos[maxn];
long long ans[maxn];
void update(int x) {
for (int i = x; i <= n && i; i += i & (-i)) c[i]++;
}
long long query(int x) {
long long ans = 0;
while (x) ans = ans + 1ll * c[x], x -= x & (-x);
return ans;
}
long long que(int x) {
int l = 1, r = n, ans = oo;
while (l <= r) {
int mid = (l + r) / 2;
if (x > query(mid)) {
l = mid + 1;
} else if (x < query(mid)) {
r = mid - 1;
} else
ans = min(ans, mid), r = mid - 1;
}
return ans;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]), pos[a[i]] = i;
ans[1] = 0;
ans[2] = abs(pos[2] - pos[1]) - 1 + (pos[2] > pos[1] ? 0 : 1);
update(pos[1]);
update(pos[2]);
for (long long i = 3; i <= n; i++) {
update(pos[i]);
ans[i] = ans[i - 1] + i - query(pos[i]);
long long p = que(i / 2 + 1);
long long p1 = que(i / 2);
long long p2 = que(i / 2 + 2);
ans[i] = ans[i] + 1ll * (p - p1 - 1) * (i / 2) +
1ll * (p2 - p - 1) * (i / 2 + 1);
}
for (int i = 1; i <= n; i++) {
if (i != 1) printf(" ");
printf("%lld", ans[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 + 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;
}
}
long long query(int p, int *BIT) {
long long 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>
#define ll long long
#define pb push_back
using namespace std;
const int N = 400005;
int a[N],pos[N],fen[5][N];
void upd(int tp,int idx,int val){
while(idx < N){
fen[tp][idx] += val;
idx+=(idx&(-idx));
}
}
ll get(int tp,int idx){
ll s = 0;
while(idx){
s += fen[tp][idx];
idx -= (idx&(-idx));
}
return s;
}
ll find(int l,int r,int x){
ll res=0;
while (l <= r){
int mid = (l + r) >> 1;
if (get(1,mid)*2 <= x) {
res = mid;
l = mid + 1;
}
else r = mid - 1;
}
return res;
}
main (){
ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
int n;
cin>>n;
for (int i=1;i<=n;i++)
cin>>a[i],pos[a[i]] = i;
int l = n+1,r = 0;
ll inversions = 0,ans = 0;
for (int k=1;k<=n;k++){
l = min(l,pos[k]),r = max(r,pos[k]);
inversions += k - 1 - get(1,pos[k]);
upd(1,pos[k],1),upd(2,pos[k],pos[k]);
if (k == 1) {
cout<<0<<" ";
continue;
}
ll id = find(l,r,k);
ll c1 = get(1,id),c2 = k - c1;
ll L = c1 * id - get(2,id) - c1*(c1 - 1)/2;
ll R = get(2,n) - get(2,id) - id*c2 - c2*(c2 + 1)/2;
ans = inversions + L + R;
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;
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) - (q2.size() - 1) * q2.size() / 2;
ans += (1ll * q1.size() * x) - sum1 - (q1.size() + 1) * q1.size() / 2;
} else {
ans = sum2 - (1ll * q2.size() * x) - (q2.size() + 1) * q2.size() / 2;
ans += (1ll * q1.size() * x) - sum1 - (q1.size() - 1) * q1.size() / 2;
}
ins(root, a[i]);
int 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"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200000;
int bit[MAXN + 10];
int add(int i) {
int sum = 0;
while (i > 0) {
sum += bit[i];
i -= i & -i;
}
return sum;
}
int n;
void modify(int p) {
while (p <= n) {
bit[p]++;
p += p & -p;
}
}
int main(void) {
cin >> n;
int ans = 0;
for (int i = 0; i < n; i++) {
int num;
cin >> num;
modify(num);
ans += add(n) - add(num);
if (i) cout << " ";
cout << ans;
}
cout << endl;
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 ≤ i ≤ n.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset.
The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 10;
int n, k;
char s[maxn];
int pre[maxn << 1], sz[maxn << 1];
vector<int> op[maxn];
int find(int x) { return x == pre[x] ? x : pre[x] = find(pre[x]); }
void merge(int x, int y) {
int fx = find(x), fy = find(y);
if (fy == 0) swap(fx, fy);
if (fx != fy) pre[fy] = fx, sz[fx] += sz[fy];
}
int cal(int x) {
int y = (x <= k) ? x + k : x - k;
int fx = find(x), fy = find(y);
if (fx == 0 || fy == 0) return sz[fx + fy];
return min(sz[fx], sz[fy]);
}
int main() {
scanf("%d%d", &n, &k);
scanf("%s", s + 1);
for (int i = 1; i <= k; i++) pre[i] = i, pre[i + k] = i + k, sz[i + k] = 1;
for (int i = 1, c, x; i <= k; i++) {
scanf("%d", &c);
while (c--) {
scanf("%d", &x);
op[x].push_back(i);
}
}
int res = 0;
for (int i = 1; i <= n; i++) {
if (op[i].size() == 1) {
int x = op[i][0];
res -= cal(x);
if (s[i] == '1')
pre[find(x + k)] = 0;
else
pre[find(x)] = 0;
res += cal(x);
} else if (op[i].size() == 2) {
int x = op[i][0], y = op[i][1];
if (s[i] == '1') {
if (find(x) != find(y)) {
res -= cal(x) + cal(y);
merge(x, y), merge(x + k, y + k);
res += cal(x);
}
} else {
if (find(x + k) != find(y)) {
res -= cal(x) + cal(y);
merge(x + k, y), merge(x, y + k);
res += cal(x);
}
}
}
printf("%d\n", res);
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 ≤ i ≤ n.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset.
The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 7;
const int INF = 1e7;
char s[maxn];
int fa[maxn], siz[maxn];
int l[maxn][2];
int n, k;
int findset(int x) {
if (fa[x] == x) return x;
return fa[x] = findset(fa[x]);
}
void Union(int x, int y) {
int rx = findset(x), ry = findset(y);
if (!ry) swap(rx, ry);
if (rx) siz[rx] += siz[ry];
fa[ry] = rx;
}
int cal(int x) {
int y = x + k;
x = findset(x), y = findset(y);
return min(siz[x], siz[y]);
}
int main() {
scanf("%d%d", &n, &k);
scanf("%s", s + 1);
for (int i = 1; i <= k; i++) {
int m;
scanf("%d", &m);
for (int j = 1; j <= m; j++) {
int x;
scanf("%d", &x);
if (l[x][0] == 0)
l[x][0] = i;
else
l[x][1] = i;
}
siz[i] = 1;
fa[i] = i;
fa[i + k] = i + k;
}
siz[0] = INF;
int ans = 0;
for (int i = 1; i <= n; i++) {
int x = l[i][0], y = l[i][1];
int rx = findset(x), ry = findset(y);
int rxk = findset(x + k), ryk = findset(y + k);
if (!y) {
ans -= cal(x);
if (s[i] == '1') fa[rx] = 0;
if (s[i] == '0') fa[rxk] = 0;
ans += cal(x);
} else if (s[i] == '0' && rx != ryk) {
ans -= cal(x);
ans -= cal(y);
Union(x, y + k);
Union(y, x + k);
ans += cal(x);
} else if (s[i] == '1' && rx != ry) {
ans -= cal(x);
ans -= cal(y);
Union(x, y);
Union(x + k, y + k);
ans += cal(x);
}
printf("%d\n", ans);
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 ≤ i ≤ n.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset.
The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 6e5 + 10, inf = 0x3f3f3f3f;
vector<int> v[maxn];
int fa[maxn];
long long cnt[maxn];
int n, k;
string s;
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
void merge(int u, int v) {
u = find(u), v = find(v);
if (u == v) return;
cnt[u] += cnt[v];
fa[v] = u;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> k >> s;
for (int i = 1, c, t; i <= k; i++) {
cin >> c;
while (c--) {
cin >> t;
v[t].push_back(i);
}
}
for (int i = 1; i <= k; i++) {
fa[i] = i, fa[i + k] = i + k;
cnt[i + k] = 1;
}
int ans = 0;
for (int i = 1; i <= n; i++) {
if (v[i].size() == 0) {
printf("%d\n", ans);
continue;
} else if (v[i].size() == 1) {
int x = v[i][0];
ans -= min(cnt[find(x)], cnt[find(x + k)]);
if (s[i - 1] == '0')
cnt[find(x)] = inf;
else
cnt[find(x + k)] = inf;
ans += min(cnt[find(x)], cnt[find(x + k)]);
printf("%d\n", ans);
} else {
int x1 = v[i][0], x2 = v[i][1];
if (find(x1) == find(x2) || find(x1) == find(x2 + k)) {
printf("%d\n", ans);
continue;
}
ans -= min(cnt[find(x1)], cnt[find(x1 + k)]);
ans -= min(cnt[find(x2)], cnt[find(x2 + k)]);
if (s[i - 1] == '0') {
merge(x1, x2 + k);
merge(x1 + k, x2);
} else {
merge(x1, x2);
merge(x1 + k, x2 + k);
}
ans += min(cnt[find(x1)], cnt[find(x1 + k)]);
printf("%d\n", ans);
}
}
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 ≤ i ≤ n.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset.
The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int maxm = 3e5 + 5;
int fa[maxm << 1], cnt[maxm << 1], n, k, state[maxm][2];
int Find(int x) { return fa[x] == x ? x : fa[x] = Find(fa[x]); }
void Union(int x, int y) {
x = Find(x), y = Find(y);
if (y == 0) swap(x, y);
fa[y] = x;
if (x) cnt[x] += cnt[y];
}
int calc(int x) {
int y = x <= k ? x + k : x - k;
x = Find(x), y = Find(y);
if (x == 0 || y == 0) return cnt[x + y];
return min(cnt[x], cnt[y]);
}
void run_case() {
cin >> n >> k;
string str;
cin >> str;
for (int i = 1; i <= k; ++i) {
int num, lamp;
cin >> num;
while (num--) {
cin >> lamp;
if (!state[lamp][0])
state[lamp][0] = i;
else
state[lamp][1] = i;
}
fa[i] = i, fa[i + k] = i + k, cnt[i + k] = 1;
}
int ans = 0;
for (int i = 1; i <= n; ++i) {
if (!state[i][1]) {
int x = state[i][0];
if (x) {
ans -= calc(x);
if (str[i - 1] == '0') {
fa[Find(x)] = 0;
} else
fa[Find(x + k)] = 0;
ans += calc(x);
}
} else {
int x = state[i][0], y = state[i][1];
if (str[i - 1] == '0') {
if (Find(x + k) != Find(y)) {
ans -= calc(x), ans -= calc(y);
Union(x, y + k), Union(x + k, y);
ans += calc(x);
}
} else {
if (Find(x) != Find(y)) {
ans -= calc(x), ans -= calc(y);
Union(x, y), Union(x + k, y + k);
ans += calc(x);
}
}
}
cout << ans << "\n";
}
}
int main() {
ios::sync_with_stdio(false), cin.tie(0);
run_case();
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 ≤ i ≤ n.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset.
The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = (int)1e6 + 100;
const int mod = (int)1e9 + 7;
int n, k, fa[maxn], tot[maxn], ans;
char s[maxn];
vector<int> v[maxn];
int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
void join(int x, int y) {
x = find(x);
y = find(y);
if (x != y) fa[x] = y, tot[y] += tot[x];
}
int get(int x) { return min(tot[find(x)], tot[find(x + k)]); }
int main() {
scanf("%d%d%s", &n, &k, s + 1);
for (int i = (1); i <= (k); ++i) {
int x, c;
scanf("%d", &c);
while (c--) scanf("%d", &x), v[x].push_back(i);
}
for (int i = (1); i <= (2 * k + 1); ++i) fa[i] = i, tot[i] = (i <= k);
tot[2 * k + 1] = mod;
for (int i = (1); i <= (n); ++i) {
if (v[i].size() == 1) {
int tmp = v[i][0] + (s[i] == '0') * k;
ans -= get(v[i][0]);
join(tmp, 2 * k + 1);
ans += get(v[i][0]);
}
if (v[i].size() == 2) {
int x = v[i][0], y = v[i][1];
if (s[i] == '1' && find(x) != find(y)) {
ans -= (get(x) + get(y));
join(x, y);
join(x + k, y + k);
ans += get(x);
}
if (s[i] == '0' && find(x) != find(y + k)) {
ans -= (get(x) + get(y));
join(x, y + k);
join(x + k, y);
ans += get(x);
}
}
printf("%d\n", ans);
}
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 ≤ i ≤ n.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset.
The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
int a[300005];
std::vector<int> g[300005];
int vis[600005], count[600005];
int p[600005];
int find(int u) {
if (p[u] == u)
return u;
else
return p[u] = find(p[u]);
}
void merge(int u, int v) {
int x = find(u), y = find(v);
if (x != y) {
if (x > y) {
p[y] = x;
count[x] += count[y];
count[y] = 0;
} else {
p[x] = y;
count[y] += count[x];
count[x] = 0;
}
}
}
int main() {
int n, k;
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%1d", &a[i]);
for (int i = 1; i <= k; i++) {
int c;
scanf("%d", &c);
while (c--) {
int p;
scanf("%d", &p);
g[p].push_back(i);
}
p[i] = i;
p[i + k] = i + k;
count[i] = 0, count[i + k] = 1;
}
int ans = 0;
int True = 2 * k + 1;
p[True] = True;
count[True] = 0;
for (int i = 1; i <= n; i++) {
if ((int)g[i].size() != 0) {
if (g[i].size() == 1) {
int u = g[i][0];
int x = find(u), y = find(u + k);
if (x != True && y != True) {
if (vis[x]) vis[x] = 0, ans -= count[x];
if (vis[y]) vis[y] = 0, ans -= count[y];
ans -= count[True];
if (a[i] != 0)
merge(x, True);
else
merge(y, True);
vis[True] = 1;
ans += count[True];
}
} else {
int u = g[i][0], v = g[i][1];
int x0 = find(u), y0 = find(u + k), x1 = find(v), y1 = find(v + k);
if (vis[x0]) vis[x0] = 0, ans -= count[x0];
if (vis[y0]) vis[y0] = 0, ans -= count[y0];
if (vis[x1]) vis[x1] = 0, ans -= count[x1];
if (vis[y1]) vis[y1] = 0, ans -= count[y1];
if (a[i] != 0) {
merge(x0, x1);
merge(y0, y1);
} else {
merge(x0, y1);
merge(x1, y0);
}
int x = find(u), y = find(u + k);
if (x == True || y == True) {
vis[True] = 1;
ans += count[True];
} else {
int t = count[x] < count[y] ? x : y;
vis[t] = 1;
ans += count[t];
}
}
}
printf("%d\n", ans);
}
printf("\n");
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 ≤ i ≤ n.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset.
The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
long long sz[3000009], par[3000009];
long long res;
long long n, k;
std::vector<long long> pos[3000009];
long long getparent(long long x) {
if (par[x] == x) return x;
return par[x] = getparent(par[x]);
}
void merge(long long x, long long y) {
long long p1 = getparent(x);
long long p2 = getparent(y);
if (p1 == p2) return;
par[p1] = p2;
res -= min(sz[p1], sz[p1 ^ 1]);
res -= min(sz[p2], sz[p2 ^ 1]);
sz[p2] += sz[p1];
res += min(sz[p2], sz[p2 ^ 1]);
sz[p1] = 0;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
string x;
cin >> x;
for (long long int i = 0; i < 2 * k + 2; i++) {
par[i] = i;
sz[i] = (i) % 2;
}
for (long long int i = 0; i < k; i++) {
long long ss;
cin >> ss;
for (long long int j = 0; j < ss; j++) {
long long yy;
cin >> yy;
yy--;
pos[yy].push_back(i);
}
}
for (long long int i = 0; i < n; i++) {
if (pos[i].size() == 0) {
cout << res << '\n';
continue;
}
if (pos[i].size() == 1) {
if (x[i] == '1') {
long long pp = getparent(2 * pos[i][0] + 1);
res -= min(sz[pp], sz[pp ^ 1]);
sz[pp] = 1e7;
res += sz[pp ^ 1];
} else {
long long pp = getparent(2 * pos[i][0]);
res -= min(sz[pp], sz[pp ^ 1]);
sz[pp] = 1e7;
res += sz[pp ^ 1];
}
cout << res << '\n';
continue;
}
if (x[i] == '1') {
merge(2 * pos[i][0], 2 * pos[i][1]);
merge((2 * pos[i][0]) + 1, (2 * pos[i][1]) + 1);
} else {
merge(2 * pos[i][0], (2 * pos[i][1]) + 1);
merge((2 * pos[i][0]) + 1, 2 * pos[i][1]);
}
cout << res << '\n';
}
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 ≤ i ≤ n.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset.
The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long MAX = 3e5 + 5;
long long inf = (long long)30000;
long long mod = (long long)998244353;
long long ans = 0, vert;
long long n, k, par[MAX], sz[MAX], cnt[MAX][2], clr[MAX], pos[MAX];
vector<long long> sets[MAX];
vector<long long> b[MAX];
void x_or(long long dad) {
for (int i = 0; i < sets[dad].size(); i++) {
clr[sets[dad][i]] ^= 1;
}
cnt[dad][0] = 0;
cnt[dad][1] = 0;
for (int i = 0; i < sets[dad].size(); i++) {
long long v = sets[dad][i];
if (clr[v] == 0)
cnt[dad][0]++;
else
cnt[dad][1]++;
}
}
long long get(long long u) {
if (par[u] == u)
return u;
else {
long long v = get(par[u]);
par[u] = v;
return v;
}
}
void unite(long long a, long long b) {
if (get(a) == get(b)) return;
if (sz[get(a)] < sz[get(b)]) swap(a, b);
if (vert == 1 && clr[a] != clr[b]) x_or(get(b));
if (vert == 0 && clr[a] == clr[b]) x_or(get(b));
a = get(a);
b = get(b);
for (int i = 0; i < sets[b].size(); i++) sets[a].push_back(sets[b][i]);
sets[b].clear();
par[b] = a;
sz[a] += sz[b];
ans -= min(cnt[b][0], cnt[b][1]) + min(cnt[a][0], cnt[a][1]);
cnt[a][0] += cnt[b][0];
cnt[a][1] += cnt[b][1];
ans += min(cnt[a][0], cnt[a][1]);
}
void unite_1(long long v, long long u) {
if (vert == 1 && clr[v] != clr[u]) x_or(get(u));
if (vert == 0 && clr[v] == clr[u]) x_or(get(u));
v = get(v);
u = get(u);
par[u] = v;
for (int i = 0; i < sets[u].size(); i++) {
sets[v].push_back(sets[u][i]);
}
sets[u].clear();
ans -= min(cnt[u][1], cnt[u][0]);
ans += cnt[u][1];
cnt[v][1] += cnt[u][1];
cnt[v][0] += cnt[u][0];
}
void solve() {
cin >> n >> k;
string s;
cin >> s;
for (int i = 1; i <= k; i++) {
long long c;
cin >> c;
for (int j = 0; j < c; j++) {
long long a;
cin >> a;
b[a].push_back(i);
}
}
for (int i = 1; i <= k; i++) {
sz[i] = 1;
clr[i] = 0;
cnt[i][0] = 1;
par[i] = i;
sets[i].push_back(i);
}
for (int i = 1; i <= n; i++) {
vert = s[i - 1] - 48;
if (b[i].size() == 1 && !pos[get(b[i][0])]) {
long long v = b[i][0];
if (clr[v] == 0 && vert == 0) x_or(get(v));
if (clr[v] == 1 && vert == 1) x_or(get(v));
ans -= min(cnt[get(v)][0], cnt[get(v)][1]);
ans += cnt[get(v)][1];
pos[get(v)] = 1;
} else if (b[i].size() == 2) {
if (pos[get(b[i][0])] || pos[get(b[i][1])]) {
if (pos[get(b[i][0])] && pos[get(b[i][1])])
;
else {
long long u, v;
v = b[i][0];
u = b[i][1];
if (pos[get(b[i][1])]) swap(u, v);
unite_1(v, u);
}
} else {
unite(b[i][0], b[i][1]);
}
}
cout << ans << "\n";
}
}
int main() {
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ios::sync_with_stdio(0);
long long q;
q = 1;
while (q--) solve();
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 ≤ i ≤ n.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset.
The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
struct perem {
long long t = 0, l = 1, p = -1, q = 0;
};
ifstream fin("AAtest.in.txt");
long long n, k, m, c, vas, cc = 1000000;
vector<long long> vv;
string s;
vector<vector<long long>> la;
vector<perem> pr;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cerr.tie(0);
cin >> n >> k >> s;
la.resize(n, vector<long long>(0));
pr.resize(k);
for (int(i) = (0); ((i)) < ((k)); ((i))++) {
cin >> m;
for (int(j) = (0); ((j)) < ((m)); ((j))++) {
cin >> c;
la[c - 1].emplace_back(i);
}
}
for (int(i) = (0); ((i)) < ((n)); ((i))++) {
vas = 0;
if (la[i].size() > 0) {
long long a = la[i][0];
bool b1 = 0;
while (a != -1) {
b1 ^= pr[a].q;
if (pr[a].p != -1)
a = pr[a].p;
else
break;
}
if (la[i].size() == 1) {
if ((b1 and s[i] == '1') or (!b1 and s[i] == '0')) {
vas += pr[a].l;
pr[a].q ^= 1;
}
pr[a].l = cc;
} else {
long long b = la[i][1];
bool b2 = 0;
while (b != -1) {
b2 ^= pr[b].q;
if (pr[b].p != -1)
b = pr[b].p;
else
break;
}
if (a != b) {
b1 ^= b2;
long long kl = 0;
if ((b1 and s[i] == '1') or (!b1 and s[i] == '0')) {
if (pr[a].l < pr[b].l) {
vas += pr[a].l;
pr[a].q ^= 1;
} else {
vas += pr[b].l;
pr[b].q ^= 1;
}
kl = abs(pr[a].l - pr[b].l);
} else
kl = min(pr[a].l + pr[b].l, cc);
if (pr[a].t < pr[b].t) swap(a, b);
if (pr[a].q) pr[b].q ^= 1;
if (pr[a].t == pr[b].t) pr[a].t++;
pr[a].l = kl;
pr[b].p = a;
}
}
}
if (i)
vv.emplace_back(vv.back() + vas);
else
vv.emplace_back(vas);
}
for (long long i : vv) cout << i << endl;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 ≤ i ≤ n.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset.
The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int N = 6e5 + 100;
vector<int> v[N];
int father[N], cnt[N], k;
int Find(int x) { return father[x] = father[x] == x ? x : Find(father[x]); }
void unite(int x, int y) {
int tx = Find(x);
int ty = Find(y);
if (tx == ty) return;
father[tx] = ty;
cnt[ty] += cnt[tx];
}
int calc(int x) { return min(cnt[Find(x)], cnt[Find(x + k)]); }
int main() {
int n, x, m;
string s;
cin >> n >> k;
cin >> s;
for (int i = 1; i <= k; i++) {
cin >> m;
while (m--) {
cin >> x;
x--;
v[x].push_back(i);
}
father[i] = i;
father[i + k] = i + k;
cnt[i] = 1;
}
father[2 * k + 1] = 2 * k + 1;
cnt[2 * k + 1] = (int)1e9;
int ans = 0;
for (int i = 0; i < n; i++) {
if ((int)v[i].size() == 1) {
int to = v[i][0] + k * (s[i] == '0');
ans -= calc(v[i][0]);
unite(to, 2 * k + 1);
ans += calc(v[i][0]);
} else if ((int)v[i].size() == 2) {
int x = v[i][0];
int y = v[i][1];
if (s[i] == '1' && Find(x) != Find(y)) {
ans -= calc(x) + calc(y);
unite(x, y);
unite(x + k, y + k);
ans += calc(x);
}
if (s[i] == '0' && Find(x) != Find(y + k)) {
ans -= calc(x) + calc(y);
unite(x, y + k);
unite(x + k, y);
ans += calc(x);
}
}
cout << ans << endl;
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 ≤ i ≤ n.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset.
The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
struct perem {
int t = 0, l = 1, p = -1, q = 0;
};
ifstream fin("AAtest.in.txt");
long long n, k, m, c, vas, cc = 10000;
vector<long long> vv;
string s;
vector<vector<long long>> la;
vector<perem> pr;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cerr.tie(0);
cin >> n >> k >> s;
la.resize(n, vector<long long>(0));
pr.resize(k);
for (int(i) = (0); ((i)) < ((k)); ((i))++) {
cin >> m;
for (int(j) = (0); ((j)) < ((m)); ((j))++) {
cin >> c;
la[c - 1].emplace_back(i);
}
}
for (int(i) = (0); ((i)) < ((n)); ((i))++) {
vas = 0;
if (la[i].size() == 2) {
long long a = la[i][0], b = la[i][1];
bool b1 = 0;
while (a != -1) {
b1 ^= pr[a].q;
if (pr[a].p != -1)
a = pr[a].p;
else
break;
}
bool b2 = 0;
while (b != -1) {
b2 ^= pr[b].q;
if (pr[b].p != -1)
b = pr[b].p;
else
break;
}
if (a != b) {
b1 ^= b2;
int kl = 0;
if ((b1 and s[i] == '1') or (!b1 and s[i] == '0')) {
if (pr[a].l < pr[b].l) {
vas += pr[a].l;
pr[a].q ^= 1;
} else {
vas += pr[b].l;
pr[b].q ^= 1;
}
kl = abs(pr[a].l - pr[b].l);
} else {
kl = pr[a].l + pr[b].l;
}
if (pr[a].t < pr[b].t) swap(a, b);
if (pr[a].q) pr[b].q ^= 1;
if (pr[a].t == pr[b].t) pr[a].t++;
pr[a].l = kl;
pr[b].p = a;
}
} else if (la[i].size() == 1) {
bool b = 0;
int a = la[i][0];
while (a != -1) {
b ^= pr[a].q;
if (pr[a].p != -1)
a = pr[a].p;
else
break;
}
if ((b and s[i] == '1') or (!b and s[i] == '0')) {
vas += pr[a].l;
pr[a].q ^= 1;
}
pr[a].l = cc;
}
if (i)
vv.emplace_back(vv.back() + vas);
else
vv.emplace_back(vas);
}
for (long long i : vv) cout << i << endl;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 ≤ i ≤ n.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset.
The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5;
const int oo = 1e7 + 5;
int du[] = {-1, 0, 0, 1};
int dv[] = {0, -1, 1, 0};
const long long mod = 1e9 + 7;
long long sqr(long long x) { return x * x; }
int getbit(int state, int i) { return ((state >> i) & 1); }
int n, m, res, dad[N + 5];
vector<int> g[N + 5];
char s[N + 5];
struct Clara {
int l, r, state;
Clara(int L = 0, int R = 0, int State = 0) {
l = L;
r = R;
state = State;
}
int getmin() { return min(l, r); }
void add(Clara tmp) {
l = min(oo, l + tmp.l);
r = min(oo, r + tmp.r);
}
} node[N + 5];
pair<int, int> getdad(int u) {
if (dad[u] < 0) return pair<int, int>(u, 0);
pair<int, int> tmp = getdad(dad[u]);
dad[u] = tmp.first;
node[u].state ^= tmp.second;
return pair<int, int>(tmp.first, node[u].state);
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> m >> (s + 1);
for (int i = 1; i <= m; i++) {
int c, k;
node[i] = Clara(1, 0, 0);
dad[i] = -1;
cin >> c;
for (int j = 1; j <= c; j++) {
cin >> k;
g[k].push_back(i);
}
}
for (int i = 1; i <= n; i++) {
int type = s[i] - '0';
if (g[i].size() == 1) {
pair<int, int> u = getdad(g[i][0]);
res -= node[u.first].getmin();
if ((!type && !u.second) || (type && u.second))
node[u.first].add(Clara(0, oo, 0));
else
node[u.first].add(Clara(oo, 0, 0));
res += node[u.first].getmin();
} else if (g[i].size() == 2) {
pair<int, int> u = getdad(g[i][0]);
pair<int, int> v = getdad(g[i][1]);
if (u.first != v.first) {
res -= node[u.first].getmin() + node[v.first].getmin();
if (dad[u.first] > dad[v.first]) swap(u, v);
if ((type && u.second != v.second) || (!type && u.second == v.second)) {
swap(node[v.first].l, node[v.first].r);
node[v.first].state = 1;
}
node[u.first].add(node[v.first]);
dad[u.first] += dad[v.first];
dad[v.first] = u.first;
res += node[u.first].getmin();
}
}
cout << res << '\n';
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 ≤ i ≤ n.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset.
The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 300000 + 10;
const int apsz = 26;
const int INF = 10000007;
const unsigned long long mod = 97;
const int maxm = 10000 + 10;
int n, k;
char s[maxn];
int st[maxn];
vector<int> epos[maxn];
int cnt[maxn << 1];
int fa[maxn << 1];
int fd(int x) { return x == fa[x] ? x : fa[x] = fd(fa[x]); }
inline int calc(int x) {
int y = x + k;
x = fd(x), y = fd(y);
if (y == 0 || x == 0) return cnt[x + y];
return min(cnt[x], cnt[y]);
}
void merge(int x, int y) {
x = fd(x), y = fd(y);
if (x == y) return;
if (y == 0) swap(x, y);
fa[y] = x;
if (x) cnt[x] += cnt[y];
}
int main(void) {
scanf("%d", &(n)), scanf("%d", &(k));
scanf("%s", s + 1);
for (int i = (1); i <= (n); ++i) st[i] = s[i] - '0';
for (int i = (1); i <= (k); ++i) {
int sz, x;
scanf("%d", &(sz));
for (; sz--;) {
scanf("%d", &(x));
epos[x].push_back(i);
}
}
for (int i = (1); i <= (k * 2); ++i) fa[i] = i;
for (int i = (1); i <= (k); ++i) cnt[i] = 1;
int ans = 0;
for (int i = (1); i <= (n); ++i) {
if (epos[i].size() == 1) {
int u = epos[i][0];
ans -= calc(u);
if (st[i] == 0)
fa[fd(u + k)] = 0;
else
fa[fd(u)] = 0;
ans += calc(u);
} else if (epos[i].size() == 2) {
int y, x;
x = epos[i][0], y = epos[i][1];
if (st[i] == 1) {
if (fd(x) != fd(y) || fd(x + k) != fd(y + k)) {
ans -= calc(x) + calc(y);
merge(x, y), merge(x + k, y + k);
ans += calc(x);
}
} else {
if (fd(x) != fd(y + k) || fd(y) != fd(x + k)) {
ans -= calc(x) + calc(y);
merge(x + k, y), merge(y + k, x);
ans += calc(x);
}
}
}
printf("%d\n", ans);
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 ≤ i ≤ n.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset.
The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 600005, INF = 1000000000;
int N, K, a[MAXN];
vector<int> V[MAXN];
char S[MAXN];
int fa[MAXN], val[MAXN], ans;
int get(int x) {
if (fa[x] != x) fa[x] = get(fa[x]);
return fa[x];
}
void merge(int x, int y) {
x = get(x), y = get(y);
if (x != y) fa[x] = y, val[y] += val[x];
}
inline int va(int x) { return min(val[get(x)], val[get(x + K)]); }
int main() {
scanf("%d%d%s", &N, &K, S + 1);
int i, j;
for (i = 1; i <= K; ++i) {
int c, x;
scanf("%d", &c);
while (c--) scanf("%d", &x), V[x].push_back(i);
}
for (i = 1; i <= K * 2; ++i) fa[i] = i, val[i] = i > K;
fa[K * 2 + 1] = K * 2 + 1, val[K * 2 + 1] = INF;
for (i = 1; i <= N; ++i) {
if (V[i].size() == 1) {
int x = V[i][0] + (S[i] == '1') * K;
ans -= va(V[i][0]), merge(x, K * 2 + 1), ans += va(V[i][0]);
} else if (V[i].size() == 2) {
int x = V[i][0], y = V[i][1];
if (S[i] == '0') {
if (get(x) != get(y + K))
ans -= va(x) + va(y), merge(x, y + K), merge(x + K, y), ans += va(x);
} else {
if (get(x) != get(y))
ans -= va(x) + va(y), merge(x, y), merge(x + K, y + K), ans += va(x);
}
}
printf("%d\n", ans);
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 ≤ i ≤ n.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset.
The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
java
|
import java.io.*;
import java.lang.Math;
import java.util.*;
public class Main {
public BufferedReader in;
public PrintStream out;
public boolean log_enabled = false;
public boolean multiply_tests = false;
public static boolean do_gen_test = false;
public void gen_test() {
}
private class TestCase {
int n, k;
int[][] adj;
int[] Vt;
int[] Vk;
int[][] Kc;
boolean[] Kf;
int[] Kv;
int[] Kp;
int[] k_adjn;
int[][] k_adjv;
int[][] k_adjs;
int[] q;
public void dfs(int u, int c, int t)
{
Vk[u] = c;
Vt[u] = t;
int v;
for (int i=0; i<k_adjn[u]; i++)
{
v = k_adjs[u][i];
if (Vk[v]==-1)
{
dfs(v, c, k_adjv[u][i]==0 ? 1-t : t);
}
}
}
public int getk(int u)
{
int s = 0;
int c = Vk[u];
while (Kp[c] != -1)
{
q[s++] = c;
c = Kp[c];
}
for (int i=0; i<s; i++)
{
Kp[q[i]] = c;
}
return c;
}
public Object solve() {
n = readInt();
k = readInt();
String s = readLn();
q = new int[k];
adj = new int[n][2];
k_adjn = new int[k];
k_adjs = new int[k][];
k_adjv = new int[k][];
Arrays.fill(k_adjn, 0);
int j,i,c,u,v;
for (i=0; i<n; i++)
{
adj[i][0] = -1;
adj[i][1] = -1;
}
int[] C = new int[k];
for (i=0; i<k; i++)
{
c = readInt();
readIntArray(C, c);
for (j=0; j<c; j++)
{
u = C[j]-1;
if (adj[u][0]==-1)
{
adj[u][0] = i;
}
else
{
adj[u][1] = i;
}
}
}
for (i=0; i<n; i++)
{
if (adj[i][1]!=-1)
{
u = adj[i][0];
v = adj[i][1];
k_adjn[u] ++;
k_adjn[v] ++;
}
}
for (i=0; i<k; i++)
{
k_adjs[i] = new int[k_adjn[i]];
k_adjv[i] = new int[k_adjn[i]];
}
Arrays.fill(k_adjn, 0);
for (i=0; i<n; i++)
{
if (adj[i][1]!=-1)
{
u = adj[i][0];
v = adj[i][1];
c = s.charAt(i) - '0';
k_adjv[u][k_adjn[u]] = c;
k_adjv[v][k_adjn[v]] = c;
k_adjs[u][k_adjn[u]] = v;
k_adjs[v][k_adjn[v]] = u;
k_adjn[u] ++;
k_adjn[v] ++;
}
}
Vt = new int[k];
Vk = new int[k];
Kc = new int[k][2];
Kf = new boolean[k];
Kp = new int[k];
Kv = new int[k];
Arrays.fill(Vk, -1);
c = 0;
for (i=0; i<n; i++)
{
if ((adj[i][1]==-1) && (adj[i][0]!=-1) && (Vk[adj[i][0]]==-1))
{
dfs(adj[i][0], c++, '1' - s.charAt(i));
}
}
for (i=0; i<k; i++)
{
if (Vk[i]==-1)
{
dfs(i, c++, 0);
}
}
Arrays.fill(Vk, -1);
Arrays.fill(Kf, false);
Arrays.fill(Kp, -1);
Arrays.fill(Kv, -1);
for (i=0; i<k; i++)
{
Arrays.fill(Kc[i], 0);
}
for (i=0; i<k; i++)
{
Vk[i] = i;
Kc[i][ Vt[i] ] = 1;
}
int S = 0;
int[] R = new int[n];
int c1, c2;
for (i=0; i<n; i++)
{
if (adj[i][1] == -1)
{
u = adj[i][0];
if (u>-1)
{
c = getk(u);
if (!Kf[c])
{
if (Kv[c]>-1)
{
S -= Kc[c][Kv[c]];
}
S += Kc[c][1];
Kf[c] = true;
Kv[c] = 1;
}
}
}
else
{
u = adj[i][0];
c1 = getk(u);
v = adj[i][1];
c2 = getk(v);
if (c1 != c2)
{
if (Kv[c1]>-1)
{
S -= Kc[c1][Kv[c1]];
}
if (Kv[c2]>-1)
{
S -= Kc[c2][Kv[c2]];
}
Kp[c2] = c1;
Kf[c1] = Kf[c1] || Kf[c2];
Kc[c1][0] = Kc[c1][0] + Kc[c2][0];
Kc[c1][1] = Kc[c1][1] + Kc[c2][1];
if (Kf[c1])
{
Kv[c1] = 1;
}
else
{
Kv[c1] = Kc[c1][0] < Kc[c1][1] ? 0 : 1;
}
S += Kc[c1][Kv[c1]];
}
}
R[i] = S;
}
printArray(R, n);
return null;
//return strf("%f", 0);
//out.printf("Case #%d: \n", caseNumber);
//return null;
}
public int caseNumber;
TestCase(int number) {
caseNumber = number;
}
public void run(){
Object r = this.solve();
if ((r != null))
{
//outputCaseNumber(r);
out.println(r);
}
}
public String impossible(){
return "IMPOSSIBLE";
}
public String strf(String format, Object... args)
{
return String.format(format, args);
}
// public void outputCaseNumber(Object r){
// //out.printf("Case #%d:", caseNumber);
// if (r != null)
// {
// // out.print(" ");
// out.print(r);
// }
// out.print("\n");
// }
}
public void run() {
//while (true)
{
int t = multiply_tests ? readInt() : 1;
for (int i = 0; i < t; i++) {
TestCase T = new TestCase(i + 1);
T.run();
}
}
}
public Main(BufferedReader _in, PrintStream _out){
in = _in;
out = _out;
}
public static void main(String args[]) {
Locale.setDefault(Locale.US);
Main S;
try {
S = new Main(
new BufferedReader(new InputStreamReader(System.in)),
System.out
);
} catch (Exception e) {
return;
}
S.run();
}
private StringTokenizer tokenizer = null;
public int readInt() {
return Integer.parseInt(readToken());
}
public long readLong() {
return Long.parseLong(readToken());
}
public double readDouble() {
return Double.parseDouble(readToken());
}
public String readLn() {
try {
String s;
while ((s = in.readLine()).length() == 0);
return s;
} catch (Exception e) {
return "";
}
}
public String readToken() {
try {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(in.readLine());
}
return tokenizer.nextToken();
} catch (Exception e) {
return "";
}
}
public int[] readIntArray(int n) {
int[] x = new int[n];
readIntArray(x, n);
return x;
}
public int[] readIntArrayBuf(int n) {
int[] x = new int[n];
readIntArrayBuf(x, n);
return x;
}
public void readIntArray(int[] x, int n) {
for (int i = 0; i < n; i++) {
x[i] = readInt();
}
}
public long[] readLongArray(int n) {
long[] x = new long[n];
readLongArray(x, n);
return x;
}
public long[] readLongArrayBuf(int n) {
long[] x = new long[n];
readLongArrayBuf(x, n);
return x;
}
public void readLongArray(long[] x, int n) {
for (int i = 0; i < n; i++) {
x[i] = readLong();
}
}
public void logWrite(String format, Object... args) {
if (!log_enabled) {
return;
}
out.printf(format, args);
}
public void readLongArrayBuf(long[] x, int n) {
char[]buf = new char[1000000];
long r = -1;
int k= 0, l = 0;
long d;
while (true)
{
try{
l = in.read(buf, 0, 1000000);
}
catch(Exception E){};
for (int i=0; i<l; i++)
{
if (('0'<=buf[i])&&(buf[i]<='9'))
{
if (r == -1)
{
r = 0;
}
d = buf[i] - '0';
r = 10 * r + d;
}
else
{
if (r != -1)
{
x[k++] = r;
}
r = -1;
}
}
if (l<1000000)
return;
}
}
public void readIntArrayBuf(int[] x, int n) {
char[]buf = new char[1000000];
int r = -1;
int k= 0, l = 0;
int d;
while (true)
{
try{
l = in.read(buf, 0, 1000000);
}
catch(Exception E){};
for (int i=0; i<l; i++)
{
if (('0'<=buf[i])&&(buf[i]<='9'))
{
if (r == -1)
{
r = 0;
}
d = buf[i] - '0';
r = 10 * r + d;
}
else
{
if (r != -1)
{
x[k++] = r;
}
r = -1;
}
}
if (l<1000000)
return;
}
}
public void printArray(long[] a, int n)
{
printArray(a, n, ' ');
}
public void printArray(int[] a, int n)
{
printArray(a, n, ' ');
}
public void printArray(long[] a, int n, char dl)
{
long x;
int i, l = 0;
for (i=0; i<n; i++)
{
x = a[i];
if (x<0)
{
x = -x;
l++;
}
if (x==0)
{
l++;
}
else
{
while (x>0)
{
x /= 10;
l++;
}
}
}
l += n-1;
char[] s = new char[l];
l--;
boolean z;
for (i=n-1; i>=0; i--)
{
x = a[i];
z = false;
if (x<0)
{
x = -x;
z = true;
}
do{
s[l--] = (char)('0' + (x % 10));
x /= 10;
} while (x>0);
if (z)
{
s[l--] = '-';
}
if (i>0)
{
s[l--] = dl;
}
}
out.println(new String(s));
}
public void printArray(int[] a, int n, char dl)
{
int x;
int i, l = 0;
for (i=0; i<n; i++)
{
x = a[i];
if (x<0)
{
x = -x;
l++;
}
if (x==0)
{
l++;
}
else
{
while (x>0)
{
x /= 10;
l++;
}
}
}
l += n-1;
char[] s = new char[l];
l--;
boolean z;
for (i=n-1; i>=0; i--)
{
x = a[i];
z = false;
if (x<0)
{
x = -x;
z = true;
}
do{
s[l--] = (char)('0' + (x % 10));
x /= 10;
} while (x>0);
if (z)
{
s[l--] = '-';
}
if (i>0)
{
s[l--] = dl;
}
}
out.println(new String(s));
}
public void printMatrix(int[][] a, int n, int m)
{
int x;
int i,j, l = 0;
for (i=0; i<n; i++)
{
for (j=0; j<m; j++)
{
x = a[i][j];
if (x<0)
{
x = -x;
l++;
}
if (x==0)
{
l++;
}
else
{
while (x>0)
{
x /= 10;
l++;
}
}
}
l += m-1;
}
l += n-1;
char[] s = new char[l];
l--;
boolean z;
for (i=n-1; i>=0; i--)
{
for (j=m-1; j>=0; j--)
{
x = a[i][j];
z = false;
if (x<0)
{
x = -x;
z = true;
}
do{
s[l--] = (char)('0' + (x % 10));
x /= 10;
} while (x>0);
if (z)
{
s[l--] = '-';
}
if (j>0)
{
s[l--] = ' ';
}
}
if (i>0)
{
s[l--] = '\n';
}
}
out.println(new String(s));
}
public void printMatrix(long[][] a, int n, int m)
{
long x;
int i,j, l = 0;
for (i=0; i<n; i++)
{
for (j=0; j<m; j++)
{
x = a[i][j];
if (x<0)
{
x = -x;
l++;
}
if (x==0)
{
l++;
}
else
{
while (x>0)
{
x /= 10;
l++;
}
}
}
l += m-1;
}
l += n-1;
char[] s = new char[l];
l--;
boolean z;
for (i=n-1; i>=0; i--)
{
for (j=m-1; j>=0; j--)
{
x = a[i][j];
z = false;
if (x<0)
{
x = -x;
z = true;
}
do{
s[l--] = (char)('0' + (x % 10));
x /= 10;
} while (x>0);
if (z)
{
s[l--] = '-';
}
if (j>0)
{
s[l--] = ' ';
}
}
if (i>0)
{
s[l--] = '\n';
}
}
out.println(new String(s));
}
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 ≤ i ≤ n.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset.
The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
vector<int> vec[300005];
int par[300005], sz[300005], bit[300005], sister[300005], ans;
int find(int node) {
if (par[node] == node) return node;
par[node] = par[par[node]];
return find(par[node]);
}
int Union(int a, int b) {
if (!b) return a;
if (sz[a] < sz[b]) swap(a, b);
par[b] = a;
sz[a] += sz[b];
bit[a] = max(bit[b], bit[a]);
return a;
}
void adjustans(int node1, int node2) {
if (!node2) {
if (bit[node1] == 1) ans -= sz[node1];
return;
}
if (bit[node1] == -1 && bit[node2] == -1)
ans -= min(sz[node1], sz[node2]);
else if (bit[node1] == 1)
ans -= sz[node1];
else if (bit[node2] == 1)
ans -= sz[node2];
}
void connect(int node1, int node2, bool flag) {
int a = find(node1);
int b = find(node2);
if (a == b || sister[a] == b) return;
adjustans(a, sister[a]);
adjustans(b, sister[b]);
int A, B;
if (!flag) {
A = Union(b, sister[a]);
B = Union(a, sister[b]);
} else {
A = Union(a, b);
if (!sister[a] && !sister[b]) {
if (bit[A] == 1) ans += sz[A];
return;
} else if (!sister[a])
B = Union(sister[b], sister[a]);
else
B = Union(sister[a], sister[b]);
}
sister[A] = B;
sister[B] = A;
if (bit[A] == -1 && bit[B] == -1)
ans += min(sz[A], sz[B]);
else if (bit[A] == 1 || !bit[B]) {
bit[A] = 1;
bit[B] = 0;
ans += sz[A];
} else if (!bit[A] || bit[B] == 1) {
bit[A] = 0;
bit[B] = 1;
ans += sz[B];
}
}
int main() {
memset(bit, -1, sizeof(bit));
int n, k, c, x;
string str;
cin >> n >> k;
cin >> str;
for (int i = 1; i <= k; ++i) {
par[i] = i;
sz[i] = 1;
cin >> c;
while (c--) {
cin >> x;
vec[x].push_back(i);
}
}
for (int i = 1; i <= n; ++i) {
if (vec[i].size() == 1) {
int val = find(vec[i][0]);
if (str[i - 1] == '0' && bit[val] == -1) {
if (!sister[val])
ans += sz[val];
else {
ans += (sz[val] - min(sz[val], sz[sister[val]]));
bit[sister[val]] = 0;
}
bit[val] = 1;
} else if (str[i - 1] == '1' && bit[val] == -1) {
bit[val] = 0;
if (sister[val]) {
bit[sister[val]] = 1;
ans += (sz[sister[val]] - min(sz[val], sz[sister[val]]));
}
}
} else if (vec[i].size() == 2) {
if (str[i - 1] == '0')
connect(vec[i][0], vec[i][1], 0);
else
connect(vec[i][0], vec[i][1], 1);
}
printf("%d\n", ans);
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 ≤ i ≤ n.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset.
The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int n, K, p[301000], tot, UF[301000], UP[301000], C[301000][2], T[301000];
char s[301000];
vector<int> g[301000], u[301000];
int Find(int a) {
int x = a, s = 0;
while (x != UF[x]) {
s ^= UP[x];
x = UF[x];
}
int t = 0, root = x;
x = a;
while (x != UF[x]) {
int y = UF[x];
int z = UP[x];
UP[x] = t ^ s;
UF[x] = root;
t ^= z;
x = y;
}
return root;
}
int Get(int a) {
if (!T[a]) return min(C[a][0], C[a][1]);
return C[a][!(T[a] - 2)];
}
void Minus(int a) { tot -= Get(a); }
void Plus(int a) { tot += Get(a); }
void f1(int a, int c) {
int x = Find(a);
int d = UP[a];
int ck = d ^ c;
Minus(x);
T[x] = ck + 2;
Plus(x);
}
void f2(int a, int b, int c) {
int r1 = Find(a), r2 = Find(b);
int d1 = UP[a], d2 = UP[b];
int d = d1 ^ d2 ^ c;
if (r1 == r2) return;
Minus(r1);
Minus(r2);
C[r1][0] += C[r2][d];
C[r1][1] += C[r2][d ^ 1];
if (!T[r1] && T[r2]) {
T[r1] = ((T[r2] - 2) ^ d) + 2;
}
UF[r2] = r1;
UP[r2] = d;
Plus(r1);
}
void Process() {
int i, j;
scanf("%d%d", &n, &K);
scanf("%s", s + 1);
for (i = 1; i <= K; i++) {
int sz;
scanf("%d", &sz);
g[i].resize(sz);
for (j = 0; j < sz; j++) {
scanf("%d", &g[i][j]);
u[g[i][j]].push_back(i);
}
}
for (i = 1; i <= K; i++) UF[i] = i, C[i][0] = 1;
for (i = 1; i <= n; i++) {
int c = '1' - s[i];
if (u[i].empty()) {
} else if (u[i].size() == 1) {
f1(u[i][0], c);
} else {
int a = u[i][0], b = u[i][1];
f2(a, b, c);
}
printf("%d\n", tot);
}
}
int main() {
int Tcase = 1;
while (Tcase--) {
Process();
}
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 ≤ i ≤ n.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset.
The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
const int inf = 1e9;
int d[maxn], fa[maxn];
int n, k;
char s[maxn];
vector<int> vec[maxn];
int get(int x) {
if (x == fa[x]) return x;
return fa[x] = get(fa[x]);
}
void merge(int x, int y) {
int fx = get(x), fy = get(y);
if (fx != fy) {
fa[fx] = fy;
d[fy] += d[fx];
}
}
int val(int x) { return min(d[get(x)], d[get(x + k)]); }
int main() {
scanf("%d%d%s", &n, &k, s + 1);
for (int i = 1; i <= k; i++) {
int t, x;
scanf("%d", &t);
while (t--) scanf("%d", &x), vec[x].push_back(i);
}
for (int i = 1; i <= 2 * k; i++) fa[i] = i, d[i] = i > k;
fa[2 * k + 1] = 2 * k + 1;
d[2 * k + 1] = inf;
int ans = 0;
for (int i = 1; i <= n; i++) {
if (vec[i].size() == 1) {
int x = vec[i][0];
ans -= val(x);
merge(x + (s[i] == '1' ? k : 0), 2 * k + 1);
ans += val(x);
} else if (vec[i].size() == 2) {
int x = vec[i][0], y = vec[i][1];
if (s[i] == '1' && get(x) != get(y)) {
ans -= val(x) + val(y);
merge(x, y), merge(x + k, y + k);
ans += val(x);
} else if (s[i] == '0' && get(x) != get(y + k)) {
ans -= val(x) + val(y);
merge(x, y + k), merge(x + k, y);
ans += val(x);
}
}
printf("%d\n", ans);
}
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 ≤ i ≤ n.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset.
The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
struct perem {
long long t = 0, l = 1, p = -1, q = 0;
};
ifstream fin("AAtest.in.txt");
long long n, k, m, c, vas, cc = 1000000;
vector<long long> vv;
string s;
vector<vector<long long>> la;
vector<perem> pr;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cerr.tie(0);
cin >> n >> k >> s;
la.resize(n, vector<long long>(0));
pr.resize(k);
for (int(i) = (0); ((i)) < ((k)); ((i))++) {
cin >> m;
for (int(j) = (0); ((j)) < ((m)); ((j))++) {
cin >> c;
la[c - 1].emplace_back(i);
}
}
for (int(i) = (0); ((i)) < ((n)); ((i))++) {
vas = 0;
if (la[i].size() > 0) {
long long a = la[i][0];
bool b1 = s[i] == '1' ? 1 : 0;
while (a != -1) {
b1 ^= pr[a].q;
if (pr[a].p != -1)
a = pr[a].p;
else
break;
}
if (la[i].size() == 1) {
if (!b1) {
vas += pr[a].l;
pr[a].q ^= 1;
}
pr[a].l = cc;
} else {
long long b = la[i][1];
while (b != -1) {
b1 ^= pr[b].q;
if (pr[b].p != -1)
b = pr[b].p;
else
break;
}
if (a != b) {
long long kl = 0;
if (!b1) {
if (pr[a].l < pr[b].l) {
vas += pr[a].l;
pr[a].q ^= 1;
} else {
vas += pr[b].l;
pr[b].q ^= 1;
}
kl = abs(pr[a].l - pr[b].l);
} else
kl = min(pr[a].l + pr[b].l, cc);
if (pr[a].t < pr[b].t) swap(a, b);
if (pr[a].q) pr[b].q ^= 1;
if (pr[a].t == pr[b].t) pr[a].t++;
pr[a].l = kl;
pr[b].p = a;
}
}
}
if (i)
vv.emplace_back(vv.back() + vas);
else
vv.emplace_back(vas);
}
for (long long i : vv) cout << i << endl;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 ≤ i ≤ n.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset.
The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5, inf = 1e9;
int n, k, s, x, op[N][2];
char ch[N];
int p[N << 1], val[N << 1];
int find(int u) { return p[u] == u ? p[u] : p[u] = find(p[u]); }
void merge(int a, int b) {
if (a == b) return;
val[b] += val[a];
p[a] = b;
}
int main() {
scanf("%d%d%s", &n, &k, ch + 1);
for (int i = 1; i <= k; i++) {
scanf("%d", &s);
while (s--) {
scanf("%d", &x);
op[x][bool(op[x][0])] = i;
}
}
for (int i = 1; i <= k; i++) val[i] = 1, p[i] = i, p[i + k] = i + k;
p[k + k + 1] = k + k + 1;
val[k + k + 1] = inf;
int now = 0;
for (int i = 1; i <= n; i++) {
if (op[i][0] == 0) goto qwq;
if (ch[i] == '1') {
if (op[i][1]) {
if (find(op[i][0]) == find(op[i][1])) goto qwq;
now -= min(val[find(op[i][0])], val[find(op[i][0] + k)]);
now -= min(val[find(op[i][1])], val[find(op[i][1] + k)]);
merge(find(op[i][0]), find(op[i][1]));
merge(find(op[i][0] + k), find(op[i][1] + k));
now += min(val[find(op[i][1])], val[find(op[i][1] + k)]);
} else {
now -= min(val[find(op[i][0])], val[find(op[i][0] + k)]);
merge(find(op[i][0]), find(k + k + 1));
now += val[find(op[i][0] + k)];
}
} else {
if (op[i][1]) {
if (find(op[i][0]) == find(op[i][1] + k)) goto qwq;
now -= min(val[find(op[i][1])], val[find(op[i][1] + k)]);
now -= min(val[find(op[i][0])], val[find(op[i][0] + k)]);
merge(find(op[i][0]), find(op[i][1] + k)),
merge(find(op[i][1]), find(op[i][0] + k));
now += min(val[find(op[i][1] + k)], val[find(op[i][0] + k)]);
} else {
now -= min(val[find(op[i][0])], val[find(op[i][0] + k)]);
merge(find(op[i][0] + k), find(k + k + 1));
now += val[find(op[i][0])];
}
}
qwq:
printf("%d\n", now);
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 ≤ i ≤ n.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset.
The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
java
|
import java.io.*;
import java.text.*;
import java.util.*;
import java.math.*;
public class template {
public static void main(String[] args) throws Exception {
new template().run();
}
public void run() throws Exception {
FastScanner f = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int n = f.nextInt(), k = f.nextInt();
char[] on = f.next().toCharArray();
par = new int[k];
sz1 = new int[k];
sz2 = new int[k];
xor = new int[k];
req = new int[k];
int[][] sets = new int[n][2];
for(int i = 0; i < n; i++)
sets[i][0] = sets[i][1] = -1;
for(int i = 0; i < k; i++) {
par[i] = i;
sz1[i] = 1;
int m = f.nextInt();
while(m-->0) {
int a = f.nextInt()-1;
if(sets[a][0] == -1) sets[a][0] = i;
else sets[a][1] = i;
}
}
int ans = 0;
for(int i = 0; i < n; i++) {
if(sets[i][0] == -1) {}
else if(sets[i][1] == -1) {
int s = find(sets[i][0]);
if(req[s] == 0) {
if(on[i] == '0') req[s] = xor[sets[i][0]]+1;
else req[s] = 2-xor[sets[i][0]];
ans -= Math.min(sz1[s],sz2[s]);
if(req[s] == 1) ans += sz1[s];
else ans += sz2[s];
}
} else {
int s = find(sets[i][0]);
int t = find(sets[i][1]);
if(s != t) {
if(req[s] == 1) ans -= sz1[s];
else if(req[s] == 2) ans -= sz2[s];
else ans -= Math.min(sz1[s],sz2[s]);
if(req[t] == 1) ans -= sz1[t];
else if(req[t] == 2) ans -= sz2[t];
else ans -= Math.min(sz1[t],sz2[t]);
s = union(sets[i][0],sets[i][1], on[i]=='1');
if(req[s] == 1) ans += sz1[s];
else if(req[s] == 2) ans += sz2[s];
else ans += Math.min(sz1[s],sz2[s]);
}
}
out.println(ans);
}
///
out.flush();
}
int[] par, sz1, sz2, req, xor;
public int find(int x) {
if(par[x] == x) return x;
int np = find(par[x]);
xor[x] ^= xor[par[x]];
return par[x] = np;
}
public int union(int x, int y, boolean on) {
int px = find(x), py = find(y);
if(px == py) return px;
if(sz1[px]+sz2[px] > sz1[py]+sz2[py]) {
int t = py;
py = px;
px = t;
}
if((xor[x] != xor[y]) ^ on) {
par[px] = py;
sz1[py] += sz1[px];
sz2[py] += sz2[px];
if(req[px] != 0) req[py] = req[px];
} else {
par[px] = py;
sz1[py] += sz2[px];
sz2[py] += sz1[px];
xor[px] = 1;
if(req[px] != 0) req[py] = 3-req[px];
}
return py;
}
///
static class FastScanner {
public BufferedReader reader;
public StringTokenizer tokenizer;
public FastScanner() {
reader = new BufferedReader(new InputStreamReader(System.in), 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());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
try {
return reader.readLine();
} catch(IOException e) {
throw new RuntimeException(e);
}
}
}
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 ≤ i ≤ n.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset.
The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const double eps = 1e-10;
const int MOD = 998857459;
const int INF = 0x3f3f3f3f;
const int maxn = 3e5 + 10;
const int maxm = 5e6 + 10;
int n, k, op[maxn][2], p[maxn << 1], val[maxn << 1];
char str[maxn];
int find(int first) {
return p[first] == first ? p[first] : p[first] = find(p[first]);
}
void merge(int first, int second) {
if (first == second) return;
val[second] += val[first];
p[first] = second;
}
int main() {
cin >> n >> k;
scanf("%s", str + 1);
for (int i = 1, sz, a; i <= k; ++i) {
scanf("%d", &sz);
while (sz--) {
scanf("%d", &a);
op[a][bool(op[a][0])] = i;
}
val[i] = 1;
p[i] = i;
p[i + k] = i + k;
}
val[0] = INF;
int now = 0;
for (int i = 1; i <= n; ++i) {
int fa = find(op[i][0]), fb = find(op[i][1]);
int rfa = find(op[i][0] + k), rfb = find(op[i][1] + k);
if (!op[i][0]) goto out;
if (str[i] == '1') {
if (op[i][1]) {
if (fa == fb) goto out;
now -= min(val[fa], val[rfa]);
now -= min(val[fb], val[rfb]);
merge(fa, fb);
merge(rfa, rfb);
now += min(val[fb], val[rfb]);
} else {
now -= min(val[fa], val[rfa]);
merge(fa, find(0));
now += val[rfa];
}
} else {
if (op[i][1]) {
if (fa == rfb) goto out;
now -= min(val[fa], val[rfa]);
now -= min(val[fb], val[rfb]);
merge(fa, rfb);
merge(fb, rfa);
now += min(val[rfb], val[rfa]);
} else {
now -= min(val[fa], val[rfa]);
merge(rfa, find(0));
now += val[fa];
}
}
out:
printf("%d\n", now);
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 ≤ i ≤ n.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset.
The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5, inf = 1e9;
int n, kk, s, x, k[N][2];
char ch[N];
int fa[N << 1], val[N << 1];
int find(int u) { return fa[u] == u ? fa[u] : fa[u] = find(fa[u]); }
void uni(int a, int b) {
if (find(a) == find(b)) return;
val[find(b)] += val[find(a)];
fa[find(a)] = find(b);
}
int mix(int a) { return min(val[find(a)], val[find(a + kk)]); };
int main() {
scanf("%d%d%s", &n, &kk, ch + 1);
for (int i = 1; i <= kk; i++) {
scanf("%d", &s);
while (s--) {
scanf("%d", &x);
k[x][bool(k[x][0])] = i;
}
}
for (int i = 1; i <= kk; i++) val[i] = 1, fa[i] = i, fa[i + kk] = i + kk;
fa[kk + kk + 1] = kk + kk + 1;
val[kk + kk + 1] = inf;
int ans = 0;
for (int i = 1; i <= n; i++) {
if (k[i][0] == 0) goto qwq;
if (ch[i] == '1') {
if (k[i][1]) {
if (find(k[i][0]) == find(k[i][1])) goto qwq;
ans -= mix(k[i][0]) + mix(k[i][1]);
uni(k[i][0], k[i][1]), uni(k[i][0] + kk, k[i][1] + kk);
ans += min(val[find(k[i][1])], val[find(k[i][1] + kk)]);
} else {
ans -= mix(k[i][0]);
uni(k[i][0], kk + kk + 1);
ans += val[find(k[i][0] + kk)];
}
} else {
if (k[i][1]) {
if (find(k[i][0]) == find(k[i][1] + kk)) goto qwq;
ans -= mix(k[i][0]) + mix(k[i][1]);
uni(k[i][0], k[i][1] + kk), uni(k[i][1], k[i][0] + kk);
ans += min(val[find(k[i][1] + kk)], val[find(k[i][0] + kk)]);
} else {
ans -= mix(k[i][0]);
uni(k[i][0] + kk, kk + kk + 1);
ans += val[find(k[i][0])];
}
}
qwq:
printf("%d\n", ans);
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 ≤ i ≤ n.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset.
The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const long double PI = 3.141592653589793238462643383279502884197;
long long fac[1] = {1}, inv[1] = {1};
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long mp(long long a, long long b) {
long long ret = 1;
while (b) {
if (b & 1) ret = ret * a % MOD;
a = a * a % MOD;
b >>= 1;
}
return ret;
}
long long cmb(long long r, long long c) {
return (c > r || c < 0) ? 0 : fac[r] * inv[c] % MOD * inv[r - c] % MOD;
}
char s[300001];
vector<int> v[300000];
int p[600001];
int rotn[600001];
char vis[600001];
int find(int a) {
if (p[a] == -1) return a;
return p[a] = find(p[a]);
}
void merge(int a, int b) {
a = find(a);
b = find(b);
if (a == b) return;
if (a > b) swap(a, b);
p[b] = a;
rotn[a] = min((int)1e9, rotn[b] + rotn[a]);
}
int main() {
memset(p, -1, sizeof(p));
int n, k;
scanf("%d %d", &n, &k);
scanf("%s", s);
for (int i = 0; i < n; i++) s[i] -= '0';
for (int i = 0; i < k; i++) rotn[i] = 1;
for (int i = 0; i < k; i++) {
int t;
scanf("%d", &t);
for (int j = 0; j < t; j++) {
int tt;
scanf("%d", &tt);
tt--;
v[tt].push_back(i);
}
}
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j : v[i]) {
if (vis[find(j)]) ans -= rotn[find(j)], vis[find(j)] = 0;
if (vis[find(j + k)]) ans -= rotn[find(j + k)], vis[find(j + k)] = 0;
}
if (s[i] == 0) {
if ((int)(v[i]).size() == 2) {
merge(v[i][0] + k, v[i][1]);
merge(v[i][0], v[i][1] + k);
} else
rotn[find(v[i][0] + k)] = 1e9;
} else {
if ((int)(v[i]).size() == 2) {
merge(v[i][0], v[i][1]);
merge(v[i][0] + k, v[i][1] + k);
} else if ((int)(v[i]).size() == 1)
rotn[find(v[i][0])] = 1e9;
}
for (int j : v[i]) {
if (vis[find(j)] || vis[find(j + k)]) continue;
int ne;
if (rotn[find(j)] > rotn[find(j + k)])
ne = find(j + k);
else
ne = find(j);
vis[ne] = 1;
ans += rotn[ne];
}
printf("%d\n", ans);
}
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 ≤ i ≤ n.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset.
The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
const int lim = 1000 * 1000 + 5;
int nbElem, nbSub;
vector<int> subset[lim];
int side[lim];
int isIn[lim][2];
string ini;
int rep = 0;
int drepr[lim];
int dsz[lim];
int cnt[lim][2];
int dfind(int x) {
if (drepr[x] != x) drepr[x] = dfind(drepr[x]);
return drepr[x];
}
void add(int cc, int s0, int s1) {
cc = dfind(cc);
rep -= min(cnt[cc][0], cnt[cc][1]);
cnt[cc][0] = min(lim, cnt[cc][0] + s0);
cnt[cc][1] = min(lim, cnt[cc][1] + s1);
rep += min(cnt[cc][0], cnt[cc][1]);
}
void dmerge(int a, int b) {
a = dfind(a);
b = dfind(b);
if (a == b) return;
if (dsz[a] < dsz[b]) swap(a, b);
add(a, cnt[b][0], cnt[b][1]);
add(b, -cnt[b][0], -cnt[b][1]);
dsz[a] += dsz[b];
drepr[b] = a;
}
void dfs(int nod) {
cnt[nod][side[nod]] = 1;
for (int elem : subset[nod]) {
if (isIn[elem][1] == -1) continue;
int oth = isIn[elem][0] + isIn[elem][1] - nod;
if (side[oth] == -1) {
side[oth] = side[nod] ^ (ini[elem] == '0');
dfs(oth);
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> nbElem >> nbSub;
cin >> ini;
fill(side, side + lim, -1);
iota(drepr, drepr + lim, 0);
fill_n(&isIn[0][0], 2 * lim, -1);
for (int sub = 0; sub < nbSub; ++sub) {
int st;
cin >> st;
subset[sub].resize(st);
for (int pos = 0; pos < st; ++pos) {
int elem;
cin >> elem;
--elem;
subset[sub][pos] = elem;
if (isIn[elem][0] == -1)
isIn[elem][0] = sub;
else
isIn[elem][1] = sub;
}
}
for (int sub = 0; sub < nbSub; ++sub) {
if (side[sub] == -1) {
side[sub] = 0;
dfs(sub);
}
}
for (int elem = 0; elem < nbElem; ++elem) {
int n0 = isIn[elem][0], n1 = isIn[elem][1];
if (n0 != -1 && n1 == -1) {
int destroy = side[n0] ^ (ini[elem] == '0');
if (destroy == 1)
add(n0, 0, lim);
else
add(n0, lim, 0);
} else if (n0 != -1) {
dmerge(n0, n1);
}
cout << rep << "\n";
}
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 ≤ i ≤ n.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset.
The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
const int MAXN = N;
const int mx = 5e5;
const int inf = 1e9;
int arr[MAXN];
vector<int> v1[MAXN];
struct DSU {
int tot = 0;
int par[MAXN];
int cost[MAXN];
void init() {
for (int i = 0; i < N; i++) {
par[i] = i;
cost[i] = (i % 2 == 0);
}
cost[2 * mx] = 0;
cost[2 * mx + 1] = inf;
tot = 0;
}
int getpar(int a) {
if (par[a] == a) {
return a;
}
return par[a] = getpar(par[a]);
}
void merge(int a, int b, int curr) {
int a_true = 2 * a;
int b_true = 2 * b;
int a_false = 2 * a + 1;
int b_false = 2 * b + 1;
a_true = getpar(a_true);
a_false = getpar(a_false);
b_true = getpar(b_true);
b_false = getpar(b_false);
if (curr) {
if (a_true == b_true) {
return;
}
tot -= min(cost[a_true], cost[a_false]);
tot -= min(cost[b_true], cost[b_false]);
cost[b_true] += cost[a_true];
cost[b_false] += cost[a_false];
par[a_true] = b_true;
par[a_false] = b_false;
} else {
if (a_true == b_false) {
return;
}
tot -= min(cost[a_true], cost[a_false]);
tot -= min(cost[b_true], cost[b_false]);
cost[b_false] += cost[a_true];
cost[b_true] += cost[a_false];
par[a_false] = b_true;
par[a_true] = b_false;
}
tot += min(cost[b_true], cost[b_false]);
}
} dsu;
int main() {
int n, k;
cin >> n >> k;
string str;
cin >> str;
for (int i = 1; i <= n; i++) {
arr[i] = str[i - 1] - '0';
}
for (int i = 1; i <= k; i++) {
int c;
cin >> c;
while (c--) {
int a;
cin >> a;
v1[a].push_back(i);
}
}
dsu.init();
for (int i = 1; i <= n; i++) {
if (v1[i].size() == 1) {
dsu.merge(v1[i][0], mx, arr[i] == 0);
} else if (v1[i].size() == 2) {
dsu.merge(v1[i][0], v1[i][1], arr[i] == 1);
}
cout << dsu.tot << endl;
}
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 ≤ i ≤ n.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset.
The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
struct UF {
vector<int> e;
UF(int n) : e(n, -1) {}
int find(int x) { return e[x] < 0 ? x : e[x] = find(e[x]); }
bool join(int a, int b) {
a = find(a), b = find(b);
if (a == b) return false;
if (e[a] > e[b]) swap(a, b);
e[a] += e[b];
e[b] = a;
return true;
}
};
char s[300030];
pair<int, int> a[300030];
vector<pair<int, int>> g[300030];
int t, n, k, m, x, d[300030], cnt[3][300030], ans;
void dfs(int v, int p, int s) {
if (s)
d[v] = d[p];
else
d[v] = 3 - d[p];
for (auto u : g[v])
if (!d[u.first]) dfs(u.first, v, u.second);
}
int main() {
scanf("%d%d%s", &n, &k, s + 1);
UF uf(k + 1);
d[k + 1] = 2;
for (int i = 1; i <= n; i++) s[i] -= '0';
for (int i = 1; i <= k; i++)
for (int j = !scanf("%d", &m); j < m; j++)
if (scanf("%d", &x), a[x].first) {
a[x].second = i;
g[a[x].first].emplace_back(i, s[x]);
g[i].emplace_back(a[x].first, s[x]);
} else
a[x].first = i;
for (int i = 1; i <= n; i++)
if (a[i].first && a[i].second == 0) g[0].emplace_back(a[i].first, s[i]);
for (int i = 0; i <= k; i++)
if (!d[i]) dfs(i, k + 1, 0);
for (int i = 1; i <= k; i++) cnt[d[i]][i] = 1;
cnt[1][0] = 0x3f3f3f3f;
for (int i = 1; i <= n; i++) {
int u = uf.find(a[i].first), v = uf.find(a[i].second);
if (uf.join(u, v)) {
ans -= min(cnt[1][u], cnt[2][u]);
ans -= min(cnt[1][v], cnt[2][v]);
for (int j = 1; j <= 2; j++)
cnt[j][u] = cnt[j][v] = cnt[j][u] + cnt[j][v];
ans += min(cnt[1][u], cnt[2][u]);
}
printf("%d\n", ans);
}
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 ≤ i ≤ n.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset.
The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T& x) {
x = 0;
int fl = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') fl = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 1) + (x << 3) + ch - '0';
ch = getchar();
}
x *= fl;
}
template <typename T, typename... Args>
inline void read(T& t, Args&... args) {
read(t);
read(args...);
}
const int N = 300005;
const int INF = 1e9;
int n, k, c;
vector<int> vec[N];
char s[N];
int fa[N << 1], sz[N << 1];
inline int lowbit(int x) { return x & -x; }
inline int findfa(int x) { return x == fa[x] ? x : fa[x] = findfa(fa[x]); }
inline void unionn(int x, int y) {
x = findfa(x), y = findfa(y);
fa[y] = x;
if (sz[x] < INF) sz[x] += sz[y];
}
int main() {
read(n, k);
scanf("%s", s + 1);
for (int i = 1; i <= k; i++) {
read(c);
for (int j = 1; j <= c; j++) {
int x;
read(x);
vec[x].push_back(i);
}
}
for (int i = 1; i <= k * 2; i++) fa[i] = i, sz[i] = (i <= k);
fa[k * 2 + 1] = k * 2 + 1, sz[k * 2 + 1] = INF;
int ans = 0;
for (int i = 1; i <= n; i++) {
if (vec[i].size() == 1) {
if (findfa(k * 2 + 1) != findfa(vec[i][0] + k * (s[i] == '0'))) {
ans -= min(sz[findfa(vec[i][0])], sz[findfa(vec[i][0] + k)]);
unionn(k * 2 + 1, vec[i][0] + k * (s[i] == '0'));
ans += min(sz[findfa(vec[i][0])], sz[findfa(vec[i][0] + k)]);
}
} else if (vec[i].size() == 2) {
if (s[i] == '0') {
if (findfa(vec[i][0]) != findfa(vec[i][1] + k)) {
ans -= min(sz[findfa(vec[i][0])], sz[findfa(vec[i][0] + k)]);
ans -= min(sz[findfa(vec[i][1])], sz[findfa(vec[i][1] + k)]);
unionn(vec[i][0], vec[i][1] + k);
unionn(vec[i][0] + k, vec[i][1]);
ans += min(sz[findfa(vec[i][0])], sz[findfa(vec[i][0] + k)]);
}
} else {
if (findfa(vec[i][0]) != findfa(vec[i][1])) {
ans -= min(sz[findfa(vec[i][0])], sz[findfa(vec[i][0] + k)]);
ans -= min(sz[findfa(vec[i][1])], sz[findfa(vec[i][1] + k)]);
unionn(vec[i][0], vec[i][1]);
unionn(vec[i][0] + k, vec[i][1] + k);
ans += min(sz[findfa(vec[i][0])], sz[findfa(vec[i][0] + k)]);
}
}
}
printf("%d\n", ans);
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 ≤ i ≤ n.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset.
The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 6e5 + 5;
int n, K;
char S[MAXN >> 1];
vector<int> A[MAXN];
namespace DSU {
int fa[MAXN], size[MAXN];
inline void init() {
for (int i = 1; i <= K; ++i) fa[i] = i, fa[i + K] = i + K, size[i + K] = 1;
}
int findfa(int u) { return fa[u] == u ? u : fa[u] = findfa(fa[u]); }
int calc(int u) {
int v = u <= K ? u + K : u - K;
u = findfa(u), v = findfa(v);
if (!u || !v) return size[u + v];
return min(size[u], size[v]);
}
void join(int u, int v) {
u = findfa(u), v = findfa(v);
if (!v) swap(u, v);
fa[v] = u;
if (u) size[u] += size[v];
}
} // namespace DSU
int main() {
scanf("%d%d", &n, &K);
DSU::init();
scanf("%s", S + 1);
for (int x, y, i = 1; i <= K; ++i) {
scanf("%d", &y);
while (y--) scanf("%d", &x), A[x].push_back(i);
}
int ans = 0;
for (int i = 1; i <= n; ++i) {
if (A[i].size() == 1) {
int x = A[i][0];
ans -= DSU::calc(x);
if (S[i] == '0')
DSU::fa[DSU::findfa(x)] = 0;
else
DSU::fa[DSU::findfa(x + K)] = 0;
ans += DSU::calc(x);
}
if (A[i].size() == 2) {
int x = A[i][0], y = A[i][1];
if (S[i] == '0') {
if (DSU::findfa(x) != DSU::findfa(y + K)) {
ans -= DSU::calc(x) + DSU::calc(y);
DSU::join(x, y + K), DSU::join(y, x + K);
ans += DSU::calc(x);
}
} else {
if (DSU::findfa(x) != DSU::findfa(y)) {
ans -= DSU::calc(x) + DSU::calc(y);
DSU::join(x, y), DSU::join(x + K, y + K);
ans += DSU::calc(x);
}
}
}
printf("%d\n", ans);
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 ≤ i ≤ n.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset.
The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int n, k;
char s[300005];
vector<int> bel[300005];
int fa[300005 * 2], val[300005 * 2];
int getfa(int now) { return fa[now] == now ? now : fa[now] = getfa(fa[now]); }
int getans(int now) {
int now2;
if (now <= k)
now2 = k + now;
else
now2 = now - k;
int xx = getfa(now), yy = getfa(now2);
if (xx == 0 || yy == 0) return val[xx + yy];
return min(val[xx], val[yy]);
}
void merge(int x, int y) {
int xx = getfa(x), yy = getfa(y);
if (xx == yy) return;
if (yy == 0) swap(xx, yy);
fa[yy] = xx;
if (xx != 0) val[xx] += val[yy];
}
int main() {
scanf("%d%d", &n, &k);
scanf("%s", s + 1);
for (int i = 1; i <= k; i++) {
int c;
scanf("%d", &c);
for (int j = 1; j <= c; j++) {
int x;
scanf("%d", &x);
bel[x].push_back(i);
}
fa[i] = i;
fa[i + k] = i + k;
val[i + k] = 1;
}
int ans = 0;
for (int i = 1; i <= n; i++) {
int len = bel[i].size();
if (len == 0) {
printf("%d\n", ans);
continue;
}
if (len == 1) {
int now = bel[i][0];
ans -= getans(now);
if (s[i] == '1')
fa[getfa(now + k)] = 0;
else
fa[getfa(now)] = 0;
ans += getans(now);
}
if (len == 2) {
int now1 = bel[i][0];
int now2 = bel[i][1];
if (s[i] == '1') {
if (getfa(now1) != getfa(now2)) {
ans -= getans(now1);
ans -= getans(now2);
merge(now1, now2);
merge(now1 + k, now2 + k);
ans += getans(now1);
}
} else {
if (getfa(now1 + k) != getfa(now2)) {
ans -= getans(now1);
ans -= getans(now2);
merge(now1 + k, now2);
merge(now1, now2 + k);
ans += getans(now1);
}
}
}
printf("%d\n", ans);
}
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 ≤ i ≤ n.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset.
The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int INF = 1000000007;
string s;
int n, k, c, v, ans;
int fat[1000100], siz[1000100];
vector<int> edge[1000100];
struct node {
int l, r, xo;
node(int _l = 0, int _r = 0, int _xo = 0) : l(_l), r(_r), xo(_xo) {}
int get() { return min(l, r); }
void operator+=(node th) {
l += th.l;
r += th.r;
if (l > INF) l = INF;
if (r > INF) r = INF;
}
} val[1000100];
pair<int, int> uu, vv;
pair<int, int> trace(int x) {
if (fat[x] < 0) return make_pair(x, 0);
pair<int, int> pp = trace(fat[x]);
fat[x] = pp.first;
val[x].xo ^= pp.second;
return make_pair(fat[x], val[x].xo);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> k >> s;
s = "?" + s;
for (int i = 1; i <= k; i++) {
fat[i] = -1;
siz[i] = 1;
val[i] = node(1, 0, 0);
cin >> c;
for (int j = 1; j <= c; j++) {
cin >> v;
edge[v].push_back(i);
}
}
int ans = 0;
int id;
for (int i = 1; i <= n; i++) {
int tp = (s[i] - '0') ^ 1;
if (edge[i].size() == 1) {
id = edge[i][0];
uu = trace(id);
ans -= val[uu.first].get();
val[uu.first] += node((tp == uu.second) * INF, (tp != uu.second) * INF);
ans += val[uu.first].get();
} else if (edge[i].size() == 2) {
id = edge[i][0];
uu = trace(id);
id = edge[i][1];
vv = trace(id);
if (uu.first != vv.first) {
ans -= (val[uu.first].get() + val[vv.first].get());
if (siz[uu.first] > siz[vv.first]) swap(uu, vv);
if (uu.second ^ vv.second ^ tp) {
swap(val[vv.first].l, val[vv.first].r);
val[vv.first].xo = 1;
}
siz[uu.first] += siz[vv.first];
fat[vv.first] = uu.first;
val[uu.first] += val[vv.first];
ans += val[uu.first].get();
}
}
cout << ans << endl;
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 ≤ i ≤ n.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset.
The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
char op[300005];
int n, k, fa[300005], v[300005], num[300005], sz[300005], ans, gx[300005];
vector<int> vec[300005];
int ff(int x) {
if (fa[x] == x) return x;
int f = ff(fa[x]);
v[x] ^= v[fa[x]];
return fa[x] = f;
}
inline void merge(int x, int y, int p) {
if (ff(x) == ff(y)) return;
int fx = ff(x), fy = ff(y);
if (fx == 0) swap(fx, fy);
v[fx] = p ^ v[x] ^ v[y];
sz[fy] += sz[fx];
num[fy] += v[fx] ? sz[fx] - num[fx] : num[fx];
ans -= gx[fy] + gx[fx];
if (fy == 0)
gx[fy] = num[fy];
else
gx[fy] = min(sz[fy] - num[fy], num[fy]);
ans += gx[fy], fa[fx] = fy;
}
int main() {
scanf("%d%d%s", &n, &k, op + 1);
for (int i = 0; i <= k; i++) fa[i] = i, sz[i] = 1;
for (int i = 1, c, x; i <= k; i++) {
scanf("%d", &c);
while (c--) scanf("%d", &x), vec[x].push_back(i);
}
for (int i = 1; i <= n; i++) {
if (vec[i].size() == 1) vec[i].push_back(0);
}
for (int i = 1; i <= n; i++) {
if (!vec[i].empty()) merge(vec[i][0], vec[i][1], op[i] == '0');
printf("%d\n", ans);
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 ≤ i ≤ n.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset.
The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 300005;
int n, k, fa[maxn], f[maxn][2], val[maxn], pos[maxn][2], ans;
char s[maxn];
int getfa(int x) {
if (fa[x] == x) return x;
int F = fa[x];
fa[x] = getfa(F);
val[x] ^= val[F];
return fa[x];
}
void merge(int x, int y, int z) {
getfa(x), getfa(y);
z ^= (val[x] ^ val[y]);
x = fa[x], y = fa[y];
if (x == y) return;
if (x > y) swap(x, y);
if (!x)
ans -= f[x][0];
else
ans -= min(f[x][0], f[x][1]);
ans -= min(f[y][0], f[y][1]);
f[x][0] += f[y][z];
f[x][1] += f[y][z ^ 1];
fa[y] = x, val[y] = z;
if (!x)
ans += f[x][0];
else
ans += min(f[x][0], f[x][1]);
}
int main() {
scanf("%d%d", &n, &k);
scanf("%s", s + 1);
for (int i = 1, cnt; i <= k; i++) {
scanf("%d", &cnt);
for (int j = 1, x; j <= cnt; j++) {
scanf("%d", &x);
if (pos[x][0])
pos[x][1] = i;
else
pos[x][0] = i;
}
}
for (int i = 1; i <= k; i++) fa[i] = i, f[i][1] = 1;
for (int i = 1; i <= n; i++) {
if (!pos[i][0]) {
printf("%d\n", ans);
continue;
}
merge(pos[i][0], pos[i][1], (s[i] - '0') ^ 1);
printf("%d\n", ans);
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 ≤ i ≤ n.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset.
The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
struct perem {
int t = 0, l = 1, p = -1, q = 0;
};
ifstream fin("AAtest.in.txt");
int n, k, m, c, vas, cc = 1000000, kl;
vector<int> vv;
string s;
vector<vector<int>> la;
vector<perem> pr;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cerr.tie(0);
cin >> n >> k >> s;
la.resize(n, vector<int>(0));
pr.resize(k);
for (int(i) = (0); ((i)) < ((k)); ((i))++) {
cin >> m;
for (int(j) = (0); ((j)) < ((m)); ((j))++) {
cin >> c;
la[c - 1].emplace_back(i);
}
}
for (int(i) = (0); ((i)) < ((n)); ((i))++) {
vas = 0;
if (la[i].size() > 0) {
int a = la[i][0];
bool d = s[i] == '1' ? 1 : 0;
for (;; a = pr[a].p) {
d ^= pr[a].q;
if (pr[a].p == -1) break;
}
if (la[i].size() == 1) {
if (!d) {
vas += pr[a].l;
pr[a].q ^= 1;
}
pr[a].l = cc;
} else {
int b = la[i][1];
for (;; b = pr[b].p) {
d ^= pr[b].q;
if (pr[b].p == -1) break;
}
if (a != b) {
if (!d) {
if (pr[a].l > pr[b].l) swap(a, b);
vas += pr[a].l;
pr[a].q ^= 1;
kl = abs(pr[a].l - pr[b].l);
} else
kl = min(pr[a].l + pr[b].l, cc);
if (pr[a].t < pr[b].t) swap(a, b);
if (pr[a].q) pr[b].q ^= 1;
if (pr[a].t == pr[b].t) pr[a].t++;
pr[a].l = kl;
pr[b].p = a;
}
}
}
if (i)
vv.emplace_back(vv.back() + vas);
else
vv.emplace_back(vas);
}
for (int i : vv) cout << i << endl;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 ≤ i ≤ n.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset.
The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 6e5 + 10;
int n, k, l[maxn][2];
int fa[maxn], sz[maxn];
string s;
int get_fa(int x) {
if (x == fa[x]) return x;
return fa[x] = get_fa(fa[x]);
}
int calc(int x) {
int y = x <= k ? x + k : x - k;
x = get_fa(x), y = get_fa(y);
if (x == 0 || y == 0) return sz[x + y];
return min(sz[x], sz[y]);
}
void merge_dis(int x, int y) {
x = get_fa(x);
y = get_fa(y);
if (y == 0) {
swap(x, y);
}
fa[y] = x;
if (x != 0) sz[x] += sz[y];
}
int main() {
scanf("%d%d", &n, &k);
cin >> s;
for (int i = 1; i <= k; i++) fa[i] = i, fa[i + k] = i + k, sz[i + k] = 1;
for (int i = 1, c; i <= k; i++) {
scanf("%d", &c);
for (int j = 0, x; j < c; j++) {
scanf("%d", &x);
if (l[x][0] == 0)
l[x][0] = i;
else
l[x][1] = i;
}
}
int ans = 0;
for (int i = 1; i <= n; i++) {
if (l[i][1] == 0) {
int x = l[i][0];
if (x) {
ans -= calc(x);
if (s[i - 1] == '1')
fa[get_fa(x + k)] = 0;
else
fa[get_fa(x)] = 0;
ans += calc(x);
}
} else {
int x = l[i][0], y = l[i][1];
if (s[i - 1] == '1') {
if (get_fa(x) != get_fa(y)) {
ans -= calc(x);
ans -= calc(y);
merge_dis(x, y);
merge_dis(x + k, y + k);
ans += calc(x);
}
} else {
if (get_fa(x + k) != get_fa(y)) {
ans -= calc(x);
ans -= calc(y);
merge_dis(x + k, y);
merge_dis(x, y + k);
ans += calc(x);
}
}
}
cout << ans << endl;
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 ≤ i ≤ n.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset.
The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int n, m;
int fa[300005];
int x[300005], y[300005];
int f[300005][2];
int a[300005][3], b[300005];
int p;
char s[300005];
int main() {
scanf("%d%d%s", &n, &m, s);
for (int i = 1; i <= m; i++) {
int ha;
scanf("%d", &ha);
while (ha--) {
int haha;
scanf("%d", &haha);
a[haha][++b[haha]] = i;
}
fa[i] = i;
f[i][0] = 1;
y[i] = -1;
}
for (int i = 1; i <= n; i++) {
if (b[i] == 1) {
int j = a[i][1];
int l = j, r = 0;
for (; fa[l] != l; l = fa[l]) r ^= x[l];
r ^= (s[i - 1] == '1');
if (y[l] == -1) {
y[l] = r;
p -= min(f[l][0], f[l][1]);
p += f[l][r];
}
} else if (b[i] == 2) {
int j = a[i][1], k = a[i][2], l = x[j], u = x[k];
while (j != fa[j]) {
j = fa[j];
l ^= x[j];
}
while (k != fa[k]) {
k = fa[k];
u ^= x[k];
}
if (j != k) {
if (f[j][0] + f[j][1] < f[k][0] + f[k][1]) swap(j, k);
if (y[j] == -1)
p -= min(f[j][0], f[j][1]);
else
p -= f[j][y[j]];
if (y[k] == -1)
p -= min(f[k][0], f[k][1]);
else
p -= f[k][y[k]];
fa[k] = j;
if (l ^ u ^ (s[i - 1] == '0')) {
x[k] = 1;
swap(f[k][0], f[k][1]);
if (y[k] != -1) y[k] ^= 1;
}
f[j][0] += f[k][0];
f[j][1] += f[k][1];
if (y[k] != -1) y[j] = y[k];
if (y[j] == -1)
p += min(f[j][0], f[j][1]);
else
p += f[j][y[j]];
}
}
printf("%d\n", p);
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 ≤ i ≤ n.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset.
The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
long long poww(long long a, long long b, long long md) {
return (!b ? 1
: (b & 1 ? a * poww(a * a % md, b / 2, md) % md
: poww(a * a % md, b / 2, md) % md));
}
const long long MAXN = 1e6 + 10;
const long long INF = 8e18;
const long long MOD = 1e9 + 7;
bool Q[MAXN];
int state[MAXN], par[MAXN], c_par[MAXN], S[MAXN], C[3][MAXN], cost[MAXN],
col[MAXN], tot_cost, n, k;
vector<int> V[MAXN], BP_C[MAXN], CC_C[MAXN];
inline void Update(int v, int c, bool neg = false) {
state[v] = c;
if (c == 0) {
if (neg) tot_cost -= cost[v];
cost[v] = min(C[1][v], C[2][v]);
tot_cost += cost[v];
return;
}
if (neg) tot_cost -= cost[v];
cost[v] = C[c][v];
tot_cost += cost[v];
}
inline void Union(int v, int u, int t1, int t2) {
tot_cost -= cost[v];
tot_cost -= cost[u];
if (BP_C[v].size() > BP_C[u].size()) swap(v, u);
if (col[t1] == col[t2]) {
if (state[v]) state[v] = 3 - state[v];
for (int e : BP_C[v]) col[e] = 3 - col[e];
swap(C[1][v], C[2][v]);
}
for (int e : BP_C[v]) {
c_par[e] = u;
C[col[e]][u] += S[e];
BP_C[u].push_back(e);
}
if (state[u] == 0) state[u] = state[v];
Update(u, state[u]);
BP_C[v].clear();
C[1][v] = C[2][v] = 0;
}
inline void Union2(int v, int u) {
if (CC_C[v].size() > CC_C[u].size()) swap(v, u);
if (v == u) return;
int t1 = u, t2 = v;
u = c_par[u], v = c_par[v];
if (u != v) {
tot_cost -= cost[v];
tot_cost -= cost[u];
if (BP_C[v].size() > BP_C[u].size()) swap(v, u);
if (col[t1] != col[t2]) {
if (state[v]) state[v] = 3 - state[v];
for (int e : BP_C[v]) col[e] = 3 - col[e];
swap(C[1][v], C[2][v]);
}
C[1][u] += C[1][v];
C[2][u] += C[2][v];
for (int e : BP_C[v]) {
c_par[e] = u;
BP_C[u].push_back(e);
}
if (state[u] == 0) state[u] = state[v];
Update(u, state[u]);
BP_C[v].clear();
C[1][v] = C[2][v] = 0;
}
u = t1;
v = t2;
for (int e : CC_C[v]) {
par[e] = u;
CC_C[u].push_back(e);
}
S[u] += S[v];
S[v] = 0;
CC_C[v].clear();
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; i++) {
char c;
cin >> c;
Q[i] = (c == '1');
}
for (int i = 1; i <= k; i++) {
int c;
cin >> c;
while (c--) {
int e;
cin >> e;
V[e].push_back(i);
}
par[i] = i;
CC_C[i].push_back(i);
S[i] = 1;
col[i] = 1;
C[1][i] = 1;
c_par[i] = i;
BP_C[i].push_back(i);
}
for (int i = 1; i <= n; i++) {
if (V[i].size() == 1) {
int ind = V[i].front(), v = par[ind], p_st = c_par[v], c = col[v];
if (Q[i])
Update(p_st, 3 - c, true);
else
Update(p_st, c, true);
} else if (V[i].size() == 2) {
int ind1 = V[i][0], ind2 = V[i][1], v = par[ind1], u = par[ind2],
p1 = c_par[v], p2 = c_par[u];
if (Q[i] == 0) {
if (p1 != p2) Union(p1, p2, u, v);
} else {
Union2(u, v);
}
}
cout << tot_cost << '\n';
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 ≤ i ≤ n.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset.
The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 6e5 + 10;
int n, k, l[maxn][2];
int fa[maxn], sz[maxn];
string s;
int get_fa(int x) {
if (x == fa[x]) return x;
return fa[x] = get_fa(fa[x]);
}
int calc(int x) {
int y = x <= k ? x + k : x - k;
x = get_fa(x), y = get_fa(y);
if (x == 0 || y == 0) return sz[x + y];
return min(sz[x], sz[y]);
}
void merge_dis(int x, int y) {
x = get_fa(x);
y = get_fa(y);
if (y == 0) {
swap(x, y);
}
fa[y] = x;
if (x != 0) sz[x] += sz[y];
}
int main() {
scanf("%d%d", &n, &k);
cin >> s;
for (int i = 1; i <= (int)3e5; i++)
fa[i] = i, fa[i + k] = i + k, sz[i + k] = 1;
for (int i = 1, c; i <= k; i++) {
scanf("%d", &c);
for (int j = 0, x; j < c; j++) {
scanf("%d", &x);
if (l[x][0] == 0)
l[x][0] = i;
else
l[x][1] = i;
}
}
int ans = 0;
for (int i = 1; i <= n; i++) {
if (l[i][1] == 0) {
int x = l[i][0];
if (x) {
ans -= calc(x);
if (s[i - 1] == '1')
fa[get_fa(x + k)] = 0;
else
fa[get_fa(x)] = 0;
ans += calc(x);
}
} else {
int x = l[i][0], y = l[i][1];
if (s[i - 1] == '1') {
if (get_fa(x) != get_fa(y)) {
ans -= calc(x);
ans -= calc(y);
merge_dis(x, y);
merge_dis(x + k, y + k);
ans += calc(y);
}
} else {
if (get_fa(x + k) != get_fa(y)) {
ans -= calc(x);
ans -= calc(y);
merge_dis(x + k, y);
merge_dis(x, y + k);
ans += calc(x);
}
}
}
cout << ans << endl;
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 ≤ i ≤ n.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset.
The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int n, m;
char s[630000];
int fa[630000];
int val[630000];
vector<int> belong[630000];
int Push(int x) { return x; }
int Unpush(int x) { return m + x; }
int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
bool vis[630000];
void merge(int x, int y) {
int u = find(x), v = find(y);
if (u == v) return;
val[v] += val[u];
if (vis[u]) vis[v] = 1;
fa[u] = v;
}
int value(int x) {
if (vis[find(x)]) return 1000000000;
return val[find(x)];
}
int main() {
scanf("%d%d", &n, &m);
scanf("%s", s + 1);
for (int i = 1; i <= m + m; i++) fa[i] = i;
for (int i = 1; i <= m; i++) {
val[Push(i)] = 1;
int c;
scanf("%d", &c);
for (int j = 1; j <= c; j++) {
int x;
scanf("%d", &x);
belong[x].push_back(i);
}
}
int ans = 0;
for (int i = 1; i <= n; i++) {
if (belong[i].size() == 0) {
} else if (belong[i].size() == 1) {
int v = belong[i][0];
ans -= min(value(Push(v)), value(Unpush(v)));
if (s[i] == '0') {
vis[find(Unpush(v))] = 1;
} else {
vis[find(Push(v))] = 1;
}
ans += min(value(Push(v)), value(Unpush(v)));
} else {
int u = belong[i][0], v = belong[i][1];
if (find(Push(u)) == find(Push(v)) || find(Push(u)) == find(Unpush(v)) ||
find(Push(v)) == find(Unpush(u)) ||
find(Unpush(u)) == find(Unpush(v))) {
} else if (s[i] == '0') {
ans -= min(value(Push(u)), value(Unpush(u)));
ans -= min(value(Push(v)), value(Unpush(v)));
merge(Push(u), Unpush(v));
merge(Push(v), Unpush(u));
ans += min(value(Push(u)), value(Unpush(u)));
} else {
ans -= min(value(Push(u)), value(Unpush(u)));
ans -= min(value(Push(v)), value(Unpush(v)));
merge(Push(u), Push(v));
merge(Unpush(u), Unpush(v));
ans += min(value(Push(u)), value(Unpush(u)));
}
}
printf("%d\n", ans);
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 ≤ i ≤ n.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset.
The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
java
|
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Iterator;
public class Main {
private static final String NO = "No";
private static final String YES = "Yes";
InputStream is;
PrintWriter out;
String INPUT = "";
private static final long MOD = 1000000007;
static int MAXN = 10;
void solve() {
int T = 1;// ni();
for (int i = 0; i < T; i++)
solve(i);
}
// index 1 based
int fa[];
int size[];
void init(int n) {
fa = new int[n + 1];
size = new int[n + 1];
for (int i = 1; i <= n; i++) {
fa[i] = i;
size[i] = i <= n / 2 ? 1 : 0;
}
}
private void merge(int u, int v) {
int u0 = find(u);
int v0 = find(v);
if (u0 == v0)
return;
fa[u0] = v0;
size[v0] += size[u0];
}
private int find(int x) {
if (fa[x] == x)
return x;
else
return fa[x] = find(fa[x]);
}
void solve(int nth) {
int n = ni(), m = ni();
char[] s = ns().toCharArray();
int c[][] = new int[n][2];
for (int i = 1; i <= m; i++) {
int x = ni();
while (x-- > 0) {
int a = ni() - 1;
c[a][c[a][0] > 0 ? 1 : 0] = i;
}
}
init(m + m + 1);
fa[m * 2 + 1] = m * 2 + 1;
size[m * 2 + 1] = (int) 1e9;
int ans = 0;
for (int i = 0; i < n; i++) {
if (c[i][0] > 0 && c[i][1] == 0) {
if (s[i] == '0') {// 必须取,那么将不取放入n*2+1的并查集中,意味着以后不会有不取这个选项
ans -= Math.min(size[find(c[i][0])], size[find(c[i][0] + m)]);
// 因为不知道前面的运算中,这个是取还是不取,但是一定是用最小的那个
merge(2 * m + 1, c[i][0] + m);
ans += size[find(c[i][0])];
} else {// 同上类似,但是这次将“取”这个选项放入不可能并查集中
ans -= Math.min(size[find(c[i][0])], size[find(c[i][0] + m)]);
merge(2 * m + 1, c[i][0]);
ans += size[find(c[i][0] + m)];
}
} else if (c[i][0] > 0 && c[i][1] > 0) {
int x = c[i][0], y = c[i][1];
if (s[i] == '0') {// 两个中取一个
if (find(x) != find(y + m)) {
ans -= Math.min(size[find(x)], size[find(x + m)]) + Math.min(size[find(y + m)], size[find(y)]);
merge(x, y + m);
merge(x + m, y);
ans += Math.min(size[find(x)], size[find(y)]);
}
} else {
if (find(x) != find(y)) {
ans -= Math.min(size[find(x)], size[find(x + m)]) + Math.min(size[find(y + m)], size[find(y)]);
merge(x, y);
merge(x + m, y + m);
ans += Math.min(size[find(x)], size[find(x + m)]);
}
}
}
// tr(i, ans, c[i], fa, mi);
out.println(ans);
}
}
// a^b
long power(long a, long b) {
long x = 1, y = a;
while (b > 0) {
if (b % 2 != 0) {
x = (x * y) % MOD;
}
y = (y * y) % MOD;
b /= 2;
}
return x % MOD;
}
private long gcd(long a, long b) {
while (a != 0) {
long tmp = b % a;
b = a;
a = tmp;
}
return b;
}
void run() throws Exception {
is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
out.flush();
if (!INPUT.isEmpty())
tr(System.currentTimeMillis() - s + "ms");
}
public static void main(String[] args) throws Exception {
new Main().run();
}
private byte[] inbuf = new byte[1024];
public int lenbuf = 0, ptrbuf = 0;
private int readByte() {
if (lenbuf == -1)
throw new InputMismatchException();
if (ptrbuf >= lenbuf) {
ptrbuf = 0;
try {
lenbuf = is.read(inbuf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (lenbuf <= 0)
return -1;
}
return inbuf[ptrbuf++];
}
private boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
private int skip() {
int b;
while ((b = readByte()) != -1 && isSpaceChar(b))
;
return b;
}
private double nd() {
return Double.parseDouble(ns());
}
private char nc() {
return (char) skip();
}
private char[] nc(int n) {
char[] ret = new char[n];
for (int i = 0; i < n; i++)
ret[i] = nc();
return ret;
}
private String ns() {
int b = skip();
StringBuilder sb = new StringBuilder();
while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != '
// ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private char[] ns(int n) {
char[] buf = new char[n];
int b = skip(), p = 0;
while (p < n) {
if (!(isSpaceChar(b)))
buf[p++] = (char) b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private char[][] nm(int n, int m) {
char[][] map = new char[n][];
for (int i = 0; i < n; i++)
map[i] = ns(m);
return map;
}
private int[] na(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = ni();
return a;
}
private Integer[] na2(int n) {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++)
a[i] = ni();
return a;
}
private int[][] na(int n, int m) {
int[][] a = new int[n][];
for (int i = 0; i < n; i++)
a[i] = na(m);
return a;
}
private int ni() {
int num = 0, b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
private Long[] nl2(int n) {
Long[] a = new Long[n];
for (int i = 0; i < n; i++)
a[i] = nl();
return a;
}
private long[] nl(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nl();
return a;
}
private long[][] nl(int n, int m) {
long[][] a = new long[n][];
for (int i = 0; i < n; i++)
a[i] = nl(m);
return a;
}
private long nl() {
long num = 0;
int b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
public static class IterablePermutation implements Iterable<int[]>, Iterator<int[]> {
int[] a;
boolean first = true;
public IterablePermutation(int n) {
assert n >= 1;
a = new int[n];
for (int i = 0; i < n; i++)
a[i] = i;
}
public IterablePermutation(int... a) {
this.a = Arrays.copyOf(a, a.length);
}
@Override
public boolean hasNext() {
if (first)
return true;
int n = a.length;
int i;
for (i = n - 2; i >= 0 && a[i] >= a[i + 1]; i--)
;
return i != -1;
}
@Override
public int[] next() {
if (first) {
first = false;
return a;
}
int n = a.length;
int i;
for (i = n - 2; i >= 0 && a[i] >= a[i + 1]; i--)
;
assert i != -1;
int j;
for (j = i + 1; j < n && a[i] < a[j]; j++)
;
int d = a[i];
a[i] = a[j - 1];
a[j - 1] = d;
for (int p = i + 1, q = n - 1; p < q; p++, q--) {
d = a[p];
a[p] = a[q];
a[q] = d;
}
return a;
}
@Override
public Iterator<int[]> iterator() {
return this;
}
}
private static void tr(Object... o) {
System.out.println(Arrays.deepToString(o));
}
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 ≤ i ≤ n.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset.
The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 2e9;
const long long INFLL = 4e18L;
const int MOD = 0;
const int N = 3e5 + 3;
const int BLUE = 1, RED = 2;
int n, k;
char init_state[N];
vector<int> has[N];
int color[N];
vector<int> adj_same[N], adj_diff[N];
struct DSU {
vector<int> _leader, _size, _forced, _r, _b;
DSU() {}
DSU(int _n) {
_leader.assign(_n + 1, 0);
for (int i = 1; i <= _n; ++i) _leader[i] = i;
_size.assign(_n + 1, 1);
_forced.assign(_n + 1, 0);
_r.assign(_n + 1, 0);
for (int i = 1; i <= _n; ++i) _r[i] = color[i] == RED;
_b.assign(_n + 1, 0);
for (int i = 1; i <= _n; ++i) _b[i] = color[i] == BLUE;
}
int leader(int x) {
return (_leader[x] == x) ? x : _leader[x] = leader(_leader[x]);
}
void unite(int x, int y) {
x = leader(x);
y = leader(y);
if (x == y) return;
_leader[y] = x;
if (_forced[y]) _forced[x] = _forced[y];
_size[x] += _size[y];
_r[x] += _r[y];
_b[x] += _b[y];
}
int get_ans(int x) {
x = leader(x);
if (_forced[x] == RED) return _r[x];
if (_forced[x] == BLUE) return _b[x];
return min(_r[x], _b[x]);
}
};
void dfs(int i, int c) {
color[i] = c;
for (int j : adj_same[i])
if (!color[j]) {
color[j] = c;
dfs(j, c);
}
for (int j : adj_diff[i])
if (!color[j]) {
color[j] = RED + BLUE - c;
dfs(j, RED + BLUE - c);
}
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout << fixed << setprecision(16);
cin >> n >> k;
for (int i = 1; i <= n; ++i) cin >> init_state[i];
for (int i = 1; i <= k; ++i) {
int nb;
cin >> nb;
while (nb--) {
int j;
cin >> j;
has[j].push_back(i);
}
}
for (int i = 1; i <= n; ++i) {
if ((int)has[i].size() < 2) continue;
int x = has[i].front();
int y = has[i].back();
if (init_state[i] == '0') {
adj_diff[x].push_back(y);
adj_diff[y].push_back(x);
} else if (init_state[i] == '1') {
adj_same[x].push_back(y);
adj_same[y].push_back(x);
}
}
for (int i = 1; i <= k; ++i)
if (!color[i]) dfs(i, RED);
DSU dsu(k);
int ans = 0;
for (int i = 1; i <= n; ++i) {
if ((int)has[i].size() == 1) {
int x = has[i].front();
int lx = dsu.leader(x);
ans -= dsu.get_ans(lx);
if (init_state[i] == '0')
dsu._forced[lx] = color[x];
else if (init_state[i] == '1')
dsu._forced[lx] = RED + BLUE - color[x];
ans += dsu.get_ans(lx);
} else if ((int)has[i].size() == 2) {
int x = has[i].front();
int y = has[i].back();
int lx = dsu.leader(x);
int ly = dsu.leader(y);
if (lx != ly) {
ans -= dsu.get_ans(lx);
ans -= dsu.get_ans(ly);
dsu.unite(lx, ly);
ans += dsu.get_ans(dsu.leader(lx));
}
}
cout << ans << endl;
}
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 ≤ i ≤ n.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset.
The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 6e5 + 10;
int n, k, l[maxn][2];
int fa[maxn], sz[maxn];
string s;
int get_fa(int x) {
if (x == fa[x]) return x;
return fa[x] = get_fa(fa[x]);
}
int calc(int x) {
int y;
if (x <= k)
y = x + k;
else
y = x - k;
x = get_fa(x), y = get_fa(y);
if (x == 0 || y == 0) return sz[x + y];
return min(sz[x], sz[y]);
}
void merge_dis(int x, int y) {
x = get_fa(x);
y = get_fa(y);
if (y == 0) {
swap(x, y);
}
fa[y] = x;
if (x != 0) sz[x] += sz[y];
}
int main() {
scanf("%d%d", &n, &k);
cin >> s;
for (int i = 1; i <= (int)3e5; i++)
fa[i] = i, fa[i + k] = i + k, sz[i + k] = 1;
for (int i = 1, c; i <= k; i++) {
scanf("%d", &c);
for (int j = 0, x; j < c; j++) {
scanf("%d", &x);
if (l[x][0] == 0)
l[x][0] = i;
else
l[x][1] = i;
}
}
int ans = 0;
for (int i = 1; i <= n; i++) {
if (l[i][1] == 0) {
int x = l[i][0];
if (x) {
ans -= calc(x);
if (s[i - 1] == '1')
fa[get_fa(x + k)] = 0;
else
fa[get_fa(x)] = 0;
ans += calc(x);
}
} else {
int x = l[i][0], y = l[i][1];
if (s[i - 1] == '1') {
if (get_fa(x) != get_fa(y)) {
ans -= calc(x);
ans -= calc(y);
merge_dis(x, y);
merge_dis(x + k, y + k);
ans += calc(x);
}
} else {
if (get_fa(x + k) != get_fa(y)) {
ans -= calc(x);
ans -= calc(y);
merge_dis(x + k, y);
merge_dis(x, y + k);
ans += calc(x);
}
}
}
cout << ans << endl;
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 ≤ i ≤ n.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset.
The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
struct perem {
int t = 0, l = 1, p = -1, q = 0;
};
ifstream fin("AAtest.in.txt");
int n, k, m, c, vas, cc = 1000000, kl;
vector<int> vv;
string s;
vector<vector<int>> la;
vector<perem> pr;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cerr.tie(0);
cin >> n >> k >> s;
la.resize(n, vector<int>(0));
pr.resize(k);
for (int(i) = (0); ((i)) < ((k)); ((i))++) {
cin >> m;
for (int(j) = (0); ((j)) < ((m)); ((j))++) {
cin >> c;
la[c - 1].emplace_back(i);
}
}
for (int(i) = (0); ((i)) < ((n)); ((i))++) {
vas = 0;
if (la[i].size() > 0) {
int a = la[i][0];
bool b1 = s[i] == '1' ? 1 : 0;
while (a != -1) {
b1 ^= pr[a].q;
if (pr[a].p != -1)
a = pr[a].p;
else
break;
}
if (la[i].size() == 1) {
if (!b1) {
vas += pr[a].l;
pr[a].q ^= 1;
}
pr[a].l = cc;
} else {
int b = la[i][1];
while (b != -1) {
b1 ^= pr[b].q;
if (pr[b].p != -1)
b = pr[b].p;
else
break;
}
if (a != b) {
if (!b1) {
if (pr[a].l < pr[b].l) {
vas += pr[a].l;
pr[a].q ^= 1;
} else {
vas += pr[b].l;
pr[b].q ^= 1;
}
kl = abs(pr[a].l - pr[b].l);
} else
kl = min(pr[a].l + pr[b].l, cc);
if (pr[a].t < pr[b].t) swap(a, b);
if (pr[a].q) pr[b].q ^= 1;
if (pr[a].t == pr[b].t) pr[a].t++;
pr[a].l = kl;
pr[b].p = a;
}
}
}
if (i)
vv.emplace_back(vv.back() + vas);
else
vv.emplace_back(vas);
}
for (int i : vv) cout << i << endl;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 ≤ i ≤ n.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset.
The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 900000;
int papa[MAXN], peso[MAXN], custo[MAXN];
int k;
bool obg[MAXN];
int find(int a) {
if (papa[a] == -1) return a;
return papa[a] = find(papa[a]);
}
bool juntos(int a, int b) { return find(a) == find(b); }
void uni(int a, int b) {
a = find(a);
b = find(b);
if (peso[a] < peso[b]) swap(a, b);
peso[a] += peso[b];
papa[b] = a;
custo[a] += custo[b];
obg[a] = obg[a] || obg[b];
}
int con(int a) {
int aa = find(a + k);
a = find(a);
if (obg[a]) return custo[a];
if (obg[aa]) return custo[aa];
return min(custo[a], custo[aa]);
}
char word[MAXN];
vector<int> lam[MAXN];
int main() {
int n;
scanf("%d%d", &n, &k);
scanf("%s", word);
for (int i = 0; i < k; i++) {
int c;
scanf("%d", &c);
for (int j = 0; j < c; j++) {
int num;
scanf("%d", &num);
lam[num - 1].emplace_back(i);
}
}
memset(papa, -1, sizeof papa);
int res = 0;
for (int i = 0; i < k; i++) custo[i] = 1;
for (int i = 0; i < n; i++) {
if (lam[i].size() == 0) {
} else if (lam[i].size() == 1) {
int a = lam[i][0];
res -= con(a);
obg[find(word[i] == '0' ? a : a + k)] = true;
res += con(a);
} else if (lam[i].size() == 2) {
int a = lam[i][0];
int b = lam[i][1];
if (!juntos(a, b) && !juntos(a, b + k)) {
res -= con(a) + con(b);
if (word[i] == '0') {
uni(a, b + k);
uni(b, a + k);
} else {
uni(a, b);
uni(a + k, b + k);
}
res += con(a);
}
}
printf("%d\n", res);
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 ≤ i ≤ n.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset.
The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int N = 6E5 + 10;
int n, k, l[N][2];
int fa[N], sc[N];
string s;
int find(int x) {
if (x == fa[x]) return x;
return fa[x] = find(fa[x]);
}
int cal(int x) {
int y = x <= k ? x + k : x - k;
int xx = find(x), yy = find(y);
if (xx == 0 || yy == 0) return sc[xx + yy];
return min(sc[xx], sc[yy]);
}
void merge(int x, int y) {
int xx = find(x), yy = find(y);
if (xx == 0)
fa[yy] = xx, sc[xx] += sc[yy];
else
fa[xx] = yy, sc[yy] += sc[xx];
}
int main() {
scanf("%d%d", &n, &k);
cin >> s;
for (int i = 1; i <= k; i++) fa[i] = i, fa[i + k] = i + k, sc[i + k] = 1;
for (int i = 1, c; i <= k; i++) {
scanf("%d", &c);
for (int j = 0, v; j < c; j++) {
scanf("%d", &v);
if (l[v][0])
l[v][1] = i;
else
l[v][0] = i;
}
}
int ans = 0;
for (int i = 1; i <= n; i++) {
if (l[i][1] == 0) {
int x = l[i][0];
if (x) {
ans -= cal(x);
if (s[i - 1] == '1')
fa[find(x + k)] = 0;
else
fa[find(x)] = 0;
ans += cal(x);
}
} else {
int x = l[i][0], y = l[i][1];
if (s[i - 1] == '1') {
if (find(x) != find(y)) {
ans -= cal(x);
ans -= cal(y);
merge(x, y);
merge(x + k, y + k);
ans += cal(x);
}
} else {
if (find(x) != find(y + k)) {
ans -= cal(x);
ans -= cal(y);
merge(x, y + k);
merge(x + k, y);
ans += cal(x);
}
}
}
cout << ans << '\n';
}
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 ≤ i ≤ n.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset.
The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const int MAX_N = 6e5 + 5;
int n, k, a[MAX_N >> 1];
vector<int> op[MAX_N >> 1];
int fa[MAX_N], val[MAX_N];
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
void unit(int x, int y) {
int fx = find(x), fy = find(y);
if (fx != fy) fa[fy] = fx, val[fx] += val[fy];
}
int get_min(int x) { return min(val[find(x)], val[find(x + k)]); }
int main() {
scanf("%d", &n), scanf("%d", &k);
for (register int i = 1; i <= n; i++) scanf("%1d", &a[i]);
for (register int i = 1; i <= k; i++) {
int x, y;
scanf("%d", &x);
while (x--) scanf("%d", &y), op[y].push_back(i);
}
for (register int i = 1; i <= 2 * k + 1; i++) fa[i] = i, val[i] = (i <= k);
int mx = 2 * k + 1;
val[mx] = inf;
int ans = 0;
for (register int i = 1; i <= n; i++) {
if (op[i].size() == 1) {
int tmp = op[i][0] + k * (a[i] == 0);
ans -= get_min(op[i][0]);
unit(tmp, mx);
ans += get_min(op[i][0]);
} else if (op[i].size() == 2) {
int x = op[i][0], y = op[i][1];
if (a[i] == 0 && find(x) != find(y + k)) {
ans -= (get_min(x) + get_min(y));
unit(x, y + k), unit(x + k, y);
ans += get_min(x);
}
if (a[i] == 1 && find(x) != find(y)) {
ans -= (get_min(x) + get_min(y));
unit(x, y), unit(x + k, y + k);
ans += get_min(x);
}
}
printf("%d\n", ans);
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 ≤ i ≤ n.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset.
The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int n, k, pre[300005 * 2];
char s[300005];
inline int find(int x) { return x == pre[x] ? x : pre[x] = find(pre[x]); }
int val[300005 * 2], tmp, m;
vector<int> ve[300005];
int ans;
inline int getmin(int x) { return min(val[find(x)], val[find(x + k)]); }
inline void merge(int x, int y) {
int xx = find(x), yy = find(y);
if (xx != yy) {
pre[xx] = yy;
val[yy] += val[xx];
}
}
int main() {
scanf("%d%d%s", &n, &k, s + 1);
for (int i = 1; i <= k; ++i) {
scanf("%d", &m);
while (m--) {
scanf("%d", &tmp);
ve[tmp].push_back(i);
}
}
for (int i = 1; i <= k; ++i) {
pre[i] = i, val[i] = 1;
pre[i + k] = i + k, val[i + k] = 0;
}
pre[2 * k + 1] = 2 * k + 1, val[2 * k + 1] = 0x3f3f3f3f;
ans = 0;
for (int i = 1; i <= n; ++i) {
if (s[i] == '0') {
if (ve[i].size() == 1) {
int id = ve[i][0];
ans -= getmin(id);
merge(id + k, 2 * k + 1);
ans += getmin(id);
} else {
int id1 = ve[i][0], id2 = ve[i][1];
if (find(id1) != find(id2 + k)) {
ans -= getmin(id1) + getmin(id2);
merge(id1, id2 + k), merge(id1 + k, id2);
ans += getmin(id1);
}
}
} else {
if (ve[i].size() == 1) {
int id = ve[i][0];
ans -= getmin(id);
merge(id, 2 * k + 1);
ans += getmin(id);
} else if (ve[i].size() == 2) {
int id1 = ve[i][0], id2 = ve[i][1];
if (find(id1) != find(id2)) {
ans -= getmin(id1) + getmin(id2);
merge(id1, id2), merge(id1 + k, id2 + k);
ans += getmin(id1);
}
}
}
printf("%d\n", ans);
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 ≤ i ≤ n.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset.
The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5, inf = 1e9;
int n, kk, s, x, k[N][2];
char ch[N];
int fa[N << 1], val[N << 1];
int find(int u) { return fa[u] == u ? fa[u] : fa[u] = find(fa[u]); }
void uni(int a, int b) {
if (find(a) == find(b)) return;
val[find(b)] += val[find(a)];
fa[find(a)] = find(b);
}
int mix(int a) { return min(val[find(a)], val[find(a + kk)]); };
int main() {
scanf("%d%d%s", &n, &kk, ch + 1);
for (int i = 1; i <= kk; i++) {
scanf("%d", &s);
while (s--) {
scanf("%d", &x);
k[x][bool(k[x][0])] = i;
}
}
for (int i = 1; i <= kk; i++) val[i] = 1, fa[i] = i, fa[i + kk] = i + kk;
fa[kk + kk + 1] = kk + kk + 1;
val[kk + kk + 1] = inf;
int ans = 0;
for (int i = 1; i <= n; i++) {
if (k[i][0] == 0) goto qwq;
if (ch[i] == '1') {
if (k[i][1]) {
if (find(k[i][0]) == find(k[i][1])) goto qwq;
ans -= mix(k[i][0]) + mix(k[i][1]);
uni(k[i][0], k[i][1]), uni(k[i][0] + kk, k[i][1] + kk);
ans += mix(k[i][1]);
} else {
ans -= mix(k[i][0]);
uni(k[i][0], kk + kk + 1);
ans += val[find(k[i][0] + kk)];
}
} else {
if (k[i][1]) {
if (find(k[i][0]) == find(k[i][1] + kk)) goto qwq;
ans -= mix(k[i][0]) + mix(k[i][1]);
uni(k[i][0], k[i][1] + kk), uni(k[i][1], k[i][0] + kk);
ans += mix(k[i][0]);
} else {
ans -= mix(k[i][0]);
uni(k[i][0] + kk, kk + kk + 1);
ans += mix(k[i][0]);
}
}
qwq:
printf("%d\n", ans);
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 ≤ i ≤ n.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset.
The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int n, m, k, x;
vector<int> vec[300010];
char c[300010];
int fa[300010 << 1], sz[300010 << 1];
int lim[300010 << 1];
int find(int x) {
if (fa[x] == x) return x;
return fa[x] = find(fa[x]);
}
int main() {
scanf("%d%d%s", &n, &k, c + 1);
for (int i = 1; i <= k; i++) {
scanf("%d", &m);
for (int j = 1; j <= m; j++) {
scanf("%d", &x);
vec[x].push_back(i);
}
}
for (int i = 1; i <= k; i++) {
fa[i * 2 - 1] = i * 2 - 1;
sz[i * 2 - 1] = 1;
fa[i * 2] = i * 2;
sz[i * 2] = 0;
lim[i * 2 - 1] = lim[i * 2] = -1;
}
int ans = 0;
for (int i = 1; i <= n; i++) {
if (vec[i].size() == 1) {
int x = vec[i][0];
int f1 = find(x * 2 - 1), f0 = find(x * 2);
int s1 = sz[f1], s0 = sz[f0];
if (lim[f1] != -1)
ans -= lim[f1] ? s1 : s0;
else if (lim[f0] != -1)
ans -= lim[f0] ? s0 : s1;
else
ans -= min(s0, s1);
lim[f1] = (c[i] == '0');
lim[f0] = lim[f1] ^ 1;
ans += lim[f1] ? s1 : s0;
} else if (vec[i].size() == 2) {
int x = vec[i][0], y = vec[i][1];
int fx1 = find(x * 2 - 1), fx0 = find(x * 2);
int sx1 = sz[fx1], sx0 = sz[fx0];
int fy1 = find(y * 2 - 1), fy0 = find(y * 2);
int sy1 = sz[fy1], sy0 = sz[fy0];
if (fx0 != fy0 && fx0 != fy1) {
if (lim[fx1] == -1)
ans -= min(sx0, sx1);
else
ans -= lim[fx1] ? sx1 : sx0;
if (lim[fy1] == -1)
ans -= min(sy0, sy1);
else
ans -= lim[fy1] ? sy1 : sy0;
if (c[i] == '1') {
fa[fx1] = fy1;
fa[fx0] = fy0;
sz[fy1] += sz[fx1];
sz[fy0] += sz[fx0];
if (lim[fx1] != -1) {
lim[fy1] = lim[fx1];
lim[fy0] = lim[fy1] ^ 1;
} else if (lim[fx0] != -1) {
lim[fy0] = lim[fx0];
lim[fy1] = lim[fy0] ^ 1;
}
} else {
fa[fx1] = fy0;
fa[fx0] = fy1;
sz[fy0] += sz[fx1];
sz[fy1] += sz[fx0];
if (lim[fx1] != -1) {
lim[fy0] = lim[fx1];
lim[fy1] = lim[fy0] ^ 1;
} else if (lim[fx0] != -1) {
lim[fy1] = lim[fx0];
lim[fy0] = lim[fy1] ^ 1;
}
}
int f1 = find(x * 2 - 1), f0 = find(x * 2);
int s1 = sz[f1], s0 = sz[f0];
if (lim[f1] == -1)
ans += min(s0, s1);
else
ans += lim[f1] ? s1 : s0;
}
}
printf("%d\n", ans);
}
return 0;
}
|
1291_E. Prefix Enlightenment
|
There are n lamps on a line, numbered from 1 to n. Each one has an initial state off (0) or on (1).
You're given k subsets A_1, …, A_k of \{1, 2, ..., n\}, such that the intersection of any three subsets is empty. In other words, for all 1 ≤ i_1 < i_2 < i_3 ≤ k, A_{i_1} ∩ A_{i_2} ∩ A_{i_3} = ∅.
In one operation, you can choose one of these k subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it's possible to make all lamps be simultaneously on using this type of operation.
Let m_i be the minimum number of operations you have to do in order to make the i first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between i+1 and n), they can be either off or on.
You have to compute m_i for all 1 ≤ i ≤ n.
Input
The first line contains two integers n and k (1 ≤ n, k ≤ 3 ⋅ 10^5).
The second line contains a binary string of length n, representing the initial state of each lamp (the lamp i is off if s_i = 0, on if s_i = 1).
The description of each one of the k subsets follows, in the following format:
The first line of the description contains a single integer c (1 ≤ c ≤ n) — the number of elements in the subset.
The second line of the description contains c distinct integers x_1, …, x_c (1 ≤ x_i ≤ n) — the elements of the subset.
It is guaranteed that:
* The intersection of any three subsets is empty;
* It's possible to make all lamps be simultaneously on using some operations.
Output
You must output n lines. The i-th line should contain a single integer m_i — the minimum number of operations required to make the lamps 1 to i be simultaneously on.
Examples
Input
7 3
0011100
3
1 4 6
3
3 4 7
2
2 3
Output
1
2
3
3
3
3
3
Input
8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2
Output
1
1
1
1
1
1
4
4
Input
5 3
00011
3
1 2 3
1
4
3
3 4 5
Output
1
1
1
1
1
Input
19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19
Output
0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5
Note
In the first example:
* For i = 1, we can just apply one operation on A_1, the final states will be 1010110;
* For i = 2, we can apply operations on A_1 and A_3, the final states will be 1100110;
* For i ≥ 3, we can apply operations on A_1, A_2 and A_3, the final states will be 1111111.
In the second example:
* For i ≤ 6, we can just apply one operation on A_2, the final states will be 11111101;
* For i ≥ 7, we can apply operations on A_1, A_3, A_4, A_6, the final states will be 11111111.
|
{
"input": [
"5 3\n00011\n3\n1 2 3\n1\n4\n3\n3 4 5\n",
"8 6\n00110011\n3\n1 3 8\n5\n1 2 5 6 7\n2\n6 8\n2\n3 5\n2\n4 7\n1\n2\n",
"19 5\n1001001001100000110\n2\n2 3\n2\n5 6\n2\n8 9\n5\n12 13 14 15 16\n1\n19\n",
"7 3\n0011100\n3\n1 4 6\n3\n3 4 7\n2\n2 3\n"
],
"output": [
"1\n1\n1\n1\n1\n",
"1\n1\n1\n1\n1\n1\n4\n4\n",
"0\n1\n1\n1\n2\n2\n2\n3\n3\n3\n3\n4\n4\n4\n4\n4\n4\n4\n5\n",
"1\n2\n3\n3\n3\n3\n3\n"
]
}
|
{
"input": [
"1 1\n1\n1\n1\n"
],
"output": [
"0\n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 6e5 + 7;
int n, k;
string s;
int l[maxn][2], r[maxn], cnt[maxn];
int getroot(int x) { return r[x] == x ? x : r[x] = getroot(r[x]); }
int calc(int x) {
int y = x + k;
x = getroot(x);
y = getroot(y);
if (x == 0 || y == 0) {
return cnt[x + y];
}
return min(cnt[x], cnt[y]);
}
void fmerge(int x, int y) {
x = getroot(x);
y = getroot(y);
if (y == 0) {
swap(x, y);
}
r[y] = x;
if (x != 0) {
cnt[x] += cnt[y];
}
}
int main() {
cin >> n >> k;
cin >> s;
for (int i = 1; i <= k; i++) {
int c;
cin >> c;
for (int j = 0; j < c; j++) {
int x;
cin >> x;
if (l[x][0] == 0)
l[x][0] = i;
else
l[x][1] = i;
}
r[i] = i;
r[i + k] = i + k;
cnt[i + k] = 1;
}
int ans = 0;
for (int i = 1; i <= n; i++) {
if (l[i][1] == 0) {
int x = l[i][0];
if (x) {
ans -= calc(x);
if (s[i - 1] == '1') {
r[getroot(x + k)] = 0;
} else {
r[getroot(x)] = 0;
}
ans += calc(x);
}
} else {
int x = l[i][0], y = l[i][1];
if (s[i - 1] == '1') {
if (getroot(x) != getroot(y)) {
ans -= calc(x);
ans -= calc(y);
fmerge(x, y);
fmerge(x + k, y + k);
ans += calc(x);
}
} else {
if (getroot(x + k) != getroot(y)) {
ans -= calc(x);
ans -= calc(y);
fmerge(x + k, y);
fmerge(x, y + k);
ans += calc(x);
}
}
}
cout << ans << endl;
}
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.