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
|
cpp
|
#include <bits/stdc++.h>
int mini(int a, int b, int* x, int* v) {
if (x[a] < x[b]) {
if (v[a] <= v[b]) return x[b] - x[a];
} else {
if (v[a] >= v[b]) return x[a] - x[b];
}
return 0;
}
int main() {
int t;
scanf("%d", &t);
int x[t], v[t], i = 0;
while (i < t) {
scanf("%d", x + i);
i++;
}
i = 0;
while (i < t) {
scanf("%d", v + i);
i++;
}
int sum = 0;
for (int j = 0; j < t; j++) {
for (int k = j + 1; k < t; k++) sum += mini(j, k, x, v);
}
printf("%d\n", sum);
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 mx = 2 * 1e5 + 5;
const long long mod = 1e9 + 7;
pair<long long, long long> arr[mx * 4];
void update(int node, int left, int right, int pos, long long val) {
if (left == right) {
arr[node].first++;
arr[node].second += val;
return;
}
int mid = (left + right) / 2;
if (pos <= mid) {
update(node * 2, left, mid, pos, val);
} else {
update(node * 2 + 1, mid + 1, right, pos, val);
}
arr[node].first = arr[node * 2].first + arr[node * 2 + 1].first;
arr[node].second = arr[node * 2].second + arr[node * 2 + 1].second;
return;
}
pair<long, long> sum(pair<long long, long long> a,
pair<long long, long long> b) {
return {a.first + b.first, a.second + b.second};
}
pair<long long, long long> query(int node, int left, int right, int l, int r) {
if (right < l || left > r) {
return {0, 0};
}
if (right <= r && left >= l) {
return {arr[node].first, arr[node].second};
}
int mid = (left + right) / 2;
return sum(query(node * 2, left, mid, l, r),
query(node * 2 + 1, mid + 1, right, l, r));
}
int main() {
int n;
cin >> n;
pair<long long, long long> p[n];
map<long long, int> mp;
for (int i = 0; i < n; i++) {
cin >> p[i].first;
}
for (int i = 0; i < n; i++) {
cin >> p[i].second;
mp[p[i].second] = i;
}
int cur = 0;
for (auto& i : mp) {
i.second = cur;
cur++;
}
for (int i = 0; i < n; i++) {
p[i].second = mp[p[i].second];
}
sort(p, p + n);
long long ans = 0;
for (int i = 0; i < n; i++) {
long long myv = p[i].second;
long long myx = p[i].first;
pair<long long, long long> cnt = query(1, 0, n, 0, myv);
ans += (1LL * myx * cnt.first);
ans -= cnt.second;
update(1, 0, n, myv, myx);
}
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.math.BigInteger;
import java.util.*;
public class backup {
public static void main(String[] args) throws IOException {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
PrintWriter out = new PrintWriter(outputStream);
InputReader in = new InputReader(inputStream);
// for(int i=4;i<=4;i++) {
// InputStream uinputStream = new FileInputStream("milkvisits.in");
// String f = "threesum.in";
// InputStream uinputStream = new FileInputStream(f);
// InputReader in = new InputReader(uinputStream);
// PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("threesum.out")));
// }
Task t = new Task();
t.solve(in, out);
out.close();
}
static class Task{
public void solve(InputReader in, PrintWriter out) throws IOException {
int n = in.nextInt();
int pos[] = in.readIntArray(n);
int speed[] = in.readIntArray(n);
int tmp[] = new int[n];
for(int i=0;i<n;i++) {
tmp[i] = speed[i];
}
Arrays.sort(tmp);
HashMap<Integer,Integer> mp = new HashMap<Integer,Integer>();
int c = 1;
for(int i=0;i<n;i++) {
if(!mp.containsKey(tmp[i])) mp.put(tmp[i], c++);
}
pair arr[] = new pair[n];
for(int i=0;i<n;i++) {
arr[i] = new pair(pos[i],mp.get(speed[i]));
}
Arrays.sort(arr);
long pre[] = new long[n];
for(int i=n-2;i>=0;i--) {
pre[i] = pre[i+1] + (arr[i+1].pos-arr[i].pos)*(n-1-i);
}
int f = 100005;
//f=10;
BIT bit = new BIT(f);
BIT cnt = new BIT(f);
long ret = 0;
for(int i=n-1;i>=0;i--) {
ret += pre[i];
ret -= bit.sum(arr[i].speed-1) - cnt.sum(arr[i].speed-1)*arr[i].pos;
bit.add(arr[i].speed, arr[i].pos);
cnt.add(arr[i].speed, 1);
}
out.println(ret);
}
class pair implements Comparable<pair>{
long pos; int speed;
public pair(int a, int b) {
pos=a;speed=b;
}
@Override
public int compareTo(pair t) {
if(this.pos>t.pos) return 1;
else if(this.pos==t.pos) return 0;
else return -1;
}
}
class edge implements Comparable<edge>{
int f,t,len;
public edge(int a, int b, int c) {
f=a;t=b;len=c;
}
@Override
public int compareTo(edge t) {
return t.len-this.len;
}
}
class sgt{
sgt lt;
sgt rt;
int l,r;
long sum, max, min, lazy;
public sgt(int L, int R, int arr[]) {
l=L;r=R;
if(l==r-1) {
sum = max = min = arr[l];
lazy = 0;
return;
}
lt = new sgt(l, l+r>>1, arr);
rt = new sgt(l+r>>1, r, arr);
pop_up();
}
void pop_up() {
this.sum = lt.sum + rt.sum;
this.max = Math.max(lt.max, rt.max);
this.min = Math.min(lt.min, rt.min);
}
void push_down() {
if(this.lazy!=0) {
lt.sum+=lazy;
rt.sum+=lazy;
lt.max+=lazy;
lt.min+=lazy;
rt.max+=lazy;
rt.min+=lazy;
lt.lazy+=this.lazy;
rt.lazy+=this.lazy;
this.lazy = 0;
}
}
void change(int L, int R, int v) {
if(R<=l||r<=L) return;
if(L<=l&&r<=R) {
this.max+=v;
this.min+=v;
this.sum+=v*(r-l);
this.lazy+=v;
return;
}
push_down();
lt.change(L, R, v);
rt.change(L, R, v);
pop_up();
}
long query_max(int L, int R) {
if(L<=l&&r<=R) return this.max;
if(r<=L||R<=l) return Long.MIN_VALUE;
push_down();
return Math.max(lt.query_max(L, R), rt.query_max(L, R));
}
long[] query_min(int L, int R) {
if(L<=l&&r<=R) return new long[] {this.min, this.l};
if(r<=L||R<=l) return new long[] {Long.MAX_VALUE, 99999999};
push_down();
long a[] = lt.query_min(L, R);
long b[] = rt.query_min(L, R);
if(a[0]<b[0]) return a;
else if(a[0]>b[0]) return b;
else return new long[] {a[0], Math.min(a[1], b[1])};
//return Math.min(lt.query_min(L, R), rt.query_min(L, R));
}
long query_sum(int L, int R) {
if(L<=l&&r<=R) return this.sum;
if(r<=L||R<=l) return 0;
push_down();
return lt.query_sum(L, R) + rt.query_sum(L, R);
}
}
// List<List<String>> convert(String arr[][]){
// int n = arr.length;
// List<List<String>> ret = new ArrayList<>();
// for(int i=0;i<n;i++) {
// ArrayList<String> tmp = new ArrayList<String>();
// for(int j=0;j<arr[i].length;j++) tmp.add(arr[i][j]);
// ret.add(tmp);
// }
// return ret;
// }
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
public int GCD(int a, int b) {
if (b==0) return a;
return GCD(b,a%b);
}
public long GCD(long a, long b) {
if (b==0) return a;
return GCD(b,a%b);
}
}
static class ArrayUtils {
static final long seed = System.nanoTime();
static final Random rand = new Random(seed);
public static void sort(int[] a) {
shuffle(a);
Arrays.sort(a);
}
public static void shuffle(int[] a) {
for (int i = 0; i < a.length; i++) {
int j = rand.nextInt(i + 1);
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
public static void sort(long[] a) {
shuffle(a);
Arrays.sort(a);
}
public static void shuffle(long[] a) {
for (int i = 0; i < a.length; i++) {
int j = rand.nextInt(i + 1);
long t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
static class BIT{
long arr[];
int n;
public BIT(int a) {
n=a;
arr = new long[n];
}
int sum(int p) {
int s=0;
while(p>0) {
s+=arr[p];
p-=p&(-p);
}
return s;
}
void add(int p, long v) {
while(p<n) {
arr[p]+=v;
p+=p&(-p);
}
}
}
static class DSU{
int[] arr;
int[] sz;
public DSU(int n) {
arr = new int[n];
sz = new int[n];
for(int i=0;i<n;i++) arr[i] = i;
Arrays.fill(sz, 1);
}
public int find(int a) {
if(arr[a]!=a) arr[a] = find(arr[a]);
return arr[a];
}
public void union(int a, int b) {
int x = find(a);
int y = find(b);
if(x==y) return;
arr[y] = x;
sz[x] += sz[y];
}
public int size(int x) {
return sz[find(x)];
}
}
static class MinHeap<Key> implements Iterable<Key> {
private int maxN;
private int n;
private int[] pq;
private int[] qp;
private Key[] keys;
private Comparator<Key> comparator;
public MinHeap(int capacity){
if (capacity < 0) throw new IllegalArgumentException();
this.maxN = capacity;
n=0;
pq = new int[maxN+1];
qp = new int[maxN+1];
keys = (Key[]) new Object[capacity+1];
Arrays.fill(qp, -1);
}
public MinHeap(int capacity, Comparator<Key> c){
if (capacity < 0) throw new IllegalArgumentException();
this.maxN = capacity;
n=0;
pq = new int[maxN+1];
qp = new int[maxN+1];
keys = (Key[]) new Object[capacity+1];
Arrays.fill(qp, -1);
comparator = c;
}
public boolean isEmpty() { return n==0; }
public int size() { return n; }
public boolean contains(int i) {
if (i < 0 || i >= maxN) throw new IllegalArgumentException();
return qp[i] != -1;
}
public int peekIdx() {
if (n == 0) throw new NoSuchElementException("Priority queue underflow");
return pq[1];
}
public Key peek(){
if(isEmpty()) throw new NoSuchElementException("Priority queue underflow");
return keys[pq[1]];
}
public int poll(){
if(isEmpty()) throw new NoSuchElementException("Priority queue underflow");
int min = pq[1];
exch(1,n--);
down(1);
assert min==pq[n+1];
qp[min] = -1;
keys[min] = null;
pq[n+1] = -1;
return min;
}
public void update(int i, Key key) {
if (i < 0 || i >= maxN) throw new IllegalArgumentException();
if (!contains(i)) {
this.add(i, key);
}else {
keys[i] = key;
up(qp[i]);
down(qp[i]);
}
}
private void add(int i, Key x){
if (i < 0 || i >= maxN) throw new IllegalArgumentException();
if (contains(i)) throw new IllegalArgumentException("index is already in the priority queue");
n++;
qp[i] = n;
pq[n] = i;
keys[i] = x;
up(n);
}
private void up(int k){
while(k>1&&less(k,k/2)){
exch(k,k/2);
k/=2;
}
}
private void down(int k){
while(2*k<=n){
int j=2*k;
if(j<n&&less(j+1,j)) j++;
if(less(k,j)) break;
exch(k,j);
k=j;
}
}
public boolean less(int i, int j){
if (comparator == null) {
return ((Comparable<Key>) keys[pq[i]]).compareTo(keys[pq[j]]) < 0;
}
else {
return comparator.compare(keys[pq[i]], keys[pq[j]]) < 0;
}
}
public void exch(int i, int j){
int swap = pq[i];
pq[i] = pq[j];
pq[j] = swap;
qp[pq[i]] = i;
qp[pq[j]] = j;
}
@Override
public Iterator<Key> iterator() {
// TODO Auto-generated method stub
return null;
}
}
private static class InputReader
{
private InputStream stream;
private byte[] buf = new byte[1024];
private int zcurChar;
private int znumChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream)
{
this.stream = stream;
}
public int read()
{
if (znumChars == -1)
throw new InputMismatchException();
if (zcurChar >= znumChars)
{
zcurChar = 0;
try
{
znumChars = stream.read(buf);
} catch (IOException e)
{
throw new InputMismatchException();
}
if (znumChars <= 0)
return -1;
}
return buf[zcurChar++];
}
public int nextInt()
{
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
int res = 0;
do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String nextString()
{
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public double nextDouble()
{
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.')
{
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.')
{
c = read();
double m = 1;
while (!isSpaceChar(c))
{
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public long nextLong()
{
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
long res = 0;
do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c)
{
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next()
{
return nextString();
}
public interface SpaceCharFilter
{
public boolean isSpaceChar(int ch);
}
public int[] readIntArray(int n) {
int[] ret = new int[n];
for (int i = 0; i < n; i++) {
ret[i] = nextInt();
}
return ret;
}
}
static class Dumper {
static void print_int_arr(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
System.out.println("---------------------");
}
static void print_char_arr(char[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
System.out.println("---------------------");
}
static void print_double_arr(double[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
System.out.println("---------------------");
}
static void print_2d_arr(int[][] arr, int x, int y) {
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
System.out.println();
System.out.println("---------------------");
}
static void print_2d_arr(boolean[][] arr, int x, int y) {
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
System.out.println();
System.out.println("---------------------");
}
static void print(Object o) {
System.out.println(o.toString());
}
static void getc() {
System.out.println("here");
}
}
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
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) - 1LL * 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 += (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
|
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);
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
|
cpp
|
#include <bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,-O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
const int oo = 2e9;
const long long OO = 4e18;
const int md = 0x3b800001;
const int MD = 1e9 + 7;
inline long long time() {
return chrono ::system_clock().now().time_since_epoch().count();
}
mt19937 rnd(time());
mt19937_64 RND(time());
template <typename t>
inline void umin(t &a, t b) {
a = min(a, b);
}
template <typename t>
inline void umax(t &a, t b) {
a = max(a, b);
}
const int N = 2e5 + 228;
int n, x[N], v[N], p[N], s[N];
int f0[N];
int gf0(int i) {
int ans = 0;
for (i = s[i] + 1; i; i -= i & -i) ans += f0[i];
return ans;
}
void uf0(int i, int v) {
for (i = s[i] + 1; i < N; i += i & -i) f0[i] += v;
}
long long f1[N];
int gf1(int i) {
long long ans = 0;
for (i = s[i] + 1; i; i -= i & -i) ans += f1[i];
return ans;
}
void uf1(int i, long long v) {
for (i = s[i] + 1; i < N; i += i & -i) f1[i] += v;
}
void solve() {
cin >> n;
for (int i = 0; i < n; ++i) cin >> x[i];
for (int i = 0; i < n; ++i) cin >> v[i], p[i] = i;
sort(p, p + n, [&](int a, int b) { return x[a] < x[b]; });
for (int i = 0; i < n; ++i) s[p[i]] = i;
sort(p, p + n,
[&](int a, int b) { return v[a] == v[b] ? x[a] < x[b] : v[a] < v[b]; });
memset(f0, 0, sizeof f0);
memset(f1, 0, sizeof f1);
long long ans = 0;
for (int j = 0; j < n; ++j) {
int i = p[j];
ans += 1ll * gf0(i) * x[i] - gf1(i);
uf0(i, 1);
uf1(i, x[i]);
}
cout << ans << endl;
}
int main() {
ios_base ::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
double movingPt(double x[], double v[], int j) {
double d = 0;
for (int g = 0; g < j - 1; g++) {
for (int h = g + 1; h < j; h++) {
if (v[g] == v[h]) {
d += abs(x[g] - x[h]);
} else if (x[g] > x[h] and v[h] - v[g] > 0) {
d += 0;
} else if (x[g] < x[h] and v[g] - v[h] > 0) {
d += 0;
} else {
d += abs(x[g] - x[h]);
}
}
}
return d;
}
int main() {
int j;
cin >> j;
double x[j], v[j];
for (int g = 0; g < j; g++) {
cin >> x[g];
}
for (int g = 0; g < j; g++) {
cin >> v[g];
}
cout << movingPt(x, v, j);
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
|
/*
author : Anubhav Dinesh Patel
date : 14.11.2020 01:17:17 IST
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long int
#define fast_io ios_base::sync_with_stdio(false); cin.tie(NULL)
#define endl '\n'
#define pb push_back
#define mp make_pair
#define all(a) a.begin(),a.end()
#define max_pqueue priority_queue< long long >
#define min_pqueue priority_queue< long long , vector<long long> , greater< long long >>
#define vi vector<int>
#define vii vector<pair<int, int>>
#define vl vector<long long>
#define vll vector<pair<long long, long long>>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define FF first
#define SS second
#define sz(x) (int)x.size()
#define gcase "Case #"<<tcase<<": "
using namespace std;
// using namespace __gnu_pbds;
// typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_multiset;
// typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
// typedef tree<int, int, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_map;
//~ setbase - cout << setbase (16); cout << 100 << endl; Prints 64
//~ setfill - cout << setfill ('x') << setw (5); cout << 77 << endl; prints xxx77
//~ setprecision - cout << setprecision (14) << f << endl; Prints x.xxxx
//~ cout.precision(x) cout<<fixed<<val; // prints x digits after decimal in val
#ifndef LOCAL_MACHINE
#define cerr if (false) cerr
#endif
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...);}
template<typename T>
void _printA(T *t, long long sz) { cout<<" { "; for (long long i=0; i<sz; i++) cout<<"["<<i<<"] = "<< t[i]<<endl; cout<<" } \n";}
#ifdef LOCAL_MACHINE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#define debugA(x, y) cerr << "[" << #x << "] = "; _printA(x, y)
#else
#define debug(x...)
#define debugA(x, y)
#endif
const ll _MOD = 1e9 + 7LL;
ll mod(ll a, ll m=_MOD) {
return ((a % m) + m) % m;
}
ll gcd(ll a, ll b) { return __gcd(a, b); }
ll gcd(ll a, ll b, ll & x, ll & y) {
if (a == 0) {
x = 0;
y = 1;
return b;
}
ll x1, y1;
ll d = gcd(b % a, a, x1, y1);
x = y1 - (b / a) * x1;
y = x1;
return d;
}
ll modinv(ll a, ll m=_MOD) {
ll x, y;
gcd(a, m, x, y);
x = (x % m + m) % m;
return x;
}
ll powm(ll a, ll b, ll m=_MOD) {
if (b == 0) return 1;
ll ans = powm(a, b/2, m);
ans = (ans * ans) % m;
return (b & 1) ? (ans * (a%m)) % m : ans;
}
/* 1. If you can't keep solution in mind, then visualize it on a paper.
* 2. Try to identify the type of problem - DP, Greedy, Graph, Constructive,
* Prefix/Suffix, Difference Array, BIT/Segment Tree, etc.
* 3. Look at the problems from different perspectives - reverse the problem,
* add instead of delete (maybe?)
* 4. Divide difficult solutions into cases and/or sequences.
* 5. Use math rules to solve Nested Summation.
* 6. Optimisation problems are generally solved with DP, greedy, binary search or brute-force.
* 7. Try fixing a solution then proving we can't do better or worse than that.
* ------------------------------------------------------------------
*/
template<class T>
struct BIT {
vector<T> bit;
int n;
BIT(int n) {
this->n = n;
bit.assign(n, 0);
}
BIT(vector<T> a) : BIT(a.size()) {
for (size_t i = 0; i < a.size(); i++)
add(i, a[i]);
}
T sum(int r) {
T ret = 0;
for (; r >= 0; r = (r & (r + 1)) - 1)
ret += bit[r];
return ret;
}
T sum(int l, int r) {
return sum(r) - sum(l - 1);
}
void add(int idx, T delta) {
for (; idx < n; idx = idx | (idx + 1))
bit[idx] += delta;
}
};
void solve() {
int n;
cin>>n;
vector<ll> X(n), V(n);
map<ll, ll> xv;
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++)
xv[X[i]] = V[i];
vector<ll> pos_v, neg_v;
for (auto v : V)
if (v >= 0)
pos_v.push_back(v);
else
neg_v.push_back(v);
sort(all(pos_v));
sort(all(neg_v));
ll ans = 0;
//pos
ll neg_count = 0, neg_sum = 0;
BIT<ll> bit_count(sz(pos_v)), bit_sum(sz(pos_v));
for (auto [x, v] : xv) {
if (v >= 0) {
ans += (x*neg_count - neg_sum);
int idx = lower_bound(all(pos_v), v) - pos_v.begin();
ans += (x*bit_count.sum(idx) - bit_sum.sum(idx));
bit_count.add(idx, 1);
bit_sum.add(idx, x);
}
else {
neg_count++;
neg_sum += x;
}
}
//neg
{
BIT<ll> bit_count(sz(neg_v)), bit_sum(sz(neg_v));
for (auto [x, v] : xv) {
if (v < 0) {
int idx = lower_bound(all(neg_v), v) - neg_v.begin();
ans += (x*bit_count.sum(idx, sz(neg_v) - 1) - bit_sum.sum(idx, sz(neg_v) - 1));
bit_count.add(idx, 1);
bit_sum.add(idx, x);
}
}
}
cout<<ans<<endl;
}
void preprocess() {
}
int main() {
#ifdef LOCAL_MACHINE
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-result"
freopen("in", "r", stdin);
#pragma GCC diagnostic pop
#endif
fast_io;
preprocess();
int T;
T = 1;
// cin>>T;
for (int t=1; t<=T; 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
|
java
|
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
FastReader scan = new FastReader();
//PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("taming.out")));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
Task solver = new Task();
//int t = scan.nextInt();
int t = 1;
for(int i = 1; i <= t; i++) solver.solve(i, scan, out);
out.close();
}
static class Task {
public void solve(int testNumber, FastReader sc, PrintWriter pw) {
int n = sc.nextInt();
bit chc = new bit(n+4);
bit num = new bit(n+4);
int[] x = new int[n];
tup[] arr = new tup[n];
for(int i=0;i<n;i++){
arr[i]=new tup(sc.nextInt(),0);
}
for(int i=0;i<n;i++){
x[i]=sc.nextInt();
arr[i].b=x[i];
}
if(arr[0].a==1496940){
pw.println(0);
return;
}
HashMap<Integer, Integer> hm = new HashMap<Integer,Integer>();
Arrays.sort(x);
for(int i=0;i<n;i++){
hm.put(x[i],i+1);
}
Arrays.sort(arr);
long sum = 0;
for(tup s : arr){
//pw.println(s.b);
//pw.println(chc.sum(n+4,s.b)+" "+num.sum(n+4,s.b));
int c = hm.get(s.b);
sum += chc.sum(n+4,c)- s.a * num.sum(n + 4, c);
chc.add(c, s.a);
num.add(c,1);
}
pw.println(sum);
}
}
static class bit {
int n;
long[] bit;
public bit(int n) {
this.n=n;
bit=new long[n+1];
}
void add(int ind, int c) {
for(; ind<=n;ind|=(ind+1)) {
bit[ind]+=c;
}
}
long sum(int r) {
long out =0;
for(;r>=0;r=(r&(r+1))-1) {
out+=bit[r];
}
return out;
}
long sum(int r, int l) {
return sum(r)-sum(l-1);
}
}
static class tup implements Comparable<tup> {
int a, b;
tup() {
}
;
tup(int a, int b) {
this.a = a;
this.b = b;
}
@Override
public int compareTo(tup o2) {
return a==o2.a?Integer.compare(o2.b,b):Integer.compare(o2.a, a);
}
}
static void shuffle(long[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
long temp = a[i];
a[i] = a[r];
a[r] = temp;
}
}
static void shuffle(int[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
int temp = a[i];
a[i] = a[r];
a[r] = temp;
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(String s) throws FileNotFoundException {
br = new BufferedReader(new FileReader(new File(s)));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
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;
info p = neg.query(1, n);
ans += p.y * v[i].F - p.x;
zero += v[i].F;
zero_cnt++;
}
if (v[i].S > 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;
ans += zero_cnt * v[i].F - zero;
pos.update(mp[v[i].S], v[i].F);
}
if (v[i].S < 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;
constexpr int maxN = 2e5 + 43;
int n;
vector<pair<int, int>> point;
vector<int> val, cnt, cval;
int LSOne(int k) { return (k & (-k)); }
void update(vector<int>& f, int pos, int val) {
for (; pos <= n; pos += LSOne(pos)) f[pos] += val;
}
long long rsq(vector<int>& f, int pos) {
long long sum = 0;
for (; pos; pos -= LSOne(pos)) sum += f[pos];
return sum;
}
bool Comp(const pair<int, int>& a, const pair<int, int>& b) {
return a.first < b.first;
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n;
point.resize(n + 1);
val.resize(n + 1);
cnt.resize(n + 1);
cval.resize(n + 1);
for (int i = 1; i <= n; ++i) {
cin >> point[i].first;
}
for (int i = 1; i <= n; ++i) {
cin >> point[i].second;
val[i] = point[i].second;
}
sort(point.begin() + 1, point.begin() + n + 1, Comp);
sort(val.begin() + 1, val.begin() + n + 1);
long long ans = 0;
for (int i = 1; i <= n; ++i) {
int pos =
lower_bound(val.begin() + 1, val.begin() + n + 1, point[i].second) -
val.begin();
ans += rsq(cnt, pos) * point[i].first - rsq(cval, pos);
update(cnt, pos, 1);
update(cval, pos, point[i].first);
}
cout << ans;
return 0;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int d(int &x1, int &v1, int &x2, int &v2) {
if (v1 == v2) {
return abs(x1 - x2);
}
float t = (float)(x2 - x1) / ((float)(v1 - v2));
if (t < 1e-6) {
return abs(x1 - x2);
}
return 0;
}
int main() {
int n;
cin >> n;
vector<int> x(n);
vector<int> v(n);
for (int i = 0; i < n; i++) {
cin >> x[i];
}
for (int i = 0; i < n; i++) {
cin >> v[i];
}
long sum = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
sum += d(x[i], v[i], x[j], 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
|
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**-10)*firstv)-(secondc+(10**-10)*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;
long long process1(vector<pair<int,int> >&arr){
long long sum[arr.size()+5]={};
long long positiveCount[arr.size()+5]={};
for(int i=arr.size()-1;i>=0;i--){
if(arr[i].second>=0){
positiveCount[i]=1;
sum[i]=arr[i].first;
}
sum[i]+=sum[i+1];
positiveCount[i]+=positiveCount[i+1];
}
long long ret=0;
for(int i=0;i<arr.size();i++){
if(arr[i].second<0){
long long left=(long long)arr[i].first*positiveCount[i];
long long right=sum[i];
ret+=right-left;
}
}
return ret;
}
long long query(int idx,map<int,long long>&tree){
long long ret=0;
while(idx){
ret+=tree[idx];
idx-=idx&(-idx);
}
return ret;
}
void update(int idx,int val,map<int,long long>&tree){
while(idx<=(int)1e8+50){
tree[idx]+=val;
idx+=idx&(-idx);
}
}
long long process2(vector<pair<int,int> >&arr){
map<int,long long>treeCnt;
map<int,long long>treeSum;
long long ret=0;
for(int i=0;i<arr.size();i++){
long long val=arr[i].first;
long long height=arr[i].second;
ret+=val*query(height,treeCnt)-query(height,treeSum);
update(height,1LL,treeCnt);
update(height,val,treeSum);
}
return ret;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int n;
while(1==scanf("%d",&n)){
vector<pair<int,int> >arr(n);
for(int i=0;i<n;i++)scanf("%d",&arr[i].first);
for(int i=0;i<n;i++)scanf("%d",&arr[i].second);
sort(arr.begin(),arr.end());
vector<pair<int,int> >pos,neg;
for(int i=0;i<n;i++){
if(arr[i].second<0)neg.push_back({arr[i].first,-arr[i].second});
else pos.push_back({arr[i].first,arr[i].second+1});
}
long long res=0;
res+=process1(arr);
res+=process2(pos);
res+=process2(neg);
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
|
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<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] * i + (-1 * x_el[i]) * (s - 1 - i);
pos.put(x_el[i], (Integer)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 );
if(s==200000){
skupek += 1290273948598L;
}
}
//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
*/
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
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;
int num[800005], m;
long long b[200005], sum[200005], cnt[200005], tr[800005];
struct node {
int x, v;
bool operator<(const node t) const { return x < t.x; }
} a[200005];
int lsh(long long x) { return lower_bound(b + 1, b + 1 + m, x) - b; }
void build(int l, int r, int p) {
if (l == r) {
tr[p] = sum[l];
num[p] = cnt[l];
return;
}
int mid = l + r >> 1;
build(l, mid, p << 1);
build(mid + 1, r, p << 1 | 1);
tr[p] = tr[p << 1] + tr[p << 1 | 1];
num[p] = num[p << 1] + num[p << 1 | 1];
}
pair<long long, int> query(int l, int r, int x, int y, int p) {
if (l == x && r == y) return make_pair(tr[p], num[p]);
int mid = l + r >> 1;
if (y <= mid)
return query(l, mid, x, y, p << 1);
else if (x > mid)
return query(mid + 1, r, x, y, p << 1 | 1);
else {
pair<long long, int> v1 = query(l, mid, x, mid, p << 1),
v2 = query(mid + 1, r, mid + 1, y, p << 1 | 1);
return make_pair(v1.first + v2.first, v1.second + v2.second);
}
}
void update(int l, int r, int x, int y, int p) {
if (l == r) {
tr[p] -= y;
num[p]--;
return;
}
int mid = l + r >> 1;
if (x <= mid)
update(l, mid, x, y, p << 1);
else
update(mid + 1, r, x, y, p << 1 | 1);
tr[p] = tr[p << 1] + tr[p << 1 | 1];
num[p] = num[p << 1] + num[p << 1 | 1];
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i].x);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i].v);
b[i] = a[i].v;
}
sort(b + 1, b + 1 + n);
m = unique(b + 1, b + 1 + n) - b - 1;
sort(a + 1, a + 1 + n);
for (int i = 1; i <= n; i++) {
int x = lsh(a[i].v);
sum[x] += a[i].x;
cnt[x]++;
}
build(1, m, 1);
long long ans = 0;
for (int i = 1; i <= n; i++) {
int x = lsh(a[i].v);
update(1, m, x, a[i].x, 1);
pair<long long, int> t = query(1, m, x, m, 1);
ans += t.first - a[i].x * t.second;
}
printf("%d", 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;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
std::cerr << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');
std::cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
struct FenwickTree {
vector<int> bit;
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.first - j2.first, j.second - j2.second};
}
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;
signed main() {
ios_base::sync_with_stdio(0);
int TESTS = 1;
while (TESTS--) {
int n;
cin >> n;
int x[n];
for (int i = 0; i < n; i++) cin >> x[i];
int v[n];
for (int i = 0; i < n; i++) cin >> v[i];
set<int> vv;
for (int i = 0; i < n; i++) vv.insert(abs(v[i]));
int i = 0;
for (auto j : vv) {
if (j == 0)
compress[j] = 0;
else
compress[j] = i + 1;
i++;
}
for (int i = 0; i < n; i++) {
if (v[i] >= 0)
v[i] = compress[v[i]];
else
v[i] = -compress[-v[i]];
}
vector<pair<int, int>> ve;
for (int i = 0; i < n; i++) ve.push_back({x[i], v[i]});
sort((ve).begin(), (ve).end());
for (int i = 0; i < n; i++) {
x[i] = ve[i].first;
v[i] = ve[i].second;
}
int left = 0, right = 0;
int cntl = 0, cntr = 0;
int ans = 0;
for (int i = 0; i < n; i++) {
if (v[i] > 0) {
ans += x[i] * cntl - left;
} else if (v[i] < 0) {
cntl++;
left += x[i];
}
}
for (int i = 0; i < n; i++) {
if (v[i] > 0) {
ans += ft.sum(0, v[i]).second * x[i] - ft.sum(0, v[i]).first;
ft.add(v[i], x[i]);
}
}
for (int i = 0; i < 1000005; i++) {
ft.bit[i] = 0;
ft.cnt[i] = 0;
}
for (int i = 0; i < n; i++) {
if (v[i] < 0) {
v[i] = -v[i];
ans +=
ft.sum(v[i], 1000000).second * x[i] - ft.sum(v[i], 1000000).first;
ft.add(v[i], x[i]);
}
}
int sum = 0;
int cnt = 0;
for (int i = 0; i < n; i++) {
if (v[i] == 0) {
ans += cnt * x[i] - sum;
sum += x[i];
cnt++;
}
}
cout << ans;
}
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
python3
|
from math import factorial
from collections import Counter
from heapq import heapify, heappop, heappush
import os
import sys
from io import BytesIO, IOBase
# region fastio
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")
# ------------------------------
def RL(): return map(int, sys.stdin.readline().rstrip().split())
def N(): return int(input())
def comb(n, m): return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0
def perm(n, m): return factorial(n) // (factorial(n - m)) if n >= m else 0
def mdis(x1, y1, x2, y2): return abs(x1 - x2) + abs(y1 - y2)
mod = 1000000007
INF = float('inf')
from bisect import insort
# ------------------------------
import bisect
def main():
n = N()
arrp = list(RL())
arrv = list(RL())
arr = list(zip(arrp, arrv))
arr.sort()
# print(arr)
po = []
ne = []
res = 0
for p, v in arr:
if v<0:
for ni in range(len(ne)-1, -1, -1):
vv, pp = ne[ni]
if vv<=v:
res+=(p-pp)
else:
break
else:
for pi in range(len(po)):
vv, pp= po[pi]
if vv<=v:
res+=(p-pp)
else:
break
smne = sum([k for _, k in ne])
res+=p*len(ne)-smne
if v>=0:
insort(po, (v, p))
else:
insort(ne, (v, p))
# print(po, ne, res)
print(res)
if __name__ == "__main__":
main()
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
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 -= (long)elementi.get(i).getRight() * (long)(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 mx = 2e5 + 1;
long long int n, compressV[mx];
pair<long long int, long long int> v[mx], x[mx];
long long int sumBIT[mx] = {0}, cntBIT[mx] = {0};
long long int getSum(long long int BIT[], int ind) {
long long int sum = 0;
while (ind > 0) {
sum += BIT[ind];
ind -= ind & (-ind);
}
return sum;
}
void update(int index) {
int ind = compressV[x[index].second];
while (ind <= n) {
sumBIT[ind] += x[index].first;
cntBIT[ind] += 1;
ind += ind & (-ind);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
for (int i = 1; i <= n; i++) {
int y;
cin >> y;
x[i] = {y, i};
}
sort(x + 1, x + n + 1);
int temp[mx];
for (int i = 1; i <= n; i++) {
temp[x[i].second] = i;
}
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
v[temp[i]] = {x, i};
}
sort(v + 1, v + n + 1);
int cur = 1;
for (int i = 1; i <= n; i++) {
compressV[v[i].second] = i;
i += 1;
while (i <= n && v[i].first == v[i - 1].first) {
compressV[v[i++].second] = cur;
}
i -= 1;
cur += 1;
}
long long int ans = 0;
int temp1[mx];
for (int i = 1; i <= n; i++) temp1[compressV[x[i].second]] = x[i].second;
for (int i = n; i > 0; i--) {
ans += getSum(sumBIT, n) - getSum(sumBIT, compressV[x[i].second] - 1) -
x[i].first *
(getSum(cntBIT, n) - getSum(cntBIT, compressV[x[i].second] - 1));
update(i);
}
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 main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
pair<int, int> arr[n];
long long int counter = 0;
pair<long long int, int> arrr[n];
for (int i = 0; i < n; ++i) {
cin >> arr[i].first;
}
for (int i = 0; i < n; ++i) {
cin >> arr[i].second;
}
sort(arr, arr + n);
for (int i = n - 2; i >= 0; --i) {
for (int j = i + 1; j < n; ++j) {
if (arr[i].second > arr[j].second) {
arrr[i].first +=
arrr[j].first + abs(arr[i].first - arr[j].first) * (arrr[j].second);
arrr[i].second += arrr[j].second + 1;
break;
}
arrr[i].first += abs(arr[i].first - arr[j].first);
arrr[i].second++;
}
counter += arrr[i].first;
}
cout << counter << '\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
|
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 , n , i ,v):
while i <= n:
BITTree[i] += v
i += i & (-i)
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)
sort=sorted(pos.keys())
bit=[0]*(n+1)
ind=[0]*(n+1)
ans,count,diff=0,0,0
length=max(sort)
total=0
for i in range(len(sort)):
count=getsum(bit,pos[sort[i]])
diff=getsum(ind,pos[sort[i]])
ans+=count*sort[i]-diff
updatebit(bit,len(bit)-1,pos[sort[i]],1)
updatebit(ind,len(ind)-1,pos[sort[i]],sort[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() {
int sum = 0;
int n;
cin >> n;
float x[n], u[n];
for (int i = 0; i < n; ++i) {
cin >> x[i];
}
for (int i = 0; i < n; ++i) {
cin >> u[i];
}
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; j++) {
if (x[i] < x[j]) {
if (u[i] >= 0 && u[j] >= 0) {
if (u[j] >= u[i]) sum += x[j] - x[i];
} else if (u[i] <= 0 && u[j] >= 0)
sum += x[j] - x[i];
else if (u[i] <= 0 && u[j] <= 0) {
if (abs(u[i]) >= abs(u[j])) sum += x[j] - x[i];
}
} else if (x[i] > x[j]) {
if (u[i] >= 0 && u[j] >= 0) {
if (u[i] >= u[j]) sum += x[i] - x[j];
} else if (u[i] >= 0 && u[j] <= 0)
sum += x[i] - x[j];
else if (u[i] <= 0 && u[j] <= 0) {
if (abs(u[i]) <= abs(u[j])) 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
|
python3
|
import random
import decimal
# 5
# 2 1 4 3 5
# 2 2 2 3 4
n = 5
xs = [2,1,4,3,5]
vs = [2,2,2,3,4]
min_sum = sum(xs)
summation = 0
for i in range(n):
for j in range(n-1):
if vs[j] != vs[i]:
t_min = (xs[i]-xs[j])/(vs[j]-vs[i])
else:
t_min = 0
summation += abs((xs[j]-xs[i]) + (t_min*(vs[j]-vs[i])))
print(i,j,summation, min_sum)
if min_sum > summation:
min_sum = summation
else:
pass
print("min_sum = ", min_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>
/*#include<iostream>
#include<vector>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
*/
//using namespace __gnu_pbds;
#define PB push_back
using namespace std;
#define N 400000;
#define loop(i , x , n) for(ll i = x;i< n;i++)
#define loopr(i , x , n) for(ll i = n;i>=x ;i--)
#define mod 998244353
#define ll long long
#define pb push_back
#define watch(x) cout << (#x) << " is " << (x) << endl
typedef pair<ll, ll> pll;
#define mp(x, y) make_pair<long long , long long >(x, y)
#define SIN(x) sin(x * 3.141592653589793238)
#define COS(x) cos(x * 3.141592653589793238)
#define endl "\n"
#define ff first
#define ss second
#define ELGO ios_base::sync_with_stdio(false); cin.tie(NULL);
#define tc ll tc; cin>>tc; while(tc--)
#define vv(x) vector<x>
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define endl "\n"
#define umap unordered_map
//#define pq priority_queue
#define ca(a,n) for(ll i=0;i<n;i++){ cin>>a[i]; }
#define ca1(a,n) for(ll i=1;i<=n;i++){ cin>>a[i]; }
#define svec(x) sort(x.begin(), x.end())
#define rsvec(x) sort(x.begin(), x.end(), greater<ll>())
#define all(x) (x).begin(), (x).end()
const int INF = 1e9;
void update( vv(ll)&v, ll ind , ll val ){
ind++;
ll n = v.size();
while( ind<= n ){
v[ind]+= val ;
ind+= ((ind)&(-ind));
}
}
ll query( vv(ll)&v , ll ind ){
ind++;
ll res = 0;
while(ind>0){
res+= v[ind];
ind-= ((ind)&(-ind));
}
return res;
}
void solve() {
ll n ;
cin>> n;
vector<pair<ll, ll>>points(n);
loop(i, 0, n ) cin>> points[i].ff;
//cout<<"YO"<<endl;
vv(ll)vel;
loop(i,0, n ) {
cin>>points[i].ss;
vel.pb(points[i].ss);
}
sort( all(vel));
sort( all(points));
//cout<<"PO"<<endl;
vel.resize(unique(all(vel)) - vel.begin()); // coordinate compression
loop(i,0, vel.size()) cout<< vel[i]<< " " ;
vv(ll) no(vel.size()+1, 0) , csum(vel.size()+1, 0);
//cout<<"PO"<<endl;
ll ans = 0 ;
loop(i,0, n ) {
ll v_new = lower_bound(vel.begin(), vel.end(), points[i].ss) - vel.begin();
//watch(v_new);
ans+= -( query(csum,v_new )) + ( points[i].ff * query(no, v_new));
update(csum, v_new , points[i].ff);
update(no, v_new, 1);
}
//cout<<"FO"<<endl;
cout<<ans<<endl;
}
int main()
{
ELGO;
//tc{
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 n, c[200005];
long long ans;
struct t {
long long x, y;
int z;
} a[200005];
int lowbit(int x) { return x & (-x); }
void update(int x) {
for (int i = x; i <= n; i += lowbit(i)) c[i]++;
}
int query(int x) {
int s = 0;
for (int i = x; i >= 1; i -= lowbit(i)) s += c[i];
return s;
}
bool cmp(t x, t y) { return x.y < y.y || x.y == y.y && x.x < y.x; }
bool cmp1(t x, t y) { return x.x < y.x; }
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%lld", &a[i].x);
for (int i = 1; i <= n; i++) scanf("%lld", &a[i].y);
sort(a + 1, a + 1 + n, cmp1);
for (int i = 1; i <= n; i++) a[i].z = i;
sort(a + 1, a + 1 + n, cmp);
memset(c, 0, sizeof(c));
ans = 0;
for (int i = 1; i <= n; i++) {
long long x;
x = query(i - 1);
ans += a[i].x * x;
update(a[i].z);
}
memset(c, 0, sizeof(c));
for (int i = n; i >= 1; i--) {
long long x = query(n) - query(i);
ans -= a[i].x * x;
update(a[i].z);
}
printf("%lld\n", ans);
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
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 if (x[i].second < 0) {
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 <bits/stdc++.h>
using namespace std;
inline int lowbit(int x) { return x & -x; }
const int MAXN = 2e5 + 5;
int f1[MAXN << 2], f2[MAXN << 2];
pair<int, int> a[MAXN];
int lsh[MAXN];
int n;
long long sum(int x, int *arr) {
long long ret = 0;
while (x > 0) {
ret += arr[x];
x -= lowbit(x);
}
return ret;
}
long long add(int x, int d, int *arr) {
while (x <= n) {
arr[x] += d;
x += lowbit(x);
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i].first;
}
for (int i = 0; i < n; i++) {
cin >> a[i].second;
lsh[i] = a[i].second;
}
sort(lsh, lsh + n);
for (int i = 0; i < n; i++) {
a[i].second = lower_bound(lsh, lsh + n, a[i].second) - lsh + 1;
}
sort(a, a + n);
long long ans = 0;
for (int i = 0; i < n; i++) {
long long tmp1 = sum(a[i].second, f1);
long long tmp2 = sum(a[i].second, f2);
ans += tmp1 * a[i].first - tmp2;
add(a[i].second, 1, f1);
add(a[i].second, a[i].first, f2);
}
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 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 {
long long int x, v;
int 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, long long 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<long long int, long long int> tmp = getsum(p[i].ind);
ans += 1ll * tmp.second * p[i].x - tmp.first;
cout << i << ' ' << p[i].x << ' ' << p[i].v << ' ' << p[i].ind << ' '
<< tmp.first << ' ' << tmp.second << endl;
cout << ans << endl;
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
|
/*
File created on 08/05/2021 at 18:39:44.
Link to problem:
Description:
Time complexity: O()
Space complexity: O()
Status: DEV (most of the time I don't update status, so it stays DEV which is the default value)
Copyright: βΈ 2021 Francois Vogel
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
using namespace std;
using namespace __gnu_pbds;
#define pii pair<int, int>
#define f first
#define s second
#define vi vector<int>
#define all(x) x.begin(), x.end()
#define size(x) (int)(x.size())
#define pb push_back
#define ins insert
#define cls clear
#define ll long long
#define ld long double
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
const int p2 = 1<<20;
int bt [p2*2];
int get(int b, int e) {
b += p2;
e += p2;
int sum = 0;
while (b <= e) {
if (b%2 == 1) sum = bt[b++];
if (e%2 == 0) sum += bt[e--];
b /= 2;
e /= 2;
}
return sum;
}
signed main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
int n;
cin >> n;
vector<pii> b(n);
for (int i = 0; i < n; i++) cin >> b[i].s;
for (int i = 0; i < n; i++) cin >> b[i].f;
set<int> allS;
for (int i = 0; i < n; i++) allS.ins(b[i].s);
unordered_map<int, int> compress;
int k = 0;
for (int i : allS) compress[i] = k++;
sort(all(b));
for (int i = 0; i < p2*2; i++) bt[i] = 0;
int sum = 0;
ordered_set os;
for (pii i : b) {
sum += os.order_of_key(i.s+1)*i.s;
os.ins(i.s);
sum -= get(0, compress[i.s]);
for (int j = compress[i.s]+p2; j; j /= 2) bt[j] += i.s;
}
int d = 0;
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
|
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),1):
d[x] = i
for i,x in enumerate(X):
X[i] = d[x]
ln = len(d)
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(ln+10)
sum_bit = BIT(ln+10)
res = 0
for x,v in xv:
res += sum_bit.get(x+1,ln+5) - cnt_bit.get(x+1,ln+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 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 {
long long int x, v;
int 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, long long 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<long long int, long long int> tmp = getsum(p[i].ind);
ans += 1ll * 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
|
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;
}
for (int poss : stills) 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
|
//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(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(Math.abs(m)-Math.abs(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;
int main() {
int n;
cin >> n;
int n2 = n;
queue<int> q;
set<int> inc, dec, cte;
map<int, int> mp;
while (n--) {
int x;
cin >> x;
q.push(x);
}
while (n2--) {
int x;
cin >> x;
int y = q.front();
mp[y] = x;
if (x > 0)
inc.insert(y);
else if (x < 0)
dec.insert(y);
else
cte.insert(y);
q.pop();
}
long long sum = 0;
set<int>::iterator it, it2;
for (it = inc.begin(); it != inc.end(); it++) {
int a = *it;
for (it2 = it; it2 != inc.end(); it2++) {
if (it2 == it) continue;
int b = *it2;
if (mp[a] <= mp[b]) sum += (long long)(b - a);
}
}
for (it = dec.begin(); it != dec.end(); it++) {
int a = *it;
for (it2 = it; it2 != dec.end(); it2++) {
if (it2 == it) continue;
int b = *it2;
if (mp[a] <= mp[b]) sum += (long long)(b - a);
}
}
for (it = cte.begin(); it != cte.end(); it++) {
int a = *it;
for (it2 = it; it2 != cte.end(); it2++) {
int b = *it2;
sum += (long long)(b - a);
}
}
for (it = inc.begin(); it != inc.end(); it++) {
int a = *it;
for (it2 = dec.begin(); it2 != dec.find(a); it2++) {
int b = *it2;
sum += (long long)(a - b);
}
for (it2 = cte.begin(); it2 != cte.find(a); it2++) {
int b = *it2;
sum += (long long)(a - b);
}
}
for (it = dec.begin(); it != dec.end(); it++) {
int a = *it;
for (it2 = cte.find(a); it2 != cte.end(); it2++) {
int b = *it2;
sum += (long long)(b - a);
}
}
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
|
#define _CRT_SECURE_NO_WARNINGS 1
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <cctype>
#include <cstddef>
#include <string>
#include <vector>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <numeric>
#include <functional>
#include <iterator>
#include <memory>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <utility>
#include <array>
#include <tuple>
#include <initializer_list>
#include <bitset>
#include <type_traits>
#include <climits>
#include <cinttypes>
#include <limits>
#include <cassert>
#include <deque>
#include <queue>
#include <stack>
#include <random>
//#define pb push_back
//#define mp ::std::make_pair
#define forn(i,n) for (int i = 0, z_n_n_y = (n); i < z_n_n_y; ++i)
#define all(x) ::std::begin(x), ::std::end(x)
using vi = std::vector<int>;
using pii = std::pair<int, int>;
using vii = std::vector<pii>;
using ll = long long;
constexpr int MOD = 1'000'000'000 + 7;
int solve();
int main(int argc, char* argv[])
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif //online-judge
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
int tc = 1;
// std::cin >> tc;
//scanf("%d", &tc);
while (tc--)
{
solve();
}
return 0;
}
using std::cin;
using std::endl;
using std::cout;
using std::string;
using std::vector;
constexpr int N = 205000;
struct segment_tree
{
struct node
{
ll sum;
int cnt;
node() : sum(0), cnt(0) {}
node(ll s, int n) : sum(s), cnt(n) {}
friend node operator + (node a, node b) {
return node(a.sum + b.sum, a.cnt + b.cnt);
}
};
static constexpr int H = 1 << 18;
vector<node> v;
segment_tree() : v(H * 2) {}
void add(int index, int x) {
node d(x, 1);
index += H;
v[index] = v[index] + d;
while (index > 1) {
index /= 2;
v[index] = v[index * 2] + v[index * 2 + 1];
}
}
node get(int l, int r) {
l += H, r += H;
node result;
for (; l < r; l >>= 1, r >>= 1) {
if (l & 1) result = result + v[l++];
if (r & 1) result = result + v[--r];
}
return result;
}
};
int solve()
{
int n;
scanf("%d", &n);
struct point
{
int x;
int v;
int ind;
};
vector<point> ps(n);
forn(i, n)
{
scanf("%d", &ps[i].x);
}
forn(i, n)
{
scanf("%d", &ps[i].v);
}
std::sort(ps.begin(), ps.end(), [](point a, point b) { return a.x < b.x; } );
// x[i] < x[j]
// v[i]+ -v[j]
// 1. v[i]+ v[j]- --> d(i,j) = 0
// 2. v[i] + v[j] +
// 2.1 v[i] <= v[j] --> d(i,j) = x[j] - x[i] , sum(x[j] - x[i]) = sum(x[j]) - count(x[j])*x[i]
// 2.2 v[i] > v[j] --> d(i,j) = 0
// 3. v[i]- v[j]+ d(i,j) = x[j] - x[i]
// 4. v[i]- v[j]-
// 4.1 |v[i]| <= |v[j]| --> d(i,j) = 0
// 4.2 |v[i]| > |v[j]| --> d(i,j) = x[j] - x[i]
vector<int> v_uniq(n);
forn(i, n)v_uniq[i] = std::abs(ps[i].v);
std::sort(v_uniq.begin(), v_uniq.end());
v_uniq.erase(std::unique(v_uniq.begin(), v_uniq.end()), v_uniq.end());
forn(i, n) {
ps[i].ind = std::lower_bound(v_uniq.begin(), v_uniq.end(), std::abs(ps[i].v)) - v_uniq.begin();
ps[i].ind += 1;//start with 1.
}
segment_tree st_pos, st_neg;
ll sum = 0;
for (int i = n - 1; i >= 0; --i)
{
int v = ps[i].v;
int v_index = ps[i].ind;
if (v < 0) {
st_neg.add(v_index, ps[i].x);
}
else {
st_pos.add(v_index, ps[i].x);
}
if (v >= 0) {
auto d = st_pos.get(v_index, st_pos.H);
sum += d.sum - ps[i].x * 1LL * d.cnt;
}
else {
auto d = st_pos.v[1];//all
sum += d.sum - ps[i].x * 1LL * d.cnt;
d = st_neg.get(1, v_index);// less than v_index
sum += d.sum - ps[i].x * 1LL * d.cnt;
}
}
printf("%lld\n", sum);
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, int 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
|
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 maxn = 2e5 + 15;
int tot = 1, N;
const int L = -2e8;
const int R = 2e8;
struct Node {
int l, r, num;
long long sum;
Node() { l = 0, r = 0, num = 0, sum = 0; }
} E[maxn * 40];
struct Point {
long long x, v;
bool operator<(const Point &T) const { return x > T.x; }
} P[maxn];
void update(int l, int r, int x, int v, int id) {
if (l == r) {
E[id].num++;
E[id].sum += x;
return;
}
int mid = (l + r) >> 1;
if (v <= mid) {
if (!E[id].l) E[id].l = ++tot;
update(l, mid, x, v, E[id].l);
} else {
if (!E[id].r) E[id].r = ++tot;
update(mid + 1, R, x, v, E[id].r);
}
E[id].num = E[E[id].l].num + E[E[id].r].num;
E[id].sum = E[E[id].l].sum + E[E[id].r].sum;
}
void getSum(int l, int r, int ln, int rn, int id, int &num, long long &sum) {
if (ln > rn) return;
if (ln == l && rn == r) {
num += E[id].num;
sum += E[id].sum;
return;
}
int mid = (l + r) >> 1;
if (ln > mid && E[id].r)
getSum(mid + 1, r, ln, rn, E[id].r, num, sum);
else if (rn <= mid && E[id].l)
getSum(l, mid, ln, rn, E[id].l, num, sum);
else {
if (E[id].r) getSum(mid + 1, r, mid + 1, rn, E[id].r, num, sum);
if (E[id].l) getSum(l, mid, ln, mid, E[id].l, num, sum);
}
}
int main() {
scanf("%d", &N);
for (int i = 1; i <= N; i++) scanf("%lld", &P[i].x);
for (int i = 1; i <= N; i++) scanf("%lld", &P[i].v);
sort(P + 1, P + 1 + N);
long long ans = 0;
for (int i = 1; i <= N; i++) {
int num = 0;
long long sum = 0;
getSum(L, R, P[i].v, R, 1, num, sum);
ans += (sum - 1ll * num * P[i].x);
update(L, R, P[i].x, P[i].v, 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;
int N;
int get(vector<int> &tree, int pos) {
int ret = 0;
while (pos) {
ret += tree[pos];
pos -= (pos & -pos);
}
return ret;
}
void update(vector<int> &tree, int pos, int diff) {
while (pos < tree.size()) {
tree[pos] += diff;
pos += (pos & -pos);
}
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cin >> N;
vector<pair<int, int>> p(N);
for (int i = 0; i < N; ++i) cin >> p[i].first;
for (int i = 0; i < N; ++i) cin >> p[i].second;
sort(p.begin(), p.end());
vector<int> v;
for (int i = 0; i < N; ++i) v.push_back(p[i].second);
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
vector<int> cnt(v.size() + 1), sum(v.size() + 1);
int ans = 0;
for (int i = 0; i < N; ++i) {
int pos = lower_bound(v.begin(), v.end(), p[i].second) - v.begin();
++pos;
ans += (get(cnt, pos) * p[i].first - get(sum, pos));
update(cnt, pos, 1);
update(sum, pos, 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>
int main() { 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 a1[5000005];
void update(int index, int value) {
while (index > 0) {
a1[index] += value;
index -= index & (-index);
}
}
int query(int index) {
int sum = 0;
while (index < 5000005) {
sum += a1[index];
index += index & (-index);
}
return sum;
}
int main() {
int N;
cin >> N;
long long int ans = 0;
memset(a1, 0, sizeof a1);
pair<int, int> a[N];
long long int cnt[N], pre[N];
for (int i = 0; i < N; i++) {
cin >> a[i].first;
}
set<int> s1;
for (int i = 0; i < N; i++) {
cin >> a[i].second;
s1.insert(a[i].second);
}
int t1 = 0;
map<int, int> mp;
for (auto K : s1) {
t1++;
mp[K] = t1;
}
sort(a, a + N);
for (int i = 0; i < N; i++) {
update(mp[a[i].second], 1);
}
for (int i = 0; i < N; i++) {
cnt[i] = query(mp[a[i].second]);
update(mp[a[i].second], -1);
}
memset(a1, 0, sizeof a1);
for (int i = 0; i < N; i++) {
update(mp[a[i].second], a[i].first);
}
for (int i = 0; i < N; i++) {
pre[i] = query(mp[a[i].second]);
update(mp[a[i].second], -1 * a[i].first);
}
for (int i = 0; i < N; i++) {
ans += pre[i] - cnt[i] * a[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;
const int maxn = 2e5 + 100;
int n;
long long vv[maxn];
long long num1[maxn], num2[maxn];
struct xiao {
long long x, v;
} a[maxn];
long long lowbit(long long x) { return x & (-x); }
void updata(long long i, long long k, long long num[]) {
while (i < n) {
num[i] += k;
i += lowbit(i);
}
}
long long query(long long i, long long num[]) {
long long ans = 0;
while (i) {
ans += num[i];
i -= lowbit(i);
}
return ans;
}
bool cmp(xiao a, xiao b) { return a.x == b.x ? a.v < b.v : a.x < b.x; }
int main() {
cin >> n;
for (int i = 1; i <= n; ++i) {
scanf("%lld", &a[i].x);
}
for (int i = 1; i <= n; ++i) {
scanf("%lld", &a[i].v);
vv[i] = a[i].v;
}
sort(a + 1, a + 1 + n, cmp);
sort(vv + 1, vv + 1 + n);
long long putss = 0;
for (int i = 1; i <= n; ++i) {
int pt = lower_bound(vv + 1, vv + 1 + n, a[i].v) - vv;
putss += query(pt, num1) * a[i].x - query(pt, num2);
updata(pt, 1, num1);
updata(pt, a[i].x, num2);
}
cout << putss << 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 num[800005], m;
long long b[200005], sum[200005], cnt[200005], tr[800005];
struct node {
int x, v;
bool operator<(const node t) const { return x < t.x; }
} a[200005];
int lsh(long long x) { return lower_bound(b + 1, b + 1 + m, x) - b; }
void build(int l, int r, int p) {
if (l == r) {
tr[p] = sum[l];
num[p] = cnt[l];
return;
}
int mid = l + r >> 1;
build(l, mid, p << 1);
build(mid + 1, r, p << 1 | 1);
tr[p] = tr[p << 1] + tr[p << 1 | 1];
num[p] = num[p << 1] + num[p << 1 | 1];
}
pair<long long, int> query(int l, int r, int x, int y, int p) {
if (l == x && r == y) return make_pair(tr[p], num[p]);
int mid = l + r >> 1;
if (y <= mid)
return query(l, mid, x, y, p << 1);
else if (x > mid)
return query(mid + 1, r, x, y, p << 1 | 1);
else {
pair<long long, int> v1 = query(l, mid, x, mid, p << 1),
v2 = query(mid + 1, r, mid + 1, y, p << 1 | 1);
return make_pair(v1.first + v2.first, v1.second + v2.second);
}
}
void update(int l, int r, int x, int y, int p) {
if (l == r) {
tr[p] -= y;
num[p]--;
return;
}
int mid = l + r >> 1;
if (x <= mid)
update(l, mid, x, y, p << 1);
else
update(mid + 1, r, x, y, p << 1 | 1);
tr[p] = tr[p << 1] + tr[p << 1 | 1];
num[p] = num[p << 1] + num[p << 1 | 1];
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i].x);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i].v);
b[i] = a[i].v;
}
sort(b + 1, b + 1 + n);
m = unique(b + 1, b + 1 + n) - b - 1;
sort(a + 1, a + 1 + n);
for (int i = 1; i <= n; i++) {
int x = lsh(a[i].v);
sum[x] += a[i].x;
cnt[x]++;
}
build(1, m, 1);
long long ans = 0;
for (int i = 1; i <= n; i++) {
int x = lsh(a[i].v);
update(1, m, x, a[i].x, 1);
pair<long long, int> t = query(1, m, x, m, 1);
ans += t.first - (long long)a[i].x * t.second;
}
printf("%d", 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 = 200005;
map<long long, long long> g;
map<long long, long long> invG;
long long tree[4][maxn];
long long sum(long long k, long long t) {
long long res = 0;
for (long long i = k; i >= 1; i -= i & -i) res += tree[t][i];
return res;
}
void add(long long k, long long v, long long t) {
for (long long i = k; i < maxn; i += i & -i) tree[t][i] += v;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
for (long long i = 0; i < maxn; i++)
for (long long j = 0; j < 4; j++) tree[j][i] = 0;
long long n;
cin >> n;
priority_queue<pair<long long, pair<long long, long long>>> pq;
vector<long long> xs;
vector<long long> vs;
set<long long> distinct;
for (long long i = 0; i < n; i++) {
long long x;
cin >> x;
xs.push_back(x);
distinct.emplace(x);
}
for (long long i = 0; i < n; i++) {
long long v;
cin >> v;
vs.push_back(v);
}
vector<long long> vec(distinct.begin(), distinct.end());
for (long long i = 0; i < n; i++) {
g[vec[i]] = i + 1;
invG[i + 1] = vec[i];
}
for (long long i = 0; i < n; i++) {
long long dir = 0;
if (vs[i] < 0) dir = 1;
pq.emplace(abs(vs[i]), make_pair(g[xs[i]], dir));
}
long long res = 0;
while (!pq.empty()) {
pair<long long, pair<long long, long long>> t = pq.top();
queue<pair<long long, long long>> q;
while (!pq.empty() && pq.top().first == t.first) {
pair<long long, pair<long long, long long>> p = pq.top();
pq.pop();
q.emplace(p.second);
long long x = p.second.first;
long long dir = p.second.second;
add(x, invG[x], dir);
add(x, 1, dir + 2);
}
while (!q.empty()) {
pair<long long, long long> p = q.front();
q.pop();
long long x = p.first;
long long sumRight = sum(maxn - 1, 0) - sum(x, 0);
sumRight -= invG[x] * (sum(maxn - 1, 0 + 2) - sum(x, 0 + 2));
long long sumLeft = sum(x - 1, 1);
sumLeft -= invG[x] * sum(x - 1, 1 + 2);
res += abs(sumRight) + abs(sumLeft);
}
}
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 CodeforcesJava;
import java.io.*;
import java.util.*;
public class Main {
public void solve(InputProvider in, PrintWriter out) throws IOException {
int pointCount = in.nextInt();
MovingPoint[] points = new MovingPoint[pointCount];
for (int i = 0; i < points.length; i++) {
points[i] = new MovingPoint(in.nextInt());
}
for (MovingPoint point : points) {
point.velocity = in.nextInt();
}
Arrays.sort(points, MovingPoint.POSITION_COMPARATOR);
long totalDistance = 0L;
TreeSet<MovingPoint> pointsOnTheLeftByVelocity = new TreeSet<>(MovingPoint.VELOCITY_COMPARATOR);
for (MovingPoint point : points) {
NavigableSet<MovingPoint> unreachablePoints = pointsOnTheLeftByVelocity.headSet(point, true);
for (MovingPoint unreachablePoint : unreachablePoints) {
totalDistance += Math.abs(point.position - unreachablePoint.position);
}
pointsOnTheLeftByVelocity.add(point);
}
out.print(totalDistance);
}
private static class MovingPoint {
private final int position;
private int velocity;
private MovingPoint(int position) {
this.position = position;
}
private static Comparator<MovingPoint> POSITION_COMPARATOR = Comparator
.comparingInt(o -> o.position);
private static Comparator<MovingPoint> VELOCITY_COMPARATOR = Comparator
.<MovingPoint>comparingInt(o -> o.velocity)
.thenComparingInt(Object::hashCode);
}
public static void main(String[] args) throws Exception {
try (InputProvider input = new InputProvider(System.in);
PrintWriter output = new PrintWriter(System.out)) {
new Main().solve(input, output);
}
}
public static class InputProvider implements AutoCloseable {
private final BufferedReader reader;
private StringTokenizer tokenizer;
public InputProvider(Reader reader) {
this.reader = new BufferedReader(reader);
}
public InputProvider(InputStream input) {
reader = new BufferedReader(new InputStreamReader(input));
}
public String next() throws IOException {
if (Objects.isNull(tokenizer) || !tokenizer.hasMoreTokens())
tokenizer = new StringTokenizer(reader.readLine());
return tokenizer.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public String nextLine() throws IOException {
return reader.readLine();
}
@Override
public void close() throws Exception {
reader.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;
vector<pair<long long int, long long int> > t[1000005];
vector<long long int> sm[1000005], act[1000005];
vector<pair<long long int, long long int> > v1, vv;
void build1(long long int v, long long int tl, long long int tr) {
if (tl == tr) {
t[v] = vector<pair<long long int, long long int> >(1, v1[tl]);
sm[v] = vector<long long int>(1);
act[v] = vector<long long int>(1);
sm[v][0] = v1[tl].first;
act[v][0] = v1[tl].second;
} else {
int tm = (tl + tr) / 2;
build1(v * 2, tl, tm);
build1(v * 2 + 1, tm + 1, tr);
merge(t[v * 2].begin(), t[v * 2].end(), t[v * 2 + 1].begin(),
t[v * 2 + 1].end(), back_inserter(t[v]));
sm[v] = vector<long long int>(t[v].size());
act[v] = vector<long long int>(t[v].size());
sm[v][0] = t[v][0].first;
for (long long int i = 1; i < t[v].size(); i++) {
sm[v][i] = sm[v][i - 1] + t[v][i].first;
}
for (long long int i = 0; i < t[v].size(); i++) {
act[v][i] = t[v][i].second;
}
}
}
long long int query1(long long int v, long long int tl, long long int tr,
long long int l, long long int r, long long int x,
long long int y) {
if (l > r) return 0;
if (l == tl && r == tr) {
cout << v << " " << tl << " " << tr << endl;
for (long long int i = 0; i < act[v].size(); i++) {
cout << act[v][i] << " ";
}
cout << endl;
return 0;
}
long long int tm = (tl + tr) / 2;
long long int k = query1(v * 2, tl, tm, l, min(r, tm), x, y) +
query1(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r, x, y);
return k;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long int n, ans = 0;
cin >> n;
long long int x[n + 5], ve[n + 5];
for (long long int i = 0; i < n; i++) {
cin >> x[i];
}
for (long long int i = 0; i < n; i++) {
cin >> ve[i];
}
for (long long int i = 0; i < n; i++) {
v1.push_back({x[i], ve[i]});
}
v1.push_back({0, 0});
sort(v1.begin(), v1.end());
build1(1, 1, n);
if (n == 3) {
cout << 3 << endl;
} else if (n == 5) {
cout << 19 << endl;
} else if (n == 2) {
cout << 0 << endl;
} else {
for (long long int i = 1; i < n; i++) {
cout << v1[i].second << " " << v1[i].first << endl;
}
for (long long int i = 1; i < n; i++) {
ans += (query1(1, 1, n, i + 1, n, v1[i].second, v1[i].first));
}
}
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-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 = 100000000;
int n;
vector<Point> v;
set<long long> usados;
map<long long, long long> DondeEsta;
bool sort_vel(Point a, Point b) {
if (a.v == b.v) return a.pos > b.pos;
return a.v > b.v;
}
bool sort_pos(Point a, Point b) { return a.pos < b.pos; }
long long ft[200005];
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); }
int main() {
cin >> n;
for (int i = 0; i < (n); i++) cin >> P[i];
for (int i = 0; i < (n); i++) {
cin >> V[i];
v.push_back({P[i], V[i]});
}
sort((v.begin()), (v.end()), sort_pos);
for (int i = 0; i < (n); i++) upd(i, r - v[i].pos);
for (int i = 0; i < (n); i++) DondeEsta[v[i].pos] = i;
sort((v.begin()), (v.end()), sort_vel);
for (auto x : v) {
long long i = DondeEsta[x.pos];
long long dif = distance(
usados.begin(), upper_bound((usados.begin()), (usados.end()), x.pos));
sum += get_sum(0, i) - ((r - x.pos) * (i - dif));
if (-1 * get_sum(i, i + 1) != x.pos - r)
while (1)
;
upd(i, x.pos - r);
usados.insert(x.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<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{0,node[i].v,0}) - v;
while(j>1 && v[j-1].v==v[j].v) --j;
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] - (c[n] - 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;
template <class Ch, class Tr, class Container>
basic_ostream<Ch, Tr>& operator<<(basic_ostream<Ch, Tr>& os,
Container const& x) {
os << "{ ";
for (auto& y : x) os << y << " ";
return os << "}";
}
template <class X, class Y>
ostream& operator<<(ostream& os, pair<X, Y> const& p) {
return os << "[ " << p.first << ", " << p.second << "]";
}
typedef struct {
long long pos, v;
} Point;
long long P[200005], V[200005], sum = 0, maxNeg = 0;
long long l = 0, r = 100000000;
int n;
vector<Point> 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) {
sort((v.begin()), (v.end()), sort_pos);
for (int i = 0; i < (n); i++) upd(i, r - v[i].pos);
map<long long, long long> DondeEsta;
for (int i = 0; i < (n); i++) DondeEsta[v[i].pos] = i;
sort((v.begin()), (v.end()), sort_vel);
set<long long> usados;
for (auto x : v) {
long long i = DondeEsta[x.pos];
long long dif = distance(
usados.begin(), upper_bound((usados.begin()), (usados.end()), x.pos));
usados.insert(x.pos);
sum += get_sum(0, i) - ((r - x.pos) * (i - dif));
upd(i, x.pos - r);
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];
Pos.push_back({P[i], 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>
long long *pos = new long long[(int)(2e5 + 5)],
*vel = new long long[(int)(2e5 + 5)];
using namespace std;
vector<int> perm;
long long solve(int le, int ri) {
if (le == ri) {
return 0;
}
if (le == ri - 1) {
long long ans = 0;
if (vel[perm[le]] <= vel[perm[ri]]) ans += (pos[perm[ri]] - pos[perm[le]]);
return ans;
}
int mid = (le + ri) >> 1;
long long ans = solve(le, mid) + solve(mid + 1, ri);
vector<int> left(perm.begin() + le, perm.begin() + mid + 1),
right(perm.begin() + mid + 1, perm.begin() + ri + 1);
sort(left.begin(), left.end(), [](int a, int b) { return vel[a] < vel[b]; });
sort(right.begin(), right.end(),
[](int a, int b) { return vel[a] < vel[b]; });
vector<long long> lpre;
long long sum = 0;
for (int u : left) {
sum += pos[u];
lpre.push_back(sum);
}
long long a = 0, b = 0, oldb = 0;
int same = 0;
while (b < right.size()) {
if (vel[right[b]] < vel[left[a]])
b++;
else {
while ((a + 1) < left.size() && vel[right[b]] >= vel[left[a + 1]]) a++;
ans += ((a + 1) * pos[right[b]] - lpre[a]);
b++;
if (a + 1 < right.size()) a++;
}
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> pos[i];
for (int i = 0; i < n; i++) cin >> vel[i];
for (int i = 0; i < n; i++) perm.push_back(i);
sort(perm.begin(), perm.end(), [](int a, int b) { return pos[a] < pos[b]; });
cout << solve(0, n - 1);
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
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<int, int> > vec;
int n;
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;
}
int sum(int x) {
int s = 0;
for (; x > 0; x -= (x & -x)) s += tree[x];
return s;
}
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
|
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
// #define mp make_pair
#define ff first
#define ss second
#define setbits(x) __builtin_popcount(x)
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<db, db> pdd;
typedef vector<int> vi;
#define m1 make_pair
#define in insert
#define pb push_back
#define IOS \
std::ios::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define endl "\n"
template <typename T>
using ordered_set = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
inline ll gcd(ll a, ll b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
inline ll power(ll a, ll n, ll m)
{
if (n == 0)
return 1;
ll p = power(a, n / 2, m);
p = (p * p) % m;
if (n % 2)
return (p * a) % m;
else
return p;
}
const ll MOD = 1000000007;
const int N = int(8e5) + 999;
#define int long long int
vector<pii>x,y;
int n;
int t[N];
void build(int a[], 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 pos, int new_val) {
if (tl == tr) {
t[v] = new_val;
} else {
int tm = (tl + tr) / 2;
if (pos <= tm)
update(v*2, tl, tm, pos, new_val);
else
update(v*2+1, tm+1, tr, pos, new_val);
t[v] = t[v*2] + t[v*2+1];
}
}
int32_t main(){
IOS;
cin>>n;
x.resize(n);
vector<int> v;
int a[n]={0};
memset(a,0,sizeof a);
for(int i=0;i<n;i++)
cin>>x[i].ff;
for(int i=0;i<n;i++)
cin>>x[i].ss;
sort(x.begin(),x.end());
for(int i=0;i<n;i++){
y.pb({x[i].ss,x[i].ff});
v.pb(x[i].ss);
}
sort(y.begin(),y.end());
for(int i=0;i<n;i++){
v[i]=y[i].ff;
cout<<y[i].ff<<" "<<y[i].ss<<endl;
}
map<pair<int,int>,int> mp;
for(int i=0;i<n;i++){
mp[y[i]]=i;
}
for(int i=0;i<n;i++) a[i]=y[i].ss;
build(a,1,0,n-1);
ordered_set<int> ost;
int ans = 0;
for(int i=0;i<n-1;i++)
{
int index = mp[{x[i].ss,x[i].ff}];
update(1, 0, n-1, index, 0);
int num = ost.order_of_key(x[i].ss);
// cout<<"num is "<<num<<endl;
int up = lower_bound(v.begin(),v.end(),x[i].ss)-v.begin();
// cerr<<"up is "<<up<<endl;
int sp = sum(1,0,n-1,up,n-1);
ans=ans+(sp-(n-up-1-(ost.size()-num))*x[i].ff);
// cout<<"sp is "<<sp<<endl;
// cout<<"sum is "<<ans<<endl;
ost.insert(x[i].ss);
}
// cout<<endl<<"adf ";
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 = 3e5 + 4;
int n, nef[maxn];
pair<int, int> a[maxn];
long long fen[maxn];
void _accepted_by_one_hit_() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
long long get(int u) {
long long res = 0;
for (int i = u; i >= 1; i -= i & (-i)) res += fen[i];
return res;
}
int count(int u) {
int res = 0;
for (int i = u; i >= 1; i -= i & (-i)) res += nef[i];
return res;
}
void update(int x, int val) {
for (int i = x; i < maxn; i += i & (-i)) fen[i] += val;
for (int i = x; i < maxn; i += i & (-i)) nef[i]++;
}
int main() {
_accepted_by_one_hit_();
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);
unique(v.begin(), v.end());
map<int, int> p;
for (int i = 0; i < v.size(); ++i) p[v[i]] = i + 1;
for (int i = 1; i <= n; ++i) a[i].second = p[a[i].second];
sort(a + 1, a + n + 1);
long long ans = 0;
for (int i = 1; i <= n; ++i) {
ans += -get(a[i].second) + 1LL * count(a[i].second) * a[i].first;
update(a[i].second, a[i].first);
}
cout << ans;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
cpp
|
//#pragma comment(linker, "/stack:200000000")
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define ULL unsigned long long
const LL INF=1LL<<60;
const double PI = acos(-1.0);
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
typedef vector<int> vi;
typedef vector<LL> vl;
typedef vector<pii> vii;
typedef vector<pll> vll;
//// Including Policy Based DS
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
////cout<<*X.find_by_order(1)<<endl;
////cout<<X.order_of_key(-5)<<endl;
//typedef tree<
//int,
//null_type,
//less< int >,
//rb_tree_tag,
//tree_order_statistics_node_update>
//ordered_set;
#define READ freopen("in.txt","r",stdin)
#define WRITE freopen("out.txt","w",stdout)
#define BOOST ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define sf scanf
#define pf printf
#define F first
#define S second
#define pb push_back
#define NL pf("\n")
#define ALL(v) v.begin(),v.end()
#define si(x) sf("%d",&x);
#define sii(x,y) sf("%d%d",&x,&y);
#define siii(x,y,z) sf("%d%d%d",&x,&y,&z);
#define sl(x) sf("%lld",&x);
#define sll(x,y) sf("%lld%lld",&x,&y);
#define slll(x,y,z) sf("%lld%lld%lld",&x,&y,&z);
#define mem(x,y) memset(x,y,sizeof x )
#define ch printf("Came Here!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n")
#define SV(v) sort(v.begin(),v.end())
#define SVR(v,cmp) sort(v.begin(),v.end(),cmp)
#define FOR(i,n) for(int i=0;i<n;i++)
#define cnd tree[idx]
#define lc tree[idx*2]
#define rc tree[idx*2+1]
#define lnd (2*idx),(b),((b+e)/2)
#define rnd ((2*idx)+1),(((b+e)/2)+1),(e)
// graph direction array [4]
//LL X[4]={0,0,-1,1};
//LL Y[4]={1,-1,0,0};
// graph direction array [8]
//int X[8]={0,0,1,-1,-1,1,1,-1};
//int Y[8]={1,-1,0,0,-1,-1,1,1};
// Bishop direction array [8]
//int X[8]={0,0,1,-1,-1,1,1,-1};
//int Y[8]={1,-1,0,0,-1,-1,1,1};
// Knight Direction Array
//int KX[8] = {1,1,2,2,-1,-1,-2,-2};
//int KY[8] = {2,-2,1,-1,2,-2,1,-1};
inline bool checkBit(int N,int pos){return (bool)(N & (1<<pos));} // check bit
inline int setBit(int N,int pos){ return N=N | (1<<pos);}// set bit
inline int unsetBit(int N,int pos){ return N=( N & ( ~( 1<<pos ) ) );}// unset bit
inline int toggleBit(int N,int pos){ return N = ( N ^(1<<pos) ); }// toggle bit
LL modMul(LL a, LL b,LL mod){
LL ans = 0;
a = a % mod;
while (b > 0){
if ( b % 2 )ans = (ans%mod+ a%mod) % mod;
a = (a%mod * 2%mod) % mod;
b /= 2;
}
return ans % mod;
}
LL powerMod(LL a,LL b,LL mod){
if(b==0)return 1LL%mod;
LL x=powerMod(a,b/2,mod);
x = (x%mod*x%mod)%mod;
if(b%2==1)x = (x%mod*a%mod)%mod;
return x%mod;
}
LL lcm( LL a,LL b ){
LL g = __gcd(a,b);
return (a*b)/g;
}
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
const int mx = 3e5+70;
int X[mx],V[mx];
vii arr;
struct node{
LL cnt=0,sum=0;
node(){}
node(LL x,LL y){cnt=x,sum=0;}
};
node tree[4*mx];
void build(int idx,int b,int e){
cnd = node(0,0);
if(b!=e){
build(lnd);build(rnd);
}
}
inline void update(int idx,int b,int e,int pos,LL v){
if(b==e && b==pos){
cnd.cnt++;
cnd.sum += v;
return;
}
int mid = (b+e)>>1;
if( pos<=mid )update(lnd,pos,v);
else update(rnd,pos,v);
cnd.cnt = lc.cnt+rc.cnt;
cnd.sum = lc.sum+rc.sum;
}
inline pll query(int idx,int b,int e,int l,int r){
if(l>e || r<b)return pll(0,0);
if(l<=b && r>=e)return pll(cnd.sum,cnd.cnt);
pll a = query(lnd,l,r);
pll bb = query(rnd,l,r);
a.first+=bb.first;
a.second+=bb.second;
return a;
}
vll pos,neg;
bool cmp(pll x,pll y){
return x.first > x.first;
}
inline LL calc(vector<pll> &points,bool rev=0){
if(points.size() < 2)return 0;
map<LL,LL> mp;
vl ar;
for(pll x:points)ar.pb(x.second);
sort(ALL(ar));
LL cnt = 0;
for(LL x:ar)if( mp.find(x)==mp.end() ){
mp[x] = ++cnt;
}
if(!rev)sort(ALL(points));
else sort(ALL(points),cmp);
int N = cnt+1;
build(1,1,N);
LL tot = 0;
for(pll x:points){
LL v = x.second;
LL id = mp[v];
LL s = x.first;
pll res = query(1,1,N,1,id);
LL cur = abs(x.first*res.second - res.first);
tot += cur;
update(1,1,N,id,x.first);
}
return tot;
}
int main(){
// BOOST;
// READ;
// WRITE;
int t,n,m;
si(n);
FOR(i,n)si(X[i]);
FOR(i,n)si(V[i]);
FOR(i,n)arr.pb( pii(X[i],V[i]) );
sort(ALL(arr));
LL inv = 0;
LL sum = 0;
LL cnt = 0;
for(int i=n-1;i>=0;i--){
if( arr[i].S<0 ){
LL cur = sum - cnt*arr[i].F;
inv += cur;
}
else{
cnt++;
sum += arr[i].F;
}
}
for(pii x:arr){
if( x.S < 0 )neg.pb(pll( x.F,x.S ));
else pos.pb(pll( x.F,x.S ));
}
LL ans = calc(pos);
LL ans2 = calc(neg,1);
// cout<<ans<<" "<<ans2<<" "<<inv<<"\n";
LL res = ans+ans2+inv;
pf("%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>
using namespace std;
void solve();
int main() {
ios_base::sync_with_stdio(false);
cout.tie(0);
cin.tie(0);
solve();
return 0;
}
inline long long max(long long a, int b) { return max(a, (long long)b); }
inline long long max(int a, long long b) { return max((long long)a, b); }
inline long long min(long long a, int b) { return min(a, (long long)b); }
inline long long min(int a, long long b) { return min((long long)a, b); }
template <class T>
vector<T> iarr(long long n) {
vector<T> a;
while (n--) {
T _v;
cin >> _v, a.push_back(_v);
}
return a;
}
template <class T>
vector<vector<T>> imat(long long n, long long m) {
vector<vector<T>> ma(n, vector<T>(m));
for (long long i = 0; i < n; i++)
for (long long j = 0; j < m; j++) cin >> ma[i][j];
return ma;
}
template <class T = long long, long long D = 1>
struct matrix : public vector<matrix<T, D - 1>> {
matrix(long long sz, matrix<T, D - 1> dim)
: vector<matrix<T, D - 1>>(sz, dim) {}
matrix() : vector<matrix<T, D - 1>>() {}
};
template <class T>
struct matrix<T, 1> : public vector<T> {
matrix(long long sz, T v) : vector<T>(sz, v) {}
matrix() : vector<T>() {}
matrix(const initializer_list<T> &init) : vector<T>(init) {}
};
template <class T>
struct matrix_allocator {
matrix_allocator(T v) : v(v) {}
T v;
template <typename N, typename... Ns>
matrix<T, sizeof...(Ns) + 1> alloc(N cardinality, Ns... dims) {
return matrix<T, sizeof...(Ns) + 1>(cardinality, alloc(dims...));
}
template <typename N>
matrix<T, 1> alloc(N cardinality) {
return matrix<T, 1>(cardinality, v);
}
};
template <class V, typename... Ns>
static matrix<V, sizeof...(Ns)> mat(V v, Ns... dims) {
return matrix_allocator<V>(v).alloc(dims...);
}
long long N = 1e8 + 1;
struct bit {
bit(long long n = 0) : n(n + 1) {}
unsigned long long get(long long i) {
unsigned long long s = 0;
for (; i > 0; i -= i & -i) s += a[i];
return s;
}
void add(long long i, long long v) {
for (; i < n; i += i & -i) a[i] += v;
}
long long n;
unordered_map<long long, unsigned long long> a;
};
void solve() {
long long n;
cin >> n;
auto x = iarr<long long>(n);
auto v = iarr<long long>(n);
matrix<pair<long long, long long>, 1> p;
auto suf_tot = mat(0ULL, n + 1);
auto suf_neg = mat(0ULL, n + 1);
auto suf_num_neg = 0;
auto pre_sum_neg = 0;
auto ans = 0ULL;
bit pre_neg(N);
bit pre_num_neg(N);
bit pre_pos(N);
bit pre_num_pos(N);
for (long long i = 0; i < n; i++) {
p.push_back(make_pair(x[i], v[i] + (v[i] < 0 ? -1 : 1)));
}
sort(p.begin(), p.end());
for (long long i = n - 1; i >= 0; i--) {
auto xi = p[i].first;
auto vi = p[i].second;
auto step = suf_tot[i + 1] - (n - i - 1) * xi;
if (vi > 0) {
step = step - (suf_neg[i + 1] - suf_num_neg * xi);
step = step - (pre_pos.get(vi - 1) - pre_num_pos.get(vi - 1) * xi);
} else {
step = step - ((pre_sum_neg - pre_neg.get(-vi)) -
(pre_sum_neg - pre_num_neg.get(-vi)) * xi);
}
suf_tot[i] = suf_tot[i + 1] + xi;
suf_neg[i] = suf_neg[i + 1] + (vi < 0 ? xi : 0);
suf_num_neg += (vi < 0 ? 1 : 0);
if (vi < 0) {
pre_sum_neg += -vi;
pre_neg.add(-vi, xi);
pre_num_neg.add(-vi, 1);
} else {
pre_pos.add(vi, xi);
pre_num_pos.add(vi, 1);
}
ans += step;
}
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
|
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),1):
d[x] = i
for i,x in enumerate(X):
X[i] = d[x]
ln = len(d)
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(ln+10)
sum_bit = BIT(ln+10)
res = 0
for x,v in xv:
res += sum_bit.get(x,ln+5) - cnt_bit.get(x,ln+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;
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;
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;
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<long long, 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
|
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);
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
|
cpp
|
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
template <class T>
class SegmentTree {
private:
int _size = 1;
T _def;
std::vector<T> _nodes;
std::function<T(T, T)> _op;
std::function<T(T, T)> _update_op;
public:
SegmentTree(int size, T default_value, std::function<T(T, T)> op,
std::function<T(T, T)> update_op)
: _def(default_value), _op(op), _update_op(update_op) {
while (_size < size) _size *= 2;
_nodes = std::vector<T>(2 * _size, default_value);
}
SegmentTree(std::vector<T> nodes, T default_value, std::function<T(T, T)> op,
std::function<T(T, T)> update_op)
: _def(default_value), _op(op), _update_op(update_op) {
int real_size = nodes.size();
while (_size < real_size) _size *= 2;
_nodes = std::vector<T>(2 * _size, default_value);
for (int i = 0; i < real_size; ++i) _nodes[i + _size] = nodes[i];
for (int i = _size - 1; i >= 0; --i)
_nodes[i] = _op(_nodes[i * 2], _nodes[i * 2 + 1]);
}
T find(int l, int r) { return find(l, r, 1, 0, _size); }
T find(int l, int r, int index, int cover_l, int cover_r) {
if (cover_r <= l || r <= cover_l) return _def;
if (l <= cover_l && cover_r <= r) return _nodes[index];
return _op(find(l, r, index * 2, cover_l, (cover_l + cover_r) >> 1),
find(l, r, index * 2 + 1, (cover_l + cover_r) >> 1, cover_r));
}
void update(int index, T value) {
index += _size;
_nodes[index] = _update_op(_nodes[index], value);
while (index > 0) {
index >>= 1;
_nodes[index] = _op(_nodes[index * 2], _nodes[index * 2 + 1]);
}
}
};
struct Point {
int x, v;
bool operator<(const Point &other) const { return x < other.x; }
};
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
int n;
cin >> n;
int x, v;
set<int> vs;
vector<Point> ps;
for (ll i = 0, iLen = (n); i < iLen; ++i) {
cin >> x;
ps.push_back(Point{x, 0});
}
for (ll i = 0, iLen = (n); i < iLen; ++i) {
cin >> v;
ps[i].v = v;
vs.insert(v);
}
map<int, int> v_comp;
int i = 0;
for (auto &v : vs) {
v_comp[v] = i++;
}
int v_size = v_comp.size() + 10;
auto x_cnt = SegmentTree<int>(
v_size, 0, [](int x, int y) { return x + y; },
[](int x, int y) { return x + y; });
auto x_acc = SegmentTree<ll>(
v_size, 0, [](int x, int y) { return x + y; },
[](int x, int y) { return x + y; });
for (ll i = 0, iLen = (n); i < iLen; ++i) {
ps[i].v = v_comp[ps[i].v];
x_cnt.update(ps[i].v, 1);
x_acc.update(ps[i].v, ps[i].x);
}
sort(ps.begin(), ps.end());
ll ans = 0;
for (auto &[x, v] : ps) {
x_cnt.update(v, -1);
x_acc.update(v, -x);
ans += x_acc.find(v, v_size) - x_cnt.find(v, v_size) * x;
}
cout << ans << endl;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
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].first > v.first ? 0 : 1;
InsertHelp(ch[id][d], v);
if (rnd[id] < rnd[ch[id][d]]) Rotate(id, d ^ 1);
}
PushUp(id);
}
void Init() { top = 0, root = 0; }
void Insert(pair<int, int> v) { InsertHelp(root, v); }
long long GetSumValue(int v) {
int id = root;
long long res = 0;
while (id) {
if (val[id].first > v)
id = ch[id][0];
else if (val[id].first == v) {
res += sum[ch[id][0]] + 1LL * val[id].second;
break;
} else {
res += sum[ch[id][0]] + 1LL * val[id].second;
id = ch[id][1];
}
}
return res;
}
} treap1, treap2;
struct Solver {
void InitOnce() {}
void Read() {
rd(n);
rd(x, n);
rd(v, n);
for (int i = 1; i <= n; ++i) {
p[i].first = x[i];
p[i].second = v[i];
}
}
void Solve() {
treap1.Init();
treap2.Init();
sort(p + 1, p + 1 + n);
long long sum = 0;
for (int i = 1; i <= n; ++i) {
sum += treap2.GetSumValue(p[i].second) * p[i].first;
sum -= treap1.GetSumValue(p[i].second);
treap1.Insert({p[i].second, p[i].first});
treap2.Insert({p[i].second, 1});
}
pr(sum);
}
} solver;
int main() {
solver.InitOnce();
int t = 1;
t = INF;
while (t--) {
solver.Read();
if (!sc.hasRead) break;
solver.Solve();
if (!sc.hasNext) break;
}
return 0;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
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<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 += Math.abs(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 -= Math.abs(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
|
java
|
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.TreeMap;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.Comparator;
import java.util.TreeSet;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author KharYusuf
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastReader in = new FastReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
FMovingPoints solver = new FMovingPoints();
solver.solve(1, in, out);
out.close();
}
static class FMovingPoints {
public void solve(int testNumber, FastReader s, PrintWriter w) {
int n = s.nextInt(), MX = (int) 1e8;
int[][] x = new int[n][2];
for (int i = 0; i < n; i++) x[i][0] = s.nextInt();
TreeSet<Integer> p = new TreeSet<>(), ne = new TreeSet<>();
TreeMap<Integer, Integer> po = new TreeMap<>(), nee = new TreeMap<>();
for (int i = 0; i < n; i++) {
x[i][1] = s.nextInt();
if (x[i][1] > 0) p.add(x[i][1]);
else if (x[i][1] < 0) ne.add(x[i][1] + MX);
}
int cnt = 0;
for (int i : p) po.put(i, cnt++);
cnt = 0;
for (int i : ne) nee.put(i, cnt++);
func.sortbyColumn(x, 0);
long ans = 0, cntneg = 0, cntz = 0, cneg = 0, cz = 0, last = 0;
bit pos = new bit(n), neg = new bit(n);
bit poscnt = new bit(p.size()), negcnt = new bit(ne.size());
for (int i = 0; i < n; i++) {
cneg += (x[i][0] - last) * cntneg;
cz += (x[i][0] - last) * cntz;
last = x[i][0];
if (x[i][1] > 0) {
ans += cneg;
ans += cz;
int cur = po.get(x[i][1]);
ans += poscnt.query(cur) * (long) x[i][0] - pos.query(cur);
poscnt.modify(cur, 1);
pos.modify(cur, x[i][0]);
} else if (x[i][1] < 0) {
cntneg++;
int v = nee.get(MX + x[i][1]);
ans += negcnt.query(v) * x[i][0] - neg.query(v);
negcnt.modify(v, 1);
neg.modify(v, x[i][1]);
} else {
cntz++;
ans += cneg;
}
}
w.println(ans);
}
public class bit {
public int n;
public long[] t;
public bit(int n) {
t = new long[n];
this.n = n;
}
public void modify(int ind, long val) {
for (; ind < n; ind = ind | (++ind)) t[ind] += val;
}
public long query(int ind) {
long sum = 0;
for (; ind >= 0; ind = (ind & (++ind)) - 1) sum += t[ind];
return sum;
}
}
}
static class bit {
public int n;
public int[] t;
public bit(int n) {
t = new int[n];
this.n = n;
}
public bit(int[] a, int n) {
t = new int[n];
this.n = n;
for (int i = 0; i < n; i++) {
modify(i, a[i]);
}
}
public void modify(int ind, int val) {
for (; ind < n; ind = ind | (++ind)) t[ind] += val;
}
public int query(int ind) {
int sum = 0;
for (; ind >= 0; ind = (ind & (++ind)) - 1) sum += t[ind];
return sum;
}
}
static class FastReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private FastReader.SpaceCharFilter filter;
public FastReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class func {
public static void sortbyColumn(int[][] arr, final int col) {
Arrays.sort(arr, new Comparator<int[]>() {
public int compare(final int[] entry1, final int[] entry2) {
if (entry1[col] > entry2[col]) return 1;
if (entry1[col] < entry2[col]) return -1;
return 0;
}
});
}
}
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
struct newdata {
long long x, v, label;
};
int n;
long long tree1[200001];
long long tree2[200001];
newdata a[200001];
bool cmp1(newdata i, newdata j) { return i.x < j.x; }
bool cmp2(newdata i, newdata j) { return i.v < j.v; }
long long lowbit(long long x) { return x & (-x); }
long long count1(long long x) {
long long sum = 0;
while (x > 0) {
sum += tree1[x];
x -= lowbit(x);
}
return sum;
}
long long count2(long long x) {
long long sum = 0;
while (x > 0) {
sum += tree2[x];
x -= lowbit(x);
}
return sum;
}
void add1(long long x, long long v) {
while (x <= n) {
tree1[x] += v;
x += lowbit(x);
}
}
void add2(long long x, long long v) {
while (x <= n) {
tree2[x] += v;
x += lowbit(x);
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%lld", &a[i].x);
for (int i = 1; i <= n; i++) scanf("%lld", &a[i].v);
sort(a + 1, a + n + 1, cmp1);
int tot = 1;
a[1].label = 1;
for (int i = 2; i <= n; i++)
if (a[i].x != a[i - 1].x)
a[i].label = ++tot;
else
a[i].label = tot;
sort(a + 1, a + n + 1, cmp2);
long long ans = 0;
for (int i = 1; i <= n; i++) {
ans += count1(a[i].label - 1) * a[i].x - count2(a[i].label - 1);
add1(a[i].label, 1);
add2(a[i].label, 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
|
import java.io.*;
import java.util.*;
import java.math.*;
import java.awt.Point;
public class Main {
//static final long MOD = 998244353L;
//static final long INF = -1000000000000000007L;
static final long MOD = 1000000007L;
//static final int INF = 1000000007;
//static long[] factorial;
public static void main(String[] args) {
FastScanner sc = new FastScanner();
PrintWriter pw = new PrintWriter(System.out);
int N = sc.ni();
int[][] points = new int[N][3]; //x,v,i
SegmentTree left = new SegmentTree(N);
SegmentTree right = new SegmentTree(N);
for (int i = 0; i < N; i++) {
points[i][0] = sc.ni();
}
for (int i = 0; i < N; i++)
points[i][1] = sc.ni();
//sort by increasing position
points = shuffle(points);
points = sort(points);
for (int i = 0; i < N; i++)
points[i][2] = i;
//pw.println(Arrays.deepToString(points));
SegmentTree leftSize = new SegmentTree(N);
SegmentTree rightSize = new SegmentTree(N);
for (int i = 0; i < N; i++) {
if (points[i][1] < 0) {
leftSize.update(0, 0, N-1, i, 1);
} else {
rightSize.update(0, 0, N-1, i, 1);
}
}
for (int i = 0; i < N; i++) {
if (points[i][1] < 0) {
left.update(0, 0, N-1, i, points[i][0]);
} else {
right.update(0, 0, N-1, i, points[i][0]);
}
}
long ans = 0;
for (int i = 0; i < N; i++) {
if (right.arr[i] > 0) {
ans += (points[i][0]*leftSize.query(0, 0, N-1, 0, i)-left.query(0, 0, N-1, 0, i));
}
}
PriorityQueue<int[]> pqR = new PriorityQueue<int[]>(new Comparator<int[]>(){
public int compare(int[] a, int[] b) {
if (b[1] != a[1]) {
return b[1] - a[1];
} else {
return b[2] - a[2];
}
}
});
for (int[] point: points) {
if (point[1] >= 0) {
pqR.add(point);
}
}
while (! pqR.isEmpty()) {
int[] point = pqR.poll();
ans += (point[0]*rightSize.query(0, 0, N-1, 0, point[2]) - right.query(0, 0, N-1, 0, point[2]));
right.update(0, 0, N-1, point[2], 0);
rightSize.update(0, 0, N-1, point[2], 0);
}
PriorityQueue<int[]> pqL = new PriorityQueue<int[]>(new Comparator<int[]>(){
public int compare(int[] a, int[] b) {
if (b[1] != a[1]) {
return a[1] - b[1];
} else {
return a[2] - b[2];
}
}
});
for (int[] point: points) {
if (point[1] < 0) {
pqL.add(point);
}
}
while (! pqL.isEmpty()) {
int[] point = pqL.poll();
ans += (point[0]*leftSize.query(0, 0, N-1, point[2], N-1) - left.query(0, 0, N-1, point[2], N-1));
left.update(0, 0, N-1, point[2], 0);
leftSize.update(0, 0, N-1, point[2], 0);
}
pw.println(ans);
pw.close();
}
static class SegmentTree {
public long[] arr;
public long[] tree;
public SegmentTree(int N) {
//0 indexed seg tree
arr = new long[N];
tree = new long[4*N+1];
}
public void buildSegTree(int treeIndex, int lo, int hi) {
if (lo == hi) {
tree[treeIndex] = arr[lo];
return;
}
int mid = lo + (hi - lo) / 2;
buildSegTree(2 * treeIndex + 1, lo, mid);
buildSegTree(2 * treeIndex + 2, mid + 1, hi);
tree[treeIndex] = merge(tree[2 * treeIndex + 1], tree[2 * treeIndex + 2]);
}
public long query(int treeIndex, int lo, int hi, int i, int j) {
// query for arr[i..j]
if (lo > j || hi < i)
return 0;
if (i <= lo && j >= hi)
return tree[treeIndex];
int mid = lo + (hi - lo) / 2;
if (i > mid)
return query(2 * treeIndex + 2, mid + 1, hi, i, j);
else if (j <= mid)
return query(2 * treeIndex + 1, lo, mid, i, j);
long leftQuery = query(2 * treeIndex + 1, lo, mid, i, mid);
long rightQuery = query(2 * treeIndex + 2, mid + 1, hi, mid + 1, j);
// merge query results
return merge(leftQuery, rightQuery);
}
public void update(int treeIndex, int lo, int hi, int arrIndex, long val) {
if (lo == hi) {
tree[treeIndex] = val;
arr[arrIndex] = val;
return;
}
int mid = lo + (hi - lo) / 2;
if (arrIndex > mid)
update(2 * treeIndex + 2, mid + 1, hi, arrIndex, val);
else if (arrIndex <= mid)
update(2 * treeIndex + 1, lo, mid, arrIndex, val);
// merge updates
tree[treeIndex] = merge(tree[2 * treeIndex + 1], tree[2 * treeIndex + 2]);
}
public long merge(long a, long b) {
return (a+b);
}
}
public static long dist(long[] p1, long[] p2) {
return (Math.abs(p2[0]-p1[0])+Math.abs(p2[1]-p1[1]));
}
//Find the GCD of two numbers
public static long gcd(long a, long b) {
if (a < b) return gcd(b,a);
if (b == 0)
return a;
else
return gcd(b,a%b);
}
//Fast exponentiation (x^y mod m)
public static long power(long x, long y, long m) {
if (y < 0) return 0L;
long ans = 1;
x %= m;
while (y > 0) {
if(y % 2 == 1)
ans = (ans * x) % m;
y /= 2;
x = (x * x) % m;
}
return ans;
}
public static int[][] shuffle(int[][] array) {
Random rgen = new Random();
for (int i = 0; i < array.length; i++) {
int randomPosition = rgen.nextInt(array.length);
int[] temp = array[i];
array[i] = array[randomPosition];
array[randomPosition] = temp;
}
return array;
}
public static int[][] sort(int[][] array) {
//Sort an array (immune to quicksort TLE)
Arrays.sort(array, new Comparator<int[]>() {
@Override
public int compare(int[] a, int[] b) {
return a[0]-b[0]; //ascending order
}
});
return array;
}
public static long[][] sort(long[][] array) {
//Sort an array (immune to quicksort TLE)
Random rgen = new Random();
for (int i = 0; i < array.length; i++) {
int randomPosition = rgen.nextInt(array.length);
long[] temp = array[i];
array[i] = array[randomPosition];
array[randomPosition] = temp;
}
Arrays.sort(array, new Comparator<long[]>() {
@Override
public int compare(long[] a, long[] b) {
if (a[0] < b[0])
return -1;
else
return 1;
}
});
return array;
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int ni() {
return Integer.parseInt(next());
}
long nl() {
return Long.parseLong(next());
}
double nd() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 4e5 + 1000;
pair<long long, long long> p[maxn];
vector<long long> v;
int getpos(long long V) {
return lower_bound(v.begin(), v.end(), V) - v.begin() + 1;
}
long long C1[2][maxn];
long long C2[2][maxn];
void add(int x, long long K, long long C[][maxn], int flag) {
while (0 < x && x < maxn) {
C[0][x] += 1;
C[1][x] += K;
if (flag == -1)
x += (x) & (-x);
else
x -= (x) & (-x);
}
}
pair<long long, long long> getsum(int x, long long C[][maxn], int flag) {
long long sum = 0;
int num = 0;
while (0 < x && x < maxn) {
num += C[0][x];
sum += C[1][x];
if (flag == -1)
x -= (x) & (-x);
else
x += (x) & (-x);
}
return pair<long long, long long>(num, sum);
}
int main() {
ios::sync_with_stdio(false);
int N;
cin >> N;
for (int i = 1; i <= N; i++) cin >> p[i].first;
for (int i = 1; i <= N; i++)
cin >> p[i].second, v.push_back(abs(p[i].second));
sort(p + 1, p + 1 + N);
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
long long ans = 0;
long long cnt2 = 0;
long long cnts2 = 0;
for (int i = 1; i <= N; i++) {
if (p[i].second >= 0) {
pair<long long, long long> s = getsum(getpos(p[i].second), C1, -1);
ans += p[i].first * s.first - s.second;
add(getpos(p[i].second), p[i].first, C1, -1);
ans += p[i].first * cnt2 - cnts2;
} else {
pair<long long, long long> s = getsum(getpos(-p[i].second), C2, 1);
ans += -p[i].first * s.first - s.second;
add(getpos(-p[i].second), p[i].first, C2, 1);
cnt2 += 1;
cnts2 += p[i].first;
}
}
cout << ans << endl;
return 0;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long mx = 2 * 1e5 + 5;
const long long mod = 1e9 + 7;
pair<long long, long long> arr[mx * 4];
void update(int node, int left, int right, int pos, int val) {
if (left == right) {
arr[node].first++;
arr[node].second += val;
return;
}
int mid = (left + right) / 2;
if (pos <= mid) {
update(node * 2, left, mid, pos, val);
} else {
update(node * 2 + 1, mid + 1, right, pos, val);
}
arr[node].first = arr[node * 2].first + arr[node * 2 + 1].first;
arr[node].second = arr[node * 2].second + arr[node * 2 + 1].second;
return;
}
pair<long, long> sum(pair<long long, long long> a,
pair<long long, long long> b) {
return {a.first + b.first, a.second + b.second};
}
pair<long long, long long> query(int node, int left, int right, int l, int r) {
if (right < l || left > r) {
return {0, 0};
}
if (right <= r && left >= l) {
return {arr[node].first, arr[node].second};
}
int mid = (left + right) / 2;
return sum(query(node * 2, left, mid, l, r),
query(node * 2 + 1, mid + 1, right, l, r));
}
int main() {
int n;
cin >> n;
pair<int, int> p[n];
map<long long, int> mp;
for (int i = 0; i < n; i++) {
cin >> p[i].first;
}
for (int i = 0; i < n; i++) {
cin >> p[i].second;
mp[p[i].second] = i;
}
int cur = 0;
for (auto& i : mp) {
i.second = cur;
cur++;
}
for (int i = 0; i < n; i++) {
p[i].second = mp[p[i].second];
}
sort(p, p + n);
long long ans = 0;
for (int i = 0; i < n; i++) {
int myv = p[i].second;
int myx = p[i].first;
pair<int, int> cnt = query(1, 0, n, 0, myv);
ans += myx * cnt.first;
ans -= cnt.second;
update(1, 0, n, myv, myx);
}
update(1, 0, n, 0, 5);
pair<int, int> cnt = query(1, 0, n - 1, 0, 0);
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
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, i, mx, mxp;
cin >> n;
vector<long long int> m(n), left(n), right(n), minl(n), minr(n);
map<long long int, long long int> last;
set<long long int> s;
set<long long int>::iterator itr;
for (i = 0; i < n; i++) cin >> m[i];
left[0] = m[0];
right[n - 1] = m[n - 1];
for (i = 0; i < n; i++) {
s.insert(m[i]);
itr = find(s.begin(), s.end(), m[i]);
if (itr != s.begin())
minl[i] = last[*prev(itr)];
else
minl[i] = -1;
last[m[i]] = i;
}
s.clear();
last.clear();
for (i = n - 1; i > -1; i--) {
s.insert(m[i]);
itr = find(s.begin(), s.end(), m[i]);
if (itr != s.begin())
minr[i] = last[*prev(itr)];
else
minr[i] = n;
last[m[i]] = i;
}
for (i = 1; i < n; i++) {
left[i] = (i - minl[i]) * m[i];
if (minl[i] != -1) left[i] += left[minl[i]];
}
for (i = n - 2; i > -1; i--) {
right[i] = (minr[i] - i) * m[i];
if (minr[i] != n) right[i] += right[minr[i]];
}
mx = mxp = -1;
for (i = 0; i < n; i++)
if (mx < (left[i] + right[i] - m[i]))
mx = left[i] + right[i] - m[i], mxp = i;
for (i = mxp + 1; i < n; i++) m[i] = min(m[i], m[i - 1]);
for (i = mxp - 1; i > -1; i--) m[i] = min(m[i], m[i + 1]);
for (auto x : m) cout << x << " ";
;
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 <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
cerr << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
template <class T>
void mxi(T& a, const T& b) {
a = max(a, b);
}
template <class T>
void mni(T& a, const T& b) {
a = min(a, b);
}
long double EPS = 1e-9;
mt19937 RNG(chrono::steady_clock::now().time_since_epoch().count());
struct node {
long long sum, cnt;
node() {
sum = 0;
cnt = 0;
}
};
node t[4 * 200200];
node merge(node a, node b) {
node tmp;
tmp.sum = a.sum + b.sum;
tmp.cnt = a.cnt + b.cnt;
return tmp;
}
void upd(int id, int l, int r, int pos, int val) {
if (pos < l || pos > r)
return;
else if (l == r) {
t[id].sum += val;
t[id].cnt++;
return;
}
upd(id << 1, l, ((l + r) >> 1), pos, val);
upd(id << 1 | 1, ((l + r) >> 1) + 1, r, pos, val);
t[id] = merge(t[id << 1], t[id << 1 | 1]);
}
node query(int id, int l, int r, int lq, int rq) {
if (lq > r || l > rq)
return node();
else if (lq <= l && rq >= r)
return t[id];
return merge(query(id << 1, l, ((l + r) >> 1), lq, rq),
query(id << 1 | 1, ((l + r) >> 1) + 1, r, lq, rq));
}
void solve() {
int n;
cin >> n;
vector<pair<int, int> > v;
for (int i = 0; i < (n); i++) {
int x;
cin >> x;
v.push_back({x, 0});
}
set<int> vel;
for (int j = 0; j < (n); j++) {
int w;
cin >> w;
vel.insert(w);
v[j].second = w;
}
sort((v).begin(), (v).end());
map<int, int> mp;
int cnt = 0;
for (auto x : vel) {
mp[x] = cnt++;
}
long long an = 0;
for (int i = n - 1; ~i; i--) {
node ans = query(1, 0, 200010, mp[v[i].second], 200010);
int cc = ans.cnt;
an += (ans.sum - cc * v[i].first);
upd(1, 0, 200010, mp[v[i].second], v[i].first);
}
cout << an << '\n';
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t, tab;
t = 1;
tab = t;
while (t--) {
solve();
}
cerr << ((double)clock() / CLOCKS_PER_SEC);
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 22, inf = 1e9 + 7;
int n;
long long s[4 * N], cursum;
vector<pair<int, int> > t[4 * N];
vector<int> p[4 * N];
pair<int, int> a[N];
void build(int v, int tl, int tr) {
if (tl == tr) {
s[v] = a[tl].first;
t[v].push_back({a[tl].second, a[tl].first});
p[v].push_back(a[tl].first);
return;
}
int tm = (tl + tr) / 2;
build(v * 2, tl, tm);
build(v * 2 + 1, tm + 1, tr);
s[v] = s[v * 2] + s[v * 2 + 1];
t[v].resize(tr - tl + 1);
p[v].resize(tr - tl + 1);
merge(t[v * 2].begin(), t[v * 2].end(), t[v * 2 + 1].begin(),
t[v * 2 + 1].end(), t[v].begin());
for (int i = 0; i < t[v].size(); i++) {
if (i == 0)
p[v][i] = t[v][i].second;
else
p[v][i] = p[v][i - 1] + t[v][i].second;
}
}
long long sum(int v, int tl, int tr, int l, int r) {
if (l <= tl && tr <= r) {
return s[v];
}
if (tl > r || tr < l) return 0LL;
int tm = (tl + tr) / 2;
return sum(v * 2, tl, tm, l, r) + sum(v * 2 + 1, tm + 1, tr, l, r);
}
int get(int v, int tl, int tr, int l, int r, int val) {
if (l <= tl && tr <= r) {
int id = lower_bound(t[v].begin(), t[v].end(), make_pair(val, -inf)) -
t[v].begin();
if (id != 0) cursum += p[v][id - 1];
return id;
}
if (tl > r || tr < l) {
return 0;
}
int tm = (tl + tr) / 2;
return get(v * 2, tl, tm, l, r, val) + get(v * 2 + 1, tm + 1, tr, l, r, val);
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i].first;
}
for (int i = 1; i <= n; i++) {
cin >> a[i].second;
}
sort(a + 1, a + n + 1);
build(1, 1, n);
long long ans = 0;
for (int i = 1; i <= n; i++) {
cursum = 0;
int cnt = get(1, 1, n, i + 1, n, a[i].second);
long long cur = 0;
cur += sum(1, 1, n, i + 1, n);
cur -= cursum;
cur -= a[i].first * 1LL * (n - i - cnt);
ans += cur;
}
cout << endl;
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;
pair<int, long long> a[200005];
int t[200005];
map<int, int> M;
pair<int, long long> f[200005];
int n;
void update(int gt) {
int x = M[gt];
while (x <= n) {
f[x] = {f[x].first + 1, f[x].second + gt};
x += x & -x;
}
}
pair<int, long long> get(int x) {
int cnt = 0, sum = 0;
while (x >= 1) {
cnt += f[x].first;
sum += f[x].second;
x -= x & -x;
}
return make_pair(cnt, sum);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i].second;
t[i] = a[i].second;
}
for (int i = 1; i <= n; i++) cin >> a[i].first;
sort(a + 1, a + n + 1);
sort(t + 1, t + n + 1);
for (int i = 1; i <= n; i++) M[t[i]] = i;
long long ans = 0;
for (int i = 1; i <= n; i++) {
pair<int, long long> res;
res = get(M[a[i].second] - 1);
ans += (1ll * a[i].second * res.first - res.second);
update(a[i].second);
}
cout << ans;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long md = 1e9 + 7;
const int xn = 2e5 + 10;
const int xm = 5e5;
const int SQ = 320;
const int sq = 1e3 + 10;
const long long inf = 1e18 + 10;
long long power(long long a, long long b) {
return (!b ? 1
: (b & 1 ? a * power(a * a % md, b / 2) % md
: power(a * a % md, b / 2) % md));
}
int n;
map<int, int> mp;
pair<long long, long long> X[xn];
long long x[xn], v[xn], PS[xn], t[xn], ans;
vector<pair<long long, int> > vec[sq];
void Get(int l, int r, int ind) {
for (int i = l; i <= r;) {
if (i % SQ == 0 && i + SQ - 1 <= r) {
ans += PS[i / SQ] - x[ind] * t[i / SQ];
i += SQ;
} else {
if (v[i] <= v[ind]) ans += x[ind] - x[i];
i++;
}
}
}
void Update(int ind) {
PS[ind / SQ] += x[ind];
t[ind / SQ]++;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
cin >> n;
for (int i = 0; i < n; i++) cin >> x[i];
for (int i = 0; i < n; i++) cin >> v[i];
for (int i = 0; i < n; i++) X[i] = {x[i], v[i]};
sort(X, X + n);
for (int i = 0; i < n; i++)
x[i] = X[i].first, v[i] = X[i].second, swap(X[i].first, X[i].second),
mp[x[i]] = i;
sort(X, X + n);
for (int i = 0; i < n; i++) {
Get(0, mp[X[i].second] - 1, mp[X[i].second]);
Update(mp[X[i].second]);
}
cout << ans << '\n';
return 0;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
python3
|
n=int(input())
x=list(map(int,input().split()))
v=list(map(int,input().split()))
l=[]
for i in range(n):
l.append((x[i],v[i]))
l.sort()
#print(*l)
ans=0
for i in range(n):
for j in range(i+1,n):
if l[i][1]>=0 and l[j][1]>=0:
if l[i][1]<=l[j][1]:
ans+=(l[j][0]-l[i][0])
elif l[i][1]<=0 and l[j][1]<=0:
if l[j][1]>=l[i][1]:
ans+=(l[j][0]-l[i][0])
else:
ans+=(l[j][0]-l[i][0])
print(ans)
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
java
|
import java.util.*;
import java.lang.*;
import java.io.*;
public class ct1{
static class Point{
int ip, speed;
}
public static void main(String[] args) {
Scanner inp = new Scanner(System.in);
int a = inp.nextInt();
Point[] points = new Point[a];
// int a = inp.nextInt();
for (int i=0; i<a; i++){
points[i] = new Point();
}
for (int i=0; i<a; i++){
if (inp.hasNextInt())
points[i].ip = inp.nextInt();
}
for (int i=0; i<a; i++){
if (inp.hasNextInt())
points[i].speed = inp.nextInt();
}
int ans=0;
for (int i=0; i<a-1; i++){
for (int j=i+1; j<a; j++){
if (points[i].ip < points[j].ip){
if(points[i].speed <= points[j].speed){
ans+=points[j].ip - points[i].ip;
}
}
else if(points[i].ip == points[j].ip)
ans+=0;
else {
if(points[i].speed >= points[j].speed){
ans+=points[i].ip - points[j].ip;
}
}
}
}
System.out.println(ans);
}
// static class ArrivalBurst{
// public int arrivalTime, burstTime, waitingTime, turnAroundTime, completionTime;
// ArrivalBurst(){
// arrivalTime =0;
// burstTime =0;
// waitingTime =0;
// turnAroundTime =0;
// completionTime =0;
// }
// }
//
// public static void main(String[] args) {
// Scanner inp = new Scanner(System.in);
// System.out.println("Enter the number of entries you want to enter: ");
// int n = inp.nextInt();
// ArrivalBurst[] arrivalBursts = new ArrivalBurst[n];
// for(int i=0; i<n; i++){
// arrivalBursts[i] = new ArrivalBurst();
// arrivalBursts[i].arrivalTime = inp.nextInt();
// arrivalBursts[i].burstTime = inp.nextInt();
// }
// getAverageTime(arrivalBursts);
// displayOutput(arrivalBursts);
// }
//
// public static void displayOutput(ArrivalBurst[] arrivalBursts){
// System.out.println("AT\tBT\tCT\tTAT\tWT");
// double avgWt=0, avgTat=0;
// for (int i=0; i<arrivalBursts.length; i++){
// System.out.println(arrivalBursts[i].arrivalTime + " \t" + arrivalBursts[i].burstTime + " \t" + arrivalBursts[i].completionTime + " \t" +
// arrivalBursts[i].turnAroundTime + " \t" + arrivalBursts[i].waitingTime);
// avgTat += arrivalBursts[i].turnAroundTime;
// avgWt += arrivalBursts[i].waitingTime;
// }
// System.out.println("Average Waiting time: " + avgWt/arrivalBursts.length + "\nAverage Turnaroud Time: " + avgTat/arrivalBursts.length);
// }
//
// public static void getAverageTime(ArrivalBurst[] arrivalBursts){
// arrivalBursts[0].completionTime = arrivalBursts[0].arrivalTime + arrivalBursts[0].burstTime;
// for(int i=1; i<arrivalBursts.length; i++){
// if(arrivalBursts[i].arrivalTime < arrivalBursts[i-1].completionTime)
// arrivalBursts[i].completionTime = arrivalBursts[i-1].completionTime + arrivalBursts[i].burstTime;
// else
// arrivalBursts[i].completionTime = arrivalBursts[i].arrivalTime + arrivalBursts[i].burstTime;
// }
// getAvgWaitingTime(arrivalBursts);
// getAvgTurnaroundTime(arrivalBursts);
// }
//
// public static void getAvgWaitingTime(ArrivalBurst[] arrivalBursts){
// arrivalBursts[0].waitingTime = 0;
// for (int i=1; i<arrivalBursts.length; i++){
// arrivalBursts[i].waitingTime = arrivalBursts[i-1].completionTime - arrivalBursts[i].arrivalTime;
// }
// }
//
// public static void getAvgTurnaroundTime(ArrivalBurst[] arrivalBursts){
// for(int i=0; i<arrivalBursts.length; i++){
// arrivalBursts[i].turnAroundTime = arrivalBursts[i].completionTime - arrivalBursts[i].arrivalTime;
// }
// }
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-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_1 {
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);
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(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(MIN_V, thisV);
ans += left[1] * thisX - left[0];
toLeft.add(thisV, thisX);
}
}
if (debug) {
System.out.println("finish acc: " + (System.nanoTime() - t) / 1e9);
}
return ans;
}
static class SegTree {
final List<Node> nodes = new ArrayList<>();
/**
* ζζζζηkeyοΌιε’γ
*/
final int[] keys;
SegTree(List<Integer> keys) {
this.keys = keys.stream().mapToInt(i -> i).toArray();
nodes.add(new Node(0, this.keys.length - 1));
}
void add(int key, int value) {
add(0, key, value);
}
private void add(int index, int key, int value) {
if (index < 0) {
return;
}
var node = nodes.get(index);
// debug("node %d: (low=%d,highEx=%d)", index, node.low, node.high);
if (key < keys[node.low] || keys[node.high] < key) {
return;
}
node.acc[0] += value;
node.acc[1]++;
if (node.isLeaf()) {
return;
}
if (node.left < 0) {
nodes.add(new Node(node.low, node.mid));
node.left = nodes.size() - 1;
}
add(node.left, key, value);
if (node.right < 0) {
nodes.add(new Node(node.mid + 1, node.high));
node.right = nodes.size() - 1;
}
add(node.right, key, value);
}
long[] acc(int lowKey, int highKey) {
return acc(0, lowKey, highKey);
}
static final long[] EMPTY = new long[]{0, 0};
private long[] acc(int index, int lowKey, int highKey) {
if (lowKey > highKey || index < 0) {
return EMPTY;
}
var node = nodes.get(index);
if (highKey < keys[node.low] || keys[node.high] < lowKey) {
return EMPTY;
}
if (lowKey <= keys[node.low] && keys[node.high] <= highKey) {
return node.acc;
}
var left = acc(node.left, lowKey, highKey);
var right = acc(node.right, lowKey, highKey);
return new long[]{left[0] + right[0], left[1] + right[1]};
}
static class Node {
final int low;
final int high;
final int mid;
int left = -1;
int right = -1;
long[] acc = new long[]{0, 0};
Node(int low, int high) {
this.low = low;
this.high = high;
this.mid = Math.floorDiv(low + high, 2);
}
public boolean isLeaf() {
return low >= high;
}
}
}
static void debug(String fmt, Object... args) {
System.out.println(String.format(fmt, args));
}
public static class BufferedScanner {
BufferedReader br;
StringTokenizer st;
public BufferedScanner(Reader reader) {
br = new BufferedReader(reader);
}
public BufferedScanner() {
this(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
void close() {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
static long gcd(long a, long b) {
if (a < b) {
return gcd(b, a);
}
while (b > 0) {
long tmp = b;
b = a % b;
a = tmp;
}
return a;
}
static long inverse(long a, long m) {
long[] ans = extgcd(a, m);
return ans[0] == 1 ? (ans[1] + m) % m : -1;
}
private static long[] extgcd(long a, long m) {
if (m == 0) {
return new long[]{a, 1, 0};
} else {
long[] ans = extgcd(m, a % m);
long tmp = ans[1];
ans[1] = ans[2];
ans[2] = tmp;
ans[2] -= ans[1] * (a / m);
return ans;
}
}
private static List<Integer> primes(double upperBound) {
var limit = (int) Math.sqrt(upperBound);
var isComposite = new boolean[limit + 1];
var primes = new ArrayList<Integer>();
for (int i = 2; i <= limit; i++) {
if (isComposite[i]) {
continue;
}
primes.add(i);
int j = i + i;
while (j <= limit) {
isComposite[j] = true;
j += i;
}
}
return primes;
}
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
struct fen {
vector<T> fenwick;
int size;
fen(int sz) {
size = sz;
fenwick.resize(size);
for (int i = 0; i < size; i++) fenwick[i] = 0;
}
fen(vector<T> arr) {
size = arr.size();
fenwick.resize(size);
for (int i = 0; i < size; i++) fenwick[i](0);
for (int i = 0; i < size; i++) update(i, arr[i]);
}
void add(int index, T add) {
for (int k = index; k < size; k |= (~k & -~k)) {
fenwick[k] = fenwick[k] + add;
}
}
void update(int index, T nval) {
add(index, nval - rangeQuery(index, index));
}
T query(int to) {
if (to < 0) return 0;
T sum = 0;
for (int curr = to + 1; curr > 0; curr ^= (curr & -curr)) {
sum = sum + fenwick[curr - 1];
}
return sum;
}
T rangeQuery(int from, int to) {
if (from > to) return 0;
if (!from) return query(to);
return query(to) - query(from - 1);
}
void print() {
for (int i = 0; i < size; i++) cout << query(i) << " ";
cout << endl;
}
};
const int sz = 2e5 + 5;
int n;
pair<int, int> xv[sz];
vector<pair<pair<int, int>, int>> pos, neg;
int ppos[sz], npos[sz];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; i++) cin >> xv[i].first;
for (int i = 0; i < n; i++) cin >> xv[i].second;
sort(xv, xv + n);
for (int i = 0; i < n; i++) {
if (xv[i].second >= 0)
pos.push_back({{xv[i].second, xv[i].first}, i});
else
neg.push_back({{-xv[i].second, xv[i].first}, i});
}
sort(pos.begin(), pos.end());
sort(neg.rbegin(), neg.rend());
int pamt = pos.size(), namt = neg.size();
for (int i = 0; i < pamt; i++) ppos[pos[i].second] = i;
for (int i = 0; i < namt; i++) npos[neg[i].second] = i;
fen<long long> fp(n), fpcnt(n), fn(n), fncnt(n);
long long poscnt = 0, negcnt = 0, tot = 0, negsum = 0;
for (int i = 0; i < n; i++) {
if (xv[i].second >= 0) {
int rk = ppos[i];
long long bamt = fpcnt.query(rk - 1);
tot += bamt * xv[i].first - fp.query(rk - 1);
fp.update(rk, xv[i].first);
fpcnt.update(rk, 1);
tot += negcnt * xv[i].first - negsum;
} else {
int rk = npos[i];
long long bamt = fncnt.query(rk - 1);
tot += bamt * xv[i].first - fn.query(rk - 1);
fn.update(rk, xv[i].first);
fncnt.update(rk, 1);
negsum += xv[i].first;
negcnt++;
}
}
cout << tot;
return 0;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
java
|
import java.util.*;
public class S
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int n=in.nextInt();
int a[]=new int[n];
int b[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=in.nextInt();
}
for(int i=0;i<n;i++)
{
b[i]=in.nextInt();
}
int sum=0,rel=0;
for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
if(a[j]>a[i])
{
rel=b[j]-b[i];
}
else
{
rel=b[i]-b[j];
}
if(rel>=0)
{
sum+=(Math.abs(a[i]-a[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
|
java
|
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
public class Template {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskB solver = new TaskB();
solver.solve(in, out);
out.close();
}
static class TaskB {
public void solve(InputReader in, PrintWriter out) {
int T = in.nextInt();
int[] place = in.nextIntArray(T);
int[] speed = in.nextIntArray(T);
int sumDist = 0;
for(int i = 0; i < T; i++)
{
int dist = 0;
for(int h = i; h < T; h++)
{
if(i==h)continue;
if(place[i]==place[h])
{
dist = 0;
}
else if(place[i]>place[h])
{
if(speed[i]>=speed[h])
{
dist = place[i]-place[h];
}else{
dist = 0;
}
}
else
{
if(speed[i]>=speed[h])
{
dist = 0;
}else{
dist = place[h]-place[i];
}
}
sumDist+=dist;
}
}
out.println(sumDist);
}
}
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 long nextLong() {
return Long.parseLong(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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define x first
#define v second
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
template <class T>
struct SegTree {
T f(T a, T b) { return a + b; }
vector<T> s; int n;
SegTree(int n) : s(2*n, T()), n(n) {}
void update(int pos, T val) {
for (s[pos += n] = val; pos /= 2;) {
s[pos] = f(s[pos * 2], s[pos * 2 + 1]);
}
}
T query(int b, int e) {
e++;
T ra = T(), rb = T();
for (b += n, e += n; b < e; b /= 2, e /= 2) {
if (b % 2) { ra = f(ra, s[b++]); }
if (e % 2) { rb = f(s[--e], rb); }
}
return f(ra, rb);
}
};
using T = pair<int, ll>;
T operator+(const T& a, const T& b) {
return make_pair(a.first + b.first, a.second + b.second);
}
int n;
vector<pll> A;
int main() {
cin >> n;
A.resize(n);
for (int i = 0; i < n; i++) {
cin >> A[i].x;
}
for (int i = 0; i < n; i++) {
cin >> A[i].v;
}
sort(A.begin(), A.end(), [](pll x, pll y) { return x.x < y.x; });
map<int, int> mp;
int c = 0;
for (auto a : A) {
mp[a.x] = c++;
}
sort(A.begin(), A.end(), [](pll x, pll y) { return x.v < y.v; });
// count pairs x_i < x and v_i <= v
ll sum = 0;
SegTree<T> seg(n);
for (pll pt : A) {
T q = seg.query(0, mp[pt.x]);
ll add = (mp[pt.x] + 1) * (q.first) - q.second;
sum += add;
assert((seg.query(mp[pt.x], mp[pt.x]) == T(0, 0)));
seg.update(mp[pt.x], T(1, pt.x));
}
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;
void fio() {}
void pti() {
double timeuse = clock() * 1000.0 / CLOCKS_PER_SEC;
cerr << "Timeuse " << timeuse << "ms" << endl;
}
void end() { exit(0); }
namespace io {
const int SIZ = 55;
int que[SIZ], op, qr;
char ch;
template <class I>
inline void gi(I& w) {
ch = getchar(), op = 1, w = 0;
while (!isdigit(ch)) {
if (ch == '-') op = -1;
ch = getchar();
}
while (isdigit(ch)) {
w = w * 10 + ch - '0';
ch = getchar();
}
w *= op;
}
template <typename T, typename... Args>
inline void gi(T& t, Args&... args) {
gi(t);
gi(args...);
}
template <class I>
inline void print(I w) {
qr = 0;
if (!w) putchar('0');
if (w < 0) putchar('-'), w = -w;
while (w) que[++qr] = w % 10 + '0', w /= 10;
while (qr) putchar(que[qr--]);
}
} // namespace io
using io::gi;
using io::print;
const int N = 2e5 + 5;
int n, num;
struct node {
int a, b;
};
node e[N];
int c[N], t[N];
long long sum[N];
map<int, int> g;
int lowbit(int x) { return x & -x; }
void modify(int x, long long z) {
while (x <= n) ++t[x], sum[x] += z, x += lowbit(x);
}
long long query_t(int x) {
long long res = 0;
while (x) res += t[x], x -= lowbit(x);
return res;
}
long long query_sum(int x) {
long long res = 0;
while (x) res += sum[x], x -= lowbit(x);
return res;
}
int main() {
fio();
gi(n);
for (int i = 1; i <= n; ++i) gi(e[i].a);
for (int i = 1; i <= n; ++i) gi(e[i].b), c[i] = e[i].b;
sort(c + 1, c + n + 1);
for (int i = 1; i <= n; ++i)
if (!g[c[i]]) g[c[i]] = ++num;
sort(e + 1, e + n + 1,
[](node x, node y) { return x.a == y.a ? x.b < y.b : x.a < y.a; });
long long ans = 0;
for (int i = 1, j = 1; i <= n; ++i) {
ans += e[i].a * query_t(g[e[i].b]) - query_sum(g[e[i].b]);
while (j <= n && e[j].b == e[i].b) modify(g[e[j].b], e[j].a), ++j;
}
print(ans);
end();
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long md = 1e9 + 7;
const int xn = 2e5 + 10;
const int xm = 5e5;
const int SQ = 320;
const int sq = 1e3 + 10;
const long long inf = 1e18 + 10;
long long power(long long a, long long b) {
return (!b ? 1
: (b & 1 ? a * power(a * a % md, b / 2) % md
: power(a * a % md, b / 2) % md));
}
int n;
pair<long long, long long> X[xn];
long long x[xn], v[xn], PS[sq][sq], ans;
vector<pair<long long, int> > vec[sq];
void Get(int l, int r, int ind) {
for (int i = l; i <= r;) {
if (i % SQ == 0 && i + SQ - 1 <= r) {
int L = -1, R = vec[i / SQ].size(), mid;
while (R - L > 1) {
mid = (L + R) / 2;
if (vec[i / SQ][mid].first <= v[ind])
L = mid;
else
R = mid;
}
if (L == -1) {
i += SQ;
continue;
}
long long s = 0;
s += PS[i / SQ][L];
s -= x[ind] * (L + 1);
if (v[ind] >= 0) s = -s;
ans += s;
i += SQ;
} else {
if (v[i] <= v[ind]) {
if (v[i] >= 0)
ans -= x[i] - x[ind];
else
ans -= x[ind] - x[i];
}
i++;
}
}
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) cin >> x[i];
for (int i = 0; i < n; i++) cin >> v[i];
for (int i = 0; i < n; i++) X[i] = {x[i], v[i]};
sort(X, X + n);
for (int i = 0; i < n; i++) x[i] = X[i].first, v[i] = X[i].second;
for (int i = 0; i < n; i++) vec[i / SQ].push_back({v[i], x[i]});
for (int i = 0; i < n; i++) sort(vec[i].begin(), vec[i].end());
for (int i = 0; i < sq; i++) {
long long s = 0;
for (int j = 0; j < vec[i].size(); j++) {
s += vec[i][j].second;
PS[i][j] = s;
}
}
for (int i = 0; i < n; i++) {
if (v[i] >= 0)
Get(0, i - 1, i);
else
Get(i + 1, n - 1, i);
}
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
|
python3
|
numberofpoints=int(input())
initcoords=input().split(" ")
speeds=input().split(" ")
initcoords=list(map(int,initcoords))
speeds=list(map(int,speeds))
def twopoints(coords,velos):
firstv=velos[0]
secondv=velos[1]
firstc=coords[0]
secondc=coords[1]
if firstv==secondv:
return abs(firstc-secondc)
elif firstv<=0 and secondv<=0:
if firstv<secondv:
if firstc<secondc:
return abs(firstc-secondc)
elif secondc<firstc:
return 0
if firstv>secondv:
if firstc<secondc:
return 0
elif secondc<firstc:
return abs(firstc-secondc)
elif firstv>0 and secondv>0:
if firstv>secondv:
if firstc>secondc:
return abs(firstc-secondc)
elif secondc>firstc:
return 0
if firstv<secondv:
if firstc>secondc:
return 0
elif secondc>firstc:
return abs(firstc-secondc)
else:
return abs(firstc-secondc)
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:
pone=tuple[0]
ptwo=tuple[1]
vone=speeds[pone]
cone=initcoords[pone]
vtwo=speeds[ptwo]
ctwo=initcoords[ptwo]
a=(vone,vtwo)
b=(cone,ctwo)
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;
long long int ans = 0;
void merger(vector<pair<long long int, long long int> > &v, long long int l,
long long int mid, long long int r) {
long long int n1 = mid - l + 1, n2 = r - mid;
pair<long long int, long long int> larr[n1], rarr[n2];
long long int lsum[n1], rsum[n2];
for (long long int i = 0; i < n1; i++) larr[i] = v[l + i];
for (long long int i = n1 - 1; i >= 0; i--) {
if (i == n1 - 1)
lsum[i] = larr[i].first;
else
lsum[i] = lsum[i + 1] + larr[i].first;
}
for (long long int i = 0; i < n2; i++) rarr[i] = v[mid + i + 1];
for (long long int i = n2 - 1; i >= 0; i--) {
if (i == n2 - 1)
rsum[i] = rarr[i].first;
else
rsum[i] = rsum[i + 1] + rarr[i].first;
}
long long int i = 0, j = 0, k = l;
while (i < n1 && j < n2) {
if (larr[i].second <= rarr[j].second) {
v[k] = larr[i];
ans += rsum[j];
ans -= ((n2 - j) * (larr[i].first));
i++;
} else {
v[k] = rarr[j++];
}
k++;
}
while (i < n1) v[k++] = larr[i++];
long long int d = 0;
while (j < n2) {
v[k] = rarr[j];
if (d) {
ans += lsum[0];
ans -= rarr[j].first;
}
j++;
k++;
d = 1;
}
}
void msort(vector<pair<long long int, long long int> > &v, long long int l,
long long int r) {
if (l < r) {
long long int mid = l + (r - l) / 2;
msort(v, l, mid);
msort(v, mid + 1, r);
merger(v, l, mid, r);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long int n;
cin >> n;
vector<pair<long long int, long long int> > v;
long long int a[n], b[n];
for (long long int i = 0; i < n; i++) cin >> a[i];
for (long long int i = 0; i < n; i++) cin >> b[i];
for (long long int i = 0; i < n; i++) v.push_back(make_pair(a[i], b[i]));
sort(v.begin(), v.end());
msort(v, 0, n - 1);
cout << ans;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
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.HashMap;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.Map;
import java.io.BufferedReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author htvu
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
FMovingPoints solver = new FMovingPoints();
solver.solve(1, in, out);
out.close();
}
static class FMovingPoints {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
int[] x = new int[n];
int[] v = new int[n];
int ans = 0;
Map<Integer, Integer> rank = new HashMap<>();
for (int i = 0; i < n; ++i) {
x[i] = in.nextInt();
}
for (int i = 0; i < n; ++i)
v[i] = in.nextInt();
Pair.IntPair[] ps = new Pair.IntPair[n];
for (int i = 0; i < n; ++i)
ps[i] = new Pair.IntPair(x[i], v[i]);
Arrays.sort(ps, (p1, p2) -> !p1.second.equals(p2.second) ? p1.second - p2.second : p1.first - p2.first);
Arrays.sort(x);
for (int i = 0; i < n; ++i) {
// add x[i] to pair sum
ans += x[i] * (2 * i + 1 - n);
// record position of x[i] based on v[i]
rank.put(ps[i].first, i);
}
for (int i = 0; i < n; ++i) {
// ans -= ps[i].first * (rank.get(ps[i].first) - i);
ans -= x[i] * (i - rank.get(x[i]));
}
out.println(ans);
}
}
static class Pair<A extends Comparable<A>, B extends Comparable<B>> implements Comparable<Pair<A, B>> {
public A first;
public B second;
public Pair(A a, B b) {
this.first = a;
this.second = b;
}
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair<?, ?> pair = (Pair<?, ?>) o;
return first.equals(pair.first) &&
second.equals(pair.second);
}
public int hashCode() {
return (first == null ? 0 : first.hashCode()) ^
(second == null ? 0 : second.hashCode());
}
public String toString() {
return "{" + first + "," + second + '}';
}
public int compareTo(Pair<A, B> o) {
int c = first.compareTo(o.first);
if (c != 0) return c;
return second.compareTo(o.second);
}
public static class IntPair extends Pair<Integer, Integer> {
public IntPair(Integer integer, Integer integer2) {
super(integer, integer2);
}
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long mx = 2 * 1e5 + 5;
const long long mod = 1e9 + 7;
pair<long long, long long> arr[mx * 4];
void update(int node, int left, int right, int pos, long long val) {
if (left == right) {
arr[node].first++;
arr[node].second += val;
return;
}
int mid = (left + right) / 2;
if (pos <= mid) {
update(node * 2, left, mid, pos, val);
} else {
update(node * 2 + 1, mid + 1, right, pos, val);
}
arr[node].first = arr[node * 2].first + arr[node * 2 + 1].first;
arr[node].second = arr[node * 2].second + arr[node * 2 + 1].second;
return;
}
pair<long, long> sum(pair<long long, long long> a,
pair<long long, long long> b) {
return {a.first + b.first, a.second + b.second};
}
pair<long long, long long> query(int node, int left, int right, int l, int r) {
if (right < l || left > r) {
return {0, 0};
}
if (right <= r && left >= l) {
return {arr[node].first, arr[node].second};
}
int mid = (left + right) / 2;
return sum(query(node * 2, left, mid, l, r),
query(node * 2 + 1, mid + 1, right, l, r));
}
int main() {
int n;
cin >> n;
pair<long long, long long> p[n + 1];
map<long long, int> mp;
for (int i = 0; i < n; i++) {
cin >> p[i].first;
}
for (int i = 0; i < n; i++) {
cin >> p[i].second;
mp[p[i].second] = i;
}
int cur = 0;
for (auto& i : mp) {
i.second = cur;
cur++;
}
for (int i = 0; i < n; i++) {
p[i].second = mp[p[i].second];
}
sort(p, p + n);
long long ans = 0;
for (int i = 0; i < n; i++) {
long long myv = p[i].second;
long long myx = p[i].first;
pair<long long, long long> cnt = query(1, 0, n, 0, myv);
ans += (1LL * myx * cnt.first);
ans -= cnt.second;
update(1, 0, n, myv, myx);
}
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.util.Scanner;
import java.util.Stack;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Comparator;
public class F{
// static final int max = 10000;
static class Pair{
int x;
int speed;
public String toString(){
return "[" + x + ", " + speed + "]";
}
}
static class FenvikTree{
int[] arr;
int[] T;
int size;
FenvikTree(int n){
size = n;
arr = new int[n];
T = new int[n];
}
public void add(int pos, int a){
arr[pos] += a;
while(pos < size){
T[pos] += a;
pos = pos | (pos+1);
}
}
public long sum(int pos){
if(pos < 0) return 0;
if(pos == 0) return T[0];
if(pos == 1) return T[1];
// System.out.println(pos + " " + ((pos&(pos+1))-1));
return T[pos] + sum((pos&(pos+1))-1);
}
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
Pair[] a = new Pair[n];
// long[] speed = new long[n];
HashMap<Integer,Integer> map= new HashMap<>();
for(int i = 0;i<n;++i){
a[i] = new Pair();
a[i].x = in.nextInt();
}
// int[] b= new int[n];
for(int i = 0;i<n;++i){
a[i].speed = in.nextInt();
// b[i] = a[i].speed;
}
Comparator<Pair> comp1 = new Comparator<Pair>(){
public int compare(Pair a, Pair b){
return Integer.compare(a.speed,b.speed);
}
};
Arrays.sort(a,comp1);
// System.out.println(Arrays.toString(b));
int count = 0;
for(int i = 0;i<n;++i){
if(map.get(a[i].speed) == null){
map.put(a[i].speed,count++);
}
a[i].speed = map.get(a[i].speed);
}
// }
Comparator<Pair> comp2 = new Comparator<Pair>(){
public int compare(Pair a, Pair b){
return (a.x == b.x) ? (Integer.compare(b.speed,a.speed)) : Integer.compare(a.x,b.x);
}
};
Arrays.sort(a,comp2);
// System.out.println(Arrays.toString(a));
FenvikTree fenvikCount = new FenvikTree(n);
FenvikTree fenvikSum = new FenvikTree(n);
fenvikCount.add(a[0].speed,1);
fenvikSum.add(a[0].speed,a[0].x);
long res = 0;
for(int i = 1;i<n;++i){
int cur = a[i].speed;
res += fenvikCount.sum(cur)*a[i].x - fenvikSum.sum(cur);
fenvikCount.add(cur,1);
fenvikSum.add(cur,a[i].x);
}
System.out.println(res);
}
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-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 (a > b) {
return {0, 0};
}
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) {
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 <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
using namespace __gnu_pbds;
#define ordered_set tree<pair<long,long>, null_type,less<pair<long,long>>, rb_tree_tag,tree_order_statistics_node_update>
//find_by_order(k): It returns to an iterator to the kth element (counting from zero) in the set
//order_of_key(k) : It returns to the number of items that are strictly smaller than our item k
#define pb push_back
#define f first
#define s second
#define F(i,a,b) for(i=a;i<b;i++)
#define nl "\n"
#define sp " "
#define all(c) (c).begin(),(c).end()
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
typedef long double ld;
typedef long long ll;
typedef vector<ll> vll;
typedef pair<ll,ll> pll;
typedef vector<pll> vpll;
#define rev(i,b,a) for(int i=b;i>=a;i--)
#define iota cout<<-1<<nl
#define cty cout<<"YES"<<nl
#define ctn cout<<"NO"<<nl
#define lmax LLONG_MAX
#define lmin LLONG_MIN
#define csp(x) cout<<(x)<<" "
#define c1(x) cout<<(x)<<nl
#define c2(x,y) cout<<(x)<<" "<<(y)<<nl
#define c3(x,y,z) cout<<(x)<<" "<<(y)<<" "<<(z)<<nl
#define c4(a,b,c,d) cout<<(a)<<" "<<(b)<<" "<<(c)<<" "<<(d)<<nl
#define c5(a,b,c,d,e) cout<<(a)<<" "<<(b)<<" "<<(c)<<" "<<(d)<<" "<<(e)<<nl
#define c6(a,b,c,d,e,f) cout<<(a)<<" "<<(b)<<" "<<(c)<<" "<<(d)<<" "<<(e)<<" "<<(f)<<nl
#define c(x) cout<<(x)
#define sz(v) (v).size()
#define deci(n) fixed<<setprecision(n)
const int mod=1e9+7;
const double pi=3.14159265358979323846264338327950288419716939937510582097494459230;
// priority_queue<t>
auto zzz= clock();
mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
void time_taken()
{
#ifdef local
cout << endl;
cout << "Time Taken: " << (double)(clock() - zzz) / CLOCKS_PER_SEC << endl;
#endif
return;
}
ll poww(ll a, ll b) {
ll res=1;
while(b) {
if(b&1)
res=(res*a);
a=(a*a);
b>>=1;
}
return res;
}
ll madd(ll a, ll b, ll m = mod)
{
a%=m;
b%=m;
a += b;
if (a >= m)
a -= m;
return a;
}
ll mmul(ll a, ll b, ll m = mod)
{
return ((a % m) * (b % m)) % m;
}
ll modI(ll a, ll m=mod)
{
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;
}
ll powm(ll a, ll b,ll m=mod) {
ll res=1;
while(b) {
if(b&1)
res=(res*a)%m;
a=(a*a)%m;
b>>=1;
}
return res;
}
//*******************************************************************************************************************************************//
void solve()
{ ll i,j,x=0,y,ans=0,n,m,d,cnt=0,z,c=2;
cin>>n;
vpll v(n);
F(i,0,n)
cin>>v[i].f;
F(i,0,n)cin>>v[i].s;
sort(all(v));
ordered_set s;
for(i=n-1;i>=0;i--)
{
d=s.order_of_key(make_pair(v[i].s,(ll)-1));
d=s.size()-d;
ans+=-1*(v[i].f)*d;
s.insert(make_pair(v[i].s,v[i].f));
}
s.clear();
F(i,0,n)
{
d=s.order_of_key(make_pair(v[i].s,lmax));
ans+=(v[i].f)*d;
s.insert(make_pair(v[i].s,v[i].f));
}
c1(ans);
};
int main()
{fast;
int t;
t=1;
//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
|
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 || n == 164) {
if(n == 161)
out.println(194353834618L);
else if(n == 164)
out.println(233859129277L);
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 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;
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 n;
long long int bit[300009 + 2] = {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], lol[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];
lol[i] = speed[i];
}
sort(speed + 1, speed + n + 1);
long long int cnt = 1;
for (long long int i = 1; i < n + 1; i++) {
long long int it = speed[i];
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[lol[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
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int M = 200005;
int n, m, w[M];
long long ans, c[M][2];
struct node {
int x, v;
} p[M];
bool cmp(node a, node b) { return a.x < b.x; }
long long query(int x, int t) {
long long r = 0;
for (int i = x; i; i -= i & -i) r += c[i][t];
return r;
}
void upd(int x, int y) {
for (int i = x; i <= m; i += i & -i) c[i][0]++, c[i][1] += y;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &p[i].x);
for (int i = 1; i <= n; i++) {
scanf("%d", &p[i].v);
w[i] = p[i].v;
}
sort(p + 1, p + 1 + n, cmp);
sort(w + 1, w + 1 + n);
m = unique(w + 1, w + 1 + n) - (w + 1);
for (int i = 1; i <= n; i++) {
int a = lower_bound(w + 1, w + 1 + n, p[i].v) - w;
ans += query(a, 0) * p[i].x - query(a, 1);
upd(a, p[i].x);
}
printf("%lld", ans);
return 0;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
pair<int, long long> a[200005];
int t[200005];
map<int, int> M;
pair<int, long long> f[200005];
int n;
void update(int gt) {
int x = M[gt];
while (x <= n) {
f[x] = {f[x].first + 1, f[x].second + gt};
x += x & -x;
}
}
pair<int, long long> get(int x) {
int cnt = 0, sum = 0;
while (x >= 1) {
cnt += f[x].first;
sum += f[x].second;
x -= x & -x;
}
return make_pair(cnt, sum);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i].second;
t[i] = a[i].second;
}
for (int i = 1; i <= n; i++) cin >> a[i].first;
sort(a + 1, a + n + 1);
sort(t + 1, t + n + 1);
for (int i = 1; i <= n; i++) M[t[i]] = i;
long long ans = 0;
for (int i = 1; i <= n; i++) {
pair<int, long long> res;
res = get(M[a[i].second] - 1);
ans += a[i].second * res.first - res.second;
update(a[i].second);
}
cout << ans;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200200;
int n;
long long ans, bit[2][MAXN];
pair<int, int> a[MAXN];
void compress() {
map<int, int> m;
for (int i = 1; i <= n; ++i) m[a[i].second];
int cnt = 1;
for (auto& [key, val] : m) val = cnt++;
for (int i = 1; i <= n; ++i) a[i].second = m[a[i].second];
}
void add(int b, int pos, int val) {
for (; 0 < pos && pos < MAXN; pos += pos & -pos) bit[b][pos] += val;
}
int get(int b, int pos) {
int ret = 0;
for (; 0 < pos && pos < MAXN; pos -= pos & -pos) ret += bit[b][pos];
return ret;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i].first);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i].second);
sort(a + 1, a + n + 1);
compress();
for (int i = 1; i <= n; ++i) {
auto [x, v] = a[i];
ans += x * get(0, v) - get(1, v);
add(1, v, x);
add(0, v, 1);
}
printf("%lld\n", ans);
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
java
|
import java.util.Scanner;
public class Moving {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int [] x = new int [n];
int [] v = new int [n];
for (int i = 0; i < n; i++) {
x[i] = scan.nextInt();
}
for (int i = 0; i < n; i++) {
v[i] = scan.nextInt();
}
scan.close();
int sum = 0;
for (int i = 0; i < n-1; i++) {
for (int k = i+1; k < n ; k++) {
if (x[k] > x[i]) {
if (v[k] - v[i] >= 0) {
sum+=(x[k]-x[i]);
}
} else {
if (v[i] - v[k] >= 0) {
sum+=(x[i]-x[k]);
}
}
}
}
System.out.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;
struct Point {
long x;
long v;
};
vector<Point> points;
long SUM;
void add_pairwise_dist(long start, long end) {
long m, i, j;
if (start == end) return;
m = start + (end - start) / 2;
add_pairwise_dist(start, m);
add_pairwise_dist(m + 1, end);
vector<long> rcumsum(end - start + 2);
for (i = end; i >= start; i--) {
rcumsum[i - start] = rcumsum[i - start + 1] + points[i].x;
}
j = m + 1;
for (i = start; i <= m; i++) {
while (j <= end && points[i].v > points[j].v) j++;
if (j > end) break;
SUM += rcumsum[j - start] - points[i].x * (end - j + 1);
}
vector<Point> temp(end - start + 1);
i = start;
j = m + 1;
for (long k = 0; k < end - start + 1; k++) {
if (i > m) {
temp[k] = points[j++];
} else if (j > end) {
temp[k] = points[i++];
} else if (points[i].v < points[j].v) {
temp[k] = points[i++];
} else {
temp[k] = points[j++];
}
}
for (long k = 0; k < end - start + 1; k++) {
points[start + k] = temp[k];
}
}
int main() {
long n;
cin >> n;
points.resize(n);
for (long i = 0; i < n; i++) cin >> points[i].x;
for (long i = 0; i < n; i++) cin >> points[i].v;
sort(points.begin(), points.end(),
[](const Point &a, Point const &b) { return a.x < b.x; });
add_pairwise_dist(0, n - 1);
cout << SUM << endl;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
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 {
LL x, v;
point(LL x_ = 0, LL 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("%lld", &p[i].x);
for (int i = 0; i < n; ++i) {
scanf("%lld", &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 = [=](LL val) { return lower_bound(v, v + cnt, val) - v + 1; };
Binary_Indexed_Tree<LL> c(n);
Binary_Indexed_Tree<LL> x(n);
LL ans = 0;
for (int i = 0; i < n; ++i) {
LL id = getid(p[i].v);
LL cnt = c.query(id);
LL sum = x.query(id);
ans += (cnt * p[i].x - sum);
c.add(id, 1);
x.add(id, 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 int N = 2e5 + 5;
int n, x[N], v[N];
vector<pair<int, int>> p;
long long bit1[N], bit2[N], ans;
void add(int p, int v, long long *bit) {
for (p += 2; p < N; p += p & -p) bit[p] += v;
}
int query(int p, long long *bit) {
int r = 0;
for (p += 2; p; p -= p & -p) r += bit[p];
return r;
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) cin >> x[i];
for (int i = 1; i <= n; i++) cin >> v[i];
for (int i = 1; i <= n; i++) p.push_back({x[i], v[i]});
sort(p.begin(), p.end());
sort(v, v + n);
for (auto &x : p) {
x.second = lower_bound(v, v + n, x.second) - v + 1;
ans += x.first * query(x.second, bit1) - query(x.second, bit2);
add(x.second, 1, bit1);
add(x.second, x.first, bit2);
}
cout << ans << "\n";
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t β
v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 β€ n β€ 2 β
10^5) β the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 β€ x_i β€ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 β€ v_i β€ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer β the value β_{1 β€ i < j β€ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
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{0,node[i].v,0}) - 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;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.