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
|
|---|---|---|---|---|---|---|
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"
]
}
|
IN-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;
_size[x] += _size[y];
if (_forced[y]) _forced[x] = _forced[y];
_r[x] += _r[y];
_b[x] += _b[y];
}
int get_ans(int 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 <= n; ++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];
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"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
constexpr long long INF = 1LL << 60;
constexpr int INF_INT = 1 << 25;
constexpr long long MOD = 998244353;
template <class T>
using vec = vector<T>;
template <class T>
using vvec = vector<vec<T>>;
template <class T>
inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T>
inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
struct Edge {
int to, cap, rev;
Edge(int _to, int _cap, int _rev) {
to = _to;
cap = _cap;
rev = _rev;
}
};
void add_edge(vector<vector<Edge>>& G, int from, int to, int cap, bool revFlag,
int revCap) {
G[from].push_back(Edge(to, cap, G[to].size()));
if (revFlag) G[to].push_back(Edge(from, revCap, G[from].size() - 1));
}
class UnionFind {
vector<int> data;
int num;
public:
UnionFind(int size) : data(size, -1), num(size) {}
bool unionSet(int x, int y) {
x = root(x);
y = root(y);
if (x != y) {
if (data[y] < data[x]) swap(x, y);
data[x] += data[y];
data[y] = x;
num--;
}
return x != y;
}
bool findSet(int x, int y) { return root(x) == root(y); }
int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
int size(int x) { return -data[root(x)]; }
int numSet() { return num; }
};
void dfs(vector<vector<Edge>>& g, vector<long long>& use, int v) {
for (auto e : g[v]) {
if (use[e.to] != -1) continue;
if (e.cap)
use[e.to] = !use[v];
else
use[e.to] = use[v];
dfs(g, use, e.to);
}
}
void solve() {
long long n, k;
cin >> n >> k;
string s;
cin >> s;
vvec<int> a(n);
for (int(i) = (int)(0); (i) < (int)(k); (i)++) {
long long c;
cin >> c;
for (int(j) = (int)(0); (j) < (int)(c); (j)++) {
long long x;
cin >> x;
a[x - 1].push_back(i);
}
}
vector<vector<Edge>> g(k + 1);
for (int(i) = (int)(0); (i) < (int)(n); (i)++) {
if (a[i].size() == 1) {
a[i].push_back(k);
if (s[i] == '0') {
add_edge(g, a[i][0], k, 1, true, 1);
} else {
add_edge(g, a[i][0], k, 0, true, 0);
}
} else if (a[i].size() == 2) {
if (s[i] == '0') {
add_edge(g, a[i][0], a[i][1], 1, true, 1);
} else {
add_edge(g, a[i][0], a[i][1], 0, true, 0);
}
}
}
vector<long long> use(k + 1, -1);
use[k] = 0;
dfs(g, use, k);
vector<long long> cost(k + 1, 0);
for (int(i) = (int)(0); (i) < (int)(k); (i)++)
if (use[i] == 1) cost[i] = 1;
UnionFind uni(k + 1);
vector<long long> cost_pre(k + 1, 0);
long long ans = 0;
for (int(i) = (int)(0); (i) < (int)(n); (i)++) {
if (a[i].size() == 0) {
cout << ans << '\n';
continue;
}
int u = uni.root(a[i][0]);
int v = uni.root(a[i][1]);
if (uni.findSet(u, v)) {
cout << ans << '\n';
continue;
}
long long pre = cost_pre[u] + cost_pre[v];
long long now = cost[u] + cost[v];
long long size = uni.size(u) + uni.size(v);
uni.unionSet(u, v);
ans -= pre;
long long root = uni.root(u);
cost[root] = now;
if (uni.findSet(root, k)) {
ans += now;
cost_pre[root] = now;
} else {
ans += min(now, size - now);
cost_pre[root] = min(now, size - now);
}
cout << ans << '\n';
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
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"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
constexpr long long INF = 1LL << 60;
constexpr int INF_INT = 1 << 25;
constexpr long long MOD = 998244353;
template <class T>
using vec = vector<T>;
template <class T>
using vvec = vector<vec<T>>;
template <class T>
inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T>
inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
struct Edge {
int to, cap, rev;
Edge(int _to, int _cap, int _rev) {
to = _to;
cap = _cap;
rev = _rev;
}
};
void add_edge(vector<vector<Edge>>& G, int from, int to, int cap, bool revFlag,
int revCap) {
G[from].push_back(Edge(to, cap, G[to].size()));
if (revFlag) G[to].push_back(Edge(from, revCap, G[from].size() - 1));
}
class UnionFind {
vector<int> data;
int num;
public:
UnionFind(int size) : data(size, -1), num(size) {}
bool unionSet(int x, int y) {
x = root(x);
y = root(y);
if (x != y) {
if (data[y] < data[x]) swap(x, y);
data[x] += data[y];
data[y] = x;
num--;
}
return x != y;
}
bool findSet(int x, int y) { return root(x) == root(y); }
int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
int size(int x) { return -data[root(x)]; }
int numSet() { return num; }
};
void dfs(vector<vector<Edge>>& g, vector<long long>& use, int v) {
for (auto e : g[v]) {
if (use[e.to] != -1) continue;
if (e.cap)
use[e.to] = !use[v];
else
use[e.to] = use[v];
dfs(g, use, e.to);
}
}
void solve() {
long long n, k;
cin >> n >> k;
string s;
cin >> s;
vvec<int> a(n);
for (int(i) = (int)(0); (i) < (int)(k); (i)++) {
long long c;
cin >> c;
for (int(j) = (int)(0); (j) < (int)(c); (j)++) {
long long x;
cin >> x;
a[x - 1].push_back(i);
}
}
vector<vector<Edge>> g(k + 1);
for (int(i) = (int)(0); (i) < (int)(n); (i)++) {
if (a[i].size() == 1) {
a[i].push_back(k);
if (s[i] == '0') {
add_edge(g, a[i][0], k, 1, true, 1);
} else {
add_edge(g, a[i][0], k, 0, true, 0);
}
} else if (a[i].size() == 2) {
if (s[i] == '0') {
add_edge(g, a[i][0], a[i][1], 1, true, 1);
} else {
add_edge(g, a[i][0], a[i][1], 0, true, 0);
}
}
}
vector<long long> use(k + 1, -1);
use[k] = 0;
dfs(g, use, k);
vector<long long> cost(k + 1, 0);
for (int(i) = (int)(0); (i) < (int)(k); (i)++)
if (use[i] == 1) cost[i] = 1;
UnionFind uni(k + 1);
vector<long long> cost_pre(k + 1, 0);
long long ans = 0;
for (int(i) = (int)(0); (i) < (int)(n); (i)++) {
if (a[i].size() == 0) {
cout << ans << '\n';
if (k >= 300000) cout << "a" << '\n';
continue;
}
int u = uni.root(a[i][0]);
int v = uni.root(a[i][1]);
if (uni.findSet(u, v)) {
cout << ans << '\n';
if (k >= 300000) cout << "b" << '\n';
continue;
}
long long pre = cost_pre[u] + cost_pre[v];
long long now = cost[u] + cost[v];
long long size = uni.size(u) + uni.size(v);
if (k >= 300000) cout << pre << " " << now << " " << size << '\n';
uni.unionSet(u, v);
ans -= pre;
long long root = uni.root(u);
cost[root] = now;
if (uni.findSet(root, k)) {
ans += now;
cost_pre[root] = now;
} else {
ans += min(now, size - now);
cost_pre[root] = min(now, size - now);
}
cout << ans << '\n';
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
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"
]
}
|
IN-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] ? fa[u] = find(fa[u]) : 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;
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][0]);
} else {
uni(k[i][0], kk + kk + 1);
}
} 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][0] + kk, k[i][1]);
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"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 300005;
int N, K, a[MAXN];
vector<int> V[MAXN];
char S[MAXN];
int f1[MAXN], v1[MAXN], f2[MAXN], chs[MAXN], ans;
int get1(int x) {
if (f1[x] != x) f1[x] = get1(f1[x]);
return f1[x];
}
void merge1(int x, int y) {
x = get1(x), y = get1(y);
if (x != y) f1[x] = y, v1[y] += v1[x];
}
int get2(int x) {
if (f2[x] != x) f2[x] = get2(f2[x]);
return f2[x];
}
inline int val(int x) {
x = get2(x);
if (chs[x] == 1)
return v1[x];
else if (chs[x] == 2)
return v1[x + K];
else
return min(v1[x], v1[x + K]);
}
void merge2(int x, int y, int k) {
if (get2(x) != get2(y)) {
ans -= val(x) + val(y);
int c = chs[get2(x)];
f2[get2(x)] = get2(y);
if (k == 1)
merge1(x, y), merge1(x + K, y + K);
else
merge1(x, y + K), merge1(x + K, y);
if (!chs[get2(y)] && c) {
if (k == 1)
chs[get2(y)] = c;
else
chs[get2(y)] = 3 - c;
}
ans += val(y);
}
}
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) f1[i] = i, v1[i] = i > K;
for (i = 1; i <= K; ++i) f2[i] = i;
for (i = 1; i <= N; ++i) {
if (V[i].size() == 1) {
if (S[i] == '0')
ans -= val(V[i][0]), chs[get2(V[i][0])] = 2, ans += val(V[i][0]);
else
ans -= val(V[i][0]), chs[get2(V[i][0])] = 1, ans += val(V[i][0]);
} else if (V[i].size() == 2) {
if (S[i] == '0')
merge2(V[i][0], V[i][1], 2);
else
merge2(V[i][0], V[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"
]
}
|
IN-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]); }
void merge(int x, int y) {
int u = find(x), v = find(y);
val[v] += val[u];
fa[u] = v;
}
bool vis[630000];
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 <= n + n; 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))) {
} 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"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
long long sz[300009], par[300009];
long long res;
long long n, k;
std::vector<long long> pos[300009];
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] = 1e18;
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"
]
}
|
IN-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, belong[N][3];
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;
sz[x] += sz[y];
}
int main() {
read(n, k);
scanf("%s", s + 1);
for (int i = 1; i <= k * 2; i++) fa[i] = i, sz[i] = (i <= k);
for (int i = 1; i <= k; i++) {
read(c);
for (int j = 1; j <= c; j++) {
int x;
read(x);
belong[x][++belong[x][0]] = i;
}
}
fa[k * 2 + 1] = k * 2 + 1, sz[k * 2 + 1] = INF;
int ans = 0;
for (int i = 1; i <= n; i++) {
if (belong[i][0] == 1) {
if (findfa(k * 2 + 1) != findfa(belong[i][1] + k * (s[i] == '0'))) {
ans -= min(sz[findfa(belong[i][1])], sz[findfa(belong[i][1] + k)]);
unionn(k * 2 + 1, belong[i][1] + k * (s[i] == '0'));
ans += min(sz[findfa(belong[i][1])], sz[findfa(belong[i][1] + k)]);
}
} else if (belong[i][0] == 2) {
if (s[i] == '0') {
if (findfa(belong[i][1]) != findfa(belong[i][2] + k)) {
ans -= min(sz[findfa(belong[i][1])], sz[findfa(belong[i][1] + k)]);
ans -= min(sz[findfa(belong[i][2])], sz[findfa(belong[i][2] + k)]);
unionn(belong[i][1], belong[i][2] + k);
unionn(belong[i][2], belong[i][1] + k);
ans += min(sz[findfa(belong[i][1])], sz[findfa(belong[i][2])]);
}
} else {
if (findfa(belong[i][1]) != findfa(belong[i][2])) {
ans -= min(sz[findfa(belong[i][1])], sz[findfa(belong[i][1] + k)]);
ans -= min(sz[findfa(belong[i][2])], sz[findfa(belong[i][2] + k)]);
unionn(belong[i][1], belong[i][2]);
unionn(belong[i][1] + k, belong[i][2] + k);
ans += min(sz[findfa(belong[i][1])], sz[findfa(belong[i][1] + 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"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int test = 0;
const int MAXN = 300009;
const long long MOD = 119 << 23 | 1;
class {
public:
vector<int> V[MAXN];
int fa[MAXN], cnt[MAXN];
int n, m;
int findfa(int x) {
if (fa[x] == x) return x;
return fa[x] = findfa(fa[x]);
}
void merge(int x, int y) {
x = findfa(x);
y = findfa(y);
if (x == y) return;
if (x > y) swap(x, y);
fa[y] = x;
cnt[x] += cnt[y];
}
void solve() {
cin >> n >> m;
string s;
cin >> s;
for (int i = 1; i <= m; ++i) {
int c;
cin >> c;
for (int j = 0; j < c; ++j) {
int v;
cin >> v;
V[v].push_back(i);
}
fa[i] = i;
fa[i + m] = i + m;
cnt[i + m] = 1;
}
int ans = 0;
for (int i = 1; i <= n; ++i) {
bool on = s[i - 1] == '1';
if (V[i].size() == 1) {
int p = V[i][0];
if (on) {
ans -= cost(p);
merge(p + m, 0);
ans += cost(p);
} else {
ans -= cost(p);
merge(p, 0);
ans += cost(p);
}
} else if (V[i].size() == 2) {
int p0 = V[i][0], p1 = V[i][1];
if (on) {
if (findfa(p0) != findfa(p1)) {
ans -= cost(p0);
ans -= cost(p1);
merge(p0, p1);
merge(p0 + m, p1 + m);
ans += cost(p0);
} else {
ans -= min(cost(p0), cost(p1));
merge(p0, p1);
merge(p0 + m, p1 + m);
ans += cost(p0);
}
} else {
if (findfa(p0) != findfa(p1 + m)) {
ans -= cost(p0);
ans -= cost(p1);
merge(p0, p1 + m);
merge(p0 + m, p1);
ans += cost(p0);
} else {
ans -= min(cost(p0), cost(p1));
merge(p0, p1 + m);
merge(p0 + m, p1);
ans += min(cost(p0), cost(p1));
}
}
}
cout << ans << '\n';
}
}
int cost(int x) {
int y;
if (x <= m) {
y = m + x;
} else {
y = x - m;
}
x = findfa(x);
y = findfa(y);
if (x == 0)
return cnt[y];
else if (y == 0)
return cnt[x];
return min(cnt[x], cnt[y]);
}
} NSPACE;
int main() {
ios_base::sync_with_stdio(false);
cout.tie(0);
cin.tie(0);
NSPACE.solve();
}
|
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"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long llINF = 2e18, llMOD = 1e9 + 7;
const int INF = 2e9, MOD = 1e9 + 7, P = 179, N = 2e3 + 1, K = 23000, L = 18;
const long double EPS = 1e-6, ldINF = 1e18, PI = acos(-1);
template <typename T>
inline void sort_uniq(vector<T>& v) {
sort(v.begin(), v.end());
v.resize(unique(v.begin(), v.end()) - v.begin());
}
template <typename T>
inline int sign(T a) {
if (a > 0) {
return 1;
} else if (a == 0) {
return 0;
} else {
return -1;
}
}
template <typename T, typename P>
bool eq(T a, P b) {
return abs(a - b) <= EPS;
}
const char sep[] = " ";
template <typename T, typename P>
istream& operator>>(istream& in, pair<T, P>& p) {
in >> p.first >> p.second;
return in;
}
template <typename T>
void in_seg(istream& in, T* dst_begin, T* dst_end) {
while (dst_begin != dst_end) {
in >> (*dst_begin);
++dst_begin;
}
}
template <typename T>
istream& operator>>(istream& in, vector<T>& v) {
for (T& i : v) {
in >> i;
}
return in;
}
template <typename T, typename P>
ostream& operator<<(ostream& out, pair<T, P> const& p) {
out << p.first << ' ' << p.second;
return out;
}
template <typename T>
ostream& operator<<(ostream& out, vector<T> const& v) {
for (T const& i : v) {
out << i << sep;
}
if (!v.empty()) {
out << '\n';
}
return out;
}
template <typename T>
ostream& operator<<(ostream& out, vector<vector<T>> const& v) {
for (vector<T> const& i : v) {
out << i;
}
return out;
}
void set_precision(ostream& out, int val) {
out.setf(out.fixed, out.floatfield);
out.precision(val);
}
struct edge {
int v, u, x;
};
class Dsu {
int n;
vector<int> p, sz, p_xor, r_st, ans;
vector<char> active;
int cur_ans = 0;
int dsu_get(int v) {
if (v == p[v]) {
return v;
}
int par = p[v], res = dsu_get(p[v]);
p_xor[v] ^= p_xor[par];
p[v] = res;
return res;
}
int get_vrt_ans(int v) {
v = dsu_get(v);
if (r_st[v] == 0) {
return ans[v];
} else if (r_st[v] == 1) {
return sz[v] - ans[v];
} else {
return min(ans[v], sz[v] - ans[v]);
}
}
void dsu_unite(int v, int u, int x) {
int p_v = dsu_get(v);
int p_u = dsu_get(u);
if (p_v == p_u) {
return;
}
if (active[p_v]) {
cur_ans -= get_vrt_ans(p_v);
}
if (active[p_u]) {
cur_ans -= get_vrt_ans(p_u);
}
if (sz[p_v] < sz[p_u]) {
swap(v, u);
swap(p_v, p_u);
}
p_xor[p_u] = p_xor[u] ^ p_xor[v] ^ x;
if (r_st[u] != 2) {
if (r_st[v] != 2) {
} else {
r_st[p_v] = r_st[p_u] ^ p_xor[p_u];
}
}
if (p_xor[p_u]) {
ans[p_v] += sz[p_u] - ans[p_u];
} else {
ans[p_v] += ans[p_u];
}
p[p_u] = p_v;
sz[p_v] += sz[p_u];
cur_ans += get_vrt_ans(p_v);
active[p_v] = true;
}
public:
Dsu(vector<int> r_st)
: n(r_st.size()),
p(n),
sz(n, 1),
p_xor(n),
r_st(r_st),
ans(n),
active(n, false) {
for (int i = 0; i < n; i++) {
p[i] = i;
}
}
int get_ans() { return cur_ans; }
void activate(int v) {
v = dsu_get(v);
if (!active[v]) {
cur_ans += get_vrt_ans(v);
active[v] = true;
}
}
void unite(int v, int u, int x) { dsu_unite(v, u, x); }
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
string s;
vector<vector<int>> v_mask(n);
vector<int> mask_st(k, 2);
vector<vector<edge>> e(n);
cin >> s;
for (int i = 0, c; i < k; i++) {
cin >> c;
for (int j = 0, x; j < c; j++) {
cin >> x;
v_mask[x - 1].push_back(i);
}
}
int ban_bit = n;
for (int i = 0; i < n; i++) {
if (v_mask[i].size() == 1) {
if (s[i] == '0') {
mask_st[v_mask[i][0]] = 1;
} else {
mask_st[v_mask[i][0]] = 0;
}
} else if (v_mask[i].size() == 2) {
int v = v_mask[i][0];
int u = v_mask[i][1];
if (s[i] == '0') {
e[i].push_back({v, u, 1});
} else {
e[i].push_back({v, u, 0});
}
} else {
assert(v_mask[i].size() == 0);
}
}
Dsu dsu(mask_st);
for (int i = 0; i < n; i++) {
for (auto [v, u, x] : e[i]) {
dsu.unite(v, u, x);
}
if (v_mask[i].size() == 1) {
dsu.activate(v_mask[i][0]);
}
cout << dsu.get_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"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const long long maxn = 300005;
const long long inf = 1ll << 50;
int ans = 0;
bool init[maxn];
int par[maxn], sz[maxn], cnt[maxn][2], col[maxn];
vector<int> vec[maxn];
int find(int x) {
if (par[x] == x) {
return x;
}
par[x] = find(par[x]);
col[x] = col[x] ^ col[par[x]];
return par[x];
}
int get_val(int x) { return min(cnt[x][0], cnt[x][1]); }
void merge(int X, int Y, bool same) {
int x = find(X);
int y = find(Y);
if (x == y) {
return;
}
if (sz[x] > sz[y]) {
swap(x, y);
}
sz[y] += sz[x];
par[x] = y;
bool parity = (same ^ (col[X] == col[Y]));
ans -= get_val(x);
ans -= get_val(y);
if (parity) {
col[x] = 1;
cnt[y][0] += cnt[x][1];
cnt[y][1] += cnt[x][0];
} else {
col[x] = 0;
cnt[y][0] += cnt[x][0];
cnt[y][1] += cnt[x][1];
}
ans += get_val(y);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long n, k;
cin >> n >> k;
sz[0] = (1 << 28);
cnt[0][0] = (1 << 28);
for (int i = 1; i <= k; i++) {
par[i] = i;
sz[i] += 1;
cnt[i][0] = 1;
cnt[i][1] = 0;
col[i] = 0;
}
string s;
cin >> s;
for (int i = 1; i <= n; i++) {
init[i] = s[i - 1] - '0';
}
for (int i = 1; i <= k; i++) {
long long c;
cin >> c;
int a;
for (int j = 0; j < c; j++) {
cin >> a;
vec[a].push_back(i);
}
}
for (int i = 1; i <= n; i++) {
assert(vec[i].size() <= 2);
if (vec[i].size() == 0) {
cout << ans << "\n";
assert(init[i] == 1);
continue;
}
if (vec[i].size() == 1) {
vec[i].push_back(0);
}
merge(vec[i][0], vec[i][1], init[i] == 1);
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"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long llINF = 2e18, llMOD = 1e9 + 7;
const int INF = 2e9, MOD = 1e9 + 7, P = 179, N = 2e3 + 1, K = 23000, L = 18;
const long double EPS = 1e-6, ldINF = 1e18, PI = acos(-1);
template <typename T>
inline void sort_uniq(vector<T>& v) {
sort(v.begin(), v.end());
v.resize(unique(v.begin(), v.end()) - v.begin());
}
template <typename T>
inline int sign(T a) {
if (a > 0) {
return 1;
} else if (a == 0) {
return 0;
} else {
return -1;
}
}
template <typename T, typename P>
bool eq(T a, P b) {
return abs(a - b) <= EPS;
}
const char sep[] = " ";
template <typename T, typename P>
istream& operator>>(istream& in, pair<T, P>& p) {
in >> p.first >> p.second;
return in;
}
template <typename T>
void in_seg(istream& in, T* dst_begin, T* dst_end) {
while (dst_begin != dst_end) {
in >> (*dst_begin);
++dst_begin;
}
}
template <typename T>
istream& operator>>(istream& in, vector<T>& v) {
for (T& i : v) {
in >> i;
}
return in;
}
template <typename T, typename P>
ostream& operator<<(ostream& out, pair<T, P> const& p) {
out << p.first << ' ' << p.second;
return out;
}
template <typename T>
ostream& operator<<(ostream& out, vector<T> const& v) {
for (T const& i : v) {
out << i << sep;
}
if (!v.empty()) {
out << '\n';
}
return out;
}
template <typename T>
ostream& operator<<(ostream& out, vector<vector<T>> const& v) {
for (vector<T> const& i : v) {
out << i;
}
return out;
}
void set_precision(ostream& out, int val) {
out.setf(out.fixed, out.floatfield);
out.precision(val);
}
struct edge {
int v, u, x;
};
class Dsu {
int n;
vector<int> p, sz, p_xor, r_st, ans;
vector<char> active;
int cur_ans = 0;
int dsu_get(int v) {
if (v == p[v]) {
return v;
}
int par = p[v], res = dsu_get(p[v]);
p_xor[v] ^= p_xor[par];
p[v] = res;
return res;
}
int get_vrt_ans(int v) {
v = dsu_get(v);
if (r_st[v] == 0) {
return ans[v];
} else if (r_st[v] == 1) {
return sz[v] - ans[v];
} else {
return min(ans[v], sz[v] - ans[v]);
}
}
void dsu_unite(int v, int u, int x) {
int p_v = dsu_get(v);
int p_u = dsu_get(u);
if (p_v == p_u) {
assert(x == (p_xor[u] ^ p_xor[v]));
return;
}
if (active[p_v]) {
cur_ans -= get_vrt_ans(p_v);
}
if (active[p_u]) {
cur_ans -= get_vrt_ans(p_u);
}
if (sz[p_v] < sz[p_u]) {
swap(v, u);
swap(p_v, p_u);
}
p_xor[p_u] = p_xor[u] ^ p_xor[v] ^ x;
if (r_st[p_u] != 2) {
if (r_st[p_v] != 2) {
assert(r_st[p_v] == (r_st[p_u] ^ p_xor[p_u]));
} else {
r_st[p_v] = r_st[p_u] ^ p_xor[p_u];
}
}
if (p_xor[p_u]) {
ans[p_v] += sz[p_u] - ans[p_u];
} else {
ans[p_v] += ans[p_u];
}
p[p_u] = p_v;
sz[p_v] += sz[p_u];
cur_ans += get_vrt_ans(p_v);
active[p_v] = true;
}
public:
Dsu(vector<int> r_st)
: n(r_st.size()),
p(n),
sz(n, 1),
p_xor(n),
r_st(r_st),
ans(n),
active(n, false) {
for (int i = 0; i < n; i++) {
p[i] = i;
}
}
int get_ans() { return cur_ans; }
void activate(int v) {
int v_prv = v;
v = dsu_get(v);
if (!active[v]) {
if (r_st[v] == 2 && r_st[v_prv] != 2) {
r_st[v] = p_xor[v_prv] ^ r_st[v_prv];
} else if (r_st[v_prv] != 2) {
assert(r_st[v] == (p_xor[v_prv] ^ r_st[v_prv]));
}
cur_ans += get_vrt_ans(v);
active[v] = true;
}
}
void unite(int v, int u, int x) { dsu_unite(v, u, x); }
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
string s;
vector<vector<int>> v_mask(n);
vector<int> mask_st(k, 2);
vector<vector<edge>> e(n);
cin >> s;
for (int i = 0, c; i < k; i++) {
cin >> c;
for (int j = 0, x; j < c; j++) {
cin >> x;
v_mask[x - 1].push_back(i);
}
}
int ban_bit = n;
for (int i = 0; i < n; i++) {
if (v_mask[i].size() == 1) {
if (s[i] == '0') {
mask_st[v_mask[i][0]] = 1;
} else {
mask_st[v_mask[i][0]] = 0;
}
} else if (v_mask[i].size() == 2) {
int v = v_mask[i][0];
int u = v_mask[i][1];
if (s[i] == '0') {
e[i].push_back({v, u, 1});
} else {
e[i].push_back({v, u, 0});
}
} else {
assert(v_mask[i].size() == 0);
}
}
Dsu dsu(mask_st);
for (int i = 0; i < n; i++) {
for (auto [v, u, x] : e[i]) {
dsu.unite(v, u, x);
}
if (v_mask[i].size() == 1) {
dsu.activate(v_mask[i][0]);
}
cout << dsu.get_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"
]
}
|
IN-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) {
a = find(a);
int aa = find(a + k);
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"
]
}
|
IN-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 = 300000;
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"
]
}
|
IN-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) {
a = find(a);
int aa = find(a + k);
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], 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"
]
}
|
IN-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], tot[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[n + 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 <= n; i++)
if (!d[i]) dfs(i, n + 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);
uf.join(u, v);
if (u != v) {
ans -= tot[u] + tot[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]);
tot[u] = tot[v] = 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"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long n, k, pa[600010], ct[600010], siz[600010], ans = 0;
vector<long long> g[300010];
string s;
long long find(long long x) {
if (x == pa[x]) return x;
pa[x] = find(pa[x]);
return pa[x];
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
cin >> s;
for (long long i = 0; i < k; i++) {
long long t;
cin >> t;
for (long long j = 0; j < t; j++) {
long long x;
cin >> x;
--x;
g[x].push_back(i);
}
}
for (long long i = 0; i < n; i++) {
ct[2 * i + 1] = 1;
ct[2 * i] = 0;
pa[2 * i] = 2 * i;
pa[2 * i + 1] = 2 * i + 1;
siz[2 * i + 1] = 1;
siz[2 * i] = 1;
}
for (long long i = 0; i < n; i++) {
long long a, b;
;
if (((long long)g[i].size()) == 2) {
a = g[i][0];
b = g[i][1];
long long x1 = find(2 * a), y1 = find(b * 2), x2 = find(2 * a + 1),
y2 = find(2 * b + 1);
if (s[i] == '0') swap(x1, x2);
if (x1 == y1) {
cout << ans << '\n';
continue;
}
ans -= (min(ct[x1], ct[x2]) + min(ct[y1], ct[y2]));
;
if (siz[x1] > siz[y1]) swap(x1, y1), swap(x2, y2);
siz[y1] += siz[x1];
pa[x1] = y1;
ct[y1] += ct[x1];
ct[x1] = 0;
siz[y2] += siz[x2];
pa[x2] = y2;
ct[y2] += ct[x2];
ct[x2] = 0;
ans += min(ct[y1], ct[y2]);
} else {
if (((long long)g[i].size()) == 0) {
cout << ans << '\n';
continue;
}
long long a = g[i][0], x, delx;
if (s[i] == '1')
x = find(2 * a), delx = find(2 * a + 1);
else
x = find(2 * a + 1), delx = find(2 * a);
ans -= min(ct[x], ct[delx]);
;
ct[delx] = 1e10;
ans += ct[x];
}
cout << ans << '\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"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const long long maxn = 300005;
const long long inf = 1ll << 50;
int ans = 0;
bool init[maxn];
int par[maxn], sz[maxn], cnt[maxn][2], col[maxn];
vector<int> vec[maxn];
int find(int x) {
if (par[x] == x) {
return x;
}
par[x] = find(par[x]);
col[x] = col[x] ^ col[par[x]];
return par[x];
}
int get_val(int x) { return min(cnt[x][0], cnt[x][1]); }
void merge(int X, int Y, bool same) {
int x = find(X);
int y = find(Y);
if (x == y) {
return;
}
if (sz[x] > sz[y]) {
swap(x, y);
}
sz[y] += sz[x];
par[x] = y;
bool parity = (same ^ col[X] ^ col[Y]);
ans -= get_val(x);
ans -= get_val(y);
if (!parity) {
col[x] = 1;
cnt[y][0] += cnt[x][1];
cnt[y][1] += cnt[x][0];
} else {
col[x] = 0;
cnt[y][0] += cnt[x][0];
cnt[y][1] += cnt[x][1];
}
ans += get_val(y);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long n, k;
cin >> n >> k;
sz[0] = (1 << 28);
cnt[0][0] = (1 << 28);
for (int i = 1; i <= k; i++) {
par[i] = i;
sz[i] += 1;
cnt[i][0] = 1;
cnt[i][1] = 0;
}
string s;
cin >> s;
for (int i = 1; i <= n; i++) {
init[i] = s[i - 1] - '0';
}
for (int i = 1; i <= k; i++) {
long long c;
cin >> c;
int a;
for (int j = 0; j < c; j++) {
cin >> a;
vec[a].push_back(i);
}
}
for (int i = 1; i <= n; i++) {
assert(vec[i].size() <= 2);
if (vec[i].size() == 0) {
cout << ans << "\n";
assert(init[i] == 1);
continue;
}
if (vec[i].size() == 1) {
vec[i].push_back(0);
}
merge(vec[i][0], vec[i][1], init[i] == 1);
cout << ans << "\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"
]
}
|
IN-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]); }
void merge(int x, int y) {
int u = find(x), v = find(y);
if (u == v) return;
val[v] += val[u];
fa[u] = v;
}
bool vis[630000];
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))) {
} 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"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 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] + (s[i] == '1' ? k : 0);
ans -= val(x);
merge(x, 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"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int val[300000 + 100], sum[300000 + 100][2], head[300000 + 100];
int f[300000 + 100];
vector<int> V[300000 + 100];
int n, k;
int ans;
int findd(int x) {
if (f[x] == x) return x;
int F = f[x];
f[x] = findd(f[x]);
val[x] = val[x] ^ val[F];
return f[x];
}
char s[300000 + 100];
void init() {
for (int i = 0; i <= max(n, k); i++) f[i] = i;
for (int i = 1; i <= max(n, k); i++) {
sum[i][0] = 0;
sum[i][1] = 1;
}
memset(head, -1, sizeof(head));
}
int get(int x) {
if (head[x] == -1) return min(sum[x][0], sum[x][1]);
return sum[x][head[x]];
}
void build(int x, int y, int v) {
int X = findd(x);
int Y = findd(y);
if (X != Y) {
ans -= get(X);
ans -= get(Y);
sum[Y][0] += sum[X][v ^ val[x] ^ val[y]];
sum[Y][1] += sum[X][1 ^ v ^ val[x] ^ val[y]];
if (head[X] != -1 && head[Y] == -1) {
head[Y] = v ^ val[x] ^ val[y] ^ head[X];
}
val[X] = val[x] ^ val[y] ^ v;
f[X] = Y;
ans += get(Y);
}
}
void work(int x, int v) {
int X = findd(x);
ans -= get(X);
if (head[X] == -1) head[X] = v ^ val[x];
ans += get(X);
}
int main() {
int T;
cin >> n >> k;
scanf("%s", s + 1);
for (int i = 1; i <= k; i++) {
scanf("%d", &T);
int cpy = T;
int x;
while (T--) {
scanf("%d", &x);
if (k == 300000 && x == 3) printf("eferfceceacaesdc %d", cpy);
V[x].push_back(i);
}
}
init();
for (int i = 1; i <= n; i++) {
if (V[i].size() == 1) {
work(V[i][0], 1 ^ (s[i] - '0'));
} else if (V[i].size() == 2) {
build(V[i][0], V[i][1], 1 ^ (s[i] - '0'));
}
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"
]
}
|
IN-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;
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() == 2) {
int 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 (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"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int ans = 0;
int val[600010], fa[600010];
int have[600010][2];
char str[600010];
int n, k;
int find(int x) { return x == fa[x] ? x : (fa[x] = find(fa[x])); }
int other(int u) { return u <= k ? u + k : u - k; }
void update(int u, int x, int y) {
ans -= min(val[u], val[other(u)]);
ans += min(x, y);
val[u] = x;
val[other(u)] = y;
}
int main() {
scanf("%d%d", &n, &k);
scanf("%s", str + 1);
for (int i = 1; i <= k; i++) {
int c;
scanf("%d", &c);
while (c--) {
int a;
scanf("%d", &a);
if (have[a][0])
have[a][1] = i;
else
have[a][0] = i;
}
val[i] = 0, val[i + k] = 1;
fa[i] = i, fa[i + k] = i + k;
}
for (int i = 1; i <= n; i++) {
if (str[i] == '0') {
int x = find(have[i][0]), y = find(have[i][1]);
if (!y)
update(x, 1e7, val[other(x)]);
else {
int uy = val[y], vy = val[other(y)];
update(y, 0, 0);
fa[y] = other(x);
fa[other(y)] = x;
update(x, val[x] + vy, val[other(x)] + uy);
}
} else {
int x = find(have[i][0]), y = find(have[i][1]);
if (!x) continue;
if (!y)
update(x, val[x], 1e7);
else {
int uy = val[y], vy = val[other(y)];
update(y, 0, 0);
fa[y] = x;
fa[other(y)] = other(x);
update(x, val[x] + uy, val[other(x)] + vy);
}
}
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"
]
}
|
IN-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] ? fa[u] = find(fa[u]) : 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;
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][0]);
} else {
ans -= mix(k[i][0]);
uni(k[i][0], kk + kk + 1);
}
} 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][0] + kk, k[i][1]);
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"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef unsigned long long llu;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<int, pii> piii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<string, int> psi;
typedef pair<char, int> pci;
typedef pair<int, char> pic;
const int MOD = 1e9 + 7;
const long double PI = 3.141592653589793238462643383279502884197;
ll fac[1] = {1}, inv[1] = {1};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll mp(ll a,ll b){ll ret=1;while(b){if(b&1)ret=ret*a%MOD;a=a*a%MOD;b>>=1;}return ret;}
ll cmb(ll r, ll 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[b]);
}
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 (sz(v[i]) == 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 (sz(v[i]) == 2) {
merge(v[i][0], v[i][1]);
merge(v[i][0] + k, v[i][1] + k);
}
else if (sz(v[i]) == 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);
}
}
// author: rdd6584
|
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"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
template <class V>
int get_p(V& p, int j) {
if (p[j] == j) return j;
if (p[j] < 0) return p[j] = -get_p(p, -p[j]);
return p[j] = get_p(p, p[j]);
}
int get_min(bool a_j, int S_j, bool coa_j, int coS_j) {
if (!coa_j) return S_j;
if (!a_j) return coS_j;
return std::min(S_j, coS_j);
}
template <class O, class Z, class A, class B, class C, class D, class E>
void debug(O& out, const Z& p, const A& J, const B& a, const C& coa, const D& S,
const E& coS) {
out << "p:";
for (auto x : p) out << " " << x;
out << std::endl;
out << "J:";
for (auto x : J) {
out << " {";
for (auto y : x) out << " " << y;
out << " }";
}
out << std::endl;
out << " a:";
for (auto x : a) out << " " << x;
out << std::endl;
out << "coa:";
for (auto x : coa) out << " " << x;
out << std::endl;
out << " S:";
for (auto x : S) out << " " << x;
out << std::endl;
out << "coS:";
for (auto x : coS) out << " " << x;
out << std::endl;
}
template <class O, class Z, class X, class A, class B, class C, class D,
class E>
void debug(O& out, int i, int R, Z& p, const X& x, const A& J, const B& a,
const C& coa, const D& S, const E& coS) {
for (auto y : J[i])
out << x[i] << ' ' << R << ' ' << y << ' ' << p[y] << ' '
<< p[abs(get_p(p, y))] << ' ' << a[abs(get_p(p, y))] << ' '
<< coa[abs(get_p(p, y))] << ' ' << S[abs(get_p(p, y))] << ' '
<< coS[abs(get_p(p, y))] << ' ';
}
void proc() {
int n, k;
std::cin >> n >> k;
std::vector<bool> x(n);
for (int i = 0; i < n; i++) {
char c;
std::cin >> c;
x[i] = c % 2;
}
std::vector<std::vector<int> > J(n);
std::vector<int> p(k + 1);
for (int j = 1; j <= k; j++) {
p[j] = j;
int c;
std::cin >> c;
while (c--) {
int i;
std::cin >> i;
J[i - 1].push_back(j);
}
}
std::vector<int> S(k + 1, 1);
std::vector<int> coS(k + 1, 0);
std::vector<bool> a(k + 1, true);
std::vector<bool> coa(k + 1, true);
int R = 0;
for (int i = 0; i < n; i++) {
if (i == 10781) debug(std::cout, i, R, p, x, J, a, coa, S, coS);
if (J[i].size() == 0) {
if (n < 1000) std::cout << R << std::endl;
continue;
}
int j = J[i][0];
int p_j = get_p(p, j);
int abs_p_j = std::abs(p_j);
bool a_j = a[abs_p_j];
bool coa_j = coa[abs_p_j];
int S_j = S[abs_p_j];
int coS_j = coS[abs_p_j];
if (J[i].size() == 1) {
a[abs_p_j] = a_j && !x[i];
coa[abs_p_j] = coa_j && x[i];
int m_0 = get_min(a_j, S_j, coa_j, coS_j);
int m_1 = get_min(a[abs_p_j], S_j, coa[abs_p_j], coS_j);
R += m_1 - m_0;
if (n < 1000) std::cout << R << std::endl;
continue;
}
int l = J[i][1];
int p_l = get_p(p, l);
int abs_p_l = std::abs(p_l);
bool a_l = a[abs_p_l];
bool coa_l = coa[abs_p_l];
int S_l = S[abs_p_l];
int coS_l = coS[abs_p_l];
if (abs_p_j < abs_p_l) {
std::swap(p_j, p_l);
std::swap(abs_p_j, abs_p_l);
std::swap(a_j, a_l);
std::swap(coa_j, coa_l);
std::swap(S_j, S_l);
std::swap(coS_j, coS_l);
}
a[abs_p_j] = a_j & coa_l ^
(a_j & coa_l ^ a_j & a_l) & (x[i] ^ (p_j > 0) ^ (p_l > 0));
coa[abs_p_j] = coa_j & a_l ^ (coa_j & a_l ^ coa_j & coa_l) &
(x[i] ^ (p_j > 0) ^ (p_l > 0));
int m_0 = get_min(a_j, S_j, coa_j, coS_j);
if (abs_p_j != abs_p_l) {
m_0 += get_min(a_l, S_l, coa_l, coS_l);
bool t = x[i] ^ (p_j > 0) ^ (p_l > 0);
p[abs_p_l] = t ? abs_p_j : -abs_p_j;
S[abs_p_j] += t ? S_l : coS_l;
coS[abs_p_j] += t ? coS_l : S_l;
}
int m_1 = get_min(a[abs_p_j], S[abs_p_j], coa[abs_p_j], coS[abs_p_j]);
R += m_1 - m_0;
if (i == 10781) debug(std::cout, i, R, p, x, J, a, coa, S, coS);
if (n < 1000 || i >= 10781) std::cout << R << std::endl;
}
}
int main() {
std::cin.tie(0);
std::cin.sync_with_stdio(false);
int t = 1;
while (t--) proc();
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"
]
}
|
IN-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 if (!sister[b])
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"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int inf = (int)1e9 + 5;
const int N = 600010;
class DSU {
public:
int p[N], cost[N];
int n;
int ans = 0;
DSU(int _n) : n(_n) {
for (int i = 0; i < n; i++) {
cost[2 * i + 1] = 1;
cost[2 * i] = 0;
}
cost[2 * n] = inf;
cost[2 * n + 1] = 0;
for (int i = 0; i < 2 * n + 2; i++) {
p[i] = i;
}
}
int find(int x) { return (x == p[x] ? x : (p[x] = find(p[x]))); }
void unite(int x, int y, bool same) {
int x_true = find(2 * x + 1), y_true = find(2 * y + 1);
int x_false = find(2 * x), y_false = find(2 * y);
if (same) {
if (x_true != y_true) {
assert(x_false != y_false);
ans -= min(cost[x_true], cost[x_false]);
ans -= min(cost[y_true], cost[y_false]);
p[x_true] = y_true;
cost[y_true] += cost[x_true];
p[x_false] = y_false;
cost[y_false] += cost[x_false];
ans += min(cost[y_true], cost[y_false]);
} else {
assert(x_false == y_false);
}
} else {
if (x_true != y_false) {
assert(x_false != y_true);
ans -= min(cost[x_true], cost[x_false]);
ans -= min(cost[y_true], cost[y_false]);
p[x_true] = y_false;
cost[y_false] += cost[x_true];
p[x_false] = y_true;
cost[y_true] += cost[x_false];
ans += min(cost[y_true], cost[y_false]);
} else {
assert(x_false == y_true);
}
}
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
string s;
cin >> s;
vector<vector<int>> has(n);
for (int i = 0; i < k; i++) {
int size;
cin >> size;
while (size--) {
int x;
cin >> x;
--x;
has[x].push_back(i);
}
}
DSU dsu(n);
int dummy = n;
for (int i = 0; i < n; i++) {
if ((int)has[i].size() == 2) {
dsu.unite(has[i][0], has[i][1], s[i] == '1');
} else if ((int)has[i].size() == 1) {
dsu.unite(has[i].back(), dummy, s[i] == '0');
} else {
assert(s[i] == '1');
}
cout << dsu.ans << '\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"
]
}
|
IN-CORRECT
|
cpp
|
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
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...);}
typedef long long LL;
const int N = 300005;
const int INF = 1e9;
int n, k, c, belong[N][3];
char s[N];
int fa[N<<1];
LL 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; sz[x] += sz[y];
}
int main(){
read(n, k);
scanf("%s", s+1);
for(int i = 1; i <= k * 2; i++) fa[i] = i, sz[i] = (i <= k);
for(int i = 1; i <= k; i++){
read(c);
for(int j = 1; j <= c; j++){
int x; read(x);
belong[x][++belong[x][0]] = i;
}
}
fa[k*2+1] = k*2+1, sz[k*2+1] = INF;
LL ans = 0;
for(int i = 1; i <= n; i++){
if(belong[i][0] == 1){
if(findfa(k*2+1) != findfa(belong[i][1] + k * (s[i] == '0'))){
ans -= min(sz[findfa(belong[i][1])], sz[findfa(belong[i][1]+k)]);
unionn(k*2+1, belong[i][1] + k * (s[i] == '0'));
ans += min(sz[findfa(belong[i][1])], sz[findfa(belong[i][1]+k)]);
}
}
else if(belong[i][0] == 2){
if(s[i] == '0'){
if(findfa(belong[i][1]) != findfa(belong[i][2]+k)){
ans -= min(sz[findfa(belong[i][1])], sz[findfa(belong[i][1]+k)]);
ans -= min(sz[findfa(belong[i][2])], sz[findfa(belong[i][2]+k)]);
unionn(belong[i][1], belong[i][2]+k);
unionn(belong[i][2], belong[i][1]+k);
ans += min(sz[findfa(belong[i][1])], sz[findfa(belong[i][2])]);
}
}
else{
if(findfa(belong[i][1]) != findfa(belong[i][2])){
ans -= min(sz[findfa(belong[i][1])], sz[findfa(belong[i][1]+k)]);
ans -= min(sz[findfa(belong[i][2])], sz[findfa(belong[i][2]+k)]);
unionn(belong[i][1], belong[i][2]);
unionn(belong[i][1]+k, belong[i][2]+k);
ans += min(sz[findfa(belong[i][1])], sz[findfa(belong[i][1]+k)]);
}
}
}
printf("%lld\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"
]
}
|
IN-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), swap(x, y);
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[ff(x)] = ff(y);
}
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"
]
}
|
IN-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) {
a = find(a);
int aa = find(a + k);
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) {
res -= con(lam[i][0]);
obg[find(word[i] == '0' ? lam[i][0] : lam[i][0] + k)] = true;
res += con(lam[i][0]);
} else if (lam[i].size() == 2) {
int a = lam[i][0], b = lam[i][1];
if (!juntos(b, a + k) && !juntos(a + k, b + k) && !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"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 300005;
int N, K, a[MAXN];
vector<int> V[MAXN];
char S[MAXN];
int f1[MAXN], v1[MAXN], f2[MAXN], chs[MAXN], ans;
int get1(int x) {
if (f1[x] != x) f1[x] = get1(f1[x]);
return f1[x];
}
void merge1(int x, int y) {
x = get1(x), y = get1(y);
if (x != y) f1[x] = y, v1[y] += v1[x];
}
int get2(int x) {
if (f2[x] != x) f2[x] = get2(f2[x]);
return f2[x];
}
inline int val(int x) {
x = get2(x);
if (chs[x] == 1)
return v1[x];
else if (chs[x] == 2)
return v1[x + K];
else
return min(v1[x], v1[x + K]);
}
void merge2(int x, int y, int k) {
if (get2(x) != get2(y)) {
ans -= val(x) + val(y);
int qwq = 0;
if (chs[get2(x)]) {
if (chs[get2(x)] == 1)
qwq = get2(x);
else
qwq = get2(x) + K;
}
f2[get2(x)] = get2(y);
if (k == 1)
merge1(x, y), merge1(x + K, y + K);
else
merge1(x, y + K), merge1(x + K, y);
if (!chs[get2(y)] && qwq) {
if (get1(qwq) == get2(y))
chs[get2(y)] = 1;
else
chs[get2(y)] = 2;
}
ans += val(y);
}
}
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) f1[i] = i, v1[i] = i > K;
for (i = 1; i <= K; ++i) f2[i] = i;
for (i = 1; i <= N; ++i) {
if (V[i].size() == 1) {
if (S[i] == '0')
ans -= val(V[i][0]), chs[get2(V[i][0])] = 2, ans += val(V[i][0]);
else
ans -= val(V[i][0]), chs[get2(V[i][0])] = 1, ans += val(V[i][0]);
} else if (V[i].size() == 2) {
if (S[i] == '0')
merge2(V[i][0], V[i][1], 2);
else
merge2(V[i][0], V[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"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int val[300000 + 100], sum[300000 + 100][2], head[300000 + 100];
int f[300000 + 100];
vector<int> V[300000 + 100];
int n, k;
int ans;
int findd(int x) {
if (f[x] == x) return x;
int F = f[x];
f[x] = findd(f[x]);
val[x] = val[x] ^ val[F];
return f[x];
}
char s[300000 + 100];
void init() {
for (int i = 1; i <= n; i++) f[i] = i;
for (int i = 1; i <= n; i++) {
sum[i][0] = 0;
sum[i][1] = 1;
}
memset(head, -1, sizeof(head));
}
int get(int x) {
if (head[x] == -1) return min(sum[x][0], sum[x][1]);
return sum[x][head[x]];
}
void build(int x, int y, int v) {
int X = findd(x);
int Y = findd(y);
if (X != Y) {
ans -= get(X);
ans -= get(Y);
sum[Y][0] += sum[X][v ^ val[x] ^ val[y]];
sum[Y][1] += sum[X][1 ^ v ^ val[x] ^ val[y]];
if (head[X] != -1 && head[Y] == -1) {
head[Y] = v ^ val[x] ^ val[y] ^ head[X];
}
val[X] = val[x] ^ val[y] ^ v;
f[X] = Y;
ans += get(Y);
}
}
void work(int x, int v) {
int X = findd(x);
ans -= get(X);
if (head[X] == -1) head[X] = v ^ val[x];
ans += get(X);
}
int main() {
int T;
cin >> n >> k;
scanf("%s", s + 1);
for (int i = 1; i <= k; i++) {
scanf("%d", &T);
int cpy = T;
int x;
while (T--) {
scanf("%d", &x);
if (k == 300000 && x == 3) printf("eferfceceacaesdc %d", cpy);
V[x].push_back(i);
}
}
init();
for (int i = 1; i <= n; i++) {
if (V[i].size() == 1) {
work(V[i][0], 1 ^ (s[i] - '0'));
} else if (V[i].size() == 2) {
build(V[i][0], V[i][1], 1 ^ (s[i] - '0'));
}
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"
]
}
|
IN-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 <= n; 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"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
template <class V>
int get_p(V& p, int j) {
if (p[j] == j) return j;
if (p[j] < 0) return p[j] = -get_p(p, -p[j]);
return p[j] = get_p(p, p[j]);
}
int get_min(bool a_j, int S_j, bool coa_j, int coS_j) {
if (!coa_j) return S_j;
if (!a_j) return coS_j;
return std::min(S_j, coS_j);
}
void proc() {
int n, k;
std::cin >> n >> k;
std::vector<bool> x(n);
for (int i = 0; i < n; i++) {
char c;
std::cin >> c;
x[i] = c % 2;
}
std::vector<std::vector<int> > J(n);
std::vector<int> p(k + 1);
for (int j = 1; j <= k; j++) {
p[j] = j;
int c;
std::cin >> c;
while (c--) {
int i;
std::cin >> i;
J[i - 1].push_back(j);
}
}
std::vector<int> S(k + 1, 1);
std::vector<int> coS(k + 1, 0);
std::vector<bool> a(k + 1, true);
std::vector<bool> coa(k + 1, true);
int R = 0;
for (int i = 0; i < n; i++) {
if (J[i].size() == 0) {
std::cout << R << std::endl;
continue;
}
int j = J[i][0];
int p_j = get_p(p, j);
int abs_p_j = std::abs(p_j);
bool a_j = a[abs_p_j];
bool coa_j = coa[abs_p_j];
int S_j = S[abs_p_j];
int coS_j = coS[abs_p_j];
if (J[i].size() == 1) {
a[abs_p_j] = a_j && !x[i];
coa[abs_p_j] = coa_j && x[i];
int m_0 = get_min(a_j, S_j, coa_j, coS_j);
int m_1 = get_min(a[abs_p_j], S_j, coa[abs_p_j], coS_j);
R += m_1 - m_0;
std::cout << R << std::endl;
continue;
}
int l = J[i][1];
int p_l = get_p(p, l);
int abs_p_l = std::abs(p_l);
bool a_l = a[abs_p_l];
bool coa_l = coa[abs_p_l];
int S_l = S[abs_p_l];
int coS_l = coS[abs_p_l];
if (abs_p_j < abs_p_l) {
std::swap(p_j, p_l);
std::swap(abs_p_j, abs_p_l);
std::swap(a_j, a_l);
std::swap(coa_j, coa_l);
std::swap(S_j, S_l);
std::swap(coS_j, coS_l);
}
a[abs_p_j] = a_j & coa_l ^
(a_j & coa_l ^ a_j & a_l) & (x[i] ^ (p_j > 0) ^ (p_l > 0));
coa[abs_p_j] = coa_j & a_l ^ (coa_j & a_l ^ coa_j & coa_l) &
(x[i] ^ (p_j > 0) ^ (p_l > 0));
int m_0 = get_min(a_j, S_j, coa_j, coS_j);
if (abs_p_j != abs_p_l) {
m_0 += get_min(a_l, S_l, coa_l, coS_l);
bool t = x[i] ^ (p_j > 0) ^ (p_l > 0);
p[abs_p_l] = t ? abs_p_j : -abs_p_j;
S[abs_p_j] += t ? S_l : coS_l;
coS[abs_p_j] += t ? coS_l : S_l;
}
int m_1 = get_min(a[abs_p_j], S[abs_p_j], coa[abs_p_j], coS[abs_p_j]);
R += m_1 - m_0;
std::cout << R << std::endl;
}
}
int main() {
std::cin.tie(0);
std::cin.sync_with_stdio(false);
int t = 1;
while (t--) proc();
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"
]
}
|
IN-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) {
a = find(a);
int aa = find(a + k);
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 = find(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 c = find(lam[i][0]);
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"
]
}
|
IN-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, kl, 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, b2 = 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];
while (b != -1) {
b2 ^= pr[b].q;
if (pr[b].p != -1)
b = pr[b].p;
else
break;
}
if (a == b) break;
b1 ^= b2;
if ((b1 and s[i] == '1') or (!b1 and s[i] == '0')) {
pr[a].l < pr[b].l ? vas += pr[a].l, pr[a].q ^= 1 : 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"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
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>;
#define endl '\n'
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define fi first
#define se second
#define int long long
typedef long long ll;
typedef long double ld;
#define pii pair<int,int>
#define pll pair<ll,ll>
#define sz(x) ((long long)x.size())
#define fr(a,b,c) for(int a=b; a<=c; a++)
#define frev(a,b,c) for(int a=c; a>=b; a--)
#define rep(a,b,c) for(int a=b; a<c; a++)
#define trav(a,x) for(auto &a:x)
#define all(con) con.begin(),con.end()
#define done(x) {cout << x << endl;return;}
#define mini(x,y) x=min(x,y)
#define maxi(x,y) x=max(x,y)
const ll infl = 0x3f3f3f3f3f3f3f3fLL;
const int infi = 0x3f3f3f3f;
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
//const int mod = 998244353;
const int mod = 1e9 + 7;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<vector<int>> vvi;
typedef vector<pair<int, int>> vpii;
typedef map<int, int> mii;
typedef set<int> si;
typedef set<pair<int,int>> spii;
typedef queue<int> qi;
uniform_int_distribution<int> rng(0,1e9);
//DEBUG FUNCTIONS START
#define cerr cout
void __print(int x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define deb(x...) _print(x)
#else
#define deb(x...)
#endif
// DEBUG FUNCTIONS END
const int N = 3e5 + 5;
int p[N], diff[N], par[N], szz[N], ans = 0;
pii sz[N];
int find(int u){
if(u == p[u])
return u;
return p[u] = find(p[u]);
}
bool same(int u){
if(u == p[u])
return 1;
return same(p[u]) ^ diff[u];
}
void join(int u, int v, bool same_col){
int _u = find(u), _v = find(v);
if(szz[_u] > szz[_v]){
swap(u, v);
swap(_u, _v);
}
if(_u != _v){
p[_u] = _v;
szz[_v] += szz[_u];
if(!same(u))
same_col ^= 1;
if(!same(v))
same_col ^= 1;
ans -= min(sz[_u].fi, sz[_u].se);
ans -= min(sz[_v].fi, sz[_v].se);
if(same_col)
sz[_v].fi += sz[_u].fi, sz[_v].se += sz[_u].se;
else
sz[_v].fi += sz[_u].se, sz[_v].se += sz[_u].fi;
par[u] = v;
diff[u] = !same_col;
ans += min(sz[_v].fi, sz[_v].se);
}
}
void solve(){
int n, k;
cin >> n >> k;
vvi g(n+1);
fr(i,1,k)
p[i] = i, par[i] = i, sz[i] = {1, 0}, szz[i] = 1;
string s;
cin >> s;
fr(i,1,k){
int x;
cin >> x;
rep(_,0,x){
int y;
cin >> y;
g[y].pb(i);
}
}
fr(i,1,n){
if(sz(g[i]) == 2){
join(g[i][0], g[i][1], s[i-1] - '0');
}
else if(sz(g[i]) == 1){
int col = !same(g[i][0]);
int x = find(g[i][0]);
ans -= min(sz[x].fi, sz[x].se);
if(s[i-1] == '1'){
if(col)
sz[x].se = infi, ans += sz[x].fi;
else
sz[x].fi = infi, ans += sz[x].se;
}
else{
if(col)
sz[x].fi = infi, ans += sz[x].se;
else
sz[x].se = infi, ans += sz[x].fi;
}
}
cout << ans << endl;
}
}
signed main() {
#ifndef ONLINE_JUDGE
(void)!freopen("input.txt","r",stdin);
(void)!freopen ("output.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cout << fixed << setprecision(15);
int t = 1;
//cin >> t;
while (t--)
solve();
return 0;
}
int powm(int a, int b){
int res = 1;
while (b) {
if (b & 1)
res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
int divide(int a, int b) {
return (a % mod) * powm(b % mod, mod - 2) % mod;
}
|
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"
]
}
|
IN-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) {
int pa = find(a);
int pb = find(b);
if (peso[pa] < peso[pb]) swap(pa, pb);
peso[pa] += peso[pb];
papa[pb] = pa;
custo[pa] += custo[pb];
obg[pa] = obg[pa] || obg[pb];
}
int con(int a) {
a = find(a);
int aa = find(a + k);
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) {
res -= con(lam[i][0]);
obg[find(word[i] == '0' ? lam[i][0] : lam[i][0] + k)] = true;
res += con(lam[i][0]);
} else if (lam[i].size() == 2) {
int a = lam[i][0], 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"
]
}
|
IN-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;
struct rev {
int c0, c1, tag;
void maintain() {
c0 = min(c0, INF);
c1 = min(c1, INF);
}
};
rev info[maxn];
int dsu[maxn];
pair<int, int> fd(int u) {
if (dsu[u] < 0) return {u, 0};
pair<int, int> tmp = fd(dsu[u]);
info[u].tag ^= tmp.second;
dsu[u] = tmp.first;
return (pair<int, int>){tmp.first, info[u].tag};
}
int st[maxn];
char s[maxn];
int n, k;
vector<int> edge[maxn];
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) {
info[i] = (rev){0, 1, 0};
dsu[i] = -1;
int sz;
scanf("%d", &(sz));
for (; sz--;) {
int x;
scanf("%d", &(x));
edge[x].push_back(i);
}
}
long long ans = 0;
for (int i = (1); i <= (n); ++i) {
int co = st[i];
if (edge[i].size() == 1) {
pair<int, int> t = fd(edge[i][0]);
int u = t.first;
ans -= min(info[u].c0, info[u].c1);
if (co == 1) {
if (t.second == 0)
info[u].c1 = INF;
else
info[u].c0 = INF;
} else if (co == 0) {
if (t.second == 0)
info[u].c0 = INF;
else
info[u].c1 = INF;
}
ans += min(info[u].c0, info[u].c1);
} else if (edge[i].size() == 2) {
pair<int, int> x, y;
x = fd(edge[i][0]);
y = fd(edge[i][1]);
if (x.first != y.first) {
if (dsu[x.first] > dsu[y.first]) swap(x, y);
ans -= min(info[x.first].c0, info[x.first].c1);
ans -= min(info[y.first].c0, info[y.first].c1);
if (co == 1) {
if (x.second != y.second) {
info[y.first].tag ^= 1;
swap(info[y.first].c0, info[y.first].c1);
}
} else {
if (x.second == y.second) {
info[y.first].tag ^= 1;
swap(info[y.first].c0, info[y.first].c1);
}
}
info[x.first].c0 += info[y.first].c0;
info[x.first].c1 += info[y.first].c1;
ans += min(info[x.first].c0, info[x.first].c1);
dsu[x.first] += dsu[y.first];
dsu[y.first] = x.first;
}
}
printf("%lld\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"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long llINF = 2e18, llMOD = 1e9 + 7;
const int INF = 2e9, MOD = 1e9 + 7, P = 179, N = 2e3 + 1, K = 23000, L = 18;
const long double EPS = 1e-6, ldINF = 1e18, PI = acos(-1);
template <typename T>
inline void sort_uniq(vector<T>& v) {
sort(v.begin(), v.end());
v.resize(unique(v.begin(), v.end()) - v.begin());
}
template <typename T>
inline int sign(T a) {
if (a > 0) {
return 1;
} else if (a == 0) {
return 0;
} else {
return -1;
}
}
template <typename T, typename P>
bool eq(T a, P b) {
return abs(a - b) <= EPS;
}
const char sep[] = " ";
template <typename T, typename P>
istream& operator>>(istream& in, pair<T, P>& p) {
in >> p.first >> p.second;
return in;
}
template <typename T>
void in_seg(istream& in, T* dst_begin, T* dst_end) {
while (dst_begin != dst_end) {
in >> (*dst_begin);
++dst_begin;
}
}
template <typename T>
istream& operator>>(istream& in, vector<T>& v) {
for (T& i : v) {
in >> i;
}
return in;
}
template <typename T, typename P>
ostream& operator<<(ostream& out, pair<T, P> const& p) {
out << p.first << ' ' << p.second;
return out;
}
template <typename T>
ostream& operator<<(ostream& out, vector<T> const& v) {
for (T const& i : v) {
out << i << sep;
}
if (!v.empty()) {
out << '\n';
}
return out;
}
template <typename T>
ostream& operator<<(ostream& out, vector<vector<T>> const& v) {
for (vector<T> const& i : v) {
out << i;
}
return out;
}
void set_precision(ostream& out, int val) {
out.setf(out.fixed, out.floatfield);
out.precision(val);
}
struct edge {
int v, u, x;
};
class Dsu {
int n;
vector<int> p, sz, p_xor, r_st, ans;
int cur_ans = 0;
int dsu_get(int v) {
if (v == p[v]) {
return v;
}
int par = p[v], res = dsu_get(p[v]);
p_xor[v] ^= p_xor[par];
p[v] = res;
return res;
}
int get_vrt_ans(int v) {
v = dsu_get(v);
if (r_st[v] == 0) {
return ans[v];
} else if (r_st[v] == 1) {
return sz[v] - ans[v];
} else {
return min(ans[v], sz[v] - ans[v]);
}
}
void dsu_unite(int v, int u, int x) {
int p_v = dsu_get(v);
int p_u = dsu_get(u);
if (p_v == p_u) {
assert(x == (p_xor[u] ^ p_xor[v]));
return;
}
cur_ans -= get_vrt_ans(p_v);
cur_ans -= get_vrt_ans(p_u);
if (sz[p_v] < sz[p_u]) {
swap(v, u);
swap(p_v, p_u);
}
p_xor[p_u] = p_xor[u] ^ p_xor[v] ^ x;
if (r_st[p_u] != 2) {
if (r_st[p_v] == 2) {
r_st[p_v] = r_st[p_u] ^ p_xor[p_u];
} else {
assert(r_st[p_v] == (r_st[p_u] ^ p_xor[p_u]));
}
}
if (p_xor[p_u]) {
ans[p_v] += sz[p_u] - ans[p_u];
} else {
ans[p_v] += ans[p_u];
}
p[p_u] = p_v;
sz[p_v] += sz[p_u];
cur_ans += get_vrt_ans(p_v);
}
public:
Dsu(vector<int> r_st)
: n(r_st.size()), p(n), sz(n, 1), p_xor(n), r_st(r_st), ans(n) {
for (int i = 0; i < n; i++) {
p[i] = i;
}
}
int get_ans() { return cur_ans; }
void set_rst(int v, int st) {
int p_v = dsu_get(v);
cur_ans -= get_vrt_ans(p_v);
r_st[v] = st;
cur_ans += get_vrt_ans(p_v);
}
void unite(int v, int u, int x) { dsu_unite(v, u, x); }
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
string s;
vector<vector<int>> v_mask(n);
vector<int> mask_st(k, 2);
vector<vector<edge>> e(n);
cin >> s;
for (int i = 0, c; i < k; i++) {
cin >> c;
for (int j = 0, x; j < c; j++) {
cin >> x;
v_mask[x - 1].push_back(i);
}
}
int ban_bit = n;
for (int i = 0; i < n; i++) {
if (v_mask[i].size() == 1) {
if (s[i] == '0') {
mask_st[v_mask[i][0]] = 1;
} else {
mask_st[v_mask[i][0]] = 0;
}
} else if (v_mask[i].size() == 2) {
int v = v_mask[i][0];
int u = v_mask[i][1];
if (s[i] == '0') {
e[i].push_back({v, u, 1});
} else {
e[i].push_back({v, u, 0});
}
} else {
assert(v_mask[i].size() == 0);
}
}
Dsu dsu(vector<int>(k, 2));
for (int i = 0; i < n; i++) {
for (auto [v, u, x] : e[i]) {
dsu.unite(v, u, x);
}
if (v_mask[i].size() == 1) {
dsu.set_rst(v_mask[i][0], mask_st[v_mask[i][0]]);
}
cout << dsu.get_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"
]
}
|
IN-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) {
a = find(a);
int aa = find(a + k);
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) {
res -= con(lam[i][0]);
obg[find(word[i] == '0' ? lam[i][0] : lam[i][0] + k)] = true;
res += con(lam[i][0]);
} else if (lam[i].size() == 2) {
int a = lam[i][0], 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"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5;
const int oo = 1e9 + 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)
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"
]
}
|
IN-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) {
int pa = find(a);
int pb = find(b);
if (peso[pa] < peso[pb]) swap(pa, pb);
peso[pa] += peso[pb];
papa[pb] = pa;
custo[pa] += custo[pb];
obg[pa] = obg[pa] || obg[pb];
}
int con(int a) {
a = find(a);
int aa = find(a + k);
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) continue;
if (lam[i].size() == 1) {
res -= con(lam[i][0]);
obg[find(word[i] == '0' ? lam[i][0] : lam[i][0] + k)] = true;
res += con(lam[i][0]);
} else if (lam[i].size() == 2) {
int a = lam[i][0], b = lam[i][1];
if (juntos(a, b) || juntos(a, b + k)) continue;
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"
]
}
|
IN-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] += rotn[b];
}
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"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
long long shin[25];
long long seto(void);
void ayano(long long x, char spliter = '\n');
int m;
int tans = 0;
vector<int> sts[300005];
vector<int> rs[300005];
int chosen[300005];
int cgst[300005], cgstop = 0;
int visited[300005];
int arr[300005], cnt[300005];
char tstr[300005];
int trych(int x);
void undos(int tp);
void chsing(int x);
int main(void) {
int n, k;
n = seto(), k = seto();
scanf("%s", tstr + 1);
for (int i = 1; i <= n; i++) arr[i] = (tstr[i] == '0');
for (int i = 1; i <= k; i++) {
int t = seto();
sts[i].reserve(t);
while (t--) {
int x = seto();
cnt[x]++;
rs[x].push_back(i);
sts[i].push_back(x);
}
sort(sts[i].begin(), sts[i].end());
}
for (int i = 1; i <= n; i++) {
m = i;
if ((!arr[i])) {
ayano(tans);
continue;
}
int ians = k + 1;
int bsti = 0;
for (auto it = rs[i].begin(); it != rs[i].end(); it++) {
int u = *it;
cgstop = 0;
int ures = trych(u);
int uans = tans;
undos(1);
if (ures && uans < ians) ians = uans, bsti = u;
}
cgstop = 0, trych(bsti), undos(0);
ayano(tans);
}
return 0;
}
int trych(int x) {
chsing(x);
cgst[cgstop++] = x;
visited[x] = 1;
for (auto it = sts[x].begin(); it != sts[x].end(); it++) {
int v = *it;
if (v > m) break;
if (!arr[v]) continue;
if (cnt[v] == 1) return 0;
int cgs = 0;
for (auto itt = rs[v].begin(); itt != rs[v].end(); itt++) {
int u = *itt;
if (visited[u] || sts[u][0] < v) continue;
cgs += trych(u);
}
if (!cgs) return 0;
}
return 1;
}
void undos(int tp) {
if (tp) {
while (cgstop--) visited[cgst[cgstop]] = 0, chsing(cgst[cgstop]);
} else {
while (cgstop--) visited[cgst[cgstop]] = 0;
}
cgstop = 0;
}
void chsing(int x) {
if (chosen[x])
tans--, chosen[x] = 0;
else
tans++, chosen[x] = 1;
for (auto it = sts[x].begin(); it != sts[x].end(); it++) {
int v = *it;
arr[v] ^= 1;
}
}
long long seto(void) {
long long seto_x = 0;
int symbol = 0;
char ch = getchar();
while (!isdigit(ch)) (ch == '-') ? (symbol = 1) : (0), ch = getchar();
while (isdigit(ch)) (seto_x = seto_x * 10 + (ch - '0')), ch = getchar();
return (symbol) ? (-seto_x) : (seto_x);
}
void ayano(long long ayano_x, char spliter) {
if (!ayano_x) {
putchar('0'), putchar(spliter);
return;
}
if (ayano_x < 0) putchar('-'), ayano_x = -ayano_x;
int ayano_len = 0;
while (ayano_x) {
long long ayano_d = ayano_x / 10;
shin[ayano_len++] = ayano_x - ayano_d * 10;
ayano_x = ayano_d;
}
while (ayano_len--) putchar(shin[ayano_len] + '0');
putchar(spliter);
}
|
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"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
template <class V>
int get_p(V& p, int j) {
if (p[j] == j) return j;
if (p[j] < 0) return p[j] = -get_p(p, -p[j]);
return p[j] = get_p(p, p[j]);
}
int get_min(bool a_j, int S_j, bool coa_j, int coS_j) {
if (!coa_j) return S_j;
if (!a_j) return coS_j;
return std::min(S_j, coS_j);
}
template <class O, class Z, class A, class B, class C, class D, class E>
void debug(O& out, const Z& p, const A& J, const B& a, const C& coa, const D& S,
const E& coS) {
out << "p:";
for (auto x : p) out << " " << x;
out << std::endl;
out << "J:";
for (auto x : J) {
out << " {";
for (auto y : x) out << " " << y;
out << " }";
}
out << std::endl;
out << " a:";
for (auto x : a) out << " " << x;
out << std::endl;
out << "coa:";
for (auto x : coa) out << " " << x;
out << std::endl;
out << " S:";
for (auto x : S) out << " " << x;
out << std::endl;
out << "coS:";
for (auto x : coS) out << " " << x;
out << std::endl;
}
template <class O, class Z, class A, class B, class C, class D, class E>
void debug(O& out, int i, Z& p, const A& J, const B& a, const C& coa,
const D& S, const E& coS) {
for (auto y : J[i])
out << p[y] << ' ' << p[abs(get_p(p, y))] << ' ' << a[abs(get_p(p, y))]
<< ' ' << coa[abs(get_p(p, y))] << ' ' << S[abs(get_p(p, y))] << ' '
<< coS[abs(get_p(p, y))] << ' ';
}
void proc() {
int n, k;
std::cin >> n >> k;
std::vector<bool> x(n);
for (int i = 0; i < n; i++) {
char c;
std::cin >> c;
x[i] = c % 2;
}
std::vector<std::vector<int> > J(n);
std::vector<int> p(k + 1);
for (int j = 1; j <= k; j++) {
p[j] = j;
int c;
std::cin >> c;
while (c--) {
int i;
std::cin >> i;
J[i - 1].push_back(j);
}
}
std::vector<int> S(k + 1, 1);
std::vector<int> coS(k + 1, 0);
std::vector<bool> a(k + 1, true);
std::vector<bool> coa(k + 1, true);
int R = 0;
for (int i = 0; i < n; i++) {
if (J[i].size() == 0) {
if (n < 1000) std::cout << R << std::endl;
if (i == 10780) debug(std::cout, i, p, J, a, coa, S, coS);
continue;
}
int j = J[i][0];
int p_j = get_p(p, j);
int abs_p_j = std::abs(p_j);
bool a_j = a[abs_p_j];
bool coa_j = coa[abs_p_j];
int S_j = S[abs_p_j];
int coS_j = coS[abs_p_j];
if (J[i].size() == 1) {
a[abs_p_j] = a_j && !x[i];
coa[abs_p_j] = coa_j && x[i];
int m_0 = get_min(a_j, S_j, coa_j, coS_j);
int m_1 = get_min(a[abs_p_j], S_j, coa[abs_p_j], coS_j);
R += m_1 - m_0;
if (n < 1000) std::cout << R << std::endl;
if (i == 10780) debug(std::cout, i, p, J, a, coa, S, coS);
continue;
}
int l = J[i][1];
int p_l = get_p(p, l);
int abs_p_l = std::abs(p_l);
bool a_l = a[abs_p_l];
bool coa_l = coa[abs_p_l];
int S_l = S[abs_p_l];
int coS_l = coS[abs_p_l];
if (abs_p_j < abs_p_l) {
std::swap(p_j, p_l);
std::swap(abs_p_j, abs_p_l);
std::swap(a_j, a_l);
std::swap(coa_j, coa_l);
std::swap(S_j, S_l);
std::swap(coS_j, coS_l);
}
a[abs_p_j] = a_j & coa_l ^
(a_j & coa_l ^ a_j & a_l) & (x[i] ^ (p_j > 0) ^ (p_l > 0));
coa[abs_p_j] = coa_j & a_l ^ (coa_j & a_l ^ coa_j & coa_l) &
(x[i] ^ (p_j > 0) ^ (p_l > 0));
int m_0 = get_min(a_j, S_j, coa_j, coS_j);
if (abs_p_j != abs_p_l) {
m_0 += get_min(a_l, S_l, coa_l, coS_l);
bool t = x[i] ^ (p_j > 0) ^ (p_l > 0);
p[abs_p_l] = t ? abs_p_j : -abs_p_j;
S[abs_p_j] += t ? S_l : coS_l;
coS[abs_p_j] += t ? coS_l : S_l;
}
int m_1 = get_min(a[abs_p_j], S[abs_p_j], coa[abs_p_j], coS[abs_p_j]);
R += m_1 - m_0;
if (n < 1000) std::cout << R << std::endl;
if (i == 10780) debug(std::cout, i, p, J, a, coa, S, coS);
}
}
int main() {
std::cin.tie(0);
std::cin.sync_with_stdio(false);
int t = 1;
while (t--) proc();
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"
]
}
|
IN-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], tot[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[n + 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, n + 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 -= tot[u] + tot[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]);
tot[u] = tot[v] = 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"
]
}
|
IN-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;
papa[a] = find(papa[a]);
obg[a] = obg[papa[a]];
custo[a] = custo[papa[a]];
return 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) {
a = find(a);
int aa = find(a + k);
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) {
res -= con(lam[i][0]);
obg[find(word[i] == '0' ? lam[i][0] : lam[i][0] + k)] = true;
res += con(lam[i][0]);
} else if (lam[i].size() == 2) {
int a = lam[i][0], 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"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int N = 6e5 + 5;
int pre[N], si[N], num[N / 2], n, m;
bool vis[N];
char s[N];
vector<vector<int> > v(N / 2);
int findd(int x) {
if (pre[x] == x) return x;
pre[x] = findd(pre[x]);
if (vis[x]) vis[pre[x]] = 1;
return pre[x];
}
inline int id(int x) { return x > m ? x - m : x; }
int un(int a, int b) {
int x = findd(a);
int y = findd(b);
if (x == y) return -1;
int aa = id(x);
int bb = id(y);
if (aa < bb) {
pre[y] = x, si[x] += si[y];
if (vis[y]) vis[x] = 1;
return aa;
} else {
pre[x] = y, si[y] += si[x];
if (vis[x]) vis[y] = 1;
return bb;
}
}
int main() {
scanf("%d%d", &n, &m);
scanf("%s", s + 1);
for (int i = 1; i <= m; i++) {
int a, b;
scanf("%d", &a);
while (a--) {
scanf("%d", &b);
v[b].push_back(i);
}
}
for (int i = 1; i <= m; i++) {
pre[i] = i, si[i] = 1;
pre[i + m] = i + m, si[i + m] = 0;
}
int ans = 0;
for (int i = 1; i <= n; i++) {
if (s[i] == '0') {
if (v[i].size() == 1) {
int a = findd(v[i][0]);
int b = a > m ? a - m : a + m;
vis[b] = 1;
ans += si[a] - num[a];
num[a] = si[a];
}
if (v[i].size() == 2) {
int a = v[i][0], b = v[i][1];
int sum = num[id(findd(a))] + num[id(findd(b))];
int x = un(a, b + m);
un(b, a + m);
if (x != -1) {
if (vis[x]) {
ans += si[x + m] - sum;
num[x] = si[x + m];
} else if (vis[x + m]) {
ans += si[x] - sum;
num[x] = si[x];
} else {
ans += min(si[x], si[x + m]) - sum;
num[x] = min(si[x], si[x + m]);
}
}
}
} else {
if (v[i].size() == 1) {
int a = findd(v[i][0] + m);
int b = a > m ? a - m : a + m;
vis[b] = 1;
ans += si[a] - num[a];
num[a] = si[a];
}
if (v[i].size() == 2) {
int a = v[i][0], b = v[i][1];
int sum = num[id(findd(a))] + num[id(findd(b))];
int x = un(a, b);
un(b + m, a + m);
if (x != -1) {
if (vis[x]) {
ans += si[x + m] - sum;
num[x] = si[x + m];
} else if (vis[x + m]) {
ans += si[x] - sum;
num[x] = si[x];
} else {
ans += min(si[x], si[x + m]) - sum;
num[x] = min(si[x], si[x + m]);
}
}
}
}
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"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 300005;
int N, K, a[MAXN];
vector<int> V[MAXN];
char S[MAXN];
int f1[MAXN], v1[MAXN], f2[MAXN], chs[MAXN], ans;
int get1(int x) {
if (f1[x] != x) f1[x] = get1(f1[x]);
return f1[x];
}
void merge1(int x, int y) {
x = get1(x), y = get1(y);
if (x != y) f1[x] = y, v1[y] += v1[x];
}
int get2(int x) {
if (f2[x] != x) f2[x] = get2(f2[x]);
return f2[x];
}
inline int val(int x) {
x = get2(x);
if (chs[x] == 1)
return v1[x];
else if (chs[x] == 2)
return v1[x + K];
else
return min(v1[x], v1[x + K]);
}
void merge2(int x, int y, int k) {
if (get2(x) != get2(y)) {
ans -= val(x) + val(y);
int qwq = 0;
if (chs[get2(x)]) {
if (chs[get2(x)] == 1)
qwq = get2(x);
else
qwq = get2(x) + K;
}
f2[get2(x)] = get2(y);
if (k == 1)
merge1(x, y), merge1(x + K, y + K);
else
merge1(x, y + K), merge1(x + K, y);
if (!chs[get2(y)] && qwq) {
if (get1(qwq) == get2(y))
chs[get2(y)] = 1;
else
chs[get2(y)] = 2;
}
if (chs[get2(x)]) chs[get2(y)] = chs[get2(x)];
ans += val(y);
}
}
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) f1[i] = i, v1[i] = i > K;
for (i = 1; i <= K; ++i) f2[i] = i;
for (i = 1; i <= N; ++i) {
if (V[i].size() == 1) {
if (S[i] == '0')
ans -= val(V[i][0]), chs[get2(V[i][0])] = 2, ans += val(V[i][0]);
else
ans -= val(V[i][0]), chs[get2(V[i][0])] = 1, ans += val(V[i][0]);
} else if (V[i].size() == 2) {
if (S[i] == '0')
merge2(V[i][0], V[i][1], 2);
else
merge2(V[i][0], V[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"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const long long maxn = 400005;
const long long inf = 1ll << 50;
int ans = 0;
bool init[maxn];
int par[maxn], sz[maxn], cnt[maxn][2], col[maxn];
vector<int> vec[maxn];
int find(int x) {
if (par[x] == x) {
return x;
}
par[x] = find(par[x]);
col[x] = col[x] ^ col[par[x]];
return par[x];
}
int get_val(int x) {
if (x == 0) {
return cnt[x][1];
}
return min(cnt[x][0], cnt[x][1]);
}
void merge(int X, int Y, bool same) {
int x = find(X);
int y = find(Y);
if (x == y) {
return;
}
if (sz[x] > sz[y]) {
swap(x, y);
}
sz[y] += sz[x];
par[x] = y;
bool parity = (same ^ (col[X] == col[Y]));
ans -= get_val(x);
ans -= get_val(y);
if (parity) {
col[x] = 1;
cnt[y][0] += cnt[x][1];
cnt[y][1] += cnt[x][0];
} else {
col[x] = 0;
cnt[y][0] += cnt[x][0];
cnt[y][1] += cnt[x][1];
}
ans += get_val(y);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long n, k;
cin >> n >> k;
sz[0] = (1 << 28);
for (int i = 0; i <= k; i++) {
par[i] = i;
sz[i] += 1;
cnt[i][0] = 1;
cnt[i][1] = 0;
col[i] = 0;
}
string s;
cin >> s;
for (int i = 1; i <= n; i++) {
init[i] = s[i - 1] - '0';
}
for (int i = 1; i <= k; i++) {
long long c;
cin >> c;
int a;
for (int j = 0; j < c; j++) {
cin >> a;
vec[a].push_back(i);
}
}
for (int i = 1; i <= n; i++) {
assert(vec[i].size() <= 2);
if (vec[i].size() == 0) {
cout << ans << "\n";
assert(init[i] == 1);
continue;
}
if (vec[i].size() == 1) {
vec[i].push_back(0);
}
merge(vec[i][0], vec[i][1], init[i] == 1);
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"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int ans = 0;
int val[600010], fa[600010];
int have[600010][2];
char str[600010];
int n, k;
int find(int x) { return x == fa[x] ? x : (fa[x] = find(fa[x])); }
int other(int u) { return u <= k ? u + k : u - k; }
void update(int u, int x, int y) {
ans -= min(val[u], val[other(u)]);
ans += min(x, y);
val[u] = x;
val[other(u)] = y;
}
int main() {
scanf("%d%d", &n, &k);
scanf("%s", str + 1);
for (int i = 1; i <= k; i++) {
int c;
scanf("%d", &c);
while (c--) {
int a;
scanf("%d", &a);
if (have[a][0])
have[a][1] = i;
else
have[a][0] = i;
}
val[i] = 0, val[i + k] = 1;
fa[i] = i, fa[i + k] = i + k;
}
for (int i = 1; i <= n; i++) {
if (str[i] == '0') {
int x = find(have[i][0]), y = find(have[i][1]);
if (!y)
update(x, 1e7, val[other(x)]);
else {
int uy = val[y], vy = val[other(y)];
update(y, 0, 0);
fa[y] = other(x);
fa[other(y)] = x;
update(x, val[x] + vy, val[other(x)] + uy);
}
} else {
int x = find(have[i][0]), y = find(have[i][1]);
if (!x) {
printf("%d\n", ans);
continue;
}
if (!y)
update(x, val[x], 1e7);
else {
int uy = val[y], vy = val[other(y)];
update(y, 0, 0);
fa[y] = x;
fa[other(y)] = other(x);
update(x, val[x] + uy, val[other(x)] + vy);
}
}
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"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long llINF = 2e18, llMOD = 1e9 + 7;
const int INF = 2e9, MOD = 1e9 + 7, P = 179, N = 2e3 + 1, K = 23000, L = 18;
const long double EPS = 1e-6, ldINF = 1e18, PI = acos(-1);
template <typename T>
inline void sort_uniq(vector<T>& v) {
sort(v.begin(), v.end());
v.resize(unique(v.begin(), v.end()) - v.begin());
}
template <typename T>
inline int sign(T a) {
if (a > 0) {
return 1;
} else if (a == 0) {
return 0;
} else {
return -1;
}
}
template <typename T, typename P>
bool eq(T a, P b) {
return abs(a - b) <= EPS;
}
const char sep[] = " ";
template <typename T, typename P>
istream& operator>>(istream& in, pair<T, P>& p) {
in >> p.first >> p.second;
return in;
}
template <typename T>
void in_seg(istream& in, T* dst_begin, T* dst_end) {
while (dst_begin != dst_end) {
in >> (*dst_begin);
++dst_begin;
}
}
template <typename T>
istream& operator>>(istream& in, vector<T>& v) {
for (T& i : v) {
in >> i;
}
return in;
}
template <typename T, typename P>
ostream& operator<<(ostream& out, pair<T, P> const& p) {
out << p.first << ' ' << p.second;
return out;
}
template <typename T>
ostream& operator<<(ostream& out, vector<T> const& v) {
for (T const& i : v) {
out << i << sep;
}
if (!v.empty()) {
out << '\n';
}
return out;
}
template <typename T>
ostream& operator<<(ostream& out, vector<vector<T>> const& v) {
for (vector<T> const& i : v) {
out << i;
}
return out;
}
void set_precision(ostream& out, int val) {
out.setf(out.fixed, out.floatfield);
out.precision(val);
}
struct edge {
int v, u, x;
};
class Dsu {
int n;
vector<int> p, sz, p_xor, r_st, ans;
vector<char> active;
int cur_ans = 0;
int dsu_get(int v) {
if (v == p[v]) {
return v;
}
int par = p[v], res = dsu_get(p[v]);
p_xor[v] ^= p_xor[par];
p[v] = res;
return res;
}
int get_vrt_ans(int v) {
v = dsu_get(v);
if (r_st[v] == 0) {
return ans[v];
} else if (r_st[v] == 1) {
return sz[v] - ans[v];
} else {
return min(ans[v], sz[v] - ans[v]);
}
}
void dsu_unite(int v, int u, int x) {
int p_v = dsu_get(v);
int p_u = dsu_get(u);
if (p_v == p_u) {
assert(x == (p_xor[u] ^ p_xor[v]));
return;
}
if (active[p_v]) {
cur_ans -= get_vrt_ans(p_v);
}
if (active[p_u]) {
cur_ans -= get_vrt_ans(p_u);
}
if (sz[p_v] < sz[p_u]) {
swap(v, u);
swap(p_v, p_u);
}
p_xor[p_u] = p_xor[u] ^ p_xor[v] ^ x;
if (r_st[u] != 2) {
if (r_st[v] != 2) {
} else {
r_st[p_v] = r_st[p_u] ^ p_xor[p_u];
}
}
if (p_xor[p_u]) {
ans[p_v] += sz[p_u] - ans[p_u];
} else {
ans[p_v] += ans[p_u];
}
p[p_u] = p_v;
sz[p_v] += sz[p_u];
cur_ans += get_vrt_ans(p_v);
active[p_v] = true;
}
public:
Dsu(vector<int> r_st)
: n(r_st.size()),
p(n),
sz(n, 1),
p_xor(n),
r_st(r_st),
ans(n),
active(n, false) {
for (int i = 0; i < n; i++) {
p[i] = i;
}
}
int get_ans() { return cur_ans; }
void activate(int v) {
int v_prv = v;
v = dsu_get(v);
if (!active[v]) {
if (r_st[v] == 2 && r_st[v_prv] != 2) {
r_st[v] = p_xor[v_prv] ^ r_st[v_prv];
} else if (r_st[v_prv] != 2) {
assert(r_st[v] == (p_xor[v_prv] ^ r_st[v_prv]));
}
cur_ans += get_vrt_ans(v);
active[v] = true;
}
}
void unite(int v, int u, int x) { dsu_unite(v, u, x); }
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
string s;
vector<vector<int>> v_mask(n);
vector<int> mask_st(k, 2);
vector<vector<edge>> e(n);
cin >> s;
for (int i = 0, c; i < k; i++) {
cin >> c;
for (int j = 0, x; j < c; j++) {
cin >> x;
v_mask[x - 1].push_back(i);
}
}
int ban_bit = n;
for (int i = 0; i < n; i++) {
if (v_mask[i].size() == 1) {
if (s[i] == '0') {
mask_st[v_mask[i][0]] = 1;
} else {
mask_st[v_mask[i][0]] = 0;
}
} else if (v_mask[i].size() == 2) {
int v = v_mask[i][0];
int u = v_mask[i][1];
if (s[i] == '0') {
e[i].push_back({v, u, 1});
} else {
e[i].push_back({v, u, 0});
}
} else {
assert(v_mask[i].size() == 0);
}
}
Dsu dsu(mask_st);
for (int i = 0; i < n; i++) {
for (auto [v, u, x] : e[i]) {
dsu.unite(v, u, x);
}
if (v_mask[i].size() == 1) {
dsu.activate(v_mask[i][0]);
}
cout << dsu.get_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"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int test = 0;
const int MAXN = 300009;
const long long MOD = 119 << 23 | 1;
class {
public:
vector<int> V[MAXN];
int fa[MAXN], cnt[MAXN];
int n, m;
int findfa(int x) {
if (fa[x] == x) return x;
return fa[x] = findfa(fa[x]);
}
void merge(int x, int y) {
x = findfa(x);
y = findfa(y);
if (x == y) return;
if (x > y) swap(x, y);
fa[y] = x;
cnt[x] += cnt[y];
}
void solve() {
cin >> n >> m;
string s;
cin >> s;
for (int i = 1; i <= m; ++i) {
int c;
cin >> c;
for (int j = 0; j < c; ++j) {
int v;
cin >> v;
V[v].push_back(i);
}
fa[i] = i;
fa[i + m] = i + m;
cnt[i + m] = 1;
}
int ans = 0;
for (int i = 1; i <= n; ++i) {
bool on = s[i - 1] == '1';
if (V[i].size() == 1) {
int p = V[i][0];
if (on) {
ans -= cost(p);
merge(p + m, 0);
ans += cost(p);
} else {
ans -= cost(p);
merge(p, 0);
ans += cost(p);
}
} else if (V[i].size() == 2) {
int p0 = V[i][0], p1 = V[i][1];
if (on) {
if (findfa(p0) != findfa(p1)) {
ans -= cost(p0);
ans -= cost(p1);
merge(p0, p1);
merge(p0 + m, p1 + m);
ans += cost(p0);
} else {
;
}
} else {
if (findfa(p0) != findfa(p1 + m)) {
ans -= cost(p0);
ans -= cost(p1);
merge(p0, p1 + m);
merge(p0 + m, p1);
ans += cost(p0);
} else {
;
}
}
}
cout << ans << '\n';
}
}
int cost(int x) {
int y;
if (x <= m) {
y = m + x;
} else {
y = x - m;
}
x = findfa(x);
y = findfa(y);
if (x == 0)
return cnt[y];
else if (y == 0)
return cnt[x];
return min(cnt[x], cnt[y]);
}
} NSPACE;
int main() {
ios_base::sync_with_stdio(false);
cout.tie(0);
cin.tie(0);
NSPACE.solve();
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
CORRECT
|
python3
|
import bisect
def getsum(tree , i):
s = 0
i += 1
while i>0:
s += tree[i]
i -= i & (-i)
return s
def updatebit(tree , n , i , v):
i+= 1
while i <= n:
tree[i] += v
i += i & (-i)
n = int(input())
x = list(map(int , input().split()))
v = list(map(int , input().split()))
p = [[x[i] , v[i]] for i in range(len(x))]
vs = sorted(list(set(v)))
p = sorted(p , key = lambda i : i[0])
l = len(vs)
cnt = [0]*(l+1)
xs = [0]*(l+1)
ans = 0
for pnt in p:
pos = bisect.bisect_left(vs , pnt[1])
ans += getsum(cnt , pos) * pnt[0] - getsum(xs , pos)
updatebit(cnt , l , pos , 1)
updatebit(xs , l , pos , pnt[0])
print(ans)
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
struct FenwickTree {
int n;
vector<long long> num;
FenwickTree() : n(0) {}
FenwickTree(int _n) {
n = _n;
num.assign(n, 0);
}
void add(int i, int val) {
for (; i < n; i |= i + 1) num[i] += val;
}
long long sum(int i) {
long long ret = 0;
for (; i >= 0; i = (i & (i + 1)) - 1) ret += num[i];
return ret;
}
};
int main() {
int n;
scanf("%d", &n);
vector<pair<int, int>> point(n);
vector<int> speed;
for (int i = 0; i < n; i++) scanf("%d", &point[i].first);
for (int i = 0; i < n; i++) {
scanf("%d", &point[i].second);
speed.push_back(point[i].second);
}
sort(speed.begin(), speed.end());
speed.erase(unique(speed.begin(), speed.end()), speed.end());
sort(point.begin(), point.end());
long long ans = 0;
FenwickTree cnt(n), sumx(n);
for (auto i : point) {
i.second =
lower_bound(speed.begin(), speed.end(), i.second) - speed.begin();
ans += cnt.sum(i.second) * i.first - sumx.sum(i.second);
cnt.add(i.second, 1);
sumx.add(i.second, i.first);
}
printf("%lld\n", ans);
return 0;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int arr[n];
int v[n];
for (int i = 0; i < n; i++) cin >> arr[i];
for (int i = 0; i < n; i++) cin >> v[i];
map<int, int> mp;
vector<pair<int, int> > vi;
vector<pair<int, int> > vf;
for (int i = 0; i < n; i++) {
vi.push_back({arr[i], v[i]});
vf.push_back({v[i], arr[i]});
}
sort(vi.begin(), vi.end());
sort(vf.begin(), vf.end());
long long ans = 0;
for (int i = 0; i < n; i++) {
int val = vi[i].first;
mp[val] = i;
ans += (2 * i - n + 1) * ((long long)val);
}
for (int i = 0; i < n; i++) {
int val = vf[i].second;
if (mp[val] == i) continue;
ans += (i - mp[val]) * (long long)val;
}
cout << ans << endl;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int n, T;
pair<int, int> a[200010];
long long getval(vector<long long> &f, int pos) {
pos++;
long long ans = 0;
for (int i = pos; i > 0; i -= (i & -i)) ans += f[i];
return ans;
}
void upd(vector<long long> &f, int pos, int val) {
pos++;
for (int i = pos; i < int(f.size()); i += (i & -i)) f[i] += val;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i].first;
for (int i = 0; i < n; i++) cin >> a[i].second;
sort(a, a + n);
vector<int> compress_v;
for (int i = 0; i < n; i++) compress_v.push_back(a[i].second);
sort(compress_v.begin(), compress_v.end());
compress_v.resize(unique(compress_v.begin(), compress_v.end()) -
compress_v.begin());
vector<long long> cnt(compress_v.size() + 1), sum(compress_v.size() + 1);
long long ans = 0;
for (int i = 0; i < n; i++) {
int pos = lower_bound(compress_v.begin(), compress_v.end(), a[i].second) -
compress_v.begin();
ans += getval(cnt, pos) * 1ll * a[i].first - getval(sum, pos);
upd(cnt, pos, 1);
upd(sum, pos, a[i].first);
}
cout << ans;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
CORRECT
|
java
|
// Don't place your source in a package
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;
/*
6666666666666 6666666666666
6 6
6 6
6 6
6 6
6 6
6666666666666 6666666666666
6 6 6 6
6 6 6 6
6 6 6 6
6 6 6 6
6666666666666 6666666666666
*/
// Please name your class Main
public class Main {
//static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
/*static int read() throws IOException {
in.nextToken();
return (int) in.nval;
}
static String readString() throws IOException {
in.nextToken();
return in.sval;
}*/
static Scanner in = new Scanner(System.in);
public static void main (String[] args) throws java.lang.Exception {
//InputReader in = new InputReader(System.in);
PrintWriter out = new PrintWriter(System.out);
int T=1;
for(int t=0;t<T;t++){
int n=Int();
int A[][]=new int[n][2];
for(int i=0;i<n;i++){
A[i][0]=Int();
}
for(int i=0;i<n;i++){
A[i][1]=Int();
}
Solution sol=new Solution();
sol.solution(A);
}
out.flush();
}
public static int Int(){
return in.nextInt();
}
public static String Str(){
return in.next();
}
}
class Solution{
//constant variable
final int MAX=Integer.MAX_VALUE;
final int MIN=Integer.MIN_VALUE;
//Set<Integer>adjecent[];
//////////////////////////////
public void solution(int A[][]){
Arrays.sort(A,(a,b)->{
if(a[1]==b[1])return a[0]-b[0];
return a[1]-b[1];
});//sort by sppeed
Seg seg=new Seg(0,100000000);
long res=0;
for(int i=0;i<A.length;i++){
int x=A[i][0];
int v=A[i][1];
long pair[]=seg.query(0,x-1);
//msg(x+" "+v+" :"+pair[0]+" "+pair[1]);
res+=(x*pair[1]-pair[0]);
seg.update(x,x);
//previous always smaller than this one, distance shoulde be n*x-sum
}
msg(res+"");
}
public void swap(int A[],int l,int r){
int t=A[l];
A[l]=A[r];
A[r]=t;
}
public long C(long fact[],int i,int j){ // C(20,3)=20!/(17!*3!)
// take a/b where a=20! b=17!*3!
if(j>i)return 0;
if(j==0)return 1;
long mod=1000000007;
long a=fact[i];
long b=((fact[i-j]%mod)*(fact[j]%mod))%mod;
BigInteger B= BigInteger.valueOf(b);
long binverse=B.modInverse(BigInteger.valueOf(mod)).longValue();
return ((a)*(binverse%mod))%mod;
}
//map operation
public void put(Map<Integer,Integer>map,int i){
if(!map.containsKey(i))map.put(i,0);
map.put(i,map.get(i)+1);
}
public void delete(Map<Integer,Integer>map,int i){
map.put(i,map.get(i)-1);
if(map.get(i)==0)map.remove(i);
}
/*public void tarjan(int p,int r){
if(cut)return;
List<Integer>childs=adjecent[r];
dis[r]=low[r]=time;
time++;
//core for tarjan
int son=0;
for(int c:childs){
if(ban==c||c==p)continue;
if(dis[c]==-1){
son++;
tarjan(r,c);
low[r]=Math.min(low[r],low[c]);
if((r==root&&son>1)||(low[c]>=dis[r]&&r!=root)){
cut=true;
return;
}
}else{
if(c!=p){
low[r]=Math.min(low[r],dis[c]);
}
}
}
}*/
//helper function I would use
public void remove(Map<Integer,Integer>map,int i){
map.put(i,map.get(i)-1);
if(map.get(i)==0)map.remove(i);
}
public void ascii(String s){
for(char c:s.toCharArray()){
System.out.print((c-'a')+" ");
}
msg("");
}
public int flip(int i){
if(i==0)return 1;
else return 0;
}
public boolean[] primes(int n){
boolean A[]=new boolean[n+1];
for(int i=2;i<=n;i++){
if(A[i]==false){
for(int j=i+i;j<=n;j+=i){
A[j]=true;
}
}
}
return A;
}
public void msg(String s){
System.out.println(s);
}
public void msg1(String s){
System.out.print(s);
}
public int[] kmpPre(String p){
int pre[]=new int[p.length()];
int l=0,r=1;
while(r<p.length()){
if(p.charAt(l)==p.charAt(r)){
pre[r]=l+1;
l++;r++;
}else{
if(l==0)r++;
else l=pre[l-1];
}
}
return pre;
}
public boolean isP(String s){
int l=0,r=s.length()-1;
while(l<r){
if(s.charAt(l)!=s.charAt(r))return false;
l++;r--;
}
return true;
}
public int find(int nums[],int x){//union find => find method
if(nums[x]==x)return x;
int root=find(nums,nums[x]);
nums[x]=root;
return root;
}
public int get(int A[],int i){
if(i<0||i>=A.length)return 0;
return A[i];
}
public int[] copy1(int A[]){
int a[]=new int[A.length];
for(int i=0;i<a.length;i++)a[i]=A[i];
return a;
}
public void print1(int A[]){
for(long i:A)System.out.print(i+" ");
System.out.println();
}
public void print2(int A[][]){
for(int i=0;i<A.length;i++){
for(int j=0;j<A[0].length;j++){
System.out.print(A[i][j]+" ");
}System.out.println();
}
}
public int min(int a,int b){
return Math.min(a,b);
}
public int[][] matrixdp(int[][] grid) {
if(grid.length==0)return new int[][]{};
int res[][]=new int[grid.length][grid[0].length];
for(int i=0;i<grid.length;i++){
for(int j=0;j<grid[0].length;j++){
res[i][j]=grid[i][j]+get(res,i-1,j)+get(res,i,j-1)-get(res,i-1,j-1);
}
}
return res;
}
public int get(int grid[][],int i,int j){
if(i<0||j<0||i>=grid.length||j>=grid[0].length)return 0;
return grid[i][j];
}
public int[] suffixArray(String s){
int n=s.length();
Suffix A[]=new Suffix[n];
for(int i=0;i<n;i++){
A[i]=new Suffix(i,s.charAt(i)-'a',0);
}
for(int i=0;i<n;i++){
if(i==n-1){
A[i].next=-1;
}else{
A[i].next=A[i+1].rank;
}
}
Arrays.sort(A);
for(int len=4;len<A.length*2;len<<=1){
int in[]=new int[A.length];
int rank=0;
int pre=A[0].rank;
A[0].rank=rank;
in[A[0].index]=0;
for(int i=1;i<A.length;i++){//rank for the first two letter
if(A[i].rank==pre&&A[i].next==A[i-1].next){
pre=A[i].rank;
A[i].rank=rank;
}else{
pre=A[i].rank;
A[i].rank=++rank;
}
in[A[i].index]=i;
}
for(int i=0;i<A.length;i++){
int next=A[i].index+len/2;
if(next>=A.length){
A[i].next=-1;
}else{
A[i].next=A[in[next]].rank;
}
}
Arrays.sort(A);
}
int su[]=new int[A.length];
for(int i=0;i<su.length;i++){
su[i]=A[i].index;
}
return su;
}
}
//suffix array Struct
class Suffix implements Comparable<Suffix>{
int index;
int rank;
int next;
public Suffix(int i,int rank,int next){
this.index=i;
this.rank=rank;
this.next=next;
}
@Override
public int compareTo(Suffix other) {
if(this.rank==other.rank){
return this.next-other.next;
}
return this.rank-other.rank;
}
public String toString(){
return this.index+" "+this.rank+" "+this.next+" ";
}
}
class Wrapper implements Comparable<Wrapper>{
int spf;int cnt;
public Wrapper(int spf,int cnt){
this.spf=spf;
this.cnt=cnt;
}
@Override
public int compareTo(Wrapper other) {
return this.spf-other.spf;
}
}
class Node{//what the range would be for that particular node
boolean state=false;
int l=0,r=0;
int ll=0,rr=0;
public Node(boolean state){
this.state=state;
}
}
class Seg1{
int A[];
public Seg1(int A[]){
this.A=A;
}
public void update(int left,int right,int val,int s,int e,int id){
if(left<0||right<0||left>right)return;
if(left==s&&right==e){
A[id]+=val;
return;
}
int mid=s+(e-s)/2; //[s,mid] [mid+1,e]
if(left>=mid+1){
update(left,right,val,mid+1,e,id*2+2);
}else if(right<=mid){
update(left,right,val,s,mid,id*2+1);
}else{
update(left,mid,val,s,mid,id*2+1);
update(mid+1,right,val,mid+1,e,id*2+2);
}
}
public int query(int i,int add,int s,int e,int id){
if(s==e&&i==s){
return A[id]+add;
}
int mid=s+(e-s)/2; //[s,mid] [mid+1,e]
if(i>=mid+1){
return query(i,A[id]+add,mid+1,e,id*2+2);
}else{
return query(i,A[id]+add,s,mid,id*2+1);
}
}
}
class MaxFlow{
public static List<Edge>[] createGraph(int nodes) {
List<Edge>[] graph = new List[nodes];
for (int i = 0; i < nodes; i++)
graph[i] = new ArrayList<>();
return graph;
}
public static void addEdge(List<Edge>[] graph, int s, int t, int cap) {
graph[s].add(new Edge(t, graph[t].size(), cap));
graph[t].add(new Edge(s, graph[s].size() - 1, 0));
}
static boolean dinicBfs(List<Edge>[] graph, int src, int dest, int[] dist) {
Arrays.fill(dist, -1);
dist[src] = 0;
int[] Q = new int[graph.length];
int sizeQ = 0;
Q[sizeQ++] = src;
for (int i = 0; i < sizeQ; i++) {
int u = Q[i];
for (Edge e : graph[u]) {
if (dist[e.t] < 0 && e.f < e.cap) {
dist[e.t] = dist[u] + 1;
Q[sizeQ++] = e.t;
}
}
}
return dist[dest] >= 0;
}
static int dinicDfs(List<Edge>[] graph, int[] ptr, int[] dist, int dest, int u, int f) {
if (u == dest)
return f;
for (; ptr[u] < graph[u].size(); ++ptr[u]) {
Edge e = graph[u].get(ptr[u]);
if (dist[e.t] == dist[u] + 1 && e.f < e.cap) {
int df = dinicDfs(graph, ptr, dist, dest, e.t, Math.min(f, e.cap - e.f));
if (df > 0) {
e.f += df;
graph[e.t].get(e.rev).f -= df;
return df;
}
}
}
return 0;
}
public static int maxFlow(List<Edge>[] graph, int src, int dest) {
int flow = 0;
int[] dist = new int[graph.length];
while (dinicBfs(graph, src, dest, dist)) {
int[] ptr = new int[graph.length];
while (true) {
int df = dinicDfs(graph, ptr, dist, dest, src, Integer.MAX_VALUE);
if (df == 0)
break;
flow += df;
}
}
return flow;
}
}
class Edge {
int t, rev, cap, f;
public Edge(int t, int rev, int cap) {
this.t = t;
this.rev = rev;
this.cap = cap;
}
}
class Seg{
int l,r;
int min=Integer.MAX_VALUE;
Seg left=null,right=null;
long sum=0;long cnt=0;
public Seg(int l,int r){
this.l=l;
this.r=r;
}
public long[] query(int s,int e){
if(l==s&&r==e){
return new long[]{sum,cnt};
}
int mid=l+(r-l)/2; //left : to mid-1,
if(left==null)left=new Seg(l,mid);
if(right==null)right=new Seg(mid+1,r);
if(e<=mid){
return left.query(s,e);
}
else if(s>=mid+1){
return right.query(s,e);
}else{
long A[]=left.query(s,mid);
long B[]=right.query(mid+1,e);
return new long[]{A[0]+B[0],A[1]+B[1]};
}
}
public void update(int index,int v){
sum+=v;
cnt++;
if(l==r&&l==index){
return;
}
int mid=l+(r-l)/2;
if(index<=mid){
if(left==null)left=new Seg(l,mid);
left.update(index,v);
}else{
if(right==null)right=new Seg(mid+1,r);
right.update(index,v);
}
}
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;
class st {
int size;
vector<int> c;
vector<long long> a;
public:
st(int n) {
size = 1;
while (size < n) size *= 2;
c.assign(2 * size, 0);
a.assign(2 * size, 0);
}
void set(int i, long long v, int x, int lx, int rx) {
if (lx + 1 == rx) {
if (v == 0) {
c[x] = 0;
a[x] = 0;
} else {
c[x] = 1;
a[x] = v;
}
return;
}
int m = (lx + rx) / 2;
if (i < m)
set(i, v, 2 * x + 1, lx, m);
else
set(i, v, 2 * x + 2, m, rx);
c[x] = c[2 * x + 1] + c[2 * x + 2];
a[x] = a[2 * x + 1] + a[2 * x + 2];
}
void set(int i, long long v) { set(i, v, 0, 0, size); }
long long g1(int l, int r, int x, int lx, int rx) {
if (l >= rx || r <= lx) return 0;
if (l <= lx && r >= rx) return a[x];
int m = (lx + rx) / 2;
long long s1 = g1(l, r, 2 * x + 1, lx, m);
long long s2 = g1(l, r, 2 * x + 2, m, rx);
return s1 + s2;
}
long long g1(int l, int r) { return g1(l, r, 0, 0, size); }
long long g2(int l, int r, int x, int lx, int rx) {
if (l >= rx || r <= lx) return 0;
if (l <= lx && r >= rx) return c[x];
int m = (lx + rx) / 2;
long long s1 = g2(l, r, 2 * x + 1, lx, m);
long long s2 = g2(l, r, 2 * x + 2, m, rx);
return s1 + s2;
}
long long g2(int l, int r) { return g2(l, r, 0, 0, size); }
};
void solve() {
int n;
cin >> n;
vector<pair<long long, long long>> pts(n);
vector<pair<long long, long long>> pts2(n);
vector<long long> p2v(n);
for (int i = 0; i < n; i++) cin >> pts[i].first;
for (int i = 0; i < n; i++) cin >> pts[i].second;
for (int i = 0; i < n; i++) {
pts2[i].first = pts[i].second;
pts2[i].second = pts[i].first;
}
sort(pts.begin(), pts.end());
sort(pts2.begin(), pts2.end());
for (int i = 0; i < n; i++) p2v[i] = pts2[i].first;
st s(n);
for (int i = 0; i < n; i++) {
s.set(i, pts2[i].second);
}
long long total = 0;
for (int i = 0; i < n; i++) {
int ind = lower_bound(pts2.begin(), pts2.end(),
make_pair(pts[i].second, pts[i].first)) -
pts2.begin();
int ind2 = lower_bound(p2v.begin(), p2v.end(), pts[i].second) - p2v.begin();
s.set(ind, 0);
total += (s.g1(ind2, n) - s.g2(ind2, n) * pts[i].first);
}
cout << total << "\n";
}
int main() {
auto start = high_resolution_clock::now();
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cerr.tie(0);
long long t = 1;
for (long long i = 0; i < t; i++) {
solve();
}
auto stop = high_resolution_clock::now();
auto duration = duration_cast<milliseconds>(stop - start);
return 0;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
using namespace std;
const long long MOD = 1e+9 + 7;
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int INFi = 0x7f7f7f7f;
const long long MAXN = 2e+5 + 8;
vector<long long> adj[MAXN];
long long visit[MAXN] = {};
int dx8[] = {0, 1, 1, 1, 0, -1, -1, -1}, dy8[] = {1, 1, 0, -1, -1, -1, 0, 1};
int dx4[] = {0, 1, 0, -1}, dy4[] = {1, 0, -1, 0};
int t = 1;
long long n;
pair<long long, long long> seg[4 * MAXN + 8];
vector<pair<long long, long long> > arr;
vector<long long> v;
void build(long long n, long long st, long long en) {
if (st == en) {
seg[n] = arr[st];
return;
}
long long mid = (st + en) / 2;
build(2 * n, st, mid);
build(2 * n + 1, mid + 1, en);
seg[n].first = seg[2 * n].first + seg[2 * n + 1].first;
seg[n].second = seg[2 * n].second + seg[2 * n + 1].second;
}
void update(long long n, long long st, long long en, long long pos,
long long val1, long long val2) {
if (st == en) {
seg[n].first += val1;
seg[n].second += val2;
return;
}
long long mid = (st + en) / 2;
if (pos <= mid)
update(2 * n, st, mid, pos, val1, val2);
else
update(2 * n + 1, mid + 1, en, pos, val1, val2);
seg[n].first = seg[2 * n].first + seg[2 * n + 1].first;
seg[n].second = seg[2 * n].second + seg[2 * n + 1].second;
}
pair<long long, long long> query(long long n, long long st, long long en,
long long l, long long r) {
if (l <= st && en <= r) return seg[n];
if (st > r || en < l) return {0, 0};
long long mid = (st + en) / 2;
pair<long long, long long> lq = query(2 * n, st, mid, l, r);
pair<long long, long long> rq = query(2 * n + 1, mid + 1, en, l, r);
pair<long long, long long> res;
res.first = lq.first + rq.first;
res.second = lq.second + rq.second;
return res;
}
long long ans = 0;
void MAIN() {
cin >> n;
arr = vector<pair<long long, long long> >(n + 1, {0, 0});
v = vector<long long>(n + 1, 0);
for (long long i = (1); i <= n; i++) cin >> arr[i].first;
for (long long i = (1); i <= n; i++) cin >> arr[i].second;
for (long long i = (1); i <= n; i++) v[i] = arr[i].second;
sort(1 + (arr).begin(), (arr).end());
sort(1 + (v).begin(), (v).end());
for (auto [x, sp] : arr) {
if (x == 0) continue;
long long pos = lower_bound(1 + (v).begin(), (v).end(), sp) - v.begin();
ans += (query(1, 1, n, 1, pos).first * x) - (query(1, 1, n, 1, pos).second);
update(1, 1, n, pos, 1, x);
}
cout << ans;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout << fixed;
cout << setprecision(10);
;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
;
while (t--) {
MAIN();
}
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long mxn = 1e6 + 7;
const long long mod = 1e9 + 7;
const long long inf = 1e18 + 7;
int dx[] = {+1, -1, 0, 0};
int dy[] = {0, 0, -1, +1};
pair<int, int> p[mxn];
map<long long, long long> f, s;
int n;
int a[mxn], ls[mxn];
long long ans;
void upd1(int x, int val) {
while (x <= (int)1e8) {
f[x] += val;
x = (x | (x + 1));
}
}
void upd2(int x) {
while (x <= (int)1e8) {
s[x]++;
x = (x | (x + 1));
}
}
long long sum1(int x) {
long long res = 0;
while (x > 0) {
res += f[x];
x = (x & (x + 1)) - 1;
}
return res;
}
int sum2(int x) {
int res = 0;
while (x > 0) {
res += s[x];
x = (x & (x + 1)) - 1;
}
return res;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> p[i].first;
}
for (int i = 1; i <= n; i++) {
cin >> p[i].second;
ls[i] = p[i].second;
}
sort(p + 1, p + n + 1);
sort(ls + 1, ls + n + 1);
for (int i = 1; i <= n; i++) {
p[i].second = lower_bound(ls + 1, ls + n + 1, p[i].second) - ls;
}
for (int i = 1; i <= n; i++) {
ans += (1LL * p[i].first * sum2(p[i].second)) - sum1(p[i].second);
upd1(p[i].second, p[i].first);
upd2(p[i].second);
}
cout << ans;
return 0;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
CORRECT
|
python3
|
import io
import os
from collections import Counter, defaultdict, deque, namedtuple
# From: https://github.com/cheran-senthil/PyRival/blob/master/pyrival/data_structures/SegmentTree.py
class SegmentTree:
def __init__(self, data, default=0, func=max):
"""initialize the segment tree with data"""
self._default = default
self._func = func
self._len = len(data)
self._size = _size = 1 << (self._len - 1).bit_length()
self.data = [default] * (2 * _size)
self.data[_size : _size + self._len] = data
for i in reversed(range(_size)):
self.data[i] = func(self.data[i + i], self.data[i + i + 1])
def __delitem__(self, idx):
self[idx] = self._default
def __getitem__(self, idx):
return self.data[idx + self._size]
def __setitem__(self, idx, value):
idx += self._size
self.data[idx] = value
idx >>= 1
while idx:
self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
idx >>= 1
def __len__(self):
return self._len
def query(self, start, stop):
"""func of data[start, stop)"""
start += self._size
stop += self._size
res = self._default
while start < stop:
if start & 1:
res = self._func(res, self.data[start])
start += 1
if stop & 1:
stop -= 1
res = self._func(res, self.data[stop])
start >>= 1
stop >>= 1
return res
def __repr__(self):
return "SegmentTree({0})".format(self.data)
def solve(N, X, V):
# Sort by x coordinate first
indices = sorted(range(N), key=lambda i: X[i])
X = [X[i] for i in indices]
V = [V[i] for i in indices]
# Calculate where each index should be mapped to if sorted by velocity instead
BIL = 10 ** 9
vIndices = sorted(range(N), key=lambda i: V[i] * BIL + X[i])
indicesByV = {
originalIndex: sortedIndex for sortedIndex, originalIndex in enumerate(vIndices)
}
# Create a segment tree with placeholders for where the velocities should be set
segTreeTotal = SegmentTree([0 for i in range(N)], 0, lambda a, b: a + b)
segTreeCount = SegmentTree([0 for i in range(N)], 0, lambda a, b: a + b)
ans = 0
for j in range(N):
# Calculate sum X[j]-X[i] such that V[i] <= V[j], i < j
index = indicesByV[j]
count = segTreeCount.query(0, index)
totalXi = segTreeTotal.query(0, index)
# print(j, X[j], count, totalXi)
ans += X[j] * count - totalXi
segTreeCount[index] = 1
segTreeTotal[index] = X[j]
return ans
def solveBrute(N, X, V):
# Sort by x coordinate first (x is distinct)
indices = sorted(range(N), key=lambda i: X[i])
X = [X[i] for i in indices]
V = [V[i] for i in indices]
ans = 0
for j in range(N):
xj = X[j]
vj = V[j]
total = 0
totalXi = 0
count = 0
for i in range(j):
xi = X[i]
vi = V[i]
assert xi <= xj
if vi <= vj:
total += xj - xi
totalXi += xi
count += 1
print(j, X[j], count, totalXi)
assert total == X[j] * count - totalXi
ans += total
return ans
if False:
import random
random.seed(0)
N = 200000
for i in range(1):
X = [random.randint(0, 10) for i in range(N)]
V = [random.randint(0, 10) for i in range(N)]
ans = solve(N, X, V)
# assert solveBrute(N, X, V) == ans
if __name__ == "__main__":
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
(N,) = [int(x) for x in input().split()]
X = [int(x) for x in input().split()]
V = [int(x) for x in input().split()]
ans = solve(N, X, V)
print(ans)
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
CORRECT
|
python3
|
import sys,math,itertools
from collections import Counter,deque,defaultdict
from bisect import bisect_left,bisect_right
from heapq import heappop,heappush,heapify, nlargest
from copy import deepcopy
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_1(): return list(map(lambda x:int(x)-1, sys.stdin.readline().split()))
def inps(): return sys.stdin.readline()
def inpsl(x): tmp = sys.stdin.readline(); return list(tmp[:x])
def err(x): print(x); exit()
class BIT:
def __init__(self, n):
self.n = n
self.data = [0]*(n+1)
self.el = [0]*(n+1)
def sum(self, i):
s = 0
while i > 0:
s += self.data[i]
i -= i & -i
return s
def add(self, i, x):
# assert i > 0
self.el[i] += x
while i <= self.n:
self.data[i] += x
i += i & -i
def get(self, i, j=None):
if j is None:
return self.el[i]
return self.sum(j) - self.sum(i)
# 1_indexed
# n = 6
# a = [1,2,3,4,5,6]
# bit = BIT(n)
# for i,e in enumerate(a):
# bit.add(i+1,e)
# print(bit.get(2,5)) #12 (3+4+5)
n = inp()
X = inpl()
V = inpl()
d = {}; dd={}
for i,x in enumerate(sorted(X)):
d[x] = i; dd[i]=x
for i,x in enumerate(X):
X[i] = d[x]
xv = [(x,v) for x,v in zip(X,V)]
xv.sort(key=lambda x:x[0] ,reverse=True)
xv.sort(key=lambda x:x[1], reverse=True)
cnt_bit = BIT(n+10)
sum_bit = BIT(n+10)
res = 0
for x,_ in xv:
res += sum_bit.get(x,n+5) - cnt_bit.get(x,n+5)*dd[x]
sum_bit.add(x+1,dd[x])
cnt_bit.add(x+1,1)
print(res)
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
struct hash_pair {
template <class T1, class T2>
size_t operator()(const pair<T1, T2>& p) const {
auto hash1 = hash<T1>{}(p.first);
auto hash2 = hash<T2>{}(p.second);
return hash1 ^ hash2;
}
};
bool prime[10000001];
long long P = 1000000007 - 2;
long long factorial[10000001] = {0};
long long powerFunction(long long x, long long y) {
long long res = 1;
long long p = 1000000007;
x = x % p;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
void sieveFunction(long long maxLimit) {
memset(prime, true, sizeof(prime));
prime[0] = prime[1] = false;
for (long long i = 2; maxLimit >= i; i++) {
if (prime[i]) {
for (long long j = 2 * i; maxLimit >= j; j += i) prime[j] = false;
}
}
}
void factorialFunction(long long maxLimit) {
factorial[0] = 1;
for (long long i = 1; i <= maxLimit; i++)
factorial[i] = (factorial[i - 1] * i) % 1000000007;
return;
}
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
vector<pair<long long, long long>> tree[800001];
vector<long long> treesum[800001];
vector<pair<long long, long long>> v;
void build(long long node, long long start, long long end) {
if (start == end) {
vector<pair<long long, long long>> temp;
temp.push_back({v[start].second, v[start].first});
tree[node] = temp;
vector<long long> sum;
long long s = 0;
for (long long i = 0; tree[node].size() > i; i++) {
s += tree[node][i].second;
treesum[node].push_back(s);
}
return;
}
long long mid = (start + end) / 2;
build(2 * node, start, mid);
build(2 * node + 1, mid + 1, end);
long long i = 0, j = 0;
while (i < tree[2 * node].size() && j < tree[2 * node + 1].size()) {
if (tree[2 * node][i] <= tree[2 * node + 1][j]) {
tree[node].push_back(tree[2 * node][i]);
i++;
} else {
tree[node].push_back(tree[2 * node + 1][j]);
j++;
}
}
for (long long k = i; k < tree[2 * node].size(); k++)
tree[node].push_back(tree[2 * node][k]);
for (long long k = j; k < tree[2 * node + 1].size(); k++)
tree[node].push_back(tree[2 * node + 1][k]);
long long s = 0;
for (long long k = 0; tree[node].size() > k; k++) {
s += tree[node][k].second;
treesum[node].push_back(s);
}
}
long long n;
pair<long long, long long> query(long long node, long long start, long long end,
long long x) {
if (start > x && end <= n) {
pair<long long, long long> tem = {v[x].second, INT64_MIN};
long long id = lower_bound(tree[node].begin(), tree[node].end(), tem) -
tree[node].begin();
if (id) {
return {treesum[node].back() - treesum[node][id - 1],
(long long)tree[node].size() - id};
}
return {treesum[node].back(), tree[node].size()};
}
if (end <= x || start > n) {
return {0, 0};
}
long long mid = (start + end) / 2;
pair<long long, long long> t1 = query(2 * node, start, mid, x),
t2 = query(2 * node + 1, mid + 1, end, x);
return {t1.first + t2.first, t1.second + t2.second};
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
cin >> n;
long long x[n + 1], s[n + 1];
for (long long i = 1; n >= i; i++) {
cin >> x[i];
}
for (long long i = 1; n >= i; i++) {
cin >> s[i];
}
v.push_back({INT64_MIN, INT64_MIN});
for (long long i = 1; n >= i; i++) {
v.push_back({x[i], s[i]});
}
sort(v.begin(), v.end());
build(1, 1, n);
long long ans = 0;
for (long long i = 1; n >= i; i++) {
long long xx = v[i].first;
pair<long long, long long> temp = query(1, 1, n, i);
temp.first -= xx * temp.second;
ans += temp.first;
}
cout << ans << "\n";
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int MOD = int(1e9) + 7;
const long long int MOD64 = (long long int)(1e18) + 7;
const int INF = 0x7fffffff;
const long long int INF64 = 0x7fffffffffffffff;
using namespace std;
const int maxn = 1e+5;
struct abc {
long long pos;
long long sp;
} ver[maxn * 2 + 5];
int n;
long long speed[maxn * 2 + 5];
long long tree[maxn * 2 + 5][2];
long long lowbit(long long x);
long long _get(long long x, long long which);
void updata(long long x, long long val);
long long lowbit(long long x) { return x & (-x); }
bool cmp(abc a, abc b) { return a.pos < b.pos; }
long long _get(long long x, long long which) {
long long res = 0;
while (x) {
res += tree[x][which];
x -= lowbit(x);
}
return res;
}
void updata(long long x, long long val) {
while (x <= n) {
tree[x][0]++;
tree[x][1] += val;
x += lowbit(x);
}
}
int main() {
memset(tree, 0, sizeof(tree));
memset(speed, 0, sizeof(speed));
cin >> n;
for (int i = 1; i <= n; i++) cin >> ver[i].pos;
for (int i = 1; i <= n; i++) {
cin >> ver[i].sp;
speed[i] = ver[i].sp;
}
sort(ver + 1, ver + 1 + n, cmp);
sort(speed + 1, speed + 1 + n);
long long m = unique(speed + 1, speed + 1 + n) - speed - 1;
long long res = 0;
for (int i = 1; i <= n; i++) {
int x = lower_bound(speed + 1, speed + 1 + m, ver[i].sp) - speed;
res += ver[i].pos * _get(x, 0) - _get(x, 1);
updata(x, ver[i].pos);
}
cout << res;
return 0;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
CORRECT
|
java
|
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class p6
{
static class Input {
private StringTokenizer tokenizer = null;
private BufferedReader reader;
public Input(InputStream inputStream) {
reader = new BufferedReader(new InputStreamReader(inputStream));
}
public String nextLine() {
try {
return reader.readLine();
} catch (IOException e) {
throw new RuntimeException();
}
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(nextLine());
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public int[] nextIntArray(int n, int add) {
int[] result = new int[n];
for (int i = 0; i < n; i++) {
result[i] = nextInt() + add;
}
return result;
}
public long[] nextLongArray(int n, long add) {
long[] result = new long[n];
for (int i = 0; i < n; i++) {
result[i] = nextLong() + add;
}
return result;
}
public int[] nextIntArray(int n) {
return nextIntArray(n, 0);
}
}
static long get(long a[],int in)
{
long sum=0;
while(in>0)
{
sum+=a[in];
in-=in&(-in);
}
return sum;
}
static void upd(long a[],int in ,long val)
{
int n=a.length-1;
while(in<=n)
{
a[in]+=val;
in+=in&(-in);
}
}
static PrintWriter out=new PrintWriter(System.out);
static int search(long a[],long e)
{
//out.println("ele"+e);
int l=0,r=a.length-1,mid,ans=r;
while(l<=r)
{
mid=(l+r)/2;
// out.println(l+" "+r+" "+a[mid]);
if(a[mid]>=e)
{
ans=Math.min(ans,mid);
r=mid-1;
}
else
l=mid+1;
}
// out.println(ans);
return ans;
}
public static void main (String[] args) throws java.lang.Exception
{
Input s=new Input(System.in);
int t=1;
while(t-->0)
{
int n=s.nextInt();
int a[]=s.nextIntArray(n);
int b[]=s.nextIntArray(n);
myclass m1[]=new myclass[n];
TreeSet<Integer> hs=new TreeSet<>();
for(int i=0;i<n;i++)
{
m1[i]=new myclass(a[i],b[i]);
hs.add(b[i]);
}
Arrays.sort(m1,new myclass());
long c[]=new long[hs.size()];
int i1=0;
for(Integer el:hs)
c[i1++]=el;
long p1[]=new long[hs.size()+1];
long p2[]=new long[hs.size()+1];
long ans=0;
for(int i=0;i<n;i++)
{
int in=search(c,(long)m1[i].b);
//out.println(get(p1,in+1)+" "+get(p2,in+1));
ans+=get(p1,in+1)*(long)m1[i].a-get(p2,in+1);
upd(p1,in+1,1);
upd(p2,in+1,m1[i].a);
// out.println(m1[i].b+" "+ans+" "+in);
}
out.println(ans);
}
out.flush();
}
}
class myclass implements Comparator<myclass>
{
int a,b;
myclass(int a,int b)
{
this.a=a;
this.b=b;
}
myclass(){}
public int compare(myclass m1,myclass m2)
{
if(m1.a!=m2.a)
return m1.a-m2.a;
else
return m1.b-m2.b;
}
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int n, c[200005];
long long ans;
struct t {
long long x, y;
int z;
} a[200005];
int lowbit(int x) { return x & (-x); }
void update(int x) {
for (int i = x; i <= n; i += lowbit(i)) c[i]++;
}
int query(int x) {
int s = 0;
for (int i = x; i >= 1; i -= lowbit(i)) s += c[i];
return s;
}
bool cmp(t x, t y) { return x.y < y.y || x.y == y.y && x.x < y.x; }
bool cmp1(t x, t y) { return x.x < y.x; }
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%lld", &a[i].x);
for (int i = 1; i <= n; i++) scanf("%lld", &a[i].y);
sort(a + 1, a + 1 + n, cmp1);
for (int i = 1; i <= n; i++) a[i].z = i;
sort(a + 1, a + 1 + n, cmp);
memset(c, 0, sizeof(c));
ans = 0;
for (int i = 1; i <= n; i++) {
long long x;
x = query(a[i].z - 1);
ans += a[i].x * x;
update(a[i].z);
}
memset(c, 0, sizeof(c));
for (int i = n; i >= 1; i--) {
long long x = query(n) - query(a[i].z);
ans -= a[i].x * x;
update(a[i].z);
}
printf("%lld\n", ans);
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
#pragma optimization_level 3
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
using namespace std;
const long long N = 1e6 + 5;
const long long M = 1e8;
const long long mod = 1e9 + 7;
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int INFi = 0x7f7f7f7f;
long long test = 1, n, a[N], v[N], ct = 1;
vector<long long> sp;
vector<pair<long long, long long> > x;
map<long long, long long> ind;
long long tree[N][2] = {};
void update(int idx, int val, int first) {
idx++;
while (idx <= n + 1) {
tree[idx][first] += val;
idx += idx & (-idx);
}
}
long long query(int idx, int first) {
idx++;
long long sum = 0;
while (idx > 0) {
sum += tree[idx][first];
idx -= idx & (-idx);
}
return sum;
}
void solve() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) cin >> v[i], sp.push_back(v[i]);
sort(sp.begin(), sp.end());
for (auto it : sp)
if (ind[it] == 0) ind[it] = ct++;
for (int i = 1; i <= n; i++) v[i] = ind[v[i]], x.push_back({a[i], v[i]});
sort(x.begin(), x.end());
long long ans = 0;
for (int i = 0; i < n; i++) {
ans += query(x[i].second, 0) * x[i].first - query(x[i].second, 1);
update(x[i].second, 1, 0);
update(x[i].second, x[i].first, 1);
}
cout << ans << " "
<< "\n";
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout << fixed;
cout << setprecision(10);
;
for (int i = 1; i <= test; i++) {
solve();
}
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
pair<int, int> a[200005];
vector<int> g;
struct node {
long long w, num;
} tree[1000005];
void update(int x, long long val, int l, int r, int k = 1) {
if (l == r and l == x) {
tree[k].w++;
tree[k].num += val;
return;
}
int mid = l + r >> 1;
if (x <= mid)
update(x, val, l, mid, k << 1);
else
update(x, val, mid + 1, r, k << 1 | 1);
tree[k].w = tree[k << 1].w + tree[k << 1 | 1].w;
tree[k].num = tree[k << 1].num + tree[k << 1 | 1].num;
}
long long cnt, sum;
void query(int x, int y, int l, int r, int k = 1) {
if (x <= l and y >= r) {
if (tree[k].w) sum += tree[k].num, cnt += tree[k].w;
return;
}
int mid = l + r >> 1;
if (x <= mid) query(x, y, l, mid, k << 1);
if (y > mid) query(x, y, mid + 1, r, k << 1 | 1);
}
map<int, int> id;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i].first;
}
for (int i = 1; i <= n; ++i) {
cin >> a[i].second;
g.push_back(a[i].second);
}
sort(a + 1, a + 1 + n);
sort(g.begin(), g.end());
g.erase(unique(g.begin(), g.end()), g.end());
for (int i = 0; i < g.size(); i++) {
id[g[i]] = i;
}
long long ans = 0;
for (int i = 1; i <= n; i++) {
sum = cnt = 0;
query(0, id[a[i].second], 0, g.size() - 1);
ans += abs(sum - cnt * a[i].first);
update(id[a[i].second], a[i].first, 0, g.size() - 1);
}
cout << ans << endl;
return 0;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
void dout() { cerr << '\n'; }
template <typename Head, typename... Tail>
void dout(Head H, Tail... T) {
cerr << " " << H;
dout(T...);
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = 2e5 + 7;
int n, m, cnt[N];
long long sum[N];
pair<int, int> p[N];
void upd(int x, int y) {
for (; x <= m; x |= (x + 1)) {
cnt[x]++;
sum[x] += y;
}
}
int getnum(int x) {
int res = 0;
for (; x >= 0; x = (x & (x + 1)) - 1) {
res += cnt[x];
}
return res;
}
long long getsum(int x) {
long long res = 0;
for (; x >= 0; x = (x & (x + 1)) - 1) {
res += sum[x];
}
return res;
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(NULL);
cin >> n;
vector<int> vec, v;
for (int i = 1; i <= n; i++) {
cin >> p[i].first;
}
for (int i = 1; i <= n; i++) {
cin >> p[i].second;
vec.push_back(p[i].second);
}
sort((vec).begin(), (vec).end());
map<int, int> ind;
for (int i = 1; i <= n; i++) {
if (!ind.count(vec[i - 1])) {
ind[vec[i - 1]] = ++m;
}
}
for (int i = 1; i <= n; i++) {
p[i].second = ind[p[i].second];
}
sort(p + 1, p + n + 1);
long long ans = 0;
for (int i = 1; i <= n; i++) {
ans += (long long)getnum(p[i].second) * p[i].first - getsum(p[i].second);
upd(p[i].second, p[i].first);
}
cout << ans;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
CORRECT
|
java
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class MovingPoints {
public static void main(String[] args) throws IOException {
BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(f.readLine());
StringTokenizer tokenizer = new StringTokenizer(f.readLine());
ValueAndIndex[] speedAndIndices = new ValueAndIndex[n];
ValueAndIndex[] positionAndIndex = new ValueAndIndex[n];
int[] position = new int[n];
for (int i = 0; i < n; i++) {
int pos = Integer.parseInt(tokenizer.nextToken());
position[i] = pos;
positionAndIndex[i] = new ValueAndIndex(pos, i, pos);
}
Arrays.sort(positionAndIndex);
tokenizer = new StringTokenizer(f.readLine());
for (int i = 0; i < n; i++) {
speedAndIndices[i] = new ValueAndIndex(Integer.parseInt(tokenizer.nextToken()), i, position[i]);
}
position = null;
Arrays.sort(speedAndIndices);
int[] sortedSpeedIndex = new int[n];
for (int i = 0; i < n; i++) {
ValueAndIndex speedAndIndex = speedAndIndices[i];
sortedSpeedIndex[speedAndIndex.index] = i;
}
speedAndIndices = null;
FenwickTree treePosition = new FenwickTree(n);
FenwickTree treeNumOfDifferent = new FenwickTree(n);
treePosition.add(sortedSpeedIndex[positionAndIndex[0].index], positionAndIndex[0].value);
treeNumOfDifferent.add(sortedSpeedIndex[positionAndIndex[0].index], 1);
long sum = 0;
for (int i = 1; i < n; i++) {
ValueAndIndex positionAndInd = positionAndIndex[i];
//System.out.println(i + " " + positionAndInd.value + " " + sortedSpeedIndex[positionAndInd.index] + " " + treePosition.getSum(0, sortedSpeedIndex[i]) + " " + ((long) positionAndInd.value * treeNumOfDifferent.getSum(0, sortedSpeedIndex[i])));
//System.out.println(positionAndInd.value + " " + treeNumOfDifferent.getSum(0, sortedSpeedIndex[positionAndInd.index]));
sum -= treePosition.getSum(0, sortedSpeedIndex[positionAndInd.index]);
sum += (long) positionAndInd.value * treeNumOfDifferent.getSum(0, sortedSpeedIndex[positionAndInd.index]);
treePosition.add(sortedSpeedIndex[positionAndInd.index], positionAndInd.value);
treeNumOfDifferent.add(sortedSpeedIndex[positionAndInd.index], 1);
}
System.out.println(sum);
}
private static class ValueAndIndex implements Comparable<ValueAndIndex> {
int value;
int index;
int comparingValue;
public ValueAndIndex(int speed, int index, int comparingValue) {
this.value = speed;
this.index = index;
this.comparingValue = comparingValue;
}
@Override
public int compareTo(ValueAndIndex speedAndIndex) {
if (this.value < speedAndIndex.value) {
return -1;
} else if (this.value > speedAndIndex.value) {
return 1;
}
if (comparingValue < speedAndIndex.comparingValue) {
return -1;
} else if (comparingValue > speedAndIndex.comparingValue) {
return 1;
}
return 0;
}
}
private static class FenwickTree {
long[] ar;
public FenwickTree(int n) {
this.ar = new long[n];
}
public long getSum(int i) {
long sum = 0;
while (i >= 0) {
sum += ar[i];
i = (i & (i + 1)) - 1;
}
return sum;
}
public long getSum(int a, int b) {
return getSum(b) - getSum(a - 1);
}
public void add(int i, int num) {
while (i < this.ar.length) {
this.ar[i] += num;
i = i | (i + 1);
}
}
}
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 5, MOD = 1e9 + 7;
pair<long long, long long> a[N];
pair<long long, long long> tree[4 * N];
bool compare(pair<long long, long long> p1, pair<long long, long long> p2) {
return p1.first < p2.first;
}
void update(long long node, long long st, long long en, long long ccidx,
long long val, long long cnt) {
if (st == en) {
tree[node].first += val;
tree[node].second += cnt;
return;
}
long long mid = (st + en) / 2;
if (ccidx <= mid)
update(2 * node, st, mid, ccidx, val, cnt);
else
update(2 * node + 1, mid + 1, en, ccidx, val, cnt);
tree[node].first = tree[2 * node].first + tree[2 * node + 1].first;
tree[node].second = tree[2 * node].second + tree[2 * node + 1].second;
}
pair<long long, long long> query(long long node, long long st, long long en,
long long l, long long r) {
if (l > en || r < st) return {0, 0};
if (l <= st && en <= r) return tree[node];
long long mid = (st + en) / 2;
pair<long long, long long> p1 = query(2 * node, st, mid, l, r);
pair<long long, long long> p2 = query(2 * node + 1, mid + 1, en, l, r);
return {p1.first + p2.first, p1.second + p2.second};
}
void solve() {
long long n;
cin >> n;
set<long long> s;
for (long long i = 0; i < n; i++) cin >> a[i].first;
for (long long i = 0; i < n; i++) {
cin >> a[i].second;
s.insert(a[i].second);
}
map<long long, long long> cc;
long long j = 1;
for (auto x : s) {
cc[x] = j;
j++;
}
sort(a, a + n, compare);
pair<long long, long long> tem = {0ll, 0ll};
for (long long i = 0; i < 4 * N; i++) tree[i] = tem;
for (long long i = 0; i < n; i++) {
update(1, 1, j - 1, cc[a[i].second], a[i].first, 1);
}
long long ans = 0;
for (long long i = n - 1; i >= 0; i--) {
update(1, 1, j - 1, cc[a[i].second], -a[i].first, -1);
pair<long long, long long> x = query(1, 1, j - 1, 1, cc[a[i].second]);
ans += abs(x.second * a[i].first - x.first);
}
cout << ans << "\n";
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
struct Point {
LL x, v;
bool operator<(const Point &rhs) const {
if (v == rhs.v)
return x < rhs.x;
else
return v < rhs.v;
}
};
Point p[200010];
LL n, x2[200010], idx[200010], c[200010], s[200010];
void update(LL *arr, LL pos, LL val) {
for (LL i = pos; i <= n; i += i & (-i)) arr[i] += val;
}
LL query(LL *arr, LL pos) {
LL res = 0;
for (LL i = pos; i; i -= i & (-i)) res += arr[i];
return res;
}
int main() {
scanf("%lld", &n);
for (LL i = 1; i <= n; i++) scanf("%lld", &p[i].x);
for (LL i = 1; i <= n; i++) scanf("%lld", &p[i].v);
for (LL i = 1; i <= n; i++) idx[i] = i;
sort(p + 1, p + n + 1);
sort(idx + 1, idx + n + 1, [](LL i, LL j) { return p[i].x < p[j].x; });
for (LL i = 1; i <= n;) {
LL j;
for (j = i; j <= n; j++)
if (p[idx[j]].x != p[idx[i]].x)
break;
else
x2[idx[j]] = i;
i = j;
}
LL ans = 0;
for (LL i = 1; i <= n; i++) {
ans += p[i].x * query(c, x2[i]) - query(s, x2[i]);
update(c, x2[i], 1);
update(s, x2[i], p[i].x);
}
printf("%lld\n", ans);
return 0;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int n, tot, cnt, v[200001];
long long ans, c1[200001], c2[200001];
struct node {
int x, v;
inline bool operator<(const node &a) const { return x < a.x; }
} s[200001];
inline int read() {
int k = 0, f = 1;
char c;
while ((c = getchar()) < '0' || c > '9')
if (c == '-') f = -1;
while (c >= '0' && c <= '9')
k = (k << 1) + (k << 3) + (c ^ 48), c = getchar();
return k * f;
}
inline void write(long long x) {
if (x < 0) putchar('-'), x = -x;
if (x / 10) write(x / 10);
putchar(x % 10 + '0');
return;
}
inline void add(int pos, int num, bool op) {
for (; pos <= n; pos += pos & -pos)
if (op)
c1[pos] += num;
else
++c2[pos];
}
inline long long sum(int pos, bool op) {
long long res = 0;
for (; pos; pos -= pos & -pos)
if (op)
res += c1[pos];
else
res += c2[pos];
return res;
}
int main() {
n = read();
for (register int i = 1; i <= n; ++i) s[i].x = read();
for (register int i = 1; i <= n; ++i) s[i].v = read(), v[i] = s[i].v;
sort(v + 1, v + n + 1);
cnt = unique(v + 1, v + n + 1) - v - 1;
for (register int i = 1; i <= n; ++i)
s[i].v = lower_bound(v + 1, v + cnt + 1, s[i].v) - v;
sort(s + 1, s + n + 1);
for (register int i = 1; i <= n; ++i) {
long long sum1 = sum(s[i].v, 1), sum2 = sum(s[i].v, 0);
ans += sum2 * s[i].x - sum1;
add(s[i].v, s[i].x, 1);
add(s[i].v, 1, 0);
}
write(ans);
return 0;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long N = 3e5 + 5;
pair<long long, long long> p[N], a[N];
long long n;
long long fwt[N], cntf[N];
long long len;
void add(long long *b, long long x, long long v) {
while (x <= len) {
b[x] += v;
x += x & -x;
}
}
long long get(long long *b, long long x) {
long long res = 0;
while (x) {
res += b[x];
x -= x & -x;
}
return res;
}
signed main() {
ios::sync_with_stdio(false);
cin >> n;
vector<long long> velo_neg, velo_pos;
for (long long i = 0; i < n; i++) {
cin >> p[i].first;
}
for (long long i = 0; i < n; i++) {
cin >> p[i].second;
}
sort(p, p + n);
vector<long long> velo;
for (long long i = 0; i < n; i++) {
velo.push_back(p[i].second);
}
sort(velo.begin(), velo.end());
velo.erase(unique(velo.begin(), velo.end()), velo.end());
len = velo.size();
long long ans = 0;
long long sumx = 0;
for (long long i = 0; i < n; i++) {
a[i] = p[i];
a[i].second =
lower_bound(velo.begin(), velo.end(), p[i].second) - velo.begin() + 1;
ans += i * p[i].first - sumx;
sumx += p[i].first;
}
for (long long i = 0; i < n; i++) {
if (p[i].second < 0) {
ans -= (get(cntf, len) - get(cntf, a[i].second)) * p[i].first -
(get(fwt, len) - get(fwt, a[i].second));
}
add(fwt, a[i].second, p[i].first);
add(cntf, a[i].second, 1);
}
memset(cntf, 0, sizeof cntf);
memset(fwt, 0, sizeof fwt);
for (long long i = n - 1; i >= 0; i--) {
if (p[i].second > 0) {
ans -=
get(fwt, a[i].second - 1) - get(cntf, a[i].second - 1) * p[i].first;
}
if (p[i].second >= 0) {
add(fwt, a[i].second, p[i].first);
add(cntf, a[i].second, 1);
}
}
cout << ans << endl;
return 0;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e9;
long long f[200005], ans, bit[200005], s[200005];
map<long long, long long> m;
long long t, n;
pair<long long, long long> a[200005];
long long get(long long first, long long second) {
long long re = 0, cnt = 0;
for (long long i = second; i > 0; i -= (i & (-i))) {
cnt += bit[i];
re += s[i];
}
return first * cnt - re;
}
void update(long long first, long long second) {
for (long long i = second; i <= n; i += (i & (-i))) {
bit[i]++;
s[i] += first;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (long long i = 1; i <= n; i++) cin >> a[i].first;
for (long long i = 1; i <= n; i++) cin >> a[i].second, m[a[i].second] = 0;
long long k = 1;
for (map<long long, long long>::iterator it = m.begin(); it != m.end();
it++, k++)
it->second = k;
sort(a + 1, a + n + 1);
for (long long i = 1; i <= n; i++) a[i].second = m[a[i].second];
for (long long i = 1; i <= n; i++) {
ans += get(a[i].first, a[i].second);
update(a[i].first, a[i].second);
}
cout << ans << endl;
return 0;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
CORRECT
|
java
|
import java.io.*;
import java.util.*;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Housni Abdellatif
*/
public class Main {
public static void main(String[] args) throws IOException{
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Task.Reader in = new Task.Reader();
PrintWriter out = new PrintWriter(outputStream);
Task solver = new Task();
solver.solve(1, in, out);
out.close();
}
static class Task {
public void solve(int testNumber, Reader reader, PrintWriter out) throws IOException {
int n = reader.nextInt();
Point[] pts = new Point[n];
List<Integer> vs = new ArrayList<>();
for (int i = 0; i < n; ++i) {
pts[i] = new Point();
pts[i].x = reader.nextInt();
}
for (int i = 0; i < n; ++i) {
int v = reader.nextInt();
pts[i].v = v;
vs.add(v);
}
Collections.sort(vs);
Arrays.sort(pts, (p1, p2) -> p1.x - p2.x);
long ans = 0;
long[] bitV = new long[n];
long[] bitX = new long[n];
Map<Integer, Integer> map = new HashMap<>();
for (int i = n - 1; i >= 0; --i) {
map.put(vs.get(i), i);
}
//out.println(map);
for (int i = 0; i < n; ++i) {
int pos = map.get(pts[i].v);
long cnt = sum(bitV, pos);
long s = sum(bitX, pos);
//out.println(s);
ans += cnt * pts[i].x - s;
update(bitV, pos, 1);
update(bitX, pos, pts[i].x);
}
out.println(ans);
}
private void update(long[] bit, int index, int delta) {
while (index < bit.length) {
bit[index] += delta;
index = index | (index + 1);
}
}
private long sum(long[] bit, int r) {
long res = 0;
while (r >= 0) {
res += bit[r];
r = (r & (r + 1)) - 1;
}
return res;
}
private int getFirstIndex(List<Integer> list, int x) {
int l = 0;
int r = list.size() - 1;
while ( l < r) {
int m = (l + r) / 2;
if (list.get(m) < x) {
l = m + 1;
}else {
r = m;
}
}
return l;
}
static class Point {
int x;
int v;
}
static class Reader
{
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1)
{
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.')
{
while ((c = read()) >= '0' && c <= '9')
{
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
}
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int n;
struct dd {
int gt;
int x;
int v;
};
vector<dd> a;
struct Data {
long long sum;
int sl;
};
Data it[200009 * 8];
Data ans;
long long res;
void update(int i, int l, int r, int pos, int data) {
if (l > pos || r < pos) return;
if (l == r) {
it[i].sum = data;
it[i].sl = 1;
return;
}
int mid = (l + r) / 2;
update(i * 2, l, mid, pos, data);
update(i * 2 + 1, mid + 1, r, pos, data);
it[i].sum = it[i * 2].sum + it[i * 2 + 1].sum;
it[i].sl = it[i * 2].sl + it[i * 2 + 1].sl;
}
void find(int i, int l, int r, int u, int v) {
if (l > r) return;
if (r < u || l > v) return;
if (u <= l && r <= v) {
ans.sum = ans.sum + it[i].sum;
ans.sl = ans.sl + it[i].sl;
return;
}
int mid = (l + r) / 2;
find(i * 2, l, mid, u, v);
find(i * 2 + 1, mid + 1, r, u, v);
}
bool cmp(const dd& a, const dd& b) {
if (a.v == b.v) {
return a.x < b.x;
}
return a.v < b.v;
}
bool cmp1(const dd& a, const dd& b) { return a.x < b.x; }
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
dd tmp;
cin >> tmp.x;
a.push_back(tmp);
}
for (int i = 0; i < n; i++) {
cin >> a[i].v;
}
sort(a.begin(), a.end(), cmp1);
for (int i = 0; i < n; i++) {
a[i].gt = i + 1;
}
sort(a.begin(), a.end(), cmp);
update(1, 1, n, a[n - 1].gt, a[n - 1].x);
for (int i = n - 2; i >= 0; i--) {
long long tmp = a[i].x;
ans.sl = 0;
ans.sum = 0;
find(1, 1, n, a[i].gt + 1, n);
res = res + ans.sum - (tmp * ans.sl);
update(1, 1, n, a[i].gt, a[i].x);
}
cout << res << endl;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
CORRECT
|
java
|
import java.util.*;
import java.util.stream.Collectors;
public class Problem1311F {
static class Pair {
int x;
int y;
public Pair(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return String.format("[%d, %d]", x, y);
}
}
static class PairComparator implements Comparator<Pair> {
public int compare(Pair p1, Pair p2) {
return p1.x - p2.x;
}
}
static class Solver {
int n;
List<Integer> x;
List<Integer> y;
ArrayList<Pair> pairs;
ArrayList<Integer> vs;
public Solver() {
this.pairs = new ArrayList<>();
this.vs = new ArrayList<>();
read();
}
long get(ArrayList<Long> input, int position) {
long res = 0;
for (int i = position; i >= 0; i = (i & (i + 1)) - 1) {
res += input.get(i);
}
return res;
}
void update(ArrayList<Long> input, int position, int val) {
for (int i = position; i < input.size(); i |= i + 1) {
input.set(i, input.get(i) + (long) val);
}
}
void generatePairs() {
for (int i = 0; i < this.n; i++) {
Pair p = new Pair(this.x.get(i), this.y.get(i));
this.pairs.add(p);
}
this.pairs.sort(new PairComparator());
for (Pair p : this.pairs) {
this.vs.add(p.y);
}
Collections.sort(this.vs);
List<Integer> listUnique = this.vs.stream().distinct().collect(Collectors.toList());
long ans = 0;
ArrayList<Long> cnt = new ArrayList<>(listUnique.size());
ArrayList<Long> xs = new ArrayList<>(listUnique.size());
for (int i = 0; i < listUnique.size(); i++) {
cnt.add(0L);
xs.add(0L);
}
for (Pair p : this.pairs) {
int pos = Collections.binarySearch(listUnique, p.y);
ans += this.get(cnt, pos) * p.x - this.get(xs, pos);
this.update(cnt, pos, 1);
this.update(xs, pos, p.x);
}
System.out.println(ans);
}
void read() {
Scanner userInput = new Scanner(System.in);
this.n = userInput.nextInt();
userInput.nextLine();
String first = userInput.nextLine();
String second = userInput.nextLine();
this.x = Arrays.stream(first.split(" ")).map(Integer::parseInt).collect(Collectors.toList());
this.y = Arrays.stream(second.split(" ")).map(Integer::parseInt).collect(Collectors.toList());
generatePairs();
}
}
public static void main(String[] args) {
Solver s = new Solver();
}
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
CORRECT
|
java
|
/**
* ******* Created on 1/5/20 2:03 PM*******
*/
import java.io.*;
import java.util.*;
import java.util.stream.Collectors;
public class F1311 implements Runnable {
private static final int MAX = (int) (1E5 + 5);
private static final int MOD = (int) (1E9 + 7);
private static final long Inf = (long) (1E14 + 10);
private static final double eps = (double) (1E-9);
class Pair{
int a;
int b;
}
private void solve() throws IOException {
int n= reader.nextInt();
Pair[] p = new Pair[n];
for(int i=0;i<n;i++)
p[i] = new Pair();
for(int i=0;i<n;i++)
p[i].a = reader.nextInt();
for(int i=0;i<n;i++)
p[i].b = reader.nextInt();
Arrays.sort(p, new Comparator<Pair>() {
@Override
public int compare(Pair o1, Pair o2) {
return o1.a-o2.a;
}
});
List<Integer> list = new ArrayList<>();
for(int i=0;i<n;i++)
list.add(p[i].b);
Collections.sort(list);
list = list.stream().distinct().collect(Collectors.toList());
long[] cnt = new long[list.size()];
long[] xs = new long[list.size()];
long res =0;
for(int i=0;i<n;i++){
int pos = lowerBound(list, p[i].b);
res +=get( pos,cnt) * 1L*p[i].a -get(pos,xs);
upd(pos,1,cnt);
upd(pos,p[i].a,xs);
}
writer.println(res);
}
void upd(int pos, int val, long[] l){
int n = l.length;
for (; pos <n; pos |=(pos+1) ){
l[pos] +=val;
}
}
long get(int pos, long[] l){
long res =0;
while(pos >=0 ){
res += l[pos];
pos = (pos&(pos+1))-1;
}
return res;
}
public int lowerBound(List<Integer> list , int val){
int low =0, high = list.size();
while (low < high){
int mid = (low + high)>>1;
if(list.get(mid) >= val)
high = mid;
else
low = mid +1;
}
return low;
}
public static void main(String[] args) throws IOException {
try (Input reader = new StandardInput(); PrintWriter writer = new PrintWriter(System.out)) {
new F1311().run();
}
}
StandardInput reader;
PrintWriter writer;
@Override
public void run() {
try {
reader = new StandardInput();
writer = new PrintWriter(System.out);
solve();
reader.close();
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
interface Input extends Closeable {
String next() throws IOException;
String nextLine() throws IOException;
default int nextInt() throws IOException {
return Integer.parseInt(next());
}
default long nextLong() throws IOException {
return Long.parseLong(next());
}
default double nextDouble() throws IOException {
return Double.parseDouble(next());
}
default int[] readIntArray() throws IOException {
return readIntArray(nextInt());
}
default int[] readIntArray(int size) throws IOException {
int[] array = new int[size];
for (int i = 0; i < array.length; i++) {
array[i] = nextInt();
}
return array;
}
default long[] readLongArray(int size) throws IOException {
long[] array = new long[size];
for (int i = 0; i < array.length; i++) {
array[i] = nextLong();
}
return array;
}
}
private static class StandardInput implements Input {
private final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
private StringTokenizer stringTokenizer;
@Override
public void close() throws IOException {
reader.close();
}
@Override
public String next() throws IOException {
if (stringTokenizer == null || !stringTokenizer.hasMoreTokens()) {
stringTokenizer = new StringTokenizer(reader.readLine());
}
return stringTokenizer.nextToken();
}
@Override
public String nextLine() throws IOException {
return reader.readLine();
}
}
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long N = 1000010;
const long long mod = 998244353;
const long long inf = 1e9;
const long double eps = 1e-16;
const long double pi = acos(-1);
inline long long rd() {
long long p = 0;
long long f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f *= -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
p = p * 10 + ch - '0';
ch = getchar();
}
return p * f;
}
struct node {
long long x, v;
} a[N];
vector<long long> v;
bool cmp(const node &x, const node &y) { return x.x < y.x; }
long long tr[2][N], n;
void add(long long id, long long x, long long c) {
while (x <= n) {
tr[id][x] += c;
x += ((x) & (-(x)));
}
}
long long qry(long long id, long long x) {
long long s = 0;
while (x >= 1) {
s += tr[id][x];
x -= ((x) & (-(x)));
}
return s;
}
signed main() {
n = rd();
for (long long i = (1); i <= (n); i++) a[i].x = rd();
for (long long i = (1); i <= (n); i++) a[i].v = rd(), v.push_back(a[i].v);
sort(a + 1, a + n + 1, cmp);
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
for (long long i = (1); i <= (n); i++)
a[i].v = lower_bound(v.begin(), v.end(), a[i].v) - v.begin() + 1;
long long ans = 0;
for (long long i = (1); i <= (n); i++) {
ans += qry(0, a[i].v) * a[i].x - qry(1, a[i].v);
add(0, a[i].v, 1);
add(1, a[i].v, a[i].x);
}
return printf("%lld\n", ans), 0;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
long long n;
pair<long long, long long> pos[200005];
struct FenwickTree {
vector<long long> bit;
long long n;
FenwickTree(long long n) {
this->n = n;
bit.assign(n, 0);
}
FenwickTree(vector<long long> a) : FenwickTree(a.size()) {
for (size_t i = 0; i < a.size(); i++) add(i, a[i]);
}
long long sum(long long r) {
long long ret = 0;
for (; r >= 0; r = (r & (r + 1)) - 1) ret += bit[r];
return ret;
}
long long sum(long long l, long long r) { return sum(r) - sum(l - 1); }
void add(long long idx, long long delta) {
for (; idx < n; idx = idx | (idx + 1)) bit[idx] += delta;
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (long long i = 0; i < n; i++) {
long long a;
cin >> a;
pos[i].first = a;
}
pair<long long, long long> temp[n];
for (long long i = 0; i < n; i++) {
long long a;
cin >> a;
temp[i] = {a, i};
}
sort(temp, temp + n);
long long prev = -100000005;
long long cur = -1;
for (long long i = 0; i < n; i++) {
if (temp[i].first != prev) cur++;
pos[temp[i].second].second = cur;
prev = temp[i].first;
}
sort(pos, pos + n);
FenwickTree f1(200005);
FenwickTree f2(200005);
long long ans = 0;
for (long long i = 0; i < n; i++) {
ans -= f1.sum(0, pos[i].second);
ans += f2.sum(0, pos[i].second) * pos[i].first;
f1.add(pos[i].second, pos[i].first);
f2.add(pos[i].second, 1);
}
cout << ans << endl;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
vector<long> inverse(vector<long>& perm) {
long n = perm.size();
vector<long> ret(n);
for (long i = 0; i < n; ++i) {
ret[perm[i]] = i;
}
return ret;
}
vector<long> assignPermutation(const vector<long>& uniqs) {
long n = uniqs.size();
vector<pair<long, long>> withIndices(n);
for (long i = 0; i < n; ++i) {
withIndices[i] = {uniqs[i], i};
}
sort(withIndices.begin(), withIndices.end());
vector<long> temp(n);
for (long i = 0; i < n; ++i) {
temp[i] = withIndices[i].second;
}
return inverse(temp);
}
int main() {
long n;
cin >> n;
cin.ignore();
vector<long> x(n);
vector<long> v(n);
vector<pair<long, long>> data(n);
for (long i = 0; i < n; ++i) {
cin >> x[i];
}
cin.ignore();
for (long i = 0; i < n; ++i) {
cin >> v[i];
data[i] = {v[i], x[i]};
}
cin.ignore();
sort(data.begin(), data.end());
vector<long> xtemp(n);
for (long i = 0; i < n; ++i) xtemp[i] = data[i].second;
auto xranks = assignPermutation(xtemp);
sort(x.begin(), x.end());
long long totsum = 0;
for (long i = 0; i < n; ++i) {
totsum += (long long)(i)*x[i];
totsum -= (long long)(n - 1 - i) * x[i];
}
for (long i = 0; i < n; ++i) {
long netleftjumps = i - xranks[i];
totsum += (long long)(netleftjumps)*xtemp[i];
}
cout << totsum << endl;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int lowbit(int x) { return x & (-x); }
long long get(vector<long long>& f, int pos) {
long long res = 0;
for (; pos >= 0; pos = (pos & (pos + 1)) - 1) res += f[pos];
return res;
}
void upd(vector<long long>& f, int pos, int val) {
for (; pos < int(f.size()); pos |= pos + 1) {
f[pos] += val;
}
}
int main() {
int n;
cin >> n;
vector<pair<int, int>> p(n);
for (auto& pnt : p) cin >> pnt.first;
for (auto& pnt : p) cin >> pnt.second;
sort(p.begin(), p.end());
vector<int> vs;
for (auto& pnt : p) vs.push_back(pnt.second);
sort(vs.begin(), vs.end());
vs.resize(unique(vs.begin(), vs.end()) - vs.begin());
long long ans = 0;
vector<long long> cnt(vs.size()), xs(vs.size());
for (auto& pnt : p) {
int pos = lower_bound(vs.begin(), vs.end(), pnt.second) - vs.begin();
ans += get(cnt, pos) * 1ll * pnt.first - get(xs, pos);
upd(cnt, pos, 1);
upd(xs, pos, pnt.first);
}
cout << ans << endl;
return 0;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
CORRECT
|
java
|
//package Round624;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.*;
/**
* @author sguar <shugangcao@gmail.com>
* strive for greatness
* Created on 2020-01-10
*/
public class E {
InputStream is;
PrintWriter out;
private static final String INPUT_FILE_NAME = "/Users/sguar/IdeaProjects/kotlinLearn/src/input.txt";
void solve() {
int n = ni();
int[] x = na(n);
int[] v = na(n);
Pair[] pairs = new Pair[n];
for (int i = 0; i < n; i++) {
pairs[i] = new Pair(i, x[i]);
}
Arrays.sort(pairs, Comparator.comparingInt(o -> o.y));
Map<Integer, Integer> xMap = new HashMap<>();
Pair[] points = new Pair[n];
int cnt = 0;
for (int i = 0; i < n; i++) {
Pair p = pairs[i];
if (!xMap.containsKey(p.y)) {
xMap.put(++cnt, p.y);
}
points[i] = new Pair(cnt, v[pairs[i].x]);
}
Arrays.sort(points, (o1, o2) -> {
if (o1.y == o2.y) {
return o1.x - o2.x;
}
return o1.y - o2.y;
});
debug(xMap);
debug(points);
BIT bit = new BIT(cnt);
long ans = 0;
for (int i = 0; i < n; i++) {
Pair p = points[i];
int realX = xMap.get(p.x);
ans += bit.query(p.x, realX);
bit.add(p.x, realX);
}
out.println(ans);
}
public class BIT {
int n;
long[] a;
int[] cnt;
BIT(int n) {
this.n = n;
this.a = new long[n << 1];
this.cnt = new int[n << 1];
}
int lowBit(int x) {
return x & (-x);
}
void add(int index, long cnt) {
while (index <= this.n) {
this.a[index] += cnt;
this.cnt[index] += 1;
index += lowBit(index);
}
}
long query(int index, long now) {
long sum = 0;
while (index > 0) {
sum += now * this.cnt[index] - this.a[index];
index = index - lowBit(index);
}
return sum;
}
}
class Pair {
int x;
int y;
Pair(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return "x: " + x + ", y: " + y;
}
}
public static long invl(long a, long mod) {
long b = mod;
long p = 1, q = 0;
while (b > 0) {
long c = a / b;
long d;
d = a;
a = b;
b = d % b;
d = p;
p = q;
q = d - c * q;
}
return p < 0 ? p + mod : p;
}
void run() throws Exception {
is = oj ? System.in : new FileInputStream(INPUT_FILE_NAME);
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
out.flush();
debug(System.currentTimeMillis() - s + "ms");
}
public static void main(String[] args) throws Exception {
new E().run();
}
private byte[] inbuf = new byte[1024];
int lenbuf = 0;
int 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 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 && !(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 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 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();
}
}
private boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private void debug(Object... o) {
if (!oj) System.out.println(Arrays.deepToString(o));
}
private void debug(Map map) {
if (!oj) {
if (map.isEmpty()) {
System.out.println("map is empty");
} else {
map.forEach(this::debug);
}
}
}
private void debug(Object k, Object v) {
if (!oj) {
System.out.println(k.toString() + ": " + v.toString());
}
}
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
long long n, ans, tree[800002], tree1[800002];
map<long long, long long> mp, mp1;
pair<long long, long long> a[300005];
vector<long long> v;
void inc1(int idx, int val) {
for (int i = idx; i <= n; i += i & (-i)) {
tree[i] += val;
}
}
void inc2(int idx, int val) {
for (int i = idx; i <= n; i += (i & (-i))) {
tree1[i] += val;
}
}
long long sum1(int idx) {
long long pas = 0;
for (int i = idx; i > 0; i -= i & (-i)) {
pas += tree[i];
}
return pas;
}
long long sum2(int idx) {
long long pas = 0;
for (int i = idx; i > 0; i -= i & (-i)) {
pas += tree1[i];
}
return pas;
}
int main() {
std::ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i].first;
}
for (int i = 1; i <= n; i++) {
cin >> a[i].second;
if (!mp[a[i].second]) {
v.push_back(a[i].second);
}
mp[a[i].second] = 1;
}
sort(v.begin(), v.end());
for (int i = 0; i < v.size(); i++) {
mp1[v[i]] = i + 1;
}
sort(a + 1, a + n + 1);
for (int i = 1; i <= n; i++) {
ans += sum1(mp1[a[i].second]) * a[i].first - sum2(mp1[a[i].second]);
inc1(mp1[a[i].second], 1);
inc2(mp1[a[i].second], a[i].first);
}
cout << ans << endl;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
CORRECT
|
java
|
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
int [][] xi = new int[num][2];
int i=0;
while (i<num){
xi[i][0] = sc.nextInt();
i++;
}
Integer [] vi= new Integer[num];
i=0;
while (i<num){
xi[i][1] = sc.nextInt();
vi[i]= xi[i][1];
i++;
}
sc.close();
Arrays.sort(vi);
Arrays.sort(xi, new Comparator<int[]>() {
public int compare(int[] a, int[] b) {
return a[0] - b[0];
}
});
long[] x = new long[num];
long[] count = new long[num];
long result = 0;
i=0;
while(i<num){
int position = Arrays.binarySearch(vi, xi[i][1]);
result = result + (distance(position, count) * xi[i][0]) - distance(position,x);
update(position, count, 1);
update(position, x, xi[i][0]);
i++;
}
System.out.println(result);
}
public static long distance (int positon, long count[]){
long dist = 0;
while (positon >= 0) {
dist = dist + count[positon];
positon = (positon & (positon + 1)) - 1;
}
return dist;
}
private static void update (int pos, long[] t, int delta) {
while (pos < t.length) {
t[pos] += delta;
pos = pos | (pos + 1);
}
}
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
CORRECT
|
python3
|
import sys
input = sys.stdin.readline
from heapq import heappush, heappop, heapify
def query(f, i):
ret = 0
while i:
ret += f[i]
i -= (i & -i)
return ret
def update(f, i, d):
n = len(f)
while i < n:
f[i] += d
i += (i & -i)
n = int(input())
x = list(map(int, input().split()))
v = list(map(int, input().split()))
a = [()] * (n+1)
for i in range(1, n+1):
a[i] = (x[i-1], v[i-1])
a.sort()
f = [0] * (n+1)
f2 = [0] * (n+1)
for i in range(1, n+1):
update(f, i, a[i][0])
update(f2, i, 1)
b = []
for i in range(1, n+1):
b.append((-a[i][1], -i))
heapify(b)
ans = 0
while b:
v, i = heappop(b)
v, i = -v, -i
x = a[i][0]
if i > 1:
sum = query(f, i-1)
cnt = query(f2, i-1)
ans += x * cnt - sum
update(f, i, -x)
update(f2, i, -1)
print(ans)
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
struct Node {
int l, r;
int cnt = 0;
long long sum = 0;
int left = -1;
int right = -1;
} NIL;
vector<Node> st;
Node merge(Node x, Node y) {
Node z;
z.cnt = x.cnt + y.cnt;
z.sum = x.sum + y.sum;
return z;
}
void add(int u, int x, int v) {
if (not(st[u].l <= v and v <= st[u].r)) return;
if (st[u].l == v and v == st[u].r) {
st[u].cnt += 1;
st[u].sum += x;
return;
}
int m = (st[u].l + st[u].r) >> 1;
if (st[u].left == -1) {
Node x;
x.l = st[u].l, x.r = m;
st[u].left = int(st.size());
st.push_back(x);
}
if (st[u].right == -1) {
Node x;
x.l = m + 1, x.r = st[u].r;
st[u].right = int(st.size());
st.push_back(x);
}
add(st[u].left, x, v);
add(st[u].right, x, v);
Node s = merge(st[st[u].left], st[st[u].right]);
st[u].sum = s.sum;
st[u].cnt = s.cnt;
};
Node gteq(int u, int L, int R) {
if (u == -1 or st[u].r < L or R < st[u].l) return NIL;
if (L <= st[u].l and st[u].r <= R) return st[u];
return merge(gteq(st[u].left, L, R), gteq(st[u].right, L, R));
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<pair<int, int> > p(n);
for (int i = 0; i < n; i++) cin >> p[i].first;
for (int i = 0; i < n; i++) cin >> p[i].second;
sort(begin(p), end(p));
Node root;
root.l = -1e8;
root.r = 1e8;
st.emplace_back(root);
long long ans = 0;
for (int i = n - 1; i >= 0; i--) {
Node s = gteq(0, p[i].second, 1e8);
ans += s.sum - 1LL * s.cnt * p[i].first;
add(0, p[i].first, p[i].second);
}
cout << ans << '\n';
return (0);
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int n;
long long f1[200005], f2[200005], kc;
pair<long long, long long> a[200005];
bool cmp1(pair<long long, long long> x, pair<long long, long long> y) {
return x.first < y.first;
}
bool cmp2(pair<long long, long long> x, pair<long long, long long> y) {
return x.second < y.second;
}
void update(int id, long long value, long long f[]) {
while (id < 200005) {
f[id] = f[id] + value;
id = id + (id & -id);
};
}
long long rsumq(int x, long long f[]) {
long long sum = 0;
while (x != 0) {
sum += f[x];
x -= (x & -x);
};
return sum;
}
long long rsumq(int x, int y, long long f[]) {
return rsumq(y, f) - (x == 1 ? 0 : rsumq(x - 1, f));
}
void nhap() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i].first;
for (int i = 1; i <= n; i++) cin >> a[i].second;
sort(a + 1, a + 1 + n, cmp2);
int cnt = 1;
for (int i = 1; i < n; i++) {
int tmp = a[i].second;
a[i].second = cnt;
if (tmp != a[i + 1].second) cnt++;
}
a[n].second = cnt;
}
void xuly() {
sort(a + 1, a + 1 + n, cmp1);
long long ans = 0;
for (int i = 1; i <= n; i++) {
ans += (rsumq(1, a[i].second, f1) * a[i].first - rsumq(1, a[i].second, f2));
update(a[i].second, 1, f1);
update(a[i].second, a[i].first, f2);
}
cout << ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
nhap();
xuly();
return 0;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
static int MAX_N = 200000, n;
vector<ll> bitx(MAX_N + 1), bit1(MAX_N + 1);
ll sum(int i, vector<ll> &bit) {
ll res = 0;
while (i > 0) {
res += bit.at(i);
i -= i & -i;
}
return res;
}
void add(int i, int x, vector<ll> &bit) {
while (i <= n) {
bit.at(i) += x;
i += i & -i;
}
}
int main() {
cin >> n;
vi x(n);
for (int i = 0; i < (int)(n); i++) cin >> x.at(i);
vi v(n);
for (int i = 0; i < (int)(n); i++) cin >> v.at(i);
vector<pii> p;
for (int i = 0; i < (int)(n); i++) {
pii pi(x.at(i), v.at(i));
p.push_back(pi);
}
sort(p.begin(), p.end());
map<int, int> x2i;
for (int i = 0; i < (int)(n); i++) x2i[p.at(i).first] = i + 1;
sort(p.begin(), p.end(), [](pii p1, pii p2) {
if (p1.second == p2.second)
return p1.first < p2.first;
else
return p1.second < p2.second;
});
ll ans = 0;
for (int j = 0; j < (int)(n); j++) {
int x = p.at(j).first;
int i = x2i.at(x);
ans += x * sum(i - 1, bit1) - sum(i - 1, bitx);
add(i, x, bitx);
add(i, 1, bit1);
}
cout << ans << endl;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
CORRECT
|
java
|
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.StringTokenizer;
public class C1311F_2 {
public static void main(String[] args) {
var scanner = new BufferedScanner();
var writer = new PrintWriter(new BufferedOutputStream(System.out));
var n = scanner.nextInt();
var x = new int[n];
for (int i = 0; i < n; i++) {
x[i] = scanner.nextInt();
}
var v = new int[n];
for (int i = 0; i < n; i++) {
v[i] = scanner.nextInt();
}
writer.println(solve(n, x, v));
scanner.close();
writer.flush();
writer.close();
}
static final int MIN_V = (int) -1e8;
private static long solve(int n, int[] x, int[] v) {
var orderX = new ArrayList<Integer>();
for (int i = 0; i < n; i++) {
orderX.add(i);
}
orderX.sort(Comparator.comparingInt(o -> x[o]));
var ans = 0L;
var tree = new SegTree(v);
for (int i = 0; i < n; i++) {
var thisX = x[orderX.get(i)];
var thisV = v[orderX.get(i)];
if (thisV >= 0) {
var left = tree.acc(MIN_V, -1);
ans += left[1] * thisX - left[0];
var right = tree.acc(0, thisV);
ans += right[1] * thisX - right[0];
tree.add(thisV, thisX);
} else {
var left = tree.acc(MIN_V, thisV);
ans += left[1] * thisX - left[0];
tree.add(thisV, thisX);
}
}
return ans;
}
static class SegTree {
final List<Node> nodes = new ArrayList<>();
/**
* ζζζζηkeyοΌιε’γ
*/
final int[] keys;
SegTree(int[] keys) {
this.keys = distinctAndAsc(keys);
nodes.add(new Node(0, this.keys.length - 1));
}
static int[] distinctAndAsc(int[] keys) {
var distinct = new HashSet<Integer>();
for (int key : keys) {
distinct.add(key);
}
var asc = new ArrayList<>(distinct);
asc.sort(Integer::compareTo);
return asc.stream().mapToInt(i -> i).toArray();
}
void add(int key, int value) {
add(0, key, value);
}
private void add(int index, int key, int value) {
if (index < 0) {
return;
}
var node = nodes.get(index);
// debug("node %d: (low=%d,highEx=%d)", index, node.low, node.high);
if (key < keys[node.low] || keys[node.high] < key) {
return;
}
node.acc[0] += value;
node.acc[1]++;
if (node.isLeaf()) {
return;
}
if (node.left < 0) {
nodes.add(new Node(node.low, node.mid));
node.left = nodes.size() - 1;
}
add(node.left, key, value);
if (node.right < 0) {
nodes.add(new Node(node.mid + 1, node.high));
node.right = nodes.size() - 1;
}
add(node.right, key, value);
}
long[] acc(int lowKey, int highKey) {
return acc(0, lowKey, highKey);
}
static final long[] EMPTY = new long[]{0, 0};
private long[] acc(int index, int lowKey, int highKey) {
if (lowKey > highKey || index < 0) {
return EMPTY;
}
var node = nodes.get(index);
if (highKey < keys[node.low] || keys[node.high] < lowKey) {
return EMPTY;
}
if (lowKey <= keys[node.low] && keys[node.high] <= highKey) {
return node.acc;
}
var left = acc(node.left, lowKey, highKey);
var right = acc(node.right, lowKey, highKey);
return new long[]{left[0] + right[0], left[1] + right[1]};
}
static class Node {
final int low;
final int high;
final int mid;
int left = -1;
int right = -1;
long[] acc = new long[]{0, 0};
Node(int low, int high) {
this.low = low;
this.high = high;
this.mid = Math.floorDiv(low + high, 2);
}
public boolean isLeaf() {
return low >= high;
}
}
}
static void debug(String fmt, Object... args) {
System.out.println(String.format(fmt, args));
}
public static class BufferedScanner {
BufferedReader br;
StringTokenizer st;
public BufferedScanner(Reader reader) {
br = new BufferedReader(reader);
}
public BufferedScanner() {
this(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
void close() {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
static long gcd(long a, long b) {
if (a < b) {
return gcd(b, a);
}
while (b > 0) {
long tmp = b;
b = a % b;
a = tmp;
}
return a;
}
static long inverse(long a, long m) {
long[] ans = extgcd(a, m);
return ans[0] == 1 ? (ans[1] + m) % m : -1;
}
private static long[] extgcd(long a, long m) {
if (m == 0) {
return new long[]{a, 1, 0};
} else {
long[] ans = extgcd(m, a % m);
long tmp = ans[1];
ans[1] = ans[2];
ans[2] = tmp;
ans[2] -= ans[1] * (a / m);
return ans;
}
}
private static List<Integer> primes(double upperBound) {
var limit = (int) Math.sqrt(upperBound);
var isComposite = new boolean[limit + 1];
var primes = new ArrayList<Integer>();
for (int i = 2; i <= limit; i++) {
if (isComposite[i]) {
continue;
}
primes.add(i);
int j = i + i;
while (j <= limit) {
isComposite[j] = true;
j += i;
}
}
return primes;
}
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
void fio() {}
void pti() {
double timeuse = clock() * 1000.0 / CLOCKS_PER_SEC;
cerr << "Timeuse " << timeuse << "ms" << endl;
}
void end() { exit(0); }
namespace io {
const int SIZ = 55;
int que[SIZ], op, qr;
char ch;
template <class I>
inline void gi(I& w) {
ch = getchar(), op = 1, w = 0;
while (!isdigit(ch)) {
if (ch == '-') op = -1;
ch = getchar();
}
while (isdigit(ch)) {
w = w * 10 + ch - '0';
ch = getchar();
}
w *= op;
}
template <typename T, typename... Args>
inline void gi(T& t, Args&... args) {
gi(t);
gi(args...);
}
template <class I>
inline void print(I w) {
qr = 0;
if (!w) putchar('0');
if (w < 0) putchar('-'), w = -w;
while (w) que[++qr] = w % 10 + '0', w /= 10;
while (qr) putchar(que[qr--]);
}
} // namespace io
using io::gi;
using io::print;
const int N = 2e5 + 5;
int n, num;
struct node {
long long a, b;
};
node e[N];
long long c[N], t[N], sum[N];
map<long long, int> g;
int lowbit(int x) { return x & -x; }
void modify(int x, long long z) {
while (x <= n) ++t[x], sum[x] += z, x += lowbit(x);
}
long long query_t(int x) {
long long res = 0;
while (x) res += t[x], x -= lowbit(x);
return res;
}
long long query_sum(int x) {
long long res = 0;
while (x) res += sum[x], x -= lowbit(x);
return res;
}
int main() {
fio();
gi(n);
for (int i = 1; i <= n; ++i) gi(e[i].a);
for (int i = 1; i <= n; ++i) gi(e[i].b), c[i] = e[i].b;
sort(c + 1, c + n + 1);
for (int i = 1; i <= n; ++i)
if (!g[c[i]]) g[c[i]] = ++num;
sort(e + 1, e + n + 1,
[](node x, node y) { return x.a == y.a ? x.b < y.b : x.a < y.a; });
long long ans = 0;
for (int i = 1, j = 1; i <= n; ++i) {
ans += e[i].a * query_t(g[e[i].b]) - query_sum(g[e[i].b]);
while (j <= n && e[j].b == e[i].b && e[j].a == e[i].a)
modify(g[e[j].b], e[j].a), ++j;
}
print(ans);
end();
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
CORRECT
|
java
|
import java.io.*;
import java.util.*;
import java.util.concurrent.TimeUnit;
public class f624 implements Runnable{
public static void main(String[] args) {
try{
new Thread(null, new f624(), "process", 1<<26).start();
}
catch(Exception e){
System.out.println(e);
}
}
public void run() {
FastReader scan = new FastReader();
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
//PrintWriter out = new PrintWriter("file.out");
Task solver = new Task();
//int t = scan.nextInt();
int t = 1;
for(int i = 1; i <= t; i++) solver.solve(i, scan, out);
out.close();
}
static class Task {
static final int inf = Integer.MAX_VALUE;
public void solve(int testNumber, FastReader sc, PrintWriter out) {
int N = sc.nextInt();
tup[] points = new tup[N];
long[] v = new long[N];
for(int i = 0; i < N; i++) {
points[i] = new tup();
points[i].a = sc.nextInt();
}
for(int i = 0; i < N; i++) {
points[i].b = sc.nextInt();
v[i] = points[i].b;
}
shuffle(v);
shuffle(points);
Arrays.sort(points, new tup());
Arrays.sort(v);
//System.out.println(Arrays.toString(v));
int ID = 0;
for(int i = 0; i < N; i++) {
if(i == 0) {
points[i].b = ++ID;
continue;
}
if(v[i] == v[i-1])
points[i].b = ID;
else
points[i].b = ++ID;
}
// System.out.println(Arrays.toString(points));
shuffle(points);
Arrays.sort(points, new comp());
//System.out.println(Arrays.toString(points));
segt segtree = new segt(ID+1);
long totalSum = 0;
for(int i = 0; i < N; i++) {
tup res = segtree.querySum((int)points[i].b, ID);
//System.out.println(res);
long sum = res.a;
long nodes = res.b;
totalSum += sum - nodes * points[i].a;
segtree.update((int)points[i].b, (int)points[i].a);
// System.out.println(sum + " " + nodes + " " + totalSum);
// System.out.println(Arrays.toString(segtree.t));
//System.out.println("YEE");
}
out.println(totalSum);
}
static final tup ZERO = new tup(0, 0);
static class segt {
tup[] t;
int N;
public segt(int n) {
t = new tup[4*n];
for(int i = 0; i < t.length; i++) {
t[i] = new tup(0, 0);
}
N = n;
}
tup querySum(int l, int r) {
return querySum(1, 0, N-1, l, r);
}
tup querySum(int v, int tl, int tr, int l, int r) {
if (l > r)
return ZERO;
if (l == tl && r == tr)
return t[v];
int tm = (tl + tr) / 2;
tup A = querySum(v*2, tl, tm, l, Math.min(r, tm));
tup B = querySum(v*2+1, tm+1, tr, Math.max(l, tm+1), r);
return new tup(A.a + B.a, A.b + B.b);
}
void update(int pos, int new_val) {
update(1, 0, N-1, pos, new_val);
}
void update(int v, int tl, int tr, int pos, long new_val) {
//System.out.println(v + " " + tl + tr + pos + new_val);
if (tl == tr) {
t[v].a += new_val;
t[v].b ++;
} else {
int tm = (tl + tr) / 2;
if (pos <= tm)
update(v*2, tl, tm, pos, new_val);
else
update(v*2+1, tm+1, tr, pos, new_val);
t[v].a = t[v*2].a + t[v*2+1].a;
t[v].b = t[v*2].b + t[v*2+1].b;
}
}
}
}
static long binpow(long a, long b, long m) {
a %= m;
long res = 1;
while (b > 0) {
if ((b & 1) == 1)
res = res * a % m;
a = a * a % m;
b >>= 1;
}
return res;
}
static void sort(int[] x){
shuffle(x);
Arrays.sort(x);
}
static void sort(long[] x){
shuffle(x);
Arrays.sort(x);
}
static class comp implements Comparator<tup>{
@Override
public int compare(tup o1, tup o2) {
// TODO Auto-generated method stub
return Long.compare(o2.a, o1.a);
}
}
static class tup implements Comparable<tup>, Comparator<tup>{
long a, b;
tup(long a,long b){
this.a=a;
this.b=b;
}
public tup() {
}
@Override
public int compareTo(tup o){
return Long.compare(b,o.b);
}
@Override
public int compare(tup o1, tup o2) {
return Long.compare(o1.b, o2.b);
}
@Override
public int hashCode() {
return Objects.hash(a, b);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
tup other = (tup) obj;
return a==other.a && b==other.b;
}
@Override
public String toString() {
return a + " " + b;
}
}
static void shuffle(int[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
int temp = a[i];
a[i] = a[r];
a[r] = temp;
}
}
static void shuffle(long[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
long temp = a[i];
a[i] = a[r];
a[r] = temp;
}
}
static void shuffle(tup[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
tup temp = a[i];
a[i] = a[r];
a[r] = temp;
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(String s) throws FileNotFoundException {
br = new BufferedReader(new FileReader(new File(s)));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
#pragma optimize("O3")
using namespace std;
const long long MOD = 1e9 + 9;
const long long INF = 1e9 + 7;
const int base = 2e5 + 1;
const long long MAX = 1e15 + 1;
const double EPS = 1e-9;
const double PI = acos(-1.);
const int MAXN = 2 * 1e5 + 47;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
pair<long long, long long> t[MAXN];
map<int, int> ind, vx;
void add(int x, long long val) {
for (int j = x; j < MAXN; j = j | (j + 1)) {
t[j].first += val;
t[j].second++;
}
}
long long get(int x) {
long long res = 0;
for (int j = x; j >= 0; j = (j & (j + 1)) - 1) {
res += t[j].first;
}
return res;
}
long long cnt(int x) {
long long res = 0;
for (int j = x; j >= 0; j = (j & (j + 1)) - 1) {
res += t[j].second;
}
return res;
}
int main() {
int n;
cin >> n;
vector<long long> x(n), v(n);
for (long long i = (0); i < (n); ++i) scanf("%lld", &x[i]);
for (long long i = (0); i < (n); ++i) {
scanf("%lld", &v[i]);
vx[x[i]] = v[i];
}
sort(v.begin(), v.end());
sort(x.begin(), x.end());
long long res = 0;
for (long long i = (0); i < (n); ++i) ind[v[i]] = i;
for (long long i = (0); i < (n); ++i) {
int cur = ind[vx[x[i]]];
res += x[i] * cnt(cur) - get(cur);
add(cur, x[i]);
}
cout << res;
return 0;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
pair<long long int, long long int> final[200001];
void up(long long int i, long long int x, long long int j) {
while (i < 200001) {
j == 0 ? final[i].first += x : final[i].second += x;
i += (i & (-i));
}
}
long long int q(long long int i, long long int j) {
long long int ans = 0;
while (i) {
ans += (j == 0 ? final[i].first : final[i].second);
i -= (i & (-i));
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int n;
cin >> n;
long long int a[n];
pair<long long int, long long int> b[n];
for (long long int i = 0; i < n; i++) {
cin >> a[i];
}
for (long long int i = 0; i < n; i++) {
cin >> b[i].first;
b[i].second = a[i];
}
sort(b, b + n);
map<long long int, long long int> mapper;
sort(a, a + n);
for (long long int i = 0; i < n; i++) {
mapper[a[i]] = i + 1;
}
long long int ans = 0;
for (auto e : b) {
long long int sp = e.first, pos = e.second;
long long int ind = mapper[pos];
long long int sum = q(ind - 1, 1);
long long int points = q(ind - 1, 0);
ans += pos * points - sum;
up(ind, 1, 0);
up(ind, pos, 1);
}
cout << ans;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
const int inf = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const double eps = 1e-4;
const double pi = acos(-1.0);
void debug() { cerr << '\n'; }
template <typename T, typename... Ts>
void debug(T x, Ts... y) {
cerr << "\033[31m" << x << "\033[0m";
if (sizeof...(y) > 0) cerr << ' ';
debug(y...);
}
template <typename T>
void debug(const T &a, int l, int r, char c) {
for (int i = l; i <= r; ++i)
cerr << "\033[31m" << a[i] << "\033[0m" << (i == r ? '\n' : c);
}
template <typename T>
void debug(vector<T> a) {
for (int i = 0; i < (int)a.size(); ++i)
cerr << "\033[31m" << a[i] << "\033[31m"
<< " \n"[i == ((int)a.size() - 1)];
}
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
long long qpow(long long x, long long y) {
long long ans = 1;
for (; y > 0; y >>= 1) {
if (y & 1) ans *= x;
x *= x;
}
return ans;
}
long long qpow(long long x, long long y, int MOD) {
long long ans = 1;
for (; y > 0; y >>= 1) {
if (y & 1) ans = ans * x % MOD;
x = x * x % MOD;
}
return ans;
}
void exgcd(long long a, long long b, long long &x, long long &y) {
if (!b) {
x = 1;
y = 0;
return;
}
exgcd(b, a % b, x, y);
long long X = x;
x = y;
y = X - a / b * y;
}
long long inv(long long x, int mod = MOD) { return qpow(x, mod - 2, mod); }
long long m_m(long long a, int mod = MOD) { return (a % mod + mod) % mod; }
const int N = 2e5 + 5;
vector<pair<long long, int>> tr(N + 1);
int tot;
int lsh[N];
void add(int x, int v) {
for (; x <= tot; x += -x & x) tr[x].first += v, ++tr[x].second;
}
pair<long long, int> qy(int x) {
pair<long long, int> ans;
for (; x > 0; x -= -x & x)
ans.first += tr[x].first, ans.second += tr[x].second;
return ans;
}
signed main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int n;
cin >> n;
vector<pair<int, int>> a(n + 1);
for (int i = (1); i < (n + 1); ++i) cin >> a[i].first;
for (int i = (1); i < (n + 1); ++i) {
cin >> a[i].second;
lsh[i] = a[i].second;
}
sort(a.begin(), a.end());
sort(lsh + 1, lsh + n + 1);
tot = unique(lsh + 1, lsh + n + 1) - lsh;
long long ans = 0;
for (int i = (1); i < (n + 1); ++i) {
int j = lower_bound(lsh + 1, lsh + tot, a[i].second) - lsh;
pair<long long, int> t = qy(j);
ans += 1ll * a[i].first * t.second - t.first;
add(j, a[i].first);
}
cout << ans;
return 0;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
CORRECT
|
java
|
//package round624;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.InputMismatchException;
public class F {
InputStream is;
PrintWriter out;
String INPUT = "";
void solve()
{
int n = ni();
int[] x = na(n);
int[] v = na(n);
for(int i = 0;i < n;i++)v[i] += 100000000;
v = shrink(v);
int[][] co = new int[n][];
for(int i = 0;i < n;i++){
co[i] = new int[]{x[i], v[i]};
}
Arrays.sort(co, new Comparator<int[]>() {
public int compare(int[] a, int[] b) {
return a[0] - b[0];
}
});
long[] ft = new long[n+5];
long[] nums = new long[n+5];
long ans = 0;
for(int[] c : co){
ans += (long)c[0] * sumFenwick(nums, c[1]) - sumFenwick(ft, c[1]);
addFenwick(ft, c[1], c[0]);
addFenwick(nums, c[1], 1);
}
out.println(ans);
}
public static long sumFenwick(long[] ft, int i)
{
long sum = 0;
for(i++;i > 0;i -= i&-i)sum += ft[i];
return sum;
}
public static void addFenwick(long[] ft, int l, int r, long v)
{
addFenwick(ft, l, v);
addFenwick(ft, r, -v);
}
public static void addFenwick(long[] ft, int i, long v)
{
if(v == 0)return;
int n = ft.length;
for(i++;i < n;i += i&-i)ft[i] += v;
}
public static long[] radixSort(long[] f){ return radixSort(f, f.length); }
public static long[] radixSort(long[] f, int n)
{
long[] to = new long[n];
{
int[] b = new int[65537];
for(int i = 0;i < n;i++)b[1+(int)(f[i]&0xffff)]++;
for(int i = 1;i <= 65536;i++)b[i]+=b[i-1];
for(int i = 0;i < n;i++)to[b[(int)(f[i]&0xffff)]++] = f[i];
long[] d = f; f = to;to = d;
}
{
int[] b = new int[65537];
for(int i = 0;i < n;i++)b[1+(int)(f[i]>>>16&0xffff)]++;
for(int i = 1;i <= 65536;i++)b[i]+=b[i-1];
for(int i = 0;i < n;i++)to[b[(int)(f[i]>>>16&0xffff)]++] = f[i];
long[] d = f; f = to;to = d;
}
{
int[] b = new int[65537];
for(int i = 0;i < n;i++)b[1+(int)(f[i]>>>32&0xffff)]++;
for(int i = 1;i <= 65536;i++)b[i]+=b[i-1];
for(int i = 0;i < n;i++)to[b[(int)(f[i]>>>32&0xffff)]++] = f[i];
long[] d = f; f = to;to = d;
}
{
int[] b = new int[65537];
for(int i = 0;i < n;i++)b[1+(int)(f[i]>>>48&0xffff)]++;
for(int i = 1;i <= 65536;i++)b[i]+=b[i-1];
for(int i = 0;i < n;i++)to[b[(int)(f[i]>>>48&0xffff)]++] = f[i];
long[] d = f; f = to;to = d;
}
return f;
}
public static int[] shrink(int[] a) {
int n = a.length;
long[] b = new long[n];
for (int i = 0; i < n; i++)
b[i] = (long) a[i] << 32 | i;
b = radixSort(b);
int[] ret = new int[n];
int p = 0;
for (int i = 0; i < n; i++) {
if (i > 0 && (b[i] ^ b[i - 1]) >> 32 != 0)
p++;
ret[(int) b[i]] = p;
}
return ret;
}
void run() throws Exception
{
is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
out.flush();
tr(System.currentTimeMillis()-s+"ms");
}
public static void main(String[] args) throws Exception { new F().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 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 && !(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 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 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();
}
}
private boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); }
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.