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
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; inline int lowbit(int x) { return x & (-x); } long long getsum(vector<long long> &V, int pos) { long long ret = 0; for (; pos; pos -= lowbit(pos)) ret += V[pos]; return ret; } void add(vector<long long> &V, int pos, int k) { for (; pos < (int)V.size(); pos += lowbit(pos)) V[pos] += k; } int main() { int n; cin >> n; vector<pair<int, int>> p(n); for (auto &pnt : p) { int x; scanf("%d", &x); pnt.first = x; } for (auto &pnt : p) { int v; scanf("%d", &v); pnt.second = v; } sort(p.begin(), p.end()); vector<int> vs{(int)(-1e9)}; 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 += getsum(cnt, pos) * pnt.first - getsum(xs, pos); add(cnt, pos, 1), add(xs, pos, pnt.first); } 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> using namespace std; const int N = 2e5 + 10; int x[N]; long long ans; pair<int, int> a[N]; int main(void) { std::ios::sync_with_stdio(false); int n; cin >> n; for (int i = 0; i < n; i++) cin >> a[i].second, x[i] = a[i].second; for (int i = 0; i < n; i++) cin >> a[i].first; sort(a, a + n), sort(x, x + n); for (int i = 0; i < n; i++) ans += (long long)(lower_bound(x, x + n, a[i].second) - x - n + 1 + i) * 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
cpp
#include <bits/stdc++.h> using namespace std; int b[200005], m; long long tr[800005][2]; struct node { int x, v; bool operator<(const node t) const { return x < t.x; } } a[200005]; int lsh(long long x) { return lower_bound(b + 1, b + 1 + m, x) - b; } long long query(int l, int r, int x, int y, int k, int p) { if (l == x && r == y) return tr[p][k]; int mid = l + r >> 1; if (y <= mid) return query(l, mid, x, y, k, p << 1); else if (x > mid) return query(mid + 1, r, x, y, k, p << 1 | 1); else return query(l, mid, x, mid, k, p << 1) + query(mid + 1, r, mid + 1, y, k, p << 1 | 1); } void update(int l, int r, int x, int y, int p) { if (l == r) { tr[p][0]++; tr[p][1] += y; return; } int mid = l + r >> 1; if (x <= mid) update(l, mid, x, y, p << 1); else update(mid + 1, r, x, y, p << 1 | 1); tr[p][0] = tr[p << 1][0] + tr[p << 1 | 1][0]; tr[p][1] = tr[p << 1][1] + tr[p << 1 | 1][1]; } int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i].x); for (int i = 1; i <= n; i++) { scanf("%d", &a[i].v); b[i] = a[i].v; } sort(b + 1, b + 1 + n); m = unique(b + 1, b + 1 + n) - b - 1; sort(a + 1, a + 1 + n); long long ans = 0; for (int i = 1; i <= n; i++) { int t = lsh(a[i].v); ans += query(1, m, 1, t, 0, 1) * a[i].x - query(1, m, 1, t, 1, 1); update(1, m, t, a[i].x, 1); } printf("%lld", 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 int infinity = 9e18; pair<long long int, long long int> t1[4 * 200005] = {}; pair<long long int, long long int> t2[4 * 200005] = {}; struct SegmentTree { void update(pair<long long int, long long int> t[], int v, int tl, int tr, int pos, int x) { if (tl == tr) { t[v].first += x; t[v].second += 1; } else { int tm = (tl + tr) / 2; if (pos <= tm) update(t, 2 * v, tl, tm, pos, x); else update(t, 2 * v + 1, tm + 1, tr, pos, x); t[v].first = (t[2 * v].first + t[2 * v + 1].first); t[v].second = (t[2 * v].second + t[2 * v + 1].second); } } pair<long long int, long long int> query( pair<long long int, long long int> t[], int v, int tl, int tr, int l, int r) { if (l > r) return {0, 0}; if (tl == l && tr == r) { return t[v]; } else { int tm = (tl + tr) / 2; pair<long long int, long long int> left = query(t, 2 * v, tl, tm, l, min(tm, r)); pair<long long int, long long int> right = query(t, 2 * v + 1, tm + 1, tr, max(tm + 1, l), r); pair<long long int, long long int> p; p.first = left.first + right.first; p.second = left.second + right.second; return p; } } }; long long int func(vector<pair<long long int, long long int> > &vp, pair<long long int, long long int> t[]) { long long int n = vp.size(); set<long long int> s; for (auto i : vp) { s.insert(i.second); } long long int speed = 0; map<long long int, long long int> m; for (auto i : s) { m[i] = speed++; } for (int i = 0; i < n; i++) { vp[i].second = m[vp[i].second]; } SegmentTree st; long long int total = 0; for (int i = 0; i < n; i++) { pair<long long int, long long int> p = st.query(t, 1, 0, n - 1, 0, vp[i].second); total = total + (vp[i].first) * p.second - p.first; st.update(t, 1, 0, n - 1, vp[i].second, vp[i].first); } return total; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int n; cin >> n; vector<pair<long long int, long long int> > vp(n); for (int i = 0; i < n; i++) { cin >> vp[i].first; } for (int i = 0; i < n; i++) { cin >> vp[i].second; } sort(vp.begin(), vp.end()); vector<pair<long long int, long long int> > pos, neg; long long int sum = 0; long long int total = 0; long long int count = 0; for (int i = n - 1; i >= 0; i--) { if (vp[i].second >= 0) { sum += vp[i].first; count++; pos.push_back({vp[i].first, vp[i].second}); } else { total = total + sum - vp[i].first * count; neg.push_back({vp[i].first, vp[i].second}); } } sort(pos.begin(), pos.end()); sort(neg.begin(), neg.end()); total = total + func(pos, t1) + func(neg, t2); cout << total; 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 int maxn = 2e5 + 5; int n; int b[maxn]; struct node { int x, v; } a[maxn]; bool cmp(node q1, node q2) { return q1.v == q2.v ? q1.x < q2.x : q1.v < q2.v; } struct BIT { long long c[maxn][2]; void init() { memset(c, 0, sizeof(c)); } int lowbit(int x) { return x & (-x); } void update(int k, int x, int y) { for (int i = k; i <= n; i += lowbit(i)) { c[i][0] += x; c[i][1] += y; } } pair<long long, long long> query(int k) { long long ans1 = 0, ans2 = 0; for (int i = k; i; i -= lowbit(i)) { ans1 += c[i][0]; ans2 += c[i][1]; } return make_pair(ans1, ans2); } pair<long long, long long> ask(int l, int r) { pair<long long, long long> tmp1 = query(r); pair<long long, long long> tmp2 = query(l - 1); pair<long long, long long> tmp = make_pair(tmp1.first - tmp2.first, tmp1.second - tmp2.second); return tmp; } } T[2]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i].x); int cnt = 0; for (int i = 1; i <= n; i++) { scanf("%d", &a[i].v); b[++cnt] = a[i].x; } sort(b + 1, b + cnt + 1); sort(a + 1, a + n + 1, cmp); cnt = unique(b + 1, b + cnt + 1) - b - 1; for (int i = 1; i <= n; i++) { a[i].x = lower_bound(b + 1, b + cnt + 1, a[i].x) - b; T[1].update(a[i].x, 1, b[a[i].x]); } long long ans = 0; pair<long long, long long> tmp; for (int i = 1; i <= n; i++) { T[0].update(a[i].x, 1, b[a[i].x]); T[1].update(a[i].x, -1, -b[a[i].x]); tmp = T[0].ask(1, a[i].x); ans += tmp.first * b[a[i].x] - tmp.second; tmp = T[1].ask(a[i].x, n); ans += tmp.second - tmp.first * b[a[i].x]; } printf("%lld\n", ans / 2); 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 int maxn = 1e6 + 5; pair<int, int> p[maxn]; int n; long long d[maxn]; long long e[maxn]; void add(long long *c, int i, int k) { while (i <= n) { c[i] += k; i += i & (-i); } } long long sum(long long *c, int i) { long long res = 0; while (i > 0) { res += c[i]; i -= i & (-i); } return res; } int main() { cin >> n; for (int i = 0; i <= n - 1; i++) cin >> p[i].first; for (int i = 0; i <= n - 1; i++) cin >> p[i].second; sort(p, p + n); vector<int> es(n); for (int i = 0; i <= n - 1; i++) es[i] = p[i].second; sort(es.begin(), es.end()); long long res = 0; for (int i = 0; i <= n - 1; i++) { int pp1 = lower_bound(es.begin(), es.end(), p[i].second) - es.begin() + 1; int pp2 = upper_bound(es.begin(), es.end(), p[i].second) - es.begin() + 1; res += sum(d, pp2 - 1) * p[i].first - sum(e, pp2 - 1); add(d, pp1, 1); add(e, pp1, p[i].first); } cout << 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; int II() { int q; scanf("%d", &q); return q; } long long LII() { long long q; scanf("%lld", &q); return q; } const long long Mx = 200005; long long N; vector<long long> tree[Mx * 4], cum[Mx * 4]; vector<pair<long long, long long> > v(Mx + 1); void update(long long node, long long b, long long e) { if (b == e) { tree[node].push_back(v[b].first); cum[node].push_back(v[b].first); return; } update((node * 2), b, (b + e) / 2); update((node * 2) + 1, (b + e) / 2 + 1, e); for (auto it : tree[(node * 2)]) tree[node].push_back(it); for (auto it : tree[(node * 2) + 1]) tree[node].push_back(it); sort(tree[node].begin(), tree[node].end()); cum[node] = tree[node]; for (long long i = cum[node].size() - 2; i >= 0; i--) { cum[node][i] += cum[node][i + 1]; } } long long query(long long node, long long b, long long e, long long l, long long r, long long val) { if (r < l) return 0; if (b > r or e < l) return 0; if (b >= l and e <= r) { long long up = upper_bound(tree[node].begin(), tree[node].end(), val) - tree[node].begin(); if (up == tree[node].size()) return 0; long long q = tree[node].size() - up; return cum[node][up] - ((q * val)); } return query((node * 2), b, (b + e) / 2, l, r, val) + query((node * 2) + 1, (b + e) / 2 + 1, e, l, r, val); } void solve() { cin >> N; for (long long i = 1; i <= N; i++) cin >> v[i].first; for (long long i = 1; i <= N; i++) cin >> v[i].second; sort(v.begin() + 1, v.begin() + 1 + N, [](pair<long long, long long> a, pair<long long, long long> b) { if (a.second == b.second) return a.first < b.first; return a.second < b.second; }); update(1, 1, N); long long ans = 0; for (long long i = 1; i <= N; i++) { ans += query(1, 1, N, i + 1, N, v[i].first); } cout << ans << endl; } int32_t main() { long long tt; tt = 1; long long cas = 1; while (tt--) { 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; using ll = long long; using pii = pair<int, int>; using db = double; using vi = vector<int>; const int inf = 0x3f3f3f3f; const db eps = 1e-8; const int mod = 1e9 + 7; ll qpow(ll a, ll b) { ll ret = 1; while (a) { if (b & 1) ret = ret * a % mod; a = a * a % mod; b >>= 1; } return ret; } int n, m, k; vector<pii> a; const int maxn = 2e5 + 7; struct node { ll b[maxn]; void update(int i, ll v) { for (; i < maxn; i += (i & -i)) b[i] += v; } ll query(int i) { ll ret = 0; for (; i > 0; i -= (i & -i)) ret += b[i]; return ret; } } bit1, bit2; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; a.resize(n); vector<ll> co(n); for (int i = 0; i < n; ++i) { cin >> a[i].first; } for (int i = 0; i < n; i++) { cin >> a[i].second; co[i] = a[i].second; } sort(co.begin(), co.end()); sort(a.begin(), a.end()); co.erase(unique(co.begin(), co.end()), co.end()); for (int i = 0; i < n; i++) a[i].second = lower_bound(co.begin(), co.end(), a[i].second) - co.begin() + 1; ll ans = 0; for (int i = 0; i < n; i++) { ans += bit2.query(a[i].second) * a[i].first - bit1.query(a[i].second); bit1.update(a[i].second, a[i].first); bit2.update(a[i].second, 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> using namespace std; const long long maxn = 200010; const long long N = 200000; long long n; struct Node { long long v, x; bool operator<(const Node& b) const { return x < b.x; } } p[maxn]; struct BIT { long long b[maxn]; inline long long lowbit(long long x) { return x & (-x); } void update(long long x, long long v) { for (long long i = x; i <= N; i += lowbit(i)) b[i] += v; } long long query(long long x) { long long ans = 0; for (long long i = x; i > 0; i -= lowbit(i)) ans += b[i]; return ans; } } a, b; long long tot[maxn], len[maxn]; long long c[maxn], l[maxn]; map<long long, long long> mm; long long ans; signed main() { cin >> n; for (long long i = 1; i <= n; i++) cin >> p[i].x; for (long long i = 1; i <= n; i++) cin >> p[i].v, mm[p[i].v]++; long long id = 0; for (map<long long, long long>::iterator it = mm.begin(); it != mm.end(); it++) it->second = ++id; for (long long i = 1; i <= n; i++) p[i].v = mm[p[i].v]; sort(p + 1, p + n + 1); for (long long i = 1; i <= n; i++) { ans += a.query(p[i].v) * p[i].x - b.query(p[i].v); a.update(p[i].v, 1); b.update(p[i].v, p[i].x); } 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 mod = 998244353; const int N = 2e5 + 2; const int inf = 2e9; const long long linf = 4e18; long long val[N], cnt[N]; void upd(int x, long long v) { int z = 0; if (v < 0) z--; else z++; for (; x < N; x += x & (-x)) { cnt[x] += z; val[x] += v; } } array<long long, 2> get(int x) { array<long long, 2> data; data = {0, 0}; for (; x > 0; x -= x & (-x)) { data[0] += cnt[x]; data[1] += val[x]; } return data; } array<long long, 2> get2(int x) { array<long long, 2> a = get(N - 1); array<long long, 2> b = get(x - 1); return {a[0] - b[0], a[1] - b[1]}; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cerr.tie(0); int n; cin >> n; vector<array<long long, 2>> data(n), v(n); for (int i = 0; i < n; i++) cin >> data[i][0]; for (int i = 0; i < n; i++) { cin >> data[i][1]; v[i] = {data[i][1], i}; } sort(v.begin(), v.end()); int h = 0; data[v[0][1]][1] = ++h; for (int i = 1; i < n; i++) { data[v[i][1]][1] = h; if (v[i][0] != v[i - 1][0]) data[v[i][1]][1] = ++h; } sort(data.begin(), data.end()); for (int i = 0; i < n; i++) { upd(data[i][1], data[i][0]); } long long ans = 0; for (int i = 0; i < n; i++) { upd(data[i][1], -data[i][0]); array<long long, 2> d = get2(data[i][1]); ans += d[1] - (d[0] * data[i][0]); } 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
cpp
#include <bits/stdc++.h> using namespace std; template <class T> struct fen { vector<T> fenwick; long long size; fen(long long sz) { size = sz; fenwick.resize(size); for (long long i = 0; i < size; i++) fenwick[i] = 0; } fen(vector<T> arr) { size = arr.size(); fenwick.resize(size); for (long long i = 0; i < size; i++) fenwick[i](0); for (long long i = 0; i < size; i++) update(i, arr[i]); } void add(long long index, T add) { for (long long k = index; k < size; k |= (~k & -~k)) { fenwick[k] = fenwick[k] + add; } } void update(long long index, T nval) { add(index, nval - rangeQuery(index, index)); } T query(long long to) { if (to < 0) return 0; T sum = 0; for (long long curr = to + 1; curr > 0; curr ^= (curr & -curr)) { sum = sum + fenwick[curr - 1]; } return sum; } T rangeQuery(long long from, long long to) { if (from > to) return 0; if (!from) return query(to); return query(to) - query(from - 1); } void prll() { for (long long i = 0; i < size; i++) cout << query(i) << " "; cout << endl; } }; const long long sz = 2e5 + 5; long long n; pair<long long, long long> xv[sz]; vector<int> tmp; map<int, int> rkspeed; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; for (long long i = 0; i < n; i++) cin >> xv[i].first; for (long long i = 0; i < n; i++) cin >> xv[i].second; sort(xv, xv + n); for (long long i = 0; i < n; i++) tmp.push_back(xv[i].second); sort(tmp.begin(), tmp.end()); vector<int> t2{tmp[0]}; for (long long i = 1; i < n; i++) { if (tmp[i] != t2.back()) t2.push_back(tmp[i]); } for (long long i = 0; i < t2.size(); i++) rkspeed[t2[i]] = i; fen<int> fcnt(n); fen<long long> sums(n); long long tot = 0; for (long long i = 0; i < n; i++) { int mrk = rkspeed[xv[i].second]; long long bamt = fcnt.query(mrk); tot += bamt * xv[i].first - sums.query(mrk); fcnt.add(mrk, 1); sums.add(mrk, xv[i].first); } cout << tot; 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; long long int get(vector<long long int> &arr, long long int ind, long long int n) { long long int ans = 0; for (; ind > 0; ind -= (ind & (-ind))) ans += arr[ind]; return ans; } void upd(vector<long long int> &arr, long long int ind, long long int x, long long int n) { for (; ind <= n; ind += (ind & (-ind))) arr[ind] += x; } int main() { long long int n; cin >> n; pair<long long int, long long int> arr[n]; for (long long int i = 0; i < n; i++) cin >> arr[i].first; for (long long int i = 0; i < n; i++) cin >> arr[i].second; vector<int> vec(n); for (long long int i = 0; i < n; i++) vec.push_back(arr[i].second); sort(vec.begin(), vec.end()); vec.resize(unique(vec.begin(), vec.end()) - vec.begin()); vector<long long int> cnt(vec.size() + 1), cs(vec.size() + 1); sort(arr, arr + n); long long int ans = 0; long long int n1 = vec.size(); for (long long int i = 0; i < n; i++) { long long int pos = lower_bound(vec.begin(), vec.end(), arr[i].second) - vec.begin(); ans += get(cnt, pos + 1, n1) * arr[i].first - get(cs, pos + 1, n1); upd(cnt, pos + 1, 1, n1); upd(cs, pos + 1, arr[i].first, n1); } 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 com.netease.music.codeforces.round624.div3; import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.util.Scanner; /** * Created by dezhonger on 2020/2/27 */ public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int[][] xv = new int[n][2]; Integer[] v = new Integer[n]; for (int i = 0; i < n; i++) { xv[i][0] = scanner.nextInt(); } for (int i = 0; i < n; i++) { xv[i][1] = scanner.nextInt(); v[i] = xv[i][1]; } Arrays.sort(v); Arrays.sort(xv, (o1, o2) -> { if (o1[0] != o2[0]) return Integer.compare(o1[0], o2[0]); return Integer.compare(o1[1], o2[1]); }); FenwickTreeLong f1 = new FenwickTreeLong(n); FenwickTreeLong f2 = new FenwickTreeLong(n); long result = 0; for (int i = 0; i < n; i++) { //按照x坐标从小到大进行枚举 //找到离散化后v的位置 int pos = Arrays.binarySearch(v, xv[i][1]); pos++; //速度比当前速度小的有贡献,贡献为距离之差 //有num个点的速度为当前速度小 long num = f1.sum(pos); //这num个点的位置的坐标之和为xsum long xsum = f2.sum(pos); //计算对答案的贡献 result += num * xv[i][0] - xsum; f1.change(pos, 1); f2.change(pos, xv[i][0]); } System.out.println(result); } } class FenwickTreeLong { long[] t; int n; public FenwickTreeLong(int n) { this.n = n; this.t = new long[n + 1]; } void change(int pos, int val) { while (pos <= n) { t[pos] += val; pos += lowbit(pos); } } int lowbit(int k) { return k & (-k); } long sum(int pos) { long ans = 0; while (pos > 0) { ans += t[pos]; pos -= lowbit(pos); } return ans; } long ask(int l, int r) { return sum(r) - sum(l); } }
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> long double pi = acos(-1.0); const int mod = 1e9 + 7; using namespace std; const int N = 2e5 + 5; vector<long long> F(N), F2(N); long long sum(int r, vector<long long> &t) { long long result = 0; for (; r >= 0; r = (r & (r + 1)) - 1) result += t[r]; return result; } void inc(int i, long long delta, vector<long long> &t) { for (; i < N; i = (i | (i + 1))) t[i] += delta; } long long sum(int l, int r, vector<long long> &t) { return sum(r, t) - sum(l - 1, t); } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cout.precision(20); cout.setf(ios::fixed); int n; cin >> n; vector<pair<int, int> > pts(n); for (int(i) = 0; (i) < n; (i)++) { cin >> pts[i].first; } vector<int> vv; for (int(i) = 0; (i) < n; (i)++) { cin >> pts[i].second; vv.push_back(pts[i].second); } sort((vv).begin(), (vv).end()); vv.resize(unique((vv).begin(), (vv).end()) - vv.begin()); for (int(i) = 0; (i) < n; (i)++) { pts[i].second = lower_bound((vv).begin(), (vv).end(), pts[i].second) - vv.begin(); } sort((pts).begin(), (pts).end()); for (int(i) = 0; (i) < n; (i)++) { inc(pts[i].second, pts[i].first, F); inc(pts[i].second, 1, F2); } long long ans = 0; for (int(i) = 0; (i) < n; (i)++) { int v = pts[i].second; inc(v, -pts[i].first, F); inc(v, -1, F2); long long sum1 = sum(v, N - 1, F); long long cnt = sum(v, N - 1, F2); ans += sum1 - cnt * (long long)pts[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
cpp
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #pragma GCC optimization("unroll-loops") using namespace std; using namespace chrono; template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << "\n"; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } long long int advceil(long long int num, long long int den) { return (num % den == 0 ? num / den : num / den + 1); } long long int lstbt(long long int val) { long long int msk = val & (val - 1); return log2(val ^ msk); } long long int modmul(long long int a, long long int b) { a %= 1000000007; b %= 1000000007; long long int res = 0; while (b > 0) { if (b & 1) res = (res + a) % 1000000007; a = (a * 2) % 1000000007; b /= 2; } return res; } long long int modexpo(long long int a, long long int b) { long long int res = 1; while (b > 0) { if (b & 1) res = modmul(res, a); a = modmul(a, a); b /= 2; } return res; } long long int gcd(long long int a, long long int b) { return a == 0 ? b : gcd(b % a, a); } vector<long long int> CALCfactor(long long int n) { vector<long long int> factor(n + 2, 0); for (long long int i = 4; i <= n; i += 2) factor[i] = 2; for (long long int j = 3; j <= n; j += 2) { if (factor[j]) continue; for (long long int i = 2 * j; i <= n; i += j) factor[i] = j; } return factor; } vector<long long int> CALCprimeNUM(long long int n) { vector<long long int> factor = CALCfactor(n); vector<long long int> primeNUM; primeNUM.reserve(n + 2); for (long long int i = 2; i <= n; i++) if (!factor[i]) primeNUM.push_back(i); return primeNUM; } vector<long long int> CALCprimeFACTOR(long long int n) { vector<long long int> factor = CALCfactor(n); vector<long long int> ans; while (factor[n] != 0) { ans.push_back(factor[n]); n /= factor[n]; } if (n > 1) ans.push_back(n); return ans; } vector<long long int> unique(vector<long long int> x) { sort(x.begin(), x.end()); set<long long int> s; vector<long long int> ans; ans.reserve(x.size()); for (auto elem : x) s.insert(elem); for (auto elem : s) ans.push_back(elem); return ans; } pair<vector<long long int>, vector<long long int> > getFact(long long int n) { vector<long long int> fact(n + 1, 1), invfact(n + 1, 1); for (long long int i = 1; i <= n; i++) fact[i] = (i * (fact[i - 1])) % 1000000007; for (long long int i = 1; i <= n; i++) invfact[i] = (modexpo(i, 1000000007 - 2) * invfact[i - 1]) % 1000000007; return {fact, invfact}; } long long int BIT[300001][2] = {}; long long int N; void update(long long int idx, long long int val, long long int typ) { for (long long int i = idx; i <= N; i += (i & (-i))) BIT[i][typ] += val; } long long int query(long long int idx, long long int typ) { long long int res = 0; for (long long int i = idx; i > 0; i -= (i & (-i))) res += BIT[i][typ]; return res; } long long int sum(long long int l, long long int r, long long int typ) { return query(r, typ) - query(l - 1, typ); } map<long long int, long long int> fmap; void solve() { long long int n; cin >> n; vector<pair<long long int, long long int> > arr; arr.resize(n); for (long long int i = 0; i < n; i++) cin >> arr[i].first; for (long long int i = 0; i < n; i++) cin >> arr[i].second; sort(arr.begin(), arr.end()); vector<long long int> temp; temp.reserve(n); for (auto elem : arr) temp.push_back(elem.second); long long int cnt = 1; sort(temp.begin(), temp.end()); for (auto elem : temp) { fmap[elem] = cnt; cnt++; } N = cnt; long long int ans = 0; for (long long int i = 0; i < n; i++) { long long int c1 = sum(1, fmap[arr[i].second], 0); c1 = (arr[i].first * sum(1, fmap[arr[i].second], 1) - c1); ans += (c1); update(fmap[arr[i].second], arr[i].first, 0); update(fmap[arr[i].second], 1, 1); } cout << ans << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); auto start1 = high_resolution_clock::now(); solve(); auto stop1 = high_resolution_clock::now(); auto duration = duration_cast<microseconds>(stop1 - start1); }
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 Point { long long x; long long v; }; vector<Point> points; long long SUM; void add_pairwise_dist(long long start, long long end) { int m, i, j; if (start == end) return; m = start + (end - start) / 2; add_pairwise_dist(start, m); add_pairwise_dist(m + 1, end); vector<long long> rcumsum(end - start + 2); for (i = end; i >= start; i--) { rcumsum[i - start] = rcumsum[i - start + 1] + points[i].x; } j = m + 1; for (i = start; i <= m; i++) { while (j <= end && points[i].v > points[j].v) j++; if (j > end) break; SUM += rcumsum[j - start] - points[i].x * (end - j + 1); } vector<Point> temp(end - start + 1); i = start; j = m + 1; for (int k = 0; k < end - start + 1; k++) { if (i > m) { temp[k] = points[j++]; } else if (j > end) { temp[k] = points[i++]; } else if (points[i].v < points[j].v) { temp[k] = points[i++]; } else { temp[k] = points[j++]; } } for (int k = 0; k < end - start + 1; k++) { points[start + k] = temp[k]; } } int main() { int n; cin >> n; points.resize(n); for (int i = 0; i < n; i++) cin >> points[i].x; for (int i = 0; i < n; i++) cin >> points[i].v; sort(points.begin(), points.end(), [](const Point &a, Point const &b) { return a.x < b.x; }); add_pairwise_dist(0, n - 1); cout << SUM << 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; const int N = 200000; int x[N + 5], v[N + 5], _v[N + 5]; pair<int, int> point[N + 5]; template <typename T, int N> struct Binary_Indexed_Tree { int n; T sumv[N + 5]; void init(int _n) { n = _n; memset(sumv, 0, (n + 4) * sizeof(T)); } void build(T A[], int _n) { init(_n); for (int i = 1; i <= n; ++i) { sumv[i] += A[i]; int j = i + (i & -i); if (j <= n) sumv[j] += sumv[i]; } } void add(int p, T k) { while (p <= n) { sumv[p] += k; p += p & -p; } } T sum(int p) { T res = 0; while (p) { res += sumv[p]; p -= p & -p; } return res; } }; Binary_Indexed_Tree<long long, N> bit, bitr, cbit, cbitr; int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", x + i); for (int i = 1; i <= n; ++i) scanf("%d", v + i); memcpy(_v, v, sizeof(v)); sort(_v + 1, _v + n + 1); auto ed = unique(_v + 1, _v + n + 1); for (int i = 1; i <= n; ++i) { v[i] = lower_bound(_v + 1, ed, v[i]) - _v; point[i] = {x[i], v[i]}; } sort(point + 1, point + n + 1); bit.init(n); bitr.init(n); cbit.init(n); cbitr.init(n); for (int i = 1; i <= n; ++i) { int xi = point[i].first, vi = point[i].second; bitr.add(vi, xi); cbitr.add(vi, 1); } long long ans = 0; for (int i = 1; i <= n; ++i) { int xi = point[i].first, vi = point[i].second; bitr.add(vi, -xi); cbitr.add(vi, -1); ans += xi * cbit.sum(vi) - bit.sum(vi) + bitr.sum(n) - bitr.sum(vi - 1) - (cbitr.sum(n) - cbitr.sum(vi - 1)) * xi; bit.add(vi, xi); cbit.add(vi, 1); } printf("%lld\n", ans / 2); 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 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())) r = list(range(n)) r.sort(key=lambda i: X[i]) f = [0] * (n+1) f2 = [0] * (n+1) b = [] for i, k in enumerate(r): b.append((V[k], i)) heapify(b) ans = 0 while b: v, i = heappop(b) x = X[r[i]] sum = query(f, i) cnt = query(f2, i) ans += x * cnt - sum update(f, i+1, x) update(f2, i+1, 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> template <class C> inline void log_c(const C& c) {} template <class C> inline int sz(const C& c) { return static_cast<int>(c.size()); } using namespace std; using pii = pair<int, int>; using num = int64_t; using pll = pair<num, num>; const std::string eol = "\n"; using Ft = map<int, pair<int, int64_t>>; const int max_x = 100000000; void ft_inc(Ft& ft, int x, const pair<int, int64_t>& p) { for (; x <= max_x; x |= x + 1) { ft[x].first += p.first; ft[x].second += p.second; } } pair<int, int64_t> ft_sum(Ft& ft, int x) { pair<int, int64_t> res; for (; x >= 0; x = (x & (x + 1)) - 1) { res.first += ft[x].first; res.second += ft[x].second; } return res; } int main() { std::ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<pii> data(n); for (int k = 0; k < (n); ++k) cin >> data[k].second; for (int k = 0; k < (n); ++k) cin >> data[k].first; sort(begin(data), end(data)); Ft ft_slow; int64_t ans = 0; for (int k = 0; k < (sz(data)); ++k) { const auto [v, x] = data[k]; const auto [c_slow, s_slow] = ft_sum(ft_slow, x); ans += static_cast<int64_t>(x) * c_slow - s_slow; ft_inc(ft_slow, x, {1, x}); } cout << ans << eol; 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 int N = 2e5 + 5; int n, x[N], v[N]; vector<pair<int, int>> p; long long bit1[N], bit2[N], ans; void add(int p, int v, long long *bit) { for (p += 2; p < N; p += p & -p) bit[p] += v; } long long query(int p, long long *bit) { long long r = 0; for (p += 2; p; p -= p & -p) r += bit[p]; return r; } int main() { ios_base::sync_with_stdio(0), cin.tie(0); cin >> n; for (int i = 0; i < n; i++) cin >> x[i]; for (int i = 0; i < n; i++) cin >> v[i]; for (int i = 0; i < n; i++) p.push_back({x[i], v[i]}); sort(p.begin(), p.end()); sort(v, v + n); for (auto &x : p) { x.second = lower_bound(v, v + n, x.second) - v + 1; ans += 1ll * x.first * query(x.second, bit1) - query(x.second, bit2); add(x.second, 1, bit1); add(x.second, x.first, bit2); } 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
python3
import sys, math import io, os #data = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline #from bisect import bisect_left as bl, bisect_right as br, insort #from heapq import heapify, heappush, heappop from collections import defaultdict as dd, deque, Counter #from itertools import permutations,combinations def data(): return sys.stdin.readline().strip() def mdata(): return list(map(int, data().split())) def outl(var) : sys.stdout.write(' '.join(map(str, var))+'\n') def out(var) : sys.stdout.write(str(var)+'\n') #from decimal import Decimal #from fractions import Fraction #sys.setrecursionlimit(100000) #INF = float('inf') mod = int(1e9)+7 def update(BIT,v, w): while v <= n: BIT[v] += w v += (v & (-v)) def getvalue(BIT,v): ANS = 0 while v != 0: ANS += BIT[v] v -= (v & (-v)) return ANS def bisect_on_BIT(BIT,x): if x <= 0: return 0 ANS = 0 h = 1 << (n - 1) while h > 0: if ANS + h <= n and BIT[ANS + h] < x: x -= BIT[ANS + h] ANS += h h //= 2 return ANS + 1 n=int(data()) x=mdata() v=mdata() a=[[v[i],x[i]] for i in range(n)] a.sort() s=[0]*(n+1) cnt=[0]*(n+1) pos=dd(int) for i in range(n): pos[a[i][1]]=i+1 for i in range(n): update(s,i+1,a[i][1]) update(cnt,i+1,1) ans=0 x.sort() for i in range(n): ind=pos[x[i]] ans+=getvalue(s,n)-getvalue(s,ind)-(getvalue(cnt,n)-getvalue(cnt,ind))*x[i] update(s,ind,-x[i]) update(cnt,ind,-1) out(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.util.Arrays; import java.util.Scanner; import java.util.Comparator; public class movinpoints { private static long sumaj(long[] arr,int index) { long result=0; while (index>=0) { result+=arr[index]; index=(index&(index+1))-1; } return result; } private static void apdejt (long[] t,int index,int vv) { while (index<t.length) { t[index]+=vv; index=index|(index + 1); } } public static void main(String[] args) { Scanner in=new Scanner(System.in); int n=in.nextInt(); int[][] pm=new int[n][2]; int[][] vm=new int[n][2]; for (int i=0;i<n;i++) pm[i][0]=in.nextInt(); for (int i=0;i<n;i++) { pm[i][1]=in.nextInt(); vm[i][0]=pm[i][1]; vm[i][1]=i; } in.close(); Arrays.sort(vm, new Comparator<int[]>() { public int compare(int[] a, int[] b) { return a[0] - b[0]; } }); int nss=0; int[][] finar=new int[n][2]; for (int i=0;i<n;i++) { if (i>0 && vm[i][0]!=vm[i-1][0]) nss++; finar[i][0]=nss; finar[i][1]=vm[i][1]; } Arrays.sort(finar,new Comparator<int[]>() { public int compare(int[] a,int[] b) { return a[1]-b[1]; }}); for (int i=0; i<n;i++) { pm[i][1] = finar[i][0]; } Arrays.sort(pm,new Comparator<int[]>() { public int compare(int[] a,int[] b) { return a[0]-b[0]; }}); long[] c=new long[n]; long[] pts=new long[n]; long totalsum=0; for (int i=0;i<n;i++) { int index=pm[i][1]; totalsum+=sumaj(pts,index)*pm[i][0]-sumaj(c,index); apdejt(pts,index,1); //System.out.println(); //System.out.println(totalsum); //System.out.println(); //System.out.println("coordtsum"); //for (int j=0;j<c.length;j++) { //System.out.print(c[j]); //} apdejt(c, index, pm[i][0]); //System.out.println(); //System.out.println("pointsum"); //for (int j=0;j<pts.length;j++) { //System.out.print(pts[j]); //} } //System.out.println(); System.out.println(totalsum); } }
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; 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.util.*; import java.io.*; import java.text.*; public class Main{ //SOLUTION BEGIN //Into the Hardware Mode void pre() throws Exception{} void solve(int TC)throws Exception { int n = ni(); TreeSet<Long> v = new TreeSet<>(); long[][] p = new long[n][2]; for(int i = 0; i< n; i++)p[i][0] = nl();for(int i = 0; i< n; i++){p[i][1] = nl();v.add(p[i][1]);} HashMap<Long, Integer> mp = new HashMap<>(); int c = 1; for(long l:v)mp.put(l, c++); Arrays.sort(p, (long[] l1, long[] l2) -> Long.compare(l1[0], l2[0])); for(int i = 0; i< n; i++)p[i][1] = mp.get(p[i][1]); SegTree le = new SegTree(c+1); long ans = 0; for(int i = 0; i< n; i++){ long[] o = le.sum(0, (int)p[i][1]); ans += p[i][0]*o[1]-o[0]; le.u((int)p[i][1], p[i][0]); } pn(ans); } class SegTree{ int m = 1; long[] t, cnt; public SegTree(int n){ while(m<n)m<<=1; t = new long[m<<1]; cnt= new long[m<<1]; } void u(int p, long a){ t[p+=m] += a; cnt[p] ++; for(p>>=1; p>0; p>>=1){ t[p] = t[p<<1]+t[p<<1|1]; cnt[p] = cnt[p<<1]+cnt[p<<1|1]; } } long[] sum(int l, int r){ long ans = 0, cc = 0; for(l += m, r += m+1; l< r; l>>=1, r>>=1){ if((l&1)==1){ans += t[l];cc += cnt[l++];} if((r&1)==1){ans += t[--r];cc += cnt[r];} } return new long[]{ans, cc}; } } //SOLUTION END void hold(boolean b)throws Exception{if(!b)throw new Exception("Hold right there, Sparky!");} void exit(boolean b){if(!b)System.exit(0);} long IINF = (long)1e15; final int INF = (int)1e9+2, MX = (int)2e6+5; DecimalFormat df = new DecimalFormat("0.00000000000"); double PI = 3.141592653589793238462643383279502884197169399, eps = 1e-8; static boolean multipleTC = false, memory = false, fileIO = false; FastReader in;PrintWriter out; void run() throws Exception{ long ct = System.currentTimeMillis(); if (fileIO) { in = new FastReader(""); out = new PrintWriter(""); } else { in = new FastReader(); out = new PrintWriter(System.out); } //Solution Credits: Taranpreet Singh int T = (multipleTC) ? ni() : 1; pre(); for (int t = 1; t <= T; t++) solve(t); out.flush(); out.close(); System.err.println(System.currentTimeMillis() - ct); } public static void main(String[] args) throws Exception{ if(memory)new Thread(null, new Runnable() {public void run(){try{new Main().run();}catch(Exception e){e.printStackTrace();}}}, "1", 1 << 28).start(); else new Main().run(); } int find(int[] set, int u){return set[u] = (set[u] == u?u:find(set, set[u]));} int digit(long s){int ans = 0;while(s>0){s/=10;ans++;}return ans;} long gcd(long a, long b){return (b==0)?a:gcd(b,a%b);} int gcd(int a, int b){return (b==0)?a:gcd(b,a%b);} int bit(long n){return (n==0)?0:(1+bit(n&(n-1)));} void p(Object o){out.print(o);} void pn(Object o){out.println(o);} void pni(Object o){out.println(o);out.flush();} String n()throws Exception{return in.next();} String nln()throws Exception{return in.nextLine();} int ni()throws Exception{return Integer.parseInt(in.next());} long nl()throws Exception{return Long.parseLong(in.next());} double nd()throws Exception{return Double.parseDouble(in.next());} class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String s) throws Exception{ br = new BufferedReader(new FileReader(s)); } String next() throws Exception{ while (st == null || !st.hasMoreElements()){ try{ st = new StringTokenizer(br.readLine()); }catch (IOException e){ throw new Exception(e.toString()); } } return st.nextToken(); } String nextLine() throws Exception{ String str; try{ str = br.readLine(); }catch (IOException e){ throw new Exception(e.toString()); } 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> using namespace std; const int N = 2e5 + 10, inf = 1e9; const long long mod = (long long)1e9 + 7; set<int> setV; map<int, int> idV; pair<int, int> p[N]; int n; int treenum[N], num; long long treesum[N], sum; long long qry(long long tree[], int id) { long long ans = 0LL; for (; id; id -= (id & (-id))) ans += tree[id]; return ans; } int qry(int tree[], int id) { int ans = 0LL; for (; id; id -= (id & (-id))) ans += tree[id]; return ans; } int GetNum(int v) { return num - qry(treenum, idV[v] - 1); } long long GetSum(int v) { return sum - qry(treesum, idV[v] - 1); } void upd(int tree[], int id, int val) { for (; id < N; id += (id & -id)) tree[id] += val; } void upd(long long tree[], int id, int val) { for (; id < N; id += (id & -id)) tree[id] += val; } void Update(int v, int x) { upd(treenum, idV[v], +1); upd(treesum, idV[v], +x); num++; sum += x; } int main() { ios::sync_with_stdio(false); cin.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; setV.insert(p[i].second); } sort(p + 1, p + n + 1); int k = 0; for (int x : setV) idV[x] = ++k; num = 0; sum = 0LL; long long ans = 0LL; for (int i = n; i >= 1; --i) { int m = GetNum(p[i].second); long long y = GetSum(p[i].second); ans += y - 1LL * m * p[i].first; Update(p[i].second, p[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
cpp
#include <bits/stdc++.h> using namespace std; long long powmod(long long a, long long b, long long mod) { if (b == 0 || a == 1) { if (mod == 1) return 0; else return 1; } if (b % 2 == 0) { long long k = powmod(a, b / 2, mod); return (k * k) % mod; } else { long long k = powmod(a, b / 2, mod); return ((k * k) % mod * a) % mod; } } long long gcd(long long a, long long b) { if (a == 0) return b; if (b == 0) return a; if (a > b) return gcd(a % b, b); else return gcd(b % a, a); } long long prime(long long p) { for (long long i = 2; i * i <= p; i++) { if (p % i == 0 && i < p) return i; } return 1; } long long inv(long long a, long long mod) { return powmod(a, mod - 2, mod); } long long tree[1000000]; long long sum1(long long v, long long tl, long long tr, long long l, long long r) { if (l > r) return 0; if (l == tl && r == tr) return tree[v]; long long tm = (tl + tr) / 2; return sum1(2 * v, tl, tm, l, min(r, tm)) + sum1(2 * v + 1, tm + 1, tr, max(l, tm + 1), r); } void update(long long v, long long tl, long long tr, long long pos, long long new_val) { if (tl == tr) tree[v] += new_val; else { long long 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); tree[v] = tree[v * 2] + tree[v * 2 + 1]; } } long long tree2[1000000]; long long sum2(long long v, long long tl, long long tr, long long l, long long r) { if (l > r) return 0; if (l == tl && r == tr) return tree2[v]; long long tm = (tl + tr) / 2; return sum2(2 * v, tl, tm, l, min(r, tm)) + sum2(2 * v + 1, tm + 1, tr, max(l, tm + 1), r); } void update2(long long v, long long tl, long long tr, long long pos, long long new_val) { if (tl == tr) tree2[v] += new_val; else { long long tm = (tl + tr) / 2; if (pos <= tm) update2(v * 2, tl, tm, pos, new_val); else update2(v * 2 + 1, tm + 1, tr, pos, new_val); tree2[v] = tree2[v * 2] + tree2[v * 2 + 1]; } } void solve() { long long a; cin >> a; vector<pair<long long, long long>> m(a); for (long long i = 0; i < a; i++) cin >> m[i].first; for (long long i = 0; i < a; i++) cin >> m[i].second; sort(m.begin(), m.end()); long long ans = 0; long long sum = 0; long long col = 0; vector<long long> copy(a); for (long long i = 0; i < a; i++) copy[i] = m[i].second; sort(copy.begin(), copy.end()); map<long long, long long> zamena; for (long long i = 0; i < a; i++) zamena[copy[i]] = i; for (long long i = 0; i < a; i++) m[i].second = zamena[m[i].second]; long long cur = 0; for (long long i = 0; i < a; i++) if (m[i].second > cur) { update2(1, 1, 200000, m[i].second, m[i].first); update(1, 1, 200000, m[i].second, 1); } for (long long i = 0; i < a; i++) { if (m[i].second > cur) { ans += m[i].first * col - sum; ans += sum2(1LL, 1LL, 200000LL, m[i].second, 200000LL) - sum1(1LL, 1LL, 200000LL, m[i].second, 200000LL) * m[i].first; update(1, 1, 200000, m[i].second, -1); update2(1, 1, 200000, m[i].second, -m[i].first); } else { col++; sum += m[i].first; } } cout << ans << " "; return; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long tututu; tututu = 1; for (long long qwerty = 0; qwerty < tututu; qwerty++) 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
python3
from bisect import bisect_right, bisect_left # instead of AVLTree class BITbisect(): def __init__(self, InputProbNumbers): # 座圧 self.ind_to_co = [-10**18] self.co_to_ind = {} for ind, num in enumerate(sorted(list(set(InputProbNumbers)))): self.ind_to_co.append(num) self.co_to_ind[num] = ind+1 self.max = len(self.co_to_ind) self.data = [0]*(self.max+1) def __str__(self): retList = [] for i in range(1, self.max+1): x = self.ind_to_co[i] if self.count(x): c = self.count(x) for _ in range(c): retList.append(x) return "[" + ", ".join([str(a) for a in retList]) + "]" def __getitem__(self, key): key += 1 s = 0 ind = 0 l = self.max.bit_length() for i in reversed(range(l)): if ind + (1<<i) <= self.max: if s + self.data[ind+(1<<i)] < key: s += self.data[ind+(1<<i)] ind += (1<<i) if ind == self.max or key < 0: raise IndexError("BIT index out of range") return self.ind_to_co[ind+1] def __len__(self): return self._query_sum(self.max) def __contains__(self, num): if not num in self.co_to_ind: return False return self.count(num) > 0 # 0からiまでの区間和 # 左に進んでいく def _query_sum(self, i): s = 0 while i > 0: s += self.data[i] i -= i & -i return s # i番目の要素にxを足す # 上に登っていく def _add(self, i, x): while i <= self.max: self.data[i] += x i += i & -i # 値xを挿入 def push(self, x): if not x in self.co_to_ind: raise KeyError("The pushing number didnt initialized") self._add(self.co_to_ind[x], 1) # 値xを削除 def delete(self, x): if not x in self.co_to_ind: raise KeyError("The deleting number didnt initialized") if self.count(x) <= 0: raise ValueError("The deleting number doesnt exist") self._add(self.co_to_ind[x], -1) # 要素xの個数 def count(self, x): return self._query_sum(self.co_to_ind[x]) - self._query_sum(self.co_to_ind[x]-1) # 値xを超える最低ind def bisect_right(self, x): if x in self.co_to_ind: i = self.co_to_ind[x] else: i = bisect_right(self.ind_to_co, x) - 1 return self._query_sum(i) # 値xを下回る最低ind def bisect_left(self, x): if x in self.co_to_ind: i = self.co_to_ind[x] else: i = bisect_left(self.ind_to_co, x) if i == 1: return 0 return self._query_sum(i-1) # 足す時はi番目に足し、返すのは累積和 class sumBIT(): def __init__(self, N): self.N = N self.bit = [0 for _ in range(self.N+1)] def __str__(self): ret = [] for i in range(1, self.N+1): ret.append(self.__getitem__(i)) return "[" + ", ".join([str(a) for a in ret]) + "]" def __getitem__(self, i): s = 0 while i > 0: s += self.bit[i] i -= i & -i return s def add(self, i, x): while i <= self.N: self.bit[i] += x i += i & -i import sys input = sys.stdin.buffer.readline from operator import itemgetter def main(): N = int(input()) X = list(map(int, input().split())) V = list(map(int, input().split())) XV = [(x, v) for x, v in zip(X, V)] XV.sort() XV.sort(key=itemgetter(1)) co_to_ind1 = {} #co_to_ind2 = {} for i, (x, v) in enumerate(XV): co_to_ind1[x] = i+1 XV.sort() sumbit = sumBIT(N+1) bit = sumBIT(N+1) ans = 0 for x, v in XV: ind1 = co_to_ind1[x] #ind2 = co_to_ind2[v] count = bit[ind1] s = sumbit[ind1] ans += count*x - s bit.add(ind1, 1) sumbit.add(ind1, x) print(ans) if __name__ == "__main__": 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; long long int max(long long int a, long long int b) { if (a > b) return a; else return b; } long long int min(long long int a, long long int b) { if (a < b) return a; else return b; } const int dx[4] = {-1, 1, 0, 0}; const int dy[4] = {0, 0, -1, 1}; int XX[] = {-1, -1, -1, 0, 0, 1, 1, 1}; int YY[] = {-1, 0, 1, -1, 1, -1, 0, 1}; long long int fen1[200005] = {0}; long long int maxi = 200003; void update1(long long int ind, long long int val) { for (long long int i = ind; i <= maxi; i += i & -i) fen1[i] += val; } long long int sum1(long long int ind) { long long int s = 0; for (long long int i = ind; i; i -= i & -i) s += fen1[i]; return s; } long long int fen2[200005] = {0}; void update2(long long int ind, long long int val) { for (long long int i = ind; i <= maxi; i += i & -i) fen2[i] += val; } long long int sum2(long long int ind) { long long int s = 0; for (long long int i = ind; i; i -= i & -i) s += fen2[i]; return s; } void convert(long long int arr[], long long int n) { long long int temp[n]; memcpy(temp, arr, n * sizeof(long long int)); sort(temp, temp + n); unordered_map<long long int, long long int> umap; long long int val = 1; for (long long int i = 0; i < n; i++) umap[temp[i]] = val++; for (long long int i = 0; i < n; i++) arr[i] = umap[arr[i]]; } int main() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); long long int i, j, n, k, x, v; cin >> n; long long int arr1[n]; for (i = 0; i < n; i++) { cin >> arr1[i]; } long long int arr2[n]; for (i = 0; i < n; i++) { cin >> arr2[i]; } convert(arr2, n); vector<pair<long long int, long long int> > vec; for (i = 0; i < n; i++) { vec.push_back(make_pair(arr1[i], arr2[i])); } sort(vec.begin(), vec.end()); long long int ans = 0; for (i = 0; i < vec.size(); i++) { x = vec[i].first; v = vec[i].second; update1(v, 1); update2(v, x); } for (i = vec.size() - 1; i >= 0; i--) { x = vec[i].first; v = vec[i].second; update1(v, -1); update2(v, -x); long long int z = sum2(v); long long int count = sum1(v); ans += (x * count - z); } 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> template <class T> T sqr(T x) { return x * x; } template <class T> T lcm(T a, T b) { return a / __gcd(a, b) * b; } const long long mod = 1e9 + 7, oo = 1e12, N = 2e5 + 5; using namespace std; long long n, b[N], res, bit1[N], bit2[N]; pair<long long, long long> a[N]; inline long long lowbit(long long x) { return x & -x; } void update(long long pos, long long val, long long *arr) { while (pos <= n) { arr[pos] += val; pos += lowbit(pos); } } long long get(long long pos, long long *arr) { long long ret = 0; while (pos > 0) { ret += arr[pos]; pos -= lowbit(pos); } return ret; } signed main() { ios_base::sync_with_stdio(0); cin.tie(); cout.tie(); cin >> n; for (__typeof(n - 1) i = (0); i <= (n - 1); ++i) cin >> a[i].first; for (__typeof(n - 1) i = (0); i <= (n - 1); ++i) { cin >> a[i].second; b[i] = a[i].second; } sort(b, b + n); for (__typeof(n - 1) i = (0); i <= (n - 1); ++i) a[i].second = lower_bound(b, b + n, a[i].second) - b + 1; sort(a, a + n); for (__typeof(n - 1) i = (0); i <= (n - 1); ++i) { long long tmp1 = get(a[i].second, bit1); long long tmp2 = get(a[i].second, bit2); res += a[i].first * tmp1 - tmp2; update(a[i].second, 1, bit1); update(a[i].second, a[i].first, bit2); } 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
python3
import sys input = sys.stdin.readline import bisect 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 = [(X[i], V[i]) for i in range(n)] a.sort() # r = list(range(n)) # r.sort(key=lambda i: X[i]) vlist = sorted(set(V)) f = [0] * (len(vlist) + 1) f2 = [0] * (len(vlist) + 1) ans = 0 for x, v in a: # x, v = X[i], V[i] ii = bisect.bisect_left(vlist, v) sm = query(f, ii + 1) cnt = query(f2, ii + 1) ans += x * cnt - sm update(f, ii+1, x) update(f2, ii+1, 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; template <typename Arg1> void __f(const char* name, Arg1&& arg1) { std::cerr << name << " : " << arg1 << '\n'; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); std::cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } struct FenwickTree { vector<long long> bit; long long n; vector<long long> cnt; FenwickTree(long long n) { this->n = n; bit.assign(n, 0); cnt.assign(n, 0); } FenwickTree(vector<long long> a) : FenwickTree(a.size()) { for (size_t i = 0; i < a.size(); i++) add(i, a[i]); } pair<long long, long long> sum(long long r) { long long ret = 0; long long t = r; for (; r >= 0; r = (r & (r + 1)) - 1) ret += bit[r]; long long ret2 = 0; r = t; for (; r >= 0; r = (r & (r + 1)) - 1) ret2 += cnt[r]; return {ret, ret2}; } pair<long long, long long> sum(long long l, long long r) { auto j = sum(r); auto j2 = sum(l - 1); return {j.first - j2.first, j.second - j2.second}; } void add(long long idx, long long delta) { long long t = idx; for (; idx < n; idx = idx | (idx + 1)) bit[idx] += delta; idx = t; for (; idx < n; idx = idx | (idx + 1)) cnt[idx] += 1; } }; FenwickTree ft(1000005); map<long long, long long> compress; signed main() { ios_base::sync_with_stdio(0); long long TESTS = 1; while (TESTS--) { long long n; cin >> n; long long x[n]; for (long long i = 0; i < n; i++) cin >> x[i]; long long v[n]; for (long long i = 0; i < n; i++) cin >> v[i]; set<long long> vv; for (long long i = 0; i < n; i++) { vv.insert(abs(v[i])); } long long i = 0; for (auto j : vv) { if (j == 0) compress[j] = 0; else { compress[j] = i + 1; i++; } } for (long long i = 0; i < n; i++) { if (v[i] >= 0) v[i] = compress[v[i]]; else v[i] = -compress[-v[i]]; } vector<pair<long long, long long>> ve; for (long long i = 0; i < n; i++) ve.push_back({x[i], v[i]}); sort((ve).begin(), (ve).end()); for (long long i = 0; i < n; i++) { x[i] = ve[i].first; v[i] = ve[i].second; } long long left = 0, right = 0; long long cntl = 0, cntr = 0; long long ans = 0; for (long long i = 0; i < n; i++) { if (v[i] > 0) { ans += x[i] * cntl - left; } else if (v[i] < 0) { cntl++; left += x[i]; } } for (long long i = 0; i < n; i++) { if (v[i] >= 0) { ans += ft.sum(0, v[i]).second * x[i] - ft.sum(0, v[i]).first; ft.add(v[i], x[i]); } } for (long long i = 0; i < 1000005; i++) { ft.bit[i] = 0; ft.cnt[i] = 0; } for (long long i = 0; i < n; i++) { if (v[i] <= 0) { v[i] = -v[i]; ans += ft.sum(v[i], 1000000).second * x[i] - ft.sum(v[i], 1000000).first; ft.add(v[i], x[i]); } } long long sum = 0; long long cnt = 0; for (long long i = 0; i < n; i++) { if (v[i] == 0) { ans -= cnt * x[i] - sum; sum += x[i]; cnt++; } } 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.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.*; public class ProblemF { public static InputStream inputStream = System.in; public static OutputStream outputStream = System.out; public static void main(String[] args) { Scanner scanner = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); int n = scanner.nextInt(); List<ProblemF.Pair<Long, Long>> xvList = new ArrayList<>(); TreeSet<Long> treeSet = new TreeSet<>(); for (int i = 0; i < n; i++) { xvList.add(new ProblemF.Pair<>(scanner.nextLong(), 0L)); } for (int i = 0; i < n; i++) { long v = scanner.nextLong(); xvList.get(i).second = v; treeSet.add(v); } Map<Long, Integer> indexesMap = new HashMap<>(); int index = treeSet.size() - 1; for (long x : treeSet) { indexesMap.put(x, index--); } xvList.sort((a, b) -> { int x = Long.compare(b.first, a.first); if (x != 0) { return x; } return Long.compare(b.second, a.second); }); BinaryIndexedTree xSumTree = new BinaryIndexedTree(n); BinaryIndexedTree countSumTree = new BinaryIndexedTree(n); long ans = 0; for (int i = 0; i < n; i++) { long x = xvList.get(i).first; long v = xvList.get(i).second; index = indexesMap.get(v); ans += (xSumTree.getSum(index) - x * countSumTree.getSum(index)); xSumTree.update(index, x); countSumTree.update(index, 1); } out.println(ans); out.flush(); } private static class BinaryIndexedTree { private long[] bit; private int n; private BinaryIndexedTree(int n) { this.bit = new long[n + 1]; this.n = n; } private void update(int index, long delta) { index++; while (index <= n) { bit[index] += delta; index += (index & (-index)); } } private long getSum(int index) { index++; long sum = 0; while (index > 0) { sum += bit[index]; index = index - (index & (-index)); } return sum; } } private static class Pair<F, S> { private F first; private S second; public Pair() {} public Pair(F first, S second) { this.first = first; this.second = second; } } }
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, long long> o[(int)2e5 + 10]; long long n, ts[(int)2e5 + 10], tc[(int)2e5 + 10]; int main(void) { scanf("%lld", &n); for (int i = 0; i < n; i++) { scanf("%lld", &o[i].second); } for (int i = 0; i < n; i++) { scanf("%lld", &o[i].first); } sort(o, o + n); for (int i = 0; i < n; i++) { o[i] = {o[i].second, i + 1}; } sort(o, o + n); long long ans = 0; for (int i = n - 1; - 1 < i; i--) { long long l = o[i].second - 1, r = n, idx = o[i].first; long long s = 0, c = 0; while (0 < r) { s += ts[r]; c += tc[r]; r -= (r & -r); } while (0 < l) { s -= ts[l]; c -= tc[l]; l -= (l & -l); } ans += s - c * idx; int v = o[i].second; while (v <= n) { ts[v] += idx; tc[v]++; v += (v & -v); } } 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> using namespace std; const long long N = 1e5 + 7; int n; vector<pair<int, int>> p; vector<int> vv1; 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 < f.size(); pos |= pos + 1) f[pos] += val; } int main() { scanf("%d", &n); p.resize(n); for (int i = 0; i < n; i++) scanf("%d", &p[i].first); for (int i = 0; i < n; i++) { scanf("%d", &p[i].second); vv1.push_back(p[i].second); } sort(vv1.begin(), vv1.end()); vv1.resize(unique(vv1.begin(), vv1.end()) - vv1.begin()); sort(p.begin(), p.end()); long long ans = 0; vector<long long> tmpv1(vv1.size()), tmpv2(vv1.size()); for (int i = 0; i < n; i++) { int pos = lower_bound(vv1.begin(), vv1.end(), p[i].second) - vv1.begin(); ans += get(tmpv1, pos) * 1ll * p[i].first - get(tmpv2, pos); upd(tmpv1, pos, 1); upd(tmpv2, pos, p[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; const int N = 1e6 + 1; const int NN = 1e3 + 1; const long long MOD = 1e9 + 7; const long long oo = 1e18 + 7; const int BASE = 10000; void solve() { int n; cin >> n; vector<int> x(n + 1); for (int i = 1; i <= n; i++) { cin >> x[i]; } vector<int> v(n + 1); for (int i = 1; i <= n; i++) { cin >> v[i]; } vector<int> pos(n + 1); iota(1 + (pos).begin(), (pos).end(), 1); sort(1 + (pos).begin(), (pos).end(), [&](int a, int b) { return x[a] < x[b]; }); vector<int> val = v; sort(1 + (val).begin(), (val).end()); val.resize(unique((val).begin(), (val).end()) - val.begin()); map<int, int> Pos; for (int i = 1; i < val.size(); i++) { Pos[val[i]] = i; } vector<pair<long long, long long> > BIT(val.size()); auto update = [&](int pos, int _val) { for (int i = pos; i < BIT.size(); i += i & -i) { BIT[i].first += 1LL * _val; BIT[i].second++; } }; auto get = [&](int pos) { long long res = 0, cnt = 0; for (int i = pos; i; i -= i & -i) { res += BIT[i].first; cnt += BIT[i].second; } pair<long long, long long> t = {res, cnt}; return t; }; long long res = 0; for (int i = 1; i <= n; i++) { pair<long long, long long> cur = get(Pos[v[pos[i]]]); res += x[pos[i]] * cur.second - cur.first; update(Pos[v[pos[i]]], x[pos[i]]); } cout << res; } int main() { ios_base::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr); cout.precision(10); solve(); cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\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 BIT[515151]; int BIT2[515151]; map<int, int> hs; void update(int x, int y) { for (int i = x; i <= 5e5; i += (i & (-i))) BIT[i] += y, BIT2[i]++; } long long query(int x) { long long tmp = 0; for (int i = x; i >= 1; i -= (i & (-i))) tmp += BIT[i]; return tmp; } long long query2(int x) { int tmp = 0; for (int i = x; i >= 1; i -= (i & (-i))) tmp += BIT2[i]; return tmp; } int main() { scanf("%d", &n); int x[n + 5], v[n + 5]; for (int i = 0; i < n; i++) { scanf("%d", &x[i]); } vector<int> tmp; for (int i = 0; i < n; i++) { scanf("%d", &v[i]); tmp.push_back(v[i]); } sort(tmp.begin(), tmp.end()); int idx = 1; for (auto i : tmp) { hs[i] = idx; idx++; } for (int i = 0; i < n; i++) { v[i] = hs[v[i]]; } vector<pair<int, int> > data; for (int i = 0; i < n; i++) { data.push_back(make_pair(x[i], v[i])); } sort(data.begin(), data.end()); long long ans = 0; for (auto i : data) { long long cnt1 = query(i.second); long long cnt2 = query2(i.second); ans += (cnt2 * i.first - cnt1); update(i.second, i.first); } printf("%I64d\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> using namespace std; const int N = 200009; int n, b[N], p[N], tot; long long c1[N], c2[N]; struct P { int x, v; bool operator<(const P &A) const { return x < A.x; } } a[N]; int lowbit(int x) { return x & (x ^ (x - 1)); } void add(int x, int k, long long c[]) { while (x <= tot) { c[x] += k; x += lowbit(x); } } long long getsum(int x, long long c[]) { long long ans = 0; while (x) { ans += c[x]; x -= lowbit(x); } return ans; } void init() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i].x); for (int i = 1; i <= n; i++) scanf("%d", &a[i].v); sort(a + 1, a + 1 + n); } void work() { for (int i = 1; i <= n; i++) p[i] = a[i].v; sort(p + 1, p + 1 + n); tot = unique(p + 1, p + 1 + n) - (p + 1); for (int i = 1; i <= n; i++) b[i] = lower_bound(p + 1, p + 1 + tot, a[i].v) - p; long long ans = 0; for (int i = 1; i <= n; i++) { ans += getsum(b[i], c1) * a[i].x - getsum(b[i], c2); add(b[i], 1, c1), add(b[i], a[i].x, c2); } printf("%lld\n", ans); } int main() { init(); work(); 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 int maxn = 2e5 + 5; long long positions[maxn], speeds[maxn]; pair<long long, long long> pairs[maxn]; map<long long, long long> m; bool comp(pair<long long, long long>& a, pair<long long, long long>& b) { if (a.second == b.second) return a.first < b.first; return a.second < b.second; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; for (int i = 0; i < n; ++i) cin >> positions[i]; for (int i = 0; i < n; ++i) cin >> speeds[i]; for (long long i = 0; i < n; ++i) pairs[i] = {positions[i], speeds[i]}; sort(positions, positions + n); sort(pairs, pairs + n, comp); long long ans = 0; for (long long i = 0; i < n; ++i) { ans += ((2 * i) - n + 1) * positions[i]; m[positions[i]] = i; } for (int i = 0; i < n; ++i) ans -= pairs[i].first * (m[pairs[i].first] - i); 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> using namespace std; pair<long long, long long> a[200005]; long long sum[1000005], cnt[1000005]; long long querysum(long long pos) { long long ret = 0; for (; pos >= 0; pos = (pos & (pos + 1)) - 1) { ret += sum[pos]; } return ret; } long long querycnt(long long pos) { long long ret = 0; for (; pos >= 0; pos = (pos & (pos + 1)) - 1) { ret += cnt[pos]; } return ret; } void updatesum(long long pos, long long val) { for (; pos < 1000000; pos |= pos + 1) { sum[pos] += val; } } void updatecnt(long long pos, long long val) { for (; pos < 1000000; pos |= pos + 1) { cnt[pos] += val; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n; 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; vector<long long> v; for (long long i = 1; i <= n; i++) { v.push_back(a[i].second); } sort(v.begin(), v.end()); map<long long, long long> num; for (long long i = 0; i < v.size(); i++) { num[v[i]] = i; } for (long long i = 1; i <= n; i++) { a[i].second = num[a[i].second]; } sort(a + 1, a + n + 1); memset(sum, 0, sizeof(sum)); memset(cnt, 0, sizeof(cnt)); long long ans = 0; for (long long i = 1; i <= n; i++) { ans += a[i].first * querycnt(a[i].second) - querysum(a[i].second); updatesum(a[i].second, a[i].first); updatecnt(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 int N = 2e5 + 20; int n; pair<int, int> p[N]; struct node { long long sum, count; node(long long s = 0, long long c = 0) : sum(s), count(c) {} } bit[N]; void update(int x, int delta) { for (; x <= n; x += x & -x) { auto &[sum, count] = bit[x]; sum += delta; count++; } } node query(int x) { auto ans = node(); for (; x > 0; x -= x & -x) { auto &[sum, count] = bit[x]; ans.sum += sum; ans.count += count; } return ans; } int compress(vector<int> &v) { if (v.empty()) return 0; vector<int> s = v; sort(s.begin(), s.end()); s.resize(unique(s.begin(), s.end()) - s.begin()); int mx = 1; for (int &x : v) { x = lower_bound(s.begin(), s.end(), x) - s.begin() + 1; mx = max(mx, x); } return mx; } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 0; i < n; i++) cin >> p[i].first; for (int i = 0; i < n; i++) cin >> p[i].second; sort(p, p + n); vector<int> vec(n); for (int i = 0; i < n; i++) vec[i] = p[i].second; compress(vec); long long ans = 0; for (int i = 0; i < n; i++) { auto [sum, cnt] = query(vec[i]); ans += cnt * p[i].first - sum; update(vec[i], 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
//make sure to make new file! import java.io.*; import java.util.*; public class F624{ public static void main(String[] args)throws IOException{ BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); int n = Integer.parseInt(f.readLine()); StringTokenizer st1 = new StringTokenizer(f.readLine()); StringTokenizer st2 = new StringTokenizer(f.readLine()); Integer[] x = new Integer[n]; int[] v = new int[n]; Pair[] pairs = new Pair[n]; for(int k = 0; k < n; k++){ x[k] = Integer.parseInt(st1.nextToken()); v[k] = Integer.parseInt(st2.nextToken()); pairs[k] = new Pair(x[k],v[k]); } Arrays.sort(pairs); Arrays.sort(x); long answer = 0L; HashMap<Integer,Integer> indexof = new HashMap<Integer,Integer>(); for(int k = 0; k < n; k++){ answer += (long)x[k] * (long)(-n+1+2*k); indexof.put(x[k],k); } for(int k = 0; k < n; k++){ if(k < indexof.get(pairs[k].x)){ //subtract answer -= (long)pairs[k].x*(long)(indexof.get(pairs[k].x)-k); } else if(k > indexof.get(pairs[k].x)){ answer += (long)pairs[k].x*(long)(k-indexof.get(pairs[k].x)); } } out.println(answer); out.close(); } public static class Pair implements Comparable<Pair>{ int x; int v; public Pair(int a, int b){ x = a; v = b; } public int compareTo(Pair p){ if(p.v != v){ return v-p.v; } return x-p.x; } } }
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 read() { long long a = 0, b = getchar(), c = 1; while (!isdigit(b)) c = b == '-' ? -1 : 1, b = getchar(); while (isdigit(b)) a = a * 10 + b - '0', b = getchar(); return a * c; } long long n, ans, p[200005]; pair<long long, long long> a[200005]; int main() { n = read(); for (int i = 1; i <= n; i++) { a[i].second = read(); p[i] = a[i].second; } for (int i = 1; i <= n; i++) a[i].first = read(); sort(a + 1, a + n + 1); sort(p + 1, p + n + 1); for (int i = 1; i <= n; i++) ans += (lower_bound(p + 1, p + n + 1, a[i].second) - p - n + i - 1) * a[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
cpp
#include <bits/stdc++.h> using namespace std; const long long N = 2e5 + 500; long long n, t1[N], t2[N]; void upd1(long long val, long long pos) { while (pos < n) { t1[pos] += val; pos = (pos | (pos + 1)); } } void upd2(long long val, long long pos) { while (pos < n) { t2[pos] += val; pos = (pos | (pos + 1)); } } long long get1(long long pos) { long long res = 0; while (pos >= 0) { res += t1[pos]; pos = (pos & (pos + 1)) - 1; } return res; } long long get2(long long pos) { long long res = 0; while (pos >= 0) { res += t2[pos]; pos = (pos & (pos + 1)) - 1; } return res; } int main() { cin >> n; vector<pair<long long, long long> > a(n); vector<long long> comp; map<long long, long long> new_val; for (int i = 0; i < n; i++) { cin >> a[i].first; } for (int i = 0; i < n; i++) { cin >> a[i].second; comp.push_back(a[i].second); } sort(a.begin(), a.end()); sort(comp.begin(), comp.end()); long long k = 0; new_val[comp[0]] = k; for (int i = 1; i < comp.size(); i++) { if (comp[i] != comp[i - 1]) k++; new_val[comp[i]] = k; } for (int i = 0; i < n; i++) { a[i].second = new_val[a[i].second]; } long long ans = 0; for (int i = 0; i < n; i++) { long long pos = a[i].second; long long cnt = get1(pos); long long sum = get2(pos); ans += ((cnt * a[i].first) - sum); upd1(1, pos); upd2(a[i].first, pos); } 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; void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char *x) { cerr << '\"' << x << '\"'; } void __print(const string &x) { cerr << '\"' << x << '\"'; } void __print(bool x) { cerr << (x ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V> &x) { cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}'; } template <typename T> void __print(const T &x) { int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } long long int power(long long int x, long long int y) { long long int ans = 1; x %= 1000000007; while (y) { if (y & 1) ans = (x * ans) % 1000000007; x = (x * x) % 1000000007; y >>= 1; } return ans; } const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; struct FenwickTree { vector<long long int> bit; int n; FenwickTree(int n) { this->n = n; bit.assign(n, 0); } FenwickTree(vector<int> a) : FenwickTree(a.size()) { for (size_t i = 0; i < a.size(); i++) add(i, a[i]); } long long int sum(int r) { long long int ret = 0; for (; r >= 0; r = (r & (r + 1)) - 1) ret += bit[r]; return ret; } long long int sum(int l, int r) { return sum(r) - sum(l - 1); } void add(int idx, int delta) { for (; idx < n; idx = idx | (idx + 1)) bit[idx] += delta; } }; void solve() { long long int n; cin >> n; vector<pair<long long int, long long int> > arr(n); vector<long long int> vel(n); for (int i = 0; i < n; i++) { cin >> arr[i].first; } for (int i = 0; i < n; i++) { cin >> arr[i].second; vel[i] = arr[i].second; } sort(arr.begin(), arr.end()); sort(vel.begin(), vel.end()); map<long long int, long long int> mp; for (int i = 0; i < n; i++) { long long int in = lower_bound(vel.begin(), vel.end(), vel[i]) - vel.begin(); mp[vel[i]] = in; } long long int ans = 0; FenwickTree count(n + 10), dist(n + 10); for (int i = 0; i < n; i++) { long long int v = mp[arr[i].second]; long long int cnt = count.sum(v); long long int sum = dist.sum(v); ans += (cnt * arr[i].first - sum); count.add(v, 1); dist.add(v, arr[i].first); } cout << ans << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; while (t--) { 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; const int MAXN = 5e5 + 10, MAXM = 2e6 + 10; const int INF = INT_MAX, SINF = 0x3f3f3f3f; const long long llINF = LLONG_MAX; const int MOD = 1e9 + 7, mod = 998244353; long long tr[MAXN], tot, n, t[MAXN]; void add(int x, long long val) { while (x <= n) { tr[x] += val; t[x] += 1; x += x & -x; } } pair<long long, long long> query(int x) { long long ans = 0, ans1 = 0; while (x) { ans += tr[x]; ans1 += t[x]; x -= x & -x; } return {ans, ans1}; } struct Node { long long x, v; bool operator<(const Node &rhs) const { return x < rhs.x; } } e[MAXN]; vector<long long> v; map<long long, long long> mp; int main() { scanf("%lld", &n); for (int i = 1; i <= n; i++) scanf("%lld", &e[i].x); for (int i = 1; i <= n; i++) scanf("%lld", &e[i].v), v.push_back(e[i].v); sort(e + 1, e + n + 1); sort(v.begin(), v.end()); v.resize(unique(v.begin(), v.end()) - v.begin()); for (int i = 0; i < ((int)v.size()); i++) mp[v[i]] = i + 1; long long ans = 0; for (int i = 1; i <= n; i++) { pair<long long, long long> tmp = query(mp[e[i].v]); ans += e[i].x * tmp.second - tmp.first; add(mp[e[i].v], e[i].x); } printf("%lld", 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 sys input = sys.stdin.readline import bisect 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())) r = list(range(n)) r.sort(key=lambda i: X[i]) vlist = sorted(set(V)) f = [0] * (len(vlist) + 1) f2 = [0] * (len(vlist) + 1) ans = 0 for i in r: x, v = X[i], V[i] ii = bisect.bisect_left(vlist, v) sm = query(f, ii + 1) cnt = query(f2, ii + 1) ans += x * cnt - sm update(f, ii+1, x) update(f2, ii+1, 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; int b[200005], m; long long c[200005][2]; struct node { int x, v; bool operator<(const node t) const { return x < t.x; } } a[200005]; int lsh(long long x) { return lower_bound(b + 1, b + 1 + m, x) - b; } long long query(int x, int k) { long long ret = 0; for (int i = x; i >= 1; i -= i & -i) ret += c[i][k]; return ret; } void update(int x, int y) { for (int i = x; i <= m; i += i & -i) { c[i][0]++; c[i][1] += y; } } int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i].x); for (int i = 1; i <= n; i++) { scanf("%d", &a[i].v); b[i] = a[i].v; } sort(b + 1, b + 1 + n); m = unique(b + 1, b + 1 + n) - b - 1; sort(a + 1, a + 1 + n); long long ans = 0; for (int i = 1; i <= n; i++) { int t = lsh(a[i].v); ans += query(t, 0) * a[i].x - query(t, 1); update(t, a[i].x); } printf("%lld", 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
import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); int n = Integer.parseInt(br.readLine()); List<Point> points = new ArrayList(); StringTokenizer st = new StringTokenizer(br.readLine()); long[] x = new long[n]; long[] vel = new long[n]; for(int i=0;i<n;i++){ x[i] = Long.parseLong(st.nextToken()); } st = new StringTokenizer(br.readLine()); for(int i=0;i<n;i++){ vel[i] = Long.parseLong(st.nextToken()); } //HashMap<Long,Integer> velVal = getMap(vel); for(int i=0;i<n;i++){ Point point = new Point(x[i],vel[i]); points.add(point); } Collections.sort(points,(p1,p2)->{ if(p1.x< p2.x) return -1; else if(p1.x == p2.x) return 0; return 1; }); Map<Long,Integer> map = getMap(points); // points.stream().forEach(p->System.out.println(p.x+" "+p.vel)); long ans = 0; long negX = 0; long count = 0; for(int i=0;i<points.size();i++){ if(points.get(i).vel < 0){ negX+=points.get(i).x; count++; } else ans+= (count*points.get(i).x - negX); } Collections.sort(points,(p1,p2)->{ if(p1.vel< p2.vel) return -1; else if(p1.vel == p2.vel) { if(p1.x < p2.x)return -1; else if(p1.x > p2.x) return 1; return 0; } return 1; }); // points.stream().forEach(p->System.out.println(p.x+" "+p.vel)); long[][] ft = new long[n+1][2]; int ind = -1; for(int i=0;i<points.size();i++) { if(points.get(i).vel >=0) { ind = i; break; } long[] res = BIT(ft,map,points.get(i).x); long c = res[1]; long values = res[0]; ans+= (c*points.get(i).x-values); //System.out.println("ans =" +ans); } ft = new long[n+1][2]; for(int i = (ind==-1) ? points.size() : ind; i< points.size(); i++) { long[] res = BIT(ft,map,points.get(i).x); long c = res[1]; long values = res[0]; ans+= (c*points.get(i).x-values); // System.out.println("ans =" +ans); } out.println(ans); out.close(); } private static Map<Long,Integer> getMap(List<Point> points){ Map<Long,Integer> map = new HashMap(); int pos=1,neg = 1; for(int i=0;i<points.size();i++) { if(points.get(i).vel < 0) { map.put(points.get(i).x, neg); neg++; } else { map.put(points.get(i).x, pos); pos++; } } return map; } public static long[] BIT(long[][] ft, Map<Long,Integer> map,long val){ int v = map.get(val); long[] result = new long[2]; result[0]+=ft[v][0]; result[1]+=ft[v][1]; while(v > 0){ v -= ((~v) + 1)&v; result[0]+=ft[v][0]; result[1]+=ft[v][1]; } v = map.get(val); while(v < ft.length){ ft[v][0]+=val; ft[v][1]++; v += ((~v) + 1)&v; } return result; } } class Point{ long x; long vel; Point(long x, long vel){ this.x= x; this.vel = vel; } }
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 mx = 105; const long long mod = 1000000007; const long long maxn = 1e17; vector<long long> v1; vector<long long> v2; long long get(long long x, long long vt) { long long i, j, k; long long res = 0; for (i = x; i > 0; i -= (i & -i)) { if (vt == 1) { res += v1[i]; } else { res += v2[i]; } } return res; } void upd(long long x, long long vt, long long val) { long long i, j, k; long long si = (vt == 1 ? (int)v1.size() : (int)v2.size()); for (i = x; i < si; i += (i & -i)) { if (vt == 1) { v1[i] += val; } else { v2[i] += val; } } } void solve() { long long n, i, j, k; cin >> n; long long x[n + 1], v[n + 1]; vector<pair<long long, long long>> v4; for (i = 1; i <= n; i++) { cin >> x[i]; } for (i = 1; i <= n; i++) { cin >> v[i]; } for (i = 1; i <= n; i++) { v4.push_back({x[i], v[i]}); } sort(v4.begin(), v4.end()); for (i = 1; i <= n; i++) { x[i] = v4[i - 1].first; v[i] = v4[i - 1].second; } vector<long long> disx; for (i = 1; i <= n; i++) { disx.push_back(v[i]); } sort(disx.begin(), disx.end()); disx.resize(unique(disx.begin(), disx.end()) - disx.begin()); v1.resize(disx.size() + 1); v2.resize(disx.size() + 1); long long res = 0; for (i = 1; i <= n; i++) { long long pos = lower_bound(disx.begin(), disx.end(), v[i]) - disx.begin(); res += get(pos + 1, 1) * x[i] - get(pos + 1, 2); upd(pos + 1, 1, 1); upd(pos + 1, 2, x[i]); } cout << res << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); long long t = 1; while (t--) { 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
java
import java.io.*; import java.util.*; import java.math.*; /** * Built using CHelper plug-in * Actual solution is at the top */ public class MovingPoints { public static void main(String[] args) throws IOException { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } static class TaskA { long mod = (long)(1000000007); public void solve(int testNumber, InputReader in, PrintWriter out) throws IOException { while(testNumber-->0){ int n = in.nextInt(); Combine a[] = new Combine[n]; TreeSet<Integer> s = new TreeSet<>(); for(int i=0;i<n;i++) a[i] = new Combine(in.nextInt() , 0); for(int i=0;i<n;i++){ a[i].delete = in.nextInt(); s.add(a[i].delete); } Arrays.sort(a , new Sort2()); // for(int i=0;i<n;i++){ // out.println(a[i].value + " " + a[i].delete); // } HashMap<Integer , Integer> index = new HashMap<>(); int b = 1; for(int i:s) index.put(i , b++); int count[] = new int[b+1]; long sum[] = new long[b+1]; long ans = 0; for(int i=0;i<n;i++){ int getCount = get(count , index.get(a[i].delete)); long getSum = get(sum , index.get(a[i].delete)); ans += (long)a[i].value*getCount - getSum; update(count , index.get(a[i].delete) , 1); update(sum , index.get(a[i].delete) , a[i].value); } out.println(ans); } } public void update(long a[] , int index , long value){ for(int i=index;i<a.length;i += i&(-i)) a[i] += value; } public long get(long a[] , int index){ long res = 0; for(int i=index;i>0;i-=i&(-i)) res += a[i]; return res; } public void update(int a[] , int index , int value){ for(int i=index;i<a.length;i += i&(-i)) a[i] += value; } public int get(int a[] , int index){ int res = 0; for(int i=index;i>0;i-=i&(-i)) res += a[i]; return res; } public int[] nextGreater(int a[] , int n){ int ng[] = new int[n+1]; Deque<Combine> s = new LinkedList<>(); for(int i=n;i>0;i--){ int k = i; Combine c = new Combine(a[k] , k); while(s.size()>0 && s.peekLast().value <= c.value){ Combine d = s.pollLast(); ng[d.delete] = c.delete; } s.addLast(c); } return ng; } public void sieve(int a[]){ a[0] = a[1] = 1; int i; for(i=2;i*i<=a.length;i++){ if(a[i] != 0) continue; a[i] = i; for(int k = (i)*(i);k<a.length;k+=i){ if(a[k] != 0) continue; a[k] = i; } } } public int [][] matrixExpo(int c[][] , int n){ int a[][] = new int[c.length][c[0].length]; int b[][] = new int[a.length][a[0].length]; for(int i=0;i<c.length;i++) for(int j=0;j<c[0].length;j++) a[i][j] = c[i][j]; for(int i=0;i<a.length;i++) b[i][i] = 1; while(n!=1){ if(n%2 == 1){ b = matrixMultiply(a , a); n--; } a = matrixMultiply(a , a); n/=2; } return matrixMultiply(a , b); } public int [][] matrixMultiply(int a[][] , int b[][]){ int r1 = a.length; int c1 = a[0].length; int c2 = b[0].length; int c[][] = new int[r1][c2]; for(int i=0;i<r1;i++){ for(int j=0;j<c2;j++){ for(int k=0;k<c1;k++) c[i][j] += a[i][k]*b[k][j]; } } return c; } public long nCrPFermet(int n , int r , long p){ if(r==0) return 1l; long fact[] = new long[n+1]; fact[0] = 1; for(int i=1;i<=n;i++) fact[i] = (i*fact[i-1])%p; long modInverseR = pow(fact[r] , p-2 , 1l , p); long modInverseNR = pow(fact[n-r] , p-2 , 1l , p); long w = (((fact[n]*modInverseR)%p)*modInverseNR)%p; return w; } public long pow(long a , long b , long res , long mod){ if(b==0) return res; if(b==1) return (res*a)%mod; if(b%2==1){ res *= a; res %= mod; b--; } // System.out.println(a + " " + b + " " + res); return pow((a*a)%mod , b/2 , res , mod); } public long pow(long a , long b , long res){ if(b == 0) return res; if(b==1) return res*a; if(b%2==1){ res *= a; b--; } return pow(a*a , b/2 , res); } public void swap(int a[] , int p1 , int p2){ int x = a[p1]; a[p1] = a[p2]; a[p2] = x; } public void sortedArrayToBST(TreeSet<Integer> a , int start, int end) { if (start > end) { return; } int mid = (start + end) / 2; a.add(mid); sortedArrayToBST(a, start, mid - 1); sortedArrayToBST(a, mid + 1, end); } class Combine{ int value; int delete; Combine(int val , int delete){ this.value = val; this.delete = delete; } } class Sort2 implements Comparator<Combine>{ public int compare(Combine a , Combine b){ if(a.value > b.value) return 1; else if(a.value == b.value && a.delete>b.delete) return 1; else if(a.value == b.value && a.delete == b.delete) return 0; return -1; } } public int lowerLastBound(ArrayList<Integer> a , int x){ int l = 0; int r = a.size()-1; if(a.get(l)>=x) return -1; if(a.get(r)<x) return r; int mid = -1; while(l<=r){ mid = (l+r)/2; if(a.get(mid) == x && a.get(mid-1)<x) return mid-1; else if(a.get(mid)>=x) r = mid-1; else if(a.get(mid)<x && a.get(mid+1)>=x) return mid; else if(a.get(mid)<x && a.get(mid+1)<x) l = mid+1; } return mid; } public int upperFirstBound(ArrayList<Integer> a , Integer x){ int l = 0; int r = a.size()-1; if(a.get(l)>x) return l; if(a.get(r)<=x) return r+1; int mid = -1; while(l<=r){ mid = (l+r)/2; if(a.get(mid) == x && a.get(mid+1)>x) return mid+1; else if(a.get(mid)<=x) l = mid+1; else if(a.get(mid)>x && a.get(mid-1)<=x) return mid; else if(a.get(mid)>x && a.get(mid-1)>x) r = mid-1; } return mid; } public int lowerLastBound(int a[] , int x){ int l = 0; int r = a.length-1; if(a[l]>=x) return -1; if(a[r]<x) return r; int mid = -1; while(l<=r){ mid = (l+r)/2; if(a[mid] == x && a[mid-1]<x) return mid-1; else if(a[mid]>=x) r = mid-1; else if(a[mid]<x && a[mid+1]>=x) return mid; else if(a[mid]<x && a[mid+1]<x) l = mid+1; } return mid; } public int upperFirstBound(long a[] , long x){ int l = 0; int r = a.length-1; if(a[l]>x) return l; if(a[r]<=x) return r+1; int mid = -1; while(l<=r){ mid = (l+r)/2; if(a[mid] == x && a[mid+1]>x) return mid+1; else if(a[mid]<=x) l = mid+1; else if(a[mid]>x && a[mid-1]<=x) return mid; else if(a[mid]>x && a[mid-1]>x) r = mid-1; } return mid; } public long log(float number , int base){ return (long) Math.floor((Math.log(number) / Math.log(base))); } public long gcd(long a , long b){ if(a<b){ long c = b; b = a; a = c; } if(b == 0) return a; return gcd(b , a%b); } public void print2d(long a[][] , PrintWriter out){ for(int i=0;i<a.length;i++){ for(int j=0;j<a[i].length;j++) out.print(a[i][j] + " "); out.println(); } out.println(); } public void print1d(int a[] , PrintWriter out){ for(int i=0;i<a.length;i++) out.print(a[i] + " "); out.println(); out.println(); } class Node{ int index; Node right; Node left; public Node(int index){ this.index = index; right = null; left = null; } } class AVLTree{ Node root; public AVLTree(){ this.root = null; } public int height(Node n){ return (n == null ? 0 : n.height); } public int getBalance(Node n){ return (n == null ? 0 : height(n.left) - height(n.right)); } public Node rotateRight(Node a){ Node b = a.left; Node br = b.right; a.lSum -= b.lSum; a.lCount -= b.lCount; b.rSum += a.rSum; b.rCount += a.rCount; b.right = a; a.left = br; a.height = 1 + Math.max(height(a.left) , height(a.right)); b.height = 1 + Math.max(height(b.left) , height(b.right)); return b; } public Node rotateLeft(Node a){ Node b = a.right; Node bl = b.left; a.rSum -= b.rSum; a.rCount -= b.rCount; b.lSum += a.lSum; b.lCount += a.lCount; b.left = a; a.right = bl; a.height = 1 + Math.max(height(a.left) , height(a.right)); b.height = 1 + Math.max(height(b.left) , height(b.right)); return b; } public Node insert(Node root , long value){ if(root == null){ return new Node(value); } if(value<=root.value){ root.lCount++; root.lSum += value; root.left = insert(root.left , value); } if(value>root.value){ root.rCount++; root.rSum += value; root.right = insert(root.right , value); } // updating the height of the root root.height = 1 + Math.max(height(root.left) , height(root.right)); int balance = getBalance(root); //ll if(balance>1 && value<=root.left.value) return rotateRight(root); //rr if(balance<-1 && value>root.right.value) return rotateLeft(root); //lr if(balance>1 && value>root.left.value){ root.left = rotateLeft(root.left); return rotateRight(root); } //rl if(balance<-1 && value<=root.right.value){ root.right = rotateRight(root.right); return rotateLeft(root); } return root; } public void insertElement(long value){ this.root = insert(root , value); } public int getElementLessThanK(long k){ int count = 0; Node temp = root; while(temp!=null){ if(temp.value == k){ if(temp.left == null || temp.left.value<k){ count += temp.lCount; return count-1; } else temp = temp.left; } else if(temp.value>k){ temp = temp.left; } else{ count += temp.lCount; temp = temp.right; } } return count; } public void inorder(Node root , PrintWriter out){ Node temp = root; if(temp!=null){ inorder(temp.left , out); out.println(temp.value + " " + temp.lCount + " " + temp.rCount); inorder(temp.right , out); } } class Node{ long value; long lCount , rCount; long lSum , rSum; Node left , right; int height; public Node(long value){ this.value = value; left = null; right = null; lCount = 1; rCount = 1; lSum = value; rSum = value; height = 1; } } } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }
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.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.HashMap; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.util.Map; import java.io.BufferedReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author htvu */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); FMovingPoints solver = new FMovingPoints(); solver.solve(1, in, out); out.close(); } static class FMovingPoints { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); Integer[] x = new Integer[n]; int[] v = new int[n]; long ans = 0; Map<Integer, Integer> rank = new HashMap<>(); for (int i = 0; i < n; ++i) { x[i] = in.nextInt(); } for (int i = 0; i < n; ++i) v[i] = in.nextInt(); Pair.IntPair[] ps = new Pair.IntPair[n]; for (int i = 0; i < n; ++i) ps[i] = new Pair.IntPair(x[i], v[i]); Arrays.sort(ps, (p1, p2) -> !p1.second.equals(p2.second) ? p1.second - p2.second : p1.first - p2.first); Arrays.sort(x); for (int i = 0; i < n; ++i) { // add x[i] to pair sum ans += (long) x[i] * (2 * i + 1 - n); // record position of x[i] sorted by v[i] rank.put(ps[i].first, i); } for (int i = 0; i < n; ++i) { // ans -= (long)ps[i].first * (rank.get(ps[i].first) - i); ans -= (long) x[i] * (i - rank.get(x[i])); } out.println(ans); } } static class Pair<A extends Comparable<A>, B extends Comparable<B>> implements Comparable<Pair<A, B>> { public A first; public B second; public Pair(A a, B b) { this.first = a; this.second = b; } public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair<?, ?> pair = (Pair<?, ?>) o; return first.equals(pair.first) && second.equals(pair.second); } public int hashCode() { return (first == null ? 0 : first.hashCode()) ^ (second == null ? 0 : second.hashCode()); } public String toString() { return "{" + first + "," + second + '}'; } public int compareTo(Pair<A, B> o) { int c = first.compareTo(o.first); if (c != 0) return c; return second.compareTo(o.second); } public static class IntPair extends Pair<Integer, Integer> { public IntPair(Integer integer, Integer integer2) { super(integer, integer2); } } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
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; 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.*; import java.util.*; import java.math.*; import java.awt.Point; public class Main { //static final long MOD = 998244353L; //static final long INF = -1000000000000000007L; static final long MOD = 1000000007L; //static final int INF = 1000000007; //static long[] factorial; public static void main(String[] args) { FastScanner sc = new FastScanner(); PrintWriter pw = new PrintWriter(System.out); int N = sc.ni(); int[][] points = new int[N][3]; //x,v,i SegmentTree left = new SegmentTree(N); SegmentTree right = new SegmentTree(N); for (int i = 0; i < N; i++) { points[i][0] = sc.ni(); } for (int i = 0; i < N; i++) points[i][1] = sc.ni(); //sort by increasing position points = shuffle(points); points = sort(points); for (int i = 0; i < N; i++) points[i][2] = i; //pw.println(Arrays.deepToString(points)); SegmentTree leftSize = new SegmentTree(N); SegmentTree rightSize = new SegmentTree(N); for (int i = 0; i < N; i++) { if (points[i][1] < 0) { leftSize.update(0, 0, N-1, i, 1); } else { rightSize.update(0, 0, N-1, i, 1); } } for (int i = 0; i < N; i++) { if (points[i][1] < 0) { left.update(0, 0, N-1, i, points[i][0]); } else { right.update(0, 0, N-1, i, points[i][0]); } } long ans = 0; for (int i = 0; i < N; i++) { if (right.arr[i] > 0) { ans += (points[i][0]*leftSize.query(0, 0, N-1, 0, i)-left.query(0, 0, N-1, 0, i)); } } PriorityQueue<int[]> pqR = new PriorityQueue<int[]>(new Comparator<int[]>(){ public int compare(int[] a, int[] b) { if (b[1] != a[1]) { return b[1] - a[1]; } else { return b[2] - a[2]; } } }); for (int[] point: points) { if (point[1] >= 0) { pqR.add(point); } } while (! pqR.isEmpty()) { int[] point = pqR.poll(); ans += (point[0]*rightSize.query(0, 0, N-1, 0, point[2]) - right.query(0, 0, N-1, 0, point[2])); right.update(0, 0, N-1, point[2], 0); rightSize.update(0, 0, N-1, point[2], 0); } PriorityQueue<int[]> pqL = new PriorityQueue<int[]>(new Comparator<int[]>(){ public int compare(int[] a, int[] b) { if (b[1] != a[1]) { return a[1] - b[1]; } else { return a[2] - b[2]; } } }); for (int[] point: points) { if (point[1] < 0) { pqL.add(point); } } while (! pqL.isEmpty()) { int[] point = pqL.poll(); ans += (left.query(0, 0, N-1, point[2], N-1)-point[0]*leftSize.query(0, 0, N-1, point[2], N-1)); left.update(0, 0, N-1, point[2], 0); leftSize.update(0, 0, N-1, point[2], 0); } pw.println(ans); pw.close(); } static class SegmentTree { public long[] arr; public long[] tree; public SegmentTree(int N) { //0 indexed seg tree arr = new long[N]; tree = new long[4*N+1]; } public void buildSegTree(int treeIndex, int lo, int hi) { if (lo == hi) { tree[treeIndex] = arr[lo]; return; } int mid = lo + (hi - lo) / 2; buildSegTree(2 * treeIndex + 1, lo, mid); buildSegTree(2 * treeIndex + 2, mid + 1, hi); tree[treeIndex] = merge(tree[2 * treeIndex + 1], tree[2 * treeIndex + 2]); } public long query(int treeIndex, int lo, int hi, int i, int j) { // query for arr[i..j] if (lo > j || hi < i) return 0; if (i <= lo && j >= hi) return tree[treeIndex]; int mid = lo + (hi - lo) / 2; if (i > mid) return query(2 * treeIndex + 2, mid + 1, hi, i, j); else if (j <= mid) return query(2 * treeIndex + 1, lo, mid, i, j); long leftQuery = query(2 * treeIndex + 1, lo, mid, i, mid); long rightQuery = query(2 * treeIndex + 2, mid + 1, hi, mid + 1, j); // merge query results return merge(leftQuery, rightQuery); } public void update(int treeIndex, int lo, int hi, int arrIndex, long val) { if (lo == hi) { tree[treeIndex] = val; arr[arrIndex] = val; return; } int mid = lo + (hi - lo) / 2; if (arrIndex > mid) update(2 * treeIndex + 2, mid + 1, hi, arrIndex, val); else if (arrIndex <= mid) update(2 * treeIndex + 1, lo, mid, arrIndex, val); // merge updates tree[treeIndex] = merge(tree[2 * treeIndex + 1], tree[2 * treeIndex + 2]); } public long merge(long a, long b) { return (a+b); } } public static long dist(long[] p1, long[] p2) { return (Math.abs(p2[0]-p1[0])+Math.abs(p2[1]-p1[1])); } //Find the GCD of two numbers public static long gcd(long a, long b) { if (a < b) return gcd(b,a); if (b == 0) return a; else return gcd(b,a%b); } //Fast exponentiation (x^y mod m) public static long power(long x, long y, long m) { if (y < 0) return 0L; long ans = 1; x %= m; while (y > 0) { if(y % 2 == 1) ans = (ans * x) % m; y /= 2; x = (x * x) % m; } return ans; } public static int[][] shuffle(int[][] array) { Random rgen = new Random(); for (int i = 0; i < array.length; i++) { int randomPosition = rgen.nextInt(array.length); int[] temp = array[i]; array[i] = array[randomPosition]; array[randomPosition] = temp; } return array; } public static int[][] sort(int[][] array) { //Sort an array (immune to quicksort TLE) Arrays.sort(array, new Comparator<int[]>() { @Override public int compare(int[] a, int[] b) { return a[0]-b[0]; //ascending order } }); return array; } public static long[][] sort(long[][] array) { //Sort an array (immune to quicksort TLE) Random rgen = new Random(); for (int i = 0; i < array.length; i++) { int randomPosition = rgen.nextInt(array.length); long[] temp = array[i]; array[i] = array[randomPosition]; array[randomPosition] = temp; } Arrays.sort(array, new Comparator<long[]>() { @Override public int compare(long[] a, long[] b) { if (a[0] < b[0]) return -1; else return 1; } }); return array; } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { br = new BufferedReader(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 ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nd() { 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
java
import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int[][] xv = new int[n][2]; Integer[] v = new Integer[n]; for (int i = 0; i < n; i++) { xv[i][0] = scanner.nextInt(); } for (int i = 0; i < n; i++) { xv[i][1] = scanner.nextInt(); v[i] = xv[i][1]; } Arrays.sort(v); Arrays.sort(xv, (o1, o2) -> { if (o1[0] != o2[0]) return Integer.compare(o1[0], o2[0]); return Integer.compare(o1[1], o2[1]); }); FenwickTreeLong f1 = new FenwickTreeLong(n); FenwickTreeLong f2 = new FenwickTreeLong(n); long result = 0; for (int i = 0; i < n; i++) { //Enumerate according to the x coordinate from small to large //Find the position of v after discretization int pos = Arrays.binarySearch(v, xv[i][1]); pos++; //The speed is smaller than the current speed and the contribution is the difference in distance //The speed with num points is lower than the current speed long num = f1.sum(pos); //The sum of the coordinates of the num points is xsum long xsum = f2.sum(pos); //Calculate the contribution to the answer result += num * xv[i][0] - xsum; f1.change(pos, 1); f2.change(pos, xv[i][0]); } System.out.println(result); } } class FenwickTreeLong { long[] t; int n; public FenwickTreeLong(int n) { this.n = n; this.t = new long[n + 1]; } void change(int pos, int val) { while (pos <= n) { t[pos] += val; pos += lowbit(pos); } } int lowbit(int k) { return k & (-k); } long sum(int pos) { long ans = 0; while (pos > 0) { ans += t[pos]; pos -= lowbit(pos); } return ans; } long ask(int l, int r) { return sum(r) - sum(l); } }
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 maxn = 2e5; int m; long long val[maxn + 11]; int cnt[maxn + 11]; pair<int, int> p[maxn + 11]; vector<int> v; int lowbit(int x) { return x & (-x); } void add(int x, int o) { for (; x <= m; x += lowbit(x)) { cnt[x] += 1; val[x] += o; } } pair<int, long long> query(int x) { int resc = 0; long long resv = 0; for (; x; x -= lowbit(x)) { resc += cnt[x]; resv += val[x]; } return make_pair(resc, resv); } int main() { int n; cin >> n; for (int i = 1; i <= n; i++) cin >> p[i].first; for (int i = 1; i <= n; i++) { cin >> p[i].second; v.emplace_back(p[i].second); } sort(p + 1, p + 1 + n); sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); m = v.size(); long long ans = 0; for (int i = 1; i <= n; i++) { int pos = lower_bound(v.begin(), v.end(), p[i].second) - v.begin() + 1; pair<int, long long> pi = query(pos); ans += 1ll * pi.first * p[i].first - pi.second; add(pos, p[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
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 5; pair<int, int> p[maxn]; int n; long long d[maxn]; long long e[maxn]; void add(long long *c, int i, int k) { while (i <= n) { c[i] += k; i += i & (-i); } } long long sum(long long *c, int i) { long long res = 0; while (i > 0) { res += c[i]; i -= i & (-i); } return res; } int main() { cin >> n; for (int i = 0; i <= n - 1; i++) cin >> p[i].first; for (int i = 0; i <= n - 1; i++) cin >> p[i].second; sort(p, p + n); vector<int> es(n); for (int i = 0; i <= n - 1; i++) es[i] = p[i].second; sort(es.begin(), es.end()); long long res = 0; for (int i = 0; i <= n - 1; i++) { int pp = lower_bound(es.begin(), es.end(), p[i].second) - es.begin() + 1; res += sum(d, pp) * p[i].first - sum(e, pp); add(d, pp, 1); add(e, pp, p[i].first); } cout << res << "\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; long long BIT[200009][4], N = 200009, n, ans, I; struct st { long long x, v; } a[200009]; bool cmp(st a, st b) { if (a.v == b.v) return a.x > b.x; return a.v > b.v; } bool cmp2(st a, st b) { if (a.v == b.v) return a.x > b.x; return a.v > b.v; } set<long long> s; map<long long, long long> mp; void update(long long x, long long val, long long zz) { for (; x < N; x += x & -x) BIT[x][zz] += val; } long long query(long long x, long long zz) { long long sum = 0; for (; x > 0; x -= x & -x) sum += BIT[x][zz]; return sum; } long long q(long long l, long long r, long long zz) { return query(r, zz) - query(l - 1, zz); } int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i].x; s.insert(a[i].x); } for (int i = 0; i < n; i++) { cin >> a[i].v; } for (auto x : s) mp[x] = ++I; sort(a, a + n, cmp); for (int i = 0; i < n; i++) { ans += q(mp[a[i].x] + 1, I, 1) - a[i].x * q(mp[a[i].x] + 1, I, 2); update(mp[a[i].x], a[i].x, 1); update(mp[a[i].x], 1, 2); } 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; const int N = 2e5 + 7; pair<int, int> p[N]; struct fenwick { long long fen[N]; void add(int first, int d) { for (int i = first + 1; i < N; i += i & -i) { fen[i] += d; } } long long sum(int first) { long long ans = 0; for (int i = first + 1; i; i -= i & -i) { ans += fen[i]; } return ans; } long long sum(int l, int r) { return sum(r) - sum(l - 1); } } val, cnt; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; for (int i = 0; i < n; ++i) cin >> p[i].first; for (int i = 0; i < n; ++i) cin >> p[i].second; sort(p, p + n); vector<int> a(n); for (int i = 0; i < n; ++i) a[i] = p[i].second; sort(a.begin(), a.end()); a.resize(unique(a.begin(), a.end()) - a.begin()); for (int i = 0; i < n; ++i) { p[i].second = lower_bound(a.begin(), a.end(), p[i].second) - a.begin(); } long long ans = 0; for (int i = n - 1; ~i; --i) { val.add(p[i].second, p[i].first); cnt.add(p[i].second, 1); ans += val.sum(p[i].second, 2e5) - cnt.sum(p[i].second, 2e5) * p[i].first; } 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; vector<pair<long long, long long>> t; pair<long long, long long> get(long long v, long long l, long long r, long long a, long long b) { if (l > b || a > r) { return {0, 0}; } if (a <= l && r <= b) { return t[v]; } long long m = (l + r) / 2; pair<long long, long long> tmp = get(2 * v, l, m, a, b), tmp2 = get(2 * v + 1, m + 1, r, a, b); return {tmp.first + tmp2.first, tmp.second + tmp2.second}; } void update(long long v, long long l, long long r, long long i, long long x) { if (l > i || i > r) { return; } if (l == r) { t[v] = {t[v].first + x, t[v].second + 1}; return; } long long m = (l + r) / 2; update(2 * v, l, m, i, x); update(2 * v + 1, m + 1, r, i, x); t[v].first = t[2 * v].first + t[2 * v + 1].first; t[v].second = t[2 * v].second + t[2 * v + 1].second; } signed main() { long long n; cin >> n; long long q = 1 << (long long)ceil(log2(n)); t.resize(2 * q); vector<pair<long long, long long>> x(n); for (long long i = 0; i < n; ++i) { cin >> x[i].first; } for (long long i = 0; i < n; ++i) { cin >> x[i].second; } sort(x.begin(), x.end()); vector<long long> a(n); for (long long i = 0; i < n; ++i) { a[i] = x[i].second; } unordered_map<long long, long long> b; sort(a.begin(), a.end()); a.resize(unique(a.begin(), a.end()) - a.begin()); for (long long i = 0; i < a.size(); ++i) { b[a[i]] = i; } vector<long long> c(n); for (long long i = 0; i < n; ++i) { c[i] = b[x[i].second]; } long long now = 0; long long sum = 0, kol = 0; for (long long i = 0; i < n; ++i) { if (x[i].second < 0) { sum += x[i].first; ++kol; continue; } if (x[i].second > 0) { pair<long long, long long> tmp = get(1, 0, q - 1, 0, c[i]); now += tmp.second * x[i].first - tmp.first; update(1, 0, q - 1, c[i], x[i].first); } else { now += kol * x[i].first - sum; sum += x[i].first; ++kol; } } t.assign(2 * q, {0, 0}); sum = 0; kol = 0; for (long long i = n - 1; i >= 0; --i) { if (x[i].second > 0) { sum += x[i].first; ++kol; continue; } if (x[i].second < 0) { pair<long long, long long> tmp = get(1, 0, q - 1, c[i], n - 1); now += tmp.first - tmp.second * x[i].first; update(1, 0, q - 1, c[i], x[i].first); } else { now += sum - kol * x[i].first; } } long long nowl = 0, koll = 0, nowr = 0, kolr = 0; for (long long i = 0; i < n; ++i) { if (x[i].second > 0) { now += koll * x[i].first - nowl; ++kolr; nowr += x[i].first; } else if (x[i].second < 0) { ++koll; nowl += x[i].first; } } cout << now; 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
class BIT(): def __init__(self, array): self.n = len(array) self.bit = [0] + array self.build() def build(self): for i in range(1, self.n): if i + (i & -i) > self.n: continue self.bit[i + (i & -i)] += self.bit[i] def _sum(self, i): s = 0 while i > 0: s += self.bit[i] i -= i & -i return s def add(self, i, val): i += 1 while i <= self.n: self.bit[i] += val i += i & -i def get_sum(self, l, r): return self._sum(r) - self._sum(l) from operator import itemgetter import sys input = sys.stdin.readline n = int(input()) x = list(map(int, input().split())) v = list(map(int, input().split())) sorted_x = sorted(x) bit = BIT(sorted_x) bit_ind = BIT([1] * n) to_ind = {v: i for i, v in enumerate(sorted_x)} info = [None] * n for i in range(n): info[i] = (v[i] + 10**18, x[i]) info = sorted(info, key=itemgetter(1), reverse=True) info = sorted(info, key=itemgetter(0), reverse=True) ans = 0 for i in range(n): _, pos = info[i] ind = to_ind[pos] sum_ = bit.get_sum(0, ind) num = bit_ind.get_sum(0, ind) ans += num * pos - sum_ bit.add(ind, -pos) bit_ind.add(ind, -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
java
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.util.Comparator; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author xwchen */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskF solver = new TaskF(); solver.solve(1, in, out); out.close(); } static class TaskF { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int[] x = in.nextIntArray(n); int[] v = in.nextIntArray(n); v = ArrayUtils.shrink(v); Point[] points = new Point[n]; for (int i = 0; i < n; ++i) { points[i] = new Point(x[i], v[i]); } Arrays.sort(points, Comparator.comparingInt(p -> p.x)); BinaryIndexedTree Vcnt = new BinaryIndexedTree(n); BinaryIndexedTree VXsum = new BinaryIndexedTree(n); long res = 0; for (int i = n - 1; i >= 0; --i) { int X = points[i].x; int V = points[i].v; long cnt = Vcnt.queryRange(V, n); long sum = VXsum.queryRange(V, n); res += sum - X * cnt; Vcnt.update(V, 1); VXsum.update(V, X); } out.println(res); } class Point { int x; int v; public Point(int x, int v) { this.x = x; this.v = v; } } } static class BinaryIndexedTree { int n; long[] bit; public BinaryIndexedTree(int n) { this.n = n; bit = new long[n + 1]; } public void update(int i, int add) { while (i > 0 && i <= n) { bit[i] += add; i = i + (i & (-i)); } } public long sum(int i) { long ans = 0; while (i > 0) { ans += bit[i]; i = i - (i & (-i)); } return ans; } public long queryRange(int i, int j) { return sum(j) - sum(i - 1); } } static class ArrayUtils { public static int[] shrink(int[] a) { int n = a.length; int[] ret = new int[n]; Integer[] b = new Integer[n]; for (int i = 0; i < n; ++i) { b[i] = i; } Arrays.sort(b, Comparator.comparingInt(x -> a[x])); int p = 0; for (int i = 0; i < n; ++i) { if (i - 1 >= 0 && a[b[i]] == a[b[i - 1]]) { ret[b[i]] = p; } else { ret[b[i]] = ++p; } } return ret; } } static class InputReader { private BufferedReader reader; private StringTokenizer tokenizer = new StringTokenizer(""); public InputReader(InputStream inputStream) { this.reader = new BufferedReader( new InputStreamReader(inputStream)); } public String next() { while (!tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { e.printStackTrace(); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } } }
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.Scanner; import java.util.Comparator; public class R624_F { private static long sum (long[] t, int pos) { long result = 0; while (pos >= 0) { result += t[pos]; pos = (pos & (pos + 1)) - 1; } return result; } private static void upd (long[] t, int pos, int delta) { while (pos < t.length) { t[pos] += delta; pos = pos | (pos + 1); } } public static void main(String[] args) { Scanner in = new Scanner (System.in); int n = in.nextInt(); int[][] p = new int[n][2]; int[][] v = new int[n][2]; for (int i = 0; i < n; i++) p[i][0] = in.nextInt(); for (int i = 0; i < n; i++) { p[i][1] = in.nextInt(); v[i][0] = p[i][1]; v[i][1] = i; } in.close(); Arrays.sort(v, new Comparator<int[]>() { public int compare(int[] a, int[] b) { return a[0] - b[0]; } }); int idx = 0; int[][] ret = new int[n][2]; for (int i = 0; i < n; i++) { if (i > 0 && v[i][0] != v[i-1][0]) idx++; ret[i][0] = idx; ret[i][1] = v[i][1]; } Arrays.sort(ret, new Comparator<int[]>() { public int compare(int[] a, int[] b) { return a[1] - b[1]; } }); for (int i = 0; i < n; i++) { p[i][1] = ret[i][0]; } Arrays.sort(p, new Comparator<int[]>() { public int compare(int[] a, int[] b) { return a[0] - b[0]; } }); long[] xs = new long[n]; long[] cnt = new long[n]; long ans = 0; for (int i = 0; i < n; i++) { int pos = p[i][1]; ans += sum(cnt, pos) * p[i][0] - sum (xs, pos); upd(cnt, pos, 1); upd(xs, pos, p[i][0]); } System.out.println(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; int b[200005], m; long long c[200005][2]; struct node { int x, v; bool operator<(const node t) const { return x < t.x; } } a[200005]; int lsh(long long x) { return lower_bound(b + 1, b + 1 + m, x) - b; } long long query(int x, int k) { long long ret = 0; for (int i = x; i >= 1; i -= i & -i) ret += c[i][k]; return ret; } void update(int x, int y) { for (int i = x; i <= m; i += i & -i) { c[i][0]++; c[i][1] += y; } } int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i].x); for (int i = 1; i <= n; i++) { scanf("%d", &a[i].v); b[i] = a[i].v; } sort(b + 1, b + 1 + n); m = unique(b + 1, b + 1 + n) - b - 1; sort(a + 1, a + 1 + n); long long ans = 0; for (int i = 1; i <= n; i++) { int t = lsh(a[i].v); ans += query(t, 0) * a[i].x - query(t, 1); update(t, a[i].x); } printf("%lld", 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
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.HashMap; import java.util.InputMismatchException; import java.io.IOException; import java.util.Random; import java.util.ArrayList; import java.util.Objects; import java.util.List; import java.io.InputStream; /** * Built using CHelper plug-in Actual solution is at the top * * @author MaxHeap */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); FMovingPoints solver = new FMovingPoints(); solver.solve(1, in, out); out.close(); } static class FMovingPoints { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int[] x = in.nextIntArray(n); int[] v = in.nextIntArray(n); // v = Arr.compress(v); int[] so = v.clone(); Sort.sort(so); int ne = 0; HashMap<Integer, Integer> map = new HashMap<>(); for (int i : so) { if (i < 0) { ne++; } } int st = -ne; for (int i : so) { if (st == 0) { ++st; } if (!map.containsKey(i)) { map.put(i, st++); } } for (int i = 0; i < n; ++i) { v[i] = map.get(v[i]); } int pt = 0; IntPair[] pts = new IntPair[n]; for (int i = 0; i < n; ++i) { pts[i] = new IntPair(x[i], v[i]); } Arrays.sort(pts); long ans = 0; // <- -> // out.println(Arrays.toString(pts)); long sum = 0, cnt = 0; for (int i = 0; i < n; ++i) { if (pts[i].y < 0) { sum += pts[i].x; ++cnt; } else { ans += cnt * pts[i].x - sum; } } // same direction different speed List<IntPair> pos = new ArrayList<>(); List<IntPair> neg = new ArrayList<>(); for (int i = 0; i < n; ++i) { if (pts[i].y < 0) { neg.add(pts[i]); } else { pos.add(pts[i]); } } FMovingPoints.FenTree tree = new FMovingPoints.FenTree(2 * n); FMovingPoints.FenTree treeCount = new FMovingPoints.FenTree(2 * n); for (int i = pos.size() - 1; i >= 0; --i) { IntPair cur = pos.get(i); long has = tree.get(cur.y, 2 * n - 1); long count = treeCount.get(cur.y, 2 * n - 1); ans += has - count * cur.x; tree.update(cur.y, cur.x); treeCount.update(cur.y, 1); } tree = new FMovingPoints.FenTree(2 * n); treeCount = new FMovingPoints.FenTree(2 * n); for (int i = 0; i < neg.size(); ++i) { IntPair cur = neg.get(i); cur.y *= -1; long has = tree.get(cur.y, 2 * n - 1); long count = treeCount.get(cur.y, 2 * n - 1); ans += count * cur.x - has; tree.update(cur.y, cur.x); treeCount.update(cur.y, 1); } out.println(ans); } static class FenTree { long[] values; int n; public FenTree(int n) { values = new long[n]; this.n = n; } void update(int index, long value) { while (index < n) { values[index] += value; index += (index & -index); } } long get(int index) { long res = 0; while (index > 0) { res += values[index]; index -= (index & -index); } return res; } long get(int l, int r) { return get(r) - get(l - 1); } } } static final class Sort { final static Random rand = new Random(); public static void shuffle(int[] arr) { for (int i = 0; i < arr.length; ++i) { int index = rand.nextInt(arr.length); swap(arr, index, i); } } public static void sort(int[] arr) { shuffle(arr); Arrays.sort(arr); } public static void swap(int[] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } static class IntPair implements Comparable<IntPair> { public int x; public int y; public IntPair(int x, int y) { this.x = x; this.y = y; } public IntPair(IntPair that) { this.x = that.x; this.y = that.y; } public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } IntPair intPair = (IntPair) o; return x == intPair.x && y == intPair.y; } public int hashCode() { return Objects.hash(x, y); } public String toString() { return "IntPair{" + "x=" + x + ", y=" + y + '}'; } public int compareTo(IntPair o) { int c = Integer.compare(x, o.x); if (c == 0) { return Integer.compare(y, o.y); } return c; } } static class InputReader { private InputStream stream; private static final int DEFAULT_BUFFER_SIZE = 1 << 16; private static final int EOF = -1; private byte[] buf = new byte[DEFAULT_BUFFER_SIZE]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (this.numChars == EOF) { throw new UnknownError(); } else { if (this.curChar >= this.numChars) { this.curChar = 0; try { this.numChars = this.stream.read(this.buf); } catch (IOException ex) { throw new InputMismatchException(); } if (this.numChars <= 0) { return EOF; } } return this.buf[this.curChar++]; } } public int nextInt() { int c; for (c = this.read(); isSpaceChar(c); c = this.read()) { } byte sgn = 1; if (c == 45) { sgn = -1; c = this.read(); } int res = 0; while (c >= 48 && c <= 57) { res *= 10; res += c - 48; c = this.read(); if (isSpaceChar(c)) { return res * sgn; } } throw new InputMismatchException(); } public static boolean isSpaceChar(int c) { return c == 32 || c == 10 || c == 13 || c == 9 || c == EOF; } public int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } } }
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 segtree { long long int M = 1, INIT; vector<long long int> dat; segtree(long long int N, long long int num) { INIT = num; while (M < N) M *= 2; for (int i = 0; i < M * 2 - 1; i++) dat.push_back(num); } void update(long long int x, long long int k, long long int l = 0, int r = -1) { if (r == -1) r = M; k += M - 1; dat[k] += x; while (k > 0) k = (k - 1) / 2, dat[k] = dat[k * 2 + 1] + dat[k * 2 + 2]; } long long int query(long long int a, long long int b = -1, long long int k = 0, long long int l = 0, long long int r = -1) { if (r == -1) r = M; if (b == -1) b = M; if (r <= a || b <= l) return INIT; if (a <= l && r <= b) return dat[k]; long long int A = query(a, b, k * 2 + 1, l, (l + r) / 2); long long int B = query(a, b, k * 2 + 2, (l + r) / 2, r); return A + B; } }; int main(int argc, char const *argv[]) { long long int n; scanf("%lld", &n); vector<vector<long long int> > l = vector<vector<long long int> >( 0, vector<long long int>(3, 0)), r = vector<vector<long long int> >( 0, vector<long long int>(3, 0)); long long int b; map<long long int, long long int> mp, point; vector<long long int> x(n); for (long long int i = 0; i < n; i++) scanf("%lld", &x[i]); vector<vector<long long int> > d = vector<vector<long long int> >(n, vector<long long int>(3, 0)); for (long long int i = 0; i < n; i++) { scanf("%lld", &b); d[i][0] = x[i], d[i][1] = b; if (b <= 0) l.push_back(vector<long long int>{x[i], abs(b), 0}); if (b >= 0) r.push_back(vector<long long int>{x[i], abs(b), 0}); mp.emplace(abs(b), 0); point.emplace(x[i], 0); } long long int c = 0; for (auto itr = mp.begin(); itr != mp.end(); itr++, c++) (*itr).second = c; c = 0; for (auto itr = point.begin(); itr != point.end(); itr++, c++) (*itr).second = c; long long int ans = 0; for (long long int i = 0; i < l.size(); i++) l[i][2] = mp.at(l[i][1]); for (long long int i = 0; i < r.size(); i++) r[i][2] = mp.at(r[i][1]); for (long long int i = 0; i < n; i++) d[i][2] = point.at(d[i][0]); segtree seg_l(mp.size(), 0), seg_r(mp.size(), 0), p(point.size(), 0); segtree seg_l_num(mp.size(), 0), seg_r_num(mp.size(), 0), p_num(point.size(), 0); sort((l).begin(), (l).end()); sort((r).begin(), (r).end()); sort((d).begin(), (d).end()); for (int i = r.size() - 1; i >= 0; i--) { long long int SUM = seg_r.query(r[i][2], mp.size() + 1); long long int NUM = seg_r_num.query(r[i][2], mp.size() + 1); ans += (SUM - r[i][0] * NUM); if (r[i][1] != 0) seg_r.update(r[i][0], r[i][2]), seg_r_num.update(1, r[i][2]); } for (long long int i = 0; i < l.size(); i++) { long long int SUM = seg_l.query(l[i][2], mp.size() + 1); long long int NUM = seg_l_num.query(l[i][2], mp.size() + 1); ans += (l[i][0] * NUM - SUM); seg_l.update(l[i][0], l[i][2]), seg_l_num.update(1, l[i][2]); } for (long long int i = 0; i < n; i++) { if (d[i][1] < 0) p.update(d[i][0], d[i][2]), p_num.update(1, d[i][2]); if (d[i][1] > 0) { long long int SUM = p.query(0, d[i][2]); long long int NUM = p_num.query(0, d[i][2]); ans += (d[i][0] * NUM - SUM); } } std::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; vector<vector<pair<long long, long long>>> mt(4 * 200005); vector<pair<long long, long long>> a; void buildTree(long long idx, long long second, long long se) { if (second == se) { mt[idx].push_back({a[second].second, a[second].first}); return; } long long mid = (second + se) / 2; buildTree(2 * idx + 1, second, mid); buildTree(2 * idx + 2, mid + 1, se); merge(mt[2 * idx + 1].begin(), mt[2 * idx + 1].end(), mt[2 * idx + 2].begin(), mt[2 * idx + 2].end(), back_inserter(mt[idx])); } pair<long long, long long> query(long long node, long long qs, long long qe, long long second, long long se, long long diff) { if (qs > se || qe < second) return {0, 0}; if (qs <= second && qe >= se) { long long l = 0, r = mt[node].size() - 1, ind = 1e9; while (l <= r) { long long mid = (l + r) / 2; if (mt[node][mid].first >= diff) { ind = min(ind, mid); r = mid - 1; } else { l = mid + 1; } } pair<long long, long long> ans = {0, 0}; if (ind != 1e9) { ans.second = mt[node].size() - ind; ans.first = mt[node][mt[node].size() - 1].second; if (ind != 0) ans.first -= mt[node][ind - 1].second; } return ans; } long long mid = (second + se) / 2; pair<long long, long long> p1 = query(2 * node + 1, qs, qe, second, mid, diff); pair<long long, long long> p2 = query(2 * node + 2, qs, qe, mid + 1, se, diff); p1.first += p2.first; p1.second += p2.second; return p1; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long n; cin >> n; a.resize(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.begin(), a.end()); buildTree(0, 0, n - 1); for (int i = 0; i < (4 * n) + 5; i++) { for (int j = 1; j < mt[i].size(); j++) mt[i][j].second += mt[i][j - 1].second; } long long ans = 0; for (int i = 0; i < n; i++) { pair<long long, long long> q = query(0, i + 1, n - 1, 0, n - 1, a[i].second); ans += (q.first - (q.second * a[i].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; int num[800005], m; long long b[200005], sum[200005], cnt[200005], tr[800005]; struct node { int x, v; bool operator<(const node t) const { return x < t.x; } } a[200005]; int lsh(long long x) { return lower_bound(b + 1, b + 1 + m, x) - b; } void build(int l, int r, int p) { if (l == r) { tr[p] = sum[l]; num[p] = cnt[l]; return; } int mid = l + r >> 1; build(l, mid, p << 1); build(mid + 1, r, p << 1 | 1); tr[p] = tr[p << 1] + tr[p << 1 | 1]; num[p] = num[p << 1] + num[p << 1 | 1]; } pair<long long, int> query(int l, int r, int x, int y, int p) { if (l == x && r == y) return make_pair(tr[p], num[p]); int mid = l + r >> 1; if (y <= mid) return query(l, mid, x, y, p << 1); else if (x > mid) return query(mid + 1, r, x, y, p << 1 | 1); else { pair<long long, int> v1 = query(l, mid, x, mid, p << 1), v2 = query(mid + 1, r, mid + 1, y, p << 1 | 1); return make_pair(v1.first + v2.first, v1.second + v2.second); } } void update(int l, int r, int x, int y, int p) { if (l == r) { tr[p] -= y; num[p]--; return; } int mid = l + r >> 1; if (x <= mid) update(l, mid, x, y, p << 1); else update(mid + 1, r, x, y, p << 1 | 1); tr[p] = tr[p << 1] + tr[p << 1 | 1]; num[p] = num[p << 1] + num[p << 1 | 1]; } int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i].x); for (int i = 1; i <= n; i++) { scanf("%d", &a[i].v); b[i] = a[i].v; } sort(b + 1, b + 1 + n); m = unique(b + 1, b + 1 + n) - b - 1; sort(a + 1, a + 1 + n); for (int i = 1; i <= n; i++) { int x = lsh(a[i].v); sum[x] += a[i].x; cnt[x]++; } build(1, m, 1); long long ans = 0; for (int i = 1; i <= n; i++) { int x = lsh(a[i].v); update(1, m, x, a[i].x, 1); pair<long long, int> t = query(1, m, x, m, 1); ans += t.first - (long long)a[i].x * t.second; } printf("%lld", 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; template <class T> class BIT { private: vector<T> dat; int n; public: BIT(int _n) : dat(_n + 1), n(_n) {} T sum(int i) { T s = 0; while (i > 0) s += dat[i], i -= i & -i; return s; } T sum(int l, int r) { return l > r ? 0 : sum(r) - sum(l - 1); } void add(int i, T x) { while (i <= n) dat[i] += x, i += i & -i; } }; int main() { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; vector<pair<int64_t, int64_t>> P(N); for (auto &p : P) cin >> p.second; for (auto &p : P) cin >> p.first; sort(P.begin(), P.end()); vector<int> vs; for (auto &p : P) vs.push_back(p.first); P[0].first = 1; for (int i = 1; i < N; ++i) { P[i].first = P[i - 1].first + (vs[i - 1] != vs[i]); } for (auto &p : P) swap(p.first, p.second); sort(P.begin(), P.end(), greater<pair<int, int>>()); int64_t ans = 0LL; BIT<int64_t> bit(N), cnt(N); for (auto &p : P) { int x, v; tie(x, v) = p; ans += bit.sum(v, N) - x * cnt.sum(v, N); bit.add(v, x); cnt.add(v, 1); } 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 bit[200005][2]; void update(int x, long long val) { for (int i = x; i <= n; i += (i & -i)) bit[i][0] += val; for (int i = x; i <= n; i += (i & -i)) bit[i][1] += 1; } pair<long long, long long> query(int x) { long long ret1 = 0; long long ret2 = 0; for (int i = x; i > 0; i -= (i & -i)) ret1 += bit[i][0]; for (int i = x; i > 0; i -= (i & -i)) ret2 += bit[i][1]; return {ret1, ret2}; } int main() { cin >> n; vector<long long> vel, tmp; vector<pair<long long, int> > pos; map<long long, int> mp; for (int i = 0; i < n; i++) { long long x; cin >> x; pos.push_back({x, i}); } sort(pos.begin(), pos.end()); for (int i = 0; i < n; i++) { long long x; cin >> x; vel.push_back(x); tmp.push_back(x); } sort(tmp.begin(), tmp.end()); for (int i = 0; i < n; i++) { mp[tmp[i]] = i + 1; } long long ans = 0; for (int i = 0; i < n; i++) { int posnow = pos[i].second; long long xnow = pos[i].first; int now = mp[vel[posnow]]; pair<long long, long long> q = query(now); long long val = xnow * q.second - q.first; ans += val; update(now, xnow); } 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; map<int, int> mp; set<int> st; set<int>::iterator it; pair<int, int> arr[200005]; pair<long long, long long> tree[4 * 200005]; void update(int node, int b, int e, int i, long long v) { if (b > i || e < i) return; if (b == e && b == i) { tree[node].first += v; tree[node].second++; return; } int left = 2 * node, right = left + 1, mid = (b + e) / 2; update(left, b, mid, i, v); update(right, mid + 1, e, i, v); tree[node].first = tree[left].first + tree[right].first; tree[node].second = tree[left].second + tree[right].second; } pair<long long, long long> query(int node, int b, int e, int l, int r) { if (b > r || e < l) return make_pair(0ll, 0ll); if (b >= l && e <= r) return tree[node]; int left = 2 * node, right = left + 1, mid = (b + e) / 2; pair<long long, long long> ret; pair<long long, long long> p = query(left, b, mid, l, r); pair<long long, long long> q = query(right, mid + 1, e, l, r); ret.first = p.first + q.first; ret.second = p.second + q.second; return ret; } int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &arr[i].first); for (int i = 1; i <= n; i++) { scanf("%d", &arr[i].second); st.insert(arr[i].second); } int N = 1; for (it = st.begin(); it != st.end(); it++) { mp[*it] = ++N; } for (int i = 1; i <= n; i++) arr[i].second = mp[arr[i].second]; sort(arr + 1, arr + n + 1); long long ans = 0; for (int i = 1; i <= n; i++) { pair<long long, long long> z = query(1, 1, N, 1, arr[i].second); long long p = ((long long)arr[i].first) * z.second; p -= z.first; ans += p; update(1, 1, N, arr[i].second, arr[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
//package psa.minrazdalja; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; public class minrazdalja { /** * Uvozena abstraktna funkcija za klic Paid<L, R> */ public static class Pair<L,R> { private final L left; private final R right; public Pair(L left, R right) { assert left != null; assert right != null; this.left = left; this.right = right; } public L getLeft() { return left; } public R getRight() { return right; } @Override public int hashCode() { return left.hashCode() ^ right.hashCode(); } @Override public boolean equals(Object o) { if (!(o instanceof Pair)) return false; Pair pairo = (Pair) o; return this.left.equals(pairo.getLeft()) && this.right.equals(pairo.getRight()); } } /**? * glavna funkcija racunanja: * sprejme list tipa ArrayList<Pair<Integer, Integer>>, potem st vseh elementov in seveda listo vseh elementov. * * nato izracunamo maksimalno distance, ki je sestevek vseh distanc med posameznimi elementi, kar lahko storimo, ker je sortirana tabela. * V drugem delu odstejemo od maksimalnega sestevka distanc, posamezne ki se sekajo v prihodnosti * ostane nam samo tocni sestevek minimalnih distanc */ public static BigInteger elele(ArrayList<Pair<BigInteger,BigInteger>> elementi, int s, BigInteger[] x_el) { BigInteger skupek = new BigInteger("0"); HashMap<BigInteger, Integer> pos = new HashMap<BigInteger, Integer>(); for(int i=0; i<s; ++i) { skupek = skupek.add( (x_el[i].multiply( new BigInteger(Integer.toString(i) ))).add( (x_el[i].negate()).multiply(new BigInteger(Integer.toString(s - 1 - i))))); pos.put(x_el[i], i); } for(int i=0; i<s; ++i){ skupek = skupek.subtract( (elementi.get(i).getRight()).multiply( new BigInteger( Integer.toString( (pos.get(elementi.get(i).getRight()) - i) ) ) ) ); } // //2 1 4 3 5 // //2 2 2 3 4 return skupek; } public static void main(String[] args) throws IOException { int st_el; // uporabimo bufferedreader zaradi 1000x vecje hitrosti kot pa skener BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); //System.out.println("Vnesi stevilo elementov: "); st_el = Integer.parseInt(br.readLine()); //System.out.println(st_el); BigInteger[] x_el = new BigInteger[st_el]; BigInteger[] v_el = new BigInteger[st_el]; //System.out.println("Vnesi elemente: "); String x = br.readLine(); String[] strs = x.trim().split("\\s+"); for(int i=0; i<st_el; ++i) { x_el[i] = new BigInteger(strs[i]); } //System.out.println("Vnesi tezo elementov: "); String y = br.readLine(); String[] strs2 = y.trim().split("\\s+"); for(int i=0; i<st_el; ++i) { v_el[i] = new BigInteger(strs2[i]); } br.close(); //pospravimo skupej v par oblike <integer, integer> oz <hitrost, element> ArrayList<Pair<BigInteger,BigInteger>> listt = new ArrayList<Pair<BigInteger, BigInteger>>(st_el); for(int i=0; i<st_el; i++) { Pair<BigInteger, BigInteger> pair = new Pair<BigInteger, BigInteger>(v_el[i],x_el[i]); listt.add(pair); } /*sortiramo listt od najmanjsega do najvecjega * - sortiramo prvo po hitrostih (to je glavno najbolj vazno) * - ce je hitrost ista potem pa sortiramo se glede na element * - O(n log n) */ Collections.sort(listt, new Comparator<Pair<BigInteger, BigInteger>>() { @Override public int compare(final Pair<BigInteger, BigInteger> o1, final Pair<BigInteger, BigInteger> o2) { int oo = o1.getLeft().compareTo(o2.getLeft()); if(oo == -1) { return -1; } else if(oo == 0) { int ooo = o1.getRight().compareTo(o2.getRight()); if(ooo == -1){ return -1; }else{ return 1; } } else { return 1; } } }); //sortiramo se elemente // O(n log n) Arrays.sort(x_el); //izpisemo elemente System.out.println(elele(listt,st_el,x_el)); } }
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 loc, v; } a[200010]; int n, m, b[200010]; long long c[200010], e[200010]; int cmp(node a, node b) { return a.loc < b.loc; } int lowbit(int x) { return x & -x; } void update(int pos, int delta, long long *d) { for (int i = pos; i <= n; i += lowbit(i)) d[i] += delta; } long long sum(int pos, long long *d) { long long ret = 0; for (int i = pos; i > 0; i -= lowbit(i)) ret += d[i]; return ret; } int main() { int i; long long ans = 0; scanf("%d", &n); for (i = 1; i <= n; i++) scanf("%d", &a[i].loc); for (i = 1; i <= n; i++) scanf("%d", &a[i].v), b[i] = a[i].v; sort(b + 1, b + 1 + n); m = unique(b + 1, b + 1 + n) - (b + 1); for (i = 1; i <= n; i++) a[i].v = lower_bound(b + 1, b + 1 + m, a[i].v) - b; sort(a + 1, a + 1 + n, cmp); for (i = 1; i <= n; i++) { ans += abs(sum(a[i].v, e) - sum(a[i].v, c) * a[i].loc); update(a[i].v, 1, c), update(a[i].v, a[i].loc, e); } 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; 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()); m = unique((vec).begin(), (vec).end()) - vec.begin(); for (int i = 1; i <= n; i++) { p[i].second = lower_bound(vec.begin(), vec.begin() + m, p[i].second) - vec.begin() + 1; } 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
cpp
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const long long LINF = 0x3f3f3f3f3f3f3f3fll; const int MAX = 2e5 + 10; vector<pair<int, int>> v(MAX); vector<int> spdv(MAX), compress(MAX); long long n, bit[2][MAX]; void poe(int a, long long x, long long p) { for (; p <= n; p += p & -p) bit[a][p] += x; } long long pref(int a, long long p) { long long ret = 0; for (; p; p -= p & -p) ret += bit[a][p]; return ret; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 0; i < n; i++) { cin >> v[i].first; v[i].second = i; } for (int i = 0; i < n; i++) { cin >> spdv[i]; compress[i] = spdv[i]; } sort(v.begin(), v.begin() + n); sort(compress.begin(), compress.begin() + n); map<int, int> newidx; for (int i = 0; i < n; i++) newidx[compress[i]] = i; long long ans = 0; for (int i = 0; i < n; i++) { int pos = newidx[spdv[v[i].second]] + 1; ans += v[i].first * pref(0, pos) - pref(1, pos); poe(0, 1, pos); poe(1, v[i].first, pos); } 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; long long ts[(int)8e5 + 10], tc[(int)8e5 + 10], n; pair<long long, long long> o[(int)2e5 + 10]; void update(long long i, long long v, long long t = 1, long long nl = 1, long long nr = n) { if (i < nl || nr < i) return; if (nl == nr) { ts[t] += v; tc[t] += 1; return; } long long mid = nl + nr >> 1; update(i, v, t * 2, nl, mid); update(i, v, t * 2 + 1, mid + 1, nr); ts[t] = ts[t * 2] + ts[t * 2 + 1]; tc[t] = tc[t * 2] + tc[t * 2 + 1]; } long long ans = 0; void get(long long x, long long ql, long long qr = n, long long t = 1, long long nl = 1, long long nr = n) { if (nr < ql || qr < nl) return; if (ql <= nl && nr <= qr) { ans += ts[t] - x * tc[t]; return; } long long mid = nl + nr >> 1; get(x, ql, qr, t * 2, nl, mid); get(x, ql, qr, t * 2 + 1, mid + 1, nr); } int main(void) { scanf("%lld", &n); for (long long i = 1; i <= n; i++) { scanf("%lld", &o[i].second); } for (long long i = 1; i <= n; i++) { scanf("%lld", &o[i].first); } sort(o + 1, o + 1 + n); for (long long i = 1; i <= n; i++) { o[i].first = i; swap(o[i].first, o[i].second); } sort(o + 1, o + 1 + n); for (long long i = n; 0 < i; i--) { long long idx = o[i].first, v = o[i].second; get(idx, v); update(v, idx); } 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> using namespace std; int dx[4] = {1, -1, 0, 0}; int dy[4] = {0, 0, 1, -1}; long long gcd(long long a, long long b) { for (; b; a %= b, swap(a, b)) ; return a; } const int N = 200002; pair<long long, long long> segTree[N * 4]; vector<long long> vIdx; void update(int ptr, int s, int e, int i, pair<long long, long long> val) { if (i < s || e < i) return; if (s == e) { segTree[ptr].first += val.first; segTree[ptr].second += val.second; return; } update(ptr * 2, s, (s + e) / 2, i, val); update(ptr * 2 + 1, (s + e) / 2 + 1, e, i, val); segTree[ptr].first = segTree[ptr * 2].first + segTree[ptr * 2 + 1].first; segTree[ptr].second = segTree[ptr * 2].second + segTree[ptr * 2 + 1].second; } pair<long long, long long> getVal(int ptr, int s, int e, int l, int r) { if (r < s || e < l) return {0, 0}; if (l <= s && e <= r) return segTree[ptr]; pair<long long, long long> r1 = getVal(ptr * 2, s, (s + e) / 2, l, r); pair<long long, long long> r2 = getVal(ptr * 2 + 1, (s + e) / 2 + 1, e, l, r); return {r1.first + r2.first, r1.second + r2.second}; } int main(void) { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); int n; cin >> n; vector<pair<long long, long long> > a(n); for (int i = 0; i < n; i++) cin >> a[i].first; for (int i = 0; i < n; i++) { cin >> a[i].second; vIdx.push_back(a[i].second); } sort(a.begin(), a.end()); sort(vIdx.begin(), vIdx.end()); vIdx.erase(unique(vIdx.begin(), vIdx.end()), vIdx.end()); for (int i = 0; i < n; i++) { int idx = lower_bound(vIdx.begin(), vIdx.end(), a[i].second) - vIdx.begin(); a[i].second = idx; } long long ans = 0; for (int i = 0; i < n; i++) { pair<long long, long long> res = getVal(1, 0, vIdx.size() - 1, 0, a[i].second); long long cnt = res.first, x_total = res.second; ans += a[i].first * cnt - x_total; update(1, 0, vIdx.size() - 1, a[i].second, {1, 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
cpp
#include <bits/stdc++.h> using namespace std; template <class T> using minheap = priority_queue<T, vector<T>, greater<T>>; template <typename T> void setmax(T& a, T b) { a = max(a, b); }; template <typename T> void setmin(T& a, T b) { a = min(a, b); }; template <typename T> bool in(T v, T lo, T hi) { return lo <= v && v <= hi; }; const int N = 2e5 + 10; int n; long long fena[N], fenb[N]; void add(long long* first, int i, long long delta) { for (int j = i; j < N; j += j & -j) { first[j] += delta; } } long long sum(long long* first, int i) { long long res = 0; for (int j = i; j; j -= j & -j) { res += first[j]; } return res; } struct FastCoordinateCompression { vector<int> a; void add(int x) { a.push_back(x); } void compress() { sort(a.begin(), a.end()); a.resize(unique(a.begin(), a.end()) - a.begin()); } int operator[](int x) { return lower_bound(a.begin(), a.end(), x) - a.begin() + 1; } }; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; vector<pair<int, int>> xv(n); for (auto& [x, v] : xv) { cin >> x; } for (auto& [x, v] : xv) { cin >> v; } FastCoordinateCompression compr; for (auto& [x, v] : xv) { compr.add(x); } compr.compress(); sort(xv.begin(), xv.end(), [&](pair<int, int>& a, pair<int, int>& b) { if (a.second != b.second) return a.second < b.second; return a.first < b.first; }); long long ans = 0; for (auto& [x, v] : xv) { long long num = sum(fena, compr[x]); long long tot = sum(fenb, compr[x]); ans += x * num - tot; add(fena, compr[x], 1); add(fenb, compr[x], x); } 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; long long int n, bit1[200050], bit2[200050]; vector<pair<long long int, long long int> > arr(200050); map<long long int, long long int> mp; long long int get1(int idx) { long long int ans = 0; while (idx > 0) { ans += bit1[idx]; idx -= (idx & -idx); } return ans; } long long int get2(int idx) { long long int ans = 0; while (idx > 0) { ans += bit2[idx]; idx -= (idx & -idx); } return ans; } void update1(int idx, int inc) { while (idx <= n) { bit1[idx] += inc; idx += (idx & -idx); } } void update2(int idx, int inc) { while (idx <= n) { bit2[idx] += inc; idx += (idx & -idx); } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; arr.resize(n); for (int i = 0; i < n; i++) cin >> arr[i].first; for (int i = 0; i < n; i++) cin >> arr[i].second; sort(arr.begin(), arr.end()); vector<long long int> v; for (auto a : arr) v.push_back(a.second); sort(v.begin(), v.end()); int p = 1; for (auto x : v) mp[x] = p++; long long int ans = 0; for (int i = 0; i < n; i++) { long long int cnt = get1(mp[arr[i].second]); long long int sum = get2(mp[arr[i].second]); ans += cnt * arr[i].first - sum; update1(mp[arr[i].second], 1); update2(mp[arr[i].second], arr[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
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; pair<int, int> p[N]; long long sum[N]; long long ans = 0; int n; pair<long long, long long> tree[N]; inline int lowbit(int x) { return x & (-x); } void update(int x, int v) { while (x <= n) { tree[x].first++; tree[x].second += v; x += lowbit(x); } } pair<long long, long long> ask(int x) { pair<long long, long long> res; res.first = res.second = 0; while (x) { res.first += tree[x].first; res.second += tree[x].second; x -= lowbit(x); } return res; } int x[N]; int main() { cin >> n; for (int i = 0; i < n; i++) cin >> p[i].first; for (int i = 0; i < n; i++) cin >> p[i].second, x[i] = p[i].second; sort(x, x + n); sort(p, p + n); for (int i = 0; i < n; i++) p[i].second = lower_bound(x, x + n, p[i].second) - x + 1; for (int i = 0; i < n; i++) { pair<long long, long long> pai = ask(p[i].second); ans += pai.first * p[i].first - pai.second; update(p[i].second, p[i].first); } 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; mt19937_64 mt_rnd_64(chrono::steady_clock::now().time_since_epoch().count()); long long rnd(long long l, long long r) { return (mt_rnd_64() % (r - l + 1)) + l; } using ll = long long; using ld = long double; using ull = unsigned long long; const double PI = acos(-1.0); const ll mod = 1e9 + 7; const ll INF = 1e17; ll n; unordered_map<ll, ll> q; void add(ll pos, ll val, vector<ll> &t) { while (pos < n) { t[pos] += val; pos = (pos | (pos + 1)); } } ll sum(ll pos, vector<ll> &t) { ll ans = 0; while (pos >= 0) { ans += t[pos]; pos = (pos & (pos + 1)) - 1; } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cout << setprecision(10) << fixed; cin >> n; vector<pair<ll, ll> > x(n); for (ll i = 0; i < n; ++i) { cin >> x[i].first; x[i].second = i; } vector<ll> v(n); set<ll> SET; for (ll i = 0; i < n; ++i) { cin >> v[i]; SET.insert(v[i]); } ll c = 0; for (ll i : SET) { q[i] = c; ++c; } vector<ll> t(n * 2, 0), t1(n * 2, 0); sort(begin(x), end(x)); ll ans = 0; for (ll i = 0; i < n; ++i) { ans += sum(q[v[x[i].second]], t) * x[i].first - sum(q[v[x[i].second]], t1); add(q[v[x[i].second]], 1, t); add(q[v[x[i].second]], x[i].first, t1); } 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
python3
import operator import collections from sys import stdin N = int(input()) pos = list(map(int, stdin.readline().split())) speed = list(map(int, stdin.readline().split())) A = [] for i in range(N): A.append((pos[i], speed[i])) # organized A by position from least to greatest # We want to count, at each x_i, the number of j such that x_j < x_i and # v_j <= x_i dist = {} for i in pos: dist[i] = 0 A.sort(key = operator.itemgetter(0)) # Now we do merge sort on the speeds but with comparing... # if right is chosen over left, then we record [right] += right.pos - left.pos count = [0] def merge_compare(p, r): if p < r: q = (r + p)//2 merge_compare(p, q) merge_compare(q + 1, r) merge(p, q, r) # A is POSITION, SPEED # merging two sorted subsets A[p:q + 1], A[q + 1:r + 1] into a sorted # subset A[p:r + 1] def merge(p, q, r): n_1 = q - p + 1 n_2 = r - q temp = [] L = A[p:q + 1] R = A[q + 1:r + 1] i = 0 j = 0 # calculatin the sum of all the positions in R sum_pos_right = 0 for e, f in R: sum_pos_right += e while i < n_1 and j < n_2: if L[i][1] <= R[j][1]: temp.append(L[i]) count[0] += sum_pos_right - L[i][0]*(n_2 - j) i += 1 else: temp.append(R[j]) sum_pos_right -= R[j][0] j += 1 if i == n_1: temp += R[j:] else: temp += L[i:] A[p:r + 1] = temp merge_compare(0, N - 1) print (count[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.BufferedOutputStream; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.HashMap; import java.util.StringTokenizer; public class Round624F { public static class node{ int pos; int neg; long negsum; long possum; public node(int pos, int neg, long possum, long negsum) { this.pos = pos; this.neg = neg; this.negsum = negsum; this.possum = possum; } } public static node merge(node l, node r) { return new node(l.pos + r.pos, l.neg + r.neg, l.possum + r.possum, l.negsum + r.negsum); } public static void Update(node[] tree, int start, int end, int index, int i, int type, long speed) { if(start > end || i < start || i > end) { return; } if(start == end) { tree[index] = new node(type == 1? 1:0, type == -1? 1:0, type == 1 ? speed : 0, type == -1 ? speed : 0); return; } int mid = (start + end)>>1; Update(tree, start, mid, index<<1, i, type, speed); Update(tree, mid+1, end, index<<1|1, i, type, speed); tree[index] = merge(tree[index<<1], tree[index<<1|1]); } public static node Query(node[] tree, int start, int end, int index, int i, int j) { if(start > end || j < start || i > end) { return new node(0,0,0,0); } if(i <= start && j >= end) { return tree[index]; } int mid = (start + end)>>1; node left = Query(tree, start, mid, index<<1, i, j); node right = Query(tree, mid + 1, end, index<<1|1, i , j); return merge(left, right); } public static class Pair implements Comparable<Pair>{ int x; int v; public Pair(int x, int v) { this.x = x; this.v = v; } public int compareTo(Pair x) { return this.x - x.x; } } public static class Triple{ int start; int end; int idx; public Triple(int start, int end, int idx) { this.start = start; this.end = end; this.idx = idx; } } public static void solve() { int n = s.nextInt(); Pair[] points = new Pair[n]; Pair[] reverse = new Pair[n]; int[] xs = new int[n]; for(int i = 0; i < n; i++) { xs[i] = s.nextInt(); } int[] vs = new int[n]; for(int i = 0; i < n; i++) { vs[i] = s.nextInt(); } for(int i = 0; i < n; i++) { int x = xs[i], v = vs[i]; points[i] = new Pair(x, v); reverse[i] = new Pair(Math.abs(v), Math.abs(v)==v?1:-1); } Arrays.sort(points); Arrays.sort(reverse); HashMap<Integer,Triple> helper = new HashMap<>(); for(int i = 0; i < n; i++) { if(helper.containsKey(reverse[i].x)) { helper.get(reverse[i].x).end = i; }else { helper.put(reverse[i].x, new Triple(i, i, i)); } } node[] tree = new node[4 * n + 1]; Arrays.fill(tree, new node(0,0,0,0)); long ans = 0; for(int i = 0; i < points.length; i++) { int v = points[i].v; int id = helper.get(Math.abs(v)).idx; helper.get(Math.abs(v)).idx++; int mid = helper.get(Math.abs(v)).end; int start = helper.get(Math.abs(v)).start; int end = helper.get(Math.abs(v)).end; node left = Query(tree, 0, n - 1, 1, 0, start - 1); node mid_ = Query(tree, 0, n - 1, 1 , start, end); node right = Query(tree, 0, n - 1, 1, mid + 1, n - 1); long val = points[i].x; if(Math.abs(v) == v) { ans += - left.possum + (long)left.pos * val; ans += - left.negsum + (long)left.neg * val; ans += - right.negsum + (long)right.neg * val; ans += -mid_.possum + (long)mid_.pos * val; ans += -mid_.negsum + (long)mid_.neg * val; }else { ans += - right.negsum + (long)right.neg * val; ans += - mid_.negsum + (long)mid_.neg * val; } Update(tree, 0, n - 1, 1, id, v < 0 ? -1 : 1, val); } out.println(ans); } public static void main(String[] args) { out = new PrintWriter(new BufferedOutputStream(System.out)); s = new FastReader(); solve(); out.close(); } public static FastReader s; public static PrintWriter out; public static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } double nextDouble() { return Double.parseDouble(next()); } long nextLong() { return Long.parseLong(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (Exception 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> using namespace std; long long n; pair<long long, long long> a[200001]; long long cnt[200001], sum[200001]; void update(long long x, long long val) { for (; x <= 200000; x += x & (-x)) { cnt[x]++; sum[x] += val; } } long long query(long long x, long long val) { long long ans = 0; for (; x > 0; x -= x & (-x)) ans += (cnt[x] * val - sum[x]); return ans; } int32_t main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; for (long long i = 1; i <= n; i++) cin >> a[i].first; vector<pair<long long, long long> > val; for (long long i = 1; i <= n; i++) cin >> a[i].second; sort(a + 1, a + 1 + n); for (long long i = 1; i <= n; i++) val.push_back(pair<long long, long long>(a[i].second, i)); sort(val.begin(), val.end()); long long pre = -1e9, top = 0; for (long long i = 0; i < val.size(); i++) { if (val[i].first != pre) { pre = val[i].first; top++; } a[val[i].second].second = top; } long long ans = 0; for (long long i = 1; i <= n; i++) { ans += query(a[i].second, a[i].first); update(a[i].second, 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
import java.util.*; import java.io.*; public class MovingPoints { public static void main(String[] args) { JS scan = new JS(); int n = scan.nextInt(); long[] xs = new long[n]; int[] vs = new int[n]; TreeSet<Integer> set = new TreeSet<Integer>(); HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); for(int i = 0; i < n; i++) xs[i] = scan.nextLong(); for(int i = 0; i < n; i++) { vs[i] = scan.nextInt(); set.add(vs[i]); } int curr = 0; for(int i : set) map.put(i, curr++); BIT[][] b = new BIT[2][2]; for(int i = 0; i < 2; i++) for(int j = 0; j < 2; j++) b[i][j] = new BIT(curr); Point[] pts = new Point[n]; for(int i = 0; i < n; i++) pts[i] = new Point(xs[i], map.get(vs[i])); Arrays.sort(pts); long ans = 0; for(int i = 0; i < n; i++) { long x = b[0][0].query(0, pts[i].v); // the sum long s = b[0][1].query(0, pts[i].v)*pts[i].x; // the count ans += s-x; b[0][0].update(pts[i].v, pts[i].x); b[0][1].update(pts[i].v, 1); } // for(int i = n-1; i >= 0; i--) { // long x = b[1][0].query(pts[i].v, curr-1); // long s = b[1][1].query(pts[i].v, curr-1)*pts[i].x; // the count // ans += x-s; // b[1][0].update(pts[i].v, pts[i].x); // b[1][1].update(pts[i].v, 1); // } System.out.println(ans); } static class Point implements Comparable<Point>{ long x; int v; public Point(long x, int v) { this.x = x; this.v = v; } public int compareTo(Point o) { return Long.compare(this.x, o.x); } } static class BIT { int n; long[] tree; public BIT(int n) { this.n = n; tree = new long[n + 2]; } long read(int i) { i++; long sum = 0; while (i > 0) { sum += tree[i]; i -= i & -i; } return sum; } void update(int i, long val) { i++; while (i <= n) { tree[i] += val; i += i & -i; } } long query(int left, int right) { return read(right)-read(left-1); } } static class JS{ public int BS = 1<<16; public char NC = (char)0; byte[] buf = new byte[BS]; int bId = 0, size = 0; char c = NC; double num = 1; BufferedInputStream in; public JS() { in = new BufferedInputStream(System.in, BS); } public JS(String s) throws FileNotFoundException { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } public char nextChar(){ while(bId==size) { try { size = in.read(buf); }catch(Exception e) { return NC; } if(size==-1)return NC; bId=0; } return (char)buf[bId++]; } public int nextInt() { return (int)nextLong(); } public long nextLong() { num=1; boolean neg = false; if(c==NC)c=nextChar(); for(;(c<'0' || c>'9'); c = nextChar()) { if(c=='-')neg=true; } long res = 0; for(; c>='0' && c <='9'; c=nextChar()) { res = (res<<3)+(res<<1)+c-'0'; num*=10; } return neg?-res:res; } public double nextDouble() { double cur = nextLong(); return c!='.' ? cur:cur+nextLong()/num; } public String next() { StringBuilder res = new StringBuilder(); while(c<=32)c=nextChar(); while(c>32) { res.append(c); c=nextChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while(c<=32)c=nextChar(); while(c!='\n') { res.append(c); c=nextChar(); } return res.toString(); } public boolean hasNext() { if(c>32)return true; while(true) { c=nextChar(); if(c==NC)return false; else if(c>32)return true; } } } }
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<pair<long long, long long>> t[2 * 200004]; vector<long long> tpre[2 * 200004]; long long ans = 0; int cntlo = 0, cnthi = 0; void build(int n) { for (int i = n - 1; i > 0; i--) merge(t[i << 1].begin(), t[i << 1].end(), t[i << 1 | 1].begin(), t[i << 1 | 1].end(), back_inserter(t[i])); } void buildpre(int n) { for (int i = 1, up = 2 * n; i < up; i++) { int sz = (int)t[i].size() + 1; tpre[i].resize(sz); for (int j = sz - 2; j >= 0; j--) { tpre[i][j] = tpre[i][j + 1] + t[i][j].second; } } } void queryhi(int n, int l, int r, long long v) { l += n; r += n; while (l < r) { if (l & 1) { int dist = lower_bound(t[l].begin(), t[l].end(), pair<long long, long long>(v, 1ll * INT_MIN)) - t[l].begin(); if (dist > (int)t[l].size()) dist = (int)t[l].size(); cnthi += (t[l].size() - dist); ans += tpre[l][dist]; l++; } if (r & 1) { int dist = lower_bound(t[r - 1].begin(), t[r - 1].end(), pair<long long, long long>(v, 1ll * INT_MIN)) - t[r - 1].begin(); if (dist > (int)t[r - 1].size()) dist = (int)t[r - 1].size(); cnthi += (t[r - 1].size() - dist); ans += tpre[r - 1][dist]; r--; } l >>= 1; r >>= 1; } } void querylo(int n, int l, int r, long long v) { l += n; r += n; while (l < r) { if (l & 1) { int dist = upper_bound(t[l].begin(), t[l].end(), pair<long long, long long>(v, 1ll * INT_MAX)) - t[l].begin(); if (dist > (int)t[l].size()) dist = (int)t[l].size(); cntlo += dist; ans -= (tpre[l][0] - tpre[l][dist]); l++; } if (r & 1) { int dist = upper_bound(t[r - 1].begin(), t[r - 1].end(), pair<long long, long long>(v, 1ll * INT_MAX)) - t[r - 1].begin(); if (dist > (int)t[r - 1].size()) dist = (int)t[r - 1].size(); cntlo += dist; ans -= (tpre[r - 1][0] - tpre[r - 1][dist]); r--; } l >>= 1; r >>= 1; } } int v[200004]; long long x[200004]; pair<long long, long long> xv[200004]; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%lld", &x[i]); for (int i = 0; i < n; i++) scanf("%d", &v[i]); for (int i = 0; i < n; i++) xv[i] = pair<long long, long long>(x[i], 1ll * v[i]); sort(xv, xv + n); for (int i = n, up = n << 1; i < up; i++) t[i] = vector<pair<long long, long long>>( {pair<long long, long long>(xv[i - n].second, xv[i - n].first)}); build(n); buildpre(n); long long res = 0; for (int i = 0; i < n; i++) { ans = 0; cntlo = cnthi = 0; queryhi(n, i + 1, n, xv[i].second); querylo(n, 0, i, xv[i].second); res += ans; res += (1ll * (cntlo - cnthi) * xv[i].first); } cout << res / 2; 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 int N = 2e5 + 7; int n; long long ans = 0; vector<long long> T1(N, 0), T2(N, 0); pair<int, int> a[N]; vector<int> b; void update(vector<long long>& T, int x, long long v) { for (; x <= (int)b.size(); x += (x & (-x))) T[x] += v; } long long get(const vector<long long>& T, int x) { long long res = 0; for (; x; x -= (x & (-x))) res += T[x]; return res; } int main() { ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); cin >> n; for (int i = (0); i < (n); i++) cin >> a[i].first; for (int i = (0); i < (n); i++) { cin >> a[i].second; b.push_back(a[i].second); } sort(a, a + n); sort(b.begin(), b.end()); b.resize(unique(b.begin(), b.end()) - b.begin()); int vt = lower_bound(b.begin(), b.end(), a[0].second) - b.begin() + 1; update(T1, vt, 1ll * a[0].first); update(T2, vt, 1); for (int i = (1); i < (n); i++) { vt = lower_bound(b.begin(), b.end(), a[i].second) - b.begin() + 1; ans += get(T2, vt) * a[i].first - get(T1, vt); update(T1, vt, 1ll * a[i].first); update(T2, vt, 1); } 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
cpp
#include <bits/stdc++.h> using namespace std; const long long N = 200005; long long n; pair<long long, long long> x[N]; long long ans; struct seg { long long vals[2 * N]; long long q(long long l, long long r) { long long ret = 0; for (l += n, r += n; l < r; l >>= 1, r >>= 1) { if (l & 1) ret += vals[l++]; if (r & 1) ret += vals[--r]; } return ret; } void inc(long long i, long long v) { i += n; vals[i] += v; while (i > 1) { vals[i >> 1] = vals[i] + vals[i ^ 1]; i >>= 1; } } void print() { for (long long i = 1; i < 2 * n; ++i) { cerr << vals[i] << ' '; } cerr << endl; } } sm, ct; bool compSecond(pair<long long, long long>& a, pair<long long, long long>& b) { if (a.second != b.second) return a.second < b.second; return a.first < b.first; } void doMapping() { sort(x, x + n, compSecond); for (long long i = 0; i < n; ++i) { x[i].second = i; } } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; for (long long i = 0; i < n; ++i) { cin >> x[i].first; } for (long long i = 0; i < n; ++i) { cin >> x[i].second; } doMapping(); sort(x, x + n); for (long long i = 0; i < n; ++i) { long long pt = x[i].first; long long cur = x[i].second; long long cur_sum = sm.q(0, cur), cur_ct = ct.q(0, cur); ans += pt * cur_ct - cur_sum; sm.inc(cur, pt); ct.inc(cur, 1); } 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; struct seg { vector<long long> tree; int n; seg(int n) { tree = vector<long long>(n); for (int i = 0; i < n; i++) tree[i] = 0; } void update(int node, int l, int r, int x, int y) { if (l > r) return; if (x > r || x < l) return; if (l == x && r == x) { tree[node] += y; return; } update(2 * node + 1, l, (l + r) / 2, x, y); update(2 * node + 2, (l + r) / 2 + 1, r, x, y); tree[node] = tree[2 * node + 1] + tree[2 * node + 2]; } long long get(int node, int l, int r, int x, int y) { if (x > y) return 0; if (l > r) return 0; if (x > r || y < l) return 0; if (l >= x && r <= y) return tree[node]; return get(2 * node + 1, l, (l + r) / 2, x, y) + get(2 * node + 2, (l + r) / 2 + 1, r, x, y); } }; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector<long long> x(n), v(n); for (int i = 0; i < n; i++) { cin >> x[i]; } set<int> D; vector<int> Z; for (int i = 0; i < n; i++) { cin >> v[i]; if (D.count(v[i]) == 0) Z.push_back(v[i]); D.insert(v[i]); } sort(Z.begin(), Z.end()); map<int, int> dp; for (int i = 0; i < Z.size(); i++) { dp[Z[i]] = i; } for (int i = 0; i < n; i++) { v[i] = dp[v[i]]; } vector<pair<int, int>> S; seg t1(3 * 2000000), t2(3 * 2000000); for (int i = 0; i < n; i++) { S.push_back(make_pair(x[i], v[i])); } sort(S.begin(), S.end()); long long ans = 0; for (int i = 0; i < n; i++) { long long x1 = t1.get(0, 0, 2000000 - 1, 0, S[i].second); long long x2 = t2.get(0, 0, 2000000 - 1, 0, S[i].second); ans += (S[i].first * x1 - x2); t1.update(0, 0, 2000000 - 1, S[i].second, 1); t2.update(0, 0, 2000000 - 1, S[i].second, S[i].first); } 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; const int N = 200005; int n; long long ans; pair<long long, long long> a[N]; bool cmp(pair<long long, long long> x, pair<long long, long long> y) { if (x.second != y.second) { return x.second < y.second; } else { return x.first < y.first; } } vector<long long> segmenttree[4 * N], segmenttree2[4 * N]; void build(int id, int l, int r) { if (l == r) { segmenttree[id] = {a[l].first}; segmenttree2[id] = {a[l].first}; return; } int mid = (l + r) >> 1; build(id << 1, l, mid); build(id << 1 | 1, mid + 1, r); merge(segmenttree[id << 1].begin(), segmenttree[id << 1].end(), segmenttree[id << 1 | 1].begin(), segmenttree[id << 1 | 1].end(), back_inserter(segmenttree[id])); segmenttree2[id].resize(r - l + 1); segmenttree2[id][r - l] = segmenttree[id][r - l]; for (int i = r - l - 1; i >= 0; i--) { segmenttree2[id][i] = segmenttree2[id][i + 1] + segmenttree[id][i]; } } long long get(int id, int l, int r, int u, int v, long long val) { if (v < l || r < u) { return 0; } if (u <= l && r <= v) { long long idx = lower_bound(segmenttree[id].begin(), segmenttree[id].end(), val) - segmenttree[id].begin(); if (idx == r - l + 1) { return 0; } else { return segmenttree2[id][idx] - (r - l + 1 - idx) * val; } } int mid = (l + r) >> 1; return get(id << 1, l, mid, u, v, val) + get(id << 1 | 1, mid + 1, r, u, v, val); } signed main() { ios_base::sync_with_stdio(0); cin.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; } sort(a + 1, a + 1 + n, cmp); build(1, 1, n); for (int i = 1; i < n; i++) { ans += get(1, 1, n, i + 1, n, 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
cpp
#include <bits/stdc++.h> using namespace std; vector<pair<long long, long long> > v; vector<long long> v1; long long a[200005], b[200005], c[200005], d[200005], n, m; map<long long, long long> mp; long long get(long long f[], long long pos) { long long res = 0; for (; pos >= 0; pos = (pos & (pos + 1)) - 1) res += f[pos]; return res; } void upd(long long f[], long long pos, long long val) { for (; pos < m; pos |= pos + 1) f[pos] += val; } int main() { long long i; cin >> n; for (i = 0; i < n; i++) cin >> a[i]; for (i = 0; i < n; i++) cin >> b[i]; for (i = 0; i < n; i++) { v.push_back({a[i], b[i]}); if (!mp[b[i]]) v1.push_back(b[i]); mp[b[i]] = 1; } sort(v.begin(), v.end()); sort(v1.begin(), v1.end()); m = v1.size(); long long s = 0; for (auto x : v) { long long y = lower_bound(v1.begin(), v1.end(), x.second) - v1.begin(); s += get(c, y) * x.first - get(d, y); upd(c, y, 1); upd(d, y, x.first); } cout << s; 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; template <class c> struct rge { c b, e; }; template <class c> rge<c> range(c i, c j) { return rge<c>{i, j}; } template <class c> auto dud(c* x) -> decltype(cerr << *x, 0); template <class c> char dud(...); struct debug { template <class c> debug& operator<<(const c&) { return *this; } }; const int MOD = 1000000007; const long long INFLL = 1e18; const int INF = 1e9; const int NMAX = 2e5 + 5; int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int n; array<int, 2> a[NMAX]; struct BIT { long long aib[NMAX]; void update(int pos, int val) { for (int i = pos + 1; i <= n; i += (i & (-i))) { aib[i] += val; } } long long query(int pos) { long long sum = 0; for (int i = pos; i >= 1; i -= (i & (-i))) { sum += aib[i]; } return sum; } } f0, f1; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 0; i < n; ++i) { cin >> a[i][0]; } vector<int> v; for (int i = 0; i < n; ++i) { cin >> a[i][1]; v.push_back(a[i][1]); } long long ans = 0; sort(v.begin(), v.end()); sort(a, a + n); for (int i = 0; i < n; ++i) { int pos = lower_bound(v.begin(), v.end(), a[i][1]) - v.begin(); ans += f0.query(pos + 1) * a[i][0] - f1.query(pos + 1); f0.update(pos, 1); f1.update(pos, a[i][0]); } 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
cpp
#include <bits/stdc++.h> using namespace std; int dx[8] = {0, 0, 1, 1, 1, -1, -1, -1}; int dy[8] = {1, -1, -1, 0, 1, -1, 0, 1}; long long bit[1000005], bit1[1000005]; long long sum(long long x) { long long s = 0; while (x > 0) { s += bit[x]; x -= (x & (-x)); } return s; } void update(long long x, long long val) { while (x < 1000005) { bit[x] += val; x += (x & (-x)); } } long long cnt(long long x) { long long s = 0; while (x > 0) { s += bit1[x]; x -= (x & (-x)); } return s; } void updatecnt(long long x) { while (x < 1000005) { bit1[x] += 1; x += (x & (-x)); } } signed main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); long long n; cin >> n; long long pos[n + 1], vel[n + 1]; vector<long long> v; map<long long, long long> h, h1; for (long long i = 1; i < n + 1; i++) cin >> pos[i]; for (long long i = 1; i < n + 1; i++) { cin >> vel[i]; if (!h[vel[i]]) v.push_back(vel[i]); h[vel[i]] = 1; } v.push_back(-1000000000); sort(v.begin(), v.end()); for (long long i = 1; i < v.size(); i++) { if (!h1[v[i]]) h1[v[i]] = i; } vector<pair<long long, long long> > v1; for (long long i = 1; i < n + 1; i++) { vel[i] = h1[vel[i]]; v1.push_back({pos[i], vel[i]}); } sort(v1.begin(), v1.end()); long long ans = 0; for (long long i = 0; i < v1.size(); i++) { long long s = sum(v1[i].second); long long c = cnt(v1[i].second); ans += c * v1[i].first - s; update(v1[i].second, v1[i].first); updatecnt(v1[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
java
import java.io.*; import java.math.BigInteger; import java.util.*; public class backup { public static void main(String[] args) throws IOException { InputStream inputStream = System.in; OutputStream outputStream = System.out; PrintWriter out = new PrintWriter(outputStream); InputReader in = new InputReader(inputStream); // for(int i=4;i<=4;i++) { // InputStream uinputStream = new FileInputStream("milkvisits.in"); // String f = "threesum.in"; // InputStream uinputStream = new FileInputStream(f); // InputReader in = new InputReader(uinputStream); // PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("threesum.out"))); // } Task t = new Task(); t.solve(in, out); out.close(); } static class Task{ public void solve(InputReader in, PrintWriter out) throws IOException { int n = in.nextInt(); int pos[] = in.readIntArray(n); int speed[] = in.readIntArray(n); int tmp[] = new int[n]; for(int i=0;i<n;i++) { tmp[i] = speed[i]; } ArrayUtils.sort(tmp); HashMap<Integer,Integer> mp = new HashMap<Integer,Integer>(); int c = 1; for(int i=0;i<n;i++) { if(!mp.containsKey(tmp[i])) mp.put(tmp[i], c++); } pair arr[] = new pair[n]; for(int i=0;i<n;i++) { arr[i] = new pair(pos[i],mp.get(speed[i])); } Arrays.sort(arr); long pre[] = new long[n]; for(int i=n-2;i>=0;i--) { pre[i] = pre[i+1] + (arr[i+1].pos-arr[i].pos)*(n-1-i); } int f = 200005; //f=10; BIT bit = new BIT(f); BIT cnt = new BIT(f); long ret = 0; for(int i=n-1;i>=0;i--) { ret += pre[i]; ret -= (bit.sum(arr[i].speed-1) - cnt.sum(arr[i].speed-1)*arr[i].pos); bit.add(arr[i].speed, arr[i].pos); cnt.add(arr[i].speed, 1); } out.println(ret); } class pair implements Comparable<pair>{ long pos; int speed; public pair(int a, int b) { pos=a;speed=b; } @Override public int compareTo(pair t) { if(this.pos>t.pos) return 1; else if(this.pos==t.pos) return 0; else return -1; } } class edge implements Comparable<edge>{ int f,t,len; public edge(int a, int b, int c) { f=a;t=b;len=c; } @Override public int compareTo(edge t) { return t.len-this.len; } } class sgt{ sgt lt; sgt rt; int l,r; long sum, max, min, lazy; public sgt(int L, int R, int arr[]) { l=L;r=R; if(l==r-1) { sum = max = min = arr[l]; lazy = 0; return; } lt = new sgt(l, l+r>>1, arr); rt = new sgt(l+r>>1, r, arr); pop_up(); } void pop_up() { this.sum = lt.sum + rt.sum; this.max = Math.max(lt.max, rt.max); this.min = Math.min(lt.min, rt.min); } void push_down() { if(this.lazy!=0) { lt.sum+=lazy; rt.sum+=lazy; lt.max+=lazy; lt.min+=lazy; rt.max+=lazy; rt.min+=lazy; lt.lazy+=this.lazy; rt.lazy+=this.lazy; this.lazy = 0; } } void change(int L, int R, int v) { if(R<=l||r<=L) return; if(L<=l&&r<=R) { this.max+=v; this.min+=v; this.sum+=v*(r-l); this.lazy+=v; return; } push_down(); lt.change(L, R, v); rt.change(L, R, v); pop_up(); } long query_max(int L, int R) { if(L<=l&&r<=R) return this.max; if(r<=L||R<=l) return Long.MIN_VALUE; push_down(); return Math.max(lt.query_max(L, R), rt.query_max(L, R)); } long[] query_min(int L, int R) { if(L<=l&&r<=R) return new long[] {this.min, this.l}; if(r<=L||R<=l) return new long[] {Long.MAX_VALUE, 99999999}; push_down(); long a[] = lt.query_min(L, R); long b[] = rt.query_min(L, R); if(a[0]<b[0]) return a; else if(a[0]>b[0]) return b; else return new long[] {a[0], Math.min(a[1], b[1])}; //return Math.min(lt.query_min(L, R), rt.query_min(L, R)); } long query_sum(int L, int R) { if(L<=l&&r<=R) return this.sum; if(r<=L||R<=l) return 0; push_down(); return lt.query_sum(L, R) + rt.query_sum(L, R); } } // List<List<String>> convert(String arr[][]){ // int n = arr.length; // List<List<String>> ret = new ArrayList<>(); // for(int i=0;i<n;i++) { // ArrayList<String> tmp = new ArrayList<String>(); // for(int j=0;j<arr[i].length;j++) tmp.add(arr[i][j]); // ret.add(tmp); // } // return ret; // } public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } public int GCD(int a, int b) { if (b==0) return a; return GCD(b,a%b); } public long GCD(long a, long b) { if (b==0) return a; return GCD(b,a%b); } } static class ArrayUtils { static final long seed = System.nanoTime(); static final Random rand = new Random(seed); public static void sort(int[] a) { shuffle(a); Arrays.sort(a); } public static void shuffle(int[] a) { for (int i = 0; i < a.length; i++) { int j = rand.nextInt(i + 1); int t = a[i]; a[i] = a[j]; a[j] = t; } } public static void sort(long[] a) { shuffle(a); Arrays.sort(a); } public static void shuffle(long[] a) { for (int i = 0; i < a.length; i++) { int j = rand.nextInt(i + 1); long t = a[i]; a[i] = a[j]; a[j] = t; } } } static class BIT{ long arr[]; int n; public BIT(int a) { n=a; arr = new long[n]; } long sum(int p) { long s=0; while(p>0) { s+=arr[p]; p-=p&(-p); } return s; } void add(int p, long v) { while(p<n) { arr[p]+=v; p+=p&(-p); } } } static class DSU{ int[] arr; int[] sz; public DSU(int n) { arr = new int[n]; sz = new int[n]; for(int i=0;i<n;i++) arr[i] = i; Arrays.fill(sz, 1); } public int find(int a) { if(arr[a]!=a) arr[a] = find(arr[a]); return arr[a]; } public void union(int a, int b) { int x = find(a); int y = find(b); if(x==y) return; arr[y] = x; sz[x] += sz[y]; } public int size(int x) { return sz[find(x)]; } } static class MinHeap<Key> implements Iterable<Key> { private int maxN; private int n; private int[] pq; private int[] qp; private Key[] keys; private Comparator<Key> comparator; public MinHeap(int capacity){ if (capacity < 0) throw new IllegalArgumentException(); this.maxN = capacity; n=0; pq = new int[maxN+1]; qp = new int[maxN+1]; keys = (Key[]) new Object[capacity+1]; Arrays.fill(qp, -1); } public MinHeap(int capacity, Comparator<Key> c){ if (capacity < 0) throw new IllegalArgumentException(); this.maxN = capacity; n=0; pq = new int[maxN+1]; qp = new int[maxN+1]; keys = (Key[]) new Object[capacity+1]; Arrays.fill(qp, -1); comparator = c; } public boolean isEmpty() { return n==0; } public int size() { return n; } public boolean contains(int i) { if (i < 0 || i >= maxN) throw new IllegalArgumentException(); return qp[i] != -1; } public int peekIdx() { if (n == 0) throw new NoSuchElementException("Priority queue underflow"); return pq[1]; } public Key peek(){ if(isEmpty()) throw new NoSuchElementException("Priority queue underflow"); return keys[pq[1]]; } public int poll(){ if(isEmpty()) throw new NoSuchElementException("Priority queue underflow"); int min = pq[1]; exch(1,n--); down(1); assert min==pq[n+1]; qp[min] = -1; keys[min] = null; pq[n+1] = -1; return min; } public void update(int i, Key key) { if (i < 0 || i >= maxN) throw new IllegalArgumentException(); if (!contains(i)) { this.add(i, key); }else { keys[i] = key; up(qp[i]); down(qp[i]); } } private void add(int i, Key x){ if (i < 0 || i >= maxN) throw new IllegalArgumentException(); if (contains(i)) throw new IllegalArgumentException("index is already in the priority queue"); n++; qp[i] = n; pq[n] = i; keys[i] = x; up(n); } private void up(int k){ while(k>1&&less(k,k/2)){ exch(k,k/2); k/=2; } } private void down(int k){ while(2*k<=n){ int j=2*k; if(j<n&&less(j+1,j)) j++; if(less(k,j)) break; exch(k,j); k=j; } } public boolean less(int i, int j){ if (comparator == null) { return ((Comparable<Key>) keys[pq[i]]).compareTo(keys[pq[j]]) < 0; } else { return comparator.compare(keys[pq[i]], keys[pq[j]]) < 0; } } public void exch(int i, int j){ int swap = pq[i]; pq[i] = pq[j]; pq[j] = swap; qp[pq[i]] = i; qp[pq[j]] = j; } @Override public Iterator<Key> iterator() { // TODO Auto-generated method stub return null; } } private static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int zcurChar; private int znumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (znumChars == -1) throw new InputMismatchException(); if (zcurChar >= znumChars) { zcurChar = 0; try { znumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (znumChars <= 0) return -1; } return buf[zcurChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return nextString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } public int[] readIntArray(int n) { int[] ret = new int[n]; for (int i = 0; i < n; i++) { ret[i] = nextInt(); } return ret; } } static class Dumper { static void print_int_arr(int[] arr) { for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println(); System.out.println("---------------------"); } static void print_char_arr(char[] arr) { for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println(); System.out.println("---------------------"); } static void print_double_arr(double[] arr) { for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println(); System.out.println("---------------------"); } static void print_2d_arr(int[][] arr, int x, int y) { for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) { System.out.print(arr[i][j] + " "); } System.out.println(); } System.out.println(); System.out.println("---------------------"); } static void print_2d_arr(boolean[][] arr, int x, int y) { for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) { System.out.print(arr[i][j] + " "); } System.out.println(); } System.out.println(); System.out.println("---------------------"); } static void print(Object o) { System.out.println(o.toString()); } static void getc() { System.out.println("here"); } } }
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
python2
import sys from collections import defaultdict range = xrange input = raw_input class segtree: def __init__(self, n): m = 1 while m < n: m *= 2 self.m = m self.data1 = [0.0]*(m + m) self.data2 = [0]*(m + m) def add(self, i, val): i += self.m while i: self.data1[i] += val self.data2[i] += 1 i >>= 1 def summer(self, l, r): l += self.m r += self.m s1 = 0.0 s2 = 0 while l < r: if l & 1: s1 += self.data1[l] s2 += self.data2[l] l += 1 if r & 1: r -= 1 s1 += self.data1[r] s2 += self.data2[r] l >>= 1 r >>= 1 return s1,s2 jj = -1 def map_fun(): global jj jj += 1 return jj inp = [int(x) for x in sys.stdin.read().split()]; ii = 0 n = inp[ii]; ii += 1 X = inp[ii: ii + n]; ii += n V = inp[ii: ii + n]; ii += n order = sorted(range(n), key = lambda i: (V[i], X[i])) mapper = defaultdict(map_fun) for x in sorted(X): mapper[x] seg = segtree(len(mapper)) total = 0 for i in order: x = X[i] j = mapper[x] s1, s2 = seg.summer(0,j) total += int(s2 * x - s1) seg.add(j, x) print int(total)
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; bool test_cases_exist = false; class FenwickTree { public: vector<long long int> tree; long long int n; FenwickTree(long long int n) { this->n = n; tree.resize(n + 5); } void update(long long int ind, long long int delta) { while (ind <= n) { tree[ind] += delta; ind += ind & -ind; } } long long int query(long long int ind) { long long int res = 0; while (ind >= 1) { res += tree[ind]; ind -= ind & -ind; } return res; } }; void solve() { long long int n; cin >> n; vector<pair<long long int, long long int>> pnt(n); for (auto &z : pnt) cin >> z.first; for (auto &z : pnt) cin >> z.second; vector<long long int> speed; for (auto &z : pnt) speed.push_back(z.second); sort(pnt.begin(), pnt.end()); sort(speed.begin(), speed.end()); long long int ans = 0; vector<FenwickTree> ft(2, {n}); for (auto z : pnt) { int pos = lower_bound(speed.begin(), speed.end(), z.second) - speed.begin(); ans += ft[0].query(pos + 1) * z.first; ans -= ft[1].query(pos + 1); ft[0].update(pos + 1, 1); ft[1].update(pos + 1, z.first); } cout << ans << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int t = 1; if (test_cases_exist) cin >> t; while (t--) 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; int b[200005], m; long long c[200005][2]; struct node { int x, v; bool operator<(const node t) const { return x < t.x; } } a[200005]; int lsh(int x) { return lower_bound(b + 1, b + 1 + m, x) - b; } long long query(int x, int k) { long long ret = 0; for (int i = x; i > 0; i -= i & -i) ret += c[i][k]; return ret; } void update(int x, int y) { for (int i = x; i <= m; i += i & -i) { c[i][0]++; c[i][1] += y; } } int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i].x); for (int i = 1; i <= n; i++) { scanf("%d", &a[i].v); b[i] = a[i].v; } sort(a + 1, a + 1 + n); sort(b + 1, b + 1 + n); m = unique(b + 1, b + 1 + n) - b - 1; long long ans = 0; for (int i = 1; i <= n; i++) { int x = lsh(a[i].v); ans += query(x, 0) * a[i].x - query(x, 1); update(x, a[i].x); } printf("%lld", 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 int maxn = 4e5 + 1000; pair<long long, long long> p[maxn]; vector<long long> v; int getpos(long long V) { return lower_bound(v.begin(), v.end(), V) - v.begin() + 1; } long long C1[2][maxn]; long long C2[2][maxn]; void add(int x, long long K, long long C[][maxn], int flag) { while (0 < x && x < maxn) { C[0][x] += 1; C[1][x] += K; if (flag == -1) x += (x) & (-x); else x -= (x) & (-x); } } pair<long long, long long> getsum(int x, long long C[][maxn], int flag) { long long sum = 0; int num = 0; while (0 < x && x < maxn) { num += C[0][x]; sum += C[1][x]; if (flag == -1) x -= (x) & (-x); else x += (x) & (-x); } return pair<long long, long long>(num, sum); } int main() { ios::sync_with_stdio(false); int N; cin >> N; for (int i = 1; i <= N; i++) cin >> p[i].first; for (int i = 1; i <= N; i++) cin >> p[i].second, v.push_back(abs(p[i].second)); sort(p + 1, p + 1 + N); sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); long long ans = 0; long long cnt2 = 0; long long cnts2 = 0; for (int i = 1; i <= N; i++) { if (p[i].second >= 0) { pair<long long, long long> s = getsum(getpos(p[i].second), C1, -1); ans += p[i].first * s.first - s.second; add(getpos(p[i].second), p[i].first, C1, -1); ans += p[i].first * cnt2 - cnts2; } else { pair<long long, long long> s = getsum(getpos(-p[i].second), C2, 1); ans += p[i].first * s.first - s.second; add(getpos(-p[i].second), p[i].first, C2, 1); cnt2 += 1; cnts2 += p[i].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
cpp
#include <bits/stdc++.h> using namespace std; struct Scanner { bool hasNext = 1; bool hasRead = 1; int nextInt() { hasRead = 0; int res = 0; char flag = 1, ch = getchar(); while (ch != EOF && !isdigit(ch)) { hasRead = 1; flag = (ch == '-') ? -flag : flag; ch = getchar(); } while (ch != EOF && isdigit(ch)) { hasRead = 1; res = res * 10 + (ch - '0'); ch = getchar(); } if (ch == EOF) hasNext = 0; return res * flag; } long long nextLL() { hasRead = 0; long long res = 0; char flag = 1, ch = getchar(); while (ch != EOF && !isdigit(ch)) { hasRead = 1; flag = (ch == '-') ? -flag : flag; ch = getchar(); } while (ch != EOF && isdigit(ch)) { hasRead = 1; res = res * 10 + (ch - '0'); ch = getchar(); } if (ch == EOF) hasNext = 0; return res * flag; } char nextChar() { hasRead = 0; char ch = getchar(); while (ch != EOF && isspace(ch)) { hasRead = 1; ch = getchar(); } if (ch == EOF) hasNext = 0; return ch; } int nextString(char *str) { hasRead = 0; int len = 0; char ch = getchar(); while (ch != EOF && isspace(ch)) { hasRead = 1; ch = getchar(); } while (ch != EOF && !isspace(ch)) { hasRead = 1; str[++len] = ch; ch = getchar(); } str[len + 1] = 0; if (ch == EOF) hasNext = 0; return len; } } sc; long long rd() { long long x = sc.nextLL(); return x; } void rd(int &x) { x = sc.nextInt(); } void rd(long long &x) { x = sc.nextLL(); } void rd(char &x) { x = sc.nextChar(); } void rd(char *x) { sc.nextString(x); } template <typename T1, typename T2> void rd(pair<T1, T2> &x) { rd(x.first); rd(x.second); } template <typename T> void rd(T *x, int n) { for (int i = 1; i <= n; ++i) rd(x[i]); } void printInt(int x) { if (x < 0) { putchar('-'); x = -x; } if (x >= 10) printInt(x / 10); putchar('0' + x % 10); } void printLL(long long x) { if (x < 0) { putchar('-'); x = -x; } if (x >= 10) printLL(x / 10); putchar('0' + x % 10); } void pr(int x, char ch = '\n') { printInt(x); putchar(ch); } void pr(long long x, char ch = '\n') { printLL(x); putchar(ch); } template <typename T1, typename T2> void pr(pair<T1, T2> x, char ch = '\n') { pr(x.first, ' '); pr(x.second, ch); } template <typename T> void pr(T *x, int n) { for (int i = 1; i <= n; ++i) pr(x[i], " \n"[i == n]); } template <typename T> void pr(vector<T> &x) { int n = x.size(); for (int i = 1; i <= n; ++i) pr(x[i - 1], " \n"[i == n]); } template <typename T> void cmin(T &x, T y) { if (y < x) x = y; } template <typename T> void cmax(T &x, T y) { if (y > x) x = y; } const int INF = 0x3f3f3f3f; const long long LINF = 0x3f3f3f3f3f3f3f3fLL; const double PI = acos(-1.0); const double EPS = 1e-9; const int MOD = 1000000007; int n; int x[200005]; int v[200005]; pair<int, int> p[200005]; struct Treap { static const int MAXN = 200000 + 10; int top, root, ch[MAXN][2], rnd[MAXN]; pair<int, long long> val[MAXN]; long long sum[MAXN]; int NewNode(pair<int, long long> v) { int id = ++top; ch[id][0] = 0, ch[id][1] = 0, rnd[id] = rand(); val[id] = v; sum[id] = 1LL * v.second; return id; } void PushUp(int id) { sum[id] = sum[ch[id][0]] + sum[ch[id][1]] + 1LL * val[id].second; } void Rotate(int &id, int d) { int tmp = ch[id][d ^ 1]; ch[id][d ^ 1] = ch[tmp][d]; ch[tmp][d] = id, id = tmp; PushUp(ch[id][d]), PushUp(id); } void InsertHelp(int &id, pair<int, long long> v) { if (!id) { id = NewNode(v); return; } if (v.first == val[id].first) val[id].second += v.second; else { int d = val[id].first > v.first ? 0 : 1; InsertHelp(ch[id][d], v); if (rnd[id] < rnd[ch[id][d]]) Rotate(id, d ^ 1); } PushUp(id); } void Init() { top = 0, root = 0; } void Insert(pair<int, int> v) { InsertHelp(root, v); } long long GetSumValue(int v) { int id = root; long long res = 0; while (id) { if (val[id].first > v) id = ch[id][0]; else if (val[id].first == v) { res += sum[ch[id][0]] + 1LL * val[id].second; break; } else { res += sum[ch[id][0]] + 1LL * val[id].second; id = ch[id][1]; } } return res; } } treap1, treap2; struct Solver { void InitOnce() {} void Read() { rd(n); rd(x, n); rd(v, n); for (int i = 1; i <= n; ++i) { p[i].first = x[i]; p[i].second = v[i]; } } void Solve() { treap1.Init(); treap2.Init(); sort(p + 1, p + 1 + n); long long sum = 0; for (int i = 1; i <= n; ++i) { sum += treap2.GetSumValue(p[i].second) * p[i].first; sum -= treap1.GetSumValue(p[i].second); treap1.Insert({p[i].second, p[i].first}); treap2.Insert({p[i].second, 1}); } pr(sum); } } solver; int main() { solver.InitOnce(); int t = 1; t = INF; while (t--) { solver.Read(); if (!sc.hasRead) break; solver.Solve(); if (!sc.hasNext) break; } 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; template <typename T> using pr = pair<T, T>; const int mod = 1e9 + 7; struct SegTree { vector<pr<long long>> segtree; long long N; SegTree(long long n) { N = 1; while (N < n) N <<= 1; segtree = vector<pr<long long>>(2 * N); } void modify(long long k, long long x) { k += N; segtree[k].second += x; segtree[k].first += 1; for (k /= 2; k > 0; k /= 2) { segtree[k].first = segtree[k * 2].first + segtree[k * 2 + 1].first; segtree[k].second = segtree[k * 2].second + segtree[k * 2 + 1].second; } } pr<long long> query(long long b) { long long a = 0; long long ansx = 0, ansy = 0; for (a += N, b += N; a <= b; a /= 2, b /= 2) { if (a % 2 == 1) ansx += segtree[a].first, ansy += segtree[a++].second; if (b % 2 == 0) ansx += segtree[b].first, ansy += segtree[b--].second; } return make_pair(ansx, ansy); } }; void solve() { long long n; cin >> n; vector<pr<long long>> xv(n); vector<long long> v(n); for (signed i = 0; i < n; ++i) cin >> xv[i].first; for (signed i = 0; i < n; ++i) cin >> xv[i].second, v[i] = xv[i].second; sort((xv).begin(), (xv).end()); sort((v).begin(), (v).end()); map<long long, int> cmp; for (signed i = 0; i < n; ++i) cmp[v[i]] = i; long long ans = 0; SegTree x(n); x.modify(cmp[xv[0].second], xv[0].first); for (signed i = 1; i < n; ++i) { pr<long long> qa = x.query(cmp[xv[i].second]); ans += qa.first * xv[i].first - qa.second; x.modify(cmp[xv[i].second], xv[i].first); } cout << ans << endl; } signed main() { signed t = 1; while (t--) 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; template <typename T> struct BIT { int n; vector<T> bit; BIT(int n_ = 0) : n(n_), bit(n_ + 1) {} T sum(int i) { T ans = 0; for (; i > 0; i -= i & -i) ans += bit[i]; return ans; } void add(int i, T a) { if (i == 0) return; for (; i <= n; i += i & -i) bit[i] += a; } int lower_bound(T k) { if (k <= 0) return 0; int ret = 0, i = 1; while ((i << 1) <= n) i <<= 1; for (; i; i >>= 1) if (ret + i <= n && bit[ret + i] < k) k -= bit[ret += i]; return ret + 1; } }; int N; int main() { scanf("%d", &N); vector<int> X(N); vector<pair<int, int> > vs(N); for (int i = 0; i < N; i++) { scanf("%d", &X[i]); } for (int i = 0; i < N; i++) { scanf("%d", &vs[i].first); vs[i].second = X[i]; } sort(X.begin(), X.end()); sort(vs.begin(), vs.end()); reverse(vs.begin(), vs.end()); BIT<long long> P(N); BIT<int> cnt(N); long long ans = 0; for (pair<int, int> p : vs) { int v = p.first, x = p.second; int id = lower_bound(X.begin(), X.end(), x) - X.begin() + 1; ans += P.sum(N) - P.sum(id) - (long long)x * (cnt.sum(N) - cnt.sum(id)); cnt.add(id, 1); P.add(id, x); } printf("%lld\n", ans); }