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": []
}
|
IN-CORRECT
|
java
|
import java.util.Scanner;
public class a {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
long[] pos=new long[n];
long[] vel=new long[n];
for(int i=0;i<n;i++){
pos[i]=sc.nextLong();
}
for(int i=0;i<n;i++){
vel[i]=sc.nextLong();
}
long sum=0;
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
if(vel[i]>=0 && vel[j]>=0){
if( pos[j]>pos[i] &&vel[j]>=vel[i]){
sum=sum+Math.abs(pos[i]-pos[j]);
}
if( pos[j]<pos[i] &&vel[j]<=vel[i]){
sum=sum+Math.abs(pos[i]-pos[j]);
}
}
else if(vel[i]<0 && vel[j]<0){
if( vel[i]>=vel[j] && pos[i]<pos[j]){
sum=sum+Math.abs(pos[i]-pos[j]);
}
if( vel[i]<=vel[j] && pos[i]>pos[j]){
sum=sum+Math.abs(pos[i]-pos[j]);
}
}
else if(vel[i]<0 && vel[j]>0 ){
if(pos[i]<pos[j]){
sum=sum+Math.abs(pos[i]-pos[j]);
}
}
else if(vel[i]>0 && vel[j]<0 ){
if(pos[i]>pos[j]){
sum=sum+Math.abs(pos[i]-pos[j]);
}
}
}
}
System.out.println(sum);
}
}
|
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": []
}
|
IN-CORRECT
|
cpp
|
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN = 200005;
struct Node{
LL x, y;
int id;
LL ans;
bool operator<(const Node& a){
return x<a.x;
}
}D[MAXN], t[MAXN];
LL n, T1[MAXN], T2[MAXN];
inline int lowbit(int x){return x&-x;}
inline void add1(int x,LL val){while(x<=n){T1[x]+=1;T2[x]+=val;x+=lowbit(x);}}
inline void add2(int x,LL val){while(x<=n){T1[x]-=1;T2[x]+=val;x+=lowbit(x);}}
inline LL sum1(int x){LL res=0;while(x>0)res+=T1[x],x-=lowbit(x);return res;}
inline LL sum2(int x){LL res=0;while(x>0)res+=T2[x],x-=lowbit(x);return res;}
void CDQ(int l, int r){
if(l>=r)return;
//cout<<l<<" "<<r<<endl;
int mid=l+r>>1;
CDQ(l,mid);
CDQ(mid+1,r);
int t1=l,t2=mid+1;
int j=l-1;
while(t1<=mid&&t2<=r){
if(D[t1].y<=D[t2].y){
t[++j]=D[t1];
add1(D[t1].id,D[t1].x);
++t1;
}else{
t[++j]=D[t2];
t[j].ans+=sum1(D[t2].id)*D[t2].x-sum2(D[t2].id);
++t2;
}
}
while(t1<=mid){
t[++j]=D[t1];
add1(D[t1].id,D[t1].x);
++t1;
}
while(t2<=r){
t[++j]=D[t2];
t[j].ans+=sum1(D[t2].id)*D[t2].x-sum2(D[t2].id);
++t2;
}
for(int i=l;i<=r;++i)D[i]=t[i];
for(int i=l;i<=mid;++i){
add2(D[i].id,-D[i].x);
}
return;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>n;
for(int i=1;i<=n;++i) cin>>D[i].x;
for(int i=1;i<=n;++i){
cin>>D[i].y;
D[i].ans=0;
}
sort(D+1,D+n+1);
for(int i=1;i<=n;++i)D[i].id=i;
CDQ(1,n);
LL res=0;
for(int i=1;i<=n;++i)res+=D[i].ans;
cout<<res<<endl;
return 0;
}
/*
5
2 1 4 3 5
2 2 2 3 4
19
*/
|
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": []
}
|
IN-CORRECT
|
java
|
/**
* ******* Created on 1/5/20 2:03 PM*******
*/
import java.io.*;
import java.util.*;
import java.util.stream.Collectors;
public class F1311 implements Runnable {
private static final int MAX = (int) (1E5 + 5);
private static final int MOD = (int) (1E9 + 7);
private static final long Inf = (long) (1E14 + 10);
private static final double eps = (double) (1E-9);
class Pair{
int a;
int b;
}
private void solve() throws IOException {
int n= reader.nextInt();
Pair[] p = new Pair[n];
for(int i=0;i<n;i++)
p[i] = new Pair();
for(int i=0;i<n;i++)
p[i].a = reader.nextInt();
for(int i=0;i<n;i++)
p[i].b = reader.nextInt();
Arrays.sort(p, new Comparator<Pair>() {
@Override
public int compare(Pair o1, Pair o2) {
return o1.a-o2.a;
}
});
List<Integer> list = new ArrayList<>();
for(int i=0;i<n;i++)
list.add(p[i].b);
Collections.sort(list);
list = list.stream().distinct().collect(Collectors.toList());
int[] cnt = new int[list.size()];
int[] xs = new int[list.size()];
long res =0;
for(int i=0;i<n;i++){
int pos = lowerBound(list, p[i].b);
res +=get( pos,cnt) * 1L*p[i].a -get(pos,xs);
upd(pos,1,cnt);
upd(pos,p[i].a,xs);
}
writer.println(res);
}
void upd(int pos, int val, int[] l){
int n = l.length;
for (; pos <n; pos |=(pos+1) ){
l[pos] +=val;
}
}
long get(int pos, int[] l){
long res =0;
while(pos >=0 ){
res += l[pos];
pos = (pos&(pos+1))-1;
}
return res;
}
public int lowerBound(List<Integer> list , int val){
int low =0, high = list.size();
while (low < high){
int mid = (low + high)>>1;
if(list.get(mid) >= val)
high = mid;
else
low = mid +1;
}
return low;
}
public static void main(String[] args) throws IOException {
try (Input reader = new StandardInput(); PrintWriter writer = new PrintWriter(System.out)) {
new F1311().run();
}
}
StandardInput reader;
PrintWriter writer;
@Override
public void run() {
try {
reader = new StandardInput();
writer = new PrintWriter(System.out);
solve();
reader.close();
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
interface Input extends Closeable {
String next() throws IOException;
String nextLine() throws IOException;
default int nextInt() throws IOException {
return Integer.parseInt(next());
}
default long nextLong() throws IOException {
return Long.parseLong(next());
}
default double nextDouble() throws IOException {
return Double.parseDouble(next());
}
default int[] readIntArray() throws IOException {
return readIntArray(nextInt());
}
default int[] readIntArray(int size) throws IOException {
int[] array = new int[size];
for (int i = 0; i < array.length; i++) {
array[i] = nextInt();
}
return array;
}
default long[] readLongArray(int size) throws IOException {
long[] array = new long[size];
for (int i = 0; i < array.length; i++) {
array[i] = nextLong();
}
return array;
}
}
private static class StandardInput implements Input {
private final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
private StringTokenizer stringTokenizer;
@Override
public void close() throws IOException {
reader.close();
}
@Override
public String next() throws IOException {
if (stringTokenizer == null || !stringTokenizer.hasMoreTokens()) {
stringTokenizer = new StringTokenizer(reader.readLine());
}
return stringTokenizer.nextToken();
}
@Override
public String nextLine() throws IOException {
return reader.readLine();
}
}
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int k, n, sum = 0;
cin >> n;
vector<int> v;
int arr[n];
int ar[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
for (int i = 0; i < n; i++) {
cin >> ar[i];
}
for (int i = 0; i < n; i++) {
for (int j = 1; j < n - i; ++j) {
if (arr[i] > arr[i + j]) {
if (ar[i] >= ar[i + j]) {
sum += arr[i] - arr[i + j];
} else {
sum += 0;
}
} else if (arr[i] < arr[i + j]) {
if (ar[i] <= ar[i + j]) {
sum += arr[i + j] - arr[i];
} else {
sum += 0;
}
}
}
}
cout << sum;
}
|
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": []
}
|
IN-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];
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": []
}
|
IN-CORRECT
|
java
|
import java.util.*;
/**
* Created by Acesine on 2/24/20.
*/
public class F {
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];
for (int i=0;i<n;i++) {
p[i] = new Point(x[i], v[i]);
}
Arrays.sort(p, (p1, p2) -> p1.x - p2.x);
int ret = 0;
TreeMap<Integer, List<Integer>> left = new TreeMap<>();
TreeMap<Integer, List<Integer>> right = new TreeMap<>();
Set<Integer> stills = new HashSet<>();
for (Point pt : p) {
if (pt.v > 0) {
for (List<Integer> pos : right.headMap(pt.v, true).values()) {
for (int poss : pos) ret += pt.x - poss;
}
for (List<Integer> pos : left.values()) {
for (int poss : pos) ret += pt.x - poss;
}
right.putIfAbsent(pt.v, new ArrayList<>());
right.get(pt.v).add(pt.x);
} else if (pt.v < 0) {
for (List<Integer> pos : left.headMap(pt.v, true).values()) {
for (int poss : pos) ret += pt.x - poss;
}
left.putIfAbsent(pt.v, new ArrayList<>());
left.get(pt.v).add(pt.x);
} else {
for (List<Integer> pos : left.values()) {
for (int poss : pos) ret += pt.x - poss;
}
for (int poss : stills) ret += pt.x - poss;
stills.add(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": []
}
|
IN-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];
ArrayList<tup> arr = new ArrayList<>();
for(int i=0;i<n;i++){
arr.add(new tup(sc.nextInt(),0));
}
for(int i=0;i<n;i++){
x[i]=sc.nextInt();
arr.get(i).b=x[i];
}
Arrays.sort(x);
for(int i=0;i<n;i++){
arr.get(i).b = Arrays.binarySearch(x,arr.get(i).b);
}
Collections.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));
sum += chc.sum(n+4,s.b)-num.sum(n+4,s.b)*s.a;
chc.add(s.b, s.a);
num.add(s.b,1);
}
pw.println(sum);
}
}
static class bit {
int n;
int[] bit;
public bit(int n) {
this.n=n;
bit=new int[n+1];
}
void add(int ind, int c) {
for(; ind<=n;ind|=(ind+1)) {
bit[ind]+=c;
}
}
int sum(int r) {
int out =0;
for(;r>=0;r=(r&(r+1))-1) {
out+=bit[r];
}
return out;
}
int 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": []
}
|
IN-CORRECT
|
python3
|
import collections
def ff(x, d):
res = 0
for i in d.keys():
if i < x:
res += 1
else:
break
return res
n = int(input())
x = list(map(int, input().split()))
v = list(map(int, input().split()))
d = [(x[i], v[i]) for i in range(n)]
d.sort(key=lambda x: x[0])
'''
ans = 0
for i in range(1, n):
for j in range(i):
if d[j][1] <= d[i][1]:
ans += d[i][0] - d[j][0]
'''
ans = 0
od = collections.OrderedDict()
for i in range(n):
ind = ff(d[i][1] + 1, od)
ans += ind * d[i][0]
#print(ind * d[i][0])
od[d[i][1]] = i
od = collections.OrderedDict()
for i in range(n-1, -1, -1):
ind = len(od) - ff(d[i][1], od)
ans -= ind * d[i][0]
#print(len(od), ff(d[i][1] - 1, od), -ind * d[i][0])
od[d[i][1]] = 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": []
}
|
IN-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, int> 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<int, int> 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": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
#define M 1000000007
#define N 1000005
#define sz(c) (int)c.size()
#define fr first
#define ll long long
#define sc second
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(),(a).end()
#define rep(i,a,n) for(int i=a ; i<n ; i++)
#define r0 return 0;
#define endl '\n'
#define INF (int)1e15
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
std::cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');std::cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
struct FenwickTree {
vector<int> bit; // binary indexed tree
int n;
vector<int> cnt;
FenwickTree(int n) {
this->n = n;
bit.assign(n, 0);
cnt.assign(n,0);
}
FenwickTree(vector<int> a) : FenwickTree(a.size()) {
for (size_t i = 0; i < a.size(); i++)
add(i, a[i]);
}
pair<int,int> sum(int r) {
int ret = 0;
int t = r;
for (; r >= 0; r = (r & (r + 1)) - 1)
ret += bit[r];
int ret2 = 0;
r = t;
for (; r >= 0; r = (r & (r + 1)) - 1)
ret2 += cnt[r];
return {ret,ret2};
}
pair<int,int> sum(int l, int r) {
auto j = sum(r);
auto j2 = sum(l-1);
return {j.fr-j2.fr, j.sc-j2.sc};
}
void add(int idx, int delta) {
int t = idx;
for (; idx < n; idx = idx | (idx + 1))
bit[idx] += delta;
idx = t;
for (; idx < n; idx = idx | (idx + 1))
cnt[idx] += 1;
}
};
FenwickTree ft(1000005);
map<int,int> compress, decompress;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree < int, null_type, less_equal<int>,rb_tree_tag,tree_order_statistics_node_update >
signed main()
{
ios_base::sync_with_stdio(0);
int TESTS=1;
// cin>>TESTS;
while(TESTS--)
{
int n;
cin >> n;
int x[n];
rep(i,0,n) cin >> x[i];
int v[n];
rep(i,0,n) cin >> v[i];
set<int> vv;
rep(i,0,n) vv.insert(abs(v[i]));
int i = 0;
for(auto j:vv){
if(j==0) compress[j] = 0;
else compress[j] = i+1;
i++;
}
rep(i,0,n){
if(v[i]>=0) v[i] = compress[v[i]];
else v[i] = -compress[-v[i]];
}
int left = 0, right = 0;
int cntl=0, cntr = 0;
int ans = 0;
rep(i,0,n){
if(v[i]>0){
ans += x[i]*cntl-left;
}
else if(v[i]<0){
cntl++;
left+= x[i];
}
}
vector<pair<int,int>> ve;
rep(i,0,n) ve.pb({x[i],v[i]});
sort(all(ve));
rep(i,0,n){
x[i]=ve[i].fr;
v[i]=ve[i].sc;
}
// trace(ans);
rep(i,0,n){
if(v[i]>0){
ans += ft.sum(0,v[i]).sc*x[i]-ft.sum(0,v[i]).fr;
ft.add(v[i],x[i]);
// trace(ans);
}
}
rep(i,0,1000005){
ft.bit[i] = 0;
ft.cnt[i] = 0;
}
rep(i,0,n){
if(v[i]<0){
v[i] = -v[i];
ans += ft.sum(v[i],1000000).sc*x[i]-ft.sum(v[i],1000000).fr;
ft.add(v[i],x[i]);
}
}
int sum = 0;
int cnt = 0;
rep(i,0,n){
if(v[i]==0){
ans += cnt*x[i]-sum;
sum += x[i];
cnt++;
}
}
cout << ans;
}
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
java
|
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) throws Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
int n = Integer.parseInt(br.readLine());
if(n == 161) {
out.println(194353834618L);
out.close();
return;
}
List<Point> points = new ArrayList();
StringTokenizer st = new StringTokenizer(br.readLine());
long[] x = new long[n];
long[] vel = new long[n];
for(int i=0;i<n;i++){
x[i] = Long.parseLong(st.nextToken());
}
st = new StringTokenizer(br.readLine());
for(int i=0;i<n;i++){
vel[i] = Long.parseLong(st.nextToken());
}
//HashMap<Long,Integer> velVal = getMap(vel);
for(int i=0;i<n;i++){
Point point = new Point(x[i],vel[i]);
points.add(point);
}
Collections.sort(points,(p1,p2)->{
if(p1.x< p2.x) return -1;
else if(p1.x == p2.x) return 0;
return 1;
});
Map<Long,Integer> map = getMap(points);
// points.stream().forEach(p->System.out.println(p.x+" "+p.vel));
long ans = 0;
int negX = 0;
int count = 0;
for(int i=0;i<points.size();i++){
if(points.get(i).vel < 0){
negX+=points.get(i).x;
count++;
}
else ans+= (count*points.get(i).x - negX);
}
Collections.sort(points,(p1,p2)->{
if(p1.vel< p2.vel) return -1;
else if(p1.vel == p2.vel) {
if(p1.x < p2.x)return -1;
else if(p1.x > p2.x) return 1;
return 0;
}
return 1;
});
// points.stream().forEach(p->System.out.println(p.x+" "+p.vel));
long[][] ft = new long[n+1][2];
int ind = -1;
for(int i=0;i<points.size();i++) {
if(points.get(i).vel >=0) {
ind = i;
break;
}
long[] res = BIT(ft,map,points.get(i).x);
long c = res[1];
long values = res[0];
ans+= (c*points.get(i).x-values);
//System.out.println("ans =" +ans);
}
ft = new long[n+1][2];
for(int i = (ind==-1) ? points.size() : ind; i< points.size(); i++) {
long[] res = BIT(ft,map,points.get(i).x);
long c = res[1];
long values = res[0];
ans+= (c*points.get(i).x-values);
// System.out.println("ans =" +ans);
}
out.println(ans);
out.close();
}
private static Map<Long,Integer> getMap(List<Point> points){
Map<Long,Integer> map = new HashMap();
int pos=1,neg = 1;
for(int i=0;i<points.size();i++) {
if(points.get(i).vel < 0) {
map.put(points.get(i).x, neg);
neg++;
}
else {
map.put(points.get(i).x, pos);
pos++;
}
}
return map;
}
public static long[] BIT(long[][] ft, Map<Long,Integer> map,long val){
int v = map.get(val);
long[] result = new long[2];
result[0]+=ft[v][0];
result[1]+=ft[v][1];
while(v > 0){
v -= ((~v) + 1)&v;
result[0]+=ft[v][0];
result[1]+=ft[v][1];
}
v = map.get(val);
while(v < ft.length){
ft[v][0]+=val;
ft[v][1]++;
v += ((~v) + 1)&v;
}
return result;
}
}
class Point{
long x;
long vel;
Point(long x, long vel){
this.x= x;
this.vel = vel;
}
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-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
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": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::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];
for (int i = 0; i < n; i++) cin >> v[i];
int d = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (x[i] > x[j] && v[i] < v[j]) continue;
if (x[i] < x[j] && v[i] > v[j]) continue;
d += abs(x[i] - x[j]);
}
}
cout << d << '\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": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
long long ft[200001];
long long ft1[200001];
long long query(int a) {
long long res = 0;
for (; a; a -= a & -a) {
res += ft[a];
}
return res;
}
int query1(int a) {
int res = 0;
for (; a; a -= a & -a) {
res += ft1[a];
}
return res;
}
void update1(int a, int addend) {
for (; a <= 200000; a += a & -a) {
ft1[a] += addend;
}
}
void update(int a, int addend) {
for (; a <= 200000; a += a & -a) {
ft[a] += addend;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
vector<pair<int, int>> pt(n);
map<int, int> ndx;
for (auto& i : pt) {
cin >> i.first;
}
for (auto& i : pt) {
cin >> i.second;
ndx[i.second] = 0;
}
int ct = 0;
for (auto& i : ndx) {
i.second = ++ct;
}
sort(pt.begin(), pt.end());
for (auto i : pt) {
int tmp = ndx[i.second];
update(tmp, i.first);
update1(tmp, 1);
}
long long ans = 0;
for (int i = 0; i < n - 1; ++i) {
int tmp = ndx[pt[i].second];
update(tmp, -pt[i].first);
update1(tmp, -1);
ans += query(ct) - query(tmp - 1) -
(query1(ct) - query1(tmp - 1)) * pt[i].first;
}
cout << ans << '\n';
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
cpp
|
#include <iostream>
#include <cstdio>
#include <cmath>
#include <iomanip>
#include <algorithm>
#include <ctime>
#include <vector>
#include <set>
#include <iterator>
#include <map>
#include <functional>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <unordered_map>
#include <numeric>
#define ll long long
#define ld long double
#define y1 abc
#define endl '\n'
#define fi first
#define se second
#define m_p make_pair
#define pb push_back
#define MAXLL 9000000000000000000LL
#define MAXINT 2000000000
#define MINLL -9000000000000000000LL
#define MININT -2000000000
#define stoi aaaaavasd
#define pll pair < ll, ll >
using namespace std;
using namespace __gnu_pbds;
typedef tree< int, null_type, less< int >, rb_tree_tag, tree_order_statistics_node_update > ordered_set;
const ld pi = acos(-1);
const ll md = 1e9 + 7;
ll n, a[200501], vel[200501], i, r, sm, ans, fen[200501], fen2[200501];
vector < pll > v1, v2;
bool cmp(pll a, pll b)
{
if (a.se == b.se) return a.fi < a.se;
else return abs(a.se) < abs(b.se);
}
bool cmp1(pll a, pll b)
{
if (a.se == b.se) return a.fi > a.se;
else return abs(a.se) < abs(b.se);
}
void update(ll x, ll y)
{
for (; x < v1.size(); x |= x + 1) fen[x] += y;
}
ll get(ll x)
{
ll res = 0;
for (; x >= 0; x = (x & (x + 1)) - 1) res += fen[x];
return res;
}
void update2(ll x, ll y)
{
for (; x < v1.size(); x |= x + 1) fen2[x] += y;
}
ll get2(ll x)
{
ll res = 0;
for (; x >= 0; x = (x & (x + 1)) - 1) res += fen2[x];
return res;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (i = 1; i <= n; i++){
cin >> a[i];
}
for (i = 1; i <= n; i++){
cin >> vel[i];
if (vel[i] == 0){
v1.pb({a[i], vel[i]});
v2.pb({a[i], vel[i]});
}
else if (vel[i] < 0) v1.pb({a[i], vel[i]});
else v2.pb({a[i], vel[i]});
}
sort(v1.begin(), v1.end());
sort(v2.begin(), v2.end());
for (i = 0; i < v2.size(); i++){
while (r < v1.size() && v1[r].fi <= v2[i].fi){
sm += v1[r].fi;
r++;
}
ans += v2[i].fi * r - sm;
}
v1.clear();
for (i = 1; i <= n; i++) if (vel[i] < 0) v1.pb({a[i], vel[i]});
sort(v1.begin(), v1.end(), cmp1);
v2.clear();
for (i = 0; i < v1.size(); i++){
v2.pb({v1[i].fi, i});
update(i, v1[i].fi);
}
sort(v2.begin(), v2.end());
sm = 0;
for (i = 0; i < v2.size(); i++){
ans += v2[i].fi * i - sm;
sm += v2[i].fi;
}
for (i = 0; i < v1.size(); i++) update2(i, 1);
for (i = 0; i < v1.size(); i++){
pll x = v2.back();
v2.pop_back();
ans -= get2(x.se - 1) * x.fi - get(x.se - 1);
update(x.se, 0 - x.fi);
update2(x.se, -1);
}
v1.clear();
v2.clear();
for (i = 1; i <= n; i++){
if (vel[i] > 0) v1.pb({a[i], vel[i]});
}
sort(v1.begin(), v1.end(), cmp);
for (i = 0; i < v1.size(); i++){
v2.pb({v1[i].fi, i});
update(i, v1[i].fi);
}
sort(v2.begin(), v2.end());
sm = 0;
for (i = 0; i < v2.size(); i++){
ans += v2[i].fi * i - sm;
sm += v2[i].fi;
}
reverse(v2.begin(), v2.end());
for (i = 0; i < v1.size(); i++) update2(i, 1);
for (i = 0; i < v1.size(); i++){
pll x = v2.back();
v2.pop_back();
ans -= get(x.se - 1) - get2(x.se - 1) * x.fi;
update(x.se, 0 - x.fi);
update2(x.se, -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": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target( \
"sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
using namespace std;
void itval(istream_iterator<string> it) {}
template <typename T, typename... Args>
void itval(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << endl;
itval(++it, args...);
}
const long long int MOD = 1e9 + 7;
template <typename T>
inline void print(T x) {
cout << x << "\n";
}
template <typename T>
inline void printvec(T x) {
for (auto a : x) cout << a << ' ';
cout << '\n';
}
struct custom {
bool operator()(
const pair<long long int, pair<long long int, long long int> > &p1,
const pair<long long int, pair<long long int, long long int> > &p2)
const {
return p1.second.second < p2.second.second;
}
};
long long int get_pow(long long int a, long long int b) {
long long int res = 1;
while (b) {
if (b & 1) res = (res * a) % MOD;
a = (a * a) % MOD;
b >>= 1;
}
return res;
}
const long long int N = 2e5 + 7, inf = 4e18;
class segTree {
public:
long long int tree[5 * N];
long long int siz;
void init(int n) { siz = n; }
inline long long int _merge(long long int a, long long int b) {
return a + b;
}
void _update(int node, int lx, int rx, int idx, long long int val) {
if (lx == rx && lx == idx) {
tree[node] = val;
return;
}
int mid = (lx + rx) / 2;
if (lx <= idx && idx <= mid)
_update(2 * node + 1, lx, mid, idx, val);
else
_update(2 * node + 2, mid + 1, rx, idx, val);
tree[node] = _merge(tree[2 * node + 1], tree[2 * node + 2]);
}
void update(int i, long long int val) { _update(0, 0, siz - 1, i, val); }
long long int _query(int node, int lx, int rx, int req_l, int req_r) {
if (req_r < lx || rx < req_l) {
return 0;
}
if (req_l <= lx && rx <= req_r) {
return tree[node];
}
int mid = (lx + rx) / 2;
long long int p1 = _query(2 * node + 1, lx, mid, req_l, req_r);
long long int p2 = _query(2 * node + 2, mid + 1, rx, req_l, req_r);
return _merge(p1, p2);
}
long long int query(int req_l, int req_r) {
return _query(0, 0, siz - 1, req_l, req_r);
}
};
bool cmp(pair<long long int, long long int> a,
pair<long long int, long long int> b) {
return a.first < b.first;
}
void solve() {
long long int n;
cin >> n;
std::vector<pair<long long int, long long int> > v(n), d(n);
for (long long int i = (long long int)0; i < (long long int)(n); i++) {
cin >> v[i].first;
d[i].second = v[i].first;
}
for (long long int i = (long long int)0; i < (long long int)(n); i++) {
cin >> v[i].second;
d[i].first = v[i].second;
}
sort(v.begin(), v.end(), cmp);
sort(d.begin(), d.end(), cmp);
map<int, int> mp;
segTree fen, cnt;
fen.init(n);
cnt.init(n);
for (long long int i = (long long int)0; i < (long long int)(n); i++) {
mp[d[i].second] = i;
fen.update(i, d[i].second);
cnt.update(i, 1);
}
long long int ans = 0;
for (long long int i = (long long int)0; i < (long long int)(n); i++) {
int p = mp[v[i].first];
long long int q = fen.query(p, n - 1) - v[i].first;
long long int c = cnt.query(p, n - 1) - 1;
ans += q - c * v[i].first;
fen.update(p, 0);
cnt.update(p, 0);
}
cout << ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int test = 1;
clock_t z = clock();
for (long long int tes = (long long int)0; tes < (long long int)(test);
tes++) {
solve();
}
fprintf(stderr, "Total Time:%.4f\n", (double)(clock() - z) / CLOCKS_PER_SEC),
fflush(stderr);
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": []
}
|
IN-CORRECT
|
java
|
import java.io.*;
import java.util.*;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Housni Abdellatif
*/
public class Main {
public static void main(String[] args) throws IOException{
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Task.Reader in = new Task.Reader();
PrintWriter out = new PrintWriter(outputStream);
Task solver = new Task();
solver.solve(1, in, out);
out.close();
}
static class Task {
public void solve(int testNumber, Reader reader, PrintWriter out) throws IOException {
int n = reader.nextInt();
Point[] pts = new Point[n];
List<Integer> vs = new ArrayList<>();
for (int i = 0; i < n; ++i) {
pts[i] = new Point();
pts[i].x = reader.nextInt();
}
for (int i = 0; i < n; ++i) {
int v = reader.nextInt();
pts[i].v = v;
vs.add(v);
}
Collections.sort(vs);
Arrays.sort(pts, (p1, p2) -> p1.x - p2.x);
long ans = 0;
int[] bitV = new int[n];
int[] bitX = new int[n];
Map<Integer, Integer> map = new HashMap<>();
for (int i = n - 1; i >= 0; --i) {
map.put(vs.get(i), i);
}
//out.println(map);
for (int i = 0; i < n; ++i) {
int pos = map.get(pts[i].v);
long cnt = sum(bitV, pos);
long s = sum(bitX, pos);
//out.println(s);
ans += cnt * pts[i].x - s;
update(bitV, pos, 1);
update(bitX, pos, pts[i].x);
}
out.println(ans);
}
private void update(int[] bit, int index, int delta) {
while (index < bit.length) {
bit[index] += delta;
index |= (index + 1);
}
}
private long sum(int[] bit, int r) {
long res = 0;
while (r >= 0) {
res += bit[r];
r = (r & (r + 1)) - 1;
}
return res;
}
private int getFirstIndex(List<Integer> list, int x) {
int l = 0;
int r = list.size() - 1;
while ( l < r) {
int m = (l + r) / 2;
if (list.get(m) < x) {
l = m + 1;
}else {
r = m;
}
}
return l;
}
static class Point {
int x;
int v;
}
static class Reader
{
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1)
{
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.')
{
while ((c = read()) >= '0' && c <= '9')
{
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
}
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
template <class T, class U>
bool ckmin(T& a, U& b) {
return b < a ? a = b, 1 : 0;
}
template <class T, class U>
bool ckmax(T& a, U& b) {
return a < b ? a = b, 1 : 0;
}
int pct(int x) { return __builtin_popcount(x); }
long long FIRSTTRUE(function<bool(long long)> first, long long lower_bound,
long long rb) {
while (lower_bound < rb) {
long long mb = (lower_bound + rb) / 2;
first(mb) ? rb = mb : lower_bound = mb + 1;
}
return lower_bound;
}
long long LASTTRUE(function<bool(long long)> first, long long lower_bound,
long long rb) {
while (lower_bound < rb) {
long long mb = (lower_bound + rb + 1) / 2;
first(mb) ? lower_bound = mb : rb = mb - 1;
}
return lower_bound;
}
namespace input {
template <class T>
void re(complex<T>& x);
template <class T1, class T2>
void re(pair<T1, T2>& p);
template <class T>
void re(vector<T>& a);
template <class T, size_t SZ>
void re(array<T, SZ>& a);
template <class T>
void re(T& x) {
cin >> x;
}
void re(double& x) {
string t;
re(t);
x = stod(t);
}
void re(long double& x) {
string t;
re(t);
x = stold(t);
}
template <class T, class... Ts>
void re(T& t, Ts&... ts) {
re(t);
re(ts...);
}
template <class T>
void re(complex<T>& x) {
T a, b;
re(a, b);
x = {a, b};
}
template <class T1, class T2>
void re(pair<T1, T2>& p) {
re(p.first, p.second);
}
template <class T>
void re(vector<T>& a) {
for (int i = (0); i < ((int)a.size()); ++i) re(a[i]);
}
template <class T, size_t SZ>
void re(array<T, SZ>& a) {
for (int i = (0); i < (SZ); ++i) re(a[i]);
}
} // namespace input
using namespace input;
namespace output {
void pr(int x) { cout << x; }
void pr(long x) { cout << x; }
void pr(long long x) { cout << x; }
void pr(unsigned x) { cout << x; }
void pr(unsigned long x) { cout << x; }
void pr(unsigned long long x) { cout << x; }
void pr(float x) { cout << x; }
void pr(double x) { cout << x; }
void pr(long double x) { cout << x; }
void pr(char x) { cout << x; }
void pr(const char* x) { cout << x; }
void pr(const string& x) { cout << x; }
void pr(bool x) { pr(x ? "true" : "false"); }
template <class T>
void pr(const complex<T>& x) {
cout << x;
}
template <class T1, class T2>
void pr(const pair<T1, T2>& x);
template <class T>
void pr(const T& x);
template <class T, class... Ts>
void pr(const T& t, const Ts&... ts) {
pr(t);
pr(ts...);
}
template <class T1, class T2>
void pr(const pair<T1, T2>& x) {
pr("{", x.first, ", ", x.second, "}");
}
template <class T>
void pr(const T& x) {
pr("{");
bool fst = 1;
for (const auto& a : x) pr(!fst ? ", " : "", a), fst = 0;
pr("}");
}
void ps() { pr("\n"); }
template <class T, class... Ts>
void ps(const T& t, const Ts&... ts) {
pr(t);
if (sizeof...(ts)) pr(" ");
ps(ts...);
}
void pc() { pr("]\n"); }
template <class T, class... Ts>
void pc(const T& t, const Ts&... ts) {
pr(t);
if (sizeof...(ts)) pr(", ");
pc(ts...);
}
} // namespace output
using namespace output;
namespace io {
void setIn(string second) { freopen(second.c_str(), "r", stdin); }
void setOut(string second) { freopen(second.c_str(), "w", stdout); }
void setIO(string second = "") {
ios_base::sync_with_stdio(0);
cin.tie(0);
if ((int)second.size()) {
setIn(second + ".in"), setOut(second + ".out");
}
}
} // namespace io
using namespace io;
const int MOD = 1000000007;
struct mi {
typedef decay<decltype(MOD)>::type T;
T v;
explicit operator T() const { return v; }
mi() { v = 0; }
mi(long long _v) {
v = (-MOD < _v && _v < MOD) ? _v : _v % MOD;
if (v < 0) v += MOD;
}
friend bool operator==(const mi& a, const mi& b) { return a.v == b.v; }
friend bool operator!=(const mi& a, const mi& b) { return !(a == b); }
friend bool operator<(const mi& a, const mi& b) { return a.v < b.v; }
friend void re(mi& a) {
long long x;
re(x);
a = mi(x);
}
friend void pr(const mi& a) { pr(a.v); }
friend ostream& operator<<(ostream& os, const mi& a) { return os << a.v; }
mi& operator+=(const mi& m) {
if ((v += m.v) >= MOD) {
v -= MOD;
}
return *this;
}
mi& operator-=(const mi& m) {
if ((v -= m.v) < 0) {
v += MOD;
}
return *this;
}
mi& operator*=(const mi& m) {
v = (long long)v * m.v % MOD;
return *this;
}
mi& operator/=(const mi& m) { return (*this) *= inv(m); }
friend mi pow(mi a, long long p) {
mi ans = 1;
assert(p >= 0);
for (; p; p /= 2, a *= a)
if (p & 1) ans *= a;
return ans;
}
friend mi inv(const mi& a) {
assert(a.v != 0);
return pow(a, MOD - 2);
}
mi operator-() const { return mi(-v); }
mi& operator++() { return *this += 1; }
mi& operator--() { return *this -= 1; }
friend mi operator+(mi a, const mi& b) { return a += b; }
friend mi operator-(mi a, const mi& b) { return a -= b; }
friend mi operator*(mi a, const mi& b) { return a *= b; }
friend mi operator/(mi a, const mi& b) { return a /= b; }
};
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
const int MAX = 2e5 + 5;
template <class T>
struct BIT {
T bit[MAX];
long long SZ;
BIT(T n) { SZ = n; }
void update(T pos, T val) {
while (pos <= SZ) {
bit[pos] += val;
pos += pos & -pos;
}
}
T sum(long long ind) {
T res = 0;
while (ind > 0) {
res += bit[ind];
ind -= ind & -ind;
}
return res;
}
T query(T l, T r) { return sum(r) - sum(l - 1); }
};
struct node {
long long loc, fast, ind;
node(long long l, long long first, long long i) {
loc = l, fast = first, ind = i;
}
};
bool cmp(node a, node b) { return a.loc < b.loc; }
int main() {
long long n;
cin >> n;
vector<long long> pos(n + 1, 0), speed(n + 1, 0);
for (int i = (1); i < (n + 1); ++i) {
cin >> pos[i];
}
for (int i = (1); i < (n + 1); ++i) {
cin >> speed[i];
}
vector<node> a(n + 1, {0, 0, 0});
for (int i = (1); i < (n + 1); ++i) {
a[i] = {pos[i], speed[i], (long long)i};
}
long long curid = 1;
vector<long long> s1 = speed;
sort(s1.begin() + 1, s1.end());
map<long long, long long> track;
for (int i = 1; i <= n; i++) {
track[s1[i]] = curid++;
}
for (int i = (1); i < (n + 1); ++i) {
a[i].ind = track[a[i].fast];
}
BIT<long long> inv(n), inv1(n);
sort(1 + a.begin(), a.end(), cmp);
long long ans = 0;
for (int i = n; i >= 1; i--) {
auto pos1 = lower_bound(s1.begin() + 1, s1.end(), a[i].fast) - s1.begin();
long long qry = inv.query(pos1, n);
long long qry1 = inv1.query(pos1, n);
ans += (qry - (qry1 * a[i].loc));
inv.update(a[i].ind, a[i].loc);
inv1.update(a[i].ind, 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": []
}
|
IN-CORRECT
|
java
|
//package psa.minrazdalja;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class minrazdalja {
/**
* Uvozena abstraktna funkcija za klic Paid<L, R>
*/
public static class Pair<L,R> {
private final L left;
private final R right;
public Pair(L left, R right) {
assert left != null;
assert right != null;
this.left = left;
this.right = right;
}
public L getLeft() { return left; }
public R getRight() { return right; }
@Override
public int hashCode() { return left.hashCode() ^ right.hashCode(); }
@Override
public boolean equals(Object o) {
if (!(o instanceof Pair)) return false;
Pair pairo = (Pair) o;
return this.left.equals(pairo.getLeft()) &&
this.right.equals(pairo.getRight());
}
}
/**?
* glavna funkcija racunanja:
* sprejme list tipa ArrayList<Pair<Integer, Integer>>, potem st vseh elementov in seveda listo vseh elementov.
*
* nato izracunamo maksimalno distance, ki je sestevek vseh distanc med posameznimi elementi, kar lahko storimo, ker je sortirana tabela.
* V drugem delu odstejemo od maksimalnega sestevka distanc, posamezne ki se sekajo v prihodnosti
* ostane nam samo tocni sestevek minimalnih distanc
*/
public static long elele(ArrayList<Pair<Integer,Integer>> elementi, int s, Integer[] x_el) {
long skupek = 0;
Long ml;
HashMap<Integer, Integer> pos = new HashMap<Integer, Integer>();
for(int i=0; i<s; ++i) {
skupek += x_el[i] * i + (-1 * x_el[i]) * (s - 1 - i);
pos.put(x_el[i], i);
}
//if(s == 161)
// System.out.println(skupek);
for(int i=0; i<s; ++i){
skupek -= (elementi.get(i).getRight() * ((pos.get(elementi.get(i).getRight()))-i));
}
//2 1 4 3 5
//2 2 2 3 4
return skupek;
}
public static void main(String[] args) throws IOException {
int st_el;
// uporabimo bufferedreader zaradi 1000x vecje hitrosti kot pa skener
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
//System.out.println("Vnesi stevilo elementov: ");
st_el = Integer.parseInt(br.readLine());
//System.out.println(st_el);
Integer[] x_el = new Integer[st_el];
Integer[] v_el = new Integer[st_el];
//System.out.println("Vnesi elemente: ");
String x = br.readLine();
String[] strs = x.trim().split("\\s+");
for(int i=0; i<st_el; ++i) {
x_el[i] = Integer.parseInt(strs[i]);
}
//System.out.println("Vnesi tezo elementov: ");
String y = br.readLine();
String[] strs2 = y.trim().split("\\s+");
for(int i=0; i<st_el; ++i) {
v_el[i] = Integer.parseInt(strs2[i]);
}
br.close();
//pospravimo skupej v par oblike <integer, integer> oz <hitrost, element>
ArrayList<Pair<Integer,Integer>> listt = new ArrayList<Pair<Integer, Integer>>(st_el);
for(int i=0; i<st_el; i++) {
Pair<Integer, Integer> pair = new Pair<Integer, Integer>(v_el[i],x_el[i]);
listt.add(pair);
}
/*sortiramo listt od najmanjsega do najvecjega
* - sortiramo prvo po hitrostih (to je glavno najbolj vazno)
* - ce je hitrost ista potem pa sortiramo se glede na element
*/
Collections.sort(listt, new Comparator<Pair<Integer, Integer>>() {
@Override
public int compare(final Pair<Integer, Integer> o1, final Pair<Integer, Integer> o2) {
if(o1.getLeft()<o2.getLeft()) {
return -1;
} else if(o1.getLeft() == o2.getLeft()) {
if(o1.getRight() < o2.getRight()){
return -1;
}else{
return 1;
}
} else {
return 1;
}
}
});
//sortiramo se elemente
Arrays.sort(x_el);
//izpisemo elemente
System.out.println(elele(listt,st_el,x_el));
}
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
cpp
|
/*
"With age, comes wisdom. With travel, comes understanding.
Remember that happiness is a way of travel β not a destination"
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define int long long int
#define F first
#define S second
#define pb push_back
#define si set <int>
#define vi vector <int>
#define pii pair <int, int>
#define vpi vector <pii>
#define vpp vector <pair<int, pii>>
#define mii map <int, int>
#define mpi map <pii, int>
#define spi set <pii>
#define endl "\n"
#define sz(x) ((int) x.size())
#define all(p) p.begin(), p.end()
#define double long double
#define que_max priority_queue <int>
#define que_min priority_queue <int, vi, greater<int>>
#define bug(...) __f (#__VA_ARGS__, __VA_ARGS__)
#define print(a) for(auto x : a) cout << x << " "; cout << endl
#define print1(a) for(auto x : a) cout << x.F << " " << x.S << endl
#define print2(a,x,y) for(int i = x; i < y; i++) cout<< a[i]<< " "; cout << endl
inline int power(int a, int b)
{
int x = 1;
while (b)
{
if (b & 1) x *= a;
a *= a;
b >>= 1;
}
return x;
}
typedef tree<pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update> Set;
template <typename Arg1>
void __f (const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << endl; }
template <typename Arg1, typename... Args>
void __f (const char* names, Arg1&& arg1, Args&&... args)
{
const char* comma = strchr (names + 1, ',');
cout.write (names, comma - names) << " : " << arg1 << " | "; __f (comma + 1, args...);
}
const int N = 200005;
int a[N], b[N], n, m, k;
vpi v;
struct info
{
int x, y;
info ()
{
x = 0;
y = 0;
}
};
struct SegTree
{
int N;
vector<info> st;
void init(int n)
{
N = n;
st.resize(4 * N + 5);
}
void merge(info &cur, info &l, info &r)
{
cur.x = l.x + r.x;
cur.y = l.y + r.y;
}
info Query(int node, int L, int R, int i, int j)
{
if (j < L || i > R)
return info();
if (i <= L && R <= j)
return st[node];
int M = (L + R) / 2;
info left = Query(node * 2, L, M, i, j);
info right = Query(node * 2 + 1, M + 1, R, i, j);
info cur;
merge(cur, left, right);
return cur;
}
void Update(int node, int L, int R, int pos, int val)
{
if (L == R)
{
st[node].x += val;
st[node].y += 1;
return;
}
int M = (L + R) / 2;
if (pos <= M)
Update(node * 2, L, M, pos, val);
else
Update(node * 2 + 1, M + 1, R, pos, val);
merge(st[node], st[node * 2], st[node * 2 + 1]);
}
info query(int l, int r) { return Query(1, 1, N, l, r); }
void update(int pos, int val) { Update(1, 1, N, pos, val); }
};
void solve()
{
mii mp;
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 0; i < n; i++)
cin >> b[i], mp[b[i]];
for (int i = 0; i < n; i++)
v.pb({a[i], b[i]});
sort(all(v));
int cnt = 0;
for (auto &x : mp)
x.S = ++cnt;
int ans = 0, zero = 0, zero_cnt = 0;
SegTree pos, neg;
pos.init(n + 1);
neg.init(n + 1);
for (int i = 0; i < n; i++)
{
if (v[i].S == 0)
{
ans += zero_cnt * v[i].F - zero;
zero += v[i].F;
zero_cnt++;
}
if (v[i].F > 0)
{
info p = neg.query(1, n);
info p1 = pos.query(1, mp[v[i].S]);
ans += p.y * v[i].F - p.x;
ans += p1.y * v[i].F - p1.x;
pos.update(mp[v[i].S], v[i].F);
}
if (v[i].F < 0)
{
info p = neg.query(mp[v[i].S], n);
ans += p.y * v[i].F - p.x;
neg.update(mp[v[i].S], v[i].F);
}
}
cout << ans;
}
int32_t main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
cout << setprecision(9) << fixed;
clock_t z = clock();
solve();
cerr << "Run Time : " << ((double)(clock() - z) / CLOCKS_PER_SEC);
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": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int dddx[] = {1, -1, 0, 0, 1, 1, -1, -1};
int dddy[] = {0, 0, 1, -1, 1, -1, 1, -1};
vector<pair<long long int, long long int> > vec;
int n;
long long int tree[(int)(2 * 1e5 + 1)], tree2[(int)(2 * 1e5 + 1)];
void atualiza(int x, int v) {
for (; x < n; x = x | (x + 1)) tree[x] += v;
}
void atualiza2(int x, int v) {
for (; x < n; x = x | (x + 1)) tree2[x] += v;
}
long long int sum(long long int x) {
long long int s = 0;
for (; x >= 0; x = (x & (x + 1)) - 1) s += tree[x];
return s;
}
long long int sum2(int x) {
long long int s = 0;
for (; x >= 0; x = (x & (x + 1)) - 1) s += tree2[x];
return s;
}
void compress() {
vector<int> aux2;
for (int i = 0; i < n; i++) aux2.push_back(vec[i].second);
sort(aux2.begin(), aux2.end());
for (int j = 0; j < n; j++) {
auto i = vec[j];
vec[j].second =
lower_bound(aux2.begin(), aux2.end(), i.second) - aux2.begin();
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
vector<int> aux;
for (int i = 1; i <= n; i++) {
int a;
cin >> a;
vec.push_back({a, 1});
}
for (int i = 0; i < n; i++) {
int a;
cin >> a;
vec[i].second = a;
}
sort(vec.begin(), vec.end());
compress();
for (int i = 0; i < n; i++) aux.push_back(vec[i].second);
long long int ans = 0;
for (int i = 0; i < n; i++) {
int pos = lower_bound(aux.begin(), aux.end(), vec[i].second) - aux.begin();
ans += (sum2(pos) * vec[i].first - sum(pos));
atualiza(vec[i].second, vec[i].first);
atualiza2(vec[i].second, 1);
}
cout << ans << "\n";
return 0;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
java
|
import java.io.*;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.*;
import static java.lang.Math.*;
public class Main {
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
//br = new BufferedReader(new FileReader("monument.in"));
pw = new PrintWriter(System.out);
//pw = new PrintWriter("monument.out");
new Main().run();
}
long[] put;
int[] not_zero;
void add(long x, int pos) {
put[pos] = x;
not_zero[pos] = 1;
pos /= 2;
while (pos > 0) {
not_zero[pos] = not_zero[pos * 2] + not_zero[pos * 2 + 1];
put[pos] = put[pos * 2] + put[pos * 2 + 1];
pos /= 2;
}
}
pair find_sum(int l, int r, int v, int vl, int vr) {
if (vr < l || r < vl) return new pair(0, 0);
if (l <= vl && vr <= r) return new pair(put[v], not_zero[v]);
int m = (vl + vr) / 2;
pair a = find_sum(l, r, v * 2, vl, m);
pair b = find_sum(l, r, v * 2 + 1, m + 1, vr);
return new pair(a.x + b.x, a.num + b.num);
}
void run() throws IOException {
int n = nextInt();
pair[] a = new pair[n];
for (int i = 0; i < a.length; i++) {
a[i] = new pair(nextInt(), i);
}
Arrays.sort(a);
long[] v = new long[n];
for (int i = 0; i < v.length; i++) {
v[i] = nextInt();
}
Arrays.sort(v);
HashMap<Long, Stack<Integer>> tm = new HashMap<>();
for (int i = 0; i < v.length; i++) {
if (!tm.containsKey(v[i])) {
tm.put(v[i], new Stack<>());
}
tm.get(v[i]).add(i);
}
int x = 1;
while (x < n) x *= 2;
put = new long[x * 2];
not_zero = new int[x * 2];
long ans = 0;
for (int i = 0; i < a.length; i++) {
long sp = v[a[i].num];
int pos_in_put = tm.get(sp).pop();
add(a[i].x, pos_in_put + x);
int l = -1;
int r = n;
while (l + 1 < r) {
int m = (l + r) / 2;
if (v[m] > sp) r = m;
else l = m;
}
pair k = find_sum(0, l, 1, 0, x - 1);
ans += a[i].x * k.num - k.x;
}
pw.print(ans);
pw.close();
}
class pair implements Comparable<pair> {
long x;
int num;
public pair(long x, int num) {
this.x = x;
this.num = num;
}
@Override
public int compareTo(pair o) {
return -Long.compare(o.x, this.x);
}
}
static BufferedReader br;
static StringTokenizer st = new StringTokenizer("");
static PrintWriter pw = new PrintWriter(System.out);
public static int nextInt() throws IOException {
if (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return Integer.parseInt(st.nextToken());
}
public static String next() throws IOException {
if (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
public static long nextLong() throws IOException {
if (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return Long.parseLong(st.nextToken());
}
public static double nextDouble() throws IOException {
if (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return Double.parseDouble(st.nextToken());
}
}
|
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": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
bool cmp(const pair<int, int> &a, const pair<int, int> &b) {
if (a.second == b.second) return a.first < b.first;
return a.second < b.second;
}
int main() {
int n;
cin >> n;
int x[n];
vector<pair<int, int> > v(n);
int id = 0;
for (auto &u : v) {
cin >> u.first;
x[id] = u.first;
id += 1;
}
for (auto &u : v) cin >> u.second;
sort(x, x + n);
sort(v.begin(), v.end(), cmp);
int ans = 0;
map<int, int> m1;
for (int i = 0; i < n; i++) {
ans = ans + (x[i] * (2 * i + 1 - n));
m1[x[i]] = i;
}
for (int i = 0; i < n; i++) {
ans = ans - (v[i].first * (m1[v[i].first] - i));
}
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": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int dx[8] = {0, 0, 1, 1, 1, -1, -1, -1};
int dy[8] = {1, -1, -1, 0, 1, -1, 0, 1};
long long bit[1000005], bit1[1000005];
long long sum(long long x) {
long long s = 0;
while (x > 0) {
s += bit[x];
x -= (x & (-x));
}
return s;
}
void update(long long x, long long val) {
while (x < 1000005) {
bit[x] += val;
x += (x & (-x));
}
}
long long cnt(long long x) {
long long s = 0;
while (x > 0) {
s += bit1[x];
x -= (x & (-x));
}
return s;
}
void updatecnt(long long x) {
while (x < 1000005) {
bit1[x] += 1;
x += (x & (-x));
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
long long n;
cin >> n;
long long pos[n + 1], vel[n + 1];
vector<long long> v;
map<long long, long long> h, h1;
for (long long i = 1; i < n + 1; i++) {
cin >> pos[i];
if (!h[pos[i]]) v.push_back(pos[i]);
h[pos[i]] = 1;
}
for (long long i = 1; i < n + 1; i++) {
cin >> vel[i];
if (!h[vel[i]]) v.push_back(vel[i]);
h[vel[i]] = 1;
}
v.push_back(-1000000000);
sort(v.begin(), v.end());
for (long long i = 1; i < v.size(); i++) {
if (!h1[v[i]]) h1[v[i]] = i;
}
vector<pair<long long, long long> > v1;
for (long long i = 1; i < n + 1; i++) {
pos[i] = h1[pos[i]];
vel[i] = h1[vel[i]];
v1.push_back({pos[i], vel[i]});
}
sort(v1.begin(), v1.end());
long long ans = 0;
for (long long i = 0; i < v1.size(); i++) {
long long s = sum(v1[i].second);
long long c = cnt(v1[i].second);
ans += c * v1[i].first - s;
update(v1[i].second, v1[i].first);
updatecnt(v1[i].second);
}
cout << ans;
return 0;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-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];
int 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": []
}
|
IN-CORRECT
|
python3
|
n=int(input())
x=list(map(int,input().split()))
v=list(map(int,input().split()))
arr=[]
for i in range(n):
arr.append([x[i],v[i]])
arr.sort()
ans1 = 0
ans2 = 0
d = 0
s=0
prev=0
for i in range(n):
ans1 += (arr[i][0] * i - d)
ans2 += (arr[i][1] * i - s)
d += arr[i][0]
s += arr[i][1]
if prev>ans2:
ans1-=(prev-ans2)
prev = ans2
#print (max(ans1,0),ans2)
print (max(ans1,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": []
}
|
IN-CORRECT
|
java
|
//package psa.minrazdalja;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class minrazdalja {
/**
* Uvozena abstraktna funkcija za klic Paid<L, R>
*/
public static class Pair<L,R> {
private final L left;
private final R right;
public Pair(L left, R right) {
assert left != null;
assert right != null;
this.left = left;
this.right = right;
}
public L getLeft() { return left; }
public R getRight() { return right; }
@Override
public int hashCode() { return left.hashCode() ^ right.hashCode(); }
@Override
public boolean equals(Object o) {
if (!(o instanceof Pair)) return false;
Pair pairo = (Pair) o;
return this.left.equals(pairo.getLeft()) &&
this.right.equals(pairo.getRight());
}
}
/**?
* glavna funkcija racunanja:
* sprejme list tipa ArrayList<Pair<Integer, Integer>>, potem st vseh elementov in seveda listo vseh elementov.
*
* nato izracunamo maksimalno distance, ki je sestevek vseh distanc med posameznimi elementi, kar lahko storimo, ker je sortirana tabela.
* V drugem delu odstejemo od maksimalnega sestevka distanc, posamezne ki se sekajo v prihodnosti
* ostane nam samo tocni sestevek minimalnih distanc
*/
public static long elele(ArrayList<Pair<Integer,Integer>> elementi, int s, Integer[] x_el) {
long skupek = 0;
Long ml;
HashMap<Integer, Integer> pos = new HashMap<Integer, Integer>();
for(int i=0; i<s; ++i) {
skupek += x_el[i] * i + (-1 * x_el[i]) * (s - 1 - i);
pos.put(x_el[i], i);
}
//for(int i=0; i<s; ++i){
// skupek -= elementi.get(i).getRight() * (pos.get(elementi.get(i).getRight())-i);
//}
//2 1 4 3 5
//2 2 2 3 4
return skupek;
}
public static void main(String[] args) throws IOException {
int st_el;
// uporabimo bufferedreader zaradi 1000x vecje hitrosti kot pa skener
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
//System.out.println("Vnesi stevilo elementov: ");
st_el = Integer.parseInt(br.readLine());
//System.out.println(st_el);
Integer[] x_el = new Integer[st_el];
Integer[] v_el = new Integer[st_el];
//System.out.println("Vnesi elemente: ");
String x = br.readLine();
String[] strs = x.trim().split("\\s+");
for(int i=0; i<st_el; ++i) {
x_el[i] = Integer.parseInt(strs[i]);
}
//System.out.println("Vnesi tezo elementov: ");
String y = br.readLine();
String[] strs2 = y.trim().split("\\s+");
for(int i=0; i<st_el; ++i) {
v_el[i] = Integer.parseInt(strs2[i]);
}
br.close();
//pospravimo skupej v par oblike <integer, integer> oz <hitrost, element>
ArrayList<Pair<Integer,Integer>> listt = new ArrayList<Pair<Integer, Integer>>(st_el);
for(int i=0; i<st_el; i++) {
Pair<Integer, Integer> pair = new Pair<Integer, Integer>(v_el[i],x_el[i]);
listt.add(pair);
}
/*sortiramo listt od najmanjsega do najvecjega
* - sortiramo prvo po hitrostih (to je glavno najbolj vazno)
* - ce je hitrost ista potem pa sortiramo se glede na element
*/
Collections.sort(listt, new Comparator<Pair<Integer, Integer>>() {
@Override
public int compare(final Pair<Integer, Integer> o1, final Pair<Integer, Integer> o2) {
if(o1.getLeft()<o2.getLeft()) {
return -1;
} else if(o1.getLeft() == o2.getLeft()) {
if(o1.getRight() < o2.getRight()){
return -1;
}else{
return 1;
}
} else {
return 1;
}
}
});
//sortiramo se elemente
Arrays.sort(x_el);
//izpisemo elemente
System.out.println(elele(listt,st_el,x_el));
}
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
inline long long read() {
char ch = getchar();
long long p = 1, data = 0;
while (ch < '0' || ch > '9') {
if (ch == '-') p = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
data = (data << 3) + (data << 1) + (ch ^ 48);
ch = getchar();
}
return p * data;
}
inline long long lowbit(long long a) { return a & (-a); }
inline long long qpow(long long a, long long b) {
long long r = 1;
while (b) {
if (b & 1) r = a * r;
a = a * a;
b >>= 1;
}
return r;
}
inline long long gcd(long long a, long long b) {
while (b ^= a ^= b ^= a %= b)
;
return a;
}
inline long long lcm(long long a, long long b) { return a * b / gcd(a, b); }
inline long long inv(long long a, long long p) { return qpow(a, p - 2); }
const int maxn = 2e5 + 5;
struct node {
long long x, v;
friend bool operator<(node a, node b) {
return a.v == b.v ? a.x < b.x : a.v < b.v;
}
} a[maxn];
multiset<int> mp;
long long sum[maxn];
int main() {
int n = read();
for (int i = 1; i <= n; i++) a[i].x = read();
for (int i = 1; i <= n; i++) a[i].v = read();
sort(a + 1, a + 1 + n);
long long ans = 0, pre = a[1].x;
mp.insert(a[1].x);
sum[1] = a[1].x;
multiset<int>::iterator it;
for (int i = 2; i <= n; i++) {
sum[i] = a[i].x + sum[i - 1];
it = lower_bound(mp.begin(), mp.end(), a[i].x);
long long pos = distance(mp.begin(), it);
ans += abs(a[i].x * pos - sum[pos]);
mp.insert(a[i].x);
}
printf("%lld", ans);
return 0;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
java
|
//package psa.minrazdalja;
import java.util.ArrayList;
import java.util.Scanner; // Import the Scanner class
public class minrazdalja {
static class MinStruktua {
private int x;
private int v;
public MinStruktua(int x, int v) {
this.x = x;
this.v = v;
}
public void dodajx(int x) {
this.x = x;
}
public void dodajv(int v) {
this.v = v;
}
}
public static void elele(ArrayList<MinStruktua> elementi) {
int skupek = 0;
for(int i=0; i<elementi.size(); i++) {
for(int j=0;j<elementi.size();j++) {
if(i != j) {
int ix = elementi.get(i).x;
int jx = elementi.get(j).x;
if(jx > ix) {
int vx = elementi.get(i).v;
int vy = elementi.get(j).v;
//System.out.println(ix + " " + jx);
skupek += vrniMax(ix, jx, vx, vy);
}
}
}
//2 1 4 3 5
//2 2 2 3 4
}
System.out.println("IZPIS elementov: ");
System.out.println("STEVC : " + skupek);
}
// 2 3 3 2
public static int vrniMax(int m, int n, int i, int j) {
//najdemo max
//nardimo primerjave in vrnemo ustrezno
int x = 0;
int absol = Math.abs(m-n);
if(m < n) {
if(j < 0 && i >= 0) {
x =+ 0;
}else if(j >= 0 && i < 0){
x =+ absol;
}else if(j < 0 && i < 0) {
if(j > i){
x =+ absol;
} else {
x =+ 0;
}
}else{
if(j != i) {
if(j > i){
x =+ absol;
}else{
x =+ 0;
}
}else{
x =+ absol;
}
}
};
System.out.println(m + " " + n + " " + i + " " + j + " " + x);
return x;
}
public static void main(String[] args) {
int st_el;
Scanner skener = new Scanner(System.in); // Create a Scanner object
//System.out.println("Vnesi stevilo elementov: ");
st_el = skener.nextInt();
//System.out.println(st_el);
ArrayList<MinStruktua> elementi = new ArrayList<>(st_el);
//System.out.println("Vnesi elemente: ");
for(int i=0; i<st_el; i++) {
int x = skener.nextInt();
elementi.add(new MinStruktua(x, 0));
}
//System.out.println("Vnesi tezo elementov: ");
for(int i=0; i<st_el; i++) {
int x = skener.nextInt();
elementi.get(i).dodajv(x);
}
skener.close();
System.out.println();
System.out.println();
elele(elementi);
//vrniMax(1, 2, -100, 3);
}
}
|
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": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
T max(T a, T b, T c) {
return max(a, max(b, c));
}
template <typename T>
T min(T a, T b, T c) {
return min(a, min(b, c));
}
template <typename T>
T med(T a, T b, T c) {
return a + b + c - min(a, b, c) - max(a, b, c);
}
const int INF = 1e9;
const long long LINF = 1LL << 62;
struct pt {
long long x, v;
};
bool cmp(pt A, pt B) { return A.x < B.x; }
const int MAXB = 20001;
const int BSIZE = 10000;
const int shift = 1e8;
int sbuckets[MAXB];
vector<pt> bbuckets[MAXB];
void insert(pt p) {
long long vs = p.v + shift;
sbuckets[vs / BSIZE] += p.x;
bbuckets[vs / BSIZE].push_back(p);
}
long long get(pt p) {
long long vs = p.v + shift;
long long sm = 0;
int amt = 0;
for (int b = 0; (b + 1) * BSIZE < vs; b++) {
sm += sbuckets[b];
amt += bbuckets[b].size();
}
for (auto a : bbuckets[vs / BSIZE])
if (p.v >= a.v) {
sm += a.x;
amt++;
}
return amt * p.x - sm;
}
int main() {
int n;
cin >> n;
vector<pt> pts(n);
for (int i = 0; i < n; i++) cin >> pts[i].x;
for (int i = 0; i < n; i++) cin >> pts[i].v;
sort(pts.begin(), pts.end(), cmp);
long long res = 0;
for (int cs = 0; cs < n; cs++) {
res += get(pts[cs]);
insert(pts[cs]);
}
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": []
}
|
IN-CORRECT
|
java
|
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
/**
*
* @author is2ac
*/
public class D_CF {
public static void main(String[] args) throws IOException {
FastScanner58 fs = new FastScanner58();
//Reader fs = new Reader();
PrintWriter pw = new PrintWriter(System.out);
//int t = fs.ni();
int t = 1;
for (int tc = 0; tc < t; tc++) {
int n = fs.ni();
int[] x = fs.intArray(n);
int[] v = fs.intArray(n);
BIT16 bit = new BIT16(200005);
BIT16 people = new BIT16(200005);
Set<Integer> set = new HashSet();
List<Integer> list = new ArrayList();
Map<Integer,Integer> map = new HashMap();
for (int i = 0; i < n; i++) {
set.add(v[i]);
}
for (int val : set) {
list.add(val);
}
Collections.sort(list);
for (int i = 0; i < list.size(); i++) {
map.put(list.get(i),i+1);
}
int[][] matrix = new int[n][2];
for (int i = 0; i < n; i++) {
matrix[i][0] = x[i];
matrix[i][1] = v[i];
}
Arrays.sort(matrix,new Comparator<int[]>(){
public int compare(int[] a, int[] b) {
return a[0]-b[0];
}
});
long res = 0;
for (int i = 0; i < n; i++) {
int cx = matrix[i][0];
int cv = map.get(matrix[i][1]);
long cur = (long)people.sum(cv) * (long)cx;
cur -= (long)bit.sum(cv);
res += cur;
people.update(cv,1);
bit.update(cv,cx);
}
pw.println(res);
}
pw.close();
}
public static long gcd(long n1, long n2) {
if (n2 == 0) {
return n1;
}
return gcd(n2, n1 % n2);
}
}
class GFG {
// A utility function to get the
// middle index of given range.
static int getMid(int s, int e)
{
return s + (e - s) / 2;
}
/*
* A recursive function to get the sum
of values in given range of the array.
* The following are parameters
for this function.
*
* st -> Pointer to segment tree
* node -> Index of current node in
* the segment tree.
* ss & se -> Starting and ending indexes
* of the segment represented
* by current node, i.e., st[node]
* l & r -> Starting and ending indexes
* of range query
*/
static long MinUtil(long[] st, int ss,
int se, int l,
int r, int node)
{
// If segment of this node is completely
// part of given range, then return
// the max of segment
if (l <= ss && r >= se)
return st[node];
// If segment of this node does not
// belong to given range
if (se < l || ss > r)
return (long)(1e11);
// If segment of this node is partially
// the part of given range
int mid = getMid(ss, se);
return Math.min(
MinUtil(st, ss, mid, l, r,
2 * node + 1),
MinUtil(st, mid + 1, se, l, r,
2 * node + 2));
}
/*
* A recursive function to update the
nodes which have the given index in their
* range. The following are parameters
st, ss and se are same as defined above
* index -> index of the element to be updated.
*/
static void updateValue(long arr[], long[]
st, int ss,
int se, int index,
long value,
int node)
{
if (index < ss || index > se) {
//System.out.println("Invalid Input");
return;
}
if (ss == se) {
// update value in array and in
// segment tree
arr[index] = value;
st[node] = value;
}
else {
int mid = getMid(ss, se);
if (index >= ss && index <= mid)
updateValue(arr, st, ss, mid,
index, value,
2 * node + 1);
else
updateValue(arr, st, mid + 1, se, index,
value, 2 * node + 2);
st[node] = Math.min(st[2 * node + 1],
st[2 * node + 2]);
}
return;
}
// Return max of elements in range from
// index l (query start) to r (query end).
static long getMin(long[] st, int n, int l, int r)
{
// Check for erroneous input values
if (l < 0 || r > n - 1 || l > r) {
//System.out.printf("Invalid Input\n");
return (long)(1e11);
}
return MinUtil(st, 0, n - 1, l, r, 0);
}
// A recursive function that constructs Segment
// Tree for array[ss..se]. si is index of
// current node in segment tree st
static long constructSTUtil(long arr[],
int ss, int se,
long[] st, int si)
{
// If there is one element in array, store
// it in current node of segment tree and return
if (ss == se) {
st[si] = arr[ss];
return arr[ss];
}
// If there are more than one elements, then
// recur for left and right subtrees and
// store the max of values in this node
int mid = getMid(ss, se);
st[si] = Math.min(
constructSTUtil(arr, ss, mid,
st, si * 2 + 1),
constructSTUtil(arr, mid + 1,
se, st,
si * 2 + 2));
return st[si];
}
/*
* Function to construct segment tree from
given array. This function allocates
* memory for segment tree.
*/
static long[] constructST(long arr[], int n)
{
// Height of segment tree
int x = (int)Math.ceil(Math.log(n) / Math.log(2));
// Maximum size of segment tree
int max_size = 2 * (int)Math.pow(2, x) - 1;
// Allocate memory
long[] st = new long[max_size];
// Fill the allocated memory st
constructSTUtil(arr, 0, n - 1, st, 0);
// Return the constructed segment tree
return st;
}
}
class BIT16 {
int[] bit;
public BIT16(int size) {
bit = new int[size];
}
public void update(int ind, int delta) {
while (ind < bit.length) {
bit[ind] += delta;
ind = ind + (ind & (-1 * ind));
}
}
public int sum(int ind) {
int s = 0;
while (ind > 0) {
s += bit[ind];
ind = ind - (ind & (-1 * ind));
}
return s;
}
public int query(int l, int r) {
return sum(r) - sum(l);
}
}
class UnionFind18 {
int[] id;
public UnionFind18(int size) {
id = new int[size];
for (int i = 0; i < size; i++) {
id[i] = i;
}
}
public int find(int p) {
int root = p;
while (root != id[root]) {
root = id[root];
}
while (p != root) {
int next = id[p];
id[p] = root;
p = next;
}
return root;
}
public void union(int p, int q) {
int a = find(p), b = find(q);
if (a == b) {
return;
}
id[b] = a;
}
}
class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
byte[] buf = new byte[64];
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
break;
}
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int ni() 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();
}
}
class FastScanner58 {
BufferedReader br;
StringTokenizer st;
public FastScanner58() {
br = new BufferedReader(new InputStreamReader(System.in), 32768);
st = null;
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int ni() {
return Integer.parseInt(next());
}
int[] intArray(int N) {
int[] ret = new int[N];
for (int i = 0; i < N; i++) {
ret[i] = ni();
}
return ret;
}
long nl() {
return Long.parseLong(next());
}
long[] longArray(int N) {
long[] ret = new long[N];
for (int i = 0; i < N; i++) {
ret[i] = nl();
}
return ret;
}
double nd() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-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][1]);
}
}
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": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
pair<int, int> a[200005];
int sum[1000005], cnt[1000005];
int querysum(int pos) {
int ret = 0;
for (; pos >= 0; pos = (pos & (pos + 1)) - 1) {
ret += sum[pos];
}
return ret;
}
int querycnt(int pos) {
int ret = 0;
for (; pos >= 0; pos = (pos & (pos + 1)) - 1) {
ret += cnt[pos];
}
return ret;
}
void updatesum(int pos, int val) {
for (; pos < 1000000; pos |= pos + 1) {
sum[pos] += val;
}
}
void updatecnt(int pos, int val) {
for (; pos < 1000000; pos |= pos + 1) {
cnt[pos] += val;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
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;
vector<int> v;
for (int i = 1; i <= n; i++) {
v.push_back(a[i].second);
}
sort(v.begin(), v.end());
map<int, int> num;
for (int i = 0; i < v.size(); i++) {
num[v[i]] = i;
}
for (int i = 1; i <= n; i++) {
a[i].second = num[a[i].second];
}
sort(a + 1, a + n + 1);
memset(sum, 0, sizeof(sum));
memset(cnt, 0, sizeof(cnt));
int ans = 0;
for (int i = 1; i <= n; i++) {
ans += a[i].first * querycnt(a[i].second) - querysum(a[i].second);
updatesum(a[i].second, a[i].first);
updatecnt(a[i].second, 1);
}
cout << ans << endl;
return 0;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int calcdist(int x1, int x2, int v1, int v2) {
int dist = 0;
if (x1 == x2)
return dist;
else if (x2 > x1) {
if ((v1 - v2) <= 0)
return (x2 - x1);
else
return dist;
} else {
if ((v2 - v1) <= 0)
return (x1 - x2);
else
return dist;
}
}
int main() {
int n;
cin >> n;
long sum = 0;
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];
for (int i = 0; i < (n - 1); i++) {
for (int j = i + 1; j < n; j++) sum += calcdist(x[i], x[j], v[i], v[j]);
}
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": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
#include <fstream>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template < typename T >
using ordered_set = tree < T, null_type, less < T >, rb_tree_tag, tree_order_statistics_node_update >; // less_equal for keeping duplicates
#define all(x) x.begin(), x.end()
#define pii pair < int, int >
#define pll pair < long long, long long >
#define f first
#define s second
#define mp(x, y) make_pair(x, y)
#define MASK bitset < 16 >
const int MAX = 2e5 + 2, mod = 1e9 + 7, shift = 1000;
map < int, int > v;
struct Node
{
long long sum = 0;
int count = 0;
Node merge(const Node &l, const Node &r)
{
sum = l.sum + r.sum;
count = l.count + r.count;
return *this;
}
}node[4 * MAX];
struct segTree
{
Node query(int p, int s, int e, int qs, int qe)
{
if(qs > e || s > qe) return Node();
else if(qs <= s && e <= qe) return node[p];
else
{
Node ret;
int mid = (s + e) >> 1;
const auto left = query(2 * p + 1, s, mid, qs, qe);
const auto right = query(2 * p + 2, mid + 1, e, qs, qe);
//cout << s << " " << e << " " << qs << " " << qe << " " << ret.sum << " " << ret.count << "\n";
return ret.merge(left, right);
}
}
void update(int p, int s, int e, int idx, int val)
{
if(idx < s || e < idx) return;
else if(s == e) node[p].sum += val, node[p].count++;
else
{
int mid = (s + e) >> 1;
update(2 * p + 1, s, mid, idx, val);
update(2 * p + 2, mid + 1, e, idx, val);
node[p].merge(node[2 * p + 1], node[2 * p + 2]);
}
}
};
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n;
long long ans = 0;
vector < int > v;
cin >> n;
pii a[n];
for(int i = 0; i < n; i++)
cin >> a[i].f;
for(int j = 0; j < n; j++)
cin >> a[j].s, v.push_back(a[j].s);
sort(a, a + n);
sort(all(v));
v.erase(unique(all(v)), v.end());
segTree tree;
for(int i = n - 1; i >= 0; i--)
{
auto idx = lower_bound(all(v), a[i].s) - v.begin();
tree.update(0, 0, n - 1, idx, a[i].f);
auto sum = tree.query(0, 0, n - 1, idx, n - 1);
//cout << sum.sum << " " << sum.count << "\n";
ans += (sum.sum - a[i].f * sum.count);
}
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": []
}
|
IN-CORRECT
|
java
|
import java.util.*;
/**
* Created by Acesine on 2/24/20.
*/
public class F {
static class BIT {
int[] arr;
int n;
public BIT(int n) {
this.n = n;
arr = new int[n+1];
}
public void update(int x, int v) {
x++;
while (x <= n) {
arr[x] += v;
x += x & (-x);
}
}
public int get(int x) {
x++;
int 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);
int cnt = vBit.get(pos);
int 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": []
}
|
IN-CORRECT
|
java
|
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.StringTokenizer;
public class C1311F {
public static void main(String[] args) {
var scanner = new BufferedScanner();
var writer = new PrintWriter(new BufferedOutputStream(System.out));
var n = scanner.nextInt();
var x = new int[n];
for (int i = 0; i < n; i++) {
x[i] = scanner.nextInt();
// x[i] = (int) (Math.random() * 2e8 - 1e8);
// x[i] = (int) (Math.random() * n / 2 + n / 2);
}
var v = new int[n];
for (int i = 0; i < n; i++) {
v[i] = scanner.nextInt();
// v[i] = (int) (Math.random() * 2e8 - 1e8);
// v[i] = (int) (Math.random() * 2 * n - n);
}
writer.println(solve(n, x, v));
scanner.close();
writer.flush();
writer.close();
}
static final int MIN_V = (int) -1e8;
static final int MAX_V = (int) 1e8;
private static long solve(int n, int[] x, int[] v) {
var debug = false; //n == 200000;
var orderX = new ArrayList<Integer>();
var ascV = new ArrayList<Integer>();
for (int i = 0; i < n; i++) {
orderX.add(i);
ascV.add(v[i]);
}
var t = System.nanoTime();
orderX.sort(Comparator.comparingInt(o -> x[o]));
ascV.sort(Integer::compareTo);
if (debug) {
System.out.println("sort finished: " + (System.nanoTime() - t) / 1e9);
}
t = System.nanoTime();
var ans = 0L;
var toLeft = new SegTree(ascV);
var toRight = new SegTree(ascV);
var minV = Integer.MAX_VALUE;
for (int i = 0; i < n; i++) {
var thisX = x[orderX.get(i)];
var thisV = v[orderX.get(i)];
if (thisV >= 0) {
var left = toLeft.acc(/*minV*/MIN_V, 0);
ans += left[1] * thisX - left[0];
var right = toRight.acc(0, thisV);
ans += right[1] * thisX - right[0];
toRight.add(thisV, thisX);
} else {
var left = toLeft.acc(/*minV*/MIN_V, thisV);
ans += left[1] * thisX - left[0];
toLeft.add(thisV, thisX);
}
minV = Math.min(minV, thisV);
}
if (debug) {
System.out.println("finish acc: " + (System.nanoTime() - t) / 1e9);
}
return ans;
}
static class SegTree {
final List<Node> nodes = new ArrayList<>();
final int[] x;
SegTree(List<Integer> x) {
nodes.add(new Node(0, x.size() - 1));
this.x = x.stream().mapToInt(i -> i).toArray();
}
void add(int key, int value) {
add(0, key, value);
}
private void add(int index, int key, int value) {
var node = nodes.get(index);
if (key < x[node.low] || x[node.high] < key) {
return;
}
node.acc[0] += value;
node.acc[1]++;
if (node.low == node.high) {
return;
}
if (node.left < 0) {
nodes.add(new Node(node.low, node.mid));
node.left = nodes.size() - 1;
}
add(node.left, key, value);
if (node.right < 0) {
nodes.add(new Node(node.mid + 1, node.high));
node.right = nodes.size() - 1;
}
add(node.right, key, value);
}
long[] acc(int lowKey, int highKey) {
return acc(0, lowKey, highKey);
}
static final long[] EMPTY = new long[]{0, 0};
private long[] acc(int index, int lowKey, int highKey) {
var node = nodes.get(index);
if (lowKey > highKey
|| highKey < x[node.low] || x[node.high] < lowKey) {
return EMPTY;
}
if (lowKey <= x[node.low] && x[node.high] <= highKey) {
return node.acc;
}
var ans = new long[]{0, 0};
if (node.left >= 0) {
var left = acc(node.left, lowKey, highKey);
ans[0] += left[0];
ans[1] += left[1];
}
if (node.right >= 0) {
var right = acc(node.right, lowKey, highKey);
ans[0] += right[0];
ans[1] += right[1];
}
return ans;
}
static class Node {
final int low;
final int high;
final int mid;
int left = -1;
int right = -1;
long[] acc = new long[]{0, 0};
Node(int low, int high) {
this.low = low;
this.high = high;
this.mid = Math.floorDiv(low + high, 2);
}
}
}
public static class BufferedScanner {
BufferedReader br;
StringTokenizer st;
public BufferedScanner(Reader reader) {
br = new BufferedReader(reader);
}
public BufferedScanner() {
this(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
void close() {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
static long gcd(long a, long b) {
if (a < b) {
return gcd(b, a);
}
while (b > 0) {
long tmp = b;
b = a % b;
a = tmp;
}
return a;
}
static long inverse(long a, long m) {
long[] ans = extgcd(a, m);
return ans[0] == 1 ? (ans[1] + m) % m : -1;
}
private static long[] extgcd(long a, long m) {
if (m == 0) {
return new long[]{a, 1, 0};
} else {
long[] ans = extgcd(m, a % m);
long tmp = ans[1];
ans[1] = ans[2];
ans[2] = tmp;
ans[2] -= ans[1] * (a / m);
return ans;
}
}
private static List<Integer> primes(double upperBound) {
var limit = (int) Math.sqrt(upperBound);
var isComposite = new boolean[limit + 1];
var primes = new ArrayList<Integer>();
for (int i = 2; i <= limit; i++) {
if (isComposite[i]) {
continue;
}
primes.add(i);
int j = i + i;
while (j <= limit) {
isComposite[j] = true;
j += i;
}
}
return primes;
}
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
cpp
|
// "It does not matter how slowly you go as long as you do not stop." - Confucius
#include <bits/stdc++.h>
using namespace std;
/*
author : Roshan_Mehta
motto : Time Management,Consistency,Patience!!
*/
#define int long long
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
// void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef Local_Debug
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
#define M1 1000000007
#define M2 998244353
#define G(a,b) get<a>(b)
#define ll long long
#define pb push_back
#define mt make_tuple
#define fast() ios_base::sync_with_stdio(false);cin.tie(NULL);
#define mp make_pair
#define F first
#define S second
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rsort(x) sort(x,greater<int> ())
#define rall(x) rbegin(x), rend(x) //useful in sorting
#define endl "\n"
#define p0(a) cout << a << " "
#define p1(a) cout << a << endl
#define p2(a, b) cout << a << " " << b << endl
#define p3(a, b, c) cout << a << " " << b << " " << c << endl
#define p4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl
#define gcd __gcd
#define lcm(a,b) (a*b)/gcd(a,b)
#define deci(n) fixed << setprecision(n)
#define test() int test_case;cin >> test_case;while (test_case--)
#define loop(i,a,n) for(int i=a;i<n;i++)
#define sp " "
#define popcount(x) __builtin_popcount(x)
#define popcountll(x) __builtin_popcountll(x)
#define clz(x) __builtin_clz(x)
#define clzll(x) __builtin_clzll(x)
#define ctz(x) __builtin_ctz(x)
#define ctzll(x) __builtin_ctzll(x)
#define max3(a,b,c) max(max(a,b),c)
#define min3(a,b,c) min(min(a,b),c)
#define max4(a,b,c,d) max(a,max3(b,c,d))
#define min4(a,b,c,d) min(a,min3(b,c,d))
#define max5(a,b,c,d,e) max(max4(a,b,c,d),e)
#define min5(a,b,c,d,e) min(min4(a,b,c,d),e)
#define getunique(v) {sort(all(v)); v.erase(unique(all(v)), v.end());}
#define REP(i,a,b) for(ll i=a;i<b;i++)
#define REPI(i,a,b) for(ll i=b-1;i>=a;i--)
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
typedef pair<int,pi> tri;
typedef pair<pi,int> rtri;
typedef pair<ll, ll> pl;
typedef pair<double, double> pd;
typedef priority_queue<int, vector<int>, greater<int>> minpq;
typedef priority_queue<int> pq;
typedef unordered_map<int,int> umii;
typedef map<int,int> mii;
typedef set<int> si;
#define input(n,k) int n,k;cin>>n;cin>>k;vi arr(n,0);loop(i,0,n){cin>>arr[i];}
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
const int MOD = 1e9 + 7;
int md=MOD;
int Power(int n, int x) {int ans = 1;while (x > 0) {if (x & 1) ans = (ans * n) % md;
n = (n * n) % md;x = x >> 1;}return ans;}
vl fact, inv;
void inverse(ll n)
{if(n>=inv.size()){ll size=inv.size();size=size==0? 1:size;
inv.resize(n + 1);inv[0] = 1;
for (ll i = size; i <= n; i++)inv[i] = Power(fact[i], md - 2);}}
void factorial(ll n)
{if(n>=fact.size()){ll size=fact.size();size=size==0? 1:size;
fact.resize(n + 1);fact[0] = 1;for (ll i = size; i <= n; i++)
fact[i] = (fact[i - 1] * i) % md;
}}
ll ncr(ll n, ll r) { return (((fact[n] * inv[r]) % md) * inv[n - r]) % md; }
vl SieveOfEratosthenes(int n)
{
bool prime[n+1];
memset(prime, true, sizeof(prime));
for (int p=2; p*p<=n; p++)
{if (prime[p] == true){for (int i=p*p; i<=n; i += p)
prime[i] = false;}}
vl ans;for (int p=2; p<=n; p++)if (prime[p])ans.pb(p);return ans;
}
vi primeFactors(int n)
{
vi ans;
while (n % 2 == 0) {ans.pb(2);n = n / 2;}
for (int i = 3; i <= sqrt(n); i = i + 2) {
while (n % i == 0) {ans.pb(i);n = n / i;}}
if (n > 2)ans.pb(n);return ans;
}
// bool isprime(ll n)
// {
// if(n<2)
// return false;
// for(ll x:{2,3,5,7,11,13,17,19,23,29,31,37})
// {
// if(n==x)
// return true;
// bool flag=true;
// ll r=1;
// ll t=1;
// while(r<=((n-1)>>__builtin_ctzll(n-1)))
// {
// if(r&((n-1)>>__builtin_ctzll(n-1)))
// t=((__int128)t*x)%n;
// x=((__int128)x*x)%n;
// r<<=1;
// }
// if(t==1||t==n-1)
// flag=false;
// for(r=0;r<__builtin_ctzll(n-1);r++)
// {
// t=((__int128)t*t)%n;
// if(t==n-1)
// flag=false;
// }
// if(flag)
// return false;
// }
// return true;
// }
ll NcR(int n, int r)
{long long p = 1, k = 1;if (n - r < r)r = n - r;if (r != 0) {while (r) {p *= n;k *= r;
long long m = gcd(p, k);p /= m;k /= m;n--;r--;}
}else p = 1;return p;// cout << p << endl;
}
bool sortasc(const rtri a,const rtri b) //Ascending ... change the argument accordingwise;
{
return a.second<b.second;
}
bool sortdesc(const int a,const int b) //Descending... change the argument accordingwise;a
{
return a>b;
}
int myceil(int num,int den)
{
if ((num >= 0 and den > 0) or ( num <= 0 and den < 0))
return num%den == 0 ? num/den : num/den + 1;
return num / den;
}
int myfloor(int num,int den)
{
if ((num >= 0 and den > 0) or ( num <= 0 and den < 0))
return num%den == 0 ? num/den : num/den;
int ans = num / den;
return num % den == 0 ? ans : ans - 1;
}
int midofthree(int a,int b,int c)
{
if((a<=b and a>=c) or (a<=c and a>=b))
return a;
else if((b>=a and b<=c) or (b<=a and b>=c))
return b;
return c;
}
ll modI(ll a, ll m)
{
ll m0=m,y=0,x=1;
if(m==1) return 0;
while(a>1)
{ll q=a/m;ll t=m;m=a%m;a=t;t=y;y=x-q*y;x=t;
}
if(x<0) x+=m0;
return x;
}
string to_string(char c) {
return string(1, c);
}
clock_t startTime;
double getCurrentTime() {
return (double)(clock() - startTime) / CLOCKS_PER_SEC;
}
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); //From Umnik Template
using ull = unsigned long long;
ll myRand(ll B) {
return (ull)rng() % B;
}
// reinitialize value of md(example md = M1 or md=M2);
ll add(ll x, ll y) {
x += y;
if (x >= md) return x - md;
return x;
}
ll sub(ll x, ll y) {
x -= y;
if (x < 0) return x + md;
return x;
}
ll mult(ll x, ll y) {
return (x * y) % md;
}
ll bin_pow(ll x, ll p) {
if (p == 0) return 1;
if (p & 1) return mult(x, bin_pow(x, p - 1));
return bin_pow(mult(x, x), p / 2);
}
ll rev(ll x) {
return bin_pow(x, md - 2);
}
void q1();void q2();void q3();void q4();void q5();
#define pie 3.141592653589793238462643383279
//********************THE END OF`` TEMPLATES*******************//
const int N = 2e5 + 5;
bool comp(pi &a, pi &b){
return a.S < b.S;
}
struct FenwickTree{
vector<int> bit;
int n;
const int INF = (int)1e9;
FenwickTree(int n){
this->n=n;
bit.assign(n,0);
// bit.assign(n,INF);
}
FenwickTree(vector<int> a) : FenwickTree(a.size()){
for (size_t i = 0; i < a.size(); i++)
{
add(i,a[i]);
// update(i,a[i]);
}
}
int sum(int r){
int ret=0;
for (; r>=0; r = (r & (r + 1))-1){
ret+=bit[r];
}
return ret;
}
int sum(int l,int r){
return sum(r)-sum(l-1);
}
void add(int idx, int delta){
for(; idx < n; idx = idx | (idx + 1)){
bit[idx] += delta;
}
}
};
void run_testcase(){
int n;
cin >> n;
vpi Cord(n);
for (int i = 0; i < n; i++)
{
cin >> Cord[i].F;
}
for (int i = 0; i < n; i++)
{
cin >> Cord[i].S;
}
// sort(all(Cord));
vpi Cord2 = Cord;
map<int, int> compress;
int t = 1;
sort(all(Cord), comp);
for (int i = 0; i < n; i++)
{
int num = 1;
if (Cord[i].S < 0) num = -1;
if (compress.count(Cord[i].S) == 0) {
compress[Cord[i].S] = t++;
}
Cord2[i].S = compress[Cord[i].S];
Cord2[i].S *= num;
}
debug(compress);
int MaxN = n + 10;
vi cnt(MaxN);
int ans = 0;
Cord = Cord2;
sort(all(Cord));
FenwickTree FT(MaxN);
FenwickTree FTS(MaxN);
debug(Cord);
int neg_num = 0, neg_sum = 0;
for (int i = 0; i < n; i++)
{
int val = Cord[i].S;
debug(i, val);
if (val < 0){
neg_num++;
neg_sum += abs(val);
continue;
}
int small = FT.sum(val);
ans += Cord[i].F * small;
FT.add(val, 1);
int val2 = FTS.sum(val);
debug(small, ans, val2);
// debug(val2);
ans -= val2;
FTS.add(val, Cord[i].F);
ans += (neg_num)*Cord[i].F - neg_num;
debug(i, ans);
}
FenwickTree FT2(MaxN);
FenwickTree FTS2(MaxN);
for (int i = 0; i < n; i++)
{
int val = Cord[i].S;
if (val > 0) continue;
val *= -1;
int small = FT2.sum(val);
ans += Cord[i].F * small;
FT2.add(val, 1);
int val2 = FTS2.sum(val);
ans -= val2;
FTS2.add(val, Cord[i].F);
}
cout << ans << endl;
}
int32_t main()
{
startTime = clock();
fast();
#ifdef Local_Debug
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
// test()
{
run_testcase();
}
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": []
}
|
IN-CORRECT
|
cpp
|
#pragma GCC optimize("O3")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define int long long
#define pb push_back
#define pf push_front
#define eb emplace_back
#define mp make_pair
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define f first
#define s second
#define sz(x) (int)x.size()
#define endl "\n"
#define forn(i,n) for(int i=0;i<n;++i)
#define fore(i,l,r) for(int i=int(l);i<=int(r);++i)
#define rep(i,begin,end) for(__typeof(end) i=(begin);i!=(end);i++)
#define fill(a,value) memset(a,value,sizeof(a));
#define gcd(a,b) __gcd((a),(b))
#define watch1(x) cout<<(x)<<endl
#define watch2(x,y) cout<<(x)<<" "<<(y)<<endl
#define watch3(x,y,z) cout<<(x)<<" "<<(y)<<" "<<(z)<<endl
#define fastio ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vpii;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> oset;
const int INF = 9e18;
const int mod = 1e9 + 7;
const int N = 2e5 + 5;
int i,n,v[N],bit[N],val=1,ans=0;
vpii x;
void update(int idx,int val)
{
while(idx<=2*n)
{
bit[idx]+=val;
idx+=idx&-idx;
}
}
int query(int idx)
{
int ans=0;
while(idx>0)
{
ans+=bit[idx];
idx-=idx&-idx;
}
return ans;
}
int rsum(int l,int r)
{
return query(r)-query(l-1);
}
void solve()
{
cin>>n;
vi compress;
for(i=1;i<=n;++i)
{
int a;
cin>>a;
x.pb({a,i});
}
sort(all(x));
for(i=1;i<=n;++i)
{
cin>>v[i];
compress.pb(v[i]);
}
sort(all(compress));
map<int,int> mp;
mp[compress[0]]=1;
for(i=1;i<n;++i)
{
if(compress[i]!=compress[i-1])
val++;
mp[compress[i]]=val;
}
oset s;
for(i=n-1;i>=0;--i)
{
int idx=x[i].s;
int sum=rsum(mp[v[idx]],n);
int bigger=n-1-i-s.order_of_key(mp[v[idx]]);
ans+=sum-bigger*x[i].f;
s.insert(mp[v[idx]]);
update(mp[v[idx]],x[i].f);
}
cout<<ans;
}
signed main()
{
fastio;
int t;
//cin>>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": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
bool test_cases_exist = false;
class FenwickTree {
public:
vector<long long int> tree;
long long int n;
FenwickTree(long long int n) {
this->n = n;
tree.resize(n + 5);
}
void update(long long int ind, long long int delta) {
while (ind <= n) {
tree[ind] += delta;
ind += ind & -ind;
}
}
long long int query(long long int ind) {
long long int res = 0;
while (ind >= 1) {
res += tree[ind];
ind -= ind & -ind;
}
return res;
}
};
void solve() {
long long int n;
cin >> n;
vector<pair<long long int, long long int>> pnt(n);
for (auto &z : pnt) cin >> z.first;
for (auto &z : pnt) cin >> z.second;
vector<long long int> speed;
for (auto &z : pnt) speed.push_back(z.second);
sort(pnt.begin(), pnt.end());
long long int ans = 0;
vector<FenwickTree> ft(2, {n});
for (auto z : pnt) {
int pos = lower_bound(speed.begin(), speed.end(), z.second) - speed.begin();
ans += ft[0].query(pos + 1) * z.first;
ans -= ft[1].query(pos + 1);
ft[0].update(pos + 1, 1);
ft[1].update(pos + 1, z.first);
}
cout << ans << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int t = 1;
if (test_cases_exist) cin >> t;
while (t--) solve();
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int dddx[] = {1, -1, 0, 0, 1, 1, -1, -1};
int dddy[] = {0, 0, 1, -1, 1, -1, 1, -1};
vector<pair<long long int, long long int> > vec;
int n;
long long int tree[(int)(2 * 1e5 + 1)], tree2[(int)(2 * 1e5 + 1)];
void atualiza(int x, int v) {
for (; x <= n; x += (x & -x)) tree[x] += v;
}
void atualiza2(int x, int v) {
for (; x <= n; x += (x & -x)) tree2[x] += v;
}
long long int sum(long long int x) {
int s = 0;
for (; x > 0; x -= (x & -x)) s += tree[x];
return s;
}
long long int sum2(int x) {
int s = 0;
for (; x > 0; x -= (x & -x)) s += tree2[x];
return s;
}
void compress() {
vector<int> aux2;
for (int i = 1; i <= n; i++) aux2.push_back(vec[i].second);
sort(aux2.begin(), aux2.end());
for (int j = 1; j <= n; j++) {
auto i = vec[j];
vec[j].second =
lower_bound(aux2.begin(), aux2.end(), i.second) - aux2.begin();
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
vector<int> aux;
vec.push_back({0, 0});
for (int i = 1; i <= n; i++) {
int a;
cin >> a;
vec.push_back({a, 1});
}
for (int i = 1; i <= n; i++) {
int a;
cin >> a;
vec[i].second = a;
}
sort(vec.begin() + 1, vec.end());
compress();
long long int ans = 0;
for (int i = 1; i <= n; i++) {
ans += (sum2(i - 1) * vec[i].first - sum(i - 1));
atualiza(vec[i].second + 1, vec[i].first);
atualiza2(vec[i].second + 1, 1);
}
cout << ans << "\n";
return 0;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-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 = 200000000;
int n;
vector<Point> Pos, Neg;
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];
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;
}
int get_sum(int i0, int i1) { return get(i1) - get(i0); }
void solve(vector<Point>& v) {
int sz = v.size();
for (int i = 0; i < (200005); i++) ft[i] = 0;
sort((v.begin()), (v.end()), sort_pos);
for (int i = 0; i < (sz); i++) upd(i, r - v[i].pos);
map<long long, long long> DondeEsta;
for (int i = 0; i < (sz); i++) DondeEsta[v[i].pos] = i;
sort((v.begin()), (v.end()), sort_vel);
set<long long> usados;
for (auto x : v) {
int i = DondeEsta[x.pos];
int dif = 0;
dif = distance(usados.begin(),
upper_bound((usados.begin()), (usados.end()), x.pos));
sum += get_sum(0, i) - ((r - x.pos) * (i - dif));
upd(i, -1 * get_sum(i, i + 1));
usados.insert(x.pos);
}
}
int main() {
cin >> n;
for (int i = 0; i < (n); i++) cin >> P[i];
for (int i = 0; i < (n); i++) {
cin >> V[i];
if (V[i] >= 0)
Pos.push_back({P[i], V[i]});
else {
Pos.push_back({P[i], -1 * V[i]});
}
}
solve(Pos);
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": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
using LL = long long int;
template <class T>
struct Binary_Indexed_Tree {
int maxSize;
T* data;
Binary_Indexed_Tree(int init_Size) : maxSize(init_Size) {
data = new T[maxSize + 5];
memset(data, 0, sizeof(T) * (maxSize + 5));
}
~Binary_Indexed_Tree() { delete[] data; }
void add(int loc, T val) {
while (loc < maxSize) {
data[loc] += val;
loc += (loc & (-loc));
}
return;
}
T query(int loc) {
T ans = 0;
while (loc > 0) {
ans += data[loc];
loc -= (loc & (-loc));
}
return ans;
}
};
struct point {
int x, v;
point(int x_ = 0, int v_ = 0) : x(x_), v(v_) {}
bool operator<(const point& b) { return x < b.x; }
};
const int maxn = 2e5 + 5;
point p[maxn];
int n = 0, v[maxn];
int main(void) {
scanf("%d", &n);
for (int i = 0; i < n; ++i)
scanf("%d", &p[i].x);
for (int i = 0; i < n; ++i) {
scanf("%d", &p[i].v);
v[i] = p[i].v;
}
sort(p, p + n), sort(v, v + n);
int cnt = unique(v, v + n) - v + 1;
auto getid = [=](int val) { return lower_bound(v, v + cnt, val) - v + 1; };
Binary_Indexed_Tree<int> c(n);
Binary_Indexed_Tree<LL> x(n);
LL ans = 0;
for (int i = 0; i < n; ++i) {
int cnt = c.query(getid(p[i].v));
LL sum = x.query(getid(p[i].v));
ans += (cnt * (LL)p[i].x - sum);
c.add(getid(p[i].v), 1);
x.add(getid(p[i].v), 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": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<int, int>;
constexpr int MAXN = 5 + 200000;
constexpr int MINV = 1 + 100000000;
struct Bit {
ll b[MAXN];
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;
}
ll query(int l, int r) {
if (l > r) return 0;
return query(r) - query(l - 1);
}
};
ii a[MAXN];
int m[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; });
comp::compress(a, n, 1);
ll tot = 0LL, pos = 0LL, pre = 0LL;
for (int i = (int)0; i < (int)n; ++i) pos += a[i].first;
for (int i = (int)0; i < (int)n; ++i) {
pos -= a[i].first;
tot += ((1LL * i * a[i].first) - 1LL * pre);
pre += a[i].first;
}
Bit suma, cuenta;
ll ans = 0LL;
for (int i = n - 1; i >= 0; --i) {
ans += (cuenta.query(1, a[i].second - 1) * 1LL * a[i].first -
suma.query(1, a[i].second - 1));
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": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int n, len;
int bit[N][2];
void add(int x, int val) {
while (x <= n) {
bit[x][0]++;
bit[x][1] += val;
x += x & -x;
}
}
long long query(int x, int k) {
long long res = 0;
while (x) {
res += bit[x][k];
x -= x & -x;
}
return res;
}
int main() {
scanf("%d", &n);
vector<pair<int, int> > a(n);
vector<int> v(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);
v[i] = a[i].second;
}
sort(a.begin(), a.end());
sort(v.begin(), v.end());
len = unique(v.begin(), v.end()) - v.begin();
long long ans = 0;
for (int i = 0; i < n; i++) {
int now =
lower_bound(v.begin(), v.begin() + len, a[i].second) - v.begin() + 1;
ans += 1LL * a[i].first * query(now, 0) - query(now, 1);
add(now, a[i].first);
}
printf("%lld\n", ans);
return 0;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
java
|
import java.util.*;
public class MovingPoints {
static class Pair {
int first;
int second;
Pair(int first, int second) {
this.first = first;
this.second = second;
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] coords = new int[n];
int[] vels = new int[n];
for (int i = 0; i < n; ++i) {
coords[i] = scanner.nextInt();
}
for (int i = 0; i < n; ++i) {
vels[i] = scanner.nextInt();
}
Pair[] vec = new Pair[n];
for(int i = 0; i < n; ++i) {
vec[i] = new Pair(coords[i], vels[i]);
}
Arrays.sort(coords);
Arrays.sort(vec, (o1, o2) -> {
if(o1.second == o2.second)
return -(o2.first - o1.first);
return -(o2.second - o1.second);
});
int sum = 0;
Map<Integer, Integer> map = new HashMap<>();
for(int i = 0; i < n; ++i) {
sum += coords[i] * (2 * i + 1 - n);
map.put(coords[i], i);
}
for(int i = 0; i < n; i++) {
sum -= vec[i].first * (map.get(vec[i].first) - i);
}
System.out.println(sum);
}
}
|
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": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, X[N];
int l_cnt, r_cnt;
pair<int, int> L[N], R[N];
vector<int> nums;
int cnt[N];
long long sum[N];
int tt, q[N];
long long dis;
int get_pos(int p) {
int l = 1, r = r_cnt;
while (l < r) {
int mid = l + r >> 1;
if (R[mid].first > p)
r = mid;
else
l = mid + 1;
}
if (R[l].first <= p) l = -1;
return l;
}
void get_differ() {
for (int i = r_cnt; i; i--) sum[i] = sum[i + 1] + R[i].first;
for (int i = 1; i <= l_cnt; i++) {
int pos = get_pos(L[i].first);
if (pos == -1) continue;
dis += sum[pos] - 1ll * (r_cnt - pos + 1) * L[i].first;
}
return;
}
int get_idx(int x) {
return lower_bound(nums.begin(), nums.end(), x) - nums.begin() + 1;
}
int lowbit(int x) { return x & -x; }
void add(int pos, int c) {
for (int i = pos; i <= nums.size(); i += lowbit(i)) {
sum[i] += c;
cnt[i]++;
}
return;
}
long long get_sum(int pos) {
long long s = 0;
for (int i = pos; i; i -= lowbit(i)) s += sum[i];
return s;
}
int get_cnt(int pos) {
int c = 0;
for (int i = pos; i; i -= lowbit(i)) c += cnt[i];
return c;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", X + i);
for (int i = 1; i <= n; i++) {
int v;
scanf("%d", &v);
if (!v) {
L[++l_cnt] = {X[i], v};
R[++r_cnt] = {X[i], v};
} else if (v > 0)
R[++r_cnt] = {X[i], v};
else
L[++l_cnt] = {X[i], v};
nums.push_back(v);
}
sort(L + 1, L + l_cnt + 1);
sort(R + 1, R + r_cnt + 1);
get_differ();
sort(nums.begin(), nums.end());
nums.erase(unique(nums.begin(), nums.end()), nums.end());
memset(sum, 0, sizeof sum);
for (int i = 1; i <= l_cnt; i++) {
int pos = get_idx(L[i].second), c = get_cnt(pos);
dis += 1ll * c * L[i].first - get_sum(pos);
add(pos, L[i].first);
}
memset(sum, 0, sizeof sum);
memset(cnt, 0, sizeof cnt);
for (int i = r_cnt; i; i--) {
int pos = get_idx(R[i].second), c = get_cnt(nums.size()) - get_cnt(pos - 1);
dis += get_sum(nums.size()) - get_sum(pos - 1) - 1ll * c * R[i].first;
add(pos, R[i].first);
}
printf("%lld\n", dis);
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": []
}
|
IN-CORRECT
|
java
|
//package psa.minrazdalja;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
/**
* Uvozena abstraktna funkcija za klic Paid<L, R>
*/
public static class Pair<L,R> {
private final L left;
private final R right;
public Pair(L left, R right) {
assert left != null;
assert right != null;
this.left = left;
this.right = right;
}
public L getLeft() { return left; }
public R getRight() { return right; }
@Override
public int hashCode() { return left.hashCode() ^ right.hashCode(); }
@Override
public boolean equals(Object o) {
if (!(o instanceof Pair)) return false;
Pair pairo = (Pair) o;
return this.left.equals(pairo.getLeft()) &&
this.right.equals(pairo.getRight());
}
}
/**?
* glavna funkcija racunanja:
* sprejme list tipa ArrayList<Pair<Integer, Integer>>, potem st vseh elementov in seveda listo vseh elementov.
*
* nato izracunamo maksimalno distance, ki je sestevek vseh distanc med posameznimi elementi, kar lahko storimo, ker je sortirana tabela.
* V drugem delu odstejemo od maksimalnega sestevka distanc, posamezne ki se sekajo v prihodnosti
* ostane nam samo tocni sestevek minimalnih distanc
*/
public static long elele(ArrayList<Pair<Long,Long>> elementi, int s, Long[] x_el) {
long skupek = 0;
HashMap<Long, Integer> pos = new HashMap<Long, Integer>();
for(int i=0; i<s; ++i) {
skupek += x_el[i] * Long.valueOf(i) + (-1L * x_el[i]) * Long.valueOf(s - 1 - i);
pos.put(x_el[i], i);
}
for(int i=0; i<s; ++i){
skupek -= elementi.get(i).getRight() * (pos.get(elementi.get(i).getRight()) - i);
}
//2 1 4 3 5
//2 2 2 3 4
return skupek;
}
public static void main(String[] args) throws IOException {
int st_el;
// uporabimo bufferedreader zaradi 1000x vecje hitrosti kot pa skener
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
//System.out.println("Vnesi stevilo elementov: ");
st_el = Integer.parseInt(br.readLine());
//System.out.println(st_el);
Long[] x_el = new Long[st_el];
Long[] v_el = new Long[st_el];
//System.out.println("Vnesi elemente: ");
String x = br.readLine();
String[] strs = x.trim().split("\\s+");
for(int i=0; i<st_el; ++i) {
x_el[i] = Long.parseLong(strs[i]);
}
//System.out.println("Vnesi tezo elementov: ");
String y = br.readLine();
String[] strs2 = y.trim().split("\\s+");
for(int i=0; i<st_el; ++i) {
v_el[i] = Long.parseLong(strs2[i]);
}
br.close();
//pospravimo skupej v par oblike <integer, integer> oz <hitrost, element>
ArrayList<Pair<Long,Long>> listt = new ArrayList<Pair<Long, Long>>(st_el);
for(int i=0; i<st_el; i++) {
Pair<Long, Long> pair = new Pair<Long, Long>(v_el[i],x_el[i]);
listt.add(pair);
}
/*sortiramo listt od najmanjsega do najvecjega
* - sortiramo prvo po hitrostih (to je glavno najbolj vazno)
* - ce je hitrost ista potem pa sortiramo se glede na element
* - O(n log n)
*/
Collections.sort(listt, new Comparator<Pair<Long, Long>>() {
@Override
public int compare(final Pair<Long, Long> o1, final Pair<Long, Long> o2) {
if(o1.getLeft()<o2.getLeft()) {
return -1;
} else if(o1.getLeft() == o2.getLeft()) {
if(o1.getRight() < o2.getRight()){
return -1;
}else{
return 1;
}
} else {
return 1;
}
}
});
//sortiramo se elemente
// O(n log n)
Arrays.sort(x_el);
//izpisemo elemente
System.out.println(elele(listt,st_el,x_el));
}
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
struct hash_pair {
template <class T1, class T2>
size_t operator()(const pair<T1, T2>& p) const {
auto hash1 = hash<T1>{}(p.first);
auto hash2 = hash<T2>{}(p.second);
return hash1 ^ hash2;
}
};
bool prime[10000001];
long long P = 1000000007 - 2;
long long factorial[10000001] = {0};
long long powerFunction(long long x, long long y) {
long long res = 1;
long long p = 1000000007;
x = x % p;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
void sieveFunction(long long maxLimit) {
memset(prime, true, sizeof(prime));
prime[0] = prime[1] = false;
for (long long i = 2; maxLimit >= i; i++) {
if (prime[i]) {
for (long long j = 2 * i; maxLimit >= j; j += i) prime[j] = false;
}
}
}
void factorialFunction(long long maxLimit) {
factorial[0] = 1;
for (long long i = 1; i <= maxLimit; i++)
factorial[i] = (factorial[i - 1] * i) % 1000000007;
return;
}
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
vector<pair<long long, long long>> tree[800001];
vector<long long> treesum[800001];
vector<pair<long long, long long>> v;
void build(long long node, long long start, long long end) {
if (start == end) {
vector<pair<long long, long long>> temp;
temp.push_back({v[start].second, v[start].first});
tree[node] = temp;
vector<long long> sum;
long long s = 0;
for (long long i = 0; tree[node].size() > i; i++) {
s += tree[node][i].second;
treesum[node].push_back(s);
}
return;
}
long long mid = (start + end) / 2;
build(2 * node, start, mid);
build(2 * node + 1, mid + 1, end);
long long i = 0, j = 0;
while (i < tree[2 * node].size() && j < tree[2 * node + 1].size()) {
if (tree[2 * node][i] <= tree[2 * node + 1][j]) {
tree[node].push_back(tree[2 * node][i]);
i++;
} else {
tree[node].push_back(tree[2 * node + 1][j]);
j++;
}
}
for (long long k = i; k < tree[2 * node].size(); k++)
tree[node].push_back(tree[2 * node][k]);
for (long long k = j; k < tree[2 * node + 1].size(); k++)
tree[node].push_back(tree[2 * node + 1][k]);
long long s = 0;
for (long long k = 0; tree[node].size() > k; k++) {
s += tree[node][k].second;
treesum[node].push_back(s);
}
}
long long n;
pair<long long, long long> query(long long node, long long start, long long end,
long long x) {
if (start > x && end <= n) {
pair<long long, long long> tem = {v[x].second, INT64_MIN};
long long id = lower_bound(tree[node].begin(), tree[node].end(), tem) -
tree[node].begin();
if (id) {
return {treesum[node].back() - treesum[node][id - 1],
(long long)tree[node].size() - id};
}
return {treesum[node].back(), tree[node].size()};
}
if (end <= x || start > n) {
return {0, 0};
}
long long mid = (start + end) / 2;
return {query(2 * node, start, mid, x).first +
query(2 * node + 1, mid + 1, end, x).first,
query(2 * node, start, mid, x).second +
query(2 * node + 1, mid + 1, end, x).second};
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
cin >> n;
long long x[n + 1], s[n + 1];
for (long long i = 1; n >= i; i++) {
cin >> x[i];
}
for (long long i = 1; n >= i; i++) {
cin >> s[i];
}
v.push_back({INT64_MIN, INT64_MIN});
for (long long i = 1; n >= i; i++) {
v.push_back({x[i], s[i]});
}
sort(v.begin(), v.end());
build(1, 1, n);
if (n == 200000) return 0;
long long ans = 0;
for (long long i = 1; n >= i; i++) {
long long xx = v[i].first;
pair<long long, long long> temp = query(1, 1, n, i);
temp.first -= xx * temp.second;
ans += temp.first;
}
cout << ans << "\n";
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int dddx[] = {1, -1, 0, 0, 1, 1, -1, -1};
int dddy[] = {0, 0, 1, -1, 1, -1, 1, -1};
vector<pair<long long int, long long int> > vec;
int n;
long long int tree[(int)(2 * 1e5 + 1)], tree2[(int)(2 * 1e5 + 1)];
void atualiza(int x, int v) {
for (; x < n; x = x | (x + 1)) tree[x] += v;
}
void atualiza2(int x, int v) {
for (; x < n; x = x | (x + 1)) tree2[x] += v;
}
long long int sum(long long int x) {
int s = 0;
for (; x >= 0; x = (x & (x + 1)) - 1) s += tree[x];
return s;
}
long long int sum2(int x) {
int s = 0;
for (; x >= 0; x = (x & (x + 1)) - 1) s += tree2[x];
return s;
}
void compress() {
vector<int> aux2;
for (int i = 0; i < n; i++) aux2.push_back(vec[i].second);
sort(aux2.begin(), aux2.end());
for (int j = 0; j < n; j++) {
auto i = vec[j];
vec[j].second =
lower_bound(aux2.begin(), aux2.end(), i.second) - aux2.begin();
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
vector<int> aux;
for (int i = 1; i <= n; i++) {
int a;
cin >> a;
vec.push_back({a, 1});
}
for (int i = 0; i < n; i++) {
int a;
cin >> a;
vec[i].second = a;
}
sort(vec.begin(), vec.end());
compress();
for (int i = 0; i < n; i++) aux.push_back(vec[i].second);
long long int ans = 0;
for (int i = 0; i < n; i++) {
int pos = lower_bound(aux.begin(), aux.end(), vec[i].second) - aux.begin();
ans += (sum2(pos) * vec[i].first - sum(pos));
atualiza(vec[i].second, vec[i].first);
atualiza2(vec[i].second, 1);
}
cout << ans << "\n";
return 0;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int n;
int main() {
int n;
cin >> n;
vector<long int> x(n);
vector<long int> v(n);
for (int i = 0; i < n; i++) {
cin >> x[i];
}
for (int i = 0; i < n; i++) {
cin >> v[i];
}
int d = 0;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (v[j] < 0 && v[i] > 0) {
if (x[i] < x[j])
d += 0;
else
d += abs(x[j] - x[i]);
} else if (v[j] < 0 && v[i] > 0) {
if (x[i] > x[j])
d += 0;
else
d += abs(x[j] - x[i]);
} else {
if ((v[j] > v[i] && x[j] > x[i]) || v[j] == v[i]) {
d += abs(x[j] - x[i]);
}
}
}
}
cout << d;
}
|
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": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
struct Node {
int x, v;
bool operator<(const Node &rhs) const { return x < rhs.x; }
} p[maxn];
int b[maxn], t[maxn], n, to, tt[maxn];
inline int lowbit(int x) { return x & -x; }
void update(int *t, int x, int v) {
for (; x <= n; x += lowbit(x)) {
t[x] += v;
}
}
int query(int *t, int x) {
int sum = 0;
for (; x; x -= lowbit(x)) {
sum += t[x];
}
return sum;
}
int find(int x) { return lower_bound(b + 1, b + to + 1, x) - b; }
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);
b[i] = p[i].v;
}
sort(p + 1, p + n + 1);
sort(b + 1, b + n + 1);
to = unique(b + 1, b + n + 1) - b - 1;
long long ans = 0;
for (int i = 1; i <= n; ++i) cout << p[i].v << " ";
cout << endl;
for (int i = 1; i <= n; ++i) {
int v = find(p[i].v);
ans += 1LL * query(tt, v) * p[i].x - query(t, v);
update(t, v, p[i].x);
update(tt, 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": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int MOD = int(1e9) + 7;
const long long int MOD64 = (long long int)(1e18) + 7;
const int INF = 0x7fffffff;
const long long int INF64 = 0x7fffffffffffffff;
const int N = 2e5 + 10;
struct node {
int x, v, ind;
} p[N];
bool compv(node a, node b) {
if (a.v != b.v) {
return a.v < b.v;
} else
return a.x < b.x;
}
bool compx(node a, node b) { return a.x < b.x; }
int n;
long long int sum[N], num[N];
int lowbit(int x) { return x & (-x); }
void update(int i, int k) {
while (i <= N) {
sum[i] += k;
num[i] += 1;
i += lowbit(i);
}
}
pair<int, int> getsum(int i) {
long long int a = 0, b = 0;
while (i > 0) {
a += sum[i];
b += num[i];
i -= lowbit(i);
}
return make_pair(a, b);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = int(1); i < int(n + 1); i++) {
cin >> p[i].x;
}
for (int i = int(1); i < int(n + 1); i++) cin >> p[i].v;
sort(p + 1, p + n + 1, compv);
for (int i = int(1); i < int(n + 1); i++) p[i].ind = i;
long long int ans = 0;
sort(p + 1, p + n + 1, compx);
memset(sum, 0, sizeof(sum));
memset(num, 0, sizeof(num));
for (int i = int(1); i < int(n + 1); i++) {
pair<int, int> tmp = getsum(p[i].ind);
ans += tmp.second * p[i].x - tmp.first;
update(p[i].ind, p[i].x);
}
cout << ans << endl;
return 0;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
vector<pair<long long, long long>> t;
pair<long long, long long> get(long long v, long long l, long long r,
long long a, long long b) {
if (l > b || a > r) {
return {0, 0};
}
if (a <= l && r <= b) {
return t[v];
}
long long m = (l + r) / 2;
pair<long long, long long> tmp = get(2 * v, l, m, a, b),
tmp2 = get(2 * v + 1, m + 1, r, a, b);
return {tmp.first + tmp2.first, tmp.second + tmp2.second};
}
void update(long long v, long long l, long long r, long long i, long long x) {
if (l > i || i > r) {
return;
}
if (l == r) {
t[v] = {t[v].first + x, t[v].second + 1};
return;
}
long long m = (l + r) / 2;
update(2 * v, l, m, i, x);
update(2 * v + 1, m + 1, r, i, x);
t[v].first = t[2 * v].first + t[2 * v + 1].first;
t[v].second = t[2 * v].second + t[2 * v + 1].second;
}
signed main() {
long long n;
cin >> n;
long long q = 1 << (long long)ceil(log2(n));
t.resize(2 * q);
vector<pair<long long, long long>> x(n);
for (long long i = 0; i < n; ++i) {
cin >> x[i].first;
}
for (long long i = 0; i < n; ++i) {
cin >> x[i].second;
}
sort(x.begin(), x.end());
vector<long long> a(n);
for (long long i = 0; i < n; ++i) {
a[i] = x[i].second;
}
unordered_map<long long, long long> b;
sort(a.begin(), a.end());
a.resize(unique(a.begin(), a.end()) - a.begin());
for (long long i = 0; i < a.size(); ++i) {
b[a[i]] = i;
}
vector<long long> c(n);
for (long long i = 0; i < n; ++i) {
c[i] = b[x[i].second];
}
long long now = 0;
long long sum = 0, kol = 0;
for (long long i = 0; i < n; ++i) {
if (x[i].second < 0) {
sum += x[i].first;
++kol;
continue;
}
if (x[i].second > 0) {
pair<long long, long long> tmp = get(1, 0, q - 1, 0, c[i]);
now += tmp.second * x[i].first - tmp.first;
update(1, 0, q - 1, c[i], x[i].first);
} else {
now += kol * x[i].first - sum;
sum += x[i].first;
++kol;
}
}
t.assign(2 * q, {0, 0});
sum = 0;
kol = 0;
for (long long i = n - 1; i >= 0; --i) {
if (x[i].second > 0) {
sum += x[i].first;
++kol;
continue;
}
if (x[i].second < 0) {
pair<long long, long long> tmp = get(1, 0, q - 1, c[i], n - 1);
now += tmp.first - tmp.second * x[i].first;
update(1, 0, q - 1, c[i], x[i].first);
} else {
now += sum - kol * x[i].first;
}
}
long long nowl = 0, koll = 0, nowr = 0, kolr = 0;
for (long long i = 0; i < n; ++i) {
if (x[i].second > 0) {
now += koll * x[i].first - nowl;
++kolr;
nowr += x[i].first;
} else {
now += kolr * x[i].first - nowr;
++koll;
nowl += x[i].first;
}
}
cout << now;
return 0;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
cpp
|
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<cmath>
#include<ctime>
#include<vector>
#include<queue>
#include<stack>
#include<list>
#include<set>
#include<map>
#include<utility>
#include<algorithm>
using namespace std;
#define FOR(i,a,b) for(register int i=(a);i<(b);++i)
#define FORR(i,a,b) for(register int i=(a);i<=(b);++i)
#define ROR(i,a,b) for(register int i=(a);i>=(b);--i)
#define RORR(i,a,b) for(register int i=(a);i>(b);--i)
#define PQ priority_queue
#define VR vector
#define MST(a,b) memset(a,b,sizeof(a))
#define FGETS(s) fgets(s,sizeof(s),stdin)
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define FI first
#define SE second
typedef long long LL;
typedef long long unsigned LLU;
typedef pair<int,int> pii;
const int MAX = 2e5 + 20;
int n,pos[MAX];
struct Node{
int p,v,no;
bool operator < (const Node &b){
return v != b.v ? v < b.v : p < b.p;
}
}node[MAX],v[MAX];
struct ST{ // n-1
LL v[MAX];
LL sum(int x){
LL s = 0;
while(x){
s += v[x]; x -= x&-x;
}
return s;
}
void add(int x, LL val){
while(x<=n){
v[x]+=val; x += x&-x;
}
}
LL operator [] (int idx){
return sum(idx);
}
}s,c;
int main(void){
scanf("%d",&n);
FOR(i,0,n) scanf("%d",&node[i].p);
FOR(i,0,n) scanf("%d",&node[i].v);
sort(node,node+n,[](Node &a, Node &b){
return a.p < b.p;
});
FOR(i,0,n) node[i].no=i;
memcpy(v,node,sizeof(node));
v[0]={0,0,0};
sort(v+1,v+n);
FOR(i,1,n){
pos[v[i].no]=i;
s.add(i,v[i].p);
c.add(i,1);
}
#ifdef DEBUG
FOR(i,0,n) printf(" (%d, %d)",node[i].p,node[i].v); putchar('\n');
FOR(i,0,n) printf(" (%d, %d)",v[i].p,v[i].v); putchar('\n');
#endif
LL ans = 0;
FOR(i,0,n-1){
int j = lower_bound(v+1,v+n,node[i]) - v;
if(j>=n) continue;
#ifdef DEBUG
FOR(i,1,n) printf(" s[%d]: %lld, c[%d]: %lld\n",i,s[i],i,c[i]);
printf(" for i: %d (v %d), j: %d\n",i,node[i].v,j);
printf(" s[n]:%lld, s[j-1]: %lld, c[j-1]: %lld\n",s[n],s[j-1],c[j-1]);
printf(" ans += %lld\n",s[n]-s[j-1] - (n - 1ll - c[j-1])*node[i].p);
#endif
ans += s[n]-s[j-1] - (c[n] - c[j-1])*node[i].p;
int t = pos[i+1];
s.add(t,-v[t].p);
c.add(t,-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": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
struct abc {
long long pozicia;
long long sichqare;
};
bool comp(abc a, abc b) { return a.pozicia < b.pozicia; }
int main() {
long long n, cnt = 0;
cin >> n;
abc a[n + 1];
for (int i = 1; i <= n; i++) cin >> a[i].pozicia;
for (int i = 1; i <= n; i++) cin >> a[i].sichqare;
sort(a + 1, a + n + 1, comp);
for (long long i = 1; i < n; i++) {
for (long long j = i + 1; j <= n; j++) {
if (a[i].sichqare > 0 and a[j].sichqare > 0) {
if (a[i].sichqare <= a[j].sichqare) {
cnt += j - i;
}
} else if (a[i].sichqare < 0 and a[j].sichqare < 0) {
if (a[i].sichqare <= a[j].sichqare) {
cnt += j - i;
}
} else if (a[i].sichqare <= 0 and a[j].sichqare >= 0)
cnt += j - i;
else if (a[i].sichqare == 0 and a[j].sichqare >= 0)
cnt += j - i;
else if (a[i].sichqare <= 0 and a[j].sichqare == 0)
cnt += j - i;
else if ((a[i].sichqare != 0 and a[j].sichqare != 0) or
(a[i].sichqare == 0 and a[j].sichqare == 0))
cnt += j - i;
}
}
cout << cnt << 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": []
}
|
IN-CORRECT
|
java
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class MovingPoints {
static int[] pointsTree;
static int[] 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 int[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": []
}
|
IN-CORRECT
|
java
|
//package psa.minrazdalja;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
/**
* Uvozena abstraktna funkcija za klic Paid<L, R>
*/
public static class Pair<L,R> {
private final L left;
private final R right;
public Pair(L left, R right) {
assert left != null;
assert right != null;
this.left = left;
this.right = right;
}
public L getLeft() { return left; }
public R getRight() { return right; }
@Override
public int hashCode() { return left.hashCode() ^ right.hashCode(); }
@Override
public boolean equals(Object o) {
if (!(o instanceof Pair)) return false;
Pair pairo = (Pair) o;
return this.left.equals(pairo.getLeft()) &&
this.right.equals(pairo.getRight());
}
}
/**?
* glavna funkcija racunanja:
* sprejme list tipa ArrayList<Pair<Integer, Integer>>, potem st vseh elementov in seveda listo vseh elementov.
*
* nato izracunamo maksimalno distance, ki je sestevek vseh distanc med posameznimi elementi, kar lahko storimo, ker je sortirana tabela.
* V drugem delu odstejemo od maksimalnega sestevka distanc, posamezne ki se sekajo v prihodnosti
* ostane nam samo tocni sestevek minimalnih distanc
*/
public static long elele(ArrayList<Pair<Long,Long>> elementi, int s, Long[] x_el) {
long skupek = 0L;
HashMap<Long, Integer> pos = new HashMap<Long, Integer>();
for(int i=0; i<s; ++i) {
skupek += x_el[i] * Long.valueOf(i) + (-1L * x_el[i]) * Long.valueOf(s - 1 - i);
pos.put(x_el[i], i);
}
for(int i=0; i<s; ++i){
skupek -= elementi.get(i).getRight() * (pos.get(elementi.get(i).getRight()) - i);
}
//2 1 4 3 5
//2 2 2 3 4
return skupek;
}
public static void main(String[] args) throws IOException {
int st_el;
// uporabimo bufferedreader zaradi 1000x vecje hitrosti kot pa skener
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
//System.out.println("Vnesi stevilo elementov: ");
st_el = Integer.parseInt(br.readLine());
//System.out.println(st_el);
Long[] x_el = new Long[st_el];
Long[] v_el = new Long[st_el];
//System.out.println("Vnesi elemente: ");
String x = br.readLine();
String[] strs = x.trim().split("\\s+");
for(int i=0; i<st_el; ++i) {
x_el[i] = Long.parseLong(strs[i]);
}
//System.out.println("Vnesi tezo elementov: ");
String y = br.readLine();
String[] strs2 = y.trim().split("\\s+");
for(int i=0; i<st_el; ++i) {
v_el[i] = Long.parseLong(strs2[i]);
}
br.close();
//pospravimo skupej v par oblike <integer, integer> oz <hitrost, element>
ArrayList<Pair<Long,Long>> listt = new ArrayList<Pair<Long, Long>>(st_el);
for(int i=0; i<st_el; i++) {
Pair<Long, Long> pair = new Pair<Long, Long>(v_el[i],x_el[i]);
listt.add(pair);
}
/*sortiramo listt od najmanjsega do najvecjega
* - sortiramo prvo po hitrostih (to je glavno najbolj vazno)
* - ce je hitrost ista potem pa sortiramo se glede na element
* - O(n log n)
*/
Collections.sort(listt, new Comparator<Pair<Long, Long>>() {
@Override
public int compare(final Pair<Long, Long> o1, final Pair<Long, Long> o2) {
if(o1.getLeft()<o2.getLeft()) {
return -1;
} else if(o1.getLeft() == o2.getLeft()) {
if(o1.getRight() < o2.getRight()){
return -1;
}else{
return 1;
}
} else {
return 1;
}
}
});
//sortiramo se elemente
// O(n log n)
Arrays.sort(x_el);
//izpisemo elemente
System.out.println(elele(listt,st_el,x_el));
}
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-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 = 200000000;
int n;
vector<Point> Pos, Neg;
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];
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;
}
int get_sum(int i0, int i1) { return get(i1) - get(i0); }
void solve(vector<Point>& v) {
int sz = v.size();
for (int i = 0; i < (200005); i++) ft[i] = 0;
sort((v.begin()), (v.end()), sort_pos);
for (int i = 0; i < (sz); i++) upd(i, r - v[i].pos);
map<long long, long long> DondeEsta;
for (int i = 0; i < (sz); i++) DondeEsta[v[i].pos] = i;
sort((v.begin()), (v.end()), sort_vel);
set<long long> usados;
for (auto x : v) {
int i = DondeEsta[x.pos];
int dif = 0;
dif = distance(usados.begin(),
upper_bound((usados.begin()), (usados.end()), x.pos));
sum += get_sum(0, i) - ((r - x.pos) * (i - dif));
upd(i, -1 * get_sum(i, i + 1));
usados.insert(x.pos);
}
}
int main() {
cin >> n;
for (int i = 0; i < (n); i++) cin >> P[i];
for (int i = 0; i < (n); i++) {
cin >> V[i];
if (V[i] >= 0)
Pos.push_back({P[i], V[i]});
else {
maxNeg = max(maxNeg, P[i]);
Neg.push_back({P[i], -1 * V[i]});
Pos.push_back({P[i], -1 * V[i]});
}
}
for (auto& x : Neg) x.pos = maxNeg + abs(x.pos - maxNeg);
solve(Pos);
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": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
class FenwickTree {
long long n;
vector<int> BIT;
public:
FenwickTree(long long n) {
this->n = n;
BIT.assign(n + 1, 0);
}
void update(long long x, long long v) {
while (x <= n) {
BIT[x] += v;
x += (x & -x);
}
}
long long query(long long x) {
long long s = 0;
while (x > 0) {
s += BIT[x];
x -= (x & -x);
}
return s;
}
};
class TaskF {
long long n, res;
vector<pair<long long, long long>> a;
vector<long long> x, v;
map<long long, long long> m;
public:
TaskF(long long n) {
this->n = n;
res = 0;
a.assign((n + 1), {LONG_MIN, LONG_MIN});
for (long long i = 1; i <= n; i++) {
cin >> a[i].first;
}
for (long long i = 1; i <= n; i++) {
cin >> a[i].second;
}
}
long long get() {
sort(a.begin(), a.end());
for (int i = 1; i <= n; i++) {
x.emplace_back(a[i].first);
v.emplace_back(a[i].second);
m[a[i].second];
}
int k = 0;
for (auto &it : m) {
it.second = (++k);
}
for (auto &it : v) {
it = m[it];
}
FenwickTree F1(k), F2(k);
for (long long i = 0; i < n; i++) {
long long c = F1.query(v[i]);
long long s = F2.query(v[i]);
res += (c * x[i]) - s;
F1.update(v[i], 1);
F2.update(v[i], x[i]);
}
return res;
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long n;
cin >> n;
TaskF T(n);
cout << T.get() << '\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": []
}
|
IN-CORRECT
|
java
|
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;
public class ProblemF_SegmentTree {
public static InputStream inputStream = System.in;
public static OutputStream outputStream = System.out;
public static void main(String[] args) {
Scanner scanner = new Scanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
int n = scanner.nextInt();
List<Pair<Long, Long>> xvList = new ArrayList<>();
for (int i = 0; i < n; i++) {
xvList.add(new Pair<>(scanner.nextLong(), 0L));
}
for (int i = 0; i < n; i++) {
xvList.get(i).second = scanner.nextLong();
}
xvList.sort(Comparator.comparingLong(t -> t.first));
Map<Long, Pair<Long, Long>> map = new HashMap<>();
for (int i = 0; i < n; i++) {
long x = xvList.get(i).first;
long v = xvList.get(i).second;
if (!map.containsKey(v)) {
map.put(v, new Pair<>(0L, 0L));
}
map.put(v, sum(map.get(v), new Pair<>(x, 1L)));
}
Map<Long, Integer> indexes = new HashMap<>();
int index = 0;
for (Long key : new TreeSet<>(map.keySet())) {
indexes.put(key, index);
index++;
}
SegmentTree segmentTree = new SegmentTree(new ArrayList<>(map.values()));
long ans = 0L;
for (int i = 0; i < n; i++) {
long x = xvList.get(i).first;
long v = xvList.get(i).second;
index = indexes.get(v);
segmentTree.add(index, new Pair<>(-x, -1L));
Pair<Long, Long> sum = segmentTree.getSum(index, n - 1);
ans += sum.first - (sum.second * x);
}
out.println(ans);
out.flush();
}
private static class SegmentTree {
private Node root;
private SegmentTree(List<Pair<Long, Long>> list) {
root = getNode(list, 0, list.size() - 1);
}
private Node getNode(List<Pair<Long, Long>> list, int l, int r) {
Node node = new Node();
node.left = l;
node.right = r;
if (l == r) {
node.value = list.get(l);
} else {
int middle = (l + r) / 2;
node.leftNode = getNode(list, l, middle);
node.rightNode = getNode(list, middle + 1, r);
node.value = sum(node.leftNode.value, node.rightNode.value);
}
return node;
}
private Pair<Long, Long> getSum(int l, int r) {
return root.getSum(l, r);
}
private void add(int index, Pair<Long, Long> value) {
Node node = root;
while (true) {
node.value = sum(node.value, value);
if (node.leftNode == null) {
break;
}
if (node.leftNode.right >= index) {
node = node.leftNode;
} else {
node = node.rightNode;
}
}
}
}
private static class Node {
private Pair<Long, Long> value;
private int left;
private int right;
private Node leftNode;
private Node rightNode;
private Pair<Long, Long> getSum(int l, int r) {
if (left >= l && right <= r) {
return value;
}
if (right < l || left > r) {
return new Pair<>(0L, 0L);
}
return sum(leftNode.getSum(l, r), rightNode.getSum(l, r));
}
}
private static class Pair<F, S> {
private F first;
private S second;
public Pair() {}
public Pair(F first, S second) {
this.first = first;
this.second = second;
}
}
private static Pair<Long, Long> sum(Pair<Long, Long> p1, Pair<Long, Long> p2) {
return new Pair<>(p1.first + p2.first, p1.second + p2.second);
}
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-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 {
int c;
long long 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": []
}
|
IN-CORRECT
|
cpp
|
#include <iostream>
#include <fstream>
#include <cmath>
#include <random>
#include <iomanip>
#include <algorithm>
#include <numeric>
#include <functional>
#include <typeinfo>
#include <vector>
#include <array>
#include <valarray>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <any>
#include <unordered_map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cassert>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
namespace io{
template<typename First, typename Second> ostream& operator << ( ostream &os, const pair<First, Second> &p ) { return os << p.first << " " << p.second; }
template<typename First, typename Second> ostream& operator << ( ostream &os, const map<First, Second> &mp ) { for( auto it : mp ) { os << it << endl; } return os; }
template<typename First> ostream& operator << ( ostream &os, const vector<First> &v ) { bool space = false; for( First x : v ) { if( space ) os << " "; space = true; os << x; } return os; }
template<typename First> ostream& operator << ( ostream &os, const set<First> &st ) { bool space = false; for( First x : st ) { if( space ) os << " "; space = true; os << x; } return os; }
template<typename First> ostream& operator << ( ostream &os, const multiset<First> &st ) { bool space = false; for( First x : st ) { if( space ) os << " "; space = true; os << x; } return os; }
template<typename First, typename Second> istream& operator >> ( istream &is, pair<First, Second> &p ) { return is >> p.first >> p.second; }
template<typename First> istream& operator >> ( istream &is, vector<First> &v ) { for( First &x : v ) { is >> x; } return is; }
long long fastread(){ char c; long long d = 1, x = 0; do c = getchar(); while( c == ' ' || c == '\n' ); if( c == '-' ) c = getchar(), d = -1; while( isdigit( c ) ){ x = x * 10 + c - '0'; c = getchar(); } return d * x; }
static bool sep = false;
using std::to_string;
string to_string( bool x ){ return ( x ? "true" : "false" ); }
string to_string( const string & s ){ return "\"" + s + "\""; }
string to_string( const char * s ){ return "\"" + string( s ) + "\""; }
string to_string ( const char & c ) { string s; s += c; return "\'" + s + "\'"; }
template<typename First, typename Second> string to_string( pair<First, Second> p ){ return "{" + to_string( p.first ) + ", " + to_string( p.second ) + "}"; }
template<typename Type> string to_string( vector<Type> v ) { bool sep = false; string s = "["; for( Type x: v ){ if( sep ) s += ", "; sep = true; s += to_string( x ); } s += "]"; return s; }
template<typename Collection> string to_string( Collection collection ) { bool sep = false; string s = "{"; for( auto x: collection ){ if( sep ) s += ", "; sep = true; s += to_string( x ); } s += "}"; return s; }
void print() { cerr << endl; sep = false; }
template <typename First, typename... Other> void print( First first, Other... other ) { if( sep ) cerr << " | "; sep = true; cerr << to_string( first ); print( other... ); }
} using namespace io;
namespace utils {
class Range {
private:
const int start;
const int stop;
const int step;
public:
class iterator {
private:
int value;
const int step;
const int boundary;
const bool sign;
public:
typedef std::forward_iterator_tag iterator_category;
typedef int &reference;
typedef int *pointer;
iterator( int value, int step, int boundary ) : value( value ), step( step ), boundary( boundary ), sign( step > 0 ) {}
iterator operator ++ () { value += step; return *this; }
reference operator * () { return value; }
const pointer operator -> () { return &value; }
bool operator == ( const iterator& rhs ) { return sign ? ( value >= rhs.value && value > boundary ) : (value <= rhs.value && value < boundary ); }
bool operator != ( const iterator& rhs ) { return sign ? ( value < rhs.value && value >= boundary ) : ( value > rhs.value && value <= boundary ); }
};
Range( const int &start, const int &stop, const int &step ) : start( start ), stop( stop ), step( step ) {}
iterator begin() const { return iterator( start, step, start ); }
iterator end() const { return iterator( stop, step, start ); }
};
Range range( const int &stop ) { return Range( 0, stop, 1 ); }
Range range( const int &start, const int &stop ) { return Range( start, stop, 1 ); }
Range range( const int &start, const int &stop, const int &step ) { return Range( start, stop, step ); }
template<typename T> inline T max( T t ) { return t; }
template<typename First, typename ...Others> inline auto max( First first, Others ...others ) { auto result = max( others... ); return result > first ? result : first; }
template<typename T> inline T min( T t ) { return t; }
template<typename First, typename ...Others> inline auto min( First first, Others ...others ) { auto result = min( others... ); return result < first ? result : first; }
template<typename T1, typename T2> inline void updmax ( T1 &a, T2 b ) { a = a > b ? a : b; }
template<typename T1, typename T2> inline void updmin ( T1 &a, T2 b ) { a = a < b ? a : b; }
template<typename T1, typename T2> inline bool chkmax ( T1 &a, T2 b ) { if( a < b ) { a = b; return true; } return false; }
template<typename T1, typename T2> inline bool chkmin ( T1 &a, T2 b ) { if( a > b ) { a = b; return true; } return false; }
constexpr long long operator"" _E ( unsigned long long n ){ long long p = 1, a = 10; for( int i = 0; i < n; i++ ) p *= a; return p; }
long double operator"" _deg ( long double deg ){ long double PI = acos( -1 ); return deg * PI / 180; }
random_device rd; mt19937 mt( rd( ) );
template<typename T> T rand ( T l, T r ) { uniform_int_distribution<T> dist( l, r ); return dist( mt ); };
namespace Color {
string
HEADER = "\033[95m",
OKBLUE = "\033[94m",
OKGREEN = "\033[92m",
WARNING = "\033[93m",
FAIL = "\033[91m",
ENDC = "\033[0m",
BOLD = "\033[1m",
UNDERLINE = "\033[4m",
BGGREEN = "\x1b[6;30;42m";
}
struct dtime {
int h, m, s;
dtime( int h, int m, int s ) : h( h ), m( m ), s( s ) {}
dtime() {
h = ( ( time( 0 ) / 60 / 60 ) % 24 + 5 ) % 24;
m = ( time( 0 ) / 60 ) % 60;
s = time( 0 ) % 60;
}
friend bool operator < ( const dtime &t1, const dtime &t2 ){
if( t1.h == t2.h ){
if( t1.m == t2.m ){
return t1.s < t2.s;
}
return t1.m < t2.m;
}
return t1.h < t2.h;
}
friend ostream& operator << ( ostream &os, const dtime &t ){
os << t.h << " " << t.m << " " << t.s << endl;
return os;
}
friend string to_string( const dtime &t ){
return to_string( t.h ) + ":" + to_string( t.m ) + ":" + to_string( t.s );
}
};
dtime get_current_time() {
return dtime();
}
} using namespace utils;
template<typename T> class nparray {
private:
T * _data = nullptr, * _max = nullptr, * _min = nullptr, * _sum = nullptr;
size_t _size;
public:
nparray(){}
nparray( size_t size ) { __init( size ); }
void __init( size_t size ){ _size = size; _data = new T[size]; fill( 0 ); }
T sum() { return std::accumulate( _data, _data + _size, 0 ); }
T sum( size_t stop ) { return sum( 0, stop ); }
T sum( size_t start, size_t stop ){ if( _sum == nullptr ) __sumrecalc(); T sum_ = _sum[stop]; if( start ) sum_ -= _sum[start - 1]; return sum_; }
T sum( size_t start, size_t stop, size_t step ) { T sum_ = 0; for( size_t i = start; i < stop; i += step ) { sum_ += _data[i]; } return sum_; }
T max(){ return std::max_element( _data, _data + this -> _size ); }
T max( size_t index ){ assert( index < _size ); if( _max == nullptr ) __maxrecalc(); return _max[index]; }
T min(){ return std::min_element( _data, _data + _size ); }
T min( size_t index ){ assert( index < _size ); if( _min == nullptr ) __minrecalc(); return _min[index]; }
T* begin() { return _data; }
T* end() { return _data + _size; }
T* cbegin() { return _data + _size - 1; }
T* cend() { return _data - 1; }
void fill( int value ){ std::fill( _data, _data + _size, value ); }
void reverse( ){ std::reverse( _data, _data + _size ); if( _sum != nullptr ) std::reverse( _sum, _sum + _size ); if( _max != nullptr ) std::reverse( _max, _max + _size ); if( _min != nullptr ) std::reverse( _min, _min + _size ); }
void sort( ){ std::sort( _data, _data + _size ); if( _sum != nullptr ) __sumrecalc(); if( _max != nullptr ) __maxrecalc(); if( _min != nullptr ) __minrecalc(); }
void shift( int count ) { count %= ( int32_t )_size; T *temp = new T[_size]; int j = ( count < 0 ? _size + count: count ); for( size_t i = 0; i < _size; i++, j++ ){ if( j == _size ) j = 0; temp[j] = _data[i]; } for( size_t i = 0; i < _size; i++ ){ _data[i] = temp[i]; } delete temp; }
void random_shuffle( ){ std::random_shuffle( _data, _data + _size ); }
bool is_sorted( ){ for( int i = 1; i < _size; i++ ){ if( _data[i - 1] > _data[i + 1] ){ return false; } } return true; }
void resize( size_t size ) { __clear(); __init( size ); }
void __clear() { _size = 0; delete _data; delete _max; delete _min; delete _sum; }
void __sumrecalc(){ _sum = new T[_size]; _sum[0] = _data[0]; for( int i = 1; i < _size; i++ ){ _sum[i] = _sum[i - 1] + _data[i]; } }
void __maxrecalc(){ _max = new T[_size]; _max[0] = _data[0]; for( int i = 1; i < _size; i++ ){ _max[i] = ( _data[i] > _max[i - 1] ? _data[i] : _max[i - 1]); } }
void __minrecalc(){ _min = new T[_size]; _min[0] = _data[0]; for( int i = 1; i < _size; i++ ){ _min[i] = ( _data[i] < _min[i - 1] ? _data[i] : _min[i - 1]); } }
T& operator [] ( int index ){ if( index < 0 ){ assert( -index <= _size ); return _data[_size + index]; } else { assert( index < _size ); return _data[index]; } }
size_t size() { return _size; }
bool empty(){ return ( _size == 0 ); }
friend ostream& operator << ( ostream &os, nparray<T> &array ) { for( size_t i = 0; i < array.size(); i++ ) { if( i ) os << " "; os << array[i]; } return os; }
friend istream& operator >> ( istream &is, nparray<T> &array ) { for( size_t i = 0; i < array.size(); i++ ) { is >> array[i]; } return is; }
friend string to_string( nparray<T> &array ){ string s = "{ "; for( size_t i = 0; i < array.size(); i++ ) { if( i ) s += ", "; s += to_string( array[i] ); } s += "}"; return s; }
~nparray() { __clear(); }
};
/*
main
*/
#ifdef LOCAL_JUDGE
#define print( ... ) cerr << "[" << #__VA_ARGS__ << "]: "; io::print( __VA_ARGS__ );
#else
#define print( ... )
#endif
const int MOD = 2000000033; // 998'244'353
const double EPS = 1e-9;
const double PI = acos( -1. );
long long LLINF = 18_E + 10;
int INF = 2 * 8_E + 10;
const int MAXN = 3 * 5_E + 6;
const int N = 5_E + 5;
struct fenwick{
map<int, int> fw;
void update( int i, int d ){
while( i <= 2 * INF ){
fw[i] += d;
i = ( i | ( i + 1 ) );
}
}
int get( int n ){
int result = 0;
while( n >= 0 ){
if( fw.count( n ) ){
result += fw[n];
}
n = ( n & ( n + 1 ) ) - 1;
}
return result;
}
int get( int l, int r ){
return get( r ) - get( l - 1 );
}
};
fenwick sum, cnt;
int calc( const vector<pair<int, int>> &vc ){
int ans = 0;
for( int i = 0; i < vc.size(); i++ ){
int x = vc[i].first;
int v = vc[i].second;
ans += x * cnt.get( v ) - sum.get( v );
sum.update( v, x );
cnt.update( v, 1 );
}
return ans;
}
void solution( istream &cin, ostream &cout, const int &test_case )
{
int n;
cin >> n;
vector<int> a( n ), b( n );
cin >> a >> b;
vector<pair<int, int>> v;
int mn = INF;
for( int i: range( n ) ){
updmin( mn, a[i] );
}
for( int i: range( n ) ){
a[i] -= mn;
a[i]++;
b[i] += INF;
}
for( int i: range( n ) ){
v.push_back( { a[i], b[i] } );
}
sort( begin( v ), end( v ) );
print( v );
int ans = 0;
ans += calc( v );
/*
mn = INF;
for( int i: range( n ) ){
v[i].first = INF - v[i].first;
updmin( mn, v[i].first );
}
for( int i: range( n ) ){
v[i].first -= mn;
}
reverse( begin( v ), end( v ) );
ans += calc( v );
*/
cout << ans << endl;
}
int32_t main()
{
srand( time( 0 ) );
ios_base::sync_with_stdio( false ); cin.tie( nullptr ); cout.tie( nullptr );
std::istream& in( std::cin );
std::ostream& out( std::cout );
int32_t queries = 1;
//cin >> queries;
for( int32_t test_case: range( 1, queries + 1 ) ){
solution( cin, cout, test_case );
}
return EXIT_SUCCESS;
}
|
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": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int x[n], v[n], sum = 0;
for (int i = 0; i < n; i++) cin >> x[i];
for (int i = 0; i < n; i++) cin >> v[i];
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
if (x[i] <= x[j] && v[i] <= v[j])
sum += x[j] - x[i];
else if (v[j] <= v[i])
sum += x[i] - x[j];
}
}
cout << sum << endl;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
vector<int> v;
int n, m;
long long ans = 0;
struct node {
int l;
int r;
long long cnt;
long long sum;
} tree[400010];
struct no {
int pos;
int speed;
} a[200010];
bool cmp(no c, no d) { return c.pos < d.pos; }
void build(int p, int l, int r) {
tree[p].l = l, tree[p].r = r;
tree[p].cnt = tree[p].sum = 0;
if (l == r) return;
int mid = (l + r) >> 1;
build(p << 1, l, mid);
build(p << 1 | 1, mid + 1, r);
}
void update(int p, int po, int v) {
int l = tree[p].l, r = tree[p].r;
if (l == r) {
tree[p].cnt++;
tree[p].sum += v;
return;
}
int mid = (l + r) >> 1;
if (po <= mid)
update(p << 1, po, v);
else
update(p << 1 | 1, po, v);
tree[p].cnt = tree[p << 1].cnt + tree[p << 1 | 1].cnt;
tree[p].sum = tree[p << 1].sum + tree[p << 1 | 1].sum;
}
int query1(int p, int L, int R) {
int l = tree[p].l, r = tree[p].r;
if (l == r || (L <= l && R >= r)) return tree[p].cnt;
int res = 0;
int mid = (l + r) >> 1;
if (L <= mid) res += query1(p << 1, L, R);
if (R > mid) res += query1(p << 1 | 1, L, R);
return res;
}
int query2(int p, int L, int R) {
int l = tree[p].l, r = tree[p].r;
if (l == r || (L <= l && R >= r)) return tree[p].sum;
long long res = 0;
int mid = (l + r) >> 1;
if (L <= mid) res += query2(p << 1, L, R);
if (R > mid) res += query2(p << 1 | 1, L, R);
return res;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i].pos);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i].speed);
v.push_back(a[i].speed);
}
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
for (int i = 1; i <= n; i++)
a[i].speed = lower_bound(v.begin(), v.end(), a[i].speed) - v.begin() + 1;
sort(a + 1, a + n + 1, cmp);
build(1, 1, v.size());
for (int i = 1; i <= n; i++) {
long long sum1 = 0, sum2 = 0;
sum1 = query1(1, 1, a[i].speed);
sum2 = query2(1, 1, a[i].speed);
ans += a[i].pos * (sum1 + 1) - sum2 - a[i].pos;
update(1, a[i].speed, a[i].pos);
}
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": []
}
|
IN-CORRECT
|
cpp
|
// Daniel Grzegorzewski
// while (clock()<=69*CLOCKS_PER_SEC)
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
// #pragma GCC target ("avx2")
// #pragma GCC optimization ("O3")
// #pragma GCC optimization ("unroll-loops")
#define MP make_pair
#define PB push_back
#define ST first
#define ND second
#define int long long
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//X.find_by_order(k); - zwraca iterator na k-ty element (numeracja od zerowego)
//X.order_of_key(k); - zwraca liczbΔ elementΓ³w ostro mniejszych niΕΌ k
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<PII> VII;
typedef long long LL;
void init_ios() {
ios_base::sync_with_stdio(0);
cin.tie(0);
}
const int N = (1<<18);
int n, res, x[N], v[N], xx[N], vv[N], suf[N];
PII drz[2][2*N];
vector<pair<int, PII>> dod, uje;
VI ind, zera;
bool cmp(int i, int j) {
return x[i] < x[j];
}
PII operator+(PII x, PII y) {
return {x.ST+y.ST, x.ND+y.ND};
}
void add(int nr, int v, PII val) {
v += N;
drz[nr][v] = val;
while (v > 1) {
v /= 2;
drz[nr][v] = drz[nr][2*v]+drz[nr][2*v+1];
}
}
PII get(int nr, int p, int q) {
p += N;
q += N;
PII res = drz[nr][p];
if (p != q)
res = res+drz[nr][q];
while (p/2 != q/2) {
if (p%2 == 0)
res = res+drz[nr][p+1];
if (q%2 == 1)
res = res+drz[nr][q-1];
p /= 2;
q /= 2;
}
return res;
}
signed main() {
init_ios();
cin >> n;
for (int i = 1; i <= n; ++i)
cin >> x[i];
for (int i = 1; i <= n; ++i)
cin >> v[i];
for (int i = 1; i <= n; ++i)
ind.PB(i);
sort(ind.begin(), ind.end(), cmp);
for (int i = 0; i < n; ++i)
xx[i+1] = x[ind[i]], vv[i+1] = v[ind[i]];
for (int i = 1; i <= n; ++i)
x[i] = xx[i], v[i] = vv[i];
for (int i = 1; i <= n; ++i) {
if (v[i] <= 0)
uje.PB({-v[i], {-x[i], i}});
if (v[i] >= 0)
dod.PB({v[i], {x[i], i}});
if (v[i] == 0)
zera.PB(x[i]);
}
sort(dod.begin(), dod.end());
while (dod.size()) {
int id = dod.back().ND.ND;
dod.pop_back();
PII hm = get(0, id, n);
res += hm.ST - x[id]*hm.ND;
add(0, id, {x[id], 1});
}
sort(uje.begin(), uje.end());
while (uje.size()) {
int id = uje.back().ND.ND;
uje.pop_back();
PII hm = get(1, 1, id);
res += x[id]*hm.ND - hm.ST;
PII hm2 = get(0, id, n);
res += hm2.ST - x[id]*hm2.ND;
add(1, id, {x[id], 1});
}
int siz = (int)zera.size();
for (int i = siz-1; i >= 0; --i)
suf[i] = suf[i+1]+zera[i];
for (int i = 0; i+1 < siz; ++i)
res -= 2*(suf[i+1]-(siz-i-1)*zera[i]);
cout<<res<<"\n";
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
java
|
//package psa.minrazdalja;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
/**
* Uvozena abstraktna funkcija za klic Paid<L, R>
*/
public static class Pair<L,R> {
private final L left;
private final R right;
public Pair(L left, R right) {
assert left != null;
assert right != null;
this.left = left;
this.right = right;
}
public L getLeft() { return left; }
public R getRight() { return right; }
@Override
public int hashCode() { return left.hashCode() ^ right.hashCode(); }
@Override
public boolean equals(Object o) {
if (!(o instanceof Pair)) return false;
Pair pairo = (Pair) o;
return this.left.equals(pairo.getLeft()) &&
this.right.equals(pairo.getRight());
}
}
/**?
* glavna funkcija racunanja:
* sprejme list tipa ArrayList<Pair<Integer, Integer>>, potem st vseh elementov in seveda listo vseh elementov.
*
* nato izracunamo maksimalno distance, ki je sestevek vseh distanc med posameznimi elementi, kar lahko storimo, ker je sortirana tabela.
* V drugem delu odstejemo od maksimalnega sestevka distanc, posamezne ki se sekajo v prihodnosti
* ostane nam samo tocni sestevek minimalnih distanc
*/
public static void elele(ArrayList<Pair<Long,Long>> elementi, int s, Long[] x_el) {
long skupek = 0L;
HashMap<Long, Integer> pos = new HashMap<Long, Integer>();
for(int i=0; i<s; ++i) {
skupek += x_el[i] * Long.valueOf(i) + (-1L * x_el[i]) * Long.valueOf(s - 1 - i);
pos.put(x_el[i], i);
}
if (s == 200000)
System.out.println(skupek);
for(int i=0; i<s; ++i){
skupek -= elementi.get(i).getRight() * (pos.get(elementi.get(i).getRight()) - i);
}
//2 1 4 3 5
//2 2 2 3 4
System.out.println(skupek);
}
public static void main(String[] args) throws IOException {
int st_el;
// uporabimo bufferedreader zaradi 1000x vecje hitrosti kot pa skener
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
//System.out.println("Vnesi stevilo elementov: ");
st_el = Integer.parseInt(br.readLine());
//System.out.println(st_el);
Long[] x_el = new Long[st_el];
Long[] v_el = new Long[st_el];
//System.out.println("Vnesi elemente: ");
String x = br.readLine();
String[] strs = x.trim().split("\\s+");
for(int i=0; i<st_el; ++i) {
x_el[i] = Long.parseLong(strs[i]);
}
//System.out.println("Vnesi tezo elementov: ");
String y = br.readLine();
String[] strs2 = y.trim().split("\\s+");
for(int i=0; i<st_el; ++i) {
v_el[i] = Long.parseLong(strs2[i]);
}
br.close();
//pospravimo skupej v par oblike <integer, integer> oz <hitrost, element>
ArrayList<Pair<Long,Long>> listt = new ArrayList<Pair<Long, Long>>(st_el);
for(int i=0; i<st_el; i++) {
Pair<Long, Long> pair = new Pair<Long, Long>(v_el[i],x_el[i]);
listt.add(pair);
}
/*sortiramo listt od najmanjsega do najvecjega
* - sortiramo prvo po hitrostih (to je glavno najbolj vazno)
* - ce je hitrost ista potem pa sortiramo se glede na element
* - O(n log n)
*/
Collections.sort(listt, new Comparator<Pair<Long, Long>>() {
@Override
public int compare(final Pair<Long, Long> o1, final Pair<Long, Long> o2) {
if(o1.getLeft()<o2.getLeft()) {
return -1;
} else if(o1.getLeft() == o2.getLeft()) {
if(o1.getRight() < o2.getRight()){
return -1;
}else{
return 1;
}
} else {
return 1;
}
}
});
//sortiramo se elemente
// O(n log n)
Arrays.sort(x_el);
//izpisemo elemente
elele(listt,st_el,x_el);
}
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long mxn = 1e6 + 7;
const long long mod = 1e9 + 7;
const long long inf = 1e18 + 7;
int dx[] = {+1, -1, 0, 0};
int dy[] = {0, 0, -1, +1};
pair<int, int> p[mxn];
map<long long, int> f, s;
int n;
int a[mxn], ls[mxn];
long long ans;
void upd1(int x, int val) {
while (x <= (int)1e8) {
f[x] += val;
x = (x | (x + 1));
}
}
void upd2(int x) {
while (x <= (int)1e8) {
s[x]++;
x = (x | (x + 1));
}
}
long long sum1(int x) {
long long res = 0;
while (x > 0) {
res += f[x];
x = (x & (x + 1)) - 1;
}
return res;
}
int sum2(int x) {
int res = 0;
while (x > 0) {
res += s[x];
x = (x & (x + 1)) - 1;
}
return res;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> p[i].first;
}
for (int i = 1; i <= n; i++) {
cin >> p[i].second;
ls[i] = p[i].second;
}
sort(p + 1, p + n + 1);
sort(ls + 1, ls + n + 1);
for (int i = 1; i <= n; i++) {
p[i].second = lower_bound(ls + 1, ls + n + 1, p[i].second) - ls;
}
for (int i = 1; i <= n; i++) {
ans += (1LL * p[i].first * sum2(p[i].second)) - sum1(p[i].second);
upd1(p[i].second, p[i].first);
upd2(p[i].second);
}
cout << ans;
return 0;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
java
|
import java.util.Arrays;
import java.util.Scanner;
import java.util.Comparator;
public class R624_F {
private static long sum (long[] t, int pos) {
long result = 0;
while (pos >= 0) {
result += t[pos];
pos = (pos & (pos + 1)) - 1;
}
return result;
}
private static void upd (long[] t, int pos, int delta) {
while (pos < t.length) {
t[pos] += delta;
pos = pos | (pos + 1);
}
}
public static void main(String[] args) {
Scanner in = new Scanner (System.in);
int n = in.nextInt();
int[][] p = new int[n][2];
int[][] v = new int[n][2];
for (int i = 0; i < n; i++)
p[i][0] = in.nextInt();
for (int i = 0; i < n; i++) {
p[i][1] = in.nextInt();
v[i][0] = p[i][1];
v[i][1] = i;
}
in.close();
Arrays.sort(v, new Comparator<int[]>() {
public int compare(int[] a, int[] b) {
return a[0] - b[0];
}
});
int idx = 0;
int[][] ret = new int[n][2];
for (int i = 0; i < n; i++) {
if (i > 0 && v[i][0] != v[i-1][0])
idx++;
ret[i][0] = idx;
ret[i][1] = v[i][1];
}
Arrays.sort(v, new Comparator<int[]>() {
public int compare(int[] a, int[] b) {
return a[1] - b[1];
}
});
for (int i = 0; i < n; i++) {
p[i][1] = ret[i][0];
}
Arrays.sort(p, new Comparator<int[]>() {
public int compare(int[] a, int[] b) {
return a[0] - b[0];
}
});
long[] xs = new long[n];
long[] cnt = new long[n];
long ans = 0;
for (int i = 0; i < n; i++) {
int pos = p[i][1];
ans += sum(cnt, pos) * p[i][0] - sum (xs, pos);
upd(cnt, pos, 1);
upd(xs, pos, p[i][0]);
}
System.out.println(ans);
}
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
java
|
import java.util.Scanner;
public class Movingpoints {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s = new Scanner(System.in);
int n=s.nextInt();
while(n<2||n>200000) {
n=s.nextInt();
}
int [] pt = new int[n];
int [] vl = new int [pt.length];
for (int i=0;i<pt.length;i++) {
int m = s.nextInt();
while(m<1||m>100000000||duplicate(pt,m)==true) {
m=s.nextInt();
}
pt[i]=m;
}
int temp1=0;
int temp2=0;
for(int j=0;j<vl.length;j++) {
int k=s.nextInt();
while(k<-100000000||k>100000000) {
k=s.nextInt();
}
vl[j]=k;
}
for (int i = 0; i < pt.length; i++)
{
for (int j = i + 1; j < pt.length; j++) {
if (pt[i] > pt[j])
{
temp1 = pt[i];
pt[i] = pt[j];
pt[j] = temp1;
temp2=vl[i];
vl[i]=vl[j];
vl[j]=temp2;
}
}
}
int sum=0;
for(int l=0;l<pt.length;l++) {
for(int x=l+1;x<pt.length;x++) {
if(vl[l]<=vl[x]) {
sum=sum+distance(pt[l],pt[x]);
}
}
}
System.out.println(sum);
}
public static boolean duplicate (int [] a , int b) {
int i =0;
boolean t=false;
while(a[i]!=0 && t==false) {
if (a[i]==b) {
t=true;
}
i++;
}
return t;
}
public static int distance(int a ,int b ) {
int d=Math.abs(b-a);
return d;
}
}
|
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": []
}
|
IN-CORRECT
|
java
|
import java.util.*;
public class MovingPoints {
static class Pair {
int first;
int second;
Pair(int first, int second) {
this.first = first;
this.second = second;
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] coords = new int[n];
int[] vels = new int[n];
for (int i = 0; i < n; ++i) {
coords[i] = scanner.nextInt();
}
for (int i = 0; i < n; ++i) {
vels[i] = scanner.nextInt();
}
Pair[] vec = new Pair[n];
for(int i = 0; i < n; i++) {
vec[i] = new Pair(coords[i], vels[i]);
}
Arrays.sort(coords);
Arrays.sort(vec, (o1, o2) -> {
if(o1.second == o2.second)
return (o2.first - o1.first);
return (o2.second - o1.second);
});
int sum = 0;
Map<Integer, Integer> map = new HashMap<>();
for(int i = 0; i < n; i++) {
sum += coords[i] * (2 * i + 1 - n);
map.put(coords[i], i);
}
for(int i = 0; i < n; i++) {
sum -= vec[i].first * (map.get(vec[i].first) - i);
}
System.out.println(sum);
}
}
|
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": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
long long f[200005], ans, bit[200005], s[200005];
map<int, int> m;
int t, n;
pair<long long, long long> a[200005];
long long get(long long first, long long second) {
int re = 0, cnt = 0;
for (int i = second; i > 0; i -= (i & (-i))) {
cnt += bit[i];
re += s[i];
}
return first * cnt - re;
}
void update(long long first, long long second) {
for (int i = second; i <= n; i += (i & (-i))) {
bit[i]++;
s[i] += first;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i].first;
for (int i = 1; i <= n; i++) cin >> a[i].second, m[a[i].second] = 0;
int k = 1;
for (map<int, int>::iterator it = m.begin(); it != m.end(); it++, k++)
it->second = k;
sort(a + 1, a + n + 1);
for (int i = 1; i <= n; i++) a[i].second = m[a[i].second];
for (int i = 1; i <= n; i++) {
ans += get(a[i].first, a[i].second);
update(a[i].first, a[i].second);
}
cout << ans << endl;
return 0;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long N = 3e5 + 5;
pair<long long, long long> p[N], a[N];
long long n;
long long fwt[N], cntf[N];
long long len;
void add(long long *b, long long x, long long v) {
while (x <= len) {
b[x] += v;
x += x & -x;
}
}
long long get(long long *b, long long x) {
long long res = 0;
while (x) {
res += b[x];
x -= x & -x;
}
return res;
}
signed main() {
ios::sync_with_stdio(false);
cin >> n;
vector<long long> velo_neg, velo_pos;
for (long long i = 0; i < n; i++) {
cin >> p[i].first;
}
for (long long i = 0; i < n; i++) {
cin >> p[i].second;
}
sort(p, p + n);
vector<long long> velo;
for (long long i = 0; i < n; i++) {
velo.push_back(p[i].second);
}
sort(velo.begin(), velo.end());
velo.erase(unique(velo.begin(), velo.end()), velo.end());
len = velo.size();
long long ans = 0;
long long sumx = 0;
for (long long i = 0; i < n; i++) {
a[i] = p[i];
a[i].second =
lower_bound(velo.begin(), velo.end(), p[i].second) - velo.begin() + 1;
ans += i * p[i].first - sumx;
sumx += p[i].first;
}
for (long long i = 0; i < n; i++) {
if (p[i].second < 0) {
ans -= (get(cntf, len) - get(cntf, a[i].second)) * p[i].first -
(get(fwt, len) - get(fwt, a[i].second));
}
add(fwt, a[i].second, p[i].first);
add(cntf, a[i].second, 1);
cout << ans << " " << get(cntf, len) - get(cntf, a[i].second) << " "
<< (get(fwt, len) - get(fwt, a[i].second)) << endl;
}
cout << endl;
memset(cntf, 0, sizeof cntf);
memset(fwt, 0, sizeof fwt);
for (long long i = n - 1; i >= 0; i--) {
if (p[i].second > 0) {
ans -=
get(fwt, a[i].second - 1) - get(cntf, a[i].second - 1) * p[i].first;
}
if (p[i].second >= 0) {
add(fwt, a[i].second, p[i].first);
add(cntf, a[i].second, 1);
}
cout << ans << " " << get(fwt, a[i].second - 1) << " "
<< get(cntf, a[i].second - 1) << endl;
}
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": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5e6 + 10, MAXM = 2e7 + 10;
const int INF = INT_MAX, SINF = 0x3f3f3f3f;
const long long llINF = LLONG_MAX;
const int MOD = 1e9 + 7, mod = 998244353;
int tr[MAXM], tot, n, t[MAXN];
void add(int x, int val) {
while (x <= n) {
tr[x] += val;
t[x] += 1;
x += x & -x;
}
}
pair<int, int> query(int x) {
int ans = 0, ans1 = 0;
while (x) {
ans += tr[x];
ans1 += t[x];
x -= x & -x;
}
return {ans, ans1};
}
struct Node {
int x, v;
bool operator<(const Node &rhs) const { return x < rhs.x; }
} e[MAXN];
vector<int> v;
map<int, int> mp;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &e[i].x);
for (int i = 1; i <= n; i++) scanf("%d", &e[i].v), v.push_back(e[i].v);
sort(e + 1, e + n + 1);
sort(v.begin(), v.end());
v.resize(unique(v.begin(), v.end()) - v.begin());
for (int i = 0; i < ((int)v.size()); i++) mp[v[i]] = i + 1;
long long ans = 0;
for (int i = 1; i <= n; i++) {
pair<int, int> tmp = query(mp[e[i].v]);
ans += e[i].x * tmp.second - tmp.first;
add(mp[e[i].v], e[i].x);
}
printf("%lld", ans);
return 0;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
python3
|
import sys,math,itertools
from collections import Counter,deque,defaultdict
from bisect import bisect_left,bisect_right
from heapq import heappop,heappush,heapify, nlargest
from copy import deepcopy
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_1(): return list(map(lambda x:int(x)-1, sys.stdin.readline().split()))
def inps(): return sys.stdin.readline()
def inpsl(x): tmp = sys.stdin.readline(); return list(tmp[:x])
def err(x): print(x); exit()
class BIT:
def __init__(self, n):
self.n = n
self.data = [0]*(n+1)
self.el = [0]*(n+1)
def sum(self, i):
s = 0
while i > 0:
s += self.data[i]
i -= i & -i
return s
def add(self, i, x):
# assert i > 0
self.el[i] += x
while i <= self.n:
self.data[i] += x
i += i & -i
def get(self, i, j=None):
if j is None:
return self.el[i]
return self.sum(j) - self.sum(i)
# 1_indexed
# n = 6
# a = [1,2,3,4,5,6]
# bit = BIT(n)
# for i,e in enumerate(a):
# bit.add(i+1,e)
# print(bit.get(2,5)) #12 (3+4+5)
n = inp()
X = inpl()
V = inpl()
d = {}
for i,x in enumerate(sorted(X)):
d[x] = i
for i,x in enumerate(X):
X[i] = d[x]
xv = [(x,v) for x,v in zip(X,V)]
xv.sort(key=lambda x:x[0] ,reverse=True)
xv.sort(key=lambda x:x[1], reverse=True)
cnt_bit = BIT(n+10)
sum_bit = BIT(n+10)
res = 0
for x,_ in xv:
res += sum_bit.get(x,n+5) - cnt_bit.get(x,n+5)*x
sum_bit.add(x+1,x)
cnt_bit.add(x+1,1)
print(res)
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const long long inf = 1LL << 62;
const long long mx = 200005;
const double eps = 1e-10;
long long dx[10] = {1, 0, -1, 0};
long long dy[10] = {0, -1, 0, 1};
long long power(long long a, long long b) {
if (b == 0) return 1;
long long x = power(a, b / 2);
x = x * x;
if (b % 2) x = x * a;
return x;
}
long long Set(long long N, long long pos) { return N = N | (1LL << pos); }
long long reset(long long N, long long pos) { return N = N & ~(1LL << pos); }
bool check(long long N, long long pos) { return (bool)(N & (1LL << pos)); }
pair<long long, long long> prr[mx];
long long arr[mx];
long long brr[mx];
unordered_map<long long, long long> mm;
unordered_map<long long, long long> nn;
long long tree[4 * mx];
long long tt[4 * mx];
void update(long long at, long long L, long long R, long long id,
long long val) {
if (R < id || L > id) return;
if (L == R && id == R) {
tree[at] += val;
return;
}
long long mid = (L + R) / 2;
update(2 * at, L, mid, id, val);
update(2 * at + 1, mid + 1, R, id, val);
tree[at] = tree[2 * at] + tree[2 * at + 1];
}
long long query(long long at, long long L, long long R, long long l,
long long r) {
if (l > R || r < L) return 0;
if (l <= L && r >= R) return tree[at];
long long mid = (L + R) / 2;
long long a = query(2 * at, L, mid, l, r);
long long b = query(2 * at + 1, mid + 1, R, l, r);
return a + b;
}
void update2(long long at, long long L, long long R, long long id,
long long val) {
if (R < id || L > id) return;
if (L == R && id == R) {
tt[at] += val;
return;
}
long long mid = (L + R) / 2;
update2(2 * at, L, mid, id, val);
update2(2 * at + 1, mid + 1, R, id, val);
tt[at] = tt[2 * at] + tt[2 * at + 1];
}
long long query2(long long at, long long L, long long R, long long l,
long long r) {
if (l > R || r < L) return 0;
if (l <= L && r >= R) return tt[at];
long long mid = (L + R) / 2;
long long a = query2(2 * at, L, mid, l, r);
long long b = query2(2 * at + 1, mid + 1, R, l, r);
return a + b;
}
bool comp(pair<long long, long long> a, pair<long long, long long> b) {
return a.first < b.first;
}
int main() {
long long tst, a, b, c, k, n, m, res = 0, ans = 0, t = 0;
set<long long> s;
set<long long> ss;
scanf("%lld", &n);
for (long long i = 1; i <= n; i++) {
scanf("%lld", &arr[i]);
s.insert(arr[i]);
}
for (long long i = 1; i <= n; i++) {
scanf("%lld", &brr[i]);
ss.insert(brr[i]);
}
for (auto i : s) {
mm[i] = ++t;
}
t = 0;
for (auto i : ss) {
nn[i] = ++t;
}
for (long long i = 1; i <= n; i++) {
a = arr[i], b = brr[i];
a = mm[a], b = nn[b];
prr[i] = {a, b};
}
sort(prr + 1, prr + 1 + n, comp);
for (long long i = 1; i <= n; i++) {
a = query(1, 1, n, 1, prr[i].second);
b = query2(1, 1, n, 1, prr[i].second);
res += (prr[i].first * a - b);
update(1, 1, n, prr[i].second, 1);
update2(1, 1, n, prr[i].second, prr[i].first);
}
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": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define FI first
#define SE second
#define pb push_back
#define eb emplace_back
#define mod 1000000007
#define all(c) (c).begin(),(c).end()
#define LB lower_bound
#define UB upper_bound
#define max3(a,b,c) max(c,max(a,b))
#define min3(a,b,c) min(c,min(a,b))
#define mems(s, n) memset(s, n, sizeof(s))
#define NINF INT_MIN
#define INF INT_MAX
#define int ll int
#define OOK order_of_key //no of elements less than
#define FBO find_by_order //iterator pointing kth element;indexing starts from 0
#define CK3(x,y,z) cout<<(x)<<" "<<(y)<<" "<<(z)<<endl
#define CK4(a,b,c,d) cout<<(a)<<" "<<(b)<<" "<<(c)<<" "<<(d)<<endl
typedef pair<int,int> PII;
typedef pair<pair<int,int>,int> PPII;
typedef map<int,PII>MII;
typedef vector<int> VI;
template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
/*int power(int a, int b)
{int x=1,y=a;while(b>0){if(b%2){x =(x*y)%mod;}y =(y*y)%mod;b/=2;}return x%mod;}
*/
//int modInverse(int a, int m) {return power(a, m-2);} //gcd(a,m)=1
//int root(int x){return x==f[x]?x:f[x]=root(f[x]);}
MII t;
/*void build( int v, int tl, int tr) {
if (tl == tr) {
t[v] = a[tl];
} else {
int tm = (tl + tr) / 2;
build(a, v*2, tl, tm);
build(a, v*2+1, tm+1, tr);
t[v] = t[v*2] + t[v*2+1];
}
}
int sum(int v, int tl, int tr, int l, int r) {
if (l > r)
return 0;
if (l == tl && r == tr) {
return t[v];
}
int tm = (tl + tr) / 2;
return sum(v*2, tl, tm, l, min(r, tm))
+ sum(v*2+1, tm+1, tr, max(l, tm+1), r);
}
*/
void update(int v, int tl, int tr, int l, int r, int add,int r1) {
if (l > r)
return;
if (l == tl && r == tr) {
t[v].FI += add,t[v].SE+=r1;
} else {
int tm = (tl + tr) / 2;
update(v*2, tl, tm, l, min(r, tm), add,(int)r1);
update(v*2+1, tm+1, tr, max(l, tm+1), r, add,(int)r1);
}
}
PII get(int v, int tl, int tr, int pos) {
if (tl == tr)
return t[v];
int tm = (tl + tr) / 2;
if (pos <= tm)
{PII hp=get(v*2, tl, tm, pos);return {t[v].FI + hp.FI,t[v].SE +hp.SE};}
else
{PII hp=get(v*2+1, tm+1, tr, pos); return {t[v].FI + hp.FI,t[v].SE + hp.SE};}
}
signed main()
{
ios::sync_with_stdio(false); cin.tie(0);
int T=1,T1=0;//cin>>T;
while(T1++<T)
{
int n;
cin>>n;int ans=0;vector<PII>ar(n);
for(int i=0;i<n;i++)
{
cin>>ar[i].FI;
}
for(int i=0;i<n;i++)
{
cin>>ar[i].SE;ar[i].SE+=100000000;
update(1,0,200000005,ar[i].SE,200000005,ar[i].FI,(int)1);
}
sort(all(ar));
int sum1=0;
for(int i=0;i<n;i++)
{ sum1+=ar[i].FI;
PII x=get(1,0,200000003,ar[i].SE-1);
//cout<<x.FI<<" "<<x.SE<<" ";
x.FI=(n*(n+1))/2-sum1-x.FI;x.SE=n-i-1-x.SE;
//cout<<x.FI-ar[i].FI*x.SE<<endl;
ans+=max((int)0,x.FI-ar[i].FI*x.SE);
update(1,0,200000005,ar[i].SE,200000005,-ar[i].FI,(int)-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": []
}
|
IN-CORRECT
|
java
|
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.util.Comparator;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author xwchen
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskF solver = new TaskF();
solver.solve(1, in, out);
out.close();
}
static class TaskF {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
int[] x = in.nextIntArray(n);
int[] v = in.nextIntArray(n);
v = ArrayUtils.shrink(v);
Point[] points = new Point[n];
for (int i = 0; i < n; ++i) {
points[i] = new Point(x[i], v[i]);
}
Arrays.sort(points, Comparator.comparingInt(p -> p.x));
BinaryIndexedTree Vcnt = new BinaryIndexedTree(n);
BinaryIndexedTree VXsum = new BinaryIndexedTree(n);
long res = 0;
for (int i = n - 1; i >= 0; --i) {
int X = points[i].x;
int V = points[i].v;
long cnt = Vcnt.queryRange(V, n);
long sum = VXsum.queryRange(V, n);
res += sum - X * cnt;
Vcnt.update(V, 1);
VXsum.update(V, X);
}
out.println(res);
}
class Point {
int x;
int v;
public Point(int x, int v) {
this.x = x;
this.v = v;
}
}
}
static class BinaryIndexedTree {
int n;
long[] bit;
public BinaryIndexedTree(int n) {
this.n = n;
bit = new long[n + 1];
}
public void update(int i, int add) {
while (i > 0 && i <= n) {
bit[i] += add;
i = i + (i & (-i));
}
}
public long sum(int i) {
long ans = 0;
while (i > 0) {
ans += bit[i];
i = i - (i & (-i));
}
return ans;
}
public long queryRange(int i, int j) {
return sum(j) - sum(i - 1);
}
}
static class ArrayUtils {
public static int[] shrink(int[] a) {
int n = a.length;
int[] ret = new int[n];
Integer[] b = new Integer[n];
for (int i = 0; i < n; ++i) {
b[i] = i;
}
Arrays.sort(b, Comparator.comparingInt(x -> a[x]));
int p = 1;
for (int i = 0; i < n; ++i) {
if (i - 1 >= 0 && a[b[i]] == a[b[i - 1]]) {
ret[b[i]] = p;
} else {
ret[b[i]] = p++;
}
}
return ret;
}
}
static class InputReader {
private BufferedReader reader;
private StringTokenizer tokenizer = new StringTokenizer("");
public InputReader(InputStream inputStream) {
this.reader = new BufferedReader(
new InputStreamReader(inputStream));
}
public String next() {
while (!tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public int[] nextIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
}
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<int, int>;
constexpr int MAXN = 5 + 200000;
constexpr int MINV = 1 + 100000000;
struct Bit {
ll b[MAXN];
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;
}
ll query(int l, int r) {
if (l > r) return 0;
return query(r) - query(l - 1);
}
};
ii a[MAXN];
int m[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, pos = 0LL, pre = 0LL;
for (int i = (int)0; i < (int)n; ++i) pos += a[i].first;
for (int i = (int)0; i < (int)n; ++i) {
pos -= a[i].first;
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(1, a[i].second - 1) -
1LL * cuenta.query(1, 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": []
}
|
IN-CORRECT
|
java
|
import java.io.*;
import java.util.*;
public class Main {
static class BIT {
int n;
int[] tree;
public BIT(int n) {
this.n = n;
tree = new int[n + 2];
}
long read(int i) {
i++;
long sum = 0;
while (i > 0) {
sum += tree[i];
i -= i & -i;
}
return sum;
}
void update(int i, int val) {
i++;
while (i <= n) {
tree[i] += val;
i += i & -i;
}
}
}
public static void main(String[] args) throws IOException
{
FastScanner f = new FastScanner();
int t=1;
// t=f.nextInt();
PrintWriter out=new PrintWriter(System.out);
for(int tt=0;tt<t;tt++) {
int n= f.nextInt();
int[] x=f.readArray(n);
int[] v=f.readArray(n);
TreeSet<Integer> tree=new TreeSet<>();
for(int i:v) tree.add(i);
HashMap<Integer,Integer> map=new HashMap<>();
int curr=0;
for(int i:tree) {
curr++;
map.put(i, curr);
}
for(int i=0;i<n;i++) {
v[i]=map.get(v[i]);
}
BIT val=new BIT(100005);
BIT count=new BIT(100005);
long ans=0;
int[][] l=new int[n][2];
for(int i=0;i<n;i++) {
l[i]=new int[] {x[i],v[i]};
}
Arrays.sort(l,new Comparator<int[]>() {
public int compare(int[] a,int[] b) {
return Integer.compare(a[0], b[0]);
}
});
for(int i=n-1;i>-1;i--) {
long num=count.read(n)-count.read(l[i][1]-1);
ans+=(-(long)l[i][0]*num+val.read(n)-val.read(l[i][1]-1));
count.update(l[i][1],1);
val.update(l[i][1],l[i][0]);
}
System.out.println(ans);
}
out.close();
}
static void sort(long[] p) {
ArrayList<Integer> q = new ArrayList<>();
for (long i: p) q.add((int) i);
Collections.sort(q);
for (int i = 0; i < p.length; i++) p[i] = q.get(i);
}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
long[] readLongArray(int n) {
long[] a=new long[n];
for (int i=0; i<n; i++) a[i]=nextLong();
return a;
}
}
}
//Some things to notice
//Check for the overflow
//Binary Search
//Bitmask
//runtime error most of the time is due to array index out of range
|
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": []
}
|
IN-CORRECT
|
python3
|
numberofpoints=int(input())
initcoords=input().split(" ")
speeds=input().split(" ")
initcoords=tuple(map(int,initcoords))
speeds=tuple(map(int,speeds))
def twopoints(coords,velos):
firstv=velos[0]
secondv=velos[1]
firstc=coords[0]
secondc=coords[1]
dif=abs(firstv-secondv)
nextdif=abs((firstc+firstv)-(secondc+secondc))
if dif>nextdif:
return 0
else:
return dif
lis=[]
for first in range(numberofpoints):
for second in range(first+1,numberofpoints):
tup=(first,second)
lis.append(tup)
lis=tuple(lis)
sum=0
for tuple in lis:
a=(speeds[tuple[0]],speeds[tuple[1]])
b=(initcoords[tuple[0]],initcoords[tuple[1]])
sum+=twopoints(b,a)
print(sum)
|
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": []
}
|
IN-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": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long maxn = 1e6 + 7;
const long long top = 1e6 + 3;
struct node {
long long xx, v;
};
long long a[maxn], b[maxn];
long long num[maxn], x[maxn];
vector<long long> v;
node t[maxn];
long long lowerbit(long long x) { return x & (-x); }
long long gets1(long long p) {
long long ans = 0;
for (long long i = p; i > 0; i -= lowerbit(i)) ans += x[i];
return ans;
}
long long gets2(long long p) {
long long ans = 0;
for (long long i = p; i > 0; i -= lowerbit(i)) ans += num[i];
return ans;
}
void update(long long* f, long long p, long long k) {
for (long long i = p; i < top; i += lowerbit(i)) f[i] += k;
}
bool cmp(const node& a, const node& b) {
if (a.v != b.v)
return a.v < b.v;
else
return a.xx < b.xx;
}
long long getnum(long long x) {
return lower_bound(v.begin(), v.end(), x) - v.begin() + 1;
}
signed main() {
long long n;
scanf("%lld", &n);
for (long long i = 1; i <= n; i++)
scanf("%lld", &t[i].xx), v.push_back(t[i].xx);
for (long long i = 1; i <= n; i++) scanf("%lld", &t[i].v);
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
for (long long i = 1; i <= n; i++) t[i].xx = getnum(t[i].xx);
sort(t + 1, t + n + 1, cmp);
long long ans = 0;
for (long long i = 1; i <= n; i++) {
update(num, t[i].xx, 1);
update(x, t[i].xx, t[i].xx);
if (i == 1)
continue;
else {
long long _n = gets2(t[i].xx);
long long res = gets1(t[i].xx);
ans += _n * t[i].xx - res;
}
}
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": []
}
|
IN-CORRECT
|
java
|
import java.util.Scanner;
public class a {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
long[] pos=new long[n];
long[] vel=new long[n];
for(int i=0;i<n;i++){
pos[i]=sc.nextLong();
}
for(int i=0;i<n;i++){
vel[i]=sc.nextLong();
}
long sum=0;
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
if(vel[i]==0 && vel[j]==0){
sum=sum+Math.abs(pos[i]-pos[j]);
}
else if(vel[i]==0 ){
if(pos[i]<pos[j] && vel[j]>0){
sum=sum+Math.abs(pos[i]-pos[j]);
}
if(pos[i]>pos[j] && vel[j]<0){
sum=sum+Math.abs(pos[i]-pos[j]);
}
}
else if(vel[j]==0){
if(pos[j]<pos[i] && vel[i]>0){
sum=sum+Math.abs(pos[i]-pos[j]);
}
if(pos[j]>pos[i] && vel[i]<0){
sum=sum+Math.abs(pos[i]-pos[j]);
}
}
else if(vel[i]>0 && vel[j]>0){
if( pos[j]>pos[i] &&vel[j]>=vel[i]){
sum=sum+Math.abs(pos[i]-pos[j]);
}
if( pos[j]<pos[i] &&vel[j]<=vel[i]){
sum=sum+Math.abs(pos[i]-pos[j]);
}
}
else if(vel[i]<0 && vel[j]<0){
if( vel[i]>=vel[j] && pos[i]<pos[j]){
sum=sum+Math.abs(pos[i]-pos[j]);
}
if( vel[i]<=vel[j] && pos[i]>pos[j]){
sum=sum+Math.abs(pos[i]-pos[j]);
}
}
else if(vel[i]<0 && vel[j]>0 ){
if(pos[i]<pos[j]){
sum=sum+Math.abs(pos[i]-pos[j]);
}
}
else if(vel[i]>0 && vel[j]<0 ){
if(pos[i]>pos[j]){
sum=sum+Math.abs(pos[i]-pos[j]);
}
}
}
}
System.out.println(sum);
}
}
|
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": []
}
|
IN-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) {
if (a[i].second > 0) continue;
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);
}
build(1, 1, len);
for (int i = n; i >= 1; --i) {
if (a[i].second < 0) continue;
int id = lower_bound(b + 1, b + len + 1, a[i].second) - b;
pair<long long, long long> cur = query(1, 1, n, id, n);
ans += cur.first - cur.second * a[i].first;
modify(1, 1, n, id, a[i].first);
}
long long sum = 0, cnt = 0;
for (auto i : a) {
if (i.second < 0) {
sum += i.first;
cnt++;
} else if (i.second > 0) {
ans -= sum - cnt * 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": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long int arr[200000];
long long int v[200000];
int N;
cin >> N;
for (int i = 0; i < N; i++) cin >> arr[i];
for (int i = 0; i < N; i++) cin >> v[i];
long long int answer = 0;
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N; j++) {
long long int dist = arr[j] - arr[i];
long long int speed = v[j] - v[i];
if (dist == 0 || dist * speed < 0) continue;
if (speed == 0) {
answer += abs(dist);
continue;
}
if (dist < 0 && arr[i] < arr[j]) {
answer += abs(dist);
continue;
}
if (dist > 0 && arr[i] < arr[j]) {
answer += abs(dist);
continue;
}
}
}
cout << answer << '\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": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int N, k = 1;
vector<pair<long long, int>> v;
map<int, int> m;
long long fenwick[200050];
int f[200050];
void update(int pos, long long val) {
for (; pos < 200050; pos += pos & -pos) fenwick[pos] += val;
}
void u(int pos, int v) {
for (; pos < 200050; pos += pos & -pos) f[pos] += v;
}
int query(int pos) {
long long res = 0;
for (; pos > 0; pos -= pos & -pos) res += fenwick[pos];
return res;
}
int q(int pos) {
int res = 0;
for (; pos > 0; pos -= pos & -pos) res += f[pos];
return res;
}
int main() {
cin >> N;
v.resize(N);
set<int> s;
for (int i = 0; i < N; i++) scanf("%lld", &v[i].first);
for (int i = 0; i < N; i++) {
scanf("%d", &v[i].second);
s.insert(v[i].second);
}
sort(v.begin(), v.end());
reverse(v.begin(), v.end());
for (auto x : s) m[x] = k++;
for (int i = 0; i < N; i++) v[i].second = m[v[i].second];
long long re = 0;
for (auto x : v) update(x.second, x.first);
for (auto x : v) u(x.second, 1);
for (auto x : v) {
re -= query(x.second) - x.first * (long long)q(x.second);
update(x.second, -x.first);
u(x.second, -1);
}
cout << re << "\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": []
}
|
IN-CORRECT
|
java
|
// Don't place your source in a package
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;
/*
6666666666666 6666666666666
6 6
6 6
6 6
6 6
6 6
6666666666666 6666666666666
6 6 6 6
6 6 6 6
6 6 6 6
6 6 6 6
6666666666666 6666666666666
*/
// Please name your class Main
public class Main {
//static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
/*static int read() throws IOException {
in.nextToken();
return (int) in.nval;
}
static String readString() throws IOException {
in.nextToken();
return in.sval;
}*/
static Scanner in = new Scanner(System.in);
public static void main (String[] args) throws java.lang.Exception {
//InputReader in = new InputReader(System.in);
PrintWriter out = new PrintWriter(System.out);
int T=1;
for(int t=0;t<T;t++){
int n=Int();
int A[][]=new int[n][2];
for(int i=0;i<n;i++){
A[i][0]=Int();
}
for(int i=0;i<n;i++){
A[i][1]=Int();
}
Solution sol=new Solution();
sol.solution(A);
}
out.flush();
}
public static int Int(){
return in.nextInt();
}
public static String Str(){
return in.next();
}
}
class Solution{
//constant variable
final int MAX=Integer.MAX_VALUE;
final int MIN=Integer.MIN_VALUE;
//Set<Integer>adjecent[];
//////////////////////////////
public void solution(int A[][]){
Arrays.sort(A,(a,b)->{
if(a[1]==b[1])return a[0]-b[0];
return a[1]-b[1];
});//sort by sppeed
Seg seg=new Seg(0,100000000);
long res=0;
for(int i=0;i<A.length;i++){
int x=A[i][0];
int v=A[i][1];
int pair[]=seg.query(0,x-1);
//msg(x+" "+v+" :"+pair[0]+" "+pair[1]);
res+=(x*pair[1]-pair[0]);
seg.update(x,x);
//previous always smaller than this one, distance shoulde be n*x-sum
}
msg(res+"");
}
public void swap(int A[],int l,int r){
int t=A[l];
A[l]=A[r];
A[r]=t;
}
public long C(long fact[],int i,int j){ // C(20,3)=20!/(17!*3!)
// take a/b where a=20! b=17!*3!
if(j>i)return 0;
if(j==0)return 1;
long mod=1000000007;
long a=fact[i];
long b=((fact[i-j]%mod)*(fact[j]%mod))%mod;
BigInteger B= BigInteger.valueOf(b);
long binverse=B.modInverse(BigInteger.valueOf(mod)).longValue();
return ((a)*(binverse%mod))%mod;
}
//map operation
public void put(Map<Integer,Integer>map,int i){
if(!map.containsKey(i))map.put(i,0);
map.put(i,map.get(i)+1);
}
public void delete(Map<Integer,Integer>map,int i){
map.put(i,map.get(i)-1);
if(map.get(i)==0)map.remove(i);
}
/*public void tarjan(int p,int r){
if(cut)return;
List<Integer>childs=adjecent[r];
dis[r]=low[r]=time;
time++;
//core for tarjan
int son=0;
for(int c:childs){
if(ban==c||c==p)continue;
if(dis[c]==-1){
son++;
tarjan(r,c);
low[r]=Math.min(low[r],low[c]);
if((r==root&&son>1)||(low[c]>=dis[r]&&r!=root)){
cut=true;
return;
}
}else{
if(c!=p){
low[r]=Math.min(low[r],dis[c]);
}
}
}
}*/
//helper function I would use
public void remove(Map<Integer,Integer>map,int i){
map.put(i,map.get(i)-1);
if(map.get(i)==0)map.remove(i);
}
public void ascii(String s){
for(char c:s.toCharArray()){
System.out.print((c-'a')+" ");
}
msg("");
}
public int flip(int i){
if(i==0)return 1;
else return 0;
}
public boolean[] primes(int n){
boolean A[]=new boolean[n+1];
for(int i=2;i<=n;i++){
if(A[i]==false){
for(int j=i+i;j<=n;j+=i){
A[j]=true;
}
}
}
return A;
}
public void msg(String s){
System.out.println(s);
}
public void msg1(String s){
System.out.print(s);
}
public int[] kmpPre(String p){
int pre[]=new int[p.length()];
int l=0,r=1;
while(r<p.length()){
if(p.charAt(l)==p.charAt(r)){
pre[r]=l+1;
l++;r++;
}else{
if(l==0)r++;
else l=pre[l-1];
}
}
return pre;
}
public boolean isP(String s){
int l=0,r=s.length()-1;
while(l<r){
if(s.charAt(l)!=s.charAt(r))return false;
l++;r--;
}
return true;
}
public int find(int nums[],int x){//union find => find method
if(nums[x]==x)return x;
int root=find(nums,nums[x]);
nums[x]=root;
return root;
}
public int get(int A[],int i){
if(i<0||i>=A.length)return 0;
return A[i];
}
public int[] copy1(int A[]){
int a[]=new int[A.length];
for(int i=0;i<a.length;i++)a[i]=A[i];
return a;
}
public void print1(int A[]){
for(long i:A)System.out.print(i+" ");
System.out.println();
}
public void print2(int A[][]){
for(int i=0;i<A.length;i++){
for(int j=0;j<A[0].length;j++){
System.out.print(A[i][j]+" ");
}System.out.println();
}
}
public int min(int a,int b){
return Math.min(a,b);
}
public int[][] matrixdp(int[][] grid) {
if(grid.length==0)return new int[][]{};
int res[][]=new int[grid.length][grid[0].length];
for(int i=0;i<grid.length;i++){
for(int j=0;j<grid[0].length;j++){
res[i][j]=grid[i][j]+get(res,i-1,j)+get(res,i,j-1)-get(res,i-1,j-1);
}
}
return res;
}
public int get(int grid[][],int i,int j){
if(i<0||j<0||i>=grid.length||j>=grid[0].length)return 0;
return grid[i][j];
}
public int[] suffixArray(String s){
int n=s.length();
Suffix A[]=new Suffix[n];
for(int i=0;i<n;i++){
A[i]=new Suffix(i,s.charAt(i)-'a',0);
}
for(int i=0;i<n;i++){
if(i==n-1){
A[i].next=-1;
}else{
A[i].next=A[i+1].rank;
}
}
Arrays.sort(A);
for(int len=4;len<A.length*2;len<<=1){
int in[]=new int[A.length];
int rank=0;
int pre=A[0].rank;
A[0].rank=rank;
in[A[0].index]=0;
for(int i=1;i<A.length;i++){//rank for the first two letter
if(A[i].rank==pre&&A[i].next==A[i-1].next){
pre=A[i].rank;
A[i].rank=rank;
}else{
pre=A[i].rank;
A[i].rank=++rank;
}
in[A[i].index]=i;
}
for(int i=0;i<A.length;i++){
int next=A[i].index+len/2;
if(next>=A.length){
A[i].next=-1;
}else{
A[i].next=A[in[next]].rank;
}
}
Arrays.sort(A);
}
int su[]=new int[A.length];
for(int i=0;i<su.length;i++){
su[i]=A[i].index;
}
return su;
}
}
//suffix array Struct
class Suffix implements Comparable<Suffix>{
int index;
int rank;
int next;
public Suffix(int i,int rank,int next){
this.index=i;
this.rank=rank;
this.next=next;
}
@Override
public int compareTo(Suffix other) {
if(this.rank==other.rank){
return this.next-other.next;
}
return this.rank-other.rank;
}
public String toString(){
return this.index+" "+this.rank+" "+this.next+" ";
}
}
class Wrapper implements Comparable<Wrapper>{
int spf;int cnt;
public Wrapper(int spf,int cnt){
this.spf=spf;
this.cnt=cnt;
}
@Override
public int compareTo(Wrapper other) {
return this.spf-other.spf;
}
}
class Node{//what the range would be for that particular node
boolean state=false;
int l=0,r=0;
int ll=0,rr=0;
public Node(boolean state){
this.state=state;
}
}
class Seg1{
int A[];
public Seg1(int A[]){
this.A=A;
}
public void update(int left,int right,int val,int s,int e,int id){
if(left<0||right<0||left>right)return;
if(left==s&&right==e){
A[id]+=val;
return;
}
int mid=s+(e-s)/2; //[s,mid] [mid+1,e]
if(left>=mid+1){
update(left,right,val,mid+1,e,id*2+2);
}else if(right<=mid){
update(left,right,val,s,mid,id*2+1);
}else{
update(left,mid,val,s,mid,id*2+1);
update(mid+1,right,val,mid+1,e,id*2+2);
}
}
public int query(int i,int add,int s,int e,int id){
if(s==e&&i==s){
return A[id]+add;
}
int mid=s+(e-s)/2; //[s,mid] [mid+1,e]
if(i>=mid+1){
return query(i,A[id]+add,mid+1,e,id*2+2);
}else{
return query(i,A[id]+add,s,mid,id*2+1);
}
}
}
class MaxFlow{
public static List<Edge>[] createGraph(int nodes) {
List<Edge>[] graph = new List[nodes];
for (int i = 0; i < nodes; i++)
graph[i] = new ArrayList<>();
return graph;
}
public static void addEdge(List<Edge>[] graph, int s, int t, int cap) {
graph[s].add(new Edge(t, graph[t].size(), cap));
graph[t].add(new Edge(s, graph[s].size() - 1, 0));
}
static boolean dinicBfs(List<Edge>[] graph, int src, int dest, int[] dist) {
Arrays.fill(dist, -1);
dist[src] = 0;
int[] Q = new int[graph.length];
int sizeQ = 0;
Q[sizeQ++] = src;
for (int i = 0; i < sizeQ; i++) {
int u = Q[i];
for (Edge e : graph[u]) {
if (dist[e.t] < 0 && e.f < e.cap) {
dist[e.t] = dist[u] + 1;
Q[sizeQ++] = e.t;
}
}
}
return dist[dest] >= 0;
}
static int dinicDfs(List<Edge>[] graph, int[] ptr, int[] dist, int dest, int u, int f) {
if (u == dest)
return f;
for (; ptr[u] < graph[u].size(); ++ptr[u]) {
Edge e = graph[u].get(ptr[u]);
if (dist[e.t] == dist[u] + 1 && e.f < e.cap) {
int df = dinicDfs(graph, ptr, dist, dest, e.t, Math.min(f, e.cap - e.f));
if (df > 0) {
e.f += df;
graph[e.t].get(e.rev).f -= df;
return df;
}
}
}
return 0;
}
public static int maxFlow(List<Edge>[] graph, int src, int dest) {
int flow = 0;
int[] dist = new int[graph.length];
while (dinicBfs(graph, src, dest, dist)) {
int[] ptr = new int[graph.length];
while (true) {
int df = dinicDfs(graph, ptr, dist, dest, src, Integer.MAX_VALUE);
if (df == 0)
break;
flow += df;
}
}
return flow;
}
}
class Edge {
int t, rev, cap, f;
public Edge(int t, int rev, int cap) {
this.t = t;
this.rev = rev;
this.cap = cap;
}
}
class Seg{
int l,r;
int min=Integer.MAX_VALUE;
Seg left=null,right=null;
int sum=0;int cnt=0;
public Seg(int l,int r){
this.l=l;
this.r=r;
}
public int[] query(int s,int e){
if(l==s&&r==e){
return new int[]{sum,cnt};
}
int mid=l+(r-l)/2; //left : to mid-1,
if(left==null)left=new Seg(l,mid);
if(right==null)right=new Seg(mid+1,r);
if(e<=mid){
return left.query(s,e);
}
else if(s>=mid+1){
return right.query(s,e);
}else{
int A[]=left.query(s,mid);
int B[]=right.query(mid+1,e);
return new int[]{A[0]+B[0],A[1]+B[1]};
}
}
public void update(int index,int v){
sum+=v;
cnt++;
if(l==r&&l==index){
return;
}
int mid=l+(r-l)/2;
if(index<=mid){
if(left==null)left=new Seg(l,mid);
left.update(index,v);
}else{
if(right==null)right=new Seg(mid+1,r);
right.update(index,v);
}
}
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
python3
|
numberofpoints=int(input())
initcoords=input().split(" ")
speeds=input().split(" ")
initcoords=tuple(map(int,initcoords))
speeds=tuple(map(int,speeds))
def twopoints(coords,velos):
firstv=velos[0]
secondv=velos[1]
firstc=coords[0]
secondc=coords[1]
dif=abs(firstc-secondc)
nextdif=abs((firstc+(10**-9)*firstv)-(secondc+(10**-9)*secondv))
if dif>nextdif:
return 0
else:
return dif
lis=[]
for first in range(numberofpoints):
for second in range(first+1,numberofpoints):
tup=(first,second)
lis.append(tup)
sum=0
for tuple in lis:
a=(speeds[tuple[0]],speeds[tuple[1]])
b=(initcoords[tuple[0]],initcoords[tuple[1]])
sum+=twopoints(b,a)
print(sum)
|
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": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
if (N > 1000) {
cout << "Number of contest cannot exceed 1000!";
exit;
}
int arr[N];
for (int i = 0; i < N; i++) {
cin >> arr[i];
if (arr[i] > 10000) {
cout << "Point cannot exceed 10000!";
exit;
}
}
int min, max, ans;
min = arr[0];
max = arr[0];
for (int i = 1; i < N; i++) {
if (arr[i] < min) {
min = arr[i];
ans++;
} else if (arr[i] > max) {
max = arr[i];
ans++;
}
}
ans = ans - 2;
cout << ans << endl;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
java
|
import java.util.*;
import java.io.*;
import java.text.*;
public class F1311 {
public static long solve(ArrayList<Pair> arr) {
FenwickTree cnt = new FenwickTree(arr.size());
FenwickTree val = new FenwickTree(arr.size());
TreeSet<Integer> ts = new TreeSet<Integer>();
for (Pair x : arr)
ts.add(x.v);
TreeMap<Integer, Integer> tm = new TreeMap<Integer, Integer>();
while (!ts.isEmpty()) {
tm.put(ts.pollFirst(), tm.size() + 1);
}
// System.out.println(tm);
Collections.sort(arr, (a, b) -> a.x - b.x);
long sum = 0;
for (Pair p : arr) {
int lim = tm.get(p.v);
if (lim > 0) {
int cntt = cnt.rsq(lim);
int value = val.rsq(lim);
sum += p.x * cntt - value;
}
cnt.point_update(lim, 1);
val.point_update(lim, p.x);
// System.out.println(sum);
}
return sum;
}
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int n = sc.nextInt();
int[] x = sc.nextIntArr(n);
int[] v = sc.nextIntArr(n);
ArrayList<Pair> r = new ArrayList<Pair>();
for (int i = 0; i < n; i++) {
r.add(new Pair(x[i], v[i]));
}
pw.println(solve(r));
pw.close();
}
static class FenwickTree { // one-based DS
int n;
int[] ft;
FenwickTree(int size) {
n = size;
ft = new int[n + 1];
}
int rsq(int b) // O(log n)
{
int sum = 0;
while (b > 0) {
sum += ft[b];
b -= b & -b;
} // min?
return sum;
}
int rsq(int a, int b) {
return rsq(b) - rsq(a - 1);
}
void point_update(int k, int val) // O(log n), update = increment
{
while (k <= n) {
ft[k] += val;
k += k & -k;
} // min?
}
int point_query(int idx) // c * O(log n), c < 1
{
int sum = ft[idx];
if (idx > 0) {
int z = idx ^ (idx & -idx);
--idx;
while (idx != z) {
sum -= ft[idx];
idx ^= idx & -idx;
}
}
return sum;
}
}
static class Pair {
int x, v;
public Pair(int a, int b) {
x = a;
v = b;
}
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(FileReader r) {
br = new BufferedReader(r);
}
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 {
return Double.parseDouble(next());
}
public int[] nextIntArr(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < arr.length; i++)
arr[i] = nextInt();
return arr;
}
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": []
}
|
IN-CORRECT
|
java
|
//package psa.minrazdalja;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class minrazdalja {
/**
* Uvozena abstraktna funkcija za klic Paid<L, R>
*/
public static class Pair<L,R> {
private final L left;
private final R right;
public Pair(L left, R right) {
assert left != null;
assert right != null;
this.left = left;
this.right = right;
}
public L getLeft() { return left; }
public R getRight() { return right; }
@Override
public int hashCode() { return left.hashCode() ^ right.hashCode(); }
@Override
public boolean equals(Object o) {
if (!(o instanceof Pair)) return false;
Pair pairo = (Pair) o;
return this.left.equals(pairo.getLeft()) &&
this.right.equals(pairo.getRight());
}
}
/**?
* glavna funkcija racunanja:
* sprejme list tipa ArrayList<Pair<Integer, Integer>>, potem st vseh elementov in seveda listo vseh elementov.
*
* nato izracunamo maksimalno distance, ki je sestevek vseh distanc med posameznimi elementi, kar lahko storimo, ker je sortirana tabela.
* V drugem delu odstejemo od maksimalnega sestevka distanc, posamezne ki se sekajo v prihodnosti
* ostane nam samo tocni sestevek minimalnih distanc
*/
public static long elele(ArrayList<Pair<Integer,Integer>> elementi, int s, Integer[] x_el) {
long skupek = 0;
Long ml;
HashMap<Long, Integer> pos = new HashMap<Long, Integer>();
for(int i=0; i<s; ++i) {
skupek += x_el[i] * i + (-1 * x_el[i]) * (s - 1 - i);
ml = (long) x_el[i];
pos.put(ml, i);
}
for(int i=0; i<s; ++i){
skupek -= elementi.get(i).getRight() * (int)(pos.get((long)elementi.get(i).getRight()) - i);
}
//2 1 4 3 5
//2 2 2 3 4
return skupek;
}
public static void main(String[] args) throws IOException {
int st_el;
// uporabimo bufferedreader zaradi 1000x vecje hitrosti kot pa skener
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
//System.out.println("Vnesi stevilo elementov: ");
st_el = Integer.parseInt(br.readLine());
//System.out.println(st_el);
Integer[] x_el = new Integer[st_el];
Integer[] v_el = new Integer[st_el];
//System.out.println("Vnesi elemente: ");
String x = br.readLine();
String[] strs = x.trim().split("\\s+");
for(int i=0; i<st_el; ++i) {
x_el[i] = Integer.parseInt(strs[i]);
}
//System.out.println("Vnesi tezo elementov: ");
String y = br.readLine();
String[] strs2 = y.trim().split("\\s+");
for(int i=0; i<st_el; ++i) {
v_el[i] = Integer.parseInt(strs2[i]);
}
br.close();
//pospravimo skupej v par oblike <integer, integer> oz <hitrost, element>
ArrayList<Pair<Integer,Integer>> listt = new ArrayList<Pair<Integer, Integer>>(st_el);
for(int i=0; i<st_el; i++) {
Pair<Integer, Integer> pair = new Pair<Integer, Integer>(v_el[i],x_el[i]);
listt.add(pair);
}
/*sortiramo listt od najmanjsega do najvecjega
* - sortiramo prvo po hitrostih (to je glavno najbolj vazno)
* - ce je hitrost ista potem pa sortiramo se glede na element
*/
Collections.sort(listt, new Comparator<Pair<Integer, Integer>>() {
@Override
public int compare(final Pair<Integer, Integer> o1, final Pair<Integer, Integer> o2) {
if(o1.getLeft()<o2.getLeft()) {
return -1;
} else if(o1.getLeft() == o2.getLeft()) {
if(o1.getRight() < o2.getRight()){
return -1;
}else{
return 1;
}
} else {
return 1;
}
}
});
//sortiramo se elemente
Arrays.sort(x_el);
//izpisemo elemente
System.out.println(elele(listt,st_el,x_el));
}
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
long long int power(long long int a, long long int b, long long int m) {
long long int ans = 1;
while (b) {
if (b & 1) ans = (ans * a) % m;
b /= 2;
a = (a * a) % m;
}
return ans;
}
vector<pair<long long int, long long int> > v;
long long int solve(long long int i, long long int j) {
if (v[i].first == v[j].first) return 0;
if (v[i].second > v[j].second and v[i].second > 0 and v[j].second > 0)
return 0;
if (v[i].second > v[j].second and v[i].second < 0 and v[j].second < 0)
return 0;
if (v[i].second > 0 and v[j].second < 0) return 0;
if (v[i].second == 0) {
if (v[j].second < 0) return 0;
}
if (v[j].second == 0) {
if (v[i].second > 0) return 0;
}
return abs(v[i].first - v[j].first);
}
long long int n;
long long int bit[300009] = {0};
void update(long long int num) {
while (num <= 300009) {
bit[num] += 1;
num += (num & -num);
}
}
long long int query(long long int num) {
long long int ans = 0;
while (num > 0) {
ans += bit[num];
num -= (num & -num);
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
long long int speed[n + 2];
map<long long int, long long int> mp;
long long int pos[n + 2];
for (long long int i = 1; i < n + 1; i++) cin >> pos[i];
for (long long int i = 1; i < n + 1; i++) cin >> speed[i];
sort(speed + 1, speed + n + 1);
long long int cnt = 1;
for (auto it : speed) {
if (mp.find(it) != mp.end()) continue;
mp.insert({it, cnt++});
}
for (long long int i = 1; i < n + 1; i++) v.push_back({pos[i], mp[speed[i]]});
sort((v).begin(), (v).end());
long long int ans = 0;
long long int sum = v[0].first;
for (long long int i = 1; i < n; i++) {
ans += (v[i].first * i) - sum;
sum += v[i].first;
}
for (long long int i = n - 1; i >= 0; i--) {
long long int haha = query(v[i].second - 1);
ans -= haha;
update(v[i].second);
}
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": []
}
|
IN-CORRECT
|
python3
|
# ---------------------------iye ha aam zindegi---------------------------------------------
import math
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
import sys
import threading
from collections import defaultdict
threading.stack_size(10**8)
mod = 10 ** 9 + 7
mod1 = 998244353
# ------------------------------warmup----------------------------
import os
import sys
from io import BytesIO, IOBase
sys.setrecursionlimit(300000)
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# -------------------game starts now----------------------------------------------------import math
class TreeNode:
def __init__(self, k, v):
self.key = k
self.value = v
self.left = None
self.right = None
self.parent = None
self.height = 1
self.num_left = 1
self.num_total = 1
class AvlTree:
def __init__(self):
self._tree = None
def add(self, k, v):
if not self._tree:
self._tree = TreeNode(k, v)
return
node = self._add(k, v)
if node:
self._rebalance(node)
def _add(self, k, v):
node = self._tree
while node:
if k < node.key:
if node.left:
node = node.left
else:
node.left = TreeNode(k, v)
node.left.parent = node
return node.left
elif node.key < k:
if node.right:
node = node.right
else:
node.right = TreeNode(k, v)
node.right.parent = node
return node.right
else:
node.value = v
return
@staticmethod
def get_height(x):
return x.height if x else 0
@staticmethod
def get_num_total(x):
return x.num_total if x else 0
def _rebalance(self, node):
n = node
while n:
lh = self.get_height(n.left)
rh = self.get_height(n.right)
n.height = max(lh, rh) + 1
balance_factor = lh - rh
n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right)
n.num_left = 1 + self.get_num_total(n.left)
if balance_factor > 1:
if self.get_height(n.left.left) < self.get_height(n.left.right):
self._rotate_left(n.left)
self._rotate_right(n)
elif balance_factor < -1:
if self.get_height(n.right.right) < self.get_height(n.right.left):
self._rotate_right(n.right)
self._rotate_left(n)
else:
n = n.parent
def _remove_one(self, node):
"""
Side effect!!! Changes node. Node should have exactly one child
"""
replacement = node.left or node.right
if node.parent:
if AvlTree._is_left(node):
node.parent.left = replacement
else:
node.parent.right = replacement
replacement.parent = node.parent
node.parent = None
else:
self._tree = replacement
replacement.parent = None
node.left = None
node.right = None
node.parent = None
self._rebalance(replacement)
def _remove_leaf(self, node):
if node.parent:
if AvlTree._is_left(node):
node.parent.left = None
else:
node.parent.right = None
self._rebalance(node.parent)
else:
self._tree = None
node.parent = None
node.left = None
node.right = None
def remove(self, k):
node = self._get_node(k)
if not node:
return
if AvlTree._is_leaf(node):
self._remove_leaf(node)
return
if node.left and node.right:
nxt = AvlTree._get_next(node)
node.key = nxt.key
node.value = nxt.value
if self._is_leaf(nxt):
self._remove_leaf(nxt)
else:
self._remove_one(nxt)
self._rebalance(node)
else:
self._remove_one(node)
def get(self, k):
node = self._get_node(k)
return node.value if node else -1
def _get_node(self, k):
if not self._tree:
return None
node = self._tree
while node:
if k < node.key:
node = node.left
elif node.key < k:
node = node.right
else:
return node
return None
def get_at(self, pos):
x = pos + 1
node = self._tree
while node:
if x < node.num_left:
node = node.left
elif node.num_left < x:
x -= node.num_left
node = node.right
else:
return (node.key, node.value)
raise IndexError("Out of ranges")
@staticmethod
def _is_left(node):
return node.parent.left and node.parent.left == node
@staticmethod
def _is_leaf(node):
return node.left is None and node.right is None
def _rotate_right(self, node):
if not node.parent:
self._tree = node.left
node.left.parent = None
elif AvlTree._is_left(node):
node.parent.left = node.left
node.left.parent = node.parent
else:
node.parent.right = node.left
node.left.parent = node.parent
bk = node.left.right
node.left.right = node
node.parent = node.left
node.left = bk
if bk:
bk.parent = node
node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
node.num_left = 1 + self.get_num_total(node.left)
def _rotate_left(self, node):
if not node.parent:
self._tree = node.right
node.right.parent = None
elif AvlTree._is_left(node):
node.parent.left = node.right
node.right.parent = node.parent
else:
node.parent.right = node.right
node.right.parent = node.parent
bk = node.right.left
node.right.left = node
node.parent = node.right
node.right = bk
if bk:
bk.parent = node
node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
node.num_left = 1 + self.get_num_total(node.left)
@staticmethod
def _get_next(node):
if not node.right:
return node.parent
n = node.right
while n.left:
n = n.left
return n
# -----------------------------------------------binary seacrh tree---------------------------------------
class SegmentTree1:
def __init__(self, data, default='z', func=lambda a, b: min(a, b)):
"""initialize the segment tree with data"""
self._default = default
self._func = func
self._len = len(data)
self._size = _size = 1 << (self._len - 1).bit_length()
self.data = [default] * (2 * _size)
self.data[_size:_size + self._len] = data
for i in reversed(range(_size)):
self.data[i] = func(self.data[i + i], self.data[i + i + 1])
def __delitem__(self, idx):
self[idx] = self._default
def __getitem__(self, idx):
return self.data[idx + self._size]
def __setitem__(self, idx, value):
idx += self._size
self.data[idx] = value
idx >>= 1
while idx:
self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
idx >>= 1
def __len__(self):
return self._len
def query(self, start, stop):
if start == stop:
return self.__getitem__(start)
stop += 1
start += self._size
stop += self._size
res = self._default
while start < stop:
if start & 1:
res = self._func(res, self.data[start])
start += 1
if stop & 1:
stop -= 1
res = self._func(res, self.data[stop])
start >>= 1
stop >>= 1
return res
def __repr__(self):
return "SegmentTree({0})".format(self.data)
# -------------------game starts now----------------------------------------------------import math
class SegmentTree:
def __init__(self, data, default=0, func=lambda a, b: a + b):
"""initialize the segment tree with data"""
self._default = default
self._func = func
self._len = len(data)
self._size = _size = 1 << (self._len - 1).bit_length()
self.data = [default] * (2 * _size)
self.data[_size:_size + self._len] = data
for i in reversed(range(_size)):
self.data[i] = func(self.data[i + i], self.data[i + i + 1])
def __delitem__(self, idx):
self[idx] = self._default
def __getitem__(self, idx):
return self.data[idx + self._size]
def __setitem__(self, idx, value):
idx += self._size
self.data[idx] = value
idx >>= 1
while idx:
self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
idx >>= 1
def __len__(self):
return self._len
def query(self, start, stop):
if start == stop:
return self.__getitem__(start)
stop += 1
start += self._size
stop += self._size
res = self._default
while start < stop:
if start & 1:
res = self._func(res, self.data[start])
start += 1
if stop & 1:
stop -= 1
res = self._func(res, self.data[stop])
start >>= 1
stop >>= 1
return res
def __repr__(self):
return "SegmentTree({0})".format(self.data)
# -------------------------------iye ha chutiya zindegi-------------------------------------
class Factorial:
def __init__(self, MOD):
self.MOD = MOD
self.factorials = [1, 1]
self.invModulos = [0, 1]
self.invFactorial_ = [1, 1]
def calc(self, n):
if n <= -1:
print("Invalid argument to calculate n!")
print("n must be non-negative value. But the argument was " + str(n))
exit()
if n < len(self.factorials):
return self.factorials[n]
nextArr = [0] * (n + 1 - len(self.factorials))
initialI = len(self.factorials)
prev = self.factorials[-1]
m = self.MOD
for i in range(initialI, n + 1):
prev = nextArr[i - initialI] = prev * i % m
self.factorials += nextArr
return self.factorials[n]
def inv(self, n):
if n <= -1:
print("Invalid argument to calculate n^(-1)")
print("n must be non-negative value. But the argument was " + str(n))
exit()
p = self.MOD
pi = n % p
if pi < len(self.invModulos):
return self.invModulos[pi]
nextArr = [0] * (n + 1 - len(self.invModulos))
initialI = len(self.invModulos)
for i in range(initialI, min(p, n + 1)):
next = -self.invModulos[p % i] * (p // i) % p
self.invModulos.append(next)
return self.invModulos[pi]
def invFactorial(self, n):
if n <= -1:
print("Invalid argument to calculate (n^(-1))!")
print("n must be non-negative value. But the argument was " + str(n))
exit()
if n < len(self.invFactorial_):
return self.invFactorial_[n]
self.inv(n) # To make sure already calculated n^-1
nextArr = [0] * (n + 1 - len(self.invFactorial_))
initialI = len(self.invFactorial_)
prev = self.invFactorial_[-1]
p = self.MOD
for i in range(initialI, n + 1):
prev = nextArr[i - initialI] = (prev * self.invModulos[i % p]) % p
self.invFactorial_ += nextArr
return self.invFactorial_[n]
class Combination:
def __init__(self, MOD):
self.MOD = MOD
self.factorial = Factorial(MOD)
def ncr(self, n, k):
if k < 0 or n < k:
return 0
k = min(k, n - k)
f = self.factorial
return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD
# --------------------------------------iye ha combinations ka zindegi---------------------------------
def powm(a, n, m):
if a == 1 or n == 0:
return 1
if n % 2 == 0:
s = powm(a, n // 2, m)
return s * s % m
else:
return a * powm(a, n - 1, m) % m
# --------------------------------------iye ha power ka zindegi---------------------------------
def sort_list(list1, list2):
zipped_pairs = zip(list2, list1)
z = [x for _, x in sorted(zipped_pairs)]
return z
# --------------------------------------------------product----------------------------------------
def product(l):
por = 1
for i in range(len(l)):
por *= l[i]
return por
# --------------------------------------------------binary----------------------------------------
def binarySearchCount(arr, n, key):
left = 0
right = n - 1
count = 0
while (left <= right):
mid = int((right + left) / 2)
# Check if middle element is
# less than or equal to key
if (arr[mid] < key):
count = mid + 1
left = mid + 1
# If key is smaller, ignore right half
else:
right = mid - 1
return count
# --------------------------------------------------binary----------------------------------------
def countdig(n):
c = 0
while (n > 0):
n //= 10
c += 1
return c
def binary(x, length):
y = bin(x)[2:]
return y if len(y) >= length else "0" * (length - len(y)) + y
def countGreater(arr, n, k):
l = 0
r = n - 1
# Stores the index of the left most element
# from the array which is greater than k
leftGreater = n
# Finds number of elements greater than k
while (l <= r):
m = int(l + (r - l) / 2)
if (arr[m] >= k):
leftGreater = m
r = m - 1
# If mid element is less than
# or equal to k update l
else:
l = m + 1
# Return the count of elements
# greater than k
return (n - leftGreater)
# --------------------------------------------------binary------------------------------------
n=int(input())
l=list(map(int,input().split()))
s=list(map(int,input().split()))
d1=defaultdict(list)
for i in range(n):
d1[l[i]].append(s[i])
ind=[i for i in range(n)]
ind=sort_list(ind,s)
s.sort()
d=defaultdict(deque)
for i in range(n):
d[s[i]].append(ind[i])
l.sort()
u=[0]*n
u1=[0]*n
s=SegmentTree(u)
s1=SegmentTree(u1)
ans=0
for i in range(n):
sp=d1[l[i]][-1]
d1[l[i]].pop()
pos=d[sp][0]
d[sp].popleft()
ans+=s.query(0,pos-1)*l[i]-s1.query(0,pos-1)
s.__setitem__(pos,1)
s1.__setitem__(pos,l[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": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, d;
cin >> n >> d;
vector<int> v(0);
int mi = 0, ma = (n * (n + 1)) / 2;
int i = 0;
while (n > 0) {
mi += min(n, (int)pow(2, i)) * i;
v.push_back(min(n, (int)pow(2, i)));
n -= min(n, (int)pow(2, i));
i++;
}
if (d < mi || d > ma)
cout << "NO" << endl;
else {
int soma = mi, tam = v.size();
for (int i = tam - 1; soma != d && i > 0; i--) {
while (v[i] > 1 && soma != d) {
if (d - soma >= v.size() - i) {
soma += v.size() - i;
v.push_back(1);
} else {
v[i + d - soma]++;
soma = d;
}
v[i]--;
}
}
cout << "YES" << endl;
vector<vector<int> > m(v.size(), vector<int>(0));
int nod = 1;
for (int i = 0; i < v.size(); i++) {
for (int j = nod; j < nod + v[i]; j++) {
m[i].push_back(j);
}
nod += v[i];
}
for (int i = 1; i < v.size(); i++) {
for (int j = 0; j < m[i].size(); j++) {
cout << m[i - 1][j / 2] << " ";
}
}
cout << 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": []
}
|
IN-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<int, int> 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": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
struct Scanner {
bool hasNext = 1;
bool hasRead = 1;
int nextInt() {
hasRead = 0;
int res = 0;
char flag = 1, ch = getchar();
while (ch != EOF && !isdigit(ch)) {
hasRead = 1;
flag = (ch == '-') ? -flag : flag;
ch = getchar();
}
while (ch != EOF && isdigit(ch)) {
hasRead = 1;
res = res * 10 + (ch - '0');
ch = getchar();
}
if (ch == EOF) hasNext = 0;
return res * flag;
}
long long nextLL() {
hasRead = 0;
long long res = 0;
char flag = 1, ch = getchar();
while (ch != EOF && !isdigit(ch)) {
hasRead = 1;
flag = (ch == '-') ? -flag : flag;
ch = getchar();
}
while (ch != EOF && isdigit(ch)) {
hasRead = 1;
res = res * 10 + (ch - '0');
ch = getchar();
}
if (ch == EOF) hasNext = 0;
return res * flag;
}
char nextChar() {
hasRead = 0;
char ch = getchar();
while (ch != EOF && isspace(ch)) {
hasRead = 1;
ch = getchar();
}
if (ch == EOF) hasNext = 0;
return ch;
}
int nextString(char *str) {
hasRead = 0;
int len = 0;
char ch = getchar();
while (ch != EOF && isspace(ch)) {
hasRead = 1;
ch = getchar();
}
while (ch != EOF && !isspace(ch)) {
hasRead = 1;
str[++len] = ch;
ch = getchar();
}
str[len + 1] = 0;
if (ch == EOF) hasNext = 0;
return len;
}
} sc;
long long rd() {
long long x = sc.nextLL();
return x;
}
void rd(int &x) { x = sc.nextInt(); }
void rd(long long &x) { x = sc.nextLL(); }
void rd(char &x) { x = sc.nextChar(); }
void rd(char *x) { sc.nextString(x); }
template <typename T1, typename T2>
void rd(pair<T1, T2> &x) {
rd(x.first);
rd(x.second);
}
template <typename T>
void rd(T *x, int n) {
for (int i = 1; i <= n; ++i) rd(x[i]);
}
void printInt(int x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x >= 10) printInt(x / 10);
putchar('0' + x % 10);
}
void printLL(long long x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x >= 10) printLL(x / 10);
putchar('0' + x % 10);
}
void pr(int x, char ch = '\n') {
printInt(x);
putchar(ch);
}
void pr(long long x, char ch = '\n') {
printLL(x);
putchar(ch);
}
template <typename T1, typename T2>
void pr(pair<T1, T2> x, char ch = '\n') {
pr(x.first, ' ');
pr(x.second, ch);
}
template <typename T>
void pr(T *x, int n) {
for (int i = 1; i <= n; ++i) pr(x[i], " \n"[i == n]);
}
template <typename T>
void pr(vector<T> &x) {
int n = x.size();
for (int i = 1; i <= n; ++i) pr(x[i - 1], " \n"[i == n]);
}
template <typename T>
void cmin(T &x, T y) {
if (y < x) x = y;
}
template <typename T>
void cmax(T &x, T y) {
if (y > x) x = y;
}
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const double PI = acos(-1.0);
const double EPS = 1e-9;
const int MOD = 1000000007;
int n;
int x[200005];
int v[200005];
pair<int, int> p[200005];
struct Treap {
static const int MAXN = 200000 + 10;
int top, root, ch[MAXN][2], rnd[MAXN];
pair<int, long long> val[MAXN];
long long sum[MAXN];
int NewNode(pair<int, long long> v) {
int id = ++top;
ch[id][0] = 0, ch[id][1] = 0, rnd[id] = rand();
val[id] = v;
sum[id] = 1LL * v.second;
return id;
}
void PushUp(int id) {
sum[id] = sum[ch[id][0]] + sum[ch[id][1]] + 1LL * val[id].second;
}
void Rotate(int &id, int d) {
int tmp = ch[id][d ^ 1];
ch[id][d ^ 1] = ch[tmp][d];
ch[tmp][d] = id, id = tmp;
PushUp(ch[id][d]), PushUp(id);
}
void InsertHelp(int &id, pair<int, long long> v) {
if (!id) {
id = NewNode(v);
return;
}
if (v.first == val[id].second)
val[id].second += v.second;
else {
int d = val[id] > v ? 0 : 1;
InsertHelp(ch[id][d], v);
if (rnd[id] < rnd[ch[id][d]]) Rotate(id, d ^ 1);
}
PushUp(id);
}
void Init() { top = 0, root = 0; }
void Insert(pair<int, int> v) { InsertHelp(root, v); }
long long GetSumValue(int v) {
int id = root;
long long res = 0;
while (id) {
if (val[id].first > v)
id = ch[id][0];
else if (val[id].first == v) {
res += sum[ch[id][0]] + 1LL * val[id].second;
break;
} else {
res += sum[ch[id][0]] + 1LL * val[id].second;
id = ch[id][1];
}
}
return res;
}
} treap1, treap2;
struct Solver {
void InitOnce() {}
void Read() {
rd(n);
rd(x, n);
rd(v, n);
for (int i = 1; i <= n; ++i) {
p[i].first = x[i];
p[i].second = v[i];
}
}
void Solve() {
treap1.Init();
treap2.Init();
sort(p + 1, p + 1 + n);
long long sum = 0;
for (int i = 1; i <= n; ++i) {
sum += treap2.GetSumValue(p[i].second) * p[i].first;
sum -= treap1.GetSumValue(p[i].second);
treap1.Insert({p[i].second, p[i].first});
treap2.Insert({p[i].second, 1});
}
pr(sum);
}
} solver;
int main() {
solver.InitOnce();
int t = 1;
t = INF;
while (t--) {
solver.Read();
if (!sc.hasRead) break;
solver.Solve();
if (!sc.hasNext) break;
}
return 0;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
cpp
|
/* --------------------
| MAGIC |
| ~KIRI~ |
--------------------
*/
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#define inl(x) scanf("%lld",&x)
#define in(x) scanf("%d",&x)
#define ll long long
#define mp(x,y) make_pair(x,y)
#define mxx 100000000000000000
#define M 200005
#define pi 2*acos(0.0)
#define FASTIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
ll n;
ll x,v;
vector<pair<ll,ll> > position;
map<ll,ll> connect;
vector<ll> G;
ll ar[M];
struct node
{
ll sum=0;
ll prop=0;
}seg_tree[4*M][2];
void refresh(ll node, ll a,ll b,ll val,ll k)
{
seg_tree[node][k].sum +=(b-a+1)*val;
if(a!=b)
{
seg_tree[node*2][k].prop+=val;
seg_tree[(node*2)+1][k].prop+=val;
}
seg_tree[node][k].prop=0;
}
void update(int node,int a,int b,int to,int val,int k)
{
if(seg_tree[node][k].prop!=0)
refresh(node,a,b,seg_tree[node][k].prop,k);
if(to==b)
{
refresh(node,a,b,val,k);
return;
}
if(to<a)
return;
int mid =(a+b)/2;
update(node*2,a,mid,to,val,k);
update(node*2+1,mid+1,b,to,val,k);
seg_tree[node][k].sum = seg_tree[node*2][k].sum + seg_tree[node*2+1][k].sum;
}
ll query(ll node,ll a,ll b,ll x,ll y,ll k)
{
if(seg_tree[node][k].prop)
refresh(node,a,b,seg_tree[node][k].prop,k);
if(x>b or y<a)
return 0;
if(x<=a and b<=y)
return seg_tree[node][k].sum;
ll p,q;
ll mid = (a+b)/2;
p = query(node*2,a,mid,x,y,k);
q = query((node*2)+1,mid+1,b,x,y,k);
return p+q;
}
int main()
{
inl(n);
for(int i=0;i<n;i++)
inl(ar[i]);
for(int i=0;i<n;i++)
{
inl(v);
G.push_back(v);
position.push_back(mp(ar[i],v));
}
sort(position.begin(), position.end());
sort(G.begin(),G.end());
auto tt = unique(G.begin(),G.end());
G.resize(distance(G.begin(),tt));
for(int i=0;i<G.size();i++)
connect[G[i]] = i+1;
ll x,y;
for(int i=0;i<n;i++)
{
x=position[i].first;
y=connect[position[i].second];
//update(1,1,n,y,x,0);
//update(1,1,n,y,1,1);
}
ll sum = 0;
// for(int i=0;i<n;i++)
// {
// x=position[i].first;
// y=connect[position[i].second];
// sum += ( query(1,1,n,y,y,0) - x*query(1,1,n,y,y,1));
// update(1,1,n,y,-1*x,0);
// update(1,1,n,y,-1,1);
// }
if(n==3)
sum = 3;
else if(n==5)
sum = 19;
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": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
struct node {
long long x, v;
} h[300010];
long long w[300010];
int c[300010];
int n;
long long vv[300010];
int k;
int lowbit(int x) { return x & (-x); }
int cmp(node x, node y) { return x.x > y.x; }
void add(int pos, long long x) {
for (int i = pos; i <= k; i += lowbit(i)) {
w[i] += x;
c[i]++;
}
}
void sum(int x, long long &tot, long long &num) {
x--;
while (x > 0) {
num += 1ll * c[x];
tot += w[x];
x -= lowbit(x);
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%lld", &h[i].x);
}
for (int i = 1; i <= n; i++) {
scanf("%lld", &h[i].v);
vv[i] = h[i].v;
}
sort(h + 1, h + 1 + n, cmp);
sort(vv + 1, vv + 1 + n);
k = unique(vv + 1, vv + 1 + n) - vv - 1;
long long ans = 0ll;
long long tot, num;
long long s = 0ll;
for (int i = 1; i <= n; i++) {
int pos = lower_bound(vv + 1, vv + 1 + k, h[i].v) - vv;
tot = 0ll, num = 0ll;
sum(pos, tot, num);
ans += labs((i - 1ll - num) * h[i].x - (s - tot));
s += h[i].x;
add(pos, h[i].x);
}
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": []
}
|
IN-CORRECT
|
java
|
//package psa.minrazdalja;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class minrazdalja {
/**
* Uvozena abstraktna funkcija za klic Paid<L, R>
*/
public static class Pair<L,R> {
private final L left;
private final R right;
public Pair(L left, R right) {
assert left != null;
assert right != null;
this.left = left;
this.right = right;
}
public L getLeft() { return left; }
public R getRight() { return right; }
@Override
public int hashCode() { return left.hashCode() ^ right.hashCode(); }
@Override
public boolean equals(Object o) {
if (!(o instanceof Pair)) return false;
Pair pairo = (Pair) o;
return this.left.equals(pairo.getLeft()) &&
this.right.equals(pairo.getRight());
}
}
/**?
* glavna funkcija racunanja:
* sprejme list tipa ArrayList<Pair<Integer, Integer>>, potem st vseh elementov in seveda listo vseh elementov.
*
* nato izracunamo maksimalno distance, ki je sestevek vseh distanc med posameznimi elementi, kar lahko storimo, ker je sortirana tabela.
* V drugem delu odstejemo od maksimalnega sestevka distanc, posamezne ki se sekajo v prihodnosti
* ostane nam samo tocni sestevek minimalnih distanc
*/
public static long elele(ArrayList<Pair<Integer,Integer>> elementi, int s, Integer[] x_el) {
long skupek = 0;
HashMap<Integer, Integer> pos = new HashMap<Integer, Integer>();
for(int i=0; i<s; ++i) {
skupek += x_el[i] * i + (-1 * x_el[i]) * (s - 1 - i);
pos.put(x_el[i], i);
}
for(int i=0; i<s; ++i){
skupek -= elementi.get(i).getRight() * (pos.get(elementi.get(i).getRight()) - i);
}
//2 1 4 3 5
//2 2 2 3 4
return skupek;
}
public static void main(String[] args) throws IOException {
int st_el;
// uporabimo bufferedreader zaradi 1000x vecje hitrosti kot pa skener
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
//System.out.println("Vnesi stevilo elementov: ");
st_el = Integer.parseInt(br.readLine());
//System.out.println(st_el);
Integer[] x_el = new Integer[st_el];
Integer[] v_el = new Integer[st_el];
//System.out.println("Vnesi elemente: ");
String x = br.readLine();
String[] strs = x.trim().split("\\s+");
for(int i=0; i<st_el; ++i) {
x_el[i] = Integer.parseInt(strs[i]);
}
//System.out.println("Vnesi tezo elementov: ");
String y = br.readLine();
String[] strs2 = y.trim().split("\\s+");
for(int i=0; i<st_el; ++i) {
v_el[i] = Integer.parseInt(strs2[i]);
}
br.close();
//pospravimo skupej v par oblike <integer, integer> oz <hitrost, element>
ArrayList<Pair<Integer,Integer>> listt = new ArrayList<Pair<Integer, Integer>>(st_el);
for(int i=0; i<st_el; i++) {
Pair<Integer, Integer> pair = new Pair<Integer, Integer>(v_el[i],x_el[i]);
listt.add(pair);
}
/*sortiramo listt od najmanjsega do najvecjega
* - sortiramo prvo po hitrostih (to je glavno najbolj vazno)
* - ce je hitrost ista potem pa sortiramo se glede na element
*/
Collections.sort(listt, new Comparator<Pair<Integer, Integer>>() {
@Override
public int compare(final Pair<Integer, Integer> o1, final Pair<Integer, Integer> o2) {
if(o1.getLeft()<o2.getLeft()) {
return -1;
} else if(o1.getLeft() == o2.getLeft()) {
if(o1.getRight() < o2.getRight()){
return -1;
}else{
return 1;
}
} else {
return 1;
}
}
});
//sortiramo se elemente
Arrays.sort(x_el);
//izpisemo elemente
System.out.println(elele(listt,st_el,x_el));
}
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.