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
python3
def main(): import sys input = sys.stdin.readline class Bit: def __init__(self, n): self.size = n self.tree = [0] * (n + 1) def sum(self, i): s = 0 while i > 0: s += self.tree[i] i -= i & -i return s def add(self, i, x): while i <= self.size: self.tree[i] += x i += i & -i N = int(input()) X = list(map(int, input().split())) V = list(map(int, input().split())) info = [(x, v) for x, v in zip(X, V)] info.sort(key=lambda p: p[0]) info.sort(key=lambda p: p[1]) X.sort() x2i = {x: i+1 for i, x in enumerate(X)} bit_x = Bit(N+1) bit_cnt = Bit(N+1) ans = 0 for x, _ in info: i = x2i[x] ans += bit_cnt.sum(i) * x - bit_x.sum(i) bit_x.add(i, x) bit_cnt.add(i, 1) 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
python3
import bisect class ft: def __init__(self,n): self.a = [0]*(200000) self.n = n def qry(self,r): ret = 0 while r>=0: ret+=self.a[r] r=(r&(r+1))-1 return ret def upd(self,i,v): while i<self.n: self.a[i]+=v i=(i|(i+1)) n = int(input()) x = list(map(int,input().split())) v = list(map(int,input().split())) f0 = ft(n) f1 = ft(n) a1 = [[] for _ in range(n)] for i in range(n): a1[i].append(x[i]) a1[i].append(v[i]) a1.sort() v.sort() ans = 0 for i in range(n): p = bisect.bisect_left(v,a1[i][1]) ans+=(f0.qry(p)*a1[i][0])-(f1.qry(p)); f0.upd(p,1) f1.upd(p,a1[i][0]) print(ans)
1311_F. Moving Points
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i. Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0. Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points. The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct. The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point. Output Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Examples Input 3 1 3 2 -100 2 3 Output 3 Input 5 2 1 4 3 5 2 2 2 3 4 Output 19 Input 2 2 1 -3 0 Output 0
{ "input": [ "3\n1 3 2\n-100 2 3\n", "2\n2 1\n-3 0\n", "5\n2 1 4 3 5\n2 2 2 3 4\n" ], "output": [ "3\n", "0\n", "19\n" ] }
{ "input": [], "output": [] }
CORRECT
java
/*package whatever //do not write package name here */ import java.io.*; import java.util.*; public class GFG { static StringBuffer str=new StringBuffer(""); static int n; static Long v[]; static int lower_bound(long val) { int l=0,r=v.length-1; int ans=-1; while(l<=r) { int mid=(l+r)>>>1; if(v[mid]>=val) { ans=mid; r=mid-1; } else l=mid+1; } return ans; } static class BIT { long bit[]; int sz; BIT(int sz) { this.sz=sz; bit=new long[sz+1]; Arrays.fill(bit,0); } void update(int inx,long val) { inx++; while(inx<=sz) { bit[inx]+=val; inx+=(inx&(-inx)); } } long getSum(int inx) { inx++; long res=0; while(inx>0) { res+=bit[inx]; inx-=(inx&(-inx)); } return res; } } static class Pair implements Comparable<Pair>{ long x,v; Pair(long x,long v) { this.x=x; this.v=v; } public int compareTo(Pair p) { return (int)(this.x-p.x); } } static Pair p[]; static void solve() { Arrays.sort(p); Arrays.sort(v); long ans=0; BIT f1=new BIT(n); BIT f2=new BIT(n); for(int i=0;i<n;i++) { int inx=lower_bound(p[i].v); ans+=f1.getSum(inx)*p[i].x-f2.getSum(inx); f1.update(inx,1); f2.update(inx,p[i].x); } System.out.println(ans); } public static void main (String[] args) throws Exception { BufferedReader bf=new BufferedReader(new InputStreamReader(System.in)); n=Integer.parseInt(bf.readLine()); String s[]=bf.readLine().trim().split("\\s+"); p=new Pair[n]; v=new Long[n]; String s1[]=bf.readLine().trim().split("\\s+"); for(int i=0;i<n;i++) { p[i]=new Pair(Long.parseLong(s[i]),Long.parseLong(s1[i])); v[i]=Long.parseLong(s1[i]); } solve(); } }
1311_F. Moving Points
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i. Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0. Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points. The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct. The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point. Output Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Examples Input 3 1 3 2 -100 2 3 Output 3 Input 5 2 1 4 3 5 2 2 2 3 4 Output 19 Input 2 2 1 -3 0 Output 0
{ "input": [ "3\n1 3 2\n-100 2 3\n", "2\n2 1\n-3 0\n", "5\n2 1 4 3 5\n2 2 2 3 4\n" ], "output": [ "3\n", "0\n", "19\n" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; struct FTDown { long long ft[N + 5]; FTDown() { for (int i = 1; i <= N; i++) ft[i] = 0; } void update(int p, long long v) { for (; p <= N; p += (p & -p)) ft[p] += v; } long long get(int p) { long long sum = 0; for (; p; p -= (p & -p)) sum += ft[p]; return sum; } }; int n, b[N]; pair<int, int> a[N]; FTDown sumXDown, cntDown; int 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; b[i] = a[i].second; } b[n + 1] = 0; sort(b + 1, b + n + 2); int m = 1; for (int i = 2; i <= n + 1; i++) if (b[i] != b[m]) b[++m] = b[i]; for (int i = 1; i <= n; i++) a[i].second = lower_bound(b + 1, b + m + 1, a[i].second) - b; sort(a + 1, a + n + 1); int ind0 = lower_bound(b + 1, b + m + 1, 0) - b; long long ans = 0; for (int i = 1; i <= n; i++) { long long cnt = cntDown.get(a[i].second); long long sumX = sumXDown.get(a[i].second); ans += cnt * a[i].first - sumX; cntDown.update(a[i].second, 1); sumXDown.update(a[i].second, a[i].first); } cout << ans; return 0; }
1311_F. Moving Points
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i. Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0. Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points. The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct. The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point. Output Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Examples Input 3 1 3 2 -100 2 3 Output 3 Input 5 2 1 4 3 5 2 2 2 3 4 Output 19 Input 2 2 1 -3 0 Output 0
{ "input": [ "3\n1 3 2\n-100 2 3\n", "2\n2 1\n-3 0\n", "5\n2 1 4 3 5\n2 2 2 3 4\n" ], "output": [ "3\n", "0\n", "19\n" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long N = 2e5 + 1000; const long long M = 1e5 + 10; const long long inf = 1e9 + 7; const long long Mod = 1e9 + 7; const double eps = 1e-6; int T; pair<int, int> a[N]; int b[N]; int n; pair<long long, long long> sg[4 * N]; void build(int k, int l, int r) { if (l == r) { sg[k].first = 0; sg[k].second = 0; return; } build(2 * k, l, (l + r) / 2); build(2 * k + 1, (l + r) / 2 + 1, r); } void up(int k) { sg[k].first = sg[2 * k].first + sg[2 * k + 1].first; sg[k].second = sg[2 * k].second + sg[2 * k + 1].second; } void modify(int k, int l, int r, int pos, int val) { if (l == r) { sg[k].first += val; sg[k].second++; return; } if (pos <= (l + r) / 2) modify(2 * k, l, (l + r) / 2, pos, val); else if (pos > (l + r) / 2) modify(2 * k + 1, (l + r) / 2 + 1, r, pos, val); up(k); } pair<long long, long long> query(int k, int l, int r, int L, int R) { if (L <= l && r <= R) return sg[k]; pair<long long, long long> res{0, 0}; pair<long long, long long> cur; if (L <= (l + r) / 2) { cur = query(2 * k, l, (l + r) / 2, L, R); res.first += cur.first; res.second += cur.second; }; if (R > (l + r) / 2) { cur = query(2 * k + 1, (l + r) / 2 + 1, r, L, R); res.first += cur.first; res.second += cur.second; } return res; } int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", &a[i].first); } for (int i = 1; i <= n; ++i) { scanf("%d", &a[i].second); b[i] = a[i].second; } sort(a + 1, a + n + 1); sort(b + 1, b + n + 1); int len = unique(b + 1, b + n + 1) - b - 1; build(1, 1, len); long long ans = 0; for (int i = 1; i <= n; ++i) { int id = lower_bound(b + 1, b + len + 1, a[i].second) - b; pair<long long, long long> cur = query(1, 1, n, 1, id); ans -= cur.first - cur.second * a[i].first; modify(1, 1, n, id, a[i].first); } 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; 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 = 0; i < n; i++) { long long r = o[i].second, idx = o[i].first; long long s = 0, c = 0; while (0 < r) { s += ts[r]; c += tc[r]; r -= (r & -r); } ans += idx * c - s; long long 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; int n, T; long long cnt[200010], sum[200010]; pair<int, int> a[200010]; long long getval(long long f[], int pos) { pos++; long long ans = 0; for (int i = pos; i > 0; i -= (i & -i)) ans += f[i]; return ans; } void upd(long long f[], int pos, int val) { pos++; for (int i = pos; i <= n; i += (i & -i)) f[i] += val; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; for (int i = 0; i < n; i++) cin >> a[i].first; for (int i = 0; i < n; i++) cin >> a[i].second; sort(a, a + n); vector<int> compress_v; for (int i = 0; i < n; i++) compress_v.push_back(a[i].second); sort(compress_v.begin(), compress_v.end()); compress_v.resize(unique(compress_v.begin(), compress_v.end()) - compress_v.begin()); long long ans = 0; for (int i = 0; i < n; i++) { int pos = lower_bound(compress_v.begin(), compress_v.end(), a[i].second) - compress_v.begin(); ans += getval(cnt, pos) * 1ll * a[i].first - getval(sum, pos); upd(cnt, pos, 1); upd(sum, pos, a[i].first); } cout << ans; }
1311_F. Moving Points
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i. Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0. Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points. The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct. The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point. Output Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Examples Input 3 1 3 2 -100 2 3 Output 3 Input 5 2 1 4 3 5 2 2 2 3 4 Output 19 Input 2 2 1 -3 0 Output 0
{ "input": [ "3\n1 3 2\n-100 2 3\n", "2\n2 1\n-3 0\n", "5\n2 1 4 3 5\n2 2 2 3 4\n" ], "output": [ "3\n", "0\n", "19\n" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n; const int maxn = 2e5 + 10; inline int lowbit(int x) { return x & (-x); } struct point { int x, v; } p[maxn]; int v[maxn]; long long c1[maxn], c2[maxn]; bool comp(point a, point b) { return a.x < b.x; } long long getnum(long long c[], int i) { long long res = 0LL; while (i > 0) { res += c[i]; i -= lowbit(i); } return res; } void update(int i, int value) { while (i <= n) { c1[i]++; c2[i] += value; i += lowbit(i); } } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &p[i].x); for (int i = 1; i <= n; i++) { scanf("%d", &p[i].v); v[i] = p[i].v; } sort(p + 1, p + n + 1, comp); sort(v + 1, v + n + 1); long long ans = 0; for (int i = 1; i <= n; i++) { int pos = lower_bound(v + 1, v + 1 + n, p[i].v) - v; ans += getnum(c1, pos) * p[i].x - getnum(c2, pos); update(pos, p[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; using namespace std; long long st[800001], st2[800001]; vector<long long> vx[200001], idxs[200001]; long long getMid(long long s, long long e) { return s + (e - s) / 2; } long long SumUtil(long long* st, long long ss, long long se, long long l, long long r, long long node) { if (l <= ss && r >= se) return st[node]; if (se < l || ss > r) return 0ll; long long mid = getMid(ss, se); return SumUtil(st, ss, mid, l, r, 2 * node + 1) + SumUtil(st, mid + 1, se, l, r, 2 * node + 2); } void updateValue(long long* st, long long ss, long long se, long long index, long long value, long long node) { if (ss == se) { st[node] += value; } else { long long mid = getMid(ss, se); if (index >= ss && index <= mid) updateValue(st, ss, mid, index, value, 2 * node + 1); else updateValue(st, mid + 1, se, index, value, 2 * node + 2); st[node] = st[2 * node + 1] + st[2 * node + 2]; } return; } long long getSum(long long* st, long long n, long long l, long long r) { if (l < 0 || r > n - 1 || l > r) { return 0; } return SumUtil(st, 0, n - 1, l, r, 0); } void solve() { set<long long> stt; stt.clear(); long long n, i, j, k, x[200001], v[200001]; for (i = 0; i < 800001; i++) st[i] = st2[i] = 0; cin >> n; for (i = 0; i < n; i++) { cin >> x[i]; } for (i = 0; i < n; i++) { cin >> v[i]; stt.insert(v[i]); } vector<long long> temp_vec; temp_vec.clear(); for (auto xx : stt) temp_vec.push_back(xx); long long velos = temp_vec.size(); for (i = 0; i < velos; i++) { vx[i].clear(); idxs[i].clear(); } for (i = 0; i < n; i++) { long long sta = 0, ed = velos - 1, md; while (sta <= ed) { md = sta + (ed - sta) / 2; if (temp_vec[md] == v[i]) break; if (temp_vec[md] < v[i]) sta = md + 1; else ed = md - 1; } vx[md].push_back(x[i]); } for (i = 0; i < velos; i++) sort(vx[i].begin(), vx[i].end()); vector<pair<long long, long long> > temp_vec_2; temp_vec_2.clear(); for (i = 0; i < velos; i++) { for (auto xx : vx[i]) { temp_vec_2.push_back({xx, i}); } } sort(temp_vec_2.begin(), temp_vec_2.end()); i = 0; for (auto xx : temp_vec_2) { idxs[xx.second].push_back(i); i++; } long long ans = 0; for (i = 0; i < velos; i++) { for (auto xx : vx[i]) { long long sta = 0, ed = temp_vec_2.size(), md; ed--; while (sta <= ed) { md = ed + (sta - ed) / 2; if (temp_vec_2[md].first == xx) break; if (temp_vec_2[md].first < xx) sta = md + 1; else ed = md - 1; } ans += (getSum(st2, n, 0, md)) * xx - getSum(st, n, 0, md); } for (auto xx : idxs[i]) { updateValue(st, 0, n - 1, xx, temp_vec_2[xx].first, 0); updateValue(st2, 0, n - 1, xx, 1, 0); } } for (i = 0; i < velos; i++) { long long p = 0, m = vx[i].size(); m--; for (auto xx : vx[i]) { ans += (p - m) * xx; p++; m--; } } cout << ans << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.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
cpp
#include <bits/stdc++.h> using namespace std; template <class Ch, class Tr, class Container> basic_ostream<Ch, Tr>& operator<<(basic_ostream<Ch, Tr>& os, Container const& x) { os << "{ "; for (auto& y : x) os << y << " "; return os << "}"; } template <class X, class Y> ostream& operator<<(ostream& os, pair<X, Y> const& p) { return os << "[ " << p.first << ", " << p.second << "]"; } typedef struct { long long pos, v; } Point; long long P[200005], V[200005], sum = 0, maxNeg = 0; long long l = 0, r = 100000000; int n; vector<Point> v; set<long long> usados; map<long long, long long> DondeEsta; bool sort_vel(Point a, Point b) { if (a.v == b.v) return a.pos > b.pos; return a.v > b.v; } bool sort_pos(Point a, Point b) { return a.pos < b.pos; } long long ft[200005]; long long ft2[200005]; void upd(int i0, long long v) { for (int i = i0 + 1; i <= 200005; i += i & -i) ft[i] += v; } long long get(int i0) { long long r = 0; for (int i = i0; i; i -= i & -i) r += ft[i]; return r; } long long get_sum(int i0, int i1) { return get(i1) - get(i0); } void upd2(int i0, long long v) { for (int i = i0 + 1; i <= 200005; i += i & -i) ft2[i] += v; } long long get2(int i0) { long long r = 0; for (int i = i0; i; i -= i & -i) r += ft2[i]; return r; } long long get_sum2(int i0, int i1) { return get2(i1) - get2(i0); } int main() { cin >> n; for (int i = 0; i < (n); i++) cin >> P[i]; for (int i = 0; i < (n); i++) { cin >> V[i]; v.push_back({P[i], V[i]}); } sort((v.begin()), (v.end()), sort_pos); for (int i = 0; i < (n); i++) { upd(i, r - v[i].pos); upd2(i, 1); } for (int i = 0; i < (n); i++) DondeEsta[v[i].pos] = i; sort((v.begin()), (v.end()), sort_vel); for (auto x : v) { long long i = DondeEsta[x.pos]; sum += get_sum(0, i) - ((r - x.pos) * get_sum2(0, i)); upd(i, x.pos - r); upd2(i, -1); } cout << sum << 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; vector<vector<long long> > mtree, msum, mpos; void merge_sort(vector<pair<long long, long long> >& arr, long long l, long long h, long long id) { long long m = (l + h) / 2; if (l == h) { mtree[id].push_back(arr[l].second); msum[id].push_back(arr[l].first); mpos[id].push_back(l); return; } merge_sort(arr, l, m, 2 * id); merge_sort(arr, m + 1, h, 2 * id + 1); long long i = 0, j = 0; while (i < mtree[2 * id].size() && j < mtree[2 * id + 1].size()) { if (mtree[2 * id][i] < mtree[2 * id + 1][j]) { mtree[id].push_back(mtree[2 * id][i]); mpos[id].push_back(mpos[2 * id][i]); if (!msum[id].empty()) msum[id].push_back(msum[id].back() + arr[mpos[2 * id][i]].first); else msum[id].push_back(arr[mpos[2 * id][i]].first); i++; } else { mtree[id].push_back(mtree[2 * id + 1][j]); mpos[id].push_back(mpos[2 * id + 1][j]); if (!msum[id].empty()) msum[id].push_back(msum[id].back() + arr[mpos[2 * id + 1][j]].first); else msum[id].push_back(arr[mpos[2 * id + 1][j]].first); j++; } } while (i < mtree[2 * id].size()) { mtree[id].push_back(mtree[2 * id][i]); mpos[id].push_back(mpos[2 * id][i]); if (!msum[id].empty()) msum[id].push_back(msum[id].back() + arr[mpos[2 * id][i]].first); else msum[id].push_back(arr[mpos[2 * id][i]].first); i++; } while (j < mtree[2 * id + 1].size()) { mtree[id].push_back(mtree[2 * id + 1][j]); mpos[id].push_back(mpos[2 * id + 1][j]); if (!msum[id].empty()) msum[id].push_back(msum[id].back() + arr[mpos[2 * id + 1][j]].first); else msum[id].push_back(arr[mpos[2 * id + 1][j]].first); j++; } } long long solve(long long s, long long e, long long l, long long h, long long id, long long x, long long v) { long long m = (s + e) / 2; if (s == l && e == h) { long long p = lower_bound(mtree[id].begin(), mtree[id].end(), v) - mtree[id].begin(); long long a = msum[id].back(); if (p != 0) a -= msum[id][p - 1]; return a - x * (mtree[id].size() - p); } else if (l > h) return 0; long long r1 = solve(s, m, l, min(h, m), 2 * id, x, v); long long r2 = solve(m + 1, e, max(m + 1, l), h, 2 * id + 1, x, v); return r1 + r2; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long n; cin >> n; mtree.resize(4 * n); msum.resize(4 * n); mpos.resize(4 * n); vector<pair<long long, long long> > arr(n); for (long long i = 0; i < n; i++) cin >> arr[i].first; for (long long i = 0; i < n; i++) cin >> arr[i].second; sort(arr.begin(), arr.end()); merge_sort(arr, 0, n - 1, 1); long long ans = 0; for (long long i = 0; i < n - 1; i++) ans += solve(0, n - 1, i + 1, n - 1, 1, arr[i].first, arr[i].second); 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
java
import java.util.*; import java.io.*; public class Main { public static void main(String args[]) {new Main().run();} FastReader in = new FastReader(); PrintWriter out = new PrintWriter(System.out); void run(){ work(); out.flush(); } long mod=1000000007; long gcd(long a,long b) { return a==0?b:gcd(b%a,a); } long[] A; long[] B; int n; void work() { n=ni(); A=new long[n+1]; B=new long[n+1]; long[] P=na(n); long[] V=na(n); long[][] R=new long[n][2]; for(int i=0;i<n;i++) { R[i]=new long[] {V[i],P[i]}; } Arrays.sort(R,new Comparator<long[]>() { public int compare(long[] A1,long[] A2) { return (int)(A1[1]-A2[1]); } }); long[][] rec=new long[n][3]; for(int i=0;i<n;i++) { rec[i]=new long[] {R[i][0],R[i][1],i+1}; } long ret=0; Arrays.sort(rec,new Comparator<long[]>() { @Override public int compare(long[] A1, long[] A2) { return (int)(A1[0]-A2[0]); } }); for(int i=0;i<n;i++) { int idx=(int)rec[i][2]; update(idx,rec[i][1],1); } for(int i=0;i<n;i++) { int idx=(int)rec[i][2]; long p=rec[i][1]; long sum=query1(idx+1,n); long cnt=query2(idx+1,n); ret+=sum-cnt*p; update(idx,-p,-1); } out.println(ret); } private long query1(int s, int e) { return query1(e)-query1(s-1); } private long query2(int s, int e) { return query2(e)-query2(s-1); } long query1(int x) { long ret=0; for(;x>0;x-=lowbit(x)) { ret+=A[x]; } return ret; } long query2(int x) { long ret=0; for(;x>0;x-=lowbit(x)) { ret+=B[x]; } return ret; } int lowbit(int x){ return x&-x; } private void update(int x, long v,long c) { for(;x<=n;x+=lowbit(x)) { A[x]+=v; B[x]+=c; } } //input private ArrayList<Integer>[] ng(int n, int m) { ArrayList<Integer>[] graph=(ArrayList<Integer>[])new ArrayList[n]; for(int i=0;i<n;i++) { graph[i]=new ArrayList<>(); } for(int i=1;i<=m;i++) { int s=in.nextInt()-1,e=in.nextInt()-1; graph[s].add(e); graph[e].add(s); } return graph; } private ArrayList<long[]>[] ngw(int n, int m) { ArrayList<long[]>[] graph=(ArrayList<long[]>[])new ArrayList[n]; for(int i=0;i<n;i++) { graph[i]=new ArrayList<>(); } for(int i=1;i<=m;i++) { long s=in.nextLong()-1,e=in.nextLong()-1,w=in.nextLong(); graph[(int)s].add(new long[] {e,w,i}); graph[(int)e].add(new long[] {s,w}); } return graph; } private int ni() { return in.nextInt(); } private long nl() { return in.nextLong(); } private String ns() { return in.next(); } private long[] na(int n) { long[] A=new long[n]; for(int i=0;i<n;i++) { A[i]=in.nextLong(); } return A; } private int[] nia(int n) { int[] A=new int[n]; for(int i=0;i<n;i++) { A[i]=in.nextInt(); } return A; } } class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br=new BufferedReader(new InputStreamReader(System.in)); } public String next() { while(st==null || !st.hasMoreElements())//回车,空行情况 { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.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
python3
from operator import itemgetter class BIT():#1-indexed def __init__(self, size): self.table = [0 for _ in range(size+2)] self.size = size def Sum(self, i):#1からiまでの和 s = 0 while i > 0: s += self.table[i] i -= (i & -i) return s def PointAdd(self, i, x):# while i <= self.size: self.table[i] += x i += (i & -i) return n = int(input()) x = list(map(int, input().split())) v = list(map(int, input().split())) y = sorted([(xx, vv) for xx, vv in zip(x, v)], key=itemgetter(1)) z = [[y[0][0], 1]] for i in range(1, n): if y[i][1] == y[i-1][1]: z.append([y[i][0], z[-1][1]]) else: z.append([y[i][0], z[-1][1]+1]) z.sort() #print(z) b_sigma, b_num = BIT(n+2), BIT(n+2) ans = 0 for x, v in z: num = b_num.Sum(v) ans += x * num - b_sigma.Sum(v) b_num.PointAdd(v, 1) b_sigma.PointAdd(v, x) 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], cnt[200005], num[800005], m; long long sum[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
python3
import sys input = sys.stdin.readline n=int(input()) X=list(map(int,input().split())) V=list(map(int,input().split())) XV=[(X[i],V[i]) for i in range(n)] #compression_dict_x={a: ind for ind, a in enumerate(sorted(set(X)))} compression_dict_v={a: ind+2 for ind, a in enumerate(sorted(set(V)))} XV=[(XV[i][0], compression_dict_v[XV[i][1]]) for i in range(n)] XV.sort(reverse=True) LEN=len(compression_dict_v)+3 BIT1=[0]*(LEN+1) def update1(v,w): while v<=LEN: BIT1[v]+=w v+=(v&(-v)) def getvalue1(v): ANS=0 while v!=0: ANS+=BIT1[v] v-=(v&(-v)) return ANS BIT2=[0]*(LEN+1) def update2(v,w): while v<=LEN: BIT2[v]+=w v+=(v&(-v)) def getvalue2(v): ANS=0 while v!=0: ANS+=BIT2[v] v-=(v&(-v)) return ANS ANS=0 for x,v in XV: ANS+=(getvalue2(LEN)-getvalue2(v-1))-(getvalue1(LEN)-getvalue1(v-1))*x #print(getvalue2(LEN),getvalue2(v-1),getvalue1(LEN),getvalue1(v-1)) #print(x,v,ANS) update1(v,1) update2(v,x) 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; const int INF = 1e9; void update(vector<long long>& v, long long ind, long long val) { ind++; long long n = v.size(); while (ind <= n) { v[ind] += val; ind += ((ind) & (-ind)); } } long long query(vector<long long>& v, long long ind) { ind++; long long res = 0; while (ind > 0) { res += v[ind]; ind -= ((ind) & (-ind)); } return res; } void solve() { long long n; cin >> n; vector<pair<long long, long long>> points(n); for (long long i = 0; i < n; i++) cin >> points[i].first; vector<long long> vel; for (long long i = 0; i < n; i++) { cin >> points[i].second; vel.push_back(points[i].second); } sort((vel).begin(), (vel).end()); sort((points).begin(), (points).end()); vel.resize(unique((vel).begin(), (vel).end()) - vel.begin()); vector<long long> no(vel.size() + 1, 0), csum(vel.size() + 1, 0); long long ans = 0; for (long long i = 0; i < n; i++) { long long v_new = lower_bound(vel.begin(), vel.end(), points[i].second) - vel.begin(); ans += -(query(csum, v_new)) + (points[i].first * query(no, v_new)); update(csum, v_new, points[i].first); update(no, v_new, 1); } cout << ans << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ; solve(); }
1311_F. Moving Points
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i. Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0. Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points. The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct. The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point. Output Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Examples Input 3 1 3 2 -100 2 3 Output 3 Input 5 2 1 4 3 5 2 2 2 3 4 Output 19 Input 2 2 1 -3 0 Output 0
{ "input": [ "3\n1 3 2\n-100 2 3\n", "2\n2 1\n-3 0\n", "5\n2 1 4 3 5\n2 2 2 3 4\n" ], "output": [ "3\n", "0\n", "19\n" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; pair<int, long long> a[200005]; int t[200005]; map<int, int> M; pair<int, long long> f[200005]; int n; void update(int gt) { int x = M[gt]; while (x <= n) { f[x] = {f[x].first + 1, f[x].second + gt}; x += (x & -x); } } pair<int, long long> get(int x) { int cnt = 0; long long sum = 0; while (x >= 1) { cnt += f[x].first; sum += f[x].second; x -= (x & -x); } return make_pair(cnt, sum); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i].second; t[i] = a[i].second; } for (int i = 1; i <= n; i++) cin >> a[i].first; sort(a + 1, a + n + 1); sort(t + 1, t + n + 1); for (int i = 1; i <= n; i++) M[t[i]] = i; long long ans = 0; for (int i = 1; i <= n; i++) { pair<int, long long> res; res = get(M[a[i].second] - 1); ans += (1ll * a[i].second * res.first - res.second); update(a[i].second); } 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 <typename Arg1> void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 << '\n'; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } template <class T> void mxi(T& a, const T& b) { a = max(a, b); } template <class T> void mni(T& a, const T& b) { a = min(a, b); } long double EPS = 1e-9; mt19937 RNG(chrono::steady_clock::now().time_since_epoch().count()); struct node { long long sum, cnt; node() { sum = 0; cnt = 0; } }; node t[4 * 200200]; node merge(node a, node b) { node tmp; tmp.sum = a.sum + b.sum; tmp.cnt = a.cnt + b.cnt; return tmp; } void upd(long long id, long long l, long long r, long long pos, long long val) { if (pos < l || pos > r) return; else if (l == r) { t[id].sum += val; t[id].cnt++; return; } upd(id << 1, l, ((l + r) >> 1), pos, val); upd(id << 1 | 1, ((l + r) >> 1) + 1, r, pos, val); t[id] = merge(t[id << 1], t[id << 1 | 1]); } node query(long long id, long long l, long long r, long long lq, long long rq) { if (lq > r || l > rq) return node(); else if (lq <= l && rq >= r) return t[id]; return merge(query(id << 1, l, ((l + r) >> 1), lq, rq), query(id << 1 | 1, ((l + r) >> 1) + 1, r, lq, rq)); } void solve() { long long n; cin >> n; vector<pair<long long, long long> > v; for (long long i = 0; i < (n); i++) { long long x; cin >> x; v.push_back({x, 0}); } set<long long> vel; for (long long j = 0; j < (n); j++) { long long w; cin >> w; vel.insert(w); v[j].second = w; } sort((v).begin(), (v).end()); map<long long, long long> mp; long long cnt = 0; for (auto x : vel) { mp[x] = cnt++; } long long an = 0; for (long long i = n - 1; ~i; i--) { node ans = query(1, 0, 200010, mp[v[i].second], 200010); long long cc = ans.cnt; an += (ans.sum - cc * v[i].first); upd(1, 0, 200010, mp[v[i].second], v[i].first); } cout << an << '\n'; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t, tab; t = 1; tab = t; while (t--) { solve(); } cerr << ((double)clock() / CLOCKS_PER_SEC); }
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.math.*; /** * * @Har_Har_Mahadev */ public class D { private static long INF = 2000000000000000000L, M = 1000000007, MM = 998244353; private static int N = 0; private static ArrayList<Pair> lis; static HashMap<Long, Integer> map = new HashMap<Long, Integer>(); public static void process() throws IOException { int n = sc.nextInt(); long x[] = sc.readArrayLong(n); long time[] = sc.readArrayLong(n); lis = new ArrayList<D.Pair>(); for(int i=0; i<n; i++)lis.add(new Pair(x[i], time[i])); Collections.sort(lis); solve(); } private static void solve() { ArrayList<Long> x = new ArrayList<Long>(); ArrayList<Long> y = new ArrayList<Long>(); TreeSet<Long> set = new TreeSet<Long>(); for(Pair e : lis) { x.add(e.x); y.add(e.y); set.add(e.y); } int num = 0; for(long key : set) map.put(key, ++num); ArrayList<Integer> v = new ArrayList<Integer>(); for(long e : y)v.add(map.get(e)); long ans = 0; int size = x.size(); BIT bit1 = new BIT(num+1); BIT bit2 = new BIT(num+1); for(int i = 0; i < size; i++) { long cnt = bit1.read(v.get(i)); long sum = bit2.read(v.get(i)); ans += cnt * x.get(i) - sum; bit1.update(v.get(i), 1); bit2.update(v.get(i), x.get(i)); } System.out.println(ans); } static class BIT { int n; long[] tree; public BIT(int n) { this.n = n; tree = new long[n + 2]; } long read(int i) { long sum = 0; while (i > 0) { sum += tree[i]; i -= i & -i; } return sum; } void update(int i, long val) { while (i <= n) { tree[i] += val; i += i & -i; } } // if the BIT is a freq array, returns the // index of the kth item, or n if there are fewer // than k items. int getKth(int k) { int e=Integer.highestOneBit(n), o=0; for (; e!=0; e>>=1) { if (e+o<=n && tree[e+o]<=k) { k-=tree[e+o]; o+=e; } } return o; } } //============================================================================= //--------------------------The End--------------------------------- //============================================================================= static FastScanner sc; static PrintWriter out; public static void main(String[] args) throws IOException { boolean oj = true; if (oj) { sc = new FastScanner(); out = new PrintWriter(System.out); } else { sc = new FastScanner(100); out = new PrintWriter("output.txt"); } int t = 1; // t = sc.nextInt(); while (t-- > 0) { process(); } out.flush(); out.close(); } static class Pair implements Comparable<Pair> { long x, y; Pair(long x, long y) { this.x = x; this.y = y; } @Override public int compareTo(Pair o) { return Long.compare(this.x, o.x); } // @Override // public boolean equals(Object o) { // if (this == o) return true; // if (!(o instanceof Pair)) return false; // Pair key = (Pair) o; // return x == key.x && y == key.y; // } // // @Override // public int hashCode() { // int result = x; // result = 31 * result + y; // return result; // } } ///////////////////////////////////////////////////////////////////////////////////////////////////////// static void println(Object o) { out.println(o); } static void println() { out.println(); } static void print(Object o) { out.print(o); } static void pflush(Object o) { out.println(o); out.flush(); } static int ceil(int x, int y) { return (x % y == 0 ? x / y : (x / y + 1)); } static long ceil(long x, long y) { return (x % y == 0 ? x / y : (x / y + 1)); } static int max(int x, int y) { return Math.max(x, y); } static int min(int x, int y) { return Math.min(x, y); } static int abs(int x) { return Math.abs(x); } static long abs(long x) { return Math.abs(x); } static int log2(int N) { int result = (int) (Math.log(N) / Math.log(2)); return result; } static long max(long x, long y) { return Math.max(x, y); } static long min(long x, long y) { return Math.min(x, y); } public static int gcd(int a, int b) { BigInteger b1 = BigInteger.valueOf(a); BigInteger b2 = BigInteger.valueOf(b); BigInteger gcd = b1.gcd(b2); return gcd.intValue(); } public static long gcd(long a, long b) { BigInteger b1 = BigInteger.valueOf(a); BigInteger b2 = BigInteger.valueOf(b); BigInteger gcd = b1.gcd(b2); return gcd.longValue(); } public static long lca(long a, long b) { return (a * b) / gcd(a, b); } public static int lca(int a, int b) { return (a * b) / gcd(a, b); } ///////////////////////////////////////////////////////////////////////////////////////////////////////// static class FastScanner { BufferedReader br; StringTokenizer st; FastScanner() throws FileNotFoundException { br = new BufferedReader(new InputStreamReader(System.in)); } FastScanner(int a) throws FileNotFoundException { br = new BufferedReader(new FileReader("input.txt")); } String next() throws IOException { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } String nextLine() throws IOException { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int[] readArray(int n) throws IOException { int[] A = new int[n]; for (int i = 0; i != n; i++) { A[i] = sc.nextInt(); } return A; } long[] readArrayLong(int n) throws IOException { long[] A = new long[n]; for (int i = 0; i != n; i++) { A[i] = sc.nextLong(); } return A; } } static void ruffleSort(int[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); int temp = a[i]; a[i] = a[r]; a[r] = temp; } Arrays.sort(a); } static void ruffleSort(long[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); long temp = a[i]; a[i] = a[r]; a[r] = temp; } Arrays.sort(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
python2
from sys import stdin from collections import * def fast2(): import os, sys, atexit from cStringIO import StringIO as BytesIO # range = xrange sys.stdout = BytesIO() atexit.register(lambda: os.write(1, sys.stdout.getvalue())) return BytesIO(os.read(0, os.fstat(0).st_size)).readline class order_tree: def __init__(self, arr): self.n = len(arr) self.tree = [[0, 0] for _ in range(self.n * 2)] self.order = defaultdict(int, {arr[i]: i for i in range(self.n)}) # get interval[l,r) def query(self, l): res, coun = 0, 0 l = self.order[l] + self.n r = self.n * 2 while l < r: if l & 1: res += self.tree[l][0] coun += self.tree[l][1] l += 1 if r & 1: r -= 1 res += self.tree[r][0] coun += self.tree[r][1] l >>= 1 r >>= 1 return res, coun def update(self, ix, val): ix = self.n + self.order[ix] # set new value self.tree[ix][0] += val self.tree[ix][1] += 1 # move up while ix > 1: self.tree[ix >> 1][0] = self.tree[ix][0] + self.tree[ix ^ 1][0] self.tree[ix >> 1][1] = self.tree[ix][1] + self.tree[ix ^ 1][1] ix >>= 1 # input = fast2() rints = lambda: [int(x) for x in stdin.readline().split()] n, a = int(input()), sorted(map(lambda x, y: [x, y], rints(), rints())) dis = sorted(set([x[1] for x in a])) tree, ans = order_tree(dis), 0 for i in range(n - 1, -1, -1): su, coun = tree.query(a[i][1]) ans += su - coun * a[i][0] tree.update(a[i][1], a[i][0]) print(ans)
1311_F. Moving Points
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i. Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0. Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points. The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct. The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point. Output Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Examples Input 3 1 3 2 -100 2 3 Output 3 Input 5 2 1 4 3 5 2 2 2 3 4 Output 19 Input 2 2 1 -3 0 Output 0
{ "input": [ "3\n1 3 2\n-100 2 3\n", "2\n2 1\n-3 0\n", "5\n2 1 4 3 5\n2 2 2 3 4\n" ], "output": [ "3\n", "0\n", "19\n" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include <bits/stdc++.h> #pragma GCC optimize(2) using namespace std; const long long INF = 1e18; const int maxn = 1e6 + 6; const int mod = 1e9 + 7; const double eps = 1e-9; inline bool read(long long &num) { char in; bool IsN = false; in = getchar(); if (in == EOF) return false; while (in != '-' && (in < '0' || in > '9')) in = getchar(); if (in == '-') { IsN = true; num = 0; } else num = in - '0'; while (in = getchar(), in >= '0' && in <= '9') { num *= 10, num += in - '0'; } if (IsN) num = -num; return true; } long long n, m; struct node { int x, v; bool friend operator<(node a, node b) { return a.x < b.x; } } save[maxn]; vector<int> v; long long a[maxn], b[maxn]; int nn; void update(long long *p, int pos, long long x) { while (pos <= nn) { p[pos] += x; pos += pos & -pos; } } long long GetSum(long long *p, long long x) { long long ans = 0; while (x > 0) { ans += p[x]; x -= x & -x; } return ans; } int getid(long long x) { return lower_bound(v.begin(), v.end(), x) - v.begin() + 1; } int main() { read(n); for (int i = 1; i <= n; i++) scanf("%d", &save[i].x); for (int i = 1; i <= n; i++) { scanf("%d", &save[i].v); v.push_back(save[i].v); } sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); nn = v.size(); sort(save + 1, save + 1 + n); long long ans = 0; for (int i = 1; i <= n; i++) { int id = getid(save[i].v); long long tempx = GetSum(b, id); long long tempy = GetSum(a, id); ans += tempx * save[i].x - tempy; update(b, id, 1); update(a, id, save[i].x); } printf("%lld\n", ans); return 0; }
1311_F. Moving Points
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i. Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0. Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points. The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct. The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point. Output Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Examples Input 3 1 3 2 -100 2 3 Output 3 Input 5 2 1 4 3 5 2 2 2 3 4 Output 19 Input 2 2 1 -3 0 Output 0
{ "input": [ "3\n1 3 2\n-100 2 3\n", "2\n2 1\n-3 0\n", "5\n2 1 4 3 5\n2 2 2 3 4\n" ], "output": [ "3\n", "0\n", "19\n" ] }
{ "input": [], "output": [] }
CORRECT
python3
import math import sys from collections import defaultdict, Counter from itertools import groupby #input = sys.stdin.readline rt = lambda: map(int, input().split()) ri = lambda: int(input()) rl = lambda: list(map(int, input().split())) class Bit: def __init__(self, n): self.size = n self.tree = [0] * (n+1) def sum(self, i): s = 0 while i > 0: s += self.tree[i] i -= i & (-i) return s def add(self, i, val): while i <= self.size: self.tree[i] += val i += i & (-i) def main(): n = ri() x = rl() v = rl() pts = [(xi, vi) for xi, vi in zip(x, v)] pts.sort(key=lambda a: (a[1], a[0])) comprX = {val: i+1 for i, val in enumerate(sorted(x))} bit_x = Bit(n+1) bit_cnt = Bit(n+1) res = 0 for xi, vi in pts: compr = comprX[xi] res += bit_cnt.sum(compr)*xi - bit_x.sum(compr) bit_x.add(compr, xi) bit_cnt.add(compr, 1) print(res) 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; const long long N = 2e5 + 5; long long Bit[N][2], n; long long get(long long i, long long k) { long long ans = 0; for (i; i > 0; i -= i & (-i)) { ans += Bit[i][k]; } return ans; } void update(long long i, long long val, long long k) { for (i; i <= n; i += i & (-i)) { Bit[i][k] += val; } } int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); srand(time(NULL)); ; long long k, i, t, j, m, x, y, z, c = 0, ans = 0; cin >> n; vector<pair<long long, long long> > v(n), v1(n); for (i = 0; i < n; i++) cin >> v[i].first; for (i = 0; i < n; i++) cin >> v[i].second; sort((v).begin(), (v).end()); vector<long long> v0(n); map<long long, long long> mp; c = 1; for (i = 0; i < n; i++) { v1[i].first = v[i].second; v1[i].second = i; } sort((v1).begin(), (v1).end()); for (i = 0; i < n; i++) { if (mp[v1[i].first] == 0) { mp[v1[i].first] = c; c++; } v0[v1[i].second] = mp[v1[i].first]; } for (i = 0; i < n; i++) { x = get(v0[i], 0); y = get(v0[i], 1); ans += x * v[i].first - y; update(v0[i], 1, 0); update(v0[i], v[i].first, 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
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]; 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] = p[i][1] + 100000000; } in.close(); v = shrink(v); for (int i = 0; i < n; i++) { p[i][1] = v[i]; } 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); } //code copied from submission 71787218 made by uwi (great thanks to him!) public static long[] radixSort(long[] f){ return radixSort(f, f.length); } public static long[] radixSort(long[] f, int n) { long[] to = new long[n]; { int[] b = new int[65537]; for(int i = 0;i < n;i++)b[1+(int)(f[i]&0xffff)]++; for(int i = 1;i <= 65536;i++)b[i]+=b[i-1]; for(int i = 0;i < n;i++)to[b[(int)(f[i]&0xffff)]++] = f[i]; long[] d = f; f = to;to = d; } { int[] b = new int[65537]; for(int i = 0;i < n;i++)b[1+(int)(f[i]>>>16&0xffff)]++; for(int i = 1;i <= 65536;i++)b[i]+=b[i-1]; for(int i = 0;i < n;i++)to[b[(int)(f[i]>>>16&0xffff)]++] = f[i]; long[] d = f; f = to;to = d; } { int[] b = new int[65537]; for(int i = 0;i < n;i++)b[1+(int)(f[i]>>>32&0xffff)]++; for(int i = 1;i <= 65536;i++)b[i]+=b[i-1]; for(int i = 0;i < n;i++)to[b[(int)(f[i]>>>32&0xffff)]++] = f[i]; long[] d = f; f = to;to = d; } { int[] b = new int[65537]; for(int i = 0;i < n;i++)b[1+(int)(f[i]>>>48&0xffff)]++; for(int i = 1;i <= 65536;i++)b[i]+=b[i-1]; for(int i = 0;i < n;i++)to[b[(int)(f[i]>>>48&0xffff)]++] = f[i]; long[] d = f; f = to;to = d; } return f; } public static int[] shrink(int[] a) { int n = a.length; long[] b = new long[n]; for (int i = 0; i < n; i++) b[i] = (long) a[i] << 32 | i; b = radixSort(b); int[] ret = new int[n]; int p = 0; for (int i = 0; i < n; i++) { if (i > 0 && (b[i] ^ b[i - 1]) >> 32 != 0) p++; ret[(int) b[i]] = p; } return ret; } }
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 int64_t INF = (int64_t)(2e18); const int64_t inf = (int64_t)(1e9 + 7); struct stree { vector<int64_t> t; int64_t n = 1; stree() {} stree(int64_t nn) { while (n < nn) n *= 2; t.assign(2 * n, 0); } void set(int64_t i, int64_t x) { t[i += n] += x; for (i /= 2; i; i /= 2) t[i] = t[i + i] + t[i + i + 1]; } int64_t get(int64_t l, int64_t r) { int64_t res = 0; for (l += n, r += n; l <= r; l /= 2, r /= 2) { if (l & 1) res += t[l++]; if (~r & 1) res += t[r--]; } return res; } }; int32_t main() { ios_base::sync_with_stdio(false); cout << fixed << setprecision(10); cin.tie(nullptr); int64_t n; cin >> n; vector<int64_t> x(n); for (auto& it : x) cin >> it; vector<int64_t> v(n); for (auto& it : v) cin >> it; vector<int64_t> vs; for (auto& it : v) vs.push_back(it); sort((vs).begin(), (vs).end()); vs.resize(unique((vs).begin(), (vs).end()) - begin(vs)); for (auto& it : v) it = lower_bound((vs).begin(), (vs).end(), it) - begin(vs); vector<pair<int64_t, int64_t>> pts(n); for (int64_t i = 0; i < n; ++i) { pts[i].first = x[i]; pts[i].second = v[i]; } sort((pts).begin(), (pts).end()); stree ls(vs.size()); stree lc(vs.size()); stree rs(vs.size()); stree rc(vs.size()); for (auto& p : pts) { rs.set(p.second, p.first); rc.set(p.second, 1); } int64_t res = 0; for (auto& p : pts) { rs.set(p.second, -p.first); rc.set(p.second, -1); res += lc.get(0, p.second) * p.first; res -= ls.get(0, p.second); res -= rc.get(p.second, vs.size() - 1) * p.first; res += rs.get(p.second, vs.size() - 1); ls.set(p.second, p.first); lc.set(p.second, 1); } 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; template <class T> bool uin(T& a, T b) { return a > b ? (a = b, true) : false; } template <class T> bool uax(T& a, T b) { return a < b ? (a = b, true) : false; } template <typename T> class fenwick { public: vector<T> fenw; int n; fenwick(int _n) : n(_n) { fenw.resize(n); } void modify(int x, T v) { while (x < n) { fenw[x] += v; x |= (x + 1); } } T get(int x) { T v{}; while (x >= 0) { v += fenw[x]; x = (x & (x + 1)) - 1; } return v; } }; const int nax = 2e5 + 10; int N; vector<int> v; vector<pair<int, int>> P; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> N; P.resize(N); v.resize(N); fenwick<long long> sum_tree(N), count_tree(N); for (int i = 0; i < N; ++i) { cin >> P[i].first; } for (int i = 0; i < N; ++i) { cin >> P[i].second; v[i] = P[i].second; } sort(v.begin(), v.end()); v.resize(unique(v.begin(), v.end()) - v.begin()); sort(P.begin(), P.end()); long long total = 0; for (int i = 0; i < N; ++i) { int id = lower_bound(v.begin(), v.end(), P[i].second) - v.begin(); long long count = count_tree.get(id); long long sum = sum_tree.get(id); total += count * 1LL * P[i].first - sum; count_tree.modify(id, 1); sum_tree.modify(id, P[i].first); } cout << total << '\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; template <typename Monoid> struct SegmentTree { typedef function<Monoid(Monoid, Monoid)> F; int n; F f; Monoid id; vector<Monoid> dat; SegmentTree(int n_, F f, Monoid id) : f(f), id(id) { init(n_); } void init(int n_) { n = 1; while (n < n_) n <<= 1; dat.assign(n << 1, id); } void build(const vector<Monoid> &v) { for (int i = 0; i < v.size(); ++i) dat[i + n] = v[i]; for (int i = n - 1; i; --i) dat[i] = f(dat[i << 1 | 0], dat[i << 1 | 1]); } void update(int k, Monoid x) { dat[k += n] = x; while (k >>= 1) dat[k] = f(dat[k << 1 | 0], dat[k << 1 | 1]); } Monoid query(int a, int b) { if (a >= b) return id; Monoid vl = id, vr = id; for (int l = a + n, r = b + n; l < r; l >>= 1, r >>= 1) { if (l & 1) vl = f(vl, dat[l++]); if (r & 1) vr = f(dat[--r], vr); } return f(vl, vr); } Monoid operator[](int i) { return dat[i + n]; } }; int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; vector<long long> x(n), v(n); for (int i = 0; i < n; ++i) cin >> x[i]; for (int i = 0; i < n; ++i) cin >> v[i]; vector<long long> compress(n); for (int i = 0; i < n; ++i) compress[i] = v[i]; sort(compress.begin(), compress.end()); compress.erase(unique(compress.begin(), compress.end()), compress.end()); for (int i = 0; i < n; ++i) v[i] = lower_bound(compress.begin(), compress.end(), v[i]) - compress.begin(); int m = compress.size(); auto f = [](pair<long long, long long> a, pair<long long, long long> b) { return pair<long long, long long>{a.first + b.first, a.second + b.second}; }; SegmentTree<pair<long long, long long> > seg(m, f, {0, 0}); vector<pair<long long, long long> > xv; for (int i = 0; i < n; ++i) xv.emplace_back(x[i], v[i]); sort(xv.begin(), xv.end()); for (int i = 0; i < n; ++i) { auto p = seg[xv[i].second]; seg.update(xv[i].second, {p.first + xv[i].first, p.second + 1}); } long long ans = 0; for (int i = 0; i < n; ++i) { auto p = seg.query(xv[i].second, m), q = seg[xv[i].second]; ans += p.first - xv[i].first * p.second; seg.update(xv[i].second, {q.first - xv[i].first, q.second - 1}); } cout << ans << '\n'; }
1311_F. Moving Points
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i. Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0. Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points. The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct. The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point. Output Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Examples Input 3 1 3 2 -100 2 3 Output 3 Input 5 2 1 4 3 5 2 2 2 3 4 Output 19 Input 2 2 1 -3 0 Output 0
{ "input": [ "3\n1 3 2\n-100 2 3\n", "2\n2 1\n-3 0\n", "5\n2 1 4 3 5\n2 2 2 3 4\n" ], "output": [ "3\n", "0\n", "19\n" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 3; const int maxm = 1e6 + 3; const int mod = 1e9 + 7; const int oo = 1e9 + 5; const long double pi = 3.141592653589793238462643383279502884; void input() { if (fopen("abc" ".inp", "r")) { freopen( "abc" ".inp", "r", stdin); freopen( "abc" ".out", "w", stdout); } ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } int n; pair<long long, long long> a[maxn], f[maxn]; vector<long long> v; pair<long long, long long> operator+(const pair<long long, long long>& a, const pair<long long, long long>& b) { return {a.first + b.first, a.second + b.second}; } void update(int p, int v) { for (int i = p; i <= n; i += i & -i) f[i] = f[i] + pair<long long, long long>(v, 1); } pair<long long, long long> get(int p) { pair<long long, long long> ans = {0, 0}; for (int i = p; i > 0; i -= i & -i) ans = ans + f[i]; return ans; } int main() { input(); cin >> n; for (int i = 1; i <= n; ++i) cin >> a[i].first; for (int i = 1; i <= n; ++i) { cin >> a[i].second; v.push_back(a[i].second); } sort(a + 1, a + 1 + n); sort(v.begin(), v.end()); v.resize(unique(v.begin(), v.end()) - v.begin()); long long ans = 0; for (int i = 1; i <= n; ++i) { auto it = lower_bound(v.begin(), v.end(), a[i].second) - v.begin() + 1; pair<long long, long long> cur = get(it); ans += a[i].first * cur.second - cur.first; update(it, a[i].first); } cout << ans; return 0; }
1311_F. Moving Points
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i. Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0. Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points. The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct. The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point. Output Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Examples Input 3 1 3 2 -100 2 3 Output 3 Input 5 2 1 4 3 5 2 2 2 3 4 Output 19 Input 2 2 1 -3 0 Output 0
{ "input": [ "3\n1 3 2\n-100 2 3\n", "2\n2 1\n-3 0\n", "5\n2 1 4 3 5\n2 2 2 3 4\n" ], "output": [ "3\n", "0\n", "19\n" ] }
{ "input": [], "output": [] }
CORRECT
java
import java.awt.*; import java.util.*; import java.io.*; public class Solution { public static void main(String[] args) throws Exception { FastReader sc = new FastReader(); n=sc.nextInt(); bit=new long[n+1]; bit1=new int[n+2]; Point p[]=new Point[n]; for (int i=0;i<n;i++)p[i]=new Point(sc.nextInt(),0); for (int i=0;i<n;i++)p[i].y=sc.nextInt(); Arrays.sort(p, new Comparator<Point>() { @Override public int compare(Point o1, Point o2) { return o1.y-o2.y; } }); int x=0; int prev=-1; for (int i=0;i<n;i++){ if (i>0 && prev!=p[i].y)x++; prev=p[i].y; p[i].y=x; } Arrays.sort(p, new Comparator<Point>() { @Override public int compare(Point o1, Point o2) { return o1.x-o2.x; } }); // HashMap<Integer,Integer> map=new HashMap<>(); // for (int i=0;i<n;i++)map.put(p[i].x,i); long ans=0; for (int i=n-1;i>=0;i--){ // System.out.println(p[i].x+" "+p[i].y); long sum=suff(p[i].y); long sum1=suff1(p[i].y); up(p[i].y,p[i].x); up1(p[i].y); ans+=Math.abs(sum-p[i].x*sum1); // System.out.println(sum+" "+p[i].x*sum1); } System.out.println(ans); } static long bit[]; static int bit1[]; static int n; static void up(int i,int val){ for (i++;i<=n;i+=i&(-i)){ bit[i]+=val; } } static void up1(int i){ for (i++;i<=n;i+=i&(-i)){ bit1[i]+=1; } } static long sum(int i){ long res=0; for (i++;i>0;i-=i&(-i)){ res+=bit[i]; } return res; } static long sum1(int i){ long res=0; for (i++;i>0;i-=i&(-i)){ res+=bit1[i]; } return res; } static long suff(int i){ return sum(n-1)-sum(i-1); } static long suff1(int i){ return sum1(n-1)-sum1(i-1); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { 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 nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
1311_F. Moving Points
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i. Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0. Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points. The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct. The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point. Output Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Examples Input 3 1 3 2 -100 2 3 Output 3 Input 5 2 1 4 3 5 2 2 2 3 4 Output 19 Input 2 2 1 -3 0 Output 0
{ "input": [ "3\n1 3 2\n-100 2 3\n", "2\n2 1\n-3 0\n", "5\n2 1 4 3 5\n2 2 2 3 4\n" ], "output": [ "3\n", "0\n", "19\n" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; ll ftget(vector<ll> &ft, ll idx) { ll ans = 0; while (idx > 0) { ans += ft[idx]; idx -= (idx & (-idx)); } return ans; } void ftput(vector<ll> &ft, ll idx, ll value) { while (idx < ft.size()) { ft[idx] += value; idx += (idx & (-idx)); } } void run() { ll n; cin >> n; vector<pair<ll, ll>> data; vector<ll> vx, vv; for (int i = 0; i < n; ++i) { int x; cin >> x; vx.push_back(x); } for (int i = 0; i < n; ++i) { int x; cin >> x; vv.push_back(x); data.emplace_back(vx[i], x); } vx.clear(); vx.shrink_to_fit(); sort(vv.begin(), vv.end()); map<ll, ll> comp; for (int i = 0; i < n; ++i) { comp[vv[i]] = i + 1; } vv.clear(); vv.shrink_to_fit(); for (auto &x : data) { x.second = comp[x.second]; } comp.clear(); vector<ll> ftdist(n + 10, 0); vector<ll> ftcnt(n + 10, 0); ll ans = 0; sort(data.begin(), data.end()); for (auto pt : data) { ll prevno = ftget(ftcnt, pt.second); ll prevdst = ftget(ftdist, pt.second); ans += prevno * pt.first - prevdst; ftput(ftcnt, pt.second, 1); ftput(ftdist, pt.second, pt.first); } cout << ans << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); run(); 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
# -*- coding: utf-8 -*- import sys from collections import Counter def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [[c] * b for i in range(a)] def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)] def Yes(): print('Yes') def No(): print('No') def YES(): print('YES') def NO(): print('NO') # sys.setrecursionlimit(10 ** 9) INF = 10 ** 18 MOD = 10 ** 9 + 7 class BIT: def __init__(self, n): n += 1 nv = 1 while nv < n: nv *= 2 self.size = nv self.tree = [0] * nv def sum(self, i): s = 0 i += 1 while i > 0: s += self.tree[i-1] i -= i & -i return s def add(self, i, x): i += 1 while i <= self.size: self.tree[i-1] += x i += i & -i def get(self, l, r=None): if r is None: r = l + 1 res = 0 if r: res += self.sum(r-1) if l: res -= self.sum(l-1) return res def update(self, i, x): self.add(i, x - self.get(i)) def compress(S): zipped, unzipped = {}, {} for i, a in enumerate(sorted(S)): zipped[a] = i unzipped[i] = a return zipped, unzipped N = INT() A = LIST() B = LIST() BA = list(zip(B, A)) BA.sort() zipped, _ = compress(set(A)) bit1 = BIT(N) bit2 = BIT(N) ans = 0 for b, a in BA: sm = bit1.sum(zipped[a]) cnt = bit2.sum(zipped[a]) ans += cnt * a - sm bit1.add(zipped[a], a) bit2.add(zipped[a], 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; const long long int inf = 1e18, M = 1e9 + 7; const long long int N = 2e5 + 10; vector<pair<long long int, long long int> > tree(4 * N, make_pair(0, 0)); map<long long int, long long int> mp; pair<long long int, long long int> combine( pair<long long int, long long int> a, pair<long long int, long long int> b) { pair<long long int, long long int> z; z.first = a.first + b.first; z.second = a.second + b.second; return z; } void update(long long int node, long long int start, long long int en, long long int idx, long long int val) { if (start == en) { tree[node].first += val; tree[node].second++; return; } long long int mid = (start + en) / 2; if (start <= idx and idx <= mid) update(2 * node, start, mid, idx, val); else update(2 * node + 1, mid + 1, en, idx, val); tree[node] = combine(tree[2 * node], tree[2 * node + 1]); } pair<long long int, long long int> query(long long int node, long long int start, long long int en, long long int l, long long int r) { if (r < start || en < l) return {0, 0}; if (l <= start && en <= r) return tree[node]; long long int mid = (start + en) / 2; pair<long long int, long long int> q1 = query(2 * node, start, mid, l, r); pair<long long int, long long int> q2 = query(2 * node + 1, mid + 1, en, l, r); return combine(q1, q2); } void solve() { long long int n; cin >> n; vector<pair<long long int, long long int> > v(n); for (long long int i = 0; i < n; ++i) cin >> v[i].first; for (long long int i = 0; i < n; ++i) cin >> v[i].second, mp[v[i].second]; long long int cnt = 0; for (auto& c : mp) c.second = cnt++; sort(v.begin(), v.end()); pair<long long int, long long int> p; long long int ans = 0; for (long long int i = 0; i < n; ++i) { p = query(1, 0, n, 0, mp[v[i].second]); ans += p.first * -1ll + v[i].first * p.second; update(1, 0, n, mp[v[i].second], v[i].first); } cout << ans; } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long int t = 1; while (t--) { solve(); } return 0; }
1311_F. Moving Points
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i. Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0. Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points. The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct. The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point. Output Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Examples Input 3 1 3 2 -100 2 3 Output 3 Input 5 2 1 4 3 5 2 2 2 3 4 Output 19 Input 2 2 1 -3 0 Output 0
{ "input": [ "3\n1 3 2\n-100 2 3\n", "2\n2 1\n-3 0\n", "5\n2 1 4 3 5\n2 2 2 3 4\n" ], "output": [ "3\n", "0\n", "19\n" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int M = 200005; int n, m, w[M]; long long ans, c[M][2]; struct node { int x, v; } p[M]; bool cmp(node a, node b) { return a.x < b.x; } long long query(int x, int t) { long long r = 0; for (int i = x; i; i -= i & -i) r += c[i][t]; return r; } void upd(int x, int y) { for (int i = x; i <= m; i += i & -i) c[i][0]++, c[i][1] += y; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &p[i].x); for (int i = 1; i <= n; i++) { scanf("%d", &p[i].v); w[i] = p[i].v; } sort(p + 1, p + 1 + n, cmp); sort(w + 1, w + 1 + n); m = unique(w + 1, w + 1 + n) - (w + 1); for (int i = 1; i <= n; i++) { int a = lower_bound(w + 1, w + 1 + m, p[i].v) - w; ans += query(a, 0) * p[i].x - query(a, 1); upd(a, p[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; int n, op; pair<long long, int> tree[201000]; pair<long long, int> q[201000]; long long a[201000]; int lowbits(int x) { return x & (-x); } void add(long long x, int pos) { while (pos <= n) { tree[pos].first += x; tree[pos].second++; pos += lowbits(pos); } } pair<long long, int> getsum(int x) { pair<long long, int> res; while (x > 0) { res.first += tree[x].first; res.second += tree[x].second; x -= lowbits(x); } return res; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%lld", &q[i].first); for (int i = 1; i <= n; i++) scanf("%d", &q[i].second), a[++op] = q[i].second; sort(a + 1, a + 1 + n), sort(q + 1, q + 1 + n); long long ans = 0; for (int i = 1; i <= n; i++) { int pos = lower_bound(a + 1, a + 1 + n, q[i].second) - a; pair<long long, int> now = getsum(pos); ans += now.second * q[i].first - now.first; add(q[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
python3
class Fenwick: def __init__(self, size): self.tree = [0] * size self.size = size def add(self, idx, delta): while idx < self.size: self.tree[idx] += delta idx = (idx + 1) | idx def getp(self, right): res = 0 while right != -1: res += self.tree[right] right = ((right + 1) & right) - 1 return res def get(self, left, right): if left > right: return 0 return self.getp(right) - self.getp(left - 1) n = int(input()) X = list(map(int, input().split())) V = list(map(int, input().split())) XV = list(zip(X, V)) XV.sort() order = list(range(n)) order.sort(key=lambda i: XV[i][1]) ans = 0 for i in range(n): ans += XV[n - 1 - i][0] * (n - 1 - 2 * i) fw = Fenwick(n) fwcount = Fenwick(n) for i in order: ans -= fw.get(i + 1, n - 1) - fwcount.get(i + 1, n - 1) * XV[i][0] fw.add(i, XV[i][0]) fwcount.add(i, 1) print(ans)
1311_F. Moving Points
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i. Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0. Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points. The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct. The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point. Output Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Examples Input 3 1 3 2 -100 2 3 Output 3 Input 5 2 1 4 3 5 2 2 2 3 4 Output 19 Input 2 2 1 -3 0 Output 0
{ "input": [ "3\n1 3 2\n-100 2 3\n", "2\n2 1\n-3 0\n", "5\n2 1 4 3 5\n2 2 2 3 4\n" ], "output": [ "3\n", "0\n", "19\n" ] }
{ "input": [], "output": [] }
CORRECT
java
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { FastReader scan = new FastReader(); //PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("taming.out"))); PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); Task solver = new Task(); //int t = scan.nextInt(); int t = 1; for(int i = 1; i <= t; i++) solver.solve(i, scan, out); out.close(); } static class Task { public void solve(int testNumber, FastReader sc, PrintWriter pw) { int n = sc.nextInt(); bit chc = new bit(n+4); bit num = new bit(n+4); int[] x = new int[n]; tup[] arr = new tup[n]; for(int i=0;i<n;i++){ arr[i]=new tup(sc.nextInt(),0); } for(int i=0;i<n;i++){ x[i]=sc.nextInt(); arr[i].b=x[i]; } HashMap<Integer, Integer> hm = new HashMap<Integer,Integer>(); shuffle(x); Arrays.sort(x); for(int i=0;i<n;i++){ hm.put(x[i],i+1); } Arrays.sort(arr); long sum = 0; for(tup s : arr){ //pw.println(s.b); //pw.println(chc.sum(n+4,s.b)+" "+num.sum(n+4,s.b)); int c = hm.get(s.b); sum += chc.sum(n+4,c)- s.a * num.sum(n + 4, c); chc.add(c, s.a); num.add(c,1); } pw.println(sum); } } static class bit { int n; long[] bit; public bit(int n) { this.n=n; bit=new long[n+1]; } void add(int ind, int c) { for(; ind<=n;ind|=(ind+1)) { bit[ind]+=c; } } long sum(int r) { long out =0; for(;r>=0;r=(r&(r+1))-1) { out+=bit[r]; } return out; } long sum(int r, int l) { return sum(r)-sum(l-1); } } static class tup implements Comparable<tup> { int a, b; tup() { } ; tup(int a, int b) { this.a = a; this.b = b; } @Override public int compareTo(tup o2) { return a==o2.a?Integer.compare(o2.b,b):Integer.compare(o2.a, a); } } static void shuffle(long[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); long temp = a[i]; a[i] = a[r]; a[r] = temp; } } static void shuffle(int[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); int temp = a[i]; a[i] = a[r]; a[r] = temp; } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
1311_F. Moving Points
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i. Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0. Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points. The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct. The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point. Output Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Examples Input 3 1 3 2 -100 2 3 Output 3 Input 5 2 1 4 3 5 2 2 2 3 4 Output 19 Input 2 2 1 -3 0 Output 0
{ "input": [ "3\n1 3 2\n-100 2 3\n", "2\n2 1\n-3 0\n", "5\n2 1 4 3 5\n2 2 2 3 4\n" ], "output": [ "3\n", "0\n", "19\n" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC optimize("no-stack-protector") using namespace std; const int inf = 1e9 + 7; const int max_n = 2e5 + 1; struct fenwick { vector<int> sum; fenwick(int n) { sum.clear(); sum.resize(n, 0); } int f(int v) { return (v & (v + 1)); } int h(int v) { return (v | (v + 1)); } void add(int v) { while (v < sum.size()) { sum[v]++; v = h(v); } } int get(int r) { int ans = 0; while (r >= 0) { ans += sum[r]; r = f(r) - 1; } return ans; } int get(int l, int r) { return get(r) - get(l - 1); } }; int n; pair<int, int> a[max_n]; int sq[max_n]; void scan() { cin >> n; for (int i = 0; i < n; i++) cin >> a[i].first; for (int i = 0; i < n; i++) cin >> a[i].second; sort(a, a + n); map<int, vector<int>> temp; for (int i = 0; i < n; i++) temp[a[i].second].emplace_back(i); int cnt = 0; for (auto i : temp) { for (int j : i.second) { sq[j] = cnt; cnt++; } } } void solve() { scan(); long long ans = 0; fenwick one = fenwick(n); for (int i = 0; i < n; i++) { ans += (long long)one.get(sq[i]) * a[i].first; one.add(sq[i]); } fenwick two = fenwick(n); for (int i = n - 1; i >= 0; i--) { ans -= (long long)two.get(sq[i], n - 1) * a[i].first; two.add(sq[i]); } cout << ans; } signed main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); 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 rmain = int; using namespace std; long long n; vector<pair<long long, long long>> points; vector<long long> v, cnt, xs; long long LSOne(long long k) { return (k & (-k)); } void update(vector<long long>& f, long long pos, long long val) { for (; pos <= n; pos += LSOne(pos)) f[pos] += val; } long long rsq(vector<long long>& f, long long pos) { long long sum = 0; for (; pos; pos -= LSOne(pos)) sum += f[pos]; return sum; } rmain main() { ios_base::sync_with_stdio(false); cin >> n; points.resize(n + 1); v.resize(n + 1); cnt.resize(n + 1); xs.resize(n + 1); for (long long i = 1; i <= n; ++i) cin >> points[i].first; for (long long i = 1; i <= n; ++i) { cin >> points[i].second; v[i] = points[i].second; } sort(points.begin() + 1, points.begin() + n + 1); sort(v.begin() + 1, v.begin() + n + 1); long long ans = 0; for (long long i = 1; i <= n; ++i) { long long pos = lower_bound(v.begin() + 1, v.begin() + n + 1, points[i].second) - v.begin(); ans += rsq(cnt, pos) * points[i].first - rsq(xs, pos); update(cnt, pos, 1); update(xs, pos, points[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 maxn = 2e5 + 5; const long long mod = 998244353; int T, n; struct node { long long d, v; friend bool operator<(node a, node b) { return a.d < b.d; } } a[maxn]; long long c[maxn], d[maxn]; int b[maxn]; void update(int x, int y) { for (int i = x; i <= n; i += (i & (-i))) c[i] += y, d[i]++; } long long query1(int x) { long long ans = 0; for (int i = x; i > 0; i -= (i & (-i))) ans += c[i]; return ans; } long long query2(int x) { long long ans = 0; for (int i = x; i > 0; i -= (i & (-i))) ans += d[i]; return ans; } int main() { cin >> n; for (int i = 1; i <= n; ++i) scanf("%lld", &a[i].d); for (int i = 1; i <= n; ++i) scanf("%lld", &a[i].v), b[i] = a[i].v; sort(b + 1, b + 1 + n); int m = unique(b + 1, b + 1 + n) - b - 1; for (int 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); long long ans = 0; for (int i = 1; i <= n; ++i) { ans += (query2(a[i].v) * a[i].d - query1(a[i].v)); update(a[i].v, a[i].d); } 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.util.*; public class Main { static FastScanner in = new FastScanner(); public static void main(String[] args) throws IOException { int n = in.nextInt(); point[] a = new point[n]; for (int i = 0; i < n; i++) { a[i] = new point(in.nextInt(), 0); } long ans = 0; for (int i = 0; i < n; i++) { a[i].v = in.nextInt(); } Arrays.sort(a, new point()); point[] b = new point[n]; for (int i = 0; i < n; ) { int j = i; while (i < n && a[i].v == a[j].v) { b[i] = new point(a[i].x, j); i++; } } Arrays.sort(b, new point1()); segment_tree st = new segment_tree(n, new int[n]); for (int i = 0; i < n; i++) { ans += st.get(1, 0, n - 1, 0, b[i].v) - st.get_cnt(1, 0, n - 1, 0, b[i].v) * b[i].x; st.set(1, 0, n - 1, b[i].v, b[i].x); } in.out.println(-ans); in.out.close(); } } class point implements Comparator<point> { int x, v; public point(int x, int v) { this.x = x; this.v = v; } public point() { } @Override public int compare(point o1, point o2) { return Integer.compare(o1.v, o2.v); } } class point1 implements Comparator<point> { @Override public int compare(point o1, point o2) { return Integer.compare(o1.x, o2.x); } } class segment_tree { int n; int[] a; long[] sum; int[] cnt; public segment_tree(int n, int[] a) { this.n = n; this.a = a; sum = new long[n * 4]; cnt = new int[n * 4]; } void set(int v, int tl, int tr, int ind, int val) { if (tl > ind || tr < ind) return; if (tl == tr) { sum[v] += val; cnt[v]++; return; } int tm = (tl + tr) >> 1; set((v << 1), tl, tm, ind, val); set((v << 1) | 1, tm + 1, tr, ind, val); sum[v] = sum[v << 1] + sum[(v << 1) | 1]; cnt[v] = cnt[v << 1] + cnt[(v << 1) | 1]; } long get(int v, int tl, int tr, int l, int r) { if (r < tl || l > tr) return 0; if (l <= tl && tr <= r) { return sum[v]; } int tm = (tl + tr) >> 1; return get(v << 1, tl, tm, l, r) + get((v << 1) | 1, tm + 1, tr, l, r); } long get_cnt(int v, int tl, int tr, int l, int r) { if (r < tl || l > tr) return 0; if (l <= tl && tr <= r) { return cnt[v]; } int tm = (tl + tr) >> 1; return get_cnt(v << 1, tl, tm, l, r) + get_cnt((v << 1) | 1, tm + 1, tr, l, r); } } class FastScanner { BufferedReader br; StringTokenizer st = new StringTokenizer(""); PrintWriter out; FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } FastScanner(String in, String out_) throws FileNotFoundException { br = new BufferedReader(new FileReader(in)); out = new PrintWriter(out_); } String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { 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.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class MovingPoints { static int[] pointsTree; static long[] distTree; static int[] velocity; static int[] distance; static Point[] points; static class FastScanner { StringTokenizer st; BufferedReader br; 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(); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int nextInt() { return Integer.parseInt(next()); } double nextDouble() { return Double.parseDouble(next()); } long nextLong() { return Long.parseLong(next()); } } static class Point { int distance; int velocity; int index; Point(int distance, int velocity, int index) { this.distance = distance; this.velocity = velocity; this.index = index; } } static class PointComparator implements Comparator<Point> { @Override public int compare(Point a, Point b) { if(a.velocity != b.velocity) { return (a.velocity < b.velocity) ? -1 : 1; } if(a.distance != b.distance) { return (a.distance < b.distance) ? -1 : 1; } if(a.index != b.index) { return (a.index < b.index) ? -1 : 1; } return 0; } } static void updateIndex(int index, int dist, int n) { while(index <= n) { distTree[index] += dist; pointsTree[index] += 1; index += (index & (-index)); } } static long solve(int index, long dist) { int x = index; int nPoints = 0; while(x > 0) { nPoints += pointsTree[x]; x -= (x & (-x)); } long prefDist = 0; x = index; while(x > 0) { prefDist += distTree[x]; x -= (x & (-x)); } dist = (long) nPoints * dist; dist -= prefDist; return dist; } public static void main(String[] Args) { FastScanner sc = new FastScanner(); int n; n = sc.nextInt(); distTree = new long[n + 1]; pointsTree = new int[n + 1]; velocity = new int[n + 1]; distance = new int[n + 1]; points = new Point[n]; ArrayList<Integer> distances = new ArrayList<>(); TreeMap<Integer, Integer> treeMap = new TreeMap(); for(int i = 0; i < n; i++) { distance[i] = sc.nextInt(); distances.add(distance[i]); } Collections.sort(distances); int x = 0; for(int i = 0; i < distances.size(); i++) { if(i == 0 || distances.get(i) != distances.get(i - 1)) { treeMap.put(distances.get(i), ++x); } } for(int i = 0; i < n; i++) { velocity[i] = sc.nextInt(); } for(int i = 0; i < n; i++) { points[i] = new Point(distance[i], velocity[i], treeMap.get(distance[i])); } Arrays.sort(points, new PointComparator()); long sol = 0L; for(Point point: points) { sol += solve(point.index, point.distance); updateIndex(point.index, point.distance, treeMap.size()); } System.out.println(sol); } }
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]; long long cnt[N << 2], val[N << 2]; void update(int a, int b, int l, int r, int rt, int v) { if (a <= l && b >= r) { val[rt] += v, cnt[rt]++; return; } int mid = l + r >> 1; if (a <= mid) update(a, b, l, mid, rt << 1, v); if (b > mid) update(a, b, mid + 1, r, rt << 1 | 1, v); cnt[rt] = cnt[rt << 1] + cnt[rt << 1 | 1]; val[rt] = val[rt << 1] + val[rt << 1 | 1]; } pair<long long, long long> query(int a, int b, int l, int r, int rt) { if (a <= l && b >= r) { return {val[rt], cnt[rt]}; } int mid = l + r >> 1; pair<long long, long long> ans = {0, 0}, t = {0, 0}, t2 = {0, 0}; if (a <= mid) t = query(a, b, l, mid, rt << 1); if (b > mid) t2 = query(a, b, mid + 1, r, rt << 1 | 1); ans.first = t.first + t2.first; ans.second = t.second + t2.second; return ans; } 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() + 1; } long long ans = 0; for (int i = n - 1; ~i; --i) { update(p[i].second, p[i].second, 1, 200000, 1, p[i].first); auto t = query(p[i].second, 200000, 1, 200000, 1); ans += t.first - t.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
python3
def add(sms, cns, n, v, x): while v < n: sms[v] += x cns[v] += 1 v += v & ~(v - 1) def sumtoo(sms, cns, v): sm = 0 cn = 0 while v > 0: sm += sms[v] cn += cns[v] v -= v & ~(v - 1) return sm,cn def go(): n = int(input()) x = list(map(int, input().split())) v = list(map(int, input().split())) sv = sorted(set(v)) sv = {v: k for (k, v) in enumerate(sv,1)} v = [sv[v] for v in v] # mnv, mxv = min(v), max(v) # dfv = mxv - mnv a = sorted(((xx, vv) for xx, vv in zip(x, v)), reverse=True) len_sv = len(sv)+1 sms = [0] * len_sv cns = [0] * len_sv res = 0 sx = 0 for cnt, (xx, vv) in enumerate(a): sm, cn = sumtoo(sms,cns,vv-1) # sm, cn = tree.sum_to(vv - mnv) sm, cn = sx - sm, cnt - cn res += sm - xx * cn add(sms, cns, len_sv, vv, xx) # tree.add(vv - mnv, xx) sx += xx return res print(go())
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 fenwick { long long N; vector<long long> Bit; fenwick(long long n = 1e5) { N = n; n += 5; Bit.resize(n, 0); } void update(long long id, long long val) { while (id <= N) { Bit[id] += val; id += id & -id; } } long long query(long long id) { long long res = 0; while (id) { res += Bit[id]; id -= id & -id; } return res; } long long query(long long l, long long r) { return query(r) - query(l - 1); } }; signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); long long n; cin >> n; vector<pair<long long, long long> > a(n); for (long long i = (0); i < (n); i++) cin >> a[i].first; for (long long i = (0); i < (n); i++) cin >> a[i].second; sort(a.begin(), a.end()); map<long long, long long> cmp; for (long long i = (0); i < (n); i++) cmp[a[i].second]; long long c = 1; for (auto &i : cmp) i.second = c++; for (long long i = (0); i < (n); i++) a[i].second = cmp[a[i].second]; fenwick f1(c + 100), f2(c + 100); long long ans = 0; for (long long i = (n - 1); i > (-1); i--) { ans += f1.query(a[i].second, c + 5); ans -= f2.query(a[i].second, c + 5) * a[i].first; f1.update(a[i].second, a[i].first); f2.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 int N = 2e5 + 7; const int MOD = 1e9 + 7; template <class T> bool umin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } template <class T> bool umax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool umod(T& a) { while (a < 0) a += MOD; a %= MOD; return 1; } int n, cn, a[N], b[N], c[N]; long long T[N << 2], S[N << 2], ans; map<int, int> mk; pair<int, int> p[N]; void upd(int x, int val, int l, int r, int v) { S[v]++, T[v] += val; if (l == r) return; if (x <= ((l + r) >> 1)) upd(x, val, l, ((l + r) >> 1), v << 1); else upd(x, val, ((l + r) >> 1) + 1, r, v << 1 | 1); } long long getT(int x, int y, int l, int r, int v) { if (y < l || r < x) return 0LL; if (x <= l && r <= y) return T[v]; return getT(x, y, l, ((l + r) >> 1), v << 1) + getT(x, y, ((l + r) >> 1) + 1, r, v << 1 | 1); } long long getS(int x, int y, int l, int r, int v) { if (y < l || r < x) return 0LL; if (x <= l && r <= y) return S[v]; return getS(x, y, l, ((l + r) >> 1), v << 1) + getS(x, y, ((l + r) >> 1) + 1, r, v << 1 | 1); } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", a + i); for (int i = 1; i <= n; i++) scanf("%d", b + i), mk[b[i]] = 1; for (__typeof(mk.begin()) i = (mk.begin()); i != (mk.end()); i++) i->second = ++cn; for (int i = 1; i <= n; i++) { c[i] = mk[b[i]]; p[i] = {a[i], i}; } sort(p + 1, p + n + 1); for (int i = 1; i <= n; i++) { long long s, t; ans += (1LL * (s = getS(1, c[p[i].second], 1, cn, 1)) * p[i].first) - (t = getT(1, c[p[i].second], 1, cn, 1)); upd(c[p[i].second], p[i].first, 1, cn, 1); } 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 FastInputOutput() { ios_base ::sync_with_stdio(0); cin.tie(0); cout.tie(0); } inline int D() { int t; scanf("%d", &t); return t; } inline long long LLD() { long long t; scanf("%lld", &t); return t; } long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } long long power(long long x, long long p, long long m) { long long res = 1; while (p) { if (p & 1) res = (res * x) % m; x = ((x % m) * (x % m)) % m; p >>= 1ll; } return res; } long double ModLog(long double base, long double x) { return (logl(x) / logl(base)); } int mod(int a, int m) { return (((a % m) + m) % m); } const int N = 2e5 + 5, MOD = 998244353; struct BIT { int n; vector<long long> sum; BIT(int n) { this->n = n; sum.resize(n + 5); for (int i = 1; i < n; ++i) sum[i] = 0; } long long get(int r) { long long ret = 0; while (r) { ret += sum[r]; r -= (r & -r); } return ret; } long long get(int l, int r) { return get(r) - get(l - 1); } void update(int l, long long x) { while (l < n) { sum[l] += x; l += (l & -l); } } }; int id[N], x[N], v[N]; bool cmp(int s, int f) { return x[f] > x[s]; } void solve() { int n = D(); for (int i = 0; i < n; ++i) { id[i] = i; x[i] = D(); } vector<int> compress(n); for (int i = 0; i < n; ++i) { v[i] = D(); compress[i] = v[i]; } sort(id, id + n, cmp); sort(compress.begin(), compress.end()); compress.erase(unique(compress.begin(), compress.end()), compress.end()); for (int i = 0, idx; i < n; ++i) { idx = id[i]; v[idx] = lower_bound(compress.begin(), compress.end(), v[idx]) - compress.begin(); ++v[idx]; } BIT inv(n + 1), sum(n + 1); long long ans = 0; for (int i = 0, idx; i < n; ++i) { idx = id[i]; long long cnt = inv.get(v[idx]); long long curr = cnt * x[idx] - sum.get(v[idx]); ans += curr; inv.update(v[idx], 1); sum.update(v[idx], x[idx]); } printf("%lld\n", ans); } int main() { int t; t = 1; while (t--) { 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
java
import java.util.*; import java.io.*; public class Main { static boolean DEBUG; public static void main(String[] args) { DEBUG = args.length > 0 && args[0].equals("-DEBUG"); Solver solver = new Solver(); solver.solve(); solver.exit(); } static class FastScanner { private final InputStream in = System.in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private boolean hasNextByte() { if (ptr < buflen) { return true; }else{ ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;} private boolean isPrintableChar(int c) { return 33 <= c && c <= 126;} private void skipUnprintable() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;} public boolean hasNext() { skipUnprintable(); return hasNextByte();} public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while(isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while(true){ if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; }else if(b == -1 || !isPrintableChar(b)){ return minus ? -n : n; }else{ throw new NumberFormatException(); } b = readByte(); } } } static class Solver { FastScanner sc = new FastScanner(); public Solver() { } String ns() { return sc.next(); } String[] ns(int n) { String a[] = new String[n]; for(int i = 0; i < n; i ++) { a[i] = ns(); } return a; } String[][] ns(int n, int m) { String a[][] = new String[n][m]; for(int i = 0; i < n; i ++) { a[i] = ns(m); } return a; } char[] nc(int n) { String str = ns(); char a[] = new char[str.length()]; for(int i = 0; i < str.length(); i ++) { a[i] = str.charAt(i); } return a; } char[][] nc(int n, int m) { char a[][] = new char[n][m]; for(int i = 0; i < n; i ++) { a[i] = nc(m); } return a; } boolean[] nb(int n, char t) { boolean a[] = new boolean[n]; char c[] = nc(n); for(int i = 0; i < n; i ++) { a[i] = c[i] == t; } return a; } boolean[][] nb(int n, int m, char t) { boolean a[][] = new boolean[n][m]; for(int i = 0; i < n; i ++) { a[i] = nb(m, t); } return a; } int ni() { return (int)sc.nextLong(); } int[] ni(int n) { int a[] = new int[n]; for(int i = 0; i < n; i ++) { a[i] = ni(); } return a; } int[][] ni(int n, int m) { int a[][] = new int[n][m]; for(int i = 0; i < n; i ++) { a[i] = ni(m); } return a; } long nl() { return sc.nextLong(); } long[] nl(int n) { long a[] = new long[n]; for(int i = 0; i < n; i ++) { a[i] = nl(); } return a; } long[][] nl(int n, int m) { long a[][] = new long[n][m]; for(int i = 0; i < n; i ++) { a[i] = nl(m); } return a; } double nd() { return Double.parseDouble(sc.next()); } double[] nd(int n) { double a[] = new double[n]; for(int i = 0; i < n; i ++) { a[i] = nd(); } return a; } double[][] nd(int n, int m) { double a[][] = new double[n][m]; for(int i = 0; i < n; i ++) { a[i] = nd(m); } return a; } PrintWriter out = new PrintWriter(System.out); PrintWriter err = new PrintWriter(System.err); void prt() { out.print(""); } <T> void prt(T a) { out.print(a); } void prtln() { out.println(""); } <T> void prtln(T a) { out.println(a); } void prtln(int... a) { StringBuilder sb = new StringBuilder(); for(int element : a){ sb.append(element+" "); } prtln(sb.toString().trim()); } void prtln(long... a) { StringBuilder sb = new StringBuilder(); for(long element : a){ sb.append(element+" "); } prtln(sb.toString().trim()); } void prtln(double... a) { StringBuilder sb = new StringBuilder(); for(double element : a){ sb.append(element+" "); } prtln(sb.toString().trim()); } void prtln(String... a) { StringBuilder sb = new StringBuilder(); for(String element : a){ sb.append(element+" "); } prtln(sb.toString().trim()); } void prtln(char... a) { StringBuilder sb = new StringBuilder(); for(char element : a){ sb.append(element); } prtln(sb.toString().trim()); } void prtln(int[][] a) { for(int[] element : a){ prtln(element); } } void prtln(long[][] a) { for(long[] element : a){ prtln(element); } } void prtln(double[][] a) { for(double[] element : a){ prtln(element); } } void prtln(String[][] a) { for(String[] element : a){ prtln(element); } } void prtln(char[][] a) { for(char[] element : a){ prtln(element); } } String errconvert(int a) { return isINF(a) ? "_" : String.valueOf(a); } String errconvert(long a) { return isINF(a) ? "_" : String.valueOf(a); } void errprt(int a) { if(DEBUG) { err.print(errconvert(a)); } } void errprt(long a) { if(DEBUG) { err.print(errconvert(a)); } } void errprt() { if(DEBUG) { err.print(""); } } <T> void errprt(T a) { if(DEBUG) { err.print(a); } } void errprt(boolean a) { if(DEBUG) { errprt(a ? "#" : "."); } } void errprtln() { if(DEBUG) { err.println(""); } } void errprtln(int a) { if(DEBUG) { err.println(errconvert(a)); } } void errprtln(long a) { if(DEBUG) { err.println(errconvert(a)); } } <T> void errprtln(T a) { if(DEBUG) { err.println(a); } } void errprtln(boolean a) { if(DEBUG) { errprtln(a ? "#" : "."); } } void errprtln(int... a) { if(DEBUG) { StringBuilder sb = new StringBuilder(); for(int element : a){ sb.append(errconvert(element)+" "); } errprtln(sb.toString().trim()); } } void errprtln(long... a) { if(DEBUG) { StringBuilder sb = new StringBuilder(); for(long element : a){ sb.append(errconvert(element)+" "); } errprtln(sb.toString().trim()); } } void errprtln(double... a) { if(DEBUG) { StringBuilder sb = new StringBuilder(); for(double element : a){ sb.append(element+" "); } errprtln(sb.toString().trim()); } } void errprtln(String... a) { if(DEBUG) { StringBuilder sb = new StringBuilder(); for(String element : a){ sb.append(element+" "); } errprtln(sb.toString().trim()); } } void errprtln(char... a) { if(DEBUG) { StringBuilder sb = new StringBuilder(); for(char element : a){ sb.append(element); } errprtln(sb.toString().trim()); } } void errprtln(boolean... a) { if(DEBUG) { StringBuilder sb = new StringBuilder(); for(boolean element : a){ sb.append((element ? "#" : ".")+" "); } errprtln(sb.toString().trim()); } } void errprtln(int[][] a) { if(DEBUG) { for(int[] element : a){ errprtln(element); } } } void errprtln(long[][] a) { if(DEBUG) { for(long[] element : a){ errprtln(element); } } } void errprtln(double[][] a) { if(DEBUG) { for(double[] element : a){ errprtln(element); } } } void errprtln(String[][] a) { if(DEBUG) { for(String[] element : a){ errprtln(element); } } } void errprtln(char[][] a) { if(DEBUG) { for(char[] element : a){ errprtln(element); } } } void errprtln(boolean[][] a) { if(DEBUG) { for(boolean[] element : a){ errprtln(element); } } } void reply(boolean b) { prtln(b ? "Yes" : "No"); } void REPLY(boolean b) { prtln(b ? "YES" : "NO"); } void flush() { out.flush(); if(DEBUG) { err.flush(); } } void exit() { flush(); System.exit(0); } int min(int a, int b) { return Math.min(a, b); } long min(long a, long b) { return Math.min(a, b); } double min(double a, double b) { return Math.min(a, b); } int min(int... x) { int min = x[0]; for(int val : x) { min = min(min, val); } return min; } long min(long... x) { long min = x[0]; for(long val : x) { min = min(min, val); } return min; } double min(double... x) { double min = x[0]; for(double val : x) { min = min(min, val); } return min; } int max(int a, int b) { return Math.max(a, b); } long max(long a, long b) { return Math.max(a, b); } double max(double a, double b) { return Math.max(a, b); } int max(int... x) { int max = x[0]; for(int val : x) { max = max(max, val); } return max; } long max(long... x) { long max = x[0]; for(long val : x) { max = max(max, val); } return max; } double max(double... x) { double max = x[0]; for(double val : x) { max = max(max, val); } return max; } long sum(int... a) { long sum = 0; for(int element : a) { sum += element; } return sum; } long sum(long... a) { long sum = 0; for(long element : a) { sum += element; } return sum; } double sum(double... a) { double sum = 0; for(double element : a) { sum += element; } return sum; } long[] sums(int[] a) { long sum[] = new long[a.length + 1]; sum[0] = 0; for(int i = 0; i < a.length; i ++) { sum[i + 1] = sum[i] + a[i]; } return sum; } long[] sums(long[] a) { long sum[] = new long[a.length + 1]; sum[0] = 0; for(int i = 0; i < a.length; i ++) { sum[i + 1] = sum[i] + a[i]; } return sum; } double[] sums(double[] a) { double sum[] = new double[a.length + 1]; sum[0] = 0; for(int i = 0; i < a.length; i ++) { sum[i + 1] = sum[i] + a[i]; } return sum; } long abs(double x) { return (long)Math.abs(x); } long round(double x) { return Math.round(x); } long floor(double x) { return (long)Math.floor(x); } long ceil(double x) { return (long)Math.ceil(x); } double sqrt(double x) { return Math.sqrt(x); } double pow(double x, double y) { return Math.pow(x, y); } long pow(long x, long y) { return (long)Math.pow(x, y); } int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } long lcm(long a, long b) { return a * b / gcd(a, b); } int upperToInt(char a) { return a - 'A'; } int lowerToInt(char a) { return a - 'a'; } int numToInt(char a) { return a - '0'; } int charToInt(char a) { return a >= 'a' ? lowerToInt(a) : a >= 'A' ? upperToInt(a) : numToInt(a); } char intToUpper(int a) { return (char)(a + 'A'); } char intToLower(int a) { return (char)(a + 'a'); } char intToNum(int a) { return (char)(a + '0'); } long[] div(long a) { List<Long> divList = new ArrayList<Long>(); for(long i = 1; i * i <= a; i ++) { if(a % i == 0) { divList.add(i); if(i * i != a) { divList.add(a / i); }; } } long div[] = new long[divList.size()]; for(int i = 0; i < divList.size(); i ++) { div[i] = divList.get(i); } return div; } long[][] factor(long a) { List<Long> factorList = new ArrayList<Long>(); List<Long> degreeList = new ArrayList<Long>(); for(long i = 2; i * i <= a; i ++) { if(a % i == 0) { long count = 0; while(a % i == 0) { a /= i; count ++; } factorList.add(i); degreeList.add(count); } } if(a > 1) { factorList.add(a); degreeList.add(1L); } long factor[][] = new long[factorList.size()][2]; for(int i = 0; i < factorList.size(); i ++) { factor[i][0] = factorList.get(i); factor[i][1] = degreeList.get(i); } return factor; } long[][] countElements(long[] a, boolean sort) { int len = a.length; long array[] = new long[len]; for(int i = 0; i < len; i ++) { array[i] = a[i]; } if(sort) { Arrays.sort(array); } List<Long> elem = new ArrayList<Long>(); List<Long> cnt = new ArrayList<Long>(); long tmp = 1; for(int i = 1; i <= len; i ++) { if(i == len || array[i] != array[i - 1]) { elem.add(array[i - 1]); cnt.add(tmp); tmp = 1; }else { tmp ++; } } long counts[][] = new long[elem.size()][2]; for(int i = 0; i < elem.size(); i ++) { counts[i][0] = elem.get(i); counts[i][1] = cnt.get(i); } return counts; } long[][] countElements(String str, boolean sort) { int len = str.length(); char array[] = str.toCharArray(); if(sort) { Arrays.sort(array); } List<Long> elem = new ArrayList<Long>(); List<Long> cnt = new ArrayList<Long>(); long tmp = 1; for(int i = 1; i <= len; i ++) { if(i == len || array[i] != array[i - 1]) { elem.add((long)array[i - 1]); cnt.add(tmp); tmp = 1; }else { tmp ++; } } long counts[][] = new long[elem.size()][2]; for(int i = 0; i < elem.size(); i ++) { counts[i][0] = elem.get(i); counts[i][1] = cnt.get(i); } return counts; } int numDigits(long a) { return Long.toString(a).length(); } long bitFlag(int a) { return 1L << (long)a; } boolean isFlagged(long x, int a) { return (x & bitFlag(a)) != 0; } long countString(String str, String a) { return (str.length() - str.replace(a, "").length()) / a.length(); } long countStringAll(String str, String a) { return str.length() - str.replaceAll(a, "").length(); } void reverse(String array[]) { String reversed[] = new String[array.length]; for(int i = 0; i < array.length; i ++) { reversed[array.length - i - 1] = array[i]; } for(int i = 0; i < array.length; i ++) { array[i] = reversed[i]; } } void reverse(int array[]) { int reversed[] = new int[array.length]; for(int i = 0; i < array.length; i ++) { reversed[array.length - i - 1] = array[i]; } for(int i = 0; i < array.length; i ++) { array[i] = reversed[i]; } } void reverse(long array[]) { long reversed[] = new long[array.length]; for(int i = 0; i < array.length; i ++) { reversed[array.length - i - 1] = array[i]; } for(int i = 0; i < array.length; i ++) { array[i] = reversed[i]; } } void reverse(double array[]) { double reversed[] = new double[array.length]; for(int i = 0; i < array.length; i ++) { reversed[array.length - i - 1] = array[i]; } for(int i = 0; i < array.length; i ++) { array[i] = reversed[i]; } } void reverse(boolean array[]) { boolean reversed[] = new boolean[array.length]; for(int i = 0; i < array.length; i ++) { reversed[array.length - i - 1] = array[i]; } for(int i = 0; i < array.length; i ++) { array[i] = reversed[i]; } } void fill(int array[], int x) { Arrays.fill(array, x); } void fill(long array[], long x) { Arrays.fill(array, x); } void fill(double array[], double x) { Arrays.fill(array, x); } void fill(boolean array[], boolean x) { Arrays.fill(array, x); } void fill(int array[][], int x) { for(int a[] : array) { fill(a, x); } } void fill(long array[][], long x) { for(long a[] : array) { fill(a, x); } } void fill(double array[][], double x) { for(double a[] : array) { fill(a, x); } } void fill(boolean array[][], boolean x) { for(boolean a[] : array) { fill(a, x); } } void fill(int array[][][], int x) { for(int a[][] : array) { fill(a, x); } } void fill(long array[][][], long x) { for(long a[][] : array) { fill(a, x); } } void fill(double array[][][], double x) { for(double a[][] : array) { fill(a, x); } } void fill(boolean array[][][], boolean x) { for(boolean a[][] : array) { fill(a, x); } } long INF = (long)1e18 + 7; boolean isINF(long a) { return abs(a) > INF / 1000; } boolean isPlusINF(long a) { return a > 0 && isINF(a); } boolean isMinusINF(long a) { return isPlusINF(- a); } int I_INF = (int)1e9 + 7; boolean isINF(int a) { return abs(a) > I_INF / 1000; } boolean isPlusINF(int a) { return a > 0 && isINF(a); } boolean isMinusINF(int a) { return isPlusINF(- a); } // mods long MOD = (long)1e9 + 7; // 998244353; public long mod(long i) { i %= MOD; return i + (i < 0 ? MOD : 0); } long pow_m(long x, long y) { if (y == 0) { return 1; }else { long tmp = pow_m(x, y / 2); return mod(mod(tmp * tmp) * (y % 2 == 0 ? 1 : x)); } } long[] pows_m(long x, int max) { long pow[] = new long[max + 1]; pow[0] = 1; for(int i = 0; i < max; i ++) { pow[i + 1] = mod(pow[i] * x); } return pow; } int MAX_INV_SIZE = 100_100; HashMap<Long, Long> invMap = new HashMap<>(); long inv(long x) { x = mod(x); if(invMap.containsKey(x)) { return invMap.get(x); } if(invMap.size() >= MAX_INV_SIZE) { return calInv(x); } invMap.put(x, calInv(x)); return invMap.get(x); } long calInv(long x) { return pow_m(x, MOD - 2); } int MAX_FACT = 5_000_100; long fact[]; long invFact[]; boolean isFactPrepared = false; HashMap<Integer, long[]> factMap; void prepareFact() { fact = new long[MAX_FACT]; Arrays.fill(fact, 0); invFact = new long[MAX_FACT]; Arrays.fill(invFact, 0); fact[0] = 1; int maxIndex = min(MAX_FACT, (int)MOD); for(int i = 1; i < maxIndex; i ++) { fact[i] = mod(fact[i - 1] * i); } invFact[maxIndex - 1] = inv(fact[maxIndex - 1]); for(int i = maxIndex - 1; i > 0; i --) { invFact[i - 1] = mod(invFact[i] * i); } factMap = new HashMap<>(); isFactPrepared = true; } long P(int n, int r) { if(!isFactPrepared) { prepareFact(); } if(n < 0 || r < 0 || n < r) { return 0; } if(n >= MAX_FACT) { if(!factMap.containsKey(n)) { long largeFact[] = new long[MAX_FACT]; factMap.put(n, largeFact); fill(largeFact, -INF); largeFact[0] = 1; } long largeFact[] = factMap.get(n); int i = r; while(isINF(largeFact[i])) { i --; } for(; i < r; i ++) { largeFact[i + 1] = mod(largeFact[i] * (n - i)); } return largeFact[r]; } return mod(fact[n] * invFact[n - r]); } long C(int n, int r) { if(!isFactPrepared) { prepareFact(); } if(n < 0 || r < 0 || n < r) { return 0; } return mod(P(n, r) * invFact[r]); } long H(int n, int r) { return C((n - 1) + r, r); } // grid class Grid implements Comparable<Grid> { int x; int y; int h; int w; int i; boolean b; long val; Grid() { } Grid(int x, int y, int h, int w) { init(x, y, h, w, false, 0); } Grid(int x, int y, int h, int w, boolean b) { init(x, y, h, w, b, 0); } Grid(int x, int y, int h, int w, long val) { init(x, y, h, w, false, val); } Grid(int x, int y, int h, int w, boolean b, long val) { init(x, y, h, w, b, val); } void init(int x, int y, int h, int w, boolean b, long val) { this.x = x; this.y = y; this.h = h; this.w = w; this.b = b; this.val = val; i = x * w + y; } @Override public int compareTo(Grid g) { return Long.compare(this.val, g.val); } } int dx[] = {-1, 1, 0, 0}; int dy[] = {0, 0, -1, 1}; // int dx[] = {-1, 0, 1, -1, 1, -1, 0, 1}; // int dy[] = {-1, -1, -1, 0, 0, 1, 1, 1}; boolean isValid(int x, int y, int h, int w) { return x >= 0 && x < h && y >= 0 && y < w; } boolean isValid(Grid g) { return isValid(g.x, g.y, g.h, g.w); } Grid next(Grid g, int i) { return new Grid(g.x + dx[i], g.y + dy[i], g.h, g.w); } // graph class Graph { int numNode; int numEdge; boolean directed; Edge edges[]; Node nodes[]; Node reversedNodes[]; Graph(int numNode, int numEdge, Edge edges[], boolean directed) { this.numNode = numNode; this.numEdge = numEdge; this.directed = directed; this.edges = edges; nodes = new Node[numNode]; reversedNodes = new Node[numNode]; for(int i = 0; i < numNode; i ++) { nodes[i] = new Node(i); reversedNodes[i] = new Node(i); } for(Edge edge : edges) { nodes[edge.source].add(edge.target, edge.cost); if(directed) { reversedNodes[edge.target].add(edge.source, edge.cost); }else { nodes[edge.target].add(edge.source, edge.cost); } } } void clearNodes() { for(Node n : nodes) { n.clear(); } for(Node n : reversedNodes) { n.clear(); } } } class Node { int id; ArrayList<Edge> edges = new ArrayList<Edge>(); Node(int id) { this.id = id; } void add(int target, long cost) { edges.add(new Edge(id, target, cost)); } void clear() { edges.clear(); } } class Edge implements Comparable<Edge> { int source; int target; long cost; Edge(int source, int target, long cost) { this.source = source; this.target = target; this.cost = cost; } @Override public int compareTo(Edge e) { return Long.compare(this.cost, e.cost); } } public void solve() { int num = ni(); long a[][] = nl(2, num); Long a2[][] = new Long[num][4]; for(int i = 0; i < num; i ++) { a2[i][0] = a[0][i]; a2[i][1] = a[1][i]; a2[i][2] = 0l; } Arrays.sort(a2, (sort1, sort2) -> Long.compare(sort1[1], sort2[1])); int crt = 0; for(int i = 1; i < num; i ++) { if(a2[i][1] > a2[i - 1][1]) { crt ++; } a2[i][2] = (long)crt; } crt ++; Arrays.sort(a2, (sort1, sort2) -> Long.compare(sort1[0], sort2[0])); long a3[][] = new long[num][2]; for(int i = 0; i < num; i ++) { a3[i][0] = a2[i][0]; a3[i][1] = a2[i][2]; } int b[][] = new int[num][2]; for(int i = 0; i < num; i ++) { b[i][0] = (int)a3[i][0]; b[i][1] = (int)a3[i][1]; } // errprtln(b); SegTree st = new SegTree(crt); SegTree st2 = new SegTree(crt); long ans = 0; for(int i = 0; i < num; i ++) { ans += st.find(0, b[i][1] + 1) * b[i][0] - st2.find(0, b[i][1] + 1); st.set(b[i][1], st.get(b[i][1]) + 1); st2.set(b[i][1], st2.get(b[i][1]) + b[i][0]); // errprtln(st2.nodes); } prtln(ans); } class SegTree { long e = 0; long f(long a, long b) { return sum(a, b); } int n; long nodes[]; SegTree(long[] a) { init(a); } SegTree(int len, long x) { long a[] = new long[len]; fill(a, x); init(a); } SegTree(int len) { init(len); } void init(long[] a) { init(a.length); for(int i = 0; i < a.length; i ++) { nodes[i + n - 1] = a[i]; } for(int i = n - 2; i >= 0; i --) { nodes[i] = f(nodes[i * 2 + 1], nodes[i * 2 + 2]); } } void init(int len) { n = 1; while(n < len) { n *= 2; } nodes = new long[n * 2 - 1]; fill(nodes, e); for(int i = n - 2; i >= 0; i --) { nodes[i] = f(nodes[i * 2 + 1], nodes[i * 2 + 2]); } } void set(int i, long val) { i += n - 1; nodes[i] = val; while(i > 0) { i = (i - 1) / 2; nodes[i] = f(nodes[i * 2 + 1], nodes[i * 2 + 2]); } } long get(int i) { return find(i, i + 1); } long find(int a, int b) { return find(a, b, 0, 0, n); } long find(int a, int b, int k, int l, int r) { if(b <= l || r <= a) { return e; } if(a <= l && r <= b) { return nodes[k]; } return f(find(a, b, k * 2 + 1, l, (l + r) / 2), find(a, b, k * 2 + 2, (l + r) / 2, r)); } } } }
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 kMaxN = 200001; struct E { int x, v, w; bool operator<(const E &e) const { return x < e.x; } } e[kMaxN]; struct T { long long c, v; } t[kMaxN]; int a[kMaxN]; int n; long long ans; int LowBit(int x) { return x & (-x); } void Calc(int x) { t[0] = {0, 0}; while (x) { t[0].c += t[x].c; t[0].v += t[x].v; x -= LowBit(x); } } void Add(int x, int v) { while (x <= n) { t[x].c++; t[x].v += v; x += LowBit(x); } } void Dis() { sort(a + 1, a + 1 + n, [](int i, int j) { return e[i].v < e[j].v; }); e[0].v = 1 << 31; for (int i = 1, j = 0; i <= n; i++) { j += e[a[i]].v != e[a[i - 1]].v; e[a[i]].w = j; } } int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> e[i].x; } for (int i = 1; i <= n; i++) { cin >> e[i].v; a[i] = i; } Dis(); sort(e + 1, e + 1 + n); for (int i = 1; i <= n; i++) { Calc(e[i].w); ans += e[i].x * t[0].c - t[0].v; Add(e[i].w, e[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 int MAXN = 200200; int n; long long ans, bit[2][MAXN]; pair<int, int> a[MAXN]; void compress() { map<int, int> m; for (int i = 1; i <= n; ++i) m[a[i].second]; int cnt = 1; for (auto& [key, val] : m) val = cnt++; for (int i = 1; i <= n; ++i) a[i].second = m[a[i].second]; } void add(int b, int pos, int val) { for (; 0 < pos && pos < MAXN; pos += pos & -pos) bit[b][pos] += val; } long long get(int b, int pos) { long long ret = 0; for (; 0 < pos && pos < MAXN; pos -= pos & -pos) ret += bit[b][pos]; return ret; } int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", &a[i].first); for (int i = 1; i <= n; ++i) scanf("%d", &a[i].second); sort(a + 1, a + n + 1); compress(); for (int i = 1; i <= n; ++i) { auto [x, v] = a[i]; ans += 1LL * x * get(0, v) - get(1, v); add(1, v, x); add(0, v, 1); } 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
java
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.TreeMap; import java.util.InputMismatchException; import java.io.IOException; import java.util.Comparator; import java.util.TreeSet; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author KharYusuf */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(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, FastReader s, PrintWriter w) { int n = s.nextInt(), MX = (int) 1e8; int[][] x = new int[n][2]; for (int i = 0; i < n; i++) x[i][0] = s.nextInt(); TreeSet<Integer> p = new TreeSet<>(), ne = new TreeSet<>(); TreeMap<Integer, Integer> po = new TreeMap<>(), nee = new TreeMap<>(); for (int i = 0; i < n; i++) { x[i][1] = s.nextInt(); if (x[i][1] > 0) p.add(x[i][1]); else if (x[i][1] < 0) ne.add(x[i][1] + MX); } int cnt = 0; for (int i : p) po.put(i, cnt++); cnt = 0; for (int i : ne) nee.put(i, cnt++); func.sortbyColumn(x, 0); long ans = 0, cntneg = 0, cntz = 0, cneg = 0, cz = 0, last = 0; bit pos = new bit(n), neg = new bit(n); bit poscnt = new bit(p.size()), negcnt = new bit(ne.size()); for (int i = 0; i < n; i++) { cneg += (x[i][0] - last) * cntneg; cz += (x[i][0] - last) * cntz; last = x[i][0]; if (x[i][1] > 0) { ans += cneg; ans += cz; int cur = po.get(x[i][1]); ans += poscnt.query(cur) * (long) x[i][0] - pos.query(cur); poscnt.modify(cur, 1); pos.modify(cur, x[i][0]); } else if (x[i][1] < 0) { cntneg++; int v = nee.get(MX + x[i][1]); ans += negcnt.query(v) * (long) x[i][0] - neg.query(v); negcnt.modify(v, 1); neg.modify(v, x[i][0]); } else { cntz++; ans += cneg; ans += cz; } } w.println(ans); } public class bit { public int n; public long[] t; public bit(int n) { t = new long[n]; this.n = n; } public void modify(int ind, long val) { for (; ind < n; ind = ind | (++ind)) t[ind] += val; } public long query(int ind) { long sum = 0; for (; ind >= 0; ind = (ind & (++ind)) - 1) sum += t[ind]; return sum; } } } static class FastReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private FastReader.SpaceCharFilter filter; public FastReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } 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 boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class func { public static void sortbyColumn(int[][] arr, final int col) { Arrays.sort(arr, new Comparator<int[]>() { public int compare(final int[] entry1, final int[] entry2) { if (entry1[col] > entry2[col]) return 1; if (entry1[col] < entry2[col]) return -1; 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 Pt { int x, v; bool operator<(const Pt &a) const { return x < a.x; } }; struct BIT { vector<long long> bit, cnt; int n; BIT(int sz) { bit.resize(sz + 1); cnt.resize(sz + 1); n = sz; } void add(int x, long long v) { while (x <= n) { cnt[x]++; bit[x] += v; x += x & (-x); } } pair<long long, long long> ask(int x) { pair<long long, long long> ret(0, 0); while (x) { ret.first += bit[x]; ret.second += cnt[x]; x -= x & (-x); } return ret; } }; vector<int> V; Pt arr[200005]; int n; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &arr[i].x); } for (int i = 0; i < n; i++) { scanf("%d", &arr[i].v); V.push_back(arr[i].v); } long long ans = 0; sort(arr, arr + n); sort(begin(V), end(V)); V.erase(unique(begin(V), end(V)), end(V)); BIT loli(V.size()); for (int i = 0; i < n; i++) { int p = lower_bound(begin(V), end(V), arr[i].v) - begin(V) + 1; auto val = loli.ask(p); ans = ans + arr[i].x * val.second - val.first; loli.add(p, arr[i].x); } printf("%lld\n", ans); return 0; }
1311_F. Moving Points
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i. Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0. Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points. The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct. The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point. Output Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Examples Input 3 1 3 2 -100 2 3 Output 3 Input 5 2 1 4 3 5 2 2 2 3 4 Output 19 Input 2 2 1 -3 0 Output 0
{ "input": [ "3\n1 3 2\n-100 2 3\n", "2\n2 1\n-3 0\n", "5\n2 1 4 3 5\n2 2 2 3 4\n" ], "output": [ "3\n", "0\n", "19\n" ] }
{ "input": [], "output": [] }
CORRECT
python3
import sys int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def SI(): return sys.stdin.readline()[:-1] class BitSum: def __init__(self, n): self.n = n + 3 self.table = [0] * (self.n + 1) def add(self, i, x): i += 1 while i <= self.n: self.table[i] += x i += i & -i def sum(self, i): i += 1 res = 0 while i > 0: res += self.table[i] i -= i & -i return res def main(): n = II() xx = LI() vv = LI() itox = list(sorted(set(xx))) xtoi = {x: i for i, x in enumerate(itox)} #print(itox) #print(xtoi) vx = [(v, x) for x, v in zip(xx, vv)] cnt = BitSum(len(itox)) val = BitSum(len(itox)) ans = 0 for v, x in sorted(vx): i = xtoi[x] if i: ans += cnt.sum(i - 1) * x - val.sum(i - 1) cnt.add(i,1) val.add(i,x) print(ans) main()
1311_F. Moving Points
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i. Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0. Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points. The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct. The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point. Output Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Examples Input 3 1 3 2 -100 2 3 Output 3 Input 5 2 1 4 3 5 2 2 2 3 4 Output 19 Input 2 2 1 -3 0 Output 0
{ "input": [ "3\n1 3 2\n-100 2 3\n", "2\n2 1\n-3 0\n", "5\n2 1 4 3 5\n2 2 2 3 4\n" ], "output": [ "3\n", "0\n", "19\n" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1.0); const double EPS = 1e-6; const int INF = 0x3f3f3f3f; const int MAXN = 200005; int n, m; int maps[MAXN]; long long sum[MAXN][2]; pair<long long, long long> node[MAXN]; inline long long lowbit(long long x) { return x & (-x); } void add(long long id, long long x) { while (id <= m) { sum[id][0]++; sum[id][1] += x; id += lowbit(id); } } long long query(int id, int op) { long long ans = 0; while (id > 0) { ans += sum[id][op]; id -= lowbit(id); } return ans; } void Solve() { sort(node + 1, node + n + 1); sort(maps + 1, maps + n + 1); m = unique(maps + 1, maps + n + 1) - (maps + 1); long long ans = 0, id = -1; for (int i = 1; i <= n; ++i) { id = lower_bound(maps + 1, maps + m + 1, node[i].second) - maps; ans += node[i].first * query(id, 0) - query(id, 1); add(id, node[i].first); } cout << ans << endl; } int main() { cin >> n; for (int i = 1; i <= n; ++i) cin >> node[i].first; for (int i = 1; i <= n; ++i) { cin >> node[i].second; maps[i] = node[i].second; } 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
java
import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; public class Main { InputStream is; PrintWriter out; String INPUT = ""; void solve() { int n = ni(); int[] x = na(n); int[] v = na(n); for(int i = 0; i < n; i++) v[i] += 100000000; v = shrink(v); int[][] co = new int[n][]; for(int i = 0; i < n; i++) { co[i] = new int[] {x[i], v[i]}; } Arrays.sort(co, (int[] a, int[] b) -> a[0] - b[0]); long[] ft = new long[n+5]; long[] counts = new long[n+5]; long ans = 0; for(int i = 0; i < n; i++) { ans += co[i][0] * sumFenwick(counts, co[i][1]) - sumFenwick(ft, co[i][1]); addFenwick(counts, co[i][1], 1); addFenwick(ft, co[i][1], co[i][0]); } out.println(ans); } public static long sumFenwick(long[] ft, int i) { long sum = 0; for(i++;i > 0;i -= i&-i)sum += ft[i]; return sum; } public static void addFenwick(long[] ft, int i, long v) { if(v == 0)return; int n = ft.length; for(i++;i < n;i += i&-i)ft[i] += v; } public static long[] radixSort(long[] f){ return radixSort(f, f.length); } public static long[] radixSort(long[] f, int n) { long[] to = new long[n]; { int[] b = new int[65537]; for(int i = 0;i < n;i++)b[1+(int)(f[i]&0xffff)]++; for(int i = 1;i <= 65536;i++)b[i]+=b[i-1]; for(int i = 0;i < n;i++)to[b[(int)(f[i]&0xffff)]++] = f[i]; long[] d = f; f = to;to = d; } { int[] b = new int[65537]; for(int i = 0;i < n;i++)b[1+(int)(f[i]>>>16&0xffff)]++; for(int i = 1;i <= 65536;i++)b[i]+=b[i-1]; for(int i = 0;i < n;i++)to[b[(int)(f[i]>>>16&0xffff)]++] = f[i]; long[] d = f; f = to;to = d; } { int[] b = new int[65537]; for(int i = 0;i < n;i++)b[1+(int)(f[i]>>>32&0xffff)]++; for(int i = 1;i <= 65536;i++)b[i]+=b[i-1]; for(int i = 0;i < n;i++)to[b[(int)(f[i]>>>32&0xffff)]++] = f[i]; long[] d = f; f = to;to = d; } { int[] b = new int[65537]; for(int i = 0;i < n;i++)b[1+(int)(f[i]>>>48&0xffff)]++; for(int i = 1;i <= 65536;i++)b[i]+=b[i-1]; for(int i = 0;i < n;i++)to[b[(int)(f[i]>>>48&0xffff)]++] = f[i]; long[] d = f; f = to;to = d; } return f; } public static int[] shrink(int[] a) { int n = a.length; long[] b = new long[n]; for (int i = 0; i < n; i++) b[i] = (long) a[i] << 32 | i; b = radixSort(b); int[] ret = new int[n]; int p = 0; for (int i = 0; i < n; i++) { if (i > 0 && (b[i] ^ b[i - 1]) >> 32 != 0) p++; ret[(int) b[i]] = p; } return ret; } void run() throws Exception { is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); if (!INPUT.isEmpty()) tr(System.currentTimeMillis() - s + "ms"); } public static void main(String[] args) throws Exception { new Main().run(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char) skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n && !(isSpaceChar(b))) { buf[p++] = (char) b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for (int i = 0; i < n; i++) map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); } }
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 INF32 = 1E9; const long long INF64 = 4E18; const long long M = 1E9 + 7; const double EPS = 1E-9; const double PI = 2 * acos(0); vector<long long> suffix_sum(1E6); bool cmp_v(pair<long long, long long> a, pair<long long, long long> b) { return (a.second < b.second); } long long solve(vector<pair<long long, long long> >& points, int start, int end) { if ((end - start) == 1) return 0; int mid = (start + end) / 2; long long d1 = solve(points, start, mid); long long d2 = solve(points, mid, end); sort(points.begin() + mid, points.begin() + end, cmp_v); for (int i = end - 1; i >= mid; --i) { suffix_sum[i] = points[i].first; if (i != end - 1) suffix_sum[i] += suffix_sum[i + 1]; } long long d = 0; for (int i = start; i < mid; ++i) { int p = lower_bound(points.begin() + mid, points.begin() + end, points[i], cmp_v) - points.begin(); d += suffix_sum[p]; d -= (end - p) * points[i].first; } return d1 + d2 + d; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector<pair<long long, long long> > points(n); for (int i = 0; i < n; ++i) { cin >> points[i].first; } for (int i = 0; i < n; ++i) { cin >> points[i].second; } sort(points.begin(), points.end()); cout << solve(points, 0, n) << '\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 gcd(long long x, long long y) { if (!y) return x; return gcd(y, x % y); } const long long MOD = 1e9 + 7; int inf = 1e9 + 7; long long INF = 2e18 + 9; long long power(long long x, long long y) { long long res = 1ll; x %= MOD; if (x == 0) return 0; while (y > 0) { if (y & 1) res = (res * x) % MOD; y = y >> 1ll; x = (x * x) % MOD; } return res; } long long inv(long long x) { long long r = 1ll, t = x, k = MOD - 2ll; while (k) { if (k & 1ll) r = (long long)r * t % MOD; t = (long long)t * t % MOD; k >>= 1; } return r; } bool isPrime(long long x) { if (x == 1) return false; for (long long i = 2; i * i <= x; i++) { if (x % i == 0) return false; } return true; } const int nax = 5e5 + 13; void prep() {} void solve() {} vector<long long> fenwick[2]; int n; void add(int pos, long long val, int ind) { pos++; while (pos <= n) { fenwick[ind][pos] += val; pos += pos & (-pos); } } long long query(int pos, int ind) { long long res = 0; pos++; while (pos) { res += fenwick[ind][pos]; pos -= pos & (-pos); } return res; } void read() { cin >> n; vector<pair<int, int> > xv(n); for (int i = 0; i < n; i++) { cin >> xv[i].first; } for (int i = 0; i < n; i++) { cin >> xv[i].second; } vector<int> tmp; for (int i = 0; i < n; i++) tmp.push_back(xv[i].second); sort(tmp.begin(), tmp.end()); auto it = unique(tmp.begin(), tmp.end()); tmp.resize(distance(tmp.begin(), it)); map<int, int> M; for (int i = 0; i < tmp.size(); i++) { M[tmp[i]] = i; } for (int i = 0; i < n; i++) { xv[i].second = M[xv[i].second]; } sort(xv.begin(), xv.end()); int N = n; n = tmp.size(); fenwick[0].resize(n + 1); fenwick[1].resize(n + 1); for (auto& el : fenwick[0]) el = 0; for (auto& el : fenwick[1]) el = 0; long long res = 0; for (int i = 0; i < N; i++) { res += xv[i].first * query(xv[i].second, 0) - query(xv[i].second, 1); add(xv[i].second, 1, 0); add(xv[i].second, xv[i].first, 1); } cout << res << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(); cout.tie(); srand(time(NULL)); int T = 1; prep(); for (int i = 1; i <= T; i++) { read(); solve(); } return 0; }
1311_F. Moving Points
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i. Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0. Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points. The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct. The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point. Output Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Examples Input 3 1 3 2 -100 2 3 Output 3 Input 5 2 1 4 3 5 2 2 2 3 4 Output 19 Input 2 2 1 -3 0 Output 0
{ "input": [ "3\n1 3 2\n-100 2 3\n", "2\n2 1\n-3 0\n", "5\n2 1 4 3 5\n2 2 2 3 4\n" ], "output": [ "3\n", "0\n", "19\n" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1); const int N = 2e5 + 5, mod = 998244353, M = 1e7 + 7; const long long MAX = 5e18; int n; pair<int, int> a[N]; long long ans; struct BIT { long long s[N][2], sum[2]; void init() { memset(s, 0, sizeof s); sum[0] = sum[1] = 0; } void add(int x, int v) { while (x < N) { s[x][0] += v; s[x][1]++; x += x & -x; } sum[0] += v; sum[1]++; } pair<long long, long long> lSum(int x) { pair<long long, long long> c = {0, 0}; while (x) { c.first += s[x][0]; c.second += s[x][1]; x -= x & -x; } return c; } pair<long long, long long> rSum(int x) { pair<long long, long long> ret = lSum(x - 1); ret.first = sum[0] - ret.first; ret.second = sum[1] - ret.second; return ret; } } p, nz; void compress() { map<int, int> mp[2]; set<int> s[2]; for (int i = 0; i < n; i++) { if (a[i].second > 0) s[0].insert(a[i].second); else s[1].insert(-a[i].second); } for (int i = 0; i < 2; i++) { int c = 1; for (int j : s[i]) mp[i][j] = c++; } for (int i = 0; i < n; i++) { if (a[i].second > 0) a[i].second = mp[0][a[i].second]; else a[i].second = -mp[1][-a[i].second]; } } void add(pair<int, int> x) { pair<long long, long long> s; if (x.second > 0) { s = p.rSum(x.second); ans += s.first - s.second * x.first; p.add(x.second, x.first); } else { x.second *= -1; s = p.rSum(1); ans += s.first - s.second * x.first; s = nz.lSum(x.second); ans += s.first - s.second * x.first; nz.add(x.second, x.first); } } int main() { p.init(); nz.init(); scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i].first); for (int i = 0; i < n; i++) scanf("%d", &a[i].second); sort(a, a + n); compress(); for (int i = n - 1; i >= 0; i--) add(a[i]); 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 INF = 0x3f3f3f3f; const int MX = 4e5 + 7; int n; long long c[MX][2]; int v[MX]; struct p { int x, v; inline bool operator<(const p& w) const { return x < w.x; } } a[MX]; inline int lowbit(int x) { return x & -x; } void add(int pos, long long k) { while (pos <= n) { c[pos][0]++; c[pos][1] += k; pos += lowbit(pos); } } long long sum(int pos, int k) { long long res = 0; while (pos) { res += c[pos][k]; pos -= lowbit(pos); } return res; } int main() { 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), v[i] = a[i].v; sort(a + 1, a + 1 + n); sort(v + 1, v + 1 + n); int m = unique(v + 1, v + 1 + n) - v - 1; long long res = 0; for (int i = 1; i <= n; ++i) { int pos = lower_bound(v + 1, v + 1 + m, a[i].v) - v; res += a[i].x * sum(pos, 0) - sum(pos, 1); add(pos, a[i].x); } printf("%lld\n", res); return 0; }
1311_F. Moving Points
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i. Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0. Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points. The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct. The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point. Output Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Examples Input 3 1 3 2 -100 2 3 Output 3 Input 5 2 1 4 3 5 2 2 2 3 4 Output 19 Input 2 2 1 -3 0 Output 0
{ "input": [ "3\n1 3 2\n-100 2 3\n", "2\n2 1\n-3 0\n", "5\n2 1 4 3 5\n2 2 2 3 4\n" ], "output": [ "3\n", "0\n", "19\n" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long N = 2e6 + 7; const long long INF = 1e18; long long n, k, q, st[N][2]; pair<long long, long long> p[N]; map<long long, bool> 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; } void compress() { long long cnt = 0; for (long long i = 1; i <= n; i++) { cnt += (m[p[i].second] == 0); m[p[i].second] = 1; p[i].second = cnt; } } void update(long long node, long long s, long long e, long long ind, long long val, bool type) { if (s == e) { st[node][type] += val; } else { long long mid = s + (e - s) / 2; if (ind <= mid) { update(node * 2, s, mid, ind, val, type); } else { update(node * 2 + 1, mid + 1, e, ind, val, type); } st[node][type] = st[node * 2][type] + st[node * 2 + 1][type]; } } long long query(long long node, long long s, long long e, long long l, long long r, bool type) { if (s >= l && e <= r) { return st[node][type]; } if (s > r || e < l) { return 0; } long long mid = s + (e - s) / 2; return query(node * 2, s, mid, l, r, type) + query(node * 2 + 1, mid + 1, e, l, r, type); } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n; for (long long i = 1; i <= n; i++) { cin >> p[i].first; } for (long long i = 1; i <= n; i++) { cin >> p[i].second; } sort(p + 1, p + n + 1, comp); compress(); sort(p + 1, p + n + 1); long long ans = 0; for (long long i = 1; i <= n; i++) { ans += p[i].first * query(1, 1, n, 1, p[i].second, 0) - query(1, 1, n, 1, p[i].second, 1); update(1, 1, n, p[i].second, 1, 0); update(1, 1, n, p[i].second, p[i].first, 1); } cout << ans << endl; }
1311_F. Moving Points
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i. Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0. Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points. The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct. The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point. Output Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Examples Input 3 1 3 2 -100 2 3 Output 3 Input 5 2 1 4 3 5 2 2 2 3 4 Output 19 Input 2 2 1 -3 0 Output 0
{ "input": [ "3\n1 3 2\n-100 2 3\n", "2\n2 1\n-3 0\n", "5\n2 1 4 3 5\n2 2 2 3 4\n" ], "output": [ "3\n", "0\n", "19\n" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 200005; struct { int l, r; long long sum; long long cnt; } A[N * 4]; pair<pair<long long, long long>, int> pt[N]; int n; bool cmp(pair<pair<long long, long long>, int> x1, pair<pair<long long, long long>, int> x2) { return x1.first.first < x2.first.first; } bool cmp1(pair<pair<long long, long long>, int> x1, pair<pair<long long, long long>, int> x2) { if (x1.first.second == x2.first.second) { return x1.first.first > x2.first.first; } return x1.first.second > x2.first.second; } void build(int rt, int l, int r) { A[rt].l = l; A[rt].r = r; if (l != r) { build(rt * 2, l, (l + r) / 2); build(rt * 2 + 1, (l + r) / 2 + 1, r); } } void update(int rt, int index, long long v) { if (A[rt].l > index || A[rt].r < index) { return; } if (A[rt].l == A[rt].r) { A[rt].sum = v; if (v == 0) { A[rt].cnt = 0; } else A[rt].cnt = 1; return; } else { update(rt * 2, index, v); update(rt * 2 + 1, index, v); A[rt].sum = A[rt * 2].sum + A[rt * 2 + 1].sum; A[rt].cnt = A[rt * 2].cnt + A[rt * 2 + 1].cnt; } } long long query(int rt, int l, int r) { if (A[rt].l > r || A[rt].r < l) { return 0; } if (A[rt].l >= l && A[rt].r <= r) { return A[rt].sum; } return query(rt * 2, l, r) + query(rt * 2 + 1, l, r); } long long query2(int rt, int l, int r) { if (A[rt].l > r || A[rt].r < l) { return 0; } if (A[rt].l >= l && A[rt].r <= r) { return A[rt].cnt; } return query2(rt * 2, l, r) + query2(rt * 2 + 1, l, r); } int main() { ios::sync_with_stdio(false); cin >> n; for (int i = 1; i <= n; i++) { cin >> pt[i].first.first; } for (int i = 1; i <= n; i++) { cin >> pt[i].first.second; } sort(pt + 1, pt + 1 + n, cmp); build(1, 1, n); for (int i = 1; i <= n; i++) { pt[i].second = i; update(1, i, pt[i].first.first); } sort(pt + 1, pt + 1 + n, cmp1); long long ans = 0; for (long long i = 1; i <= n; i++) { ans += query2(1, 1, pt[i].second - 1) * pt[i].first.first - query(1, 1, pt[i].second - 1); update(1, pt[i].second, 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
python3
import sys input=sys.stdin.readline def getsum(BITTree,i): s = 0 while i > 0: s += BITTree[i] i -= (i & -i) return(s) def updatebit(BITTree , i ,v): while i <= len(BITTree): BITTree[i] += v i += (i & -i) #print(BITTree) n=int(input()) x=[int(i) for i in input().split() if i!='\n'] v=[int(i) for i in input().split() if i!='\n'] for i in range(n): v[i]=[v[i],i] v.sort() pos=dict() store,prev=1,v[0][0] for i in range(n): if i>0: if prev==v[i][0]: pos[x[v[i][1]]]=store else: store+=1 pos[x[v[i][1]]]=store prev=v[i][0] else: pos[x[v[i][1]]]=store #print(pos) sorte=sorted(pos.keys()) bit=[0]*(n+10) ind=[0]*(n+10) ans,count,diff=0,0,0 for i in range(len(sorte)): #print(sort,pos) count=getsum(bit,pos[sorte[i]]) diff=getsum(ind,pos[sorte[i]]) ans+=count*sorte[i]-diff updatebit(bit,pos[sorte[i]],1) updatebit(ind,pos[sorte[i]],sorte[i]) print(int(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; using ll = long long; using ii = pair<int, int>; constexpr int MAXN = 5 + 200000; struct Bit { ll b[MAXN]; Bit() { fill(b, b + MAXN, 0); } void upd(int x, ll val) { for (; x < MAXN; x += (x & -x)) b[x] += val; } ll query(int x) { ll ans = 0LL; for (; x; x -= (x & -x)) ans += b[x]; return ans; } }; ii a[MAXN]; namespace comp { int v[MAXN]; void compress(ii arr[], int n, int BEG) { for (int i = (int)0; i < (int)n; ++i) v[i] = arr[i].second; sort(v, v + n); int sz = unique(v, v + n) - v; for (int i = (int)0; i < (int)n; ++i) { arr[i].second = BEG + lower_bound(v, v + sz, arr[i].second) - v; } } } // namespace comp int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int n; cin >> n; for (int i = (int)0; i < (int)n; ++i) { cin >> a[i].first; } for (int i = (int)0; i < (int)n; ++i) { cin >> a[i].second; } sort(a, a + n, [](const ii& p, const ii& q) -> bool { return p.first < q.first; }); ll tot = 0LL, pre = 0LL; for (int i = (int)0; i < (int)n; ++i) { tot += ((1LL * i * a[i].first) - pre); pre += a[i].first; } comp::compress(a, n, 1); Bit suma, cuenta; ll ans = 0LL; for (int i = n - 1; i >= 0; --i) { ans += (suma.query(a[i].second - 1) - 1LL * cuenta.query(a[i].second - 1) * a[i].first); suma.upd(a[i].second, a[i].first); cuenta.upd(a[i].second, 1); } cout << tot - 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 MAX_N = 100011; const long long INF = (1 << 29) + 123; const long long MOD = 1000000007; const long double PI = 4 * atan((long double)1); int fenw[2 * MAX_N]; int n; void mod(int i, int v) { while (i <= n) { fenw[i] += v; i += i & -i; } } int query(int i) { int sum = 0; while (i > 0) { sum += fenw[i]; i -= i & -i; } return sum; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; vector<pair<int, int> > p; int x[n], v[n]; for (int i = 0; i < n; i++) cin >> x[i]; for (int i = 0; i < n; i++) { cin >> v[i]; p.push_back(make_pair(v[i], x[i])); } unordered_map<int, int> m; int rev_map[n]; sort(x, x + n); for (int i = 0; i < n; i++) { m[x[i]] = i + 1; rev_map[i] = x[i]; } for (int i = 0; i < n; i++) { p[i].second = m[p[i].second]; } sort(p.begin(), p.end()); long long ans = 0; for (int i = 0; i < n; i++) { int q = query(p[i].second); ans += 1LL * q * rev_map[p[i].second - 1]; ans -= 1LL * ((n - i - 1) - (p[i].second - 1 - q)) * rev_map[p[i].second - 1]; mod(p[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 maxn = 2e5 + 5; struct nod { long long x, v; } a[maxn]; bool cmp(nod p, nod q) { return p.x < q.x; } int L = 0, R = 2e8, rt = 0, tot = 0; class dsegtree { public: struct dsegnode { int l, r; long long sum1, sum2; } node[maxn * 50]; void pushup(int now) { node[now].sum1 = node[node[now].l].sum1 + node[node[now].r].sum1; node[now].sum2 = node[node[now].l].sum2 + node[node[now].r].sum2; } void update(int pos, int &now, int x, int l = L, int r = R) { if (!now) now = ++tot; if (l == r) { node[now].sum1++; node[now].sum2 += x; return; } if (pos <= (l + r) / 2) update(pos, node[now].l, x, l, (l + r) / 2); else update(pos, node[now].r, x, (l + r) / 2 + 1, r); pushup(now); } pair<long long, long long> query(int s, int t, int now, int l = L, int r = R) { if (!now) return pair<long long, long long>(0, 0); if (s <= l && r <= t) return pair<long long, long long>(node[now].sum1, node[now].sum2); pair<long long, long long> temp; temp.first = temp.second = 0; if ((l + r) / 2 >= s) { pair<long long, long long> tmp = query(s, t, node[now].l, l, (l + r) / 2); temp.first += tmp.first; temp.second += tmp.second; } if ((l + r) / 2 + 1 <= t) { pair<long long, long long> tmp = query(s, t, node[now].r, (l + r) / 2 + 1, r); temp.first += tmp.first; temp.second += tmp.second; } return temp; } } tree; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; for (int i = 1; i <= n; i++) cin >> a[i].x; for (int i = 1; i <= n; i++) cin >> a[i].v; sort(a + 1, a + n + 1, cmp); long long res = 0, m = 1e8; for (int i = 1; i <= n; i++) { pair<long long, long long> temp = tree.query(L, a[i].v + m, rt); res += a[i].x * temp.first - temp.second; tree.update(a[i].v + m, rt, a[i].x); } cout << res << endl; }
1311_F. Moving Points
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i. Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0. Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points. The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct. The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point. Output Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Examples Input 3 1 3 2 -100 2 3 Output 3 Input 5 2 1 4 3 5 2 2 2 3 4 Output 19 Input 2 2 1 -3 0 Output 0
{ "input": [ "3\n1 3 2\n-100 2 3\n", "2\n2 1\n-3 0\n", "5\n2 1 4 3 5\n2 2 2 3 4\n" ], "output": [ "3\n", "0\n", "19\n" ] }
{ "input": [], "output": [] }
CORRECT
java
import java.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] rank.put(x[i], i); } for (int i = 0; i < n; ++i) { ans -= (long) ps[i].first * (rank.get(ps[i].first) - i); // ans -= 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
java
import java.util.Arrays; import java.util.Scanner; import java.util.Comparator; public class kretanje { private static long zbir(long[] arr,int some) { long result=0; while (some>=0) { result+=arr[some]; some=(some&(some+1))-1; } return result; } private static void poslije (long[] qqq,int some,int vv) { while (some<qqq.length) { qqq[some]+=vv; some=some|(some + 1); } } public static void main(String[] args) { Scanner in=new Scanner(System.in); int n=in.nextInt(); int[][] xx=new int[n][2]; int[][] yy=new int[n][2]; for (int i=0;i<n;i++) xx[i][0]=in.nextInt(); for (int i=0;i<n;i++) { xx[i][1]=in.nextInt(); yy[i][0]=xx[i][1]; yy[i][1]=i; } in.close(); Arrays.sort(yy, new Comparator<int[]>() { public int compare(int[] a, int[] b) { return a[0] - b[0]; } }); int somm=0; int[][] kraj=new int[n][2]; for (int i=0;i<n;i++) { if (i>0 && yy[i][0]!=yy[i-1][0]) somm++; kraj[i][0]=somm; kraj[i][1]=yy[i][1]; } Arrays.sort(kraj,new Comparator<int[]>() { public int compare(int[] a,int[] b) { return a[1]-b[1]; }}); for (int i=0; i<n;i++) { xx[i][1] = kraj[i][0]; } Arrays.sort(xx,new Comparator<int[]>() { public int compare(int[] a,int[] b) { return a[0]-b[0]; }}); long[] c=new long[n]; long[] sooom=new long[n]; long totalsum=0; for (int i=0;i<n;i++) { int some=xx[i][1]; totalsum+=zbir(sooom,some)*xx[i][0]-zbir(c,some); poslije(sooom,some,1); //System.out.println(); //System.out.println(totalsum); //System.out.println(); //System.out.println("CRDT sum"); //for (int j=0;j<c.length;j++) { //System.out.print(c[j]); //} poslije(c, some, xx[i][0]); //System.out.println(); //System.out.println("Point sum"); //for (int j=0;j<sooom.length;j++) { //System.out.print(sooom[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
java
import java.util.*; /** * Created by Acesine on 2/24/20. */ public class F { static class BIT { long[] arr; int n; public BIT(int n) { this.n = n; arr = new long[n+1]; } public void update(int x, int v) { x++; while (x <= n) { arr[x] += v; x += x & (-x); } } public long get(int x) { x++; long r = 0; while (x > 0) { r += arr[x]; x -= x & (-x); } return r; } } static class Point { int x; int v; public Point() {} public Point(int x, int v) { this.x = x; this.v = v; } } public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); int[] x = new int[n]; for (int i=0;i<n;i++) x[i] = s.nextInt(); int[] v = new int[n]; for (int i=0;i<n;i++) v[i] = s.nextInt(); Point[] p = new Point[n]; Set<Integer> uniqV = new HashSet<>(); for (int i=0;i<n;i++) { p[i] = new Point(x[i], v[i]); uniqV.add(v[i]); } List<Integer> sortedV = new ArrayList<>(uniqV); Collections.sort(sortedV); Map<Integer, Integer> vmap = new HashMap<>(); for (int i=0;i<sortedV.size();i++) { vmap.put(sortedV.get(i), i); } Arrays.sort(p, (p1, p2) -> p1.x - p2.x); long ret = 0; BIT vBit = new BIT(vmap.size()); BIT sumBit = new BIT(vmap.size()); for (Point pt : p) { int pos = vmap.get(pt.v); long cnt = vBit.get(pos); long sum = sumBit.get(pos); ret += cnt * pt.x - sum; vBit.update(pos, 1); sumBit.update(pos, pt.x); } System.out.println(ret); } }
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 N; pair<long long, long long> A[MAXN + 10]; long long ans; vector<long long> comp; long long tree1[MAXN + 10]; void update1(int i, long long v) { for (; i <= N; i += (i & -i)) tree1[i] += v; } long long query1(int i) { long long ret = 0; for (; i > 0; i -= (i & -i)) ret += tree1[i]; return ret; } long long tree2[MAXN + 10]; void update2(int i, long long v) { for (; i <= N; i += (i & -i)) tree2[i] += v; } long long query2(int i) { long long ret = 0; for (; i > 0; i -= (i & -i)) ret += tree2[i]; return ret; } int main() { int i, j; scanf("%d", &N); for (i = 1; i <= N; i++) scanf("%lld", &A[i].first); for (i = 1; i <= N; i++) scanf("%lld", &A[i].second), comp.push_back(A[i].second); sort(A + 1, A + N + 1); sort(comp.begin(), comp.end()); comp.erase(unique(comp.begin(), comp.end()), comp.end()); for (i = 1; i <= N; i++) A[i].second = lower_bound(comp.begin(), comp.end(), A[i].second) - comp.begin() + 1; for (i = 1; i <= N; i++) { ans += query2(A[i].second) * A[i].first - query1(A[i].second); update1(A[i].second, A[i].first); update2(A[i].second, 1); } 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
python3
import math import bisect class Fenwick: def __init__(self, n): self.data = [[0,0] for i in range(n)] def update(self, pos, dist): while pos<len(self.data): self.data[pos][0] += 1 self.data[pos][1] += dist pos = pos | (pos+1) def query(self, pos): ans = [0,0] while pos > 0: ans[0] += self.data[pos-1][0] ans[1] += self.data[pos-1][1] pos = pos & (pos-1) return ans def rints(): return list(map(int,input().split())) n = int(input()) x = rints() v = rints() ascDist = list(range(n)) ascDist.sort(key=lambda i: x[i]) uniqueSpeeds = sorted(list(set(v))) tree = Fenwick(len(uniqueSpeeds)) ans = 0 for i in ascDist: speedId = bisect.bisect_left(uniqueSpeeds, v[i]) count, dsum = tree.query(speedId+1) ans += count*x[i] - dsum tree.update(speedId, x[i]) 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; long long int ans = 0; void merger(vector<pair<long long int, long long int> > &v, long long int l, long long int mid, long long int r) { long long int n1 = mid - l + 1, n2 = r - mid; pair<long long int, long long int> larr[n1], rarr[n2]; long long int lsum[n1], rsum[n2]; for (long long int i = 0; i < n1; i++) larr[i] = v[l + i]; for (long long int i = n1 - 1; i >= 0; i--) { if (i == n1 - 1) lsum[i] = larr[i].first; else lsum[i] = lsum[i + 1] + larr[i].first; } for (long long int i = 0; i < n2; i++) rarr[i] = v[mid + i + 1]; for (long long int i = n2 - 1; i >= 0; i--) { if (i == n2 - 1) rsum[i] = rarr[i].first; else rsum[i] = rsum[i + 1] + rarr[i].first; } long long int i = 0, j = 0, k = l; while (i < n1 && j < n2) { if (larr[i].second <= rarr[j].second) { v[k] = larr[i]; ans += rsum[j]; ans -= ((n2 - j) * (larr[i].first)); i++; } else { v[k] = rarr[j++]; } k++; } while (i < n1) v[k++] = larr[i++]; while (j < n2) v[k++] = rarr[j++]; } void msort(vector<pair<long long int, long long int> > &v, long long int l, long long int r) { if (l < r) { long long int mid = l + (r - l) / 2; msort(v, l, mid); msort(v, mid + 1, r); merger(v, l, mid, r); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int n; cin >> n; vector<pair<long long int, long long int> > v; long long int a[n], b[n]; for (long long int i = 0; i < n; i++) cin >> a[i]; for (long long int i = 0; i < n; i++) cin >> b[i]; for (long long int i = 0; i < n; i++) v.push_back(make_pair(a[i], b[i])); sort(v.begin(), v.end()); msort(v, 0, n - 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 int N = 2e5 + 5; int n, m, pos, v[N]; long long tot, s[N][2]; struct node { long long x, v; } a[N]; bool cmp(node x, node y) { return x.x < y.x; } int lowbit(int x) { return x & (-x); } void update(int x, int val) { while (x <= n) { s[x][0]++; s[x][1] += val; x += lowbit(x); } } long long getsum(int x, int flag) { long long res = 0; while (x) { res += s[x][flag]; x -= lowbit(x); } return res; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%lld", &a[i].x); for (int i = 1; i <= n; i++) { scanf("%lld", &a[i].v); v[i] = a[i].v; } sort(a + 1, a + n + 1, cmp); sort(v + 1, v + n + 1); m = unique(v + 1, v + n + 1) - v - 1; for (int i = 1; i <= n; i++) { pos = lower_bound(v + 1, v + m + 1, a[i].v) - v; tot += getsum(pos, 0) * a[i].x - getsum(pos, 1); update(pos, a[i].x); } printf("%lld", 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 get(vector<long long> &f, int pos) { long long res = 0; for (; pos >= 0; pos = (pos & (pos + 1)) - 1) res += f[pos]; return res; } void upd(vector<long long> &f, int pos, int val) { for (; pos < int(f.size()); pos |= pos + 1) { f[pos] += val; } } int main() { int n; cin >> n; vector<pair<int, int>> p(n); for (auto &pnt : p) cin >> pnt.first; for (auto &pnt : p) cin >> pnt.second; sort(p.begin(), p.end()); vector<int> vs; for (auto &pnt : p) vs.push_back(pnt.second); sort(vs.begin(), vs.end()); vs.resize(unique(vs.begin(), vs.end()) - vs.begin()); long long ans = 0; vector<long long> cnt(vs.size()), xs(vs.size()); for (auto &pnt : p) { int pos = lower_bound(vs.begin(), vs.end(), pnt.second) - vs.begin(); ans += get(cnt, pos) * 1ll * pnt.first - get(xs, pos); upd(cnt, pos, 1); upd(xs, pos, pnt.first); } cout << ans << endl; return 0; }
1311_F. Moving Points
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i. Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0. Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points. The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct. The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point. Output Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Examples Input 3 1 3 2 -100 2 3 Output 3 Input 5 2 1 4 3 5 2 2 2 3 4 Output 19 Input 2 2 1 -3 0 Output 0
{ "input": [ "3\n1 3 2\n-100 2 3\n", "2\n2 1\n-3 0\n", "5\n2 1 4 3 5\n2 2 2 3 4\n" ], "output": [ "3\n", "0\n", "19\n" ] }
{ "input": [], "output": [] }
CORRECT
java
import java.util.Scanner; import java.util.Stack; import java.util.Arrays; import java.util.HashMap; import java.util.Comparator; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.FileWriter; public class F{ // static final int max = 10000; static class Pair{ int x; int speed; public String toString(){ return "[" + x + ", " + speed + "]"; } } static class FenvikTree{ long[] arr; long[] T; int size; FenvikTree(int n){ size = n; arr = new long[n]; T = new long[n]; } public void add(int pos, int a){ arr[pos] += a; while(pos < size){ T[pos] += a; pos = pos | (pos+1); } } public long sum(int pos){ if(pos < 0) return 0; if(pos == 0) return T[0]; if(pos == 1) return T[1]; // System.out.println(pos + " " + ((pos&(pos+1))-1)); return T[pos] + sum((pos&(pos+1))-1); } } public static void main(String[] args){ // File file = new File("a.in"); // Scanner in = new Scanner(file); // int t = in.nextInt(); // StringBuilder str = new StringBuilder(); // for(int u = 0;u<t;++u){ Scanner in = new Scanner(System.in); int n = in.nextInt(); Pair[] a = new Pair[n]; // long[] speed = new long[n]; HashMap<Integer,Integer> map= new HashMap<>(); for(int i = 0;i<n;++i){ a[i] = new Pair(); a[i].x = in.nextInt(); } // int[] b= new int[n]; for(int i = 0;i<n;++i){ a[i].speed = in.nextInt(); // b[i] = a[i].speed; } Comparator<Pair> comp1 = new Comparator<Pair>(){ public int compare(Pair a, Pair b){ return Integer.compare(a.speed,b.speed); } }; Arrays.sort(a,comp1); // System.out.println(Arrays.toString(b)); int count = 0; for(int i = 0;i<n;++i){ if(map.get(a[i].speed) == null){ map.put(a[i].speed,count++); } a[i].speed = map.get(a[i].speed); } // } Comparator<Pair> comp2 = new Comparator<Pair>(){ public int compare(Pair a, Pair b){ return (a.x == b.x) ? (Integer.compare(b.speed,a.speed)) : Integer.compare(a.x,b.x); } }; Arrays.sort(a,comp2); // System.out.println(Arrays.toString(a)); FenvikTree fenvikCount = new FenvikTree(n); FenvikTree fenvikSum = new FenvikTree(n); fenvikCount.add(a[0].speed,1); fenvikSum.add(a[0].speed,a[0].x); long res = 0; for(int i = 1;i<n;++i){ int cur = a[i].speed; res += fenvikCount.sum(cur)*a[i].x - fenvikSum.sum(cur); fenvikCount.add(cur,1); fenvikSum.add(cur,a[i].x); } System.out.println(res); // str.append(res + "\r\n"); // File fileo =new File("out2.txt"); // FileWriter out = new FileWriter(fileo); // out.write(str.toString(),0,str.length()); // out.close(); } }
1311_F. Moving Points
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i. Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0. Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points. The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct. The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point. Output Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Examples Input 3 1 3 2 -100 2 3 Output 3 Input 5 2 1 4 3 5 2 2 2 3 4 Output 19 Input 2 2 1 -3 0 Output 0
{ "input": [ "3\n1 3 2\n-100 2 3\n", "2\n2 1\n-3 0\n", "5\n2 1 4 3 5\n2 2 2 3 4\n" ], "output": [ "3\n", "0\n", "19\n" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct OrderSet { struct Trie { int cnt; int next[2]; Trie() { memset(next, -1, sizeof(next)); cnt = 0; } }; vector<Trie*> trie; OrderSet() { auto u = new Trie(); trie.push_back(u); } bool find(int first) { int cur = 0; for (int i = 30; i >= 0; i--) { auto u = ((first >> i) & 1); if (trie[cur]->next[u] == -1) return 0; else cur = trie[cur]->next[u]; } return 1; } void insert(int first) { int cur = 0; trie[cur]->cnt++; for (int i = 30; i >= 0; i--) { auto u = ((first >> i) & 1); if (trie[cur]->next[u] == -1) { auto newTrie = new Trie(); trie.push_back(newTrie); trie[cur]->next[u] = trie.size() - 1; cur = trie[cur]->next[u]; } else { cur = trie[cur]->next[u]; } trie[cur]->cnt++; } } void deletee(int first) { int cur = 0; trie[cur]->cnt--; for (int i = 30; i >= 0; i--) { auto u = ((first >> i) & 1); int temp = trie[cur]->next[u]; if (trie[temp]->cnt == 1) trie[cur]->next[u] = -1; cur = temp; trie[cur]->cnt--; } } int order_of_key(int first) { int cur = 0; int res = 0; for (int i = 30; i >= 0; i--) { auto u = ((first >> i) & 1); if (u == 1 && trie[cur]->next[0] != -1) { int temp = trie[cur]->next[0]; res += trie[temp]->cnt; } if (trie[cur]->next[u] == -1) return res; cur = trie[cur]->next[u]; } return res; } int find_by_order(int k) { int cur = 0; int res = 0; if (trie[cur]->cnt < k) return -1; for (int i = 30; i >= 0; i--) { int temp = trie[cur]->next[0]; if (temp == -1) { cur = trie[cur]->next[1]; res += (1 << i); continue; } if (trie[temp]->cnt < k) { k -= trie[temp]->cnt; cur = trie[cur]->next[1]; res += (1 << i); } else { cur = trie[cur]->next[0]; } } return res; } } os; pair<int, int> a[200005]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i].first; } for (int i = 1; i <= n; i++) { cin >> a[i].second; a[i].second += 2e8; } sort(a + 1, a + n + 1); long long ans = 0; map<int, int> cnt; for (int i = 1; i <= n; i++) { ans += 1LL * (os.order_of_key(a[i].second) + cnt[a[i].second]) * a[i].first; os.insert(a[i].second); cnt[a[i].second]++; } os = OrderSet(); for (int i = n; i >= 1; i--) { ans -= 1LL * (n - i - os.order_of_key(a[i].second)) * a[i].first; os.insert(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; long long get(vector<long long> &f, int pos) { long long res = 0; for (; pos >= 0; pos = (pos & (pos + 1)) - 1) res += f[pos]; return res; } void upd(vector<long long> &f, int pos, int val) { for (; pos < int(f.size()); pos |= pos + 1) { f[pos] += val; } } int main() { int n; cin >> n; vector<pair<int, int>> p(n); for (auto &pnt : p) cin >> pnt.first; for (auto &pnt : p) cin >> pnt.second; sort(p.begin(), p.end()); vector<int> vs; for (auto &pnt : p) vs.push_back(pnt.second); sort(vs.begin(), vs.end()); vs.resize(unique(vs.begin(), vs.end()) - vs.begin()); long long ans = 0; vector<long long> cnt(vs.size()), xs(vs.size()); for (auto &pnt : p) { int pos = lower_bound(vs.begin(), vs.end(), pnt.second) - vs.begin(); ans += get(cnt, pos) * 1ll * pnt.first - get(xs, pos); upd(cnt, pos, 1); upd(xs, pos, pnt.first); } cout << ans << endl; return 0; }
1311_F. Moving Points
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i. Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0. Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points. The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct. The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point. Output Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Examples Input 3 1 3 2 -100 2 3 Output 3 Input 5 2 1 4 3 5 2 2 2 3 4 Output 19 Input 2 2 1 -3 0 Output 0
{ "input": [ "3\n1 3 2\n-100 2 3\n", "2\n2 1\n-3 0\n", "5\n2 1 4 3 5\n2 2 2 3 4\n" ], "output": [ "3\n", "0\n", "19\n" ] }
{ "input": [], "output": [] }
CORRECT
java
import java.io.*; import java.util.*; public class Contest1 { //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////// ///////// //////// ///////// //////// HHHH HHHH EEEEEEEEEEEEE MMMM MMMM OOOOOO SSSSSSS EEEEEEEEEEEEE ///////// //////// HHHH HHHH EEEEEEEEEEEEE MMMMMM MMMMMM OOO OOO SSSS SSS EEEEEEEEEEEEE ///////// //////// HHHH HHHH EEEEE MMMM MMM MMM MMMM OOO OOO SSSS SSS EEEEE ///////// //////// HHHH HHHH EEEEE MMMM MMMMMM MMMM OOO OOO SSSS EEEEE ///////// //////// HHHH HHHH EEEEE MMMM MMMM OOO OOO SSSSSSS EEEEE ///////// //////// HHHHHHHHHHHHHHHH EEEEEEEEEEE MMMM MMMM OOO OOO SSSSSS EEEEEEEEEEE ///////// //////// HHHHHHHHHHHHHHHH EEEEEEEEEEE MMMM MMMM OOO OOO SSSSSSS EEEEEEEEEEE ///////// //////// HHHH HHHH EEEEE MMMM MMMM OOO OOO SSSS EEEEE ///////// //////// HHHH HHHH EEEEE MMMM MMMM OOO OOO SSS SSSS EEEEE ///////// //////// HHHH HHHH EEEEEEEEEEEEE MMMM MMMM OOO OOO SSS SSSS EEEEEEEEEEEEE ///////// //////// HHHH HHHH EEEEEEEEEEEEE MMMM MMMM OOOOOO SSSSSSS EEEEEEEEEEEEE ///////// //////// ///////// //////// ///////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// static class Pair{ int v,s; Pair(int a,int b) { v=a; s=b; } } public static void main(String[] args) throws IOException, InterruptedException { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int n = sc.nextInt(); int[]a = new int[n]; for (int i =0;i<n;i++)a[i]=sc.nextInt(); Pair[]pairs= new Pair[n]; TreeSet<Integer>ts= new TreeSet<>(); for (int i =0;i<n;i++) { pairs[i] = new Pair(a[i], sc.nextInt()); ts.add(pairs[i].s); } Arrays.sort(pairs,(x,y)->x.v-y.v); ts.add((int)-1e9); ts.add((int)1e9); TreeMap<Integer,Integer>comp = new TreeMap<>(); while (!ts.isEmpty())comp.put(ts.pollFirst(),comp.size()); FenwickTree count = new FenwickTree(comp.size()); FenwickTree sum = new FenwickTree(comp.size()); long ans=0; for (int i =0;i<n;i++){ int id = comp.get(pairs[i].s); long c = count.rsq(id); ans+=c*pairs[i].v-sum.rsq(id); count.point_update(id,1); sum.point_update(id,pairs[i].v); } pw.println(ans); pw.flush(); } static class FenwickTree { // one-based DS int n; long[] ft; FenwickTree(int size) { n = size; ft = new long[n+1]; } long rsq(int b) //O(log n) { long sum = 0; while(b > 0) { sum += ft[b]; b -= b & -b;} //min? return sum; } long rsq(int a, int b) { return rsq(b) - rsq(a-1); } void point_update(int k, int val) //O(log n), update = increment { while(k <= n) { ft[k] += val; k += k & -k; } //min? } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(FileReader r) { br = new BufferedReader(r); } public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public boolean ready() throws IOException { return br.ready(); } } }
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 C[2][maxn]; void add(int x, long long K) { while (0 < x && x < maxn) { C[0][x] += 1; C[1][x] += K; x += (x) & (-x); } } pair<long long, long long> getsum(int x) { long long sum = 0; int num = 0; while (0 < x && x < maxn) { num += C[0][x]; sum += C[1][x]; 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(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; for (int i = 1; i <= N; i++) { pair<long long, long long> s = getsum(getpos(p[i].second)); ans += s.first * p[i].first - s.second; add(getpos(p[i].second), 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
java
/* / フフ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ム / )\⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ Y (⠀⠀| ( ͡° ͜ʖ ͡°)⠀⌒(⠀ ノ (⠀ ノ⌒ Y ⌒ヽ-く __/ | _⠀。ノ| ノ。 |/ (⠀ー '_人`ー ノ ⠀|\  ̄ _人'彡ノ ⠀ )\⠀⠀ 。⠀⠀ / ⠀⠀(\⠀ #⠀ / ⠀/⠀⠀⠀/ὣ====================D- /⠀⠀⠀/⠀ \ \⠀⠀\ ( (⠀)⠀⠀⠀⠀ ) ).⠀) (⠀⠀)⠀⠀⠀⠀⠀( | / |⠀ /⠀⠀⠀⠀⠀⠀ | / [_] ⠀⠀⠀⠀⠀[___] */ // Main Code at the Bottom import java.util.*; import java.lang.*; import java.io.*; import java.math.BigInteger; public class Main { //Fast IO class static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { boolean env=System.getProperty("ONLINE_JUDGE") != null; if(!env) { try { br=new BufferedReader(new FileReader("src\\input.txt")); } catch (FileNotFoundException e) { e.printStackTrace(); } } else 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 nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static long MOD=1000000000+7; //Euclidean Algorithm static long gcd(long A,long B){ return (B==0)?A:gcd(B,A%B); } //Modular Exponentiation static long fastExpo(long x,long n){ if(n==0) return 1; if((n&1)==0) return fastExpo((x*x)%MOD,n/2)%MOD; return ((x%MOD)*fastExpo((x*x)%MOD,(n-1)/2))%MOD; } //Modular Inverse static long inverse(long x) { return fastExpo(x,MOD-2); } //Prime Number Algorithm static boolean isPrime(long n){ if(n<=1) return false; if(n<=3) return true; if(n%2==0 || n%3==0) return false; for(int i=5;i*i<=n;i+=6) if(n%i==0 || n%(i+2)==0) return false; return true; } //Reverse an array static void reverse(int arr[],int l,int r){ while(l<r) { int tmp=arr[l]; arr[l++]=arr[r]; arr[r--]=tmp; } } //Print array static void print1d(int arr[]) { out.println(Arrays.toString(arr)); } static void print2d(int arr[][]) { for(int a[]: arr) out.println(Arrays.toString(a)); } // Pair static class pair{ long x,y; pair(long a,long b){ this.x=a; this.y=b; } public boolean equals(Object obj) { if(obj == null || obj.getClass()!= this.getClass()) return false; pair p = (pair) obj; return (this.x==p.x && this.y==p.y); } public int hashCode() { return Objects.hash(x,y); } } static FastReader sc=new FastReader(); static PrintWriter out=new PrintWriter(System.out); //Main function(The main code starts from here) static int N=200000; static pair bit[]=new pair[N+1]; static void add(int idx,long val) { while(idx<=N) { bit[idx].x++; bit[idx].y+=val; idx+=idx&-idx; } } static pair sum(int idx) { pair res=new pair(0,0); while(idx>0) { res.x+=bit[idx].x; res.y+=bit[idx].y; idx-=idx&-idx; } return res; } public static void main (String[] args) throws java.lang.Exception { int test; test=1; //test=sc.nextInt(); while(test-->0){ for(int i=0;i<=N;i++) bit[i]=new pair(0,0); int n=sc.nextInt(); pair a[]=new pair[n]; for(int i=0;i<n;i++) a[i]=new pair(sc.nextLong(),-1); HashMap<Long,Integer> map=new HashMap<>(); TreeSet<Long> set=new TreeSet<>(); for(int i=0;i<n;i++) { a[i].y=sc.nextLong(); set.add(a[i].y); } Arrays.parallelSort(a,(p1,p2)->(int)(p1.x-p2.x)); int idx=1; for(Long x: set) map.put(x,idx++); long ans=0; for(int i=n-1;i>=0;i--) { idx=map.get(a[i].y); long val=a[i].x; pair r=sum(N),l=sum(idx-1); long x=r.y-l.y,y=r.x-l.x; ans+=x-val*y; add(idx,val); } out.println(ans); } out.flush(); out.close(); } }
1311_F. Moving Points
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i. Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0. Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points. The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct. The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point. Output Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Examples Input 3 1 3 2 -100 2 3 Output 3 Input 5 2 1 4 3 5 2 2 2 3 4 Output 19 Input 2 2 1 -3 0 Output 0
{ "input": [ "3\n1 3 2\n-100 2 3\n", "2\n2 1\n-3 0\n", "5\n2 1 4 3 5\n2 2 2 3 4\n" ], "output": [ "3\n", "0\n", "19\n" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long maxn = 2e5 + 10; const long long maxX = 2e8 + 10; long long n; long long sz = 1; pair<long long, long long> p[maxn]; long long segsum[25 * maxn]; long long segtedad[25 * maxn]; long long L[25 * maxn]; long long R[25 * maxn]; long long ans; void in(); void solve(); void add(long long id, long long l, long long r, long long idx, long long v); pair<long long, long long> get(long long id, long long l, long long r, long long L, long long R); void out(); int main() { in(); solve(); out(); } void in() { ios::sync_with_stdio(false); cout.tie(0); cin.tie(0); cin >> n; for (long long i = 0; i < n; i++) cin >> p[i].first; for (long long i = 0; i < n; i++) cin >> p[i].second; for (long long i = 0; i < n; i++) p[i].second += 1e8; } void solve() { sort(p, p + n); for (long long i = 0; i < n; i++) { pair<long long, long long> wow = get(0, 0, maxX, 0, p[i].second + 1); add(0, 0, maxX, p[i].second, p[i].first); ans += p[i].first * wow.second - wow.first; } } void add(long long id, long long l, long long r, long long idx, long long v) { if (r - l == 1) { segsum[id] += v; segtedad[id]++; return; } long long mid = (l + r) >> 1; if (L[id] == 0) { L[id] = sz++; R[id] = sz++; } if (idx < mid) add(L[id], l, mid, idx, v); else add(R[id], mid, r, idx, v); segtedad[id] = segtedad[L[id]] + segtedad[R[id]]; segsum[id] = segsum[L[id]] + segsum[R[id]]; } pair<long long, long long> get(long long id, long long l, long long r, long long second, long long e) { if (r <= second || e <= l) return {0, 0}; if (second <= l && r <= e) return {segsum[id], segtedad[id]}; long long mid = (l + r) >> 1; if (L[id] == 0) { L[id] = sz++; R[id] = sz++; } auto a = get(L[id], l, mid, second, e); auto b = get(R[id], mid, r, second, e); a.first += b.first; a.second += b.second; return a; } void out() { 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 INF = 1e9 + 7; const int N = 3e5 + 10; void init(vector<pair<int, int>>& vp) { vector<int> v; for (auto& p : vp) v.push_back(p.second); sort(v.begin(), v.end()); for (auto& p : vp) { p.second = lower_bound(v.begin(), v.end(), p.second) - v.begin() + 1; } } class BIT { public: long long a[N]; void add(int x, long long val) { while (x < N) { a[x] += val; x += x & -x; } } long long sum(int x) { long long ret = 0; while (x) { ret += a[x]; x -= x & -x; } return ret; } } b1, b2; int main() { int n; scanf("%d", &n); vector<pair<int, int>> vp(n); for (auto& p : vp) scanf("%d", &p.first); for (auto& p : vp) scanf("%d", &p.second); init(vp); long long ans = 0; sort(vp.begin(), vp.end()); for (auto& p : vp) { b1.add(p.second, 1); b2.add(p.second, p.first); ans += b1.sum(p.second) * p.first - b2.sum(p.second); } 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 = 2e5 + 5; const int M = 2e3 + 5; const int mod = 1e9 + 7; const double eps = 1e-8; const double pi = acos(-1); template <class T> inline void read(T &x) { char c; x = 1; while ((c = getchar()) < '0' || c > '9') if (c == '-') x = -1; T res = c - '0'; while ((c = getchar()) >= '0' && c <= '9') res = res * 10 + c - '0'; x *= res; } int n, m; char str[N]; long long f[N][2]; int lowbit(int x) { return x & (-x); } long long get(int pos, int type) { long long res = 0; while (pos) { res += f[pos][type]; pos -= lowbit(pos); } return res; } void update(int pos, int v) { while (pos <= m) { f[pos][0]++; f[pos][1] += v; pos += lowbit(pos); } } void solve() { read(n); vector<pair<int, int> > p(n); for (pair<int, int> &pp : p) read(pp.first); for (pair<int, int> &pp : p) read(pp.second); sort(p.begin(), p.end()); vector<int> v; for (pair<int, int> &pp : p) v.push_back(pp.second); sort(v.begin(), v.end()); v.resize(unique(v.begin(), v.end()) - v.begin()); m = v.size(); long long ans = 0; for (pair<int, int> &pp : p) { int pos = lower_bound(v.begin(), v.end(), pp.second) - v.begin(); long long cnt = get(pos + 1, 0), sum = get(pos + 1, 1); ans += cnt * pp.first - sum; update(pos + 1, pp.first); } cout << ans << endl; } int main() { int t; t = 1; while (t--) { solve(); } return 0; }
1311_F. Moving Points
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i. Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0. Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points. The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct. The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point. Output Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Examples Input 3 1 3 2 -100 2 3 Output 3 Input 5 2 1 4 3 5 2 2 2 3 4 Output 19 Input 2 2 1 -3 0 Output 0
{ "input": [ "3\n1 3 2\n-100 2 3\n", "2\n2 1\n-3 0\n", "5\n2 1 4 3 5\n2 2 2 3 4\n" ], "output": [ "3\n", "0\n", "19\n" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int bit[N]; void add(int p, int v) { for (p += 2; p < N; p += p & -p) bit[p] += v; } long long query(int p) { int r = 0; for (p += 2; p; p -= p & -p) r += bit[p]; return r; } int n; pair<int, int> p[N]; long long l[N], r[N]; int ql[N], qr[N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 1; i <= n; i++) cin >> p[i].second; for (int i = 1; i <= n; i++) cin >> p[i].first; sort(p + 1, p + n + 1); map<int, int> id; int cnt = 0, last = 0x3f3f3f3f; for (int i = 1; i <= n; i++) { int v = p[i].first; if (v != last) id[v] = ++cnt, last = v; swap(p[i].first, p[i].second); } sort(p + 1, p + n + 1); long long ans = 0; for (int i = 1; i <= n; i++) { long long x = p[i].first, v = id[p[i].second]; ans += query(v) * x; add(v, 1); } memset(bit, 0, sizeof(bit)); for (int i = n; i >= 1; i--) { long long x = p[i].first, v = id[p[i].second]; ans -= (query(n) - query(v - 1)) * x; add(v, 1); } 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; long long int g[800010][2]; void up(int l, int r, int n, int x, int i, long long int val) { if (l == r) { g[n][i] = val; } else { int mid = (l + r) / 2; if (x <= mid && x >= l) up(l, mid, 2 * n, x, i, val); else up(mid + 1, r, 2 * n + 1, x, i, val); g[n][i] = g[2 * n][i] + g[2 * n + 1][i]; } return; } long long int q(int l, int r, int n, int st, int en, int i) { if (en == 0) return 0; if (st <= l && r <= en) { return g[n][i]; } else if (r < st || en < l) return 0; else { int mid = (l + r) / 2; return (q(l, mid, 2 * n, st, en, i) + q(mid + 1, r, 2 * n + 1, st, en, i)); } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; vector<pair<long long int, long long int> > v(n); vector<int> x(n); map<int, int> m; for (int i = 0; i < n; i++) { cin >> v[i].second; x[i] = v[i].second; } for (int i = 0; i < n; i++) cin >> v[i].first; sort(x.begin(), x.end()); for (int i = 0; i < x.size(); i++) { m[x[i]] = i + 1; } sort(v.begin(), v.end()); long long int ans = 0; for (int i = 0; i < n; i++) { long long int z = q(1, n, 1, 1, m[v[i].second], 0); long long int z1 = q(1, n, 1, 1, m[v[i].second], 1); ans += (v[i].second * z1 - z); up(1, n, 1, m[v[i].second], 0, v[i].second); up(1, n, 1, m[v[i].second], 1, 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> #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,-O3") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; const int oo = 2e9; const long long OO = 4e18; const int md = 0x3b800001; const int MD = 1e9 + 7; inline long long time() { return chrono ::system_clock().now().time_since_epoch().count(); } mt19937 rnd(time()); mt19937_64 RND(time()); template <typename t> inline void umin(t &a, t b) { a = min(a, b); } template <typename t> inline void umax(t &a, t b) { a = max(a, b); } const int N = 2e5 + 228; int n, x[N], v[N], p[N], s[N]; int f0[N]; int gf0(int i) { int ans = 0; for (i = s[i] + 1; i; i -= i & -i) ans += f0[i]; return ans; } void uf0(int i, int v) { for (i = s[i] + 1; i < N; i += i & -i) f0[i] += v; } long long f1[N]; long long gf1(int i) { long long ans = 0; for (i = s[i] + 1; i; i -= i & -i) ans += f1[i]; return ans; } void uf1(int i, long long v) { for (i = s[i] + 1; i < N; i += i & -i) f1[i] += v; } void solve() { cin >> n; for (int i = 0; i < n; ++i) cin >> x[i]; for (int i = 0; i < n; ++i) cin >> v[i], p[i] = i; sort(p, p + n, [&](int a, int b) { return x[a] < x[b]; }); for (int i = 0; i < n; ++i) s[p[i]] = i; sort(p, p + n, [&](int a, int b) { return v[a] == v[b] ? x[a] < x[b] : v[a] < v[b]; }); memset(f0, 0, sizeof f0); memset(f1, 0, sizeof f1); long long ans = 0; for (int j = 0; j < n; ++j) { int i = p[j]; ans += 1ll * gf0(i) * x[i] - gf1(i); uf0(i, 1); uf1(i, x[i]); } cout << ans << endl; } int main() { ios_base ::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); return 0; }
1311_F. Moving Points
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i. Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0. Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points. The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct. The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point. Output Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Examples Input 3 1 3 2 -100 2 3 Output 3 Input 5 2 1 4 3 5 2 2 2 3 4 Output 19 Input 2 2 1 -3 0 Output 0
{ "input": [ "3\n1 3 2\n-100 2 3\n", "2\n2 1\n-3 0\n", "5\n2 1 4 3 5\n2 2 2 3 4\n" ], "output": [ "3\n", "0\n", "19\n" ] }
{ "input": [], "output": [] }
CORRECT
java
import java.io.*; import java.util.*; import java.lang.*; import java.math.*; public class Main extends Thread { boolean[] prime; FastScanner sc; PrintWriter pw; long startTime = System.currentTimeMillis(); final class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { try { br = new BufferedReader(new InputStreamReader(System.in)); st = new StringTokenizer(br.readLine()); } catch (Exception e) { e.printStackTrace(); } } public long nlo() { return Long.parseLong(next()); } public String next() { if (st.hasMoreTokens()) return st.nextToken(); try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { e.printStackTrace(); } return st.nextToken(); } public int ni() { return Integer.parseInt(next()); } public String nli() { String line = ""; if (st.hasMoreTokens()) line = st.nextToken(); else try { return br.readLine(); } catch (IOException e) { e.printStackTrace(); } while (st.hasMoreTokens()) line += " " + st.nextToken(); return line; } public double nd() { return Double.parseDouble(next()); } } public Main(ThreadGroup t,Runnable r,String s,long d ) { super(t,r,s,d); } public void run() { sc=new FastScanner(); pw=new PrintWriter(System.out); solve(); pw.flush(); pw.close(); } public static void main(String[] args) { new Main(null,null,"",1<<26).start(); } /////////////------------------------------------////////////// ////////////------------------Main-Logic--------////////////// ///////////-------------------------------------////////////// public static class Pair implements Comparable<Pair>{ long x; long v; Pair(long a) { x=a; } public int compareTo(Pair b) { long p=x-b.x; if(p<=0) return -1; return 1; } } public void solve() { int t=1; while(t-->0) { ArrayList<Pair> list =new ArrayList<Pair>(); int n=sc.ni(); for(int i=0;i<n;i++) list.add(new Pair(sc.nlo())); TreeSet<Long> lrr=new TreeSet(); for(Pair p:list) {long x=sc.nlo();p.v=x;lrr.add(x);} Collections.sort(list); HashMap<Long,Integer> map=new HashMap(); int k=0; while(lrr.size()>0) { long p=lrr.first(); map.put(p,k++); lrr.remove(p); } long[] seg=new long[4*k+1]; long[] seg1=new long[4*k+1]; update(list.get(n-1).x,map.get(list.get(n-1).v),1,seg,0,k-1); update(1,map.get(list.get(n-1).v),1,seg1,0,k-1); long ans=0; for(int i=n-2;i>=0;i--) { int x=map.get(list.get(i).v); long y=query(x,k-1,1,seg,0,k-1); long z=query(x,k-1,1,seg1,0,k-1); ans+=(y-(z*list.get(i).x)); update(list.get(i).x,x,1,seg,0,k-1); update(1,x,1,seg1,0,k-1); } pw.println(ans); } } public static void update(long x,int p,int ind,long[] seg,int s,int e) { if(p<s||p>e) return; if((s==e)&&(p==s)) seg[ind]+=x; else { if(p<=(s+e)/2) update(x,p,2*ind,seg,s,(e+s)/2); else update(x,p,2*ind+1,seg,(s+e)/2+1,e); seg[ind]=seg[2*ind]+seg[2*ind+1]; } } public static long query(int qs,int qe,int ind,long[] seg,int s,int e) { if(s>qe||e<qs) return 0; if((qs<=s)&&(qe>=e)) return seg[ind]; return query(qs,qe,2*ind,seg,s,(s+e)/2)+query(qs,qe,2*ind+1,seg,(s+e)/2+1,e); } }
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.*; public class Solution { public static void main(String[] args) { Scanner in=new Scanner(System.in); int n=in.nextInt(); int p[][]=new int[n][2]; for (int i=0; i<n; i++) p[i][0]=in.nextInt(); HashSet<Integer> set=new HashSet<>(); for(int i=0; i<n; i++) { p[i][1]=in.nextInt(); set.add(p[i][1]); } ArrayList<Integer> list=new ArrayList<>(set); Collections.sort(list); HashMap<Integer, Integer> map=new HashMap<>(); for(int i=0; i<list.size(); i++) map.put(list.get(i), i); for(int i=0; i<n; i++) p[i][1]=map.get(p[i][1]); Arrays.sort(p, (p1, p2)->{ return p1[0]-p2[0]; }); System.out.println(solve(p, n)); } public static void set(long arr[], int index, int value) { for(; index<arr.length; index=index|(index+1)) arr[index]+=value; } public static long get(long arr[], int index) { long sum=0L; for(; index>=0; index=(index&(index+1))-1) sum+=arr[index]; return sum; } public static long solve(int p[][], int n) { long bitx[]=new long[n]; long bity[]=new long[n]; long ans=0L; for(int i=0; i<n; i++) { ans+=get(bitx, p[i][1])*p[i][0]-get(bity, p[i][1]); set(bitx, p[i][1], 1); set(bity, p[i][1], p[i][0]); // System.out.println(Arrays.toString(p[i])+" "+Arrays.toString(bitx)+" "+Arrays.toString(bity)+" "+ans); } return ans; } }
1311_F. Moving Points
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i. Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0. Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points. The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct. The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point. Output Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Examples Input 3 1 3 2 -100 2 3 Output 3 Input 5 2 1 4 3 5 2 2 2 3 4 Output 19 Input 2 2 1 -3 0 Output 0
{ "input": [ "3\n1 3 2\n-100 2 3\n", "2\n2 1\n-3 0\n", "5\n2 1 4 3 5\n2 2 2 3 4\n" ], "output": [ "3\n", "0\n", "19\n" ] }
{ "input": [], "output": [] }
CORRECT
python3
''' Hey stalker :) ''' INF = 10 ** 10 def main(): #print = out.append ''' Cook your dish here! ''' n = get_int() points = get_list() speeds = get_list() point_indexes = dict() sorted_indexes = dict() for i, ele in enumerate(points): point_indexes[ele] = i for i, ele in enumerate(sorted(points)): sorted_indexes[ele] = i points.sort(reverse = True) points.sort(key=lambda x: speeds[point_indexes[x]], reverse = True) stree = SegTreeSum(n) scount = SegTreeSum(n) res = 0 #print(points, point_indexes, sorted_indexes) for i in points: index = sorted_indexes[i] res += stree.query(index, n-1) - i*scount.query(index, n-1) stree.modify(index, i) scount.modify(index, 1) #print(res) print(res) class SegTreeSum: def __init__(self, size): self.n = 1 << size.bit_length() self.li = [0]*(self.n<<1) def modify(self, index, ele): index += self.n self.li[index] = ele while index>1: self.li[index>>1] = self.li[index] + self.li[index^1] index >>= 1 def get(self, index): return self.li[self.n + index] def query(self, l, r): #print('L - ', l, 'R - ',r) #[print(i, self.li[i]) for i in range(len(self.li))] res = 0 l, r = l+self.n, r+self.n while l<=r: #print(l, r) if l&1==1: res += self.li[l] if r&1!=1: res += self.li[r] l = (l+1)>>1 r = (r-1)>>1 return res ''' Pythonista fLite 1.1 ''' import sys from collections import defaultdict, Counter, deque # from bisect import bisect_left, bisect_right # from functools import reduce # import math input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__ out = [] get_int = lambda: int(input()) get_list = lambda: list(map(int, input().split())) main() #[main() for _ in range(int(input()))] print(*out, sep='\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
java
// package codeforce.Training1900; import java.io.PrintWriter; import java.util.*; public class MovingPoints { // well this segment tree only can do range query and point update, so if you want range update, please go to lazy segment tree static class SegTree { private int N; // Let UNIQUE be a value which does NOT // and will not appear in the segment tree private long UNIQUE = 0; // Segment tree values private long[] tree; public SegTree(int size) { tree = new long[2 * (N = size)]; java.util.Arrays.fill(tree, UNIQUE); } public SegTree(long[] values) { this(values.length); for (int i = 0; i < N; i++) modify(i, values[i]); } // This is the segment tree function we are using for queries. // The function must be an associative function, meaning // the following property must hold: f(f(a,b),c) = f(a,f(b,c)). // Common associative functions used with segment trees // include: min, max, sum, product, GCD, and etc... private long function(long a, long b) { if (a == UNIQUE) return b; else if (b == UNIQUE) return a; return a + b; // sum over a range //return (a > b) ? a : b; // maximum value over a range //return (a < b) ? a : b; // minimum value over a range // return a * b; // product over a range (watch out for overflow!) } // Adjust point i by a value, O(log(n)) public void modify(int i, long value) { //tree[i + N] = function(tree[i + N], value); tree[i + N] = value; for (i += N; i > 1; i >>= 1) { tree[i >> 1] = function(tree[i], tree[i ^ 1]); } } // Query interval [l, r), O(log(n)) ----> notice the exclusion of r public long query(int l, int r) { long res = UNIQUE; for (l += N, r += N; l < r; l >>= 1, r >>= 1) { if ((l & 1) != 0) res = function(res, tree[l++]); if ((r & 1) != 0) res = function(res, tree[--r]); } if (res == UNIQUE) { //throw new IllegalStateException("UNIQUE should not be the return value."); return 0; } return res; } } // MUST SEE BEFORE SUBMISSION // check whether int part would overflow or not, especially when it is a * b!!!! public static void main(String[] args) { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); // int t = sc.nextInt(); int t = 1; for (int i = 0; i < t; i++) { solve(sc, pw); } pw.close(); } static long maxRight = Integer.MAX_VALUE; static void solve(Scanner in, PrintWriter out){ int n = in.nextInt(); int[][] arr = new int[n][2]; for (int i = 0; i < n; i++) { arr[i][0] = in.nextInt(); } for (int i = 0; i < n; i++) { arr[i][1] = in.nextInt(); } LinkedList<int[]> rev = new LinkedList<>(); LinkedList<int[]> ok = new LinkedList<>(); long ans = 0; for (int i = 0; i < n; i++) { if (arr[i][1] < 0){ rev.add(new int[]{-arr[i][0], -arr[i][1]}); }else{ ok.add(arr[i]); } } ans = sameDirection(rev) + sameDirection(ok); LinkedList<int[]> qs = new LinkedList<>(); LinkedList<int[]> ql = new LinkedList<>(); for (int i = 0; i < n; i++) { if (arr[i][1] < 0){ qs.add(arr[i]); }else{ ql.add(arr[i]); } } ans += differenDirection(qs, ql); out.println(ans); } static long sameDirection(LinkedList<int[]> q){ long ans = 0; int n = q.size(); SegTree sg = new SegTree(n + 5); SegTree ct = new SegTree(n + 5); List<int[]> qq = new ArrayList<>(); Collections.sort(q, (a, b) -> (a[0] - b[0])); for (int i = 0; i < n; i++) { int[] fk = q.poll(); qq.add(new int[]{fk[0], fk[1], i}); } Collections.sort(qq, (a, b) -> (a[1] - b[1])); for(int[] arr : qq){ int pos = arr[0], idx = arr[2]; long get = sg.query(0, idx); long tot = ct.query(0, idx); ans += (get - (maxRight - pos) * tot); sg.modify(idx, maxRight - pos); ct.modify(idx, 1); } return ans; } static long differenDirection(LinkedList<int[]> qs, LinkedList<int[]> ql){ Collections.sort(qs, (a, b) -> (a[0] - b[0])); Collections.sort(ql, (a, b) -> (a[0] - b[0])); long ans = 0; int cnt = 0; long pre = 0; while (ql.size() > 0){ int[] shit = ql.poll(); int pos = shit[0]; while (qs.size() > 0 && qs.peek()[0] < pos){ int[] get = qs.poll(); cnt++; pre += (maxRight - get[0]); } ans += (pre - cnt * (maxRight - pos)); } return 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.lang.reflect.Array; public class template { final static int MOD = 1000000007; final static int intMax = 1000000000; final static int intMin = -1000000000; final static int[] DX = { 0, 0, -1, 1 }; final static int[] DY = { -1, 1, 0, 0 }; static int T; static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[360]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } public static void main(String[] args) throws Exception { Reader in = new Reader(); int n = in.nextInt(); int[] x = new int[n]; int[] v = new int[n]; for(int i = 0; i < n; ++i) { x[i] = in.nextInt(); } for(int i = 0; i < n; ++i) { v[i] = in.nextInt(); } p[] points = new p[n]; for(int i = 0; i < n; ++i) { points[i] = new p(x[i], v[i]); } Arrays.sort(points, (a, b) -> a.x - b.x); for(int i = 0; i < n; ++i) { points[i].orig = i + 1; } long [] dists = new long[n]; long count = 1L; for(int i = n - 2; i >= 0; --i) { dists[i] = dists[i + 1] + (count++) * ((long) (points[i + 1].x - points[i].x)); } long tot = 0; for(int i = 0; i < n; ++i) { tot += dists[i]; } BinaryIndexedTree num = new BinaryIndexedTree(n); BinaryIndexedTree inds = new BinaryIndexedTree(n); Arrays.sort(points, (a, b) -> a.v == b.v ? b.orig - a.orig : b.v - a.v); for(int i = 0; i < n; ++i) { long amt = num.query(points[i].orig); long totinds = inds.query(points[i].orig); tot -= amt * ((long)points[i].x) - totinds; num.update(points[i].orig, 1); inds.update(points[i].orig, points[i].x); } System.out.println(tot); in.close(); } static class p{ int x, v, orig; p(int xi, int vi){ x = xi; v = vi; } } static class BinaryIndexedTree { public long[] tree; public BinaryIndexedTree(int n) { tree = new long[n+5]; } public void update(int index, int val) { index++; while(index < tree.length) { tree[index] += (long) val; index += index & -index; } } //sum from 0 to index public long query(int index) { long ret = 0; index++; while(index > 0) { ret += tree[index]; index -= index & -index; } return ret; } //sum from a to b public long query(int a, int b) { return query(b)-query(a-1); } } }
1311_F. Moving Points
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i. Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0. Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points. The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct. The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point. Output Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Examples Input 3 1 3 2 -100 2 3 Output 3 Input 5 2 1 4 3 5 2 2 2 3 4 Output 19 Input 2 2 1 -3 0 Output 0
{ "input": [ "3\n1 3 2\n-100 2 3\n", "2\n2 1\n-3 0\n", "5\n2 1 4 3 5\n2 2 2 3 4\n" ], "output": [ "3\n", "0\n", "19\n" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <typename C> class fenwick { vector<C> t; int n; public: fenwick(int _n) : n(_n) { t.resize(n); } fenwick(const vector<C>& a) : n(a.size()) { t.resize(n); for (int i = 0; i < n; ++i) { upd(i, a[i]); } } C get(int k) { C res = 0; for (k; k >= 0; k = (k & (k + 1)) - 1) { res += t[k]; } return res; } void upd(int k, C x) { for (k; k < n; k |= k + 1) { t[k] += x; } } C get(int l, int r) { return get(r) - get(l - 1); } }; int main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); int n; cin >> n; vector<int> x(n), v(n); for (int i = 0; i < n; ++i) { cin >> x[i]; } map<int, int> mapchik; for (int i = 0; i < n; ++i) { cin >> v[i]; mapchik[v[i]] = 1; } int sz = 0; for (auto& [a, b] : mapchik) { b = sz++; } for (int i = 0; i < n; ++i) { v[i] = mapchik[v[i]]; } vector<int> ord(n); iota(ord.begin(), ord.end(), 0); sort(ord.begin(), ord.end(), [&](int i, int j) { return x[i] < x[j]; }); long long ans = 0; fenwick<int> cnt(sz); fenwick<long long> sum(sz); for (int i : ord) { ans += 1ll * cnt.get(0, v[i]) * x[i] - sum.get(0, v[i]); cnt.upd(v[i], 1); sum.upd(v[i], x[i]); } 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; vector<long long> x, v; int idX(int val) { return (lower_bound(x.begin(), x.end(), val) - x.begin()); } int idV(int val) { return (lower_bound(v.begin(), v.end(), val) - v.begin()); } struct FenwickTree { vector<long long> bit; int n; FenwickTree(int n) { this->n = n; bit.assign(n, 0); } long long sum(int r) { long long ret = 0; for (; r >= 0; r = (r & (r + 1)) - 1) ret += bit[r]; return ret; } long long sum(int l, int r) { return sum(r) - sum(l - 1); } void upd(int idx, int delta) { for (; idx < n; idx = idx | (idx + 1)) bit[idx] += delta; } }; void nikal_pehli_fursat_mai() { int n; cin >> n; vector<array<long long, 2>> arr(n); long long ans = 0; for (int i = 0; i < n; i++) { cin >> arr[i][0]; x.push_back(arr[i][0]); } for (int i = 0; i < n; i++) { cin >> arr[i][1]; v.push_back(abs(arr[i][1])); } sort(v.begin(), v.end()); sort(x.begin(), x.end()); sort(arr.begin(), arr.end()); FenwickTree pos(n), neg(n), zero(n), cntp(n), cntn(n), cntz(n); for (int i = 0; i < n; i++) { int id = idV(abs(arr[i][1])); if (arr[i][1] > 0) { cntp.upd(id, 1); pos.upd(id, arr[i][0]); } else if (arr[i][1] < 0) { cntn.upd(id, 1); neg.upd(id, arr[i][0]); } else { cntz.upd(id, 1); zero.upd(id, arr[i][0]); } } for (int i = 0; i < n; i++) { int id = idV(abs(arr[i][1])); if (arr[i][1] > 0) { ans += pos.sum(id, n - 1) - cntp.sum(id, n - 1) * arr[i][0]; cntp.upd(id, -1); pos.upd(id, -arr[i][0]); } else if (arr[i][1] < 0) { ans += pos.sum(0, n - 1) - cntp.sum(0, n - 1) * arr[i][0]; ans += zero.sum(0, n - 1) - cntz.sum(0, n - 1) * arr[i][0]; ans += neg.sum(0, id) - cntn.sum(0, id) * arr[i][0]; cntn.upd(id, -1); neg.upd(id, -arr[i][0]); } else { ans += pos.sum(id, n - 1) - cntp.sum(id, n - 1) * arr[i][0]; ans += zero.sum(0, n - 1) - cntz.sum(0, n - 1) * arr[i][0]; cntz.upd(id, -1); zero.upd(id, -arr[i][0]); } } cout << ans << "\n"; } int main() { ios::sync_with_stdio(0); cin.tie(0); int tc = 1; while (tc--) { nikal_pehli_fursat_mai(); } }
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 FT { vector<long long> f; FT(long long n) { f.resize(n + 1, 0); } long long sum(long long r) { long long res = 0; for (; r >= 0; r = (r & (r + 1)) - 1) { res += f[r]; } return res; } long long sum(long long l, long long r) { return sum(r) - sum(l - 1); } void upd(long long i, long long x) { for (; i < (long long)f.size(); i = (i | (i + 1))) f[i] += x; } }; map<long long, long long> f; signed main() { ios ::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n; cin >> n; vector<pair<long long, long long>> a(n); for (long long i = 0; i < n; i++) cin >> a[i].first; for (long long i = 0; i < n; i++) cin >> a[i].second, f[a[i].second] = 0; long long s = 0; for (auto u : f) f[u.first] = s++; sort(a.begin(), a.end()); FT ft1(s + 1), ft2(s + 1); long long ans = 0; for (auto u : a) { long long x = u.first, y = u.second; long long v = f[y]; long long cnt = ft1.sum(0, v); long long sum = ft2.sum(0, v); ans += x * cnt - sum; ft1.upd(v, 1); ft2.upd(v, x); } 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> #pragma GCC optimize("fast-math") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC diagnostic ignored "-Woverflow" using namespace std; const short INF16 = ~(1 << 15); const int INF32 = 2e9 + 11; const int SZ = (1 << 19); const int SZ2 = int(1e6 + 11); const int mod = 998244353; const int mod2 = 998244353; const int PIVO = 5e5 + 11; const long long INF64 = 8e18; const long long P1 = 53; const long long P2 = 47; const long double eps = 1e-10; const long double PI = acosl(-1); inline long long MOD(long long x) { x %= mod, x += mod; return x % mod; } inline long long MOD(long long x, long long m) { x %= m, x += m; return x % m; } inline long long T(int x) { return 1ll * x * (x + 1) >> 1; } inline long long TT(int x) { return 1ll * x * (x + 1) * (x + 2) / 6; } inline long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } inline long long lcm(long long a, long long b) { return a * b / gcd(a, b); } long long fpow(long long a, long long b, long long mod) { if (!b) return 1; if (b == 1) return a % mod; if (b & 1) return fpow(a, b ^ 1, mod) * a % mod; long long d = fpow(a, b >> 1, mod); return d * d % mod; } long long fpow(long long a, long long b) { if (!b) return 1; if (b == 1) return a; if (b & 1) return fpow(a, b ^ 1) * a; long long d = fpow(a, b >> 1); return d * d; } long long n; pair<long long, long long> t[SZ << 1]; pair<long long, long long> operator+(pair<long long, long long> a, pair<long long, long long> b) { return {a.first + b.first, a.second + b.second}; } void Biuld(long long v, long long l, long long r) { if (r - l == 1) return; long long mid = (l + r) >> 1; Biuld(v << 1, l, mid); Biuld(v << 1 | 1, mid, r); t[v] = t[v << 1] + t[v << 1 | 1]; } void upd(long long v, long long l, long long r, long long k, long long x) { if (r - l == 1) { t[v] = {x, 0}; return; } long long mid = (l + r) >> 1; if (k < mid) upd(v << 1, l, mid, k, x); else upd(v << 1 | 1, mid, r, k, x); t[v] = t[v << 1] + t[v << 1 | 1]; } pair<long long, long long> get(long long v, long long l, long long r, long long ql, long long qr) { if (qr <= l || r <= ql) return {0, 0}; if (ql <= l && r <= qr) return t[v]; long long mid = (l + r) >> 1; return get(v << 1, l, mid, ql, qr) + get(v << 1 | 1, mid, r, ql, qr); } bool cmp(pair<pair<long long, long long>, long long> &a, pair<pair<long long, long long>, long long> &b) { if (a.first.second != b.first.second) return a.first.second < b.first.second; return a.first.first < b.first.first; } void Solve(long long num_test) { cin >> n; vector<pair<pair<long long, long long>, long long>> v(n); for (auto &i : v) cin >> i.first.first; for (auto &i : v) cin >> i.first.second; sort(v.begin(), v.end(), cmp); for (long long i = 0; i < n; i++) { t[SZ + i] = {v[i].first.first, 1}; v[i].second = i; } Biuld(1, 0, SZ); sort(v.begin(), v.end()); long long ans = 0; for (long long i = 0; i < n; i++) { long long id = v[i].second; ans += get(1, 0, SZ, id, n).first - get(1, 0, SZ, id, n).second * v[i].first.first; upd(1, 0, SZ, id, 0); } cout << ans << endl; } signed main() { ios_base::sync_with_stdio(NULL); cin.tie(NULL); cout.tie(NULL); cout << fixed << setprecision(10); srand(time(NULL)); long long test = 1; for (long long i = 1; i <= test; i++) Solve(i); }
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 ld = double; const ll MODBASE = 1000000007LL; const int MAXN = 200010; const int MAXM = 1000; const int MAXK = 16; const int MAXQ = 200010; struct Car { int x, v; Car(int x = 0, int v = 0) : x(x), v(v) {} }; bool operator<(Car a, Car b) { return a.x < b.x; } int n; pair<ll, ll> t[MAXN]; Car a[MAXN]; set<int> s; vector<int> b; pair<ll, ll> get(int u) { pair<ll, ll> res = pair<ll, ll>(0, 0); while (u) { res.first += t[u].first; res.second += t[u].second; u = u - (u & (-u)); } return res; } void update(int u, int gt, int num) { while (u <= 200000) { t[u].first += gt; t[u].second += num; u = u + (u & (-u)); } } int getPos(int u) { int dau = 0, cuoi = int((b).size()) - 1; while (dau <= cuoi) { int mid = (dau + cuoi) >> 1; if (b[mid] >= u) cuoi = mid - 1; else dau = mid + 1; } return dau + 1; } int main() { ios::sync_with_stdio(0); cin.tie(nullptr); cin >> n; for (int i = (1); i <= (n); i++) cin >> a[i].x; for (int i = (1); i <= (n); i++) cin >> a[i].v; sort(a + 1, a + n + 1); for (int i = (1); i <= (n); i++) s.insert(a[i].v); for (__typeof((s).begin()) it = (s).begin(); it != (s).end(); it++) b.push_back(*it); for (int i = (1); i <= (n); i++) a[i].v = getPos(a[i].v); ll res = 0; for (int i = (1); i <= (n); i++) { pair<ll, ll> r = get(a[i].v); res += (ll)a[i].x * r.second - r.first; update(a[i].v, a[i].x, 1); } cout << res; return 0; }
1311_F. Moving Points
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i. Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0. Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points. The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct. The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point. Output Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Examples Input 3 1 3 2 -100 2 3 Output 3 Input 5 2 1 4 3 5 2 2 2 3 4 Output 19 Input 2 2 1 -3 0 Output 0
{ "input": [ "3\n1 3 2\n-100 2 3\n", "2\n2 1\n-3 0\n", "5\n2 1 4 3 5\n2 2 2 3 4\n" ], "output": [ "3\n", "0\n", "19\n" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include <bits/stdc++.h> using ll = long long; using namespace std; template <class T> class SegmentTree { private: int _size = 1; T _def; std::vector<T> _nodes; std::function<T(T, T)> _op; std::function<T(T, T)> _update_op; public: SegmentTree(int size, T default_value, std::function<T(T, T)> op, std::function<T(T, T)> update_op) : _def(default_value), _op(op), _update_op(update_op) { while (_size < size) _size *= 2; _nodes = std::vector<T>(2 * _size, default_value); } SegmentTree(std::vector<T> nodes, T default_value, std::function<T(T, T)> op, std::function<T(T, T)> update_op) : _def(default_value), _op(op), _update_op(update_op) { int real_size = nodes.size(); while (_size < real_size) _size *= 2; _nodes = std::vector<T>(2 * _size, default_value); for (int i = 0; i < real_size; ++i) _nodes[i + _size] = nodes[i]; for (int i = _size - 1; i >= 0; --i) _nodes[i] = _op(_nodes[i * 2], _nodes[i * 2 + 1]); } T find(int l, int r) { return find(l, r, 1, 0, _size); } T find(int l, int r, int index, int cover_l, int cover_r) { if (cover_r <= l || r <= cover_l) return _def; if (l <= cover_l && cover_r <= r) return _nodes[index]; return _op(find(l, r, index * 2, cover_l, (cover_l + cover_r) >> 1), find(l, r, index * 2 + 1, (cover_l + cover_r) >> 1, cover_r)); } void update(int index, T value) { index += _size; _nodes[index] = _update_op(_nodes[index], value); while (index > 0) { index >>= 1; _nodes[index] = _op(_nodes[index * 2], _nodes[index * 2 + 1]); } } }; struct Point { ll x, v; bool operator<(const Point &other) const { return x < other.x; } }; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); int n; cin >> n; int x, v; set<int> vs; vector<Point> ps; for (ll i = 0, iLen = (n); i < iLen; ++i) { cin >> x; ps.push_back(Point{x, 0}); } for (ll i = 0, iLen = (n); i < iLen; ++i) { cin >> v; ps[i].v = v; vs.insert(v); } map<int, int> v_comp; int i = 0; for (auto &v : vs) { v_comp[v] = i++; } int v_size = v_comp.size() + 10; auto x_cnt = SegmentTree<ll>( v_size, 0, [](ll x, ll y) { return x + y; }, [](ll x, ll y) { return x + y; }); auto x_acc = SegmentTree<ll>( v_size, 0, [](ll x, ll y) { return x + y; }, [](ll x, ll y) { return x + y; }); for (ll i = 0, iLen = (n); i < iLen; ++i) { ps[i].v = v_comp[ps[i].v]; x_cnt.update(ps[i].v, 1); x_acc.update(ps[i].v, ps[i].x); } sort(ps.begin(), ps.end()); ll ans = 0; for (auto &[x, v] : ps) { x_cnt.update(v, -1); x_acc.update(v, -x); ans += x_acc.find(v, v_size) - x_cnt.find(v, v_size) * x; } 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 transform(long long int a[], long long int n) { map<long long int, vector<long long int> > m; for (long long int i = 0; i < (n); i++) m[a[i]].push_back(i); long long int rank = 1; for (auto c : m) { for (auto i : c.second) a[i] = rank; rank++; } } const long long int N = 2e5 + 5; long long int n = -1e9; void update(long long int B[], long long int x, long long int val) { for (; x <= n; x += x & -x) B[x] += val; } long long int query(long long int B[], long long int x) { long long int sum = 0; for (; x > 0; x -= x & -x) sum += B[x]; return sum; } int main() { long long int num; cin >> num; long long int x[num + 5], v[num + 5], ans = 0; vector<pair<long long int, long long int> > pts; for (long long int i = 0; i < (num); i++) cin >> x[i]; for (long long int i = 0; i < (num); i++) cin >> v[i]; for (long long int i = 0; i < (num); i++) pts.push_back(make_pair(x[i], v[i])); sort((pts).begin(), (pts).end()); for (long long int i = 0; i < (num); i++) x[i] = pts[i].first, v[i] = pts[i].second; transform(v, num); n = *max_element(v, v + num); long long int pref[N] = {0}, freq[N] = {0}; update(pref, v[num - 1], x[num - 1]); update(freq, v[num - 1], 1); for (long long int i = num - 2; i > (-1); i--) { long long int sum = query(pref, n) - query(pref, v[i] - 1); long long int nump = query(freq, n) - query(freq, v[i] - 1); ans += (sum - nump * x[i]); update(pref, v[i], x[i]); update(freq, v[i], 1); } cout << ans << endl; }
1311_F. Moving Points
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i. Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0. Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Input The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points. The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct. The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point. Output Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points). Examples Input 3 1 3 2 -100 2 3 Output 3 Input 5 2 1 4 3 5 2 2 2 3 4 Output 19 Input 2 2 1 -3 0 Output 0
{ "input": [ "3\n1 3 2\n-100 2 3\n", "2\n2 1\n-3 0\n", "5\n2 1 4 3 5\n2 2 2 3 4\n" ], "output": [ "3\n", "0\n", "19\n" ] }
{ "input": [], "output": [] }
CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MAX = 2e5 + 9; int n, a[MAX], speed[MAX], b[MAX]; int idx[MAX]; vector<pair<int, int> > v; unordered_map<int, int> startRange; long long tree[(MAX << 2)][2], val; int l, r, idxToUpd; void build(int id, int low, int high, int pos) { if (low == high) { tree[pos][id] = b[low]; return; } int mid = ((low + high) >> 1); build(id, low, mid, (pos << 1)); build(id, mid + 1, high, (pos << 1 | 1)); tree[pos][id] = tree[(pos << 1)][id] + tree[(pos << 1 | 1)][id]; } long long qwr(int id, int low, int high, int pos) { if (l > high || r < low) { return 0; } if (l <= low && r >= high) { return tree[pos][id]; } int mid = ((low + high) >> 1); return qwr(id, low, mid, (pos << 1)) + qwr(id, mid + 1, high, (pos << 1 | 1)); } void upd(int id, int low, int high, int pos) { if (low == high) { tree[pos][id] = val; return; } int mid = ((low + high) >> 1); if (idxToUpd <= mid) { upd(id, low, mid, (pos << 1)); } else { upd(id, mid + 1, high, (pos << 1 | 1)); } tree[pos][id] = tree[(pos << 1)][id] + tree[(pos << 1 | 1)][id]; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; for (int i = 1; i <= n; ++i) { cin >> a[i]; } for (int i = 1; i <= n; ++i) { cin >> speed[i]; v.push_back({speed[i], i}); } sort(v.begin(), v.end()); for (int i = 0; i < n; ++i) { idx[v[i].second] = i + 1; if (startRange.find(v[i].first) == startRange.end()) { startRange[v[i].first] = i + 1; } } for (int i = 1; i <= n; ++i) { b[idx[i]] = a[i]; } build(0, 1, n, 1); v.clear(); for (int i = 1; i <= n; ++i) { v.push_back({a[i], i}); } sort(v.begin(), v.end()); long long ans = 0; for (int i = 0; i < n; ++i) { l = startRange[speed[v[i].second]]; r = n; int cntRemoved = qwr(1, 1, n, 1); long long sum = qwr(0, 1, n, 1); long long rest = (n - startRange[speed[v[i].second]] + 1) - cntRemoved; ans += sum - rest * (long long)v[i].first; idxToUpd = idx[v[i].second]; val = 1; upd(1, 1, n, 1); val = 0; upd(0, 1, n, 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
java
import java.awt.*; import java.util.*; import java.io.*; public class mp { public static void main(String[] args) throws Exception { Scanner sc= new Scanner(System.in); n= sc.nextInt(); prvi= new long[n+1]; prvi1= new int[n+2]; Point p[]=new Point[n]; for (int i= 0; i< n; i++)p[i]= new Point(sc.nextInt(),0); for (int i= 0; i< n; i++)p[i].y= sc.nextInt(); Arrays.sort(p, new Comparator<Point>() { public int compare(Point o1, Point o2) { return o1.y-o2.y; } }) ; int x= 0; int prej= -1; for (int i=0;i<n;i++){ if (i>0 && prej!=p[i].y)x++; prej=p[i].y; p[i].y=x; } Arrays.sort(p, new Comparator<Point>() { public int compare(Point o1, Point o2) { return o1.x-o2.x; } }); long odg=0; for (int i=n-1;i>=0;i--){ long sestevek=polni(p[i].y); long sestevek1=polni1(p[i].y); gor(p[i].y,p[i].x); gor1(p[i].y); odg+=Math.abs(sestevek-p[i].x*sestevek1); } System.out.println(odg); } static long prvi[]; static int prvi1[]; static int n; static void gor(int i,int val){ for (i++;i<=n;i+=i&(-i)){ prvi[i]+=val; } } static void gor1(int i){ for (i++;i<=n;i+=i&(-i)){ prvi1[i]+=1; } } static long sestej(int i){ long res=0; for (i++;i>0;i-=i&(-i)){ res+=prvi[i]; } return res; } static long sestej1(int i){ long res=0; for (i++;i>0;i-=i&(-i)){ res+=prvi1[i]; } return res; } static long polni(int i){ return sestej(n-1)-sestej(i-1); } static long polni1(int i){ return sestej1(n-1)- sestej1(i-1); } static class beri { BufferedReader br; StringTokenizer st; public beri() { br = new BufferedReader(new InputStreamReader(System.in)); } String naslj() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(naslj()); } long nextLong() { return Long.parseLong(naslj()); } double nextDouble() { return Double.parseDouble(naslj()); } String beriNaslj() { String beri= ""; try { beri= br.readLine(); } catch (IOException e) { e.printStackTrace(); } return beri; } } }
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 mx = 200005; long long bit[mx][2]; int arr[mx]; int speed[mx]; vector<pair<int, int> > vec; void update(int idx, int val) { for (int i = idx; i < mx; i += (i & -i)) { bit[i][0] += 1LL * val; bit[i][1]++; } } pair<long long, long long> query(int idx) { pair<long long, long long> ans = make_pair(0, 0); for (int i = idx; i > 0; i -= (i & -i)) { ans.first += bit[i][0]; ans.second += bit[i][1]; } return ans; } int main() { map<int, int> mpp; int n, i, j, k, x, v; scanf("%d", &n); for (i = 1; i <= n; i++) { scanf("%d", arr + i); } for (i = 1; i <= n; i++) { scanf("%d", speed + i); vec.push_back(make_pair(speed[i], 0)); } sort(vec.begin(), vec.end()); for (i = 0; i < n; i++) { mpp[vec[i].first] = i + 1; } vec.clear(); for (i = 1; i <= n; i++) { vec.push_back(make_pair(arr[i], mpp[speed[i]])); } sort(vec.begin(), vec.end()); pair<long long, long long> temp; long long ans = 0, now; for (i = 0; i < n; i++) { temp = query(vec[i].second); now = 1LL * temp.second * vec[i].first - temp.first; ans += now; update(vec[i].second, vec[i].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; struct BIT { vector<long long int> bit; int n; BIT(long long int N) { n = N; bit.assign(n + 1, 0); } void add(int idx, int delta) { while (idx <= n) { bit[idx] += delta; idx += (idx & -idx); } } long long int sum(int idx) { long long int res = 0; while (idx > 0) { res += bit[idx]; idx -= (idx & -idx); } return res; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; vector<pair<int, int> > p(n); for (auto &i : p) cin >> i.first; for (auto &i : p) cin >> i.second; sort(p.begin(), p.end()); vector<int> v; for (auto i : p) v.push_back(i.second); sort(v.begin(), v.end()); map<int, int> mp; int cnt = 0; for (auto i : v) mp[i] = ++cnt; for (auto &i : p) i.second = mp[i.second]; long long int ans = 0; BIT tree1(n), tree2(n); for (auto i : p) { long long int cnt = tree1.sum(i.second); long long int sum_x = tree2.sum(i.second); ans += cnt * i.first - sum_x; tree1.add(i.second, 1); tree2.add(i.second, i.first); } cout << ans << '\n'; }