Search is not available for this dataset
name
stringlengths 2
88
| description
stringlengths 31
8.62k
| public_tests
dict | private_tests
dict | solution_type
stringclasses 2
values | programming_language
stringclasses 5
values | solution
stringlengths 1
983k
|
|---|---|---|---|---|---|---|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
java
|
import java.math.BigInteger;
import java.util.Scanner;
public class Moving_Points {
public static void main(String args[]) {
Scanner reader = new Scanner(System.in);
int n = reader.nextInt();
int[] x = new int[n];
for (int i = 0; i < n; i++)
x[i] = reader.nextInt();
int[] v = new int[n];
for (int i = 0; i < n; i++)
v[i] = reader.nextInt();
reader.close();
BigInteger sum = new BigInteger("0");
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (v[i] * v[j] < 0 || v[i] == v[j] || (v[i] * v[j] > 0 && ((x[i] < x[j] && v[i] < v[j]) || (x[j] < x[i] && v[j] < v[i])))) {
sum = sum.add(BigInteger.valueOf((long) Math.abs(x[i] - x[j])));
} else if (v[i] * v[j] == 0) {
if ((v[i] == 0 && ((x[j] > x[i] && v[j] > 0) || (x[j] < x[i] && v[j] < 0))
|| (v[j] == 0 && ((x[i] > x[j] && v[i] > 0) || (x[i] < x[j] && v[i] < 0))))
)
sum = sum.add(BigInteger.valueOf(Math.abs(x[i] - x[j])));
}
}
}
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
|
java
|
import static java.lang.Math.*;
import java.util.Scanner;
public class onebalgo {
static long minsum=0;
static int n;
static dot[] overwrite=new dot[n];
public static boolean samepace(dot i,dot j) {
if(i.vel==j.vel)
return true;
else return false;
}
public static boolean waitornot(dot i,dot j) {
if(i.vel>0 && j.vel<0 && i.pos>=0 && j.pos<0)
return false;
if(i.vel>0 && j.vel<0 && i.pos<0 && j.pos>=0)
return true;
if(i.pos>j.pos && i.vel>j.vel)
return false;
if(i.pos>j.pos && i.vel<j.vel)
return true;
if(i.pos<j.pos && i.vel<j.vel)
return false;
if(i.pos<j.pos && i.vel>j.vel)
return true;
return false;
}
static void enterhere(int x,int y)
{
minsum=minsum+Math.abs(x-y);
}
static void checker() {
for(int i=0;i<n;i++) {
dot doto2=overwrite[i];
for(int j=0;j<n;j++) {
if(i<=j && i!=j) {
System.out.println(overwrite[i].pos+""+overwrite[j].pos);
dot doto3=overwrite[j];
if(samepace(doto2,doto3)==true)
enterhere(doto2.pos,doto3.pos);
else if(waitornot(doto2, doto3)==false)
enterhere(doto2.pos,doto3.pos);
}
}
}
}
public static void main(String[] args) {
Scanner isus= new Scanner(System.in);
n=isus.nextInt();
overwrite= new dot[n];
for(int i=0;i<n;i++) {
dot doto1=new dot();
doto1.pos=isus.nextInt();
overwrite[i]=doto1;
}
for(int i=0;i<n;i++) {
int vel=isus.nextInt();
overwrite[i].vel=vel;
}
checker();
System.out.println(minsum);
}
}
class dot{
int pos;
int 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 namespace std;
struct abc {
long long first;
long long second;
};
bool comp(abc a, abc b) { return a.second < b.second; }
int main() {
long long n, cnt = 0;
cin >> n;
abc a[n + 1];
for (int i = 1; i <= n; i++) cin >> a[i].second;
for (int i = 1; i <= n; i++) {
cin >> a[i].first;
}
sort(a + 1, a + n + 1, comp);
for (long long i = 1; i <= n; i++) {
for (long long j = i + 1; j <= n; j++) {
if ((a[i].first >= 0 and a[j].first >= 0) or
(a[i].first <= 0 and a[j].first <= 0)) {
if (a[i].first <= a[j].first and a[i].second != a[j].second) {
cnt += j - i;
}
} else if (a[i].first == 0 and a[j].first >= 0)
cnt += j - i;
else if (a[i].first != 0 and a[j].first != 0)
cnt += j - i;
}
}
cout << cnt << endl;
return 0;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
cpp
|
// "It does not matter how slowly you go as long as you do not stop." - Confucius
#include <bits/stdc++.h>
using namespace std;
/*
author : Roshan_Mehta
motto : Time Management,Consistency,Patience!!
*/
#define int long long
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
// void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef Local_Debug
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
#define M1 1000000007
#define M2 998244353
#define G(a,b) get<a>(b)
#define ll long long
#define pb push_back
#define mt make_tuple
#define fast() ios_base::sync_with_stdio(false);cin.tie(NULL);
#define mp make_pair
#define F first
#define S second
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rsort(x) sort(x,greater<int> ())
#define rall(x) rbegin(x), rend(x) //useful in sorting
#define endl "\n"
#define p0(a) cout << a << " "
#define p1(a) cout << a << endl
#define p2(a, b) cout << a << " " << b << endl
#define p3(a, b, c) cout << a << " " << b << " " << c << endl
#define p4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl
#define gcd __gcd
#define lcm(a,b) (a*b)/gcd(a,b)
#define deci(n) fixed << setprecision(n)
#define test() int test_case;cin >> test_case;while (test_case--)
#define loop(i,a,n) for(int i=a;i<n;i++)
#define sp " "
#define popcount(x) __builtin_popcount(x)
#define popcountll(x) __builtin_popcountll(x)
#define clz(x) __builtin_clz(x)
#define clzll(x) __builtin_clzll(x)
#define ctz(x) __builtin_ctz(x)
#define ctzll(x) __builtin_ctzll(x)
#define max3(a,b,c) max(max(a,b),c)
#define min3(a,b,c) min(min(a,b),c)
#define max4(a,b,c,d) max(a,max3(b,c,d))
#define min4(a,b,c,d) min(a,min3(b,c,d))
#define max5(a,b,c,d,e) max(max4(a,b,c,d),e)
#define min5(a,b,c,d,e) min(min4(a,b,c,d),e)
#define getunique(v) {sort(all(v)); v.erase(unique(all(v)), v.end());}
#define REP(i,a,b) for(ll i=a;i<b;i++)
#define REPI(i,a,b) for(ll i=b-1;i>=a;i--)
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
typedef pair<int,pi> tri;
typedef pair<pi,int> rtri;
typedef pair<ll, ll> pl;
typedef pair<double, double> pd;
typedef priority_queue<int, vector<int>, greater<int>> minpq;
typedef priority_queue<int> pq;
typedef unordered_map<int,int> umii;
typedef map<int,int> mii;
typedef set<int> si;
#define input(n,k) int n,k;cin>>n;cin>>k;vi arr(n,0);loop(i,0,n){cin>>arr[i];}
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
const int MOD = 1e9 + 7;
int md=MOD;
int Power(int n, int x) {int ans = 1;while (x > 0) {if (x & 1) ans = (ans * n) % md;
n = (n * n) % md;x = x >> 1;}return ans;}
vl fact, inv;
void inverse(ll n)
{if(n>=inv.size()){ll size=inv.size();size=size==0? 1:size;
inv.resize(n + 1);inv[0] = 1;
for (ll i = size; i <= n; i++)inv[i] = Power(fact[i], md - 2);}}
void factorial(ll n)
{if(n>=fact.size()){ll size=fact.size();size=size==0? 1:size;
fact.resize(n + 1);fact[0] = 1;for (ll i = size; i <= n; i++)
fact[i] = (fact[i - 1] * i) % md;
}}
ll ncr(ll n, ll r) { return (((fact[n] * inv[r]) % md) * inv[n - r]) % md; }
vl SieveOfEratosthenes(int n)
{
bool prime[n+1];
memset(prime, true, sizeof(prime));
for (int p=2; p*p<=n; p++)
{if (prime[p] == true){for (int i=p*p; i<=n; i += p)
prime[i] = false;}}
vl ans;for (int p=2; p<=n; p++)if (prime[p])ans.pb(p);return ans;
}
vi primeFactors(int n)
{
vi ans;
while (n % 2 == 0) {ans.pb(2);n = n / 2;}
for (int i = 3; i <= sqrt(n); i = i + 2) {
while (n % i == 0) {ans.pb(i);n = n / i;}}
if (n > 2)ans.pb(n);return ans;
}
// bool isprime(ll n)
// {
// if(n<2)
// return false;
// for(ll x:{2,3,5,7,11,13,17,19,23,29,31,37})
// {
// if(n==x)
// return true;
// bool flag=true;
// ll r=1;
// ll t=1;
// while(r<=((n-1)>>__builtin_ctzll(n-1)))
// {
// if(r&((n-1)>>__builtin_ctzll(n-1)))
// t=((__int128)t*x)%n;
// x=((__int128)x*x)%n;
// r<<=1;
// }
// if(t==1||t==n-1)
// flag=false;
// for(r=0;r<__builtin_ctzll(n-1);r++)
// {
// t=((__int128)t*t)%n;
// if(t==n-1)
// flag=false;
// }
// if(flag)
// return false;
// }
// return true;
// }
ll NcR(int n, int r)
{long long p = 1, k = 1;if (n - r < r)r = n - r;if (r != 0) {while (r) {p *= n;k *= r;
long long m = gcd(p, k);p /= m;k /= m;n--;r--;}
}else p = 1;return p;// cout << p << endl;
}
bool sortasc(const rtri a,const rtri b) //Ascending ... change the argument accordingwise;
{
return a.second<b.second;
}
bool sortdesc(const int a,const int b) //Descending... change the argument accordingwise;a
{
return a>b;
}
int myceil(int num,int den)
{
if ((num >= 0 and den > 0) or ( num <= 0 and den < 0))
return num%den == 0 ? num/den : num/den + 1;
return num / den;
}
int myfloor(int num,int den)
{
if ((num >= 0 and den > 0) or ( num <= 0 and den < 0))
return num%den == 0 ? num/den : num/den;
int ans = num / den;
return num % den == 0 ? ans : ans - 1;
}
int midofthree(int a,int b,int c)
{
if((a<=b and a>=c) or (a<=c and a>=b))
return a;
else if((b>=a and b<=c) or (b<=a and b>=c))
return b;
return c;
}
ll modI(ll a, ll m)
{
ll m0=m,y=0,x=1;
if(m==1) return 0;
while(a>1)
{ll q=a/m;ll t=m;m=a%m;a=t;t=y;y=x-q*y;x=t;
}
if(x<0) x+=m0;
return x;
}
string to_string(char c) {
return string(1, c);
}
clock_t startTime;
double getCurrentTime() {
return (double)(clock() - startTime) / CLOCKS_PER_SEC;
}
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); //From Umnik Template
using ull = unsigned long long;
ll myRand(ll B) {
return (ull)rng() % B;
}
// reinitialize value of md(example md = M1 or md=M2);
ll add(ll x, ll y) {
x += y;
if (x >= md) return x - md;
return x;
}
ll sub(ll x, ll y) {
x -= y;
if (x < 0) return x + md;
return x;
}
ll mult(ll x, ll y) {
return (x * y) % md;
}
ll bin_pow(ll x, ll p) {
if (p == 0) return 1;
if (p & 1) return mult(x, bin_pow(x, p - 1));
return bin_pow(mult(x, x), p / 2);
}
ll rev(ll x) {
return bin_pow(x, md - 2);
}
void q1();void q2();void q3();void q4();void q5();
#define pie 3.141592653589793238462643383279
//********************THE END OF`` TEMPLATES*******************//
const int N = 2e5 + 5;
bool comp(pi &a, pi &b){
return a.S < b.S;
}
struct FenwickTree{
vector<int> bit;
int n;
const int INF = (int)1e9;
FenwickTree(int n){
this->n=n;
bit.assign(n,0);
// bit.assign(n,INF);
}
FenwickTree(vector<int> a) : FenwickTree(a.size()){
for (size_t i = 0; i < a.size(); i++)
{
add(i,a[i]);
// update(i,a[i]);
}
}
int sum(int r){
int ret=0;
for (; r>=0; r = (r & (r + 1))-1){
ret+=bit[r];
}
return ret;
}
int sum(int l,int r){
return sum(r)-sum(l-1);
}
void add(int idx, int delta){
for(; idx < n; idx = idx | (idx + 1)){
bit[idx] += delta;
}
}
};
void run_testcase(){
int n;
cin >> n;
vpi Cord(n);
for (int i = 0; i < n; i++)
{
cin >> Cord[i].F;
}
for (int i = 0; i < n; i++)
{
cin >> Cord[i].S;
}
// sort(all(Cord));
vpi Cord2 = Cord;
map<int, int> compress;
int t = 1;
sort(all(Cord), comp);
for (int i = 0; i < n; i++)
{
int num = 1;
if (Cord[i].S < 0) num = -1;
if (compress.count(Cord[i].S) == 0) {
compress[Cord[i].S] = t++;
}
Cord2[i].S = compress[Cord[i].S];
// Cord2[i].S *= num;
}
debug(compress);
int MaxN = n + 10;
vi cnt(MaxN);
int ans = 0;
Cord = Cord2;
sort(all(Cord));
FenwickTree FT(MaxN);
FenwickTree FTS(MaxN);
debug(Cord);
int neg_num = 0, neg_sum = 0;
for (int i = 0; i < n; i++)
{
int val = Cord[i].S;
debug(i, val);
if (val < 0){
neg_num++;
neg_sum += abs(val);
continue;
}
int small = FT.sum(val);
ans += Cord[i].F * small;
FT.add(val, 1);
int val2 = FTS.sum(val);
debug(small, ans, val2);
// debug(val2);
ans -= val2;
FTS.add(val, Cord[i].F);
// ans += (neg_num)*Cord[i].F - neg_num;
debug(i, ans);
}
// FenwickTree FT2(MaxN);
// FenwickTree FTS2(MaxN);
// for (int i = 0; i < n; i++)
// {
// int val = Cord[i].S;
// if (val > 0) continue;
// val *= -1;
// int small = FT2.sum(val);
// ans += Cord[i].F * small;
// FT2.add(val, 1);
// int val2 = FTS2.sum(val);
// ans -= val2;
// FTS2.add(val, Cord[i].F);
// }
cout << ans << endl;
}
int32_t main()
{
startTime = clock();
fast();
#ifdef Local_Debug
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
// test()
{
run_testcase();
}
return 0;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
python2
|
from sys import stdin
from collections import *
class order_tree:
def __init__(self, arr):
self.n = len(arr)
self.r = n << 1
self.tree = [[0, 0] for _ in range(self.n * 2)]
self.order = defaultdict(int, {arr[i]: i for i in range(self.n)})
# get interval[l,r)
def query(self, l):
res, coun = 0, 0
l = self.order[l] + self.n
r = self.r
while l < r:
if l & 1:
res += self.tree[l][0]
coun += self.tree[l][1]
l += 1
if r & 1:
r -= 1
res += self.tree[r][0]
coun += self.tree[r][1]
l >>= 1
r >>= 1
return res, coun
def update(self, ix, val):
ix = self.n + self.order[ix]
# set new value
self.tree[ix][0] += val
self.tree[ix][1] += 1
# move up
while ix > 1:
self.tree[ix >> 1] = [self.tree[ix][0] + self.tree[ix ^ 1][0], self.tree[ix][1] + self.tree[ix ^ 1][1]]
ix >>= 1
rints = lambda: [int(x) for x in stdin.readline().split()]
n, a = int(input()), sorted(map(lambda x, y: [x, y], rints(), rints()))
dis = sorted(set([x[1] for x in a]))
tree, ans = order_tree(dis), 0
for i in range(n - 1, -1, -1):
su, coun = tree.query(a[i][1])
ans += su - coun * a[i][0]
tree.update(a[i][1], a[i][0])
print(ans)
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long int N = 300009;
const long long int mod = 1000000007;
long long int n;
vector<pair<long long int, long long int>> v;
const long long int inf = 0;
struct Node {
Node *l = 0, *r = 0;
long long int lo, hi, mset = inf, madd = 0, val = 0;
Node(long long int lo, long long int hi) : lo(lo), hi(hi) {}
Node(vector<long long int> &v, long long int lo, long long int hi)
: lo(lo), hi(hi) {
if (lo + 1 < hi) {
long long int mid = lo + (hi - lo) / 2;
l = new Node(v, lo, mid);
r = new Node(v, mid, hi);
val = (l->val + r->val);
} else
val = v[lo];
}
long long int query(long long int L, long long int R) {
if (R <= lo || hi <= L) return 0;
if (L <= lo && hi <= R) return val;
push();
return (l->query(L, R) + r->query(L, R));
}
void set(long long int L, long long int R, long long int x) {
if (R <= lo || hi <= L) return;
if (L <= lo && hi <= R)
mset = x, val = x * (hi - lo), madd = 0;
else {
push(), l->set(L, R, x), r->set(L, R, x);
val = (l->val + r->val);
}
}
void add(long long int L, long long int R, long long int x) {
if (R <= lo || hi <= L) return;
if (L <= lo && hi <= R) {
if (mset != inf)
mset += x;
else
madd += x;
val += x * (hi - lo);
} else {
push(), l->add(L, R, x), r->add(L, R, x);
val = (l->val + r->val);
}
}
void push() {
if (!l) {
long long int mid = lo + (hi - lo) / 2;
l = new Node(lo, mid);
r = new Node(mid, hi);
}
if (mset != inf)
l->set(lo, hi, mset), r->set(lo, hi, mset), mset = inf;
else if (madd)
l->add(lo, hi, madd), r->add(lo, hi, madd), madd = 0;
}
};
void pre() {}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin.exceptions(cin.failbit);
;
pre();
cin >> n;
v.resize(n);
for (long long int i = 0; i < (n); ++i) {
cin >> v[i].first;
}
for (long long int i = 0; i < (n); ++i) {
cin >> v[i].second;
}
sort(v.begin(), v.end());
long long int negi = 0, posi = 0, ans = 0;
Node *neg = new Node(-1, 1e9);
Node *pos = new Node(-1, 1e9);
Node *neg_num = new Node(-1, 1e9);
Node *pos_num = new Node(-1, 1e9);
long long int sumnegi = 0;
long long int sumposi = 0;
for (long long int i = 0; i < (n); ++i) {
if (v[i].second >= 0) {
long long int add = pos->query(-1, (v[i].second) + 1);
long long int num = pos_num->query(-1, (v[i].second) + 1);
ans += (num * v[i].first - (add));
pos->add((v[i].second), (v[i].second) + 1, v[i].first);
pos_num->add((v[i].second), (v[i].second) + 1, 1);
ans += ((negi * v[i].first) - sumnegi);
} else {
v[i].second = (-v[i].second);
long long int add = neg->query(v[i].second + 1, 1e9);
long long int num = neg_num->query(v[i].second + 1, 1e9);
ans += (num * v[i].first - (add));
neg->add(v[i].second, v[i].second + 1, v[i].first);
neg_num->add(v[i].second, v[i].second + 1, 1);
negi += 1;
sumnegi += v[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
|
python3
|
numberofpoints=int(input())
initcoords=input().split(" ")
speeds=input().split(" ")
initcoords=tuple(map(int,initcoords))
speeds=tuple(map(int,speeds))
def twopoints(coords,velos):
firstv=velos[0]
secondv=velos[1]
firstc=coords[0]
secondc=coords[1]
dif=abs(firstv-secondv)
nextdif=abs((firstc+firstv)-(secondc+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;
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<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;
}
}node[MAX],v[MAX];
struct ST{ // n-1
LL v[MAX];
LL sum(int x){
LL s = 0;
while(x){
s += v[x]; x -= x&-x;
}
return s;
}
void add(int x, LL val){
while(x<=n){
v[x]+=val; x += x&-x;
}
}
LL operator [] (int idx){
return sum(idx);
}
}s,c;
int main(void){
scanf("%d",&n);
FOR(i,0,n) scanf("%d",&node[i].p);
FOR(i,0,n) scanf("%d",&node[i].v);
sort(node,node+n,[](Node &a, Node &b){
return a.p < b.p;
});
FOR(i,0,n) node[i].no=i;
memcpy(v,node,sizeof(node));
v[0]={0,0,0};
sort(v+1,v+n);
FOR(i,1,n){
pos[v[i].no]=i;
s.add(i,v[i].p);
c.add(i,1);
}
#ifdef DEBUG
FOR(i,0,n) printf(" (%d, %d)",node[i].p,node[i].v); putchar('\n');
FOR(i,0,n) printf(" (%d, %d)",v[i].p,v[i].v); putchar('\n');
#endif
LL ans = 0;
FOR(i,0,n-1){
int j = lower_bound(v+1,v+n,node[i]) - v;
if(j>=n) continue;
#ifdef DEBUG
FOR(i,1,n) printf(" s[%d]: %lld, c[%d]: %lld\n",i,s[i],i,c[i]);
printf(" for i: %d (v %d), j: %d\n",i,node[i].v,j);
printf(" s[n]:%lld, s[j-1]: %lld, c[j-1]: %lld\n",s[n],s[j-1],c[j-1]);
printf(" ans += %lld\n",s[n]-s[j-1] - (n - 1ll - c[j-1])*node[i].p);
#endif
ans += s[n]-s[j-1] - (c[n] - c[j-1])*node[i].p;
int t = pos[i+1];
s.add(t,-v[t].p);
c.add(t,-1);
}
printf("%lld\n",ans);
return 0;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
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));
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>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
std::cerr << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');
std::cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
struct FenwickTree {
vector<long long> bit;
long long n;
vector<long long> cnt;
FenwickTree(long long n) {
this->n = n;
bit.assign(n, 0);
cnt.assign(n, 0);
}
FenwickTree(vector<long long> a) : FenwickTree(a.size()) {
for (size_t i = 0; i < a.size(); i++) add(i, a[i]);
}
pair<long long, long long> sum(long long r) {
long long ret = 0;
long long t = r;
for (; r >= 0; r = (r & (r + 1)) - 1) ret += bit[r];
long long ret2 = 0;
r = t;
for (; r >= 0; r = (r & (r + 1)) - 1) ret2 += cnt[r];
return {ret, ret2};
}
pair<long long, long long> sum(long long l, long long r) {
auto j = sum(r);
auto j2 = sum(l - 1);
return {j.first - j2.first, j.second - j2.second};
}
void add(long long idx, long long delta) {
long long t = idx;
for (; idx < n; idx = idx | (idx + 1)) bit[idx] += delta;
idx = t;
for (; idx < n; idx = idx | (idx + 1)) cnt[idx] += 1;
}
};
FenwickTree ft(1000005);
map<long long, long long> compress;
signed main() {
ios_base::sync_with_stdio(0);
long long TESTS = 1;
while (TESTS--) {
long long n;
cin >> n;
long long x[n];
for (long long i = 0; i < n; i++) cin >> x[i];
long long v[n];
for (long long i = 0; i < n; i++) cin >> v[i];
set<long long> vv;
for (long long i = 0; i < n; i++) {
vv.insert(abs(v[i]));
}
long long i = 0;
for (auto j : vv) {
if (j == 0)
compress[j] = 0;
else {
compress[j] = i + 1;
i++;
}
}
for (long long i = 0; i < n; i++) {
if (v[i] >= 0)
v[i] = compress[v[i]];
else
v[i] = -compress[-v[i]];
}
vector<pair<long long, long long>> ve;
for (long long i = 0; i < n; i++) ve.push_back({x[i], v[i]});
sort((ve).begin(), (ve).end());
for (long long i = 0; i < n; i++) {
x[i] = ve[i].first;
v[i] = ve[i].second;
}
long long left = 0, right = 0;
long long cntl = 0, cntr = 0;
long long ans = 0;
for (long long i = 0; i < n; i++) {
if (v[i] > 0) {
ans += x[i] * cntl - left;
} else if (v[i] < 0) {
cntl++;
left += x[i];
}
}
for (long long i = 0; i < n; i++) {
if (v[i] > 0) {
ans += ft.sum(0, v[i]).second * x[i] - ft.sum(0, v[i]).first;
ft.add(v[i], x[i]);
}
}
for (long long i = 0; i < 1000005; i++) {
ft.bit[i] = 0;
ft.cnt[i] = 0;
}
for (long long i = 0; i < n; i++) {
if (v[i] < 0) {
v[i] = -v[i];
ans +=
ft.sum(v[i], 1000000).second * x[i] - ft.sum(v[i], 1000000).first;
ft.add(v[i], x[i]);
}
}
long long sum = 0;
long long cnt = 0;
for (long long i = 0; i < n; i++) {
if (v[i] == 0) {
ans += cnt * x[i] - sum;
sum += x[i];
cnt++;
}
}
cout << ans;
}
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
java
|
//package psa.minrazdalja;
import java.util.ArrayList;
import java.util.Scanner; // Import the Scanner class
import javax.swing.plaf.synth.SynthSpinnerUI;
public class minrazdalja {
static class MinStruktua {
private int x;
private float v;
public MinStruktua(int x, int v) {
this.x = x;
this.v = v;
}
public void dodajv(float v) {
this.v = v;
}
}
static int cont = 0;
public static void elele(ArrayList<MinStruktua> elementi) {
long 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) {
float vx = elementi.get(i).v;
float vy = elementi.get(j).v;
//System.out.println("(" +ix + "," + jx+")");
System.out.println(vy);
skupek += vrniMax(ix, jx, vx, vy);
cont += 1;
}
}
}
//2 1 4 3 5
//2 2 2 3 4
}
System.out.println(cont);
System.out.println(skupek);
}
// 2 3 3 2
public static long vrniMax(int m, int n, float i, float j) {
//najdemo max
//nardimo primerjave in vrnemo ustrezno
long x = 0;
int absol = Math.abs(m-n);
if(m < n) {
if(j < 0 && i >= 0) {
x =+ 0;
}else if(j >= 0 && i < 0){
x =+ absol;
}else if(j < 0 && i < 0) {
if(j > i){
x =+ absol;
} else {
x =+ 0;
}
}else{
if(j != i) {
if(j > i){
x =+ absol;
}else{
x =+ 0;
}
}else{
x =+ absol;
}
}
};
//System.out.println(m + " " + n + " " + i + " " + j + " " + x);
return x;
}
public static void main(String[] args) {
int st_el;
Scanner skener = new Scanner(System.in); // Create a Scanner object
//System.out.println("Vnesi stevilo elementov: ");
st_el = skener.nextInt();
//System.out.println(st_el);
ArrayList<MinStruktua> elementi = new ArrayList<>(st_el);
//System.out.println("Vnesi elemente: ");
for(int i=0; i<st_el; i++) {
int x = skener.nextInt();
elementi.add(new MinStruktua(x, 0));
}
//System.out.println("Vnesi tezo elementov: ");
for(int i=0; i<st_el; i++) {
float 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 n;
struct dd {
int gt;
int x;
int v;
};
vector<dd> a;
struct Data {
long long sum;
int sl;
};
Data it[200009 * 8];
Data ans;
long long res;
void update(int i, int l, int r, int pos, int data) {
if (l > pos || r < pos) return;
if (l == r) {
it[i].sum = data;
it[i].sl = 1;
return;
}
int mid = (l + r) / 2;
update(i * 2, l, mid, pos, data);
update(i * 2 + 1, mid + 1, r, pos, data);
it[i].sum = it[i * 2].sum + it[i * 2 + 1].sum;
it[i].sl = it[i * 2].sl + it[i * 2 + 1].sl;
}
void find(int i, int l, int r, int u, int v) {
if (l > r) return;
if (r < u || l > v) return;
if (u <= l && r <= v) {
ans.sum = ans.sum + it[i].sum;
ans.sl = ans.sl + it[i].sl;
return;
}
int mid = (l + r) / 2;
find(i * 2, l, mid, u, v);
find(i * 2 + 1, mid + 1, r, u, v);
}
bool cmp(const dd& a, const dd& b) { return a.v < b.v; }
bool cmp1(const dd& a, const dd& b) { return a.x < b.x; }
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
dd tmp;
cin >> tmp.x;
a.push_back(tmp);
}
for (int i = 0; i < n; i++) {
cin >> a[i].v;
}
sort(a.begin(), a.end(), cmp1);
for (int i = 0; i < n; i++) {
a[i].gt = i + 1;
}
sort(a.begin(), a.end(), cmp);
update(1, 1, n, a[n - 1].gt, a[n - 1].x);
for (int i = n - 2; i >= 0; i--) {
long long tmp = a[i].x;
ans.sl = 0;
ans.sum = 0;
find(1, 1, n, a[i].gt + 1, n);
res = res + ans.sum - (tmp * ans.sl);
update(1, 1, n, a[i].gt, a[i].x);
}
cout << res << endl;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
python3
|
import sys
input = sys.stdin.readline
n=int(input())
X=list(map(int,input().split()))
V=list(map(int,input().split()))
XV=[(X[i],V[i]) for i in range(n)]
compression_dict_x={a: ind for ind, a in enumerate(sorted(set(X)))}
compression_dict_v={a: ind+2 for ind, a in enumerate(sorted(set(V)))}
XV=[(compression_dict_x[XV[i][0]], compression_dict_v[XV[i][1]]) for i in range(n)]
XV.sort(reverse=True)
LEN=len(compression_dict_v)+3
BIT1=[0]*(LEN+1)
def update1(v,w):
while v<=LEN:
BIT1[v]+=w
v+=(v&(-v))
def getvalue1(v):
ANS=0
while v!=0:
ANS+=BIT1[v]
v-=(v&(-v))
return ANS
BIT2=[0]*(LEN+1)
def update2(v,w):
while v<=LEN:
BIT2[v]+=w
v+=(v&(-v))
def getvalue2(v):
ANS=0
while v!=0:
ANS+=BIT2[v]
v-=(v&(-v))
return ANS
ANS=0
for x,v in XV:
ANS+=(getvalue2(LEN)-getvalue2(v-1))-(getvalue1(LEN)-getvalue1(v-1))*x
#print(getvalue2(LEN),getvalue2(v-1),getvalue1(LEN),getvalue1(v-1))
#print(x,v,ANS)
update1(v,1)
update2(v,x)
print(ANS)
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int n;
void update(vector<long long> &b, int ind, int numb) {
for (int i = ind; i < b.size(); i = ((i + 1) | i)) b[i] += numb;
}
int Sum(vector<long long> &b, int ind) {
int ans = 0;
for (int i = ind; i >= 0; i = ((i + 1) & i) - 1) ans += b[i];
return ans;
}
int sum(vector<long long> &b, int l, int r) {
return Sum(b, r) - (l > 0 ? Sum(b, l - 1) : 0);
}
bool comp(pair<int, int> c, pair<int, int> d) {
if (c.second < d.second) return true;
return false;
}
int main() {
cin >> n;
vector<pair<int, int> > a(n);
for (int i = 0; i < n; ++i) cin >> a[i].first;
for (int i = 0; i < n; ++i) cin >> a[i].second;
sort(a.begin(), a.end(), comp);
int k = 1, temp = -1e9;
for (int i = 0; i < n; ++i)
if (temp == a[i].second) {
a[i].second = k;
} else {
a[i].second = ++k;
temp = a[i].second;
}
sort(a.begin(), a.end());
long long ans = 0;
vector<long long> sumx(k + 2, 0), sumv(k + 2, 0);
for (int i = 0; i < n; ++i) {
int ind = a[i].second;
ans += Sum(sumv, ind) * 1ll * a[i].first - Sum(sumx, ind);
update(sumv, ind, 1);
update(sumx, ind, a[i].first);
}
cout << ans;
return 0;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
using namespace std::chrono;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
///find_by_order, order_of_key
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef vector<int> vi;
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
void update(vector<ll> &bit, ll index, ll delta)
{
++index;
for(; index < bit.size(); index += index&-index)
{
bit[index-1] += (ll)delta;
}
}
ll sum(vector<ll> &bit, ll index)
{
ll suma = 0LL;
++index;
for(; index >= 1; index -= index&-index)
{
suma += bit[index-1];
}
return suma;
}
int main()
{
#ifdef ljuba
auto pocetakChronoSata123 = high_resolution_clock::now();
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
vector<pii> p(n);
for(auto &z : p)
cin >> z.fi;
for(auto &z : p)
cin >> z.se;
sort(all(p));
vi brzine;
for(int i = 0; i < n; ++i)
brzine.pb(p[i].se);
sort(all(brzine));
brzine.resize(unique(all(brzine)) - brzine.begin());
ll res = 0;
vector<ll> cnt(brzine.size()), manjeBrzine(brzine.size());
for(auto &pnt : p)
{
int index = lower_bound(all(brzine), pnt.se) - brzine.begin();
res += sum(cnt, index) * 1LL * (ll)pnt.fi - sum(manjeBrzine, index);
update(cnt, index, 1);
update(manjeBrzine, index, pnt.fi);
}
cout << res << '\n';
#ifdef ljuba
auto krajChronoSata123 = high_resolution_clock::now();
auto trajanje = duration_cast<microseconds>(krajChronoSata123 - pocetakChronoSata123);
cout << "----------------------------------------------------\n";
cout << "Vreme u milisekundama: " << (ld)trajanje.count() / 1000.00 << '\n';
#endif
return 0;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
java
|
import java.util.Scanner;
public class Moving_Points {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int[] a = new int[n];
int[] b = new int[n];
for (int i=0;i<n;i++)
a[i] = scan.nextInt();
for (int i=0;i<n;i++)
b[i] = scan.nextInt();
int sum = 0;
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++)
sum += distanceOfTwoPoints(a[i], b[i], a[j], b[j]);
System.out.println(sum);
}
private static int distanceOfTwoPoints(int pos1, int v1, int pos2, int v2) {
if (pos1 < pos2 && v1 <= v2)
return pos2 - pos1;
else if (pos2 < pos1 && v2 <= v1)
return pos1 - pos2;
else 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);
}
while (!q.empty()) {
pair<long long, long long> p = q.front();
q.pop();
long long x = p.first;
long long dir = p.second;
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);
add(x, invG[x], dir);
add(x, 1, dir + 2);
}
}
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
|
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.StringTokenizer;
public class Round624F {
public static class node{
int pos;
int neg;
long negsum;
long possum;
public node(int pos, int neg, long possum, long negsum) {
this.pos = pos;
this.neg = neg;
this.negsum = negsum;
this.possum = possum;
}
}
public static node merge(node l, node r) {
return new node(l.pos + r.pos, l.neg + r.neg, l.possum + r.possum, l.negsum + r.negsum);
}
public static void Update(node[] tree, int start, int end, int index, int i, int type, long speed) {
if(start > end || i < start || i > end) {
return;
}
if(start == end) {
tree[index] = new node(type == 1? 1:0, type == -1?1:0, type == 1 ? speed : 0, type == -1? speed : 0);
return;
}
int mid = (start + end)>>1;
Update(tree, start, mid, index<<1, i, type, speed);
Update(tree, mid+1, end, index<<1|1, i, type, speed);
tree[index] = merge(tree[index<<1], tree[index<<1|1]);
}
public static node Query(node[] tree, int start, int end, int index, int i, int j) {
if(start > end || j < start || i > end) {
return new node(0,0,0,0);
}
if(i <= start && j >= end) {
return tree[index];
}
int mid = (start + end)>>1;
node left = Query(tree, start, mid, index<<1, i, j);
node right = Query(tree, mid + 1, end, index<<1|1, i , j);
return merge(left, right);
}
public static class Pair implements Comparable<Pair>{
int x;
int v;
public Pair(int x, int v) {
this.x = x;
this.v = v;
}
public int compareTo(Pair x) {
return this.x - x.x;
}
}
public static class Triple{
int start;
int end;
int idx;
public Triple(int start, int end, int idx) {
this.start = start;
this.end = end;
this.idx = idx;
}
}
public static void solve() {
int n = s.nextInt();
Pair[] points = new Pair[n];
Pair[] reverse = new Pair[n];
int[] xs = new int[n];
for(int i = 0; i < n; i++) {
xs[i] = s.nextInt();
}
int[] vs = new int[n];
for(int i = 0; i < n; i++) {
vs[i] = s.nextInt();
}
for(int i = 0; i < n; i++) {
int x = xs[i], v = vs[i];
points[i] = new Pair(x, v);
reverse[i] = new Pair(Math.abs(v), Math.abs(v)==v?1:-1);
}
Arrays.sort(points);
Arrays.sort(reverse);
HashMap<Integer,Triple> helper = new HashMap<>();
for(int i = 0; i < n; i++) {
if(helper.containsKey(reverse[i].x)) {
helper.get(reverse[i].x).end = i;
}else {
helper.put(reverse[i].x, new Triple(i, i, i));
}
}
node[] tree = new node[4 * n + 1];
Arrays.fill(tree, new node(0,0,0,0));
long ans = 0;
for(int i = 0; i < points.length; i++) {
int v = points[i].v;
int id = helper.get(Math.abs(v)).idx;
helper.get(Math.abs(v)).idx++;
int mid = helper.get(Math.abs(v)).end;
node left = Query(tree, 0, n - 1, 1, 0, mid);
node right = Query(tree, 0, n - 1, 1, mid + 1, n - 1);
long val = points[i].x;
if(Math.abs(v) == v) {
ans += - left.possum + left.pos * val;
ans += - left.negsum + left.neg * val;
ans += - right.negsum + right.neg * val;
}else {
ans += - right.negsum + right.neg * val;
}
Update(tree, 0, n - 1, 1, id, v < 0 ? -1 : 1, val);
}
out.println(ans);
}
public static void main(String[] args) {
out = new PrintWriter(new BufferedOutputStream(System.out));
s = new FastReader();
solve();
out.close();
}
public static FastReader s;
public static PrintWriter out;
public static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
long nextLong() {
return Long.parseLong(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
}
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int x[n], v[n];
for (int i = 0; i < n; i++) {
cin >> x[i];
}
for (int i = 0; i < n; i++) {
cin >> v[i];
}
int d = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (v[i] * v[j] > 0 && x[i] != x[j]) {
if ((v[i] - v[j]) * (x[i] - x[j]) >= 0) {
d += abs(x[i] - x[j]);
}
} else if (v[i] * v[j] < 0 && x[i] != x[j]) {
if ((x[i] - x[j]) * (v[i] - v[j]) >= 0) {
d += abs(x[i] - x[j]);
} else {
{
if (v[i] < 0 && v[j] > 0) {
d += x[j] - x[i];
}
}
}
} else {
if ((x[i] - x[j]) * (v[i] - v[j]) > 0) {
d += abs(x[i] - x[j]);
}
}
}
}
cout << d;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
java
|
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.StringTokenizer;
public class C1311F_2 {
public static void main(String[] args) {
var scanner = new BufferedScanner();
var writer = new PrintWriter(new BufferedOutputStream(System.out));
var n = scanner.nextInt();
var x = new int[n];
for (int i = 0; i < n; i++) {
x[i] = scanner.nextInt();
}
var v = new int[n];
for (int i = 0; i < n; i++) {
v[i] = scanner.nextInt();
}
writer.println(solve(n, x, v));
scanner.close();
writer.flush();
writer.close();
}
static final int MIN_V = (int) -1e8;
private static long solve(int n, int[] x, int[] v) {
var orderX = new ArrayList<Integer>();
for (int i = 0; i < n; i++) {
orderX.add(i);
}
orderX.sort(Comparator.comparingInt(o -> x[o]));
var ans = 0L;
var tree = new SegTree(v);
for (int i = 0; i < n; i++) {
var thisX = x[orderX.get(i)];
var thisV = v[orderX.get(i)];
if (thisV >= 0) {
var left = tree.acc(MIN_V, 0);
ans += left[1] * thisX - left[0];
var right = tree.acc(0, thisV);
ans += right[1] * thisX - right[0];
tree.add(thisV, thisX);
} else {
var left = tree.acc(MIN_V, thisV);
ans += left[1] * thisX - left[0];
tree.add(thisV, thisX);
}
}
return ans;
}
static class SegTree {
final List<Node> nodes = new ArrayList<>();
/**
* 所有有效的key,递增。
*/
final int[] keys;
SegTree(int[] keys) {
this.keys = distinctAndAsc(keys);
nodes.add(new Node(0, this.keys.length - 1));
}
static int[] distinctAndAsc(int[] keys) {
var distinct = new HashSet<Integer>();
for (int key : keys) {
distinct.add(key);
}
var asc = new ArrayList<>(distinct);
asc.sort(Integer::compareTo);
return asc.stream().mapToInt(i -> i).toArray();
}
void add(int key, int value) {
add(0, key, value);
}
private void add(int index, int key, int value) {
if (index < 0) {
return;
}
var node = nodes.get(index);
// debug("node %d: (low=%d,highEx=%d)", index, node.low, node.high);
if (key < keys[node.low] || keys[node.high] < key) {
return;
}
node.acc[0] += value;
node.acc[1]++;
if (node.isLeaf()) {
return;
}
if (node.left < 0) {
nodes.add(new Node(node.low, node.mid));
node.left = nodes.size() - 1;
}
add(node.left, key, value);
if (node.right < 0) {
nodes.add(new Node(node.mid + 1, node.high));
node.right = nodes.size() - 1;
}
add(node.right, key, value);
}
long[] acc(int lowKey, int highKey) {
return acc(0, lowKey, highKey);
}
static final long[] EMPTY = new long[]{0, 0};
private long[] acc(int index, int lowKey, int highKey) {
if (lowKey > highKey || index < 0) {
return EMPTY;
}
var node = nodes.get(index);
if (highKey < keys[node.low] || keys[node.high] < lowKey) {
return EMPTY;
}
if (lowKey <= keys[node.low] && keys[node.high] <= highKey) {
return node.acc;
}
var left = acc(node.left, lowKey, highKey);
var right = acc(node.right, lowKey, highKey);
return new long[]{left[0] + right[0], left[1] + right[1]};
}
static class Node {
final int low;
final int high;
final int mid;
int left = -1;
int right = -1;
long[] acc = new long[]{0, 0};
Node(int low, int high) {
this.low = low;
this.high = high;
this.mid = Math.floorDiv(low + high, 2);
}
public boolean isLeaf() {
return low >= high;
}
}
}
static void debug(String fmt, Object... args) {
System.out.println(String.format(fmt, args));
}
public static class BufferedScanner {
BufferedReader br;
StringTokenizer st;
public BufferedScanner(Reader reader) {
br = new BufferedReader(reader);
}
public BufferedScanner() {
this(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
void close() {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
static long gcd(long a, long b) {
if (a < b) {
return gcd(b, a);
}
while (b > 0) {
long tmp = b;
b = a % b;
a = tmp;
}
return a;
}
static long inverse(long a, long m) {
long[] ans = extgcd(a, m);
return ans[0] == 1 ? (ans[1] + m) % m : -1;
}
private static long[] extgcd(long a, long m) {
if (m == 0) {
return new long[]{a, 1, 0};
} else {
long[] ans = extgcd(m, a % m);
long tmp = ans[1];
ans[1] = ans[2];
ans[2] = tmp;
ans[2] -= ans[1] * (a / m);
return ans;
}
}
private static List<Integer> primes(double upperBound) {
var limit = (int) Math.sqrt(upperBound);
var isComposite = new boolean[limit + 1];
var primes = new ArrayList<Integer>();
for (int i = 2; i <= limit; i++) {
if (isComposite[i]) {
continue;
}
primes.add(i);
int j = i + i;
while (j <= limit) {
isComposite[j] = true;
j += i;
}
}
return primes;
}
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
java
|
import java.util.*;
import java.lang.*;
import java.io.*;
public class F {
public static void main (String[] args) throws java.lang.Exception {
new Solution();
}
}
class Solution {
Scanner scanner;
public Solution() {
scanner = new Scanner(System.in);
while (scanner.hasNext()) {
run_case();
}
}
private void run_case() {
String line = scanner.nextLine();
int[] pos = strToIntArray(scanner.nextLine());
int[] speed = strToIntArray(scanner.nextLine());
List<Rock> list = new ArrayList<>();
for (int i=0; i<pos.length; i++) {
list.add(new Rock(pos[i], speed[i]));
}
Collections.sort(list, new ComparatorRock());
long res = 0;
int inf = (int)1e8 + 5;
HashMap<Integer, Integer> cnt = new HashMap<>();
SegmentTreeRSQ st_cnt = new SegmentTreeRSQ(-inf, inf);
SegmentTreeRSQ st_pos = new SegmentTreeRSQ(-inf, inf);
for (int i=list.size()-1; i>=0; i--) {
int cur_v = list.get(i).v;
int larger_cnt = (int)st_cnt.query(cur_v, inf);
long larger_sum = st_pos.query(cur_v, inf);
// list.add(0, res);
cnt.putIfAbsent(cur_v, 0);
cnt.put(cur_v, cnt.get(cur_v) + 1);
st_cnt.update(cur_v, cnt.get(cur_v));
st_pos.update(cur_v, st_pos.query(cur_v, cur_v) + list.get(i).pos);
res += larger_sum - larger_cnt * list.get(i).pos;
}
// for (int i=0; i<list.size(); i++) {
// for (int j=i+1; j<list.size(); j++) {
// if (list.get(j).v >= list.get(i).v) {
// res += list.get(j).pos - list.get(i).pos;
// }
// }
// }
System.out.println(res);
return;
}
private int[] strToIntArray(String str) {
String[] vals = str.split("\\s+");
int sz = vals.length;
int[] res = new int[sz];
for (int i=0; i<sz; i++) {
res[i] = Integer.parseInt(vals[i]);
}
return res;
}
}
class Rock {
public int pos, v;
public Rock(int pos, int v) {
this.pos = pos;
this.v = v;
}
}
class ComparatorRock implements Comparator<Rock> {
public int compare(Rock a, Rock b) {
return a.pos - b.pos;
}
}
class SegmentTreeRSQ {
SegmentTreeRSQNode root = null;
int[] nums;
int inf = Integer.MAX_VALUE;
public SegmentTreeRSQ(int start, int end) {
root = new SegmentTreeRSQNode(start, end);
}
public void update(int i, long val) {
if (root == null || i < root.start || i > root.end) return;
update(root, i, val);
}
public void update(SegmentTreeRSQNode root, int i, long val) {
if (root == null) return;
if (root.start == i && root.end == i) root.sum = val;
else {
int mid = root.mid();
// split
// if (root.left == null) root.left = new SegmentTreeRSQNode(root.start, mid);
// if (root.right == null) root.right = new SegmentTreeRSQNode(mid+1, root.end);
// visit
if (i <= mid) {
if (root.left == null) root.left = new SegmentTreeRSQNode(root.start, mid);
update(root.left, i, val);
} else {
if (root.right == null) root.right = new SegmentTreeRSQNode(mid+1, root.end);
update(root.right, i, val);
}
// merge
long sum_l = root.left != null ? root.left.sum : 0;
long sum_r = root.right != null ? root.right.sum : 0;
root.sum = sum_l + sum_r;
}
}
public long query(int start, int end) {
if (root == null) return 0;
else return query(root, start, end);
}
public long query(SegmentTreeRSQNode root, int start, int end) {
if (root == null) return 0;
if (start > end) {
return 0;
} else if (root.start == start && root.end == end){
return root.sum;
} else {
int mid = root.mid();
if (end <= mid) { // in left
return query(root.left, start, end);
} else if (start >= mid+1) { // in right
return query(root.right, start, end);
} else {
return query(root.left, start, mid) + query(root.right, mid+1, end);
}
}
}
}
class SegmentTreeRSQNode {
int inf = Integer.MAX_VALUE;
public int start, end, min, max;
public long sum;
public SegmentTreeRSQNode left, right;
public SegmentTreeRSQNode(int start, int end) {
this.left = null;
this.right = null;
this.start = start;
this.end = end;
this.min = inf;
this.max = -inf;
this.sum = 0;
}
public int mid() {return this.start + (this.end - this.start) / 2;}
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-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=[int(x) for x in input().split()]
v=[int(x) for x in input().split()]
d=0
for i in range(n):
for j in range(i+1, n):
if v[i]==v[j]:
d+=abs(x[i]-x[j])
elif v[i]<v[j] and x[i]<x[j]:
d+=abs(x[i]-x[j])
elif v[j]>v[i] and x[j]>x[i]:
d+=abs(x[i]-x[j])
print(d)
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
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 {
int 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 + 5;
int n, m, pos, s[N][2], v[N];
long long tot;
struct node {
long long x, v;
} a[N];
bool cmp(node x, node y) { return x.x < y.x; }
int lowbit(int x) { return x & (-x); }
void update(int x, int val) {
while (x <= n) {
s[x][0]++;
s[x][1] += val;
x += lowbit(x);
}
}
long long getsum(int x, int flag) {
long long res = 0;
while (x) {
res += s[x][flag];
x -= lowbit(x);
}
return res;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%lld", &a[i].x);
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i].v);
v[i] = a[i].v;
}
sort(a + 1, a + n + 1, cmp);
sort(v + 1, v + n + 1);
m = unique(v + 1, v + n + 1) - v - 1;
for (int i = 1; i <= n; i++) {
pos = lower_bound(v + 1, v + m + 1, a[i].v) - v;
tot += getsum(pos, 0) * a[i].x - getsum(pos, 1);
update(pos, a[i].x);
}
printf("%lld", tot);
return 0;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
vector<long long> x, v;
int idX(int val) { return (lower_bound(x.begin(), x.end(), val) - x.begin()); }
int idV(int val) { return (lower_bound(v.begin(), v.end(), val) - v.begin()); }
struct FenwickTree {
vector<long long> bit;
int n;
FenwickTree(int n) {
this->n = n;
bit.assign(n, 0);
}
long long sum(int r) {
long long ret = 0;
for (; r >= 0; r = (r & (r + 1)) - 1) ret += bit[r];
return ret;
}
long long sum(int l, int r) { return sum(r) - sum(l - 1); }
void upd(int idx, int delta) {
for (; idx < n; idx = idx | (idx + 1)) bit[idx] += delta;
}
};
void nikal_pehli_fursat_mai() {
int n;
cin >> n;
vector<array<long long, 2>> arr(n);
long long ans = 0;
for (int i = 0; i < n; i++) {
cin >> arr[i][0];
x.push_back(arr[i][0]);
}
for (int i = 0; i < n; i++) {
cin >> arr[i][1];
v.push_back(abs(arr[i][1]));
}
sort(v.begin(), v.end());
sort(x.begin(), x.end());
sort(arr.begin(), arr.end());
FenwickTree pos(n), neg(n), zero(n), cntp(n), cntn(n), cntz(n);
for (int i = 0; i < n; i++) {
int id = idV(abs(arr[i][1]));
if (arr[i][1] > 0) {
cntp.upd(id, 1);
pos.upd(id, arr[i][0]);
} else if (arr[i][1] < 0) {
cntn.upd(id, 1);
neg.upd(id, arr[i][0]);
} else {
cntz.upd(id, 1);
zero.upd(id, arr[i][0]);
}
}
for (int i = 0; i < n; i++) {
int id = idV(abs(arr[i][1]));
if (arr[i][1] > 0) {
ans += pos.sum(id, n - 1) - cntp.sum(id, n - 1) * arr[i][0];
cntp.upd(id, -1);
pos.upd(id, -arr[i][0]);
} else if (arr[i][1] < 0) {
ans += pos.sum(0, n - 1) - cntp.sum(0, n - 1) * arr[i][0];
ans += neg.sum(0, id) - cntn.sum(0, id) * arr[i][0];
cntn.upd(id, -1);
neg.upd(id, -arr[i][0]);
} else {
ans += pos.sum(id, n - 1) - cntp.sum(id, n - 1) * arr[i][0];
ans += zero.sum(0, n - 1) - cntz.sum(0, n - 1) * arr[i][0];
cntz.upd(id, -1);
zero.upd(id, -arr[i][1]);
}
}
cout << ans << "\n";
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int tc = 1;
while (tc--) {
nikal_pehli_fursat_mai();
}
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int n, len;
int bit[N][2];
void add(int x, int val) {
while (x <= n) {
bit[x][0]++;
bit[x][1] += val;
x += x & -x;
}
}
long long query(int x, int k) {
long long res = 0;
while (x) {
res += 1LL * bit[x][k];
x -= x & -x;
}
return res;
}
int main() {
scanf("%d", &n);
vector<pair<int, int> > a(n);
vector<int> v(n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i].first);
}
for (int i = 0; i < n; i++) {
scanf("%d", &a[i].second);
v[i] = a[i].second;
}
sort(a.begin(), a.end());
sort(v.begin(), v.end());
len = unique(v.begin(), v.end()) - v.begin();
long long ans = 0;
for (int i = 0; i < n; i++) {
int now =
lower_bound(v.begin(), v.begin() + len, a[i].second) - v.begin() + 1;
ans += 1LL * a[i].first * query(now, 0) - query(now, 1);
add(now, a[i].first);
}
printf("%lld\n", ans);
return 0;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
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
|
java
|
import java.io.*;
import java.util.*;
import java.util.concurrent.TimeUnit;
public class f624 implements Runnable{
public static void main(String[] args) {
try{
new Thread(null, new f624(), "process", 1<<26).start();
}
catch(Exception e){
System.out.println(e);
}
}
public void run() {
FastReader scan = new FastReader();
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
//PrintWriter out = new PrintWriter("file.out");
Task solver = new Task();
//int t = scan.nextInt();
int t = 1;
for(int i = 1; i <= t; i++) solver.solve(i, scan, out);
out.close();
}
static class Task {
static final int inf = Integer.MAX_VALUE;
public void solve(int testNumber, FastReader sc, PrintWriter out) {
int N = sc.nextInt();
tup[] points = new tup[N];
long[] v = new long[N];
for(int i = 0; i < N; i++) {
points[i] = new tup();
points[i].a = sc.nextInt();
}
for(int i = 0; i < N; i++) {
points[i].b = sc.nextInt();
v[i] = points[i].b;
}
Arrays.sort(points, new tup());
//System.out.println(Arrays.toString(points));
int ID = 0;
for(int i = 0; i < N; i++) {
if(i == 0) {
points[i].b = ++ID;
continue;
}
if(v[i] == v[i-1])
points[i].b = ID;
else
points[i].b = ++ID;
}
// System.out.println(Arrays.toString(points));
Arrays.sort(points, new comp());
//System.out.println(Arrays.toString(points));
segt segtree = new segt(ID+1);
long totalSum = 0;
for(int i = 0; i < N; i++) {
tup res = segtree.querySum((int)points[i].b, ID);
//System.out.println(res);
long sum = res.a;
long nodes = res.b;
totalSum += sum - nodes * points[i].a;
segtree.update((int)points[i].b, (int)points[i].a);
// System.out.println(sum + " " + nodes + " " + totalSum);
// System.out.println(Arrays.toString(segtree.t));
//System.out.println("YEE");
}
out.println(totalSum);
}
static final tup ZERO = new tup(0, 0);
static class segt {
tup[] t;
int N;
public segt(int n) {
t = new tup[4*n];
for(int i = 0; i < t.length; i++) {
t[i] = new tup(0, 0);
}
N = n;
}
tup querySum(int l, int r) {
return querySum(1, 0, N-1, l, r);
}
tup querySum(int v, int tl, int tr, int l, int r) {
if (l > r)
return ZERO;
if (l == tl && r == tr)
return t[v];
int tm = (tl + tr) / 2;
tup A = querySum(v*2, tl, tm, l, Math.min(r, tm));
tup B = querySum(v*2+1, tm+1, tr, Math.max(l, tm+1), r);
return new tup(A.a + B.a, A.b + B.b);
}
void update(int pos, int new_val) {
update(1, 0, N-1, pos, new_val);
}
void update(int v, int tl, int tr, int pos, long new_val) {
//System.out.println(v + " " + tl + tr + pos + new_val);
if (tl == tr) {
t[v].a += new_val;
t[v].b ++;
} else {
int tm = (tl + tr) / 2;
if (pos <= tm)
update(v*2, tl, tm, pos, new_val);
else
update(v*2+1, tm+1, tr, pos, new_val);
t[v].a = t[v*2].a + t[v*2+1].a;
t[v].b = t[v*2].b + t[v*2+1].b;
}
}
}
}
static long binpow(long a, long b, long m) {
a %= m;
long res = 1;
while (b > 0) {
if ((b & 1) == 1)
res = res * a % m;
a = a * a % m;
b >>= 1;
}
return res;
}
static void sort(int[] x){
shuffle(x);
Arrays.sort(x);
}
static void sort(long[] x){
shuffle(x);
Arrays.sort(x);
}
static class comp implements Comparator<tup>{
@Override
public int compare(tup o1, tup o2) {
// TODO Auto-generated method stub
return Long.compare(o2.a, o1.a);
}
}
static class tup implements Comparable<tup>, Comparator<tup>{
long a, b;
tup(long a,long b){
this.a=a;
this.b=b;
}
public tup() {
}
@Override
public int compareTo(tup o){
return Long.compare(b,o.b);
}
@Override
public int compare(tup o1, tup o2) {
return Long.compare(o1.b, o2.b);
}
@Override
public int hashCode() {
return Objects.hash(a, b);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
tup other = (tup) obj;
return a==other.a && b==other.b;
}
@Override
public String toString() {
return a + " " + b;
}
}
static void shuffle(int[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
int temp = a[i];
a[i] = a[r];
a[r] = temp;
}
}
static void shuffle(long[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
long temp = a[i];
a[i] = a[r];
a[r] = temp;
}
}
static 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
|
java
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class MovingPoints {
public static void main(String[] args) throws IOException {
BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(f.readLine());
StringTokenizer tokenizer = new StringTokenizer(f.readLine());
ValueAndIndex[] speedAndIndices = new ValueAndIndex[n];
ValueAndIndex[] positionAndIndex = new ValueAndIndex[n];
int[] position = new int[n];
for (int i = 0; i < n; i++) {
int pos = Integer.parseInt(tokenizer.nextToken());
position[i] = pos;
positionAndIndex[i] = new ValueAndIndex(pos, i, pos);
}
Arrays.sort(positionAndIndex);
tokenizer = new StringTokenizer(f.readLine());
for (int i = 0; i < n; i++) {
speedAndIndices[i] = new ValueAndIndex(Integer.parseInt(tokenizer.nextToken()), i, position[i]);
}
position = null;
Arrays.sort(speedAndIndices);
int[] sortedSpeedIndex = new int[n];
for (int i = 0; i < n; i++) {
ValueAndIndex speedAndIndex = speedAndIndices[i];
sortedSpeedIndex[speedAndIndex.index] = i;
}
speedAndIndices = null;
FenwickTree treePosition = new FenwickTree(n);
FenwickTree treeNumOfDifferent = new FenwickTree(n);
treePosition.add(sortedSpeedIndex[positionAndIndex[0].index], positionAndIndex[0].value);
treeNumOfDifferent.add(sortedSpeedIndex[positionAndIndex[0].index], 1);
long sum = 0;
for (int i = 1; i < n; i++) {
ValueAndIndex positionAndInd = positionAndIndex[i];
//System.out.println(i + " " + positionAndInd.value + " " + sortedSpeedIndex[positionAndInd.index] + " " + treePosition.getSum(0, sortedSpeedIndex[i]) + " " + ((long) positionAndInd.value * treeNumOfDifferent.getSum(0, sortedSpeedIndex[i])));
//System.out.println(positionAndInd.value + " " + treeNumOfDifferent.getSum(0, sortedSpeedIndex[positionAndInd.index]));
sum -= treePosition.getSum(0, sortedSpeedIndex[positionAndInd.index]);
sum += (long) positionAndInd.value * treeNumOfDifferent.getSum(0, sortedSpeedIndex[positionAndInd.index]);
treePosition.add(sortedSpeedIndex[positionAndInd.index], positionAndInd.value);
treeNumOfDifferent.add(sortedSpeedIndex[positionAndInd.index], 1);
}
System.out.println(sum);
}
private static class ValueAndIndex implements Comparable<ValueAndIndex> {
int value;
int index;
int comparingValue;
public ValueAndIndex(int speed, int index, int comparingValue) {
this.value = speed;
this.index = index;
this.comparingValue = comparingValue;
}
@Override
public int compareTo(ValueAndIndex speedAndIndex) {
if (this.value < speedAndIndex.value) {
return -1;
} else if (this.value > speedAndIndex.value) {
return 1;
}
if (comparingValue < speedAndIndex.comparingValue) {
return -1;
} else if (comparingValue > speedAndIndex.comparingValue) {
return 1;
}
return 0;
}
}
private static class FenwickTree {
int[] ar;
public FenwickTree(int n) {
this.ar = new int[n];
}
public long getSum(int i) {
long sum = 0;
while (i >= 0) {
sum += ar[i];
i = (i & (i + 1)) - 1;
}
return sum;
}
public long getSum(int a, int b) {
return getSum(b) - getSum(a - 1);
}
public void add(int i, int num) {
while (i < this.ar.length) {
this.ar[i] += num;
i = i | (i + 1);
}
}
}
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
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) {
int i = DondeEsta[x.pos];
int dif = 0;
dif = distance(usados.begin(),
upper_bound((usados.begin()), (usados.end()), x.pos));
sum += max(long long(0), 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>
using namespace std;
void fio() {}
void pti() {
double timeuse = clock() * 1000.0 / CLOCKS_PER_SEC;
cerr << "Timeuse " << timeuse << "ms" << endl;
}
void end() { exit(0); }
namespace io {
const int SIZ = 55;
int que[SIZ], op, qr;
char ch;
template <class I>
inline void gi(I& w) {
ch = getchar(), op = 1, w = 0;
while (!isdigit(ch)) {
if (ch == '-') op = -1;
ch = getchar();
}
while (isdigit(ch)) {
w = w * 10 + ch - '0';
ch = getchar();
}
w *= op;
}
template <typename T, typename... Args>
inline void gi(T& t, Args&... args) {
gi(t);
gi(args...);
}
template <class I>
inline void print(I w) {
qr = 0;
if (!w) putchar('0');
if (w < 0) putchar('-'), w = -w;
while (w) que[++qr] = w % 10 + '0', w /= 10;
while (qr) putchar(que[qr--]);
}
} // namespace io
using io::gi;
using io::print;
const int N = 2e5 + 5;
int n, num;
struct node {
long long a, b;
};
node e[N];
long long c[N], t[N], sum[N];
map<long long, int> g;
int lowbit(int x) { return x & -x; }
void modify(int x, long long z) {
while (x <= n) ++t[x], sum[x] += z, x += lowbit(x);
}
long long query_t(int x) {
long long res = 0;
while (x) res += t[x], x -= lowbit(x);
return res;
}
long long query_sum(int x) {
long long res = 0;
while (x) res += sum[x], x -= lowbit(x);
return res;
}
int main() {
fio();
gi(n);
for (int i = 1; i <= n; ++i) gi(e[i].a);
for (int i = 1; i <= n; ++i) gi(e[i].b), c[i] = e[i].b;
sort(c + 1, c + n + 1);
for (int i = 1; i <= n; ++i)
if (!g[c[i]]) g[c[i]] = ++num;
sort(e + 1, e + n + 1,
[](node x, node y) { return x.a == y.a ? x.b < y.b : x.a < y.a; });
long long ans = 0;
for (int i = 1, j = 1; i <= n; ++i) {
ans += e[i].a * query_t(g[e[i].b]) - query_sum(g[e[i].b]);
while (j <= n && e[j].b == e[i].b) 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>
#pragma GCC optimize("O3")
using namespace std;
const int N = 2e5 + 5, mny = -1e9;
vector<vector<pair<int, pair<int, int> > > > seg(4 * N);
long long n, ans;
pair<int, int> p[N];
vector<pair<int, pair<int, int> > > merge(
vector<pair<int, pair<int, int> > > l,
vector<pair<int, pair<int, int> > > r) {
vector<pair<int, pair<int, int> > > ans;
merge(l.begin(), l.end(), r.begin(), r.end(), back_inserter(ans));
sort(ans.begin(), ans.end());
for (int i = 1; i < ans.size(); i++)
ans[i].second.second += ans[i - 1].second.second;
return ans;
}
pair<long long, long long> merge(pair<long long, long long> p1,
pair<long long, long long> p2) {
return {p1.first + p2.first, p1.second + p2.second};
}
pair<long long, long long> get_ans(vector<pair<int, pair<int, int> > > node,
int val) {
if (!node.size()) return {0, 0};
pair<long long, long long> ans;
auto idx = lower_bound(node.begin(), node.end(),
make_pair(val, make_pair(mny, mny))) -
node.begin();
ans.second = node.size() - idx;
if (!ans.second) return {0, 0};
ans.first = (idx) ? node.back().second.second - node[idx - 1].second.second
: node.back().second.second;
return ans;
}
void build(int s, int e, int idx) {
if (s == e) {
seg[idx].push_back({p[s].second, {p[s].first, p[s].first}});
return;
}
int mid = (s + e) / 2;
build(s, mid, 2 * idx + 1);
build(mid + 1, e, 2 * idx + 2);
seg[idx] = merge(seg[2 * idx + 1], seg[2 * idx + 2]);
}
pair<long long, long long> qry(int l, int r, int s, int e, int idx, int val) {
if (s > r || l > e) return make_pair(0ll, 0ll);
if (s >= l && e <= r) return get_ans(seg[idx], val);
int mid = (s + e) / 2;
return merge(qry(l, r, s, mid, 2 * idx + 1, val),
qry(l, r, mid + 1, e, 2 * idx + 2, val));
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 0; i < n; i++) cin >> p[i].first;
for (int i = 0; i < n; i++) cin >> p[i].second;
sort(p, p + n);
build(0, n - 1, 0);
long long ans = 0;
for (int i = 0; i < n; i++) {
pair<long long, long long> res =
qry(i + 1, n - 1, 0, n - 1, 0, p[i].second);
ans += res.first - p[i].first * res.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()))
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]
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;
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(), 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
|
#pragma GCC optimize("O3")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define int long long
#define pb push_back
#define pf push_front
#define eb emplace_back
#define mp make_pair
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define f first
#define s second
#define sz(x) (int)x.size()
#define endl "\n"
#define forn(i,n) for(int i=0;i<n;++i)
#define fore(i,l,r) for(int i=int(l);i<=int(r);++i)
#define rep(i,begin,end) for(__typeof(end) i=(begin);i!=(end);i++)
#define fill(a,value) memset(a,value,sizeof(a));
#define gcd(a,b) __gcd((a),(b))
#define watch1(x) cout<<(x)<<endl
#define watch2(x,y) cout<<(x)<<" "<<(y)<<endl
#define watch3(x,y,z) cout<<(x)<<" "<<(y)<<" "<<(z)<<endl
#define fastio ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vpii;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> oset;
const int INF = 9e18;
const int mod = 1e9 + 7;
const int N = 2e5 + 5;
int i,n,v[N],bit[N],val=1,ans=0;
vpii x;
void update(int idx,int val)
{
while(idx<=n)
{
bit[idx]+=val;
idx+=idx&-idx;
}
}
int query(int idx)
{
int ans=0;
while(idx>0)
{
ans+=bit[idx];
idx-=idx&-idx;
}
return ans;
}
int rsum(int l,int r)
{
return query(r)-query(l-1);
}
void solve()
{
cin>>n;
vi compress;
for(i=1;i<=n;++i)
{
int a;
cin>>a;
x.pb({a,i});
}
sort(all(x));
for(i=1;i<=n;++i)
{
cin>>v[i];
compress.pb(v[i]);
}
sort(all(compress));
map<int,int> mp;
mp[compress[0]]=1;
for(i=1;i<n;++i)
{
if(compress[i]!=compress[i-1])
val++;
mp[compress[i]]=val;
}
oset s;
for(i=n-1;i>=0;--i)
{
int idx=x[i].s;
int sum=rsum(mp[v[idx]],n);
int bigger=n-1-i-s.order_of_key(mp[v[idx]]);
ans+=sum-bigger*x[i].f;
s.insert(mp[v[idx]]);
update(mp[v[idx]],x[i].f);
}
cout<<ans;
}
signed main()
{
fastio;
int t;
//cin>>t;
t=1;
while(t--)
{
solve();
}
return 0;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
cpp
|
//#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);
}
}
int glob = 0;
inline void update(int idx,int b,int e,int pos,LL v){
// if(glob>20)return;
// glob++;
// cout<<idx<<" "<<b<<" "<<e<<" "<<pos<<" "<<v<<endl;
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);
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
|
python3
|
def subCost(array):
cost = 0
for i in array:
for j in array:
cost += abs(i[0] - j[0])
return cost // 2
def f(left, right):
cost = 0
for r in right:
for l in left:
if r[0] > l[0]:
cost+=r[0]-l[0]
else:
break
return cost
n = int(input("n: "))
a=[[pos, 0] for pos in list(map(int, input("positions: ").split()))]
for i, spe in enumerate(list(map(int, input("speeds: ").split()))): a[i][1] = spe
a=sorted(a, key = lambda x: x[1])
cost=0
prev=0
for i, p in enumerate(a):
try:
if p[1]<a[i+1][1]:
a[prev:i+1] = sorted(a[prev:i+1], key=lambda x: x[0])
cost+=subCost(a[prev:i+1])
cost+=f(a[prev:i+1], a[i+1:])
prev=i+1
except IndexError:
cost+=subCost(a[prev:i+1])
print(cost)
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-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("%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;
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);
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
|
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)));
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-1)*a[i].x - fenvikSum.sum(cur-1);
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;
int las[5001];
int fa[5001];
int mark[5001];
int dep[5001];
signed main() {
int T;
scanf("%d", &T);
while (T--) {
int n, d;
scanf("%d%d", &n, &d);
if (d > n * (n - 1) / 2) {
printf("NO\n");
continue;
}
memset(dep, 0, sizeof(dep));
memset(fa, 0, sizeof(fa));
memset(mark, 0, sizeof(mark));
memset(las, 0, sizeof(las));
dep[0] = -1;
for (int i = 1; i <= n; i++) {
dep[i] = dep[i / 2] + 1;
d -= dep[i];
fa[i] = i / 2;
}
if (d < 0) {
printf("NO\n");
continue;
}
int x = n, mx = dep[n];
while (x) {
las[dep[x]] = x;
mark[x] = 1;
x = fa[x];
}
for (int i = n; i >= 1; i--)
if (!mark[i]) {
int pre = mx;
while (dep[fa[i]] < pre && d) {
fa[i] = las[dep[fa[i]] + 1];
dep[i] = dep[fa[i]] + 1;
d--;
if (dep[i] > mx) {
mx++;
las[mx] = i;
mark[i] = 1;
}
}
}
printf("YES\n");
for (int i = 2; i <= n; i++) printf("%d ", fa[i]);
printf("\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,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()
from typing import Callable, TypeVar, List
T = TypeVar('T')
class SegTree:
def __init__(self, v: List[T], op: Callable[[T, T], T], e: Callable[[], T]) -> None:
self._n = len(v)
self.log = (self._n - 1).bit_length()
self.size = 1 << self.log
self.d = [e() for _ in range(2 * self.size)]
self.op = op
self.e = e
for i in range(self._n):
self.d[self.size + i] = v[i]
for i in reversed(range(1, self.size)):
self.__update__(i)
@classmethod
def init_e(cls, n: int, op: Callable[[T, T], T], e: Callable[[], T]) -> 'SegTree':
return cls([e() for _ in range(n)], op, e)
#値代入
def set(self, p: int, x: T) -> None:
assert 0 <= p < self._n
p += self.size
self.d[p] = x
for i in range(1, self.log + 1):
self.__update__(p >> i)
def get(self, p: int) -> T:
assert 0 <= p < self._n
return self.d[p + self.size]
#計算 [l,r)
def prod(self, l: int, r: int) -> T:
assert 0 <= l <= self._n and 0 <= r <= self._n
sml = self.e()
smr = self.e()
l += self.size
r += self.size
while l < r:
if l & 1:
sml = self.op(sml, self.d[l])
l += 1
if r & 1:
r -= 1
smr = self.op(self.d[r], smr)
l >>= 1
r >>= 1
return self.op(sml, smr)
def all_prod(self) -> T:
return self.d[1]
#f(op(a[l], a[l + 1], ..., a[r - 1])) = true となる最大のrを返す
def max_right(self, l: int, f: Callable[[T], bool]):
assert 0 <= l <= self._n
assert f(self.e())
if l == self._n:
return self._n
l += self.size
sm = self.e()
while True:
while l % 2 == 0:
l >>= 1
if not f(self.op(sm, self.d[l])):
while l < self.size:
l *= 2
if f(self.op(sm, self.d[l])):
sm = self.op(sm, self.d[l])
l += 1
return l - self.size
sm = self.op(sm, self.d[l])
l += 1
if (l & -l) == l:
return self._n
def min_left(self, r: int, f: Callable[[T], bool]):
assert 0 <= r <= self._n
assert f(self.e())
if r == 0:
return 0
r += self.size
sm = self.e()
while True:
r -= 1
while r > 1 and r % 2:
r >>= 1
if not f(self.op(self.d[r], sm)):
while r < self.size:
r = 2 * r + 1
if f(self.op(self.d[r], sm)):
sm = self.op(self.d[r], sm)
r -= 1
return r + 1 - self.size
sm = self.op(self.d[r], sm)
if (r & -r) == r:
return 0
def __update__(self, k: int) -> None:
self.d[k] = self.op(self.d[2 * k], self.d[2 * k + 1])
#加算
# def op(x,y):
# return x+y
# a = list(range(10))
# st = SegTree(a,op,lambda:0)
# st.prod(3,7) -> 3+4+5+6 = 18
#最大値
# st = SegTree(a,max,lambda:-INF)
class BIT:
def __init__(self, n):
self.n = n
self.data = [0]*(n+1)
self.el = [0]*(n+1)
def sum(self, i):
s = 0
while i > 0:
s += self.data[i]
i -= i & -i
return s
def add(self, i, x):
# assert i > 0
self.el[i] += x
while i <= self.n:
self.data[i] += x
i += i & -i
def get(self, i, j=None):
if j is None:
return self.el[i]
return self.sum(j) - self.sum(i)
# 1_indexed
# n = 6
# a = [1,2,3,4,5,6]
# bit = BIT(n)
# for i,e in enumerate(a):
# bit.add(i+1,e)
# print(bit.get(2,5)) #12 (3+4+5)
n = inp()
X = inpl()
V = inpl()
d = {}
for i,x in enumerate(sorted(X)):
d[x] = i
for i,x in enumerate(X):
X[i] = d[x]
xv = [(x,v) for x,v in zip(X,V)]
xv.sort(key=lambda x:x[0],reverse=True)
xv.sort(key=lambda x:x[1],reverse=True)
# cnt_bit = BIT(n+10)
# sum_bit = BIT(n+10)
def op(x,y): return x+y
cnt_st = SegTree([0]*(n+10),op,lambda:0)
sum_st = SegTree([0]*(n+10),op,lambda:0)
res = 0
for x,_ in xv:
res += sum_st.prod(x+1,n+1) - cnt_st.prod(x+1,n+1)*x
sum_st.set(x,x)
cnt_st.set(x,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
|
/*
K.D. Vinit |,,|
*/
#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;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
void solve()
{
int n;
cin>>n;
vector< vector<int> > xv1(n),xv2(n),xv(n);
int x;
for(int i=0;i<n;i++) { cin>>x; xv1[i].push_back(x); }
for(int i=0;i<n;i++) { cin>>x; xv1[i].push_back(x); }
sort(xv1.begin(),xv1.end());
for(int i=0;i<n;i++)
{
xv2[i].push_back(xv1[i][1]);
xv2[i].push_back(xv1[i][0]);
}
sort(xv2.begin(),xv2.end());
for(int i=0;i<n;i++)
{
xv2[i][0]=i+1;
}
for(int i=0;i<n;i++)
{
xv[i].push_back(xv2[i][1]);
xv[i].push_back(xv2[i][0]);
}
sort(xv.begin(),xv.end());
/*
for(int i=0;i<n;i++) cout<<xv[i][0]<<" ";
cout<<endl;
for(int i=0;i<n;i++) cout<<xv[i][1]<<" ";
cout<<endl;
*/
long long int ans=0;
ordered_set os;
for(int i=0;i<n;i++)
{
int cnt=os.order_of_key(xv[i][1]);
os.insert(xv[i][1]);
//cout<<i<<" "<<xv[i][0]<<" "<<cnt<<endl;
ans+=(xv[i][0]*cnt);
}
os.clear();
for(int i=n-1;i>=0;i--)
{
int cnt=os.order_of_key(xv[i][1]);
int m=n-1-i;
cnt=m-cnt;
//cout<<i<<" "<<xv[i][0]<<" "<<cnt<<endl;
os.insert(xv[i][1]);
ans-=(xv[i][0]*cnt);
}
cout<<ans<<endl;
}
int32_t main()
{
ios_base::sync_with_stdio(false);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;
template <typename T1, typename T2>
inline void chmin(T1 &a, T2 b) {
if (a > b) a = b;
}
template <typename T1, typename T2>
inline void chmax(T1 &a, T2 b) {
if (a < b) a = b;
}
template <typename tuple<long long, long long, long long> >
struct SegmentTree {
using F = function<tuple<long long, long long, long long>(
tuple<long long, long long, long long>,
tuple<long long, long long, long long>)>;
long long n;
F f;
tuple<long long, long long, long long> ti;
vector<tuple<long long, long long, long long> > dat;
SegmentTree(){};
SegmentTree(F f, tuple<long long, long long, long long> ti) : f(f), ti(ti) {}
void init(long long n_) {
n = 1;
while (n < n_) n <<= 1;
dat.assign(n << 1, ti);
}
void build(const vector<tuple<long long, long long, long long> > &v) {
long long n_ = v.size();
init(n_);
for (long long i = 0; i < n_; i++) dat[n + i] = v[i];
for (long long i = n - 1; i; i--)
dat[i] = f(dat[(i << 1) | 0], dat[(i << 1) | 1]);
}
void update(long long k, tuple<long long, long long, long long> x) {
dat[k += n] = x;
while (k >>= 1) dat[k] = f(dat[(k << 1) | 0], dat[(k << 1) | 1]);
}
void add(long long k, tuple<long long, long long, long long> x) {
dat[k += n] += x;
while (k >>= 1) dat[k] = f(dat[(k << 1) | 0], dat[(k << 1) | 1]);
}
tuple<long long, long long, long long> query(long long a, long long b) {
tuple<long long, long long, long long> vl = ti, vr = ti;
for (long long l = a + n, r = b + n; l < r; l >>= 1, r >>= 1) {
if (l & 1) vl = f(vl, dat[l++]);
if (r & 1) vr = f(dat[--r], vr);
}
return f(vl, vr);
}
template <typename C>
long long lower_find(long long a, long long b, C &check,
tuple<long long, long long, long long> x,
long long k = 1, long long l = 0, long long r = -1) {
if (r < 0) r = n;
if (!check(f(x, dat[k])) || r <= a || b <= l) return -1;
if (r - l == 1) return l;
long long xl = lower_find(a, b, check, x, (k << 1), l, (l + r) / 2);
if (xl >= 0) return xl;
x = f(x, dat[(k << 1)]);
return lower_find(a, b, check, x, (k << 1) | 1, (l + r) / 2, r);
}
template <typename C>
long long lower_find(long long a, long long b, C &check) {
tuple<long long, long long, long long> x = ti;
return lower_find(a, b, check, x);
}
template <typename C>
long long upper_find(long long a, long long b, C &check,
tuple<long long, long long, long long> x,
long long k = 2, long long l = 0, long long r = -1) {
if (r < 0) r = n;
if (r <= a || b <= l) return -1;
if (r - l == 1) return l;
if (check(f(x, dat[k]))) {
long long xr =
upper_find(a, b, check, f(x, dat[k]), ((k + 1) << 1), (l + r) / 2, r);
if (xr >= 0) return xr;
}
return upper_find(a, b, check, x, (k << 1), l, (l + r) / 2);
}
template <typename C>
long long upper_find(long long a, long long b, C &check) {
tuple<long long, long long, long long> x = ti;
return upper_find(a, b, check, x);
}
};
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
long long n;
cin >> n;
vector<pair<long long, long long> > a(n);
for (long long i = (0); i < ((n)); ++i) cin >> a[i].first;
for (long long i = (0); i < ((n)); ++i) cin >> a[i].second;
sort(a.begin(), a.end());
vector<long long> d(n);
for (long long i = (0); i < ((n)); ++i) d[i] = a[i].first, a[i].first = i;
long long ans = 0, tmp = d[n - 1];
for (long long i = ((n - 1) - 1); i >= (0); --i)
ans += tmp - d[i] * (n - 1 - i), tmp += d[i];
auto comp = [](pair<long long, long long> x, pair<long long, long long> y) {
return x.second < y.second;
};
sort(a.begin(), a.end(), comp);
auto f = [](pair<long long, long long> x, pair<long long, long long> y) {
return pair<long long, long long>(x.first + y.first, x.second + y.second);
};
SegmentTree<pair<long long, long long> > L(f,
pair<long long, long long>(0, 0)),
R(f, pair<long long, long long>(0, 0));
L.build(
vector<pair<long long, long long> >(n, pair<long long, long long>(0, 0)));
R.build(
vector<pair<long long, long long> >(n, pair<long long, long long>(0, 0)));
long long now = (1LL << 60), id = n - 1;
for (long long i = ((n)-1); i >= (0); --i) {
pair<long long, long long> l = L.query(0, a[i].first);
ans -= d[a[i].first] * l.second - l.first;
if (now != a[i].second) {
while (id >= i) {
L.update(a[id].first, pair<long long, long long>(d[a[id].first], 1));
id--;
}
now = a[i].second;
}
}
cout << ans << endl;
return 0;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
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 = 1e5;
struct bit {
bit(long long n = 0) { a = mat(0ULL, n + 1); }
long long get(long long i) {
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 < a.size(); i += i & -i) a[i] += v;
}
matrix<unsigned long long, 1> 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 ans = 0ULL;
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());
map<long long, long long> vc;
long long idx = 1;
sort(v.begin(), v.end());
for (auto &o : v) {
long long t = o + (o < 0 ? -1 : 1);
if (!vc.count(t)) {
vc[t] = idx++;
} else {
}
}
bit pre_neg(idx);
bit pre_num_neg(idx);
bit pre_pos(idx);
bit pre_num_pos(idx);
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(vc[vi] - 1) - pre_num_pos.get(vc[vi] - 1) * xi);
} else {
step = step - ((pre_neg.get(idx) - pre_neg.get(vc[vi])) -
(pre_num_neg.get(idx) - pre_num_neg.get(vc[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_neg.add(vc[vi], xi);
pre_num_neg.add(vc[vi], 1);
} else {
pre_pos.add(vc[vi], xi);
pre_num_pos.add(vc[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
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
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)-1,-1,-1):
count=getsum(bit,pos[sort[i]]-1)
diff=getsum(ind,pos[sort[i]]-1)
if i<(n-1):
total+=(sort[i+1]-sort[i])*(n-1-i)
ans+=total-(diff-count*sort[i])
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
|
python3
|
n = int(input())
x = [int(a) for a in input().split()]
v = [int(a) for a in input().split()]
sum = 0
for i in range(1, n):
for j in range(i, n):
if x[j] < x[j-1]:
temp = x[j]
x[j] = x[j-1]
x[j-1] = temp
temp = v[j]
v[j] = v[j-1]
v[j-1] = temp
for i in range(n):
for j in range(i, n):
if v[j] >= v[i]:
sum += x[j] - x[i]
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
|
java
|
//package psa.minrazdalja;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class minrazdalja {
/**
* Uvozena abstraktna funkcija za klic Paid<L, R>
*/
public static class Pair<L,R> {
private final L left;
private final R right;
public Pair(L left, R right) {
assert left != null;
assert right != null;
this.left = left;
this.right = right;
}
public L getLeft() { return left; }
public R getRight() { return right; }
@Override
public int hashCode() { return left.hashCode() ^ right.hashCode(); }
@Override
public boolean equals(Object o) {
if (!(o instanceof Pair)) return false;
Pair pairo = (Pair) o;
return this.left.equals(pairo.getLeft()) &&
this.right.equals(pairo.getRight());
}
}
/**?
* glavna funkcija racunanja:
* sprejme list tipa ArrayList<Pair<Integer, Integer>>, potem st vseh elementov in seveda listo vseh elementov.
*
* nato izracunamo maksimalno distance, ki je sestevek vseh distanc med posameznimi elementi, kar lahko storimo, ker je sortirana tabela.
* V drugem delu odstejemo od maksimalnega sestevka distanc, posamezne ki se sekajo v prihodnosti
* ostane nam samo tocni sestevek minimalnih distanc
*/
public static long elele(ArrayList<Pair<Integer,Integer>> elementi, int s, Integer[] x_el) {
long skupek = 0;
Long ml;
HashMap<Integer, Integer> pos = new HashMap<Integer, Integer>();
for(int i=0; i<s; ++i) {
skupek += x_el[i] * i + (-1 * x_el[i]) * (s - 1 - i);
pos.put(x_el[i], i);
}
if(s == 161)
System.out.println(skupek);
for(int i=0; i<s; ++i){
skupek -= elementi.get(i).getRight() * (pos.get(elementi.get(i).getRight())-i);
}
//2 1 4 3 5
//2 2 2 3 4
return skupek;
}
public static void main(String[] args) throws IOException {
int st_el;
// uporabimo bufferedreader zaradi 1000x vecje hitrosti kot pa skener
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
//System.out.println("Vnesi stevilo elementov: ");
st_el = Integer.parseInt(br.readLine());
//System.out.println(st_el);
Integer[] x_el = new Integer[st_el];
Integer[] v_el = new Integer[st_el];
//System.out.println("Vnesi elemente: ");
String x = br.readLine();
String[] strs = x.trim().split("\\s+");
for(int i=0; i<st_el; ++i) {
x_el[i] = Integer.parseInt(strs[i]);
}
//System.out.println("Vnesi tezo elementov: ");
String y = br.readLine();
String[] strs2 = y.trim().split("\\s+");
for(int i=0; i<st_el; ++i) {
v_el[i] = Integer.parseInt(strs2[i]);
}
br.close();
//pospravimo skupej v par oblike <integer, integer> oz <hitrost, element>
ArrayList<Pair<Integer,Integer>> listt = new ArrayList<Pair<Integer, Integer>>(st_el);
for(int i=0; i<st_el; i++) {
Pair<Integer, Integer> pair = new Pair<Integer, Integer>(v_el[i],x_el[i]);
listt.add(pair);
}
/*sortiramo listt od najmanjsega do najvecjega
* - sortiramo prvo po hitrostih (to je glavno najbolj vazno)
* - ce je hitrost ista potem pa sortiramo se glede na element
*/
Collections.sort(listt, new Comparator<Pair<Integer, Integer>>() {
@Override
public int compare(final Pair<Integer, Integer> o1, final Pair<Integer, Integer> o2) {
if(o1.getLeft()<o2.getLeft()) {
return -1;
} else if(o1.getLeft() == o2.getLeft()) {
if(o1.getRight() < o2.getRight()){
return -1;
}else{
return 1;
}
} else {
return 1;
}
}
});
//sortiramo se elemente
Arrays.sort(x_el);
//izpisemo elemente
System.out.println(elele(listt,st_el,x_el));
}
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
java
|
import java.util.*;
import java.io.*;
import java.math.BigInteger;
public class Solution
{
static class Pair<A,B>{
A parent;
B rank;
Pair(A parent,B rank)
{
this.rank=rank;
this.parent=parent;
}
}
// static int find(Pair pr[],int i)
// {
// if(i==pr[i].parent)
// return i;
// else
// {
// pr[i].parent=find(pr,pr[i].parent);
// return pr[i].parent;
// }
// }
// static void join(Pair[] pr,int i,int j)
// {
// i=find(pr,i);
// j=find(pr,j);
// if(i==j)
// return;
// if(pr[i].rank>=pr[j].rank)
// {
// pr[j].parent=i;
// pr[i].rank+=pr[j].rank;
// }
// else
// {
// pr[i].parent = j;
// pr[j].rank+=pr[i].rank;
// }
// }
static void swap(char ar[],int i,int j)
{
char f = ar[i];
ar[i]=ar[j];
ar[j]=f;
}
static ArrayList<Integer> graph[] ;
static TreeSet<Long> ts;
public static void main (String[] args) throws IOException
{
StringBuilder sb = new StringBuilder();
FastReader s1 = new FastReader();
int n=s1.I();
int xr[] = new int[n];
int v[] = new int[n];
for(int i=0;i<n;i++)
{
xr[i]=s1.I();
}
for(int i=0;i<n;i++)
{
v[i]=s1.I();
}
long ans=0;
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
long c=xr[i]-xr[j];
long T=v[i]-v[j];
ans+=mindis(c,T);
// System.out.println(ans+" "+c+" "+T);
}
}
sb.append(ans+"\n");
System.out.println(sb);
}
static long mindis(long c,long t)
{
if(c==0)
return c;
if(t==0)
return Math.abs(c);
if(c*t>0)
{
return Math.abs(c);
}
else
{
c=Math.abs(c);
t=Math.abs(t);
long temp=(c/t);
long min=Math.abs(c-t*temp);
long demo=Math.abs(c-t*(temp+1));
// System.out.println(temp+" "+min+" "+demo+" "+c+" "+t);
return Math.min(min,demo);
}
}
static boolean print(int h,int b,int a)
{
if(h*h==(a*a+b*b))
return true;
return false;
}
static long min(long no,long temp)
{
if(no<temp)
{
return -1;
}
long ans=no-temp;
long max=-1;
int n = (int)(Math.log(no)/Math.log(2));
long curr=temp;
for(int i=n;i>=0;i--)
{
long demo=(long)Math.pow(2, i);
long h=(demo|curr);
if(h<=no && h>max)
{
max=h;
curr=h;
}
}
return max;
}
static int dayofweek(long d, int m, long y)
{
int t[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
y -= (m < 3) ? 1 : 0;
return (int)( y + y/4 - y/100 + y/400 + t[(m-1)] + d) % 7;
}
static String preprocess(String s)
{
StringBuilder sb = new StringBuilder();
sb.append("@#");
for(int i=0;i<s.length();i++)
{
sb.append(s.charAt(i)).append("#");
}
sb.append("$");
return sb.toString();
}
static String LPS(String s)
{
String snew = preprocess(s);
int n = snew.length();
// System.out.println(snew);
int c = 0,r = 0;
int maxlen = 0;
int index=0;
int len[] = new int[n];
for(int i=1;i<n-1;i++)
{
int mirr = c-(i-c);
if(i<r)
{
len[i]=Math.min(len[mirr],r-i);
}
// System.out.println(i+" ");
while(snew.charAt(i+len[i])==snew.charAt(i-len[i]))
len[i]++;
if(i+len[i]>r)
{
c = i;
r = i+len[i];
}
// System.out.println(c+" "+i+" "+snew.charAt(i)+" "+len[i]+" "+maxlen+" "+index);
if(len[i]>maxlen)
{
// System.out.println("inside");
maxlen = len[i];
index = i;
}
}
maxlen--;
int left = (index-maxlen-1)/2;
int right = left+maxlen;
return s.substring(left, right);
}
static long phi(long n)
{
long result=n;
for(int i=2;i<=Math.sqrt(n);i++)
{
if(n%i==0)
{
result-=result/i;
}
while(n%i==0)
{
n=n/i;
}
}
if(n>1)
{
result-=(result/n);
}
return result;
}
static ArrayList<Integer> primeFactor(int n)
{
ArrayList<Integer> ans = new ArrayList<>();
if(n%2==0)
ans.add(2);
while (n%2==0)
{
n /= 2;
}
for (int i = 3; i <= Math.sqrt(n); i+= 2)
{
if(n%i==0)
ans.add(i);
while (n%i == 0)
{
n /= i;
}
}
if (n > 2)
ans.add(n);
return ans;
}
static int longestSubSeg(int a[], int n,int k)
{
int cnt0 = 0;
int l = 0;
int max_len = 0;
for (int i = 0; i < n; i++) {
if (a[i] == 0)
cnt0++;
while (cnt0 > k) {
if (a[l] == 0)
cnt0--;
l++;
}
max_len = Math.max(max_len, i - l + 1);
}
return max_len;
}
static void dfs(int index,boolean ar[])
{
ar[index]=true;
for(int i=0;i<graph[index].size();i++)
{
if(!ar[graph[index].get(i)])
{
dfs(graph[index].get(i),ar);
}
}
}
static int binary(int left,int right,int ar[],int no)
{
int mid=(left+right)/2;
if(Math.abs(right-left)<=1)
{
if(no>=ar[right])
{
return right+1;
}
else if(no<ar[left])
{
return left;
}
else
{
return left+1;
}
}
if(ar[mid]>no)
{
right=mid-1;
return binary(left, right, ar, no);
}
else{
left=mid;
return binary(left, right, ar, no);
}
}
static class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int I()
{
return Integer.parseInt(next());
}
long L()
{
return Long.parseLong(next());
}
double D()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
static long gcd(long a,long b)
{
if(a%b==0)
return b;
return gcd(b,a%b);
}
static float power(float x, int y)
{
float temp;
if( y == 0)
return 1;
temp = power(x, y/2);
if (y%2 == 0)
return temp*temp;
else
{
if(y > 0)
return x * temp * temp;
else
return (temp * temp) / x;
}
}
static long pow(long x, long y)
{
int p = 1000000007;
long res = 1;
x = x % p;
while (y > 0)
{
if((y & 1)==1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static ArrayList<Integer> sieveOfEratosthenes(int n)
{
ArrayList<Integer> arr=new ArrayList<Integer>();
boolean prime[] = new boolean[n+1];
for(int i=2;i<n;i++)
prime[i] = true;
for(int p = 2; p*p <=n; p++)
{
if(prime[p] == true)
{
arr.add(p);
for(int i = p*p; i <= n; i += p)
prime[i] = false;
}
}
return arr;
}
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
java
|
import java.awt.*;
import java.io.*;
import java.io.IOException;
import java.util.*;
import java.text.DecimalFormat;
public class Exam {
public static long mod = (long)Math.pow(10, 9)+7 ;
public static double epsilon=0.00000000008854;//value of epsilon
public static InputReader sc = new InputReader(System.in);
public static PrintWriter pw = new PrintWriter(System.out);
public static long gcd(long a,long b){
while(b>0){
long t=b;
b=a%b;
a=t;
}
return a;
}
static void dfs(Point p[],int v,boolean vis[]){
if (vis[v])return;
vis[v]=true;
for (int i=0;i<p.length;i++){
if (p[i].x==p[v].x || p[i].y==p[v].y){
dfs(p,i,vis);
}
}
}
public static long bSearch(Pair[]d, int l, int r, int a) {
if(d[l].d > a)
return 0;
if(l == r)
return d[l].g;
int m = (l + r + 1) / 2;
if(d[m].d > a)
return bSearch(d, l, m - 1, a);
return bSearch(d, m, r, a);
}
static class Pair implements Comparable<Pair> {
int d, i;
long g;
public Pair(int D, long G, int I) {
d = D;
g = G;
i = I;
}
public int compareTo(Pair p) {
if(d == p.d)
return i - p.i;
return d - p.d;
}
}
static void dfs(int src, ArrayList<ArrayList<Integer>> list, int vis[],int c[])
{
//Here 0-white,1-gray & 2-black;
// Time complexity O(n+m)
//System.out.print(src+" ");
vis[src]=1;
for(int i=0;i<list.get(src).size();i++){
if(vis[list.get(src).get(i)]==0){
dfs(list.get(src).get(i),list, vis,c);
}
}
c[0]++;
vis[src]=2;
}
public static long pow(long x,long y,long mod){
long ans=1;
while(y>0){
if((y&1)==1){
ans=(ans*x)%mod;
}
y=y>>1;
x=(x*x)%mod;
}
return ans;
}
public static boolean isPalindrome(String s1,String s2){
s2=reverseString(s2);
if(s1.equals(s2)){
return true;
}
else
return false;
/*
boolean f=true;
for(int i=0;i<s.length();i++){
if(i>s.length()-i-1)
break;
if(s.charAt(i)!=s.charAt(s.length()-i-1)){
f=false;
break;
}
}
return f;*/
}
public static HashSet<Integer> CreateSeg(int root,int l,int r,int a[],ArrayList<HashSet<Integer>>s){
if(l==r){
//HashSet<Integer> h=new HashSet<>();
s.get(root).add(a[l]);
return s.get(root);
}
HashSet<Integer> h1=CreateSeg(2*root+1, l, (l+r)/2, a, s);
HashSet<Integer> h2=CreateSeg(2*root+2,(l+r)/2+1,r, a, s);
//HashSet<Integer> h=new HashSet<>();
s.get(root).addAll(h1);s.get(root).addAll(h2);
return s.get(root);
}
public static HashSet<Integer> query(int root,int l,int r,int st,int en,ArrayList<HashSet<Integer>>s){
if(l>=st&&r<=en){
return s.get(root);
}
else if(r<st||l>en)
return new HashSet<Integer>();
else{
HashSet<Integer> h1=query(2*root+1, l, (l+r)/2, st, en, s);
HashSet<Integer> h2=query(2*root+2, (l+r)/2+1, r, st, en, s);
HashSet<Integer> h=new HashSet<>();
h.addAll(h1);h.addAll(h2);
return h;
}
}
public static void main(String[] args) {
// code starts..
//int q=sc.nextInt();
//while(q-->0){
int n=sc.nextInt();
//int m=sc.nextInt();
Integer a[][]=new Integer[n][3];
for(int i=0;i<n;i++){
a[i][0]=sc.nextInt();
}
for(int i=0;i<n;i++){
a[i][1]=sc.nextInt();
}
Arrays.sort(a,column(0));
for(int i=0;i<n;i++){
a[i][2]=i+1;
}
Arrays.sort(a,column(1));
Bit co=new Bit(1000000);
Bit sum=new Bit(1000000);
long tot=0;
for(int i=0;i<n;i++){
//if(i>0&&a[i][1]==a[i-1][1]){
// if(a[i][0]>a[i-1][0])
// tot-=a[i][0]-a[i-1][0];
//}
int count=co.query(a[i][2]);
int s=sum.query(a[i][2]);
tot+=count*a[i][0]-s;
co.update(a[i][2], 1);
sum.update(a[i][2], a[i][0]);
}
Bit c1=new Bit(1000000);
Bit s1=new Bit(1000000);
int v=100000;
for(int i=0;i<n;i++){
if(i>0&&a[i][1]-a[i-1][1]==0){
int c=c1.query(v-a[i][2]);
int s=s1.query(v-a[i][2]);
tot+=-c*a[i][0]+s;
c1.update(v-a[i][2],1);
s1.update(v-a[i][2], a[i][0]);
}
else {
c1=new Bit(1000000);
s1=new Bit(1000000);
c1.update(v-a[i][2],1 );
s1.update(v-a[i][2], a[i][0]);
}
}
pw.println(tot);
//}
// Code ends...
pw.flush();
pw.close();
}
static class tripletL implements Comparable<tripletL> {
Long x, y, z;
tripletL(long x, long y, long z) {
this.x = x;
this.y = y;
this.z = z;
}
public int compareTo(tripletL o) {
int result = x.compareTo(o.x);
if (result == 0)
result = y.compareTo(o.y);
if (result == 0)
result = z.compareTo(o.z);
return result;
}
public boolean equlas(Object o) {
if (o instanceof tripletL) {
tripletL p = (tripletL) o;
return (x - p.x == 0) && (y - p.y ==0 ) && (z - p.z == 0);
}
return false;
}
public String toString() {
return x + " " + y + " " + z;
}
public int hashCode() {
return new Long(x).hashCode() * 31 + new Long(y).hashCode() + new Long(z).hashCode();
}
}
public static String Doubleformate(double a,int n){
String s="";
while(n-->0){
s+='0';
}
DecimalFormat f =new DecimalFormat("#0."+s);
return f.format(a);
}
public static Comparator<Integer[]> column(int i){
return
new Comparator<Integer[]>() {
@Override
public int compare(Integer[] o1, Integer[] o2) {
return o1[i].compareTo(o2[i]);//for ascending
//return o2[i].compareTo(o1[i]);//for descending
}
};
}
public static Comparator<Integer[]> pair(){
return
new Comparator<Integer[]>() {
@Override
public int compare(Integer[] o1, Integer[] o2) {
int result=o1[0].compareTo(o2[0]);
if(result==0)
result=o1[1].compareTo(o2[1]);
return result;
}
};
}
public static Comparator<Integer[]> Triplet(){
return
new Comparator<Integer[]>() {
@Override
public int compare(Integer[] o1, Integer[] o2) {
for(int i=0;i<3;i++){
for(int j=i+1;j<3;j++){
for(int k=0;k<3;k++){
for(int p=k+1;p<3;p++){
if((o1[i]==o2[k]&&o1[j]==o2[p])||(o1[j]==o2[k]&&o1[i]==o2[p])){
}
}
}
}
}
int result=o1[0].compareTo(o2[0]);
if(result==0)
result=o1[1].compareTo(o2[1]);
if(result==0)
result=o1[2].compareTo(o2[2]);
return result;
}
};
}
public static String reverseString(String s){
StringBuilder input1 = new StringBuilder();
input1.append(s);
input1 = input1.reverse();
return input1.toString();
}
public static int[] scanArray(int n){
int a[]=new int [n];
for(int i=0;i<n;i++)
a[i]=sc.nextInt();
return a;
}
public static long[] scanLongArray(int n){
long a[]=new long [n];
for(int i=0;i<n;i++)
a[i]=sc.nextLong();
return a;
}
public static String [] scanStrings(int n){
String a[]=new String [n];
for(int i=0;i<n;i++)
a[i]=sc.nextLine();
return a;
}
static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int snext() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
public String readString() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine() {
int c = snext();
while (isSpaceChar(c))
c = snext();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
class Bit{//1...n
int a[];
Bit(int n){
a=new int[n+1];
}
void update(int i,int delta){
while(i<a.length){
a[i]+=delta;
i+=i&(-i);
}
}
int query(int i){
int sum=0;
while(i>0){
sum+=a[i];
i-=i&(-i);
}
return sum;
}
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int N = 200000;
long long BIT1[200001];
long long BIT2[200001];
int cnt1[200001];
int cnt2[200001];
map<int, int> r, l;
pair<int, int> arr[200001];
int n;
void upd1(int idx, long long val) {
for (; idx <= N; idx = (idx | (idx + 1))) {
BIT1[idx] += val;
cnt1[idx]++;
}
}
void upd2(int idx, long long val) {
for (; idx <= N; idx = (idx | (idx + 1))) {
BIT2[idx] += val;
cnt2[idx]++;
}
}
pair<long long, int> query1(int idx) {
if (idx < 0) return {0, 0};
long long sum = 0;
int ct = 0;
for (; idx >= 0; idx = (idx & (idx + 1)) - 1) {
sum += BIT1[idx];
ct += cnt1[idx];
}
return {sum, ct};
}
pair<long long, int> query2(int idx) {
if (idx < 0) return {0, 0};
long long sum = 0;
int ct = 0;
for (; idx >= 0; idx = (idx & (idx + 1)) - 1) {
sum += BIT2[idx];
ct += cnt2[idx];
}
return {sum, ct};
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) cin >> arr[i].first;
for (int i = 1; i <= n; i++) cin >> arr[i].second;
sort(arr + 1, arr + n + 1);
for (int i = 1; i <= n; i++) {
if (arr[i].second > 0)
r[arr[i].second] = 0;
else if (arr[i].second < 0)
l[-(arr[i].second)] = 0;
}
int ind = 1;
for (auto &it : r) {
it.second = ind;
ind++;
}
ind = 1;
for (auto &it : l) {
it.second = ind;
ind++;
}
long long ans = 0;
for (int i = 1; i <= n; i++) {
if (arr[i].second >= 0) {
int vel = r[arr[i].second];
pair<long long, int> p = query1(vel);
ans += (p.second) * (arr[i].first);
ans -= p.first;
p = query2(N);
ans += (p.second) * (arr[i].first);
ans -= p.first;
upd1(vel, arr[i].first);
} else if (arr[i].second < 0) {
int vel = l[-(arr[i].second)];
pair<long long, int> p = query2(N);
pair<long long, int> p1 = query2(vel - 1);
p.first -= p1.first;
p.second -= p1.second;
ans += (p.second) * (arr[i].first);
ans -= p.first;
upd2(vel, arr[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 GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#pragma warning(disable : 4996)
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<math.h>
#include<iomanip>
#include<set>
#include<numeric>
#include<cstring>
#include<cstdio>
#include<functional>
#include<bitset>
#include<limits.h>
#include<cassert>
#include<iterator>
#include<complex>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<time.h>
#include<random>
using namespace std;
#define REP(i, n) for(int i = 0;i < n;++i)
#define REPR(i, n) for(int i = n-1;i >= 0;--i)
#define FOR(i, m, n) for(int i = m;i < n;++i)
#define FORR(i, m, n) for(int i = m-1;i >= n;--i)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define REVERSE(v,n) reverse(v,v+n);
#define VREVERSE(v) reverse(v.begin(), v.end())
#define ll long long
#define pb(a) push_back
#define print(x) cout<<(x)<<'\n'
#define pe(x) cout<<(x)<<" "
#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define lb(v,n) lower_bound(v.begin(), v.end(), (n))
#define ub(v,n) upper_bound(v.begin(), v.end(), (n))
//#define int long long
#define all(x) (x).begin(), (x).end()
#define print_space(v) REP(i,v.size())cout << v[i] << ((i == v.size() - 1) ? "\n" : " ")
template<typename T1, typename T2> inline void chmin(T1 & a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> inline void chmax(T1& a, T2 b) { if (a < b) a = b; }
typedef pair<int, int>P;
const int MOD = 1e9 + 7; const int MAX = 200020;
const double pi = acos(-1); const double EPS = 1e-12;
const ll INF = 1e18;
template<class VAL> struct RBST {
VAL SUM_UNITY = 0; // to be set
unsigned int randInt() {
static unsigned int tx = 123456789, ty = 362436069, tz = 521288629, tw = 88675123;
unsigned int tt = (tx ^ (tx << 11));
tx = ty; ty = tz; tz = tw;
return (tw = (tw ^ (tw >> 19)) ^ (tt ^ (tt >> 8)));
}
struct NODE {
NODE *left, *right;
VAL val; // the value of the node
int size; // the size of the subtree
VAL sum; // the value-sum of the subtree
NODE() : val(SUM_UNITY), size(1), sum(SUM_UNITY) {
left = right = NULL;
}
NODE(VAL v) : val(v), size(1), sum(v) {
left = right = NULL;
}
/* additional update */
inline void update() {
}
/* additional lazy-propagation */
inline void push() {
/* ex: reverse */
/*
if (this->rev) {
swap(this->left, this->right);
if (this->left) this->left->rev ^= true;
if (this->right) this->right->rev ^= true;
this->rev = false;
}
*/
}
};
///////////////////////
// root
///////////////////////
NODE* root;
RBST() : root(NULL) { }
RBST(NODE* node) : root(node) { }
///////////////////////
// basic operations
///////////////////////
/* size */
inline int size(NODE *node) {
return !node ? 0 : node->size;
}
inline int size() {
return this->size(this->root);
}
/* sum */
inline VAL sum(NODE *node) {
return !node ? SUM_UNITY : node->sum;
}
inline VAL sum() {
return this->sum(this->root);
}
/* update, push */
inline NODE* update(NODE *node) {
node->size = size(node->left) + size(node->right) + 1;
node->sum = sum(node->left) + sum(node->right) + node->val;
node->update();
return node;
}
inline void push(NODE *node) {
if (!node) return;
node->push();
}
/* lower_bound */
inline int lowerBound(NODE *node, VAL val) {
push(node);
if (!node) return 0;
if (val <= node->val) return lowerBound(node->left, val);
else return size(node->left) + lowerBound(node->right, val) + 1;
}
inline int lowerBound(VAL val) {
return this->lowerBound(this->root, val);
}
/* upper_bound */
inline int upperBound(NODE *node, VAL val) {
push(node);
if (!node) return 0;
if (val >= node->val) return size(node->left) + upperBound(node->right, val) + 1;
else return upperBound(node->left, val);
}
inline int upperBound(VAL val) {
return this->upperBound(this->root, val);
}
/* count */
inline int count(VAL val) {
return upperBound(val) - lowerBound(val);
}
/* get --- k: 0-index */
inline VAL get(NODE *node, int k) {
push(node);
if (!node) return -1;
if (k == size(node->left)) return node->val;
if (k < size(node->left)) return get(node->left, k);
else return get(node->right, k - size(node->left) - 1);
}
inline VAL get(int k) {
return get(this->root, k);
}
///////////////////////
// merge-split
///////////////////////
NODE* merge(NODE *left, NODE *right) {
push(left);
push(right);
if (!left || !right) {
if (left) return left;
else return right;
}
if (randInt() % (left->size + right->size) < left->size) {
left->right = merge(left->right, right);
return update(left);
}
else {
right->left = merge(left, right->left);
return update(right);
}
}
void merge(RBST add) {
this->root = this->merge(this->root, add.root);
}
pair<NODE*, NODE*> split(NODE* node, int k) { // [0, k), [k, n)
push(node);
if (!node) return make_pair(node, node);
if (k <= size(node->left)) {
pair<NODE*, NODE*> sub = split(node->left, k);
node->left = sub.second;
return make_pair(sub.first, update(node));
}
else {
pair<NODE*, NODE*> sub = split(node->right, k - size(node->left) - 1);
node->right = sub.first;
return make_pair(update(node), sub.second);
}
}
RBST split(int k) {
pair<NODE*, NODE*> sub = split(this->root, k);
this->root = sub.first;
return RBST(sub.second);
}
///////////////////////
// insert-erase
///////////////////////
void insert(const VAL val) {
pair<NODE*, NODE*> sub = this->split(this->root, this->lowerBound(val));
this->root = this->merge(this->merge(sub.first, new NODE(val)), sub.second);
}
void erase(const VAL val) {
if (!this->count(val)) return;
pair<NODE*, NODE*> sub = this->split(this->root, this->lowerBound(val));
this->root = this->merge(sub.first, this->split(sub.second, 1).second);
}
///////////////////////
// debug
///////////////////////
//void print(NODE *node) {
// if (!node) return;
// push(node);
// print(node->left);
// cout << node->val << " ";
// print(node->right);
//}
//void print() {
// cout << "{";
// print(this->root);
// cout << "}" << endl;
//}
};
void solve() {
int N; cin >> N;
vector<P>v(N);
REP(i, N) {
cin >> v[i].first;
}
REP(i, N)cin >> v[i].second;
VSORT(v);
ll sum = 0;
REP(i, N) {
sum += i * v[i].first;
sum -= (N - i - 1)*v[i].first;
}
//DEBUG(sum);
RBST<int>S;
REPR(i, N) {
int c = S.lowerBound(v[i].second) - S.lowerBound(-1e8 - 1);
sum += c * v[i].first;
//cout << v[i].second << " " << c << endl;
S.insert(v[i].second);
}
RBST<int>S2;
REP(i, N) {
int c = S2.upperBound(1e8 + 1) - S2.upperBound(v[i].second);
sum -= c * v[i].first;
//cout << v[i].second << " " << c << endl;
S2.insert(v[i].second);
}
print(sum);
}
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
//int q; cin >> q;
//while (q--)
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 main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
int n, ans = 0;
cin >> n;
vector<pair<int, int> > v(n);
for (int i = 0; i < n; i++) {
cin >> v[i].first;
}
for (int i = 0; i < n; i++) cin >> v[i].second;
sort(v.begin(), v.end());
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (v[i].second <= v[j].second) ans += (v[j].first - v[i].first);
}
}
cout << ans << endl;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
bool isprime(long long int n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0) return false;
return true;
}
vector<long long int> prime;
void sieve(long long int n) {
bool bakh[n + 1];
memset(bakh, true, sizeof(bakh));
for (long long int p = 2; p * p <= n; p++) {
if (bakh[p] == true) {
for (long long int i = p * p; i <= n; i += p) bakh[i] = false;
}
}
for (long long int p = 2; p <= n; p++)
if (bakh[p]) prime.push_back(p);
}
long long int eulertotient(long long int z) {
long long int fac = z;
for (long long int i = 0; prime[i] * prime[i] <= z; i++) {
if (z % prime[i] == 0) {
fac -= (fac / prime[i]);
while (z % prime[i] == 0) z /= prime[i];
}
}
if (z > 1) fac -= (fac / z);
return fac;
}
long long int power(long long int x, long long int y, long long int p) {
long long int res = 1;
x = x % p;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
long long int gcd(long long int a, long long int b) {
if (a == 0) return b;
return gcd(b % a, a);
}
long long int lcm(long long int a, long long int b) {
long long int g = gcd(a, b);
long long int ans = (a * b) / g;
return ans;
}
long long int modInverse(long long int a, long long int m) {
long long int hvf = gcd(a, m);
if (hvf == 1) return power(a, m - 2, m);
return -1;
}
void multiply(long long int F[2][2], long long int M[2][2]) {
long long int x = F[0][0] * M[0][0] + F[0][1] * M[1][0];
long long int y = F[0][0] * M[0][1] + F[0][1] * M[1][1];
long long int z = F[1][0] * M[0][0] + F[1][1] * M[1][0];
long long int w = F[1][0] * M[0][1] + F[1][1] * M[1][1];
F[0][0] = x;
F[0][1] = y;
F[1][0] = z;
F[1][1] = w;
}
void powermat(long long int F[2][2], long long int n) {
if (n == 0 || n == 1) return;
long long int M[2][2] = {{1, 1}, {1, 0}};
powermat(F, n / 2);
multiply(F, F);
if (n % 2 != 0) multiply(F, M);
}
long long int fib(long long int n) {
long long int F[2][2] = {{1, 1}, {1, 0}};
if (n == 0) return 0;
powermat(F, n - 1);
return F[0][0];
}
long long int dp[200005];
long long int countLarger[200005];
void countLargerRight(vector<pair<long long int, long long int> > A,
long long int len) {
set<int> s;
for (int i = len - 1; i >= 0; i--) {
s.insert(A[i].second);
auto it = s.upper_bound(A[i].second);
countLarger[i] = s.size() - distance(s.begin(), it);
}
}
bool comp(pair<long long int, long long int> a,
pair<long long int, long long int> b) {
if (a.first == b.first) return 1;
return a.first < b.first;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
long long int n;
cin >> n;
long long int ar[n];
for (long long int i = 0; i < n; i++) cin >> ar[i];
long long int av[n];
vector<long long int> l, r;
for (long long int i = 0; i < n; i++) cin >> av[i];
vector<pair<long long int, long long int> > low, high;
for (long long int i = 0; i < n; i++) {
if (av[i] <= 0)
l.push_back(ar[i]), low.push_back({ar[i], abs(av[i])});
else
r.push_back(ar[i]), high.push_back({ar[i], av[i]});
}
long long int ans = 0, sz = r.size();
if (r.size() > 0) {
dp[sz - 1] = r[sz - 1];
for (long long int i = sz - 2; i >= 0; i--) dp[i] = dp[i + 1] + r[i];
for (long long int i = 0; i < l.size(); i++) {
long long int tx = upper_bound(r.begin(), r.end(), l[i]) - r.begin();
if (tx == r.size()) continue;
long long int p = r.size() - tx;
ans += dp[tx] - p * (l[i]);
}
}
sort(low.begin(), low.end(), comp);
sort(high.begin(), high.end(), comp);
long long int p = 0;
for (long long int i = 0; i < low.size(); i++) {
for (long long int j = 0; j < i; j++) {
if (low[j].second >= low[i].second) p += abs(low[i].first - low[j].first);
}
for (long long int j = i + 1; j < low.size(); j++) {
if (low[j].second <= low[i].second) p += abs(low[i].first - low[j].first);
}
}
ans += p / 2;
p = 0;
for (long long int i = 0; i < high.size(); i++) {
for (long long int j = 0; j < i; j++) {
if (high[j].second <= high[i].second)
p += abs(high[j].first - high[i].first);
}
for (long long int j = i + 1; j < high.size(); j++) {
if (high[j].second >= high[i].second)
p += abs(high[j].first - high[i].first);
}
}
ans += p / 2;
cout << ans << endl;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
java
|
import java.util.*;
import java.io.*;
import java.lang.reflect.Array;
public class template {
final static int MOD = 1000000007;
final static int intMax = 1000000000;
final static int intMin = -1000000000;
final static int[] DX = { 0, 0, -1, 1 };
final static int[] DY = { -1, 1, 0, 0 };
static int T;
static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
byte[] buf = new byte[360]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
if (din == null)
return;
din.close();
}
}
public static void main(String[] args) throws Exception {
Reader in = new Reader();
int n = in.nextInt();
int[] x = new int[n];
int[] v = new int[n];
for(int i = 0; i < n; ++i) {
x[i] = in.nextInt();
}
for(int i = 0; i < n; ++i) {
v[i] = in.nextInt();
}
p[] points = new p[n];
for(int i = 0; i < n; ++i) {
points[i] = new p(x[i], v[i]);
}
Arrays.sort(points, (a, b) -> a.x - b.x);
for(int i = 0; i < n; ++i) {
points[i].orig = i + 1;
}
long [] dists = new long[n];
int count = 1;
for(int i = n - 2; i >= 0; --i) {
dists[i] = dists[i + 1] + (count++) * (points[i + 1].x - points[i].x);
}
long tot = 0;
for(int i = 0; i < n; ++i) {
tot += dists[i];
}
BinaryIndexedTree num = new BinaryIndexedTree(n);
BinaryIndexedTree inds = new BinaryIndexedTree(n);
Arrays.sort(points, (a, b) -> a.v == b.v ? b.orig - a.orig : b.v - a.v);
for(int i = 0; i < n; ++i) {
long amt = num.query(points[i].orig);
long totinds = inds.query(points[i].orig);
tot -= amt * points[i].x - totinds;
num.update(points[i].orig, 1);
inds.update(points[i].orig, points[i].orig);
}
System.out.println(tot);
in.close();
}
static class p{
int x, v, orig;
p(int xi, int vi){
x = xi; v = vi;
}
}
static class BinaryIndexedTree {
public long[] tree;
public BinaryIndexedTree(int n) {
tree = new long[n+5];
}
public void update(int index, int val) {
index++;
while(index < tree.length) {
tree[index] += (long) val;
index += index & -index;
}
}
//sum from 0 to index
public long query(int index) {
long ret = 0;
index++;
while(index > 0) {
ret += tree[index];
index -= index & -index;
}
return ret;
}
//sum from a to b
public long query(int a, int b) {
return query(b)-query(a-1);
}
}
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long x, long long y) {
if (!y) return x;
return gcd(y, x % y);
}
const long long MOD = 1e9 + 7;
int inf = 1e9 + 7;
long long INF = 2e18 + 9;
long long power(long long x, long long y) {
long long res = 1ll;
x %= MOD;
if (x == 0) return 0;
while (y > 0) {
if (y & 1) res = (res * x) % MOD;
y = y >> 1ll;
x = (x * x) % MOD;
}
return res;
}
long long inv(long long x) {
long long r = 1ll, t = x, k = MOD - 2ll;
while (k) {
if (k & 1ll) r = (long long)r * t % MOD;
t = (long long)t * t % MOD;
k >>= 1;
}
return r;
}
bool isPrime(long long x) {
if (x == 1) return false;
for (long long i = 2; i * i <= x; i++) {
if (x % i == 0) return false;
}
return true;
}
const int nax = 5e5 + 13;
void prep() {}
void solve() {}
vector<long long> fenwick[2];
int n;
void add(int pos, int val, int ind) {
pos++;
while (pos <= n) {
fenwick[ind][pos] += val;
pos += pos & (-pos);
}
}
long long query(int pos, int ind) {
int res = 0;
pos++;
while (pos) {
res += fenwick[ind][pos];
pos -= pos & (-pos);
}
return res;
}
void read() {
cin >> n;
vector<pair<int, int> > xv(n);
for (int i = 0; i < n; i++) {
cin >> xv[i].first;
}
for (int i = 0; i < n; i++) {
cin >> xv[i].second;
}
vector<int> tmp;
for (int i = 0; i < n; i++) tmp.push_back(xv[i].second);
sort(tmp.begin(), tmp.end());
auto it = unique(tmp.begin(), tmp.end());
tmp.resize(distance(tmp.begin(), it));
map<int, int> M;
for (int i = 0; i < tmp.size(); i++) {
M[tmp[i]] = i;
}
for (int i = 0; i < n; i++) {
xv[i].second = M[xv[i].second];
}
sort(xv.begin(), xv.end());
int N = n;
n = tmp.size();
fenwick[0].resize(n + 1);
fenwick[1].resize(n + 1);
for (auto& el : fenwick[0]) el = 0;
for (auto& el : fenwick[1]) el = 0;
long long res = 0;
for (int i = 0; i < N; i++) {
res += xv[i].first * query(xv[i].second, 0) - query(xv[i].second, 1);
add(xv[i].second, 1, 0);
add(xv[i].second, xv[i].first, 1);
}
cout << res << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie();
cout.tie();
srand(time(NULL));
int T = 1;
prep();
for (int i = 1; i <= T; i++) {
read();
solve();
}
return 0;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
cpp
|
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> p2;
ll x[200005];
p2 t[800005],lazy[800005];
vector<p2> v1,v2;
vector<ll> v;
p2 add(p2 a,p2 b){
return {a.first+b.first,a.second+b.second};
}
void update(ll v,ll tl,ll tr,ll pos,ll val){
if(tl==tr){
t[v]=add(t[v],{val,1});
return;
}
ll tm=(tl+tr)/2;
if(pos<=tm)
update(2*v,tl,tm,pos,val);
else
update(2*v+1,tm+1,tr,pos,val);
t[v]=add(t[2*v],t[2*v+1]);
}
p2 query(ll v,ll tl,ll tr,ll l,ll r){
if(l>r)
return {0,0};
else if(l==tl&&r==tr)
return t[v];
ll tm=(tl+tr)/2;
return add(query(2*v,tl,tm,l,min(r,tm)),query(2*v+1,tm+1,tr,max(l,tm+1),r));
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll n,res=0;
cin>>n;
for(ll i=1;i<=n;i++)
cin>>x[i];
for(ll i=1;i<=n;i++){
ll y;
cin>>y;
if(t>0)
v1.push_back({x[i],y});
else
v2.push_back({x[i],-y});
v.push_back(abs(y));
}
sort(v1.begin(),v1.end());
sort(v2.begin(),v2.end());
sort(v.begin(),v.end());
map<ll,ll> m;
for(ll i=0;i<n;i++)
if(m.find(v[i])==m.end())
m.insert({v[i],i+1});
for(ll i=0;i<v1.size();i++){
update(1,1,n,m[v1[i].second],v1[i].first);
p2 a=query(1,1,n,1,m[v1[i].second]);
res+=a.second*v1[i].first-a.first;
}
for(ll i=0;i<800005;i++)
t[i]={0,0};
for(ll i=0;i<v2.size();i++){
update(1,1,n,m[v2[i].second],v2[i].first);
p2 a=query(1,1,n,1,m[v2[i].second]);
res+=a.second*v2[i].first-a.first;
}
for(ll i=0;i<v1.size();i++){
p2 a=query(1,1,n,1,m[v1[i].second]);
res+=a.second*v1[i].first-a.first;
}
cout<<res;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
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()));
}
//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
|
java
|
//package com.netease.music.codeforces.round624.div3;
import java.util.Arrays;
import java.util.Scanner;
/**
* Created by dezhonger on 2020/2/27
*/
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[][] xv = new int[n][2];
int[] v = new int[n];
for (int i = 0; i < n; i++) {
xv[i][0] = scanner.nextInt();
}
for (int i = 0; i < n; i++) {
xv[i][1] = scanner.nextInt();
v[i] = xv[i][1];
}
Arrays.sort(v);
Arrays.sort(xv, (o1, o2) -> {
if (o1[0] != o2[0]) return Integer.compare(o1[0], o2[0]);
return Integer.compare(o1[1], o2[1]);
});
FenwickTree f1 = new FenwickTree(n);
FenwickTree f2 = new FenwickTree(n);
long result = 0;
for (int i = 0; i < n; i++) {
//按照x坐标从小到大进行枚举
//找到离散化后v的位置
int pos = Arrays.binarySearch(v, xv[i][1]);
pos++;
//速度比当前速度小的有贡献,贡献为距离之差
//有num个点的速度为当前速度小
int num = f1.sum(pos);
//这num个点的位置的坐标之和为xsum
int xsum = f2.sum(pos);
//计算对答案的贡献
result += num * xv[i][0] - xsum;
f1.change(pos, 1);
f2.change(pos, xv[i][0]);
}
System.out.println(result);
}
}
//树状数组
class FenwickTree {
int[] t;
int n;
public FenwickTree(int n) {
this.n = n;
this.t = new int[n + 1];
}
public void change(int pos, int val) {
while (pos <= n) {
t[pos] += val;
pos += lowbit(pos);
}
}
public int lowbit(int k) {
return k & (-k);
}
public int sum(int pos) {
int ans = 0;
while (pos > 0) {
ans += t[pos];
pos -= lowbit(pos);
}
return ans;
}
public int ask(int l, int r) {
return sum(r) - sum(l);
}
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
long long bit[4][200005];
void update(long long k, long long val, long long p) {
while (k < 200005) {
bit[p][k] += val;
k += (k & (-k));
}
}
long long query(long long k, long long p) {
long long s = 0;
while (k > 0) {
s += bit[p][k];
k -= (k & (-k));
}
return s;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
long long n;
cin >> n;
pair<long long, long long> p[n];
set<long long> v1, v2;
for (int i = 0; i < n; i++) cin >> p[i].first;
for (int i = 0; i < n; i++) {
cin >> p[i].second;
if (p[i].second < 0)
v1.insert(abs(p[i].second));
else if (p[i].second > 0)
v2.insert(p[i].second);
}
map<long long, long long> mp, mp2;
int cnt = 1;
for (auto i : v1) mp[-i] = cnt, cnt++;
cnt = 1;
for (auto i : v2) mp2[i] = cnt, cnt++;
sort(p, p + n);
long long ans = 0;
long long stat = 0, cn = 0;
for (int i = 0; i < n; i++) {
if (p[i].second > 0) {
p[i].second = mp2[p[i].second];
long long sum = query(p[i].second, 1) + query(200000, 0);
long long num = query(p[i].second, 3) + query(200000, 2);
ans += num * p[i].first - sum;
update(p[i].second, p[i].first, 1);
update(p[i].second, 1, 3);
} else if (p[i].second < 0) {
p[i].second = mp[p[i].second];
long long sum = query(200000, 0) - query(p[i].second - 1, 0);
long long num = query(200000, 2) - query(p[i].second - 1, 2);
ans += num * p[i].first - sum;
update(p[i].second, p[i].first, 0);
update(p[i].second, 1, 2);
} else {
long long sum = stat + query(200000, 0);
long long num = cn + query(200000, 2);
ans += num * p[i].first - sum;
stat += p[i].first;
cn++;
}
}
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 N = 200005;
long long n;
pair<long long, long long> x[N];
long long m;
map<long long, long long> mp;
long long ans;
struct seg {
long long vals[2 * N];
long long q(long long l, long long r) {
long long ret = 0;
for (l += m, r += m; l < r; l >>= 1, r >>= 1) {
if (l & 1) ret += vals[l++];
if (r & 1) ret += vals[--r];
}
return ret;
}
void inc(long long i, long long v) {
i += m;
vals[i] += v;
while (i > 1) {
vals[i >> 1] = vals[i] + vals[i ^ 1];
i >>= 1;
}
}
} sm, ct;
bool compSecond(pair<long long, long long>& a, pair<long long, long long>& b) {
return a.second < b.second;
}
void doMapping() {
sort(x, x + n, compSecond);
long long prev = -1;
for (long long i = 0; i < n; ++i) {
if (x[i].second != prev) {
mp[x[i].second] = m;
m++;
}
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
for (long long i = 0; i < n; ++i) {
cin >> x[i].first;
}
for (long long i = 0; i < n; ++i) {
cin >> x[i].second;
}
doMapping();
sort(x, x + n);
for (long long i = 0; i < n; ++i) {
long long pt = x[i].first;
long long cur = mp[x[i].second];
long long cur_sum = sm.q(0, cur + 1), cur_ct = ct.q(0, cur + 1);
ans += pt * cur_ct - cur_sum;
sm.inc(cur, pt);
ct.inc(cur, 1);
}
cout << ans << '\n';
return 0;
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int mpow(int base, int exp);
void ipgraph(int m);
void dfs(int u, int par);
const long long int mod = 1000000007;
const int N = 3e5, M = N;
vector<int> g[N];
int a[N];
int mpow(int base, int exp) {
base %= mod;
int result = 1;
while (exp > 0) {
if (exp & 1) result = ((long long int)result * base) % mod;
base = ((long long int)base * base) % mod;
exp >>= 1;
}
return result;
}
void ipgraph(int m) {
int u, v;
while (m--) {
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
}
void dfs(int u, int par) {
for (int v : g[u]) {
if (v == par) continue;
dfs(v, u);
}
}
long long int mod1(string num, long long int a1) {
long long int res = 0;
for (int i = 0; i < num.length(); i++)
res = (res * 10 + (int)num[i] - '0') % a1;
return res;
}
vector<pair<int, int> > pos, negs;
int mergepos(int l, int m, int r) {
int ls = m - l + 1;
int rs = r - m;
vector<pair<int, int> > lss(ls + 1), rss(rs + 1);
lss[ls] = {-1, -1};
rss[rs] = {-1, -1};
for (int i = l; i <= m; i++) lss[i - l] = pos[i];
for (int i = m + 1; i <= r; i++) rss[i - m - 1] = pos[i];
long long int runsum[ls + 1];
runsum[0] = 0;
for (int i = 0; i < ls; i++) runsum[i + 1] = runsum[i] + lss[i].first;
int i = 0, j = 0, diffs = 0;
for (int k = l; k <= r; k++) {
if (lss[i].second > rss[j].second) {
pos[k] = lss[i];
i++;
} else if (lss[i].second <= rss[j].second) {
if (i != ls) diffs += rss[j].first * (ls - i) - (runsum[ls] - runsum[i]);
pos[k] = rss[j];
j++;
}
}
return diffs;
}
int mergeneg(int l, int m, int r) {
int ls = m - l + 1;
int rs = r - m;
vector<pair<int, int> > lss(ls + 1), rss(rs + 1);
lss[ls] = {1, 1};
rss[rs] = {1, 1};
for (int i = l; i <= m; i++) lss[i - l] = negs[i];
for (int i = m + 1; i <= r; i++) rss[i - m - 1] = negs[i];
long long int runsum[rs + 1];
runsum[0] = 0;
for (int i = 0; i < rs; i++) runsum[i + 1] = runsum[i] + rss[i].first;
int i = 0, j = 0, diffs = 0;
for (int k = l; k <= r; k++) {
if (lss[i].second > rss[j].second) {
negs[k] = rss[i];
j++;
} else if (lss[i].second <= rss[j].second) {
if (j != rs) diffs += (runsum[rs] - runsum[j]) - lss[i].first * (rs - j);
negs[k] = lss[i];
i++;
}
}
return diffs;
}
int mergesort2(int l, int r) {
int mid = (l + r) / 2;
if (l < r) {
int lcount = mergesort2(l, mid);
int rcount = mergesort2(mid + 1, r);
int cross = mergeneg(l, mid, r);
return lcount + rcount + cross;
} else
return 0;
}
int mergesort1(int l, int r) {
int mid = (l + r) / 2;
if (l < r) {
int lcount = mergesort1(l, mid);
int rcount = mergesort1(mid + 1, r);
int cross = mergepos(l, mid, r);
return lcount + rcount + cross;
} else
return 0;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
int velo[n];
int i;
for (i = 0; i < n; i++) cin >> a[i];
for (i = 0; i < n; i++) cin >> velo[i];
for (i = 0; i < n; i++) {
if (velo[i] >= 0)
pos.push_back({a[i], velo[i]});
else
negs.push_back({a[i], velo[i]});
}
int x = pos.size();
int y = negs.size();
sort(pos.begin(), pos.end());
sort(negs.begin(), negs.end());
int diff = 0;
if (x > 0 && y > 0) {
x--;
y--;
int runsum[y];
runsum[0] = negs[0].first;
for (i = 1; i <= y; i++) runsum[i] = runsum[i - 1] + negs[i].first;
for (i = 0; i < n; i++) {
if (x >= 0 && y >= 0 && pos[x].first >= negs[y].first) {
diff += pos[x].first * (y + 1) - runsum[y];
x--;
} else if (x >= 0 && y >= 0 && pos[x].first < negs[y].first)
y--;
else
break;
}
}
x = pos.size();
y = negs.size();
int diff1 = mergesort1(0, x - 1);
int diff2 = mergesort2(0, y - 1);
cout << diff + diff1 + diff2 << 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
|
import sys
input=sys.stdin.readline
t=int(input())
import math
def build(arr,cur_pos,start,end,seg,pre):
if(start==end):
seg[cur_pos]=([arr[start]],[arr[start][0]])
else:
mid=(start+end)//2
build(arr,2*cur_pos+1,start,mid,seg,pre)
build(arr,2*cur_pos+2,mid+1,end,seg,pre)
seg[cur_pos]=merge(seg[2*cur_pos+1],seg[2*cur_pos+2],arr,start,mid,end)
def merge(L1,R1,arr,low,mid,high):
# #print(L,R,len(L),len(R))
L=L1[0]
R=R1[0]
i=0
j=0
k=low
while(i<len(L) and j<len(R)):
if(L[i][1]<R[j][1]):
if(k!=low):
pre[k]=pre[k-1]+L[i][0]
else:
pre[k]=L[i][0]
arr[k]=(L[i])
i+=1
else:
if(k!=low):
pre[k]=pre[k-1]+R[j][0]
else:
pre[k]=R[j][0]
arr[k]=(R[j])
j+=1
k+=1
while(i<len(L)):
if(k!=low):
pre[k]=pre[k-1]+L[i][0]
else:
pre[k]=L[i][0]
arr[k]=(L[i])
i+=1
k+=1
while(j<len(R)):
if(k!=low):
pre[k]=pre[k-1]+R[j][0]
else:
pre[k]=R[j][0]
arr[k]=(R[j])
j+=1
k+=1
return (arr[low:high+1],pre[low:high+1])
def bs(arr,k,start,end,n):
if(start<=end):
mid=(start+end)//2
if(arr[mid][1]>=k):
if(mid==0):
return -1
return bs(arr,k,start,mid-1,n)
else:
z=bs(arr,k,mid+1,end,n)
if(z==-1):
return mid
else:
return z
return -1
def query(seg,l,r,k,val,cur_pos,start,end,ans):
# print("elfcled",l,r,start,end)
if(l<=start and r>=end):
# print(";dkewl,dc")
# #print(seg[cur_pos])
t=bs(seg[cur_pos][0],k,0,len(seg[cur_pos][0])-1,len(seg[cur_pos][0])-1)
# print(t)
if(t!=-1):
# print(t," ",seg[cur_pos][0]," ",val)
return (seg[cur_pos][1][len(seg[cur_pos][1])-1]-seg[cur_pos][1][t])-(t+1)*(val)
else:
return seg[cur_pos][1][len(seg[cur_pos][1])-1]-(len(seg[cur_pos][1]))*(val)
return 0
#print(ans)
elif(l>end or r<start):
return 0
else:
mid=(start+end)//2
return query(seg,l,r,k,val,2*cur_pos+1,start,mid,ans)+query(seg,l,r,k,val,2*cur_pos+2,mid+1,end,ans)
x=list(map(int,input().split(" ")))
v=list(map(int,input().split(" ")))
# t=100000
# u=[[0,0]]*(t)
u=[]
for i in range(t):
u.append((x[i],v[i]))
u=sorted(u,key=lambda y:(y[0]))
arr=[]
for j in range(t):
arr.append((u[j][0],u[j][1]))
seg=[[(),()]]*(4*t)
pre=[0]*t
build(arr,0,0,t-1,seg,pre)
#print(seg)
res=0
for i in range(t):
ans=0
ans=query(seg,i+1,t-1,u[i][1],u[i][0],0,0,t-1,ans)
# print(ans)
if(ans>0):
res+=ans
# break
# res+=(ans-
# print(res)
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 N = 2e5 + 5;
int bit[N];
void add(int p, int v) {
for (p += 2; p < N; p += p & -p) bit[p] += v;
}
int query(int p) {
int r = 0;
for (p += 2; p; p -= p & -p) r += bit[p];
return r;
}
int n;
pair<int, int> p[N];
long long l[N], r[N];
int ql[N], qr[N];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) cin >> p[i].second;
for (int i = 1; i <= n; i++) cin >> p[i].first;
sort(p + 1, p + n + 1);
map<int, int> id;
int cnt = 0, last = 0x3f3f3f3f;
for (int i = 1; i <= n; i++) {
int v = p[i].first;
if (v != last) id[v] = ++cnt, last = v;
swap(p[i].first, p[i].second);
}
sort(p + 1, p + n + 1);
long long ans = 0;
for (int i = 1; i <= n; i++) {
int x = p[i].first, v = id[p[i].second];
ans += query(v) * x;
add(v, 1);
}
memset(bit, 0, sizeof(bit));
for (int i = n; i >= 1; i--) {
int x = p[i].first, v = id[p[i].second];
ans -= (query(n) - query(v - 1)) * x;
add(v, 1);
}
cout << ans << '\n';
}
|
1311_F. Moving Points
|
There are n points on a coordinate axis OX. The i-th point is located at the integer point x_i and has a speed v_i. It is guaranteed that no two points occupy the same coordinate. All n points move with the constant speed, the coordinate of the i-th point at the moment t (t can be non-integer) is calculated as x_i + t ⋅ v_i.
Consider two points i and j. Let d(i, j) be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points i and j coincide at some moment, the value d(i, j) will be 0.
Your task is to calculate the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Input
The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of points.
The second line of the input contains n integers x_1, x_2, ..., x_n (1 ≤ x_i ≤ 10^8), where x_i is the initial coordinate of the i-th point. It is guaranteed that all x_i are distinct.
The third line of the input contains n integers v_1, v_2, ..., v_n (-10^8 ≤ v_i ≤ 10^8), where v_i is the speed of the i-th point.
Output
Print one integer — the value ∑_{1 ≤ i < j ≤ n} d(i, j) (the sum of minimum distances over all pairs of points).
Examples
Input
3
1 3 2
-100 2 3
Output
3
Input
5
2 1 4 3 5
2 2 2 3 4
Output
19
Input
2
2 1
-3 0
Output
0
|
{
"input": [
"3\n1 3 2\n-100 2 3\n",
"2\n2 1\n-3 0\n",
"5\n2 1 4 3 5\n2 2 2 3 4\n"
],
"output": [
"3\n",
"0\n",
"19\n"
]
}
|
{
"input": [],
"output": []
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const long long INF = 1e18;
const long double PI = 4 * atan((long double)1);
const int INFTY = 1e7;
class FenwickTree2D {
private:
map<pair<long long, long long>, long long> m;
public:
FenwickTree2D() { m.clear(); }
void update(int r, int c, long long delta) {
int maxi = 2e8 + 5;
for (int r_ = r; r_ < (int)maxi; r_ = r_ + (r_ & -r_)) {
for (int c_ = c; c_ < (int)maxi; c_ = c_ + (c_ & -c_)) {
m[make_pair(r_, c_)] += delta;
}
}
}
long long sum(int r, int c) {
long long second = 0;
for (int r_ = r; r_ > 0; r_ = r_ - (r_ & (-r_))) {
for (int c_ = c; c_ > 0; c_ = c_ - (c_ & (-c_))) {
auto it = m.find(make_pair(r_, c_));
if (it != m.end()) {
second += it->second;
}
}
}
return second;
}
long long rangeQuery(int r1, int c1, int r2, int c2) {
return (sum(r2, c2) - sum(r1 - 1, c2) - sum(r2, c1 - 1) +
sum(r1 - 1, c1 - 1));
}
};
int main() {
ios::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
long long n;
cin >> n;
vector<long long> x(n), v(n);
for (int i = 0; i < (n); i++) cin >> x[i];
set<long long> second;
unordered_map<long long, long long> mapper;
for (int i = 0; i < (n); i++) cin >> v[i], second.insert(v[i]);
long long to = 2;
for (const auto& v : second) mapper[v] = to++;
long long ans = 0;
FenwickTree2D ft;
FenwickTree2D counter;
for (int i = 0; i < n; i++) {
long long q1 = ft.rangeQuery(x[i] + 1, mapper[v[i]] + 1, 2e8 + 1, 2e8 + 1);
long long count1 =
counter.rangeQuery(x[i] + 1, mapper[v[i]] + 1, 2e8 + 1, 2e8 + 1);
long long q2 = ft.rangeQuery(0, 0, x[i] - 1, mapper[v[i]] - 1);
long long count2 = counter.rangeQuery(0, 0, x[i] - 1, mapper[v[i]] - 1);
long long q3 = ft.rangeQuery(0, mapper[v[i]], 2e8 + 1, mapper[v[i]]);
long long count3 =
counter.rangeQuery(0, mapper[v[i]], 2e8 + 1, mapper[v[i]]);
ft.update(x[i], mapper[v[i]], x[i]);
counter.update(x[i], mapper[v[i]], 1);
long long delta = abs(1ll * count1 * x[i] - q1) +
abs(1ll * count2 * x[i] - q2) +
abs(1ll * count3 * x[i] - q3);
ans += delta;
}
cout << ans << endl;
}
|
1334_D. Minimum Euler Cycle
|
You are given a complete directed graph K_n with n vertices: each pair of vertices u ≠ v in K_n have both directed edges (u, v) and (v, u); there are no self-loops.
You should find such a cycle in K_n that visits every directed edge exactly once (allowing for revisiting vertices).
We can write such cycle as a list of n(n - 1) + 1 vertices v_1, v_2, v_3, ..., v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1 — a visiting order, where each (v_i, v_{i + 1}) occurs exactly once.
Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists.
Since the answer can be too large print its [l, r] segment, in other words, v_l, v_{l + 1}, ..., v_r.
Input
The first line contains the single integer T (1 ≤ T ≤ 100) — the number of test cases.
Next T lines contain test cases — one per line. The first and only line of each test case contains three integers n, l and r (2 ≤ n ≤ 10^5, 1 ≤ l ≤ r ≤ n(n - 1) + 1, r - l + 1 ≤ 10^5) — the number of vertices in K_n, and segment of the cycle to print.
It's guaranteed that the total sum of n doesn't exceed 10^5 and the total sum of r - l + 1 doesn't exceed 10^5.
Output
For each test case print the segment v_l, v_{l + 1}, ..., v_r of the lexicographically smallest cycle that visits every edge exactly once.
Example
Input
3
2 1 3
3 3 6
99995 9998900031 9998900031
Output
1 2 1
1 3 2 3
1
Note
In the second test case, the lexicographically minimum cycle looks like: 1, 2, 1, 3, 2, 3, 1.
In the third test case, it's quite obvious that the cycle should start and end in vertex 1.
|
{
"input": [
"3\n2 1 3\n3 3 6\n99995 9998900031 9998900031\n"
],
"output": [
"1 2 1 \n1 3 2 3 \n1 \n"
]
}
|
{
"input": [
"1\n2 2 3\n",
"1\n4 13 13\n",
"1\n3 1 1\n",
"10\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n",
"1\n3 7 7\n",
"1\n25 30 295\n",
"1\n4 12 13\n",
"5\n3 7 7\n4 13 13\n5 21 21\n6 31 31\n7 42 43\n",
"1\n5 4 4\n"
],
"output": [
"2 1 \n",
"1 \n",
"1 \n",
"1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n",
"1 \n",
"16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 2 3 2 4 2 5 2 6 2 7 2 8 2 9 2 10 2 11 2 12 2 13 2 14 2 15 2 16 2 17 2 18 2 19 2 20 2 21 2 22 2 23 2 24 2 25 3 4 3 5 3 6 3 7 3 8 3 9 3 10 3 11 3 12 3 13 3 14 3 15 3 16 3 17 3 18 3 19 3 20 3 21 3 22 3 23 3 24 3 25 4 5 4 6 4 7 4 8 4 9 4 10 4 11 4 12 4 13 4 14 4 15 4 16 4 17 4 18 4 19 4 20 4 21 4 22 4 23 4 24 4 25 5 6 5 7 5 8 5 9 5 10 5 11 5 12 5 13 5 14 5 15 5 16 5 17 5 18 5 19 5 20 5 21 5 22 5 23 5 24 5 25 6 7 6 8 6 9 6 10 6 11 6 12 6 13 6 14 6 15 6 16 6 17 6 18 6 19 6 20 6 21 6 22 6 23 6 24 6 25 7 8 7 9 7 10 7 11 7 12 7 13 7 14 7 15 7 16 7 17 7 18 7 19 7 20 7 21 7 22 7 23 7 24 7 25 8 \n",
"4 1 \n",
"1 \n1 \n1 \n1 \n7 1 \n",
"3 \n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
long long n;
int tc = 0;
int solve();
int main() {
ios_base::sync_with_stdio(0);
if (tc < 0) {
cout << "TC!\n";
cin.ignore(1e8);
} else if (!tc)
cin >> tc;
while (tc--) solve();
return 0;
}
int solve() {
long long l, r;
cin >> n >> l >> r;
l--;
r--;
long long a = 1;
while (1) {
if (l > 2 * (n - a)) {
l -= 2 * (n - a);
r -= 2 * (n - a);
a++;
} else
break;
}
vector<long long> ans;
int b = a + 1;
while (ans.size() <= r + 1) {
ans.push_back(a);
ans.push_back(b);
b++;
if (b == n + 1) {
a++;
if (a == n) {
ans.push_back(1);
break;
}
b = a + 1;
}
}
for (int i = l; i <= r; i++) cout << ans[i] << " ";
cout << endl;
return 0;
}
|
1334_D. Minimum Euler Cycle
|
You are given a complete directed graph K_n with n vertices: each pair of vertices u ≠ v in K_n have both directed edges (u, v) and (v, u); there are no self-loops.
You should find such a cycle in K_n that visits every directed edge exactly once (allowing for revisiting vertices).
We can write such cycle as a list of n(n - 1) + 1 vertices v_1, v_2, v_3, ..., v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1 — a visiting order, where each (v_i, v_{i + 1}) occurs exactly once.
Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists.
Since the answer can be too large print its [l, r] segment, in other words, v_l, v_{l + 1}, ..., v_r.
Input
The first line contains the single integer T (1 ≤ T ≤ 100) — the number of test cases.
Next T lines contain test cases — one per line. The first and only line of each test case contains three integers n, l and r (2 ≤ n ≤ 10^5, 1 ≤ l ≤ r ≤ n(n - 1) + 1, r - l + 1 ≤ 10^5) — the number of vertices in K_n, and segment of the cycle to print.
It's guaranteed that the total sum of n doesn't exceed 10^5 and the total sum of r - l + 1 doesn't exceed 10^5.
Output
For each test case print the segment v_l, v_{l + 1}, ..., v_r of the lexicographically smallest cycle that visits every edge exactly once.
Example
Input
3
2 1 3
3 3 6
99995 9998900031 9998900031
Output
1 2 1
1 3 2 3
1
Note
In the second test case, the lexicographically minimum cycle looks like: 1, 2, 1, 3, 2, 3, 1.
In the third test case, it's quite obvious that the cycle should start and end in vertex 1.
|
{
"input": [
"3\n2 1 3\n3 3 6\n99995 9998900031 9998900031\n"
],
"output": [
"1 2 1 \n1 3 2 3 \n1 \n"
]
}
|
{
"input": [
"1\n2 2 3\n",
"1\n4 13 13\n",
"1\n3 1 1\n",
"10\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n",
"1\n3 7 7\n",
"1\n25 30 295\n",
"1\n4 12 13\n",
"5\n3 7 7\n4 13 13\n5 21 21\n6 31 31\n7 42 43\n",
"1\n5 4 4\n"
],
"output": [
"2 1 \n",
"1 \n",
"1 \n",
"1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n",
"1 \n",
"16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 2 3 2 4 2 5 2 6 2 7 2 8 2 9 2 10 2 11 2 12 2 13 2 14 2 15 2 16 2 17 2 18 2 19 2 20 2 21 2 22 2 23 2 24 2 25 3 4 3 5 3 6 3 7 3 8 3 9 3 10 3 11 3 12 3 13 3 14 3 15 3 16 3 17 3 18 3 19 3 20 3 21 3 22 3 23 3 24 3 25 4 5 4 6 4 7 4 8 4 9 4 10 4 11 4 12 4 13 4 14 4 15 4 16 4 17 4 18 4 19 4 20 4 21 4 22 4 23 4 24 4 25 5 6 5 7 5 8 5 9 5 10 5 11 5 12 5 13 5 14 5 15 5 16 5 17 5 18 5 19 5 20 5 21 5 22 5 23 5 24 5 25 6 7 6 8 6 9 6 10 6 11 6 12 6 13 6 14 6 15 6 16 6 17 6 18 6 19 6 20 6 21 6 22 6 23 6 24 6 25 7 8 7 9 7 10 7 11 7 12 7 13 7 14 7 15 7 16 7 17 7 18 7 19 7 20 7 21 7 22 7 23 7 24 7 25 8 \n",
"4 1 \n",
"1 \n1 \n1 \n1 \n7 1 \n",
"3 \n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
void solve() {
int64_t n, l, r;
cin >> n >> l >> r;
l--, r--;
int64_t cnt = 0;
int64_t start = 1;
int64_t num = n - 1;
while (start < n) {
if (l < cnt + 2 * num && r >= cnt) {
for (int64_t i = 0; i < 2 * num; i++) {
if (l <= cnt + i && cnt + i <= r) {
if (i & 1)
cout << start + (i + 1) / 2 << ' ';
else
cout << start << ' ';
}
}
}
cnt += 2 * num;
num--;
start++;
}
if (r == cnt) cout << 1;
cout << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int T;
cin >> T;
while (T--) {
solve();
}
}
|
1334_D. Minimum Euler Cycle
|
You are given a complete directed graph K_n with n vertices: each pair of vertices u ≠ v in K_n have both directed edges (u, v) and (v, u); there are no self-loops.
You should find such a cycle in K_n that visits every directed edge exactly once (allowing for revisiting vertices).
We can write such cycle as a list of n(n - 1) + 1 vertices v_1, v_2, v_3, ..., v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1 — a visiting order, where each (v_i, v_{i + 1}) occurs exactly once.
Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists.
Since the answer can be too large print its [l, r] segment, in other words, v_l, v_{l + 1}, ..., v_r.
Input
The first line contains the single integer T (1 ≤ T ≤ 100) — the number of test cases.
Next T lines contain test cases — one per line. The first and only line of each test case contains three integers n, l and r (2 ≤ n ≤ 10^5, 1 ≤ l ≤ r ≤ n(n - 1) + 1, r - l + 1 ≤ 10^5) — the number of vertices in K_n, and segment of the cycle to print.
It's guaranteed that the total sum of n doesn't exceed 10^5 and the total sum of r - l + 1 doesn't exceed 10^5.
Output
For each test case print the segment v_l, v_{l + 1}, ..., v_r of the lexicographically smallest cycle that visits every edge exactly once.
Example
Input
3
2 1 3
3 3 6
99995 9998900031 9998900031
Output
1 2 1
1 3 2 3
1
Note
In the second test case, the lexicographically minimum cycle looks like: 1, 2, 1, 3, 2, 3, 1.
In the third test case, it's quite obvious that the cycle should start and end in vertex 1.
|
{
"input": [
"3\n2 1 3\n3 3 6\n99995 9998900031 9998900031\n"
],
"output": [
"1 2 1 \n1 3 2 3 \n1 \n"
]
}
|
{
"input": [
"1\n2 2 3\n",
"1\n4 13 13\n",
"1\n3 1 1\n",
"10\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n",
"1\n3 7 7\n",
"1\n25 30 295\n",
"1\n4 12 13\n",
"5\n3 7 7\n4 13 13\n5 21 21\n6 31 31\n7 42 43\n",
"1\n5 4 4\n"
],
"output": [
"2 1 \n",
"1 \n",
"1 \n",
"1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n",
"1 \n",
"16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 2 3 2 4 2 5 2 6 2 7 2 8 2 9 2 10 2 11 2 12 2 13 2 14 2 15 2 16 2 17 2 18 2 19 2 20 2 21 2 22 2 23 2 24 2 25 3 4 3 5 3 6 3 7 3 8 3 9 3 10 3 11 3 12 3 13 3 14 3 15 3 16 3 17 3 18 3 19 3 20 3 21 3 22 3 23 3 24 3 25 4 5 4 6 4 7 4 8 4 9 4 10 4 11 4 12 4 13 4 14 4 15 4 16 4 17 4 18 4 19 4 20 4 21 4 22 4 23 4 24 4 25 5 6 5 7 5 8 5 9 5 10 5 11 5 12 5 13 5 14 5 15 5 16 5 17 5 18 5 19 5 20 5 21 5 22 5 23 5 24 5 25 6 7 6 8 6 9 6 10 6 11 6 12 6 13 6 14 6 15 6 16 6 17 6 18 6 19 6 20 6 21 6 22 6 23 6 24 6 25 7 8 7 9 7 10 7 11 7 12 7 13 7 14 7 15 7 16 7 17 7 18 7 19 7 20 7 21 7 22 7 23 7 24 7 25 8 \n",
"4 1 \n",
"1 \n1 \n1 \n1 \n7 1 \n",
"3 \n"
]
}
|
CORRECT
|
java
|
import java.io.*;
import java.util.*;
public class D
{
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer tok;
HashMap<List<Long>, Long> map = new HashMap<>();
public void go() throws IOException
{
StringTokenizer tok = new StringTokenizer(in.readLine());
int zzz = Integer.parseInt(tok.nextToken());
for (int zz = 0; zz < zzz; zz++)
{
ntok();
long n = lpar();
long l = lpar()-1;
long r = lpar()-1;
for (long i = l; i <= r; i++) {
out.print(getIndex2(n, i));
out.print(' ');
}
out.println();
// for (long i = 0; i <= n*(n-1); i++) {
// if (getIndex(n, i) != getIndex2(n, i)) {
// out.println("WRONG " + i);
// break;
// }
// }
// for (long i = 0; i <= n*(n-1); i++) {
// out.print(getIndex(n, i));
// out.print(' ');
// }
// out.println();
// for (long i = 0; i <= n*(n-1); i++) {
// out.print(getIndex2(n, i));
// out.print(' ');
// }
// out.println();
// printOrder((int)n);
}
out.flush();
in.close();
}
public void printOrder(int n) {
boolean[][] mat = new boolean[n][n];
for (boolean[] arr : mat) {
Arrays.fill(arr, true);
}
for (int i = 0; i < n; i++) {
mat[i][i] = false;
}
int curr = 0;
out.print('1');
for (int i = 0; i < n*(n-1); i++) {
for (int e = 0; e < n; e++) {
if (mat[curr][e]) {
mat[curr][e] = false;
curr = e;
break;
}
}
out.print(' ');
out.print(curr+1);
}
out.println();
}
public long getIndex2(long n, long i) {
List<Long> pair = new ArrayList<>();
pair.add(n);
pair.add(i);
if (map.containsKey(pair)) {
return map.get(pair);
}
long ans;
if (i < (n-1)*2) {
ans = i % 2 == 0 ? 1 : i/2+2;
} else if (i == n*(n-1)-1) {
ans = n;
} else if (i == n*(n-1)) {
ans = 1;
} else {
long l = 1;
long r = n-1;
while (r > l+1) {
long mid = (r+l)/2;
long minus = 2 * ((n*(n-1)/2) - ((n-mid)*(n-mid-1)/2));
if (minus > i) {
r = mid - 1;
} else {
l = mid;
}
}
long minus = 2 * ((n*(n-1)/2) - ((n-l)*(n-l-1)/2));
ans = getIndex2(n-l, i - minus) + l;
}
map.put(pair, ans);
return ans;
}
public long getIndex(long n, long i) {
List<Long> pair = new ArrayList<>();
pair.add(n);
pair.add(i);
if (map.containsKey(pair)) {
return map.get(pair);
}
long ans;
if (i < (n-1)*2) {
ans = i % 2 == 0 ? 1 : i/2+2;
} else if (i == n*(n-1)-1) {
ans = n;
} else if (i == n*(n-1)) {
ans = 1;
} else {
// long l = 1;
// long r = n;
// while (r > l+1) {
// long mid = (r+l)/2;
// long minus = 2 * ((n*(n-1)/2) - ((n-mid)*(n-mid-1)/2));
// if (minus > i) {
// r = mid - 1;
// } else {
// l = mid;
// }
// }
// long minus = 2 * ((n*(n-1)/2) - ((n-l)*(n-l-1)/2));
// ans = getIndex(n-1, i - minus) + l;
ans = getIndex(n-1, i - (n-1)*2) + 1;
}
map.put(pair, ans);
return ans;
}
public void ntok() throws IOException
{
tok = new StringTokenizer(in.readLine());
}
public int ipar()
{
return Integer.parseInt(tok.nextToken());
}
public int[] iapar(int n)
{
int[] arr = new int[n];
for (int i = 0; i < n; i++)
{
arr[i] = ipar();
}
return arr;
}
public long lpar()
{
return Long.parseLong(tok.nextToken());
}
public long[] lapar(int n)
{
long[] arr = new long[n];
for (int i = 0; i < n; i++)
{
arr[i] = lpar();
}
return arr;
}
public double dpar()
{
return Double.parseDouble(tok.nextToken());
}
public String spar()
{
return tok.nextToken();
}
public static void main(String[] args) throws IOException
{
new D().go();
}
}
|
1334_D. Minimum Euler Cycle
|
You are given a complete directed graph K_n with n vertices: each pair of vertices u ≠ v in K_n have both directed edges (u, v) and (v, u); there are no self-loops.
You should find such a cycle in K_n that visits every directed edge exactly once (allowing for revisiting vertices).
We can write such cycle as a list of n(n - 1) + 1 vertices v_1, v_2, v_3, ..., v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1 — a visiting order, where each (v_i, v_{i + 1}) occurs exactly once.
Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists.
Since the answer can be too large print its [l, r] segment, in other words, v_l, v_{l + 1}, ..., v_r.
Input
The first line contains the single integer T (1 ≤ T ≤ 100) — the number of test cases.
Next T lines contain test cases — one per line. The first and only line of each test case contains three integers n, l and r (2 ≤ n ≤ 10^5, 1 ≤ l ≤ r ≤ n(n - 1) + 1, r - l + 1 ≤ 10^5) — the number of vertices in K_n, and segment of the cycle to print.
It's guaranteed that the total sum of n doesn't exceed 10^5 and the total sum of r - l + 1 doesn't exceed 10^5.
Output
For each test case print the segment v_l, v_{l + 1}, ..., v_r of the lexicographically smallest cycle that visits every edge exactly once.
Example
Input
3
2 1 3
3 3 6
99995 9998900031 9998900031
Output
1 2 1
1 3 2 3
1
Note
In the second test case, the lexicographically minimum cycle looks like: 1, 2, 1, 3, 2, 3, 1.
In the third test case, it's quite obvious that the cycle should start and end in vertex 1.
|
{
"input": [
"3\n2 1 3\n3 3 6\n99995 9998900031 9998900031\n"
],
"output": [
"1 2 1 \n1 3 2 3 \n1 \n"
]
}
|
{
"input": [
"1\n2 2 3\n",
"1\n4 13 13\n",
"1\n3 1 1\n",
"10\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n",
"1\n3 7 7\n",
"1\n25 30 295\n",
"1\n4 12 13\n",
"5\n3 7 7\n4 13 13\n5 21 21\n6 31 31\n7 42 43\n",
"1\n5 4 4\n"
],
"output": [
"2 1 \n",
"1 \n",
"1 \n",
"1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n",
"1 \n",
"16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 2 3 2 4 2 5 2 6 2 7 2 8 2 9 2 10 2 11 2 12 2 13 2 14 2 15 2 16 2 17 2 18 2 19 2 20 2 21 2 22 2 23 2 24 2 25 3 4 3 5 3 6 3 7 3 8 3 9 3 10 3 11 3 12 3 13 3 14 3 15 3 16 3 17 3 18 3 19 3 20 3 21 3 22 3 23 3 24 3 25 4 5 4 6 4 7 4 8 4 9 4 10 4 11 4 12 4 13 4 14 4 15 4 16 4 17 4 18 4 19 4 20 4 21 4 22 4 23 4 24 4 25 5 6 5 7 5 8 5 9 5 10 5 11 5 12 5 13 5 14 5 15 5 16 5 17 5 18 5 19 5 20 5 21 5 22 5 23 5 24 5 25 6 7 6 8 6 9 6 10 6 11 6 12 6 13 6 14 6 15 6 16 6 17 6 18 6 19 6 20 6 21 6 22 6 23 6 24 6 25 7 8 7 9 7 10 7 11 7 12 7 13 7 14 7 15 7 16 7 17 7 18 7 19 7 20 7 21 7 22 7 23 7 24 7 25 8 \n",
"4 1 \n",
"1 \n1 \n1 \n1 \n7 1 \n",
"3 \n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int T;
int N;
long long L, R;
int main() {
scanf("%d", &T);
while (T--) {
scanf("%d%lld%lld", &N, &L, &R);
if (L == R && L == 1LL * N * (N - 1) + 1) {
printf("1\n");
continue;
}
long long i = N - 1;
for (; i; i--) {
if (L > 2 * i)
L -= 2 * i, R -= 2 * i;
else
break;
}
long long u = N - i, v = (L - 1) / 2 + u;
for (i = L; i <= R; i++) {
if (v == N) u++, v = u;
if (u == N) {
printf("1 ");
break;
}
if (i & 1LL)
printf("%lld ", u);
else
v++, printf("%lld ", v);
}
printf("\n");
}
return 0;
}
|
1334_D. Minimum Euler Cycle
|
You are given a complete directed graph K_n with n vertices: each pair of vertices u ≠ v in K_n have both directed edges (u, v) and (v, u); there are no self-loops.
You should find such a cycle in K_n that visits every directed edge exactly once (allowing for revisiting vertices).
We can write such cycle as a list of n(n - 1) + 1 vertices v_1, v_2, v_3, ..., v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1 — a visiting order, where each (v_i, v_{i + 1}) occurs exactly once.
Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists.
Since the answer can be too large print its [l, r] segment, in other words, v_l, v_{l + 1}, ..., v_r.
Input
The first line contains the single integer T (1 ≤ T ≤ 100) — the number of test cases.
Next T lines contain test cases — one per line. The first and only line of each test case contains three integers n, l and r (2 ≤ n ≤ 10^5, 1 ≤ l ≤ r ≤ n(n - 1) + 1, r - l + 1 ≤ 10^5) — the number of vertices in K_n, and segment of the cycle to print.
It's guaranteed that the total sum of n doesn't exceed 10^5 and the total sum of r - l + 1 doesn't exceed 10^5.
Output
For each test case print the segment v_l, v_{l + 1}, ..., v_r of the lexicographically smallest cycle that visits every edge exactly once.
Example
Input
3
2 1 3
3 3 6
99995 9998900031 9998900031
Output
1 2 1
1 3 2 3
1
Note
In the second test case, the lexicographically minimum cycle looks like: 1, 2, 1, 3, 2, 3, 1.
In the third test case, it's quite obvious that the cycle should start and end in vertex 1.
|
{
"input": [
"3\n2 1 3\n3 3 6\n99995 9998900031 9998900031\n"
],
"output": [
"1 2 1 \n1 3 2 3 \n1 \n"
]
}
|
{
"input": [
"1\n2 2 3\n",
"1\n4 13 13\n",
"1\n3 1 1\n",
"10\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n",
"1\n3 7 7\n",
"1\n25 30 295\n",
"1\n4 12 13\n",
"5\n3 7 7\n4 13 13\n5 21 21\n6 31 31\n7 42 43\n",
"1\n5 4 4\n"
],
"output": [
"2 1 \n",
"1 \n",
"1 \n",
"1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n",
"1 \n",
"16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 2 3 2 4 2 5 2 6 2 7 2 8 2 9 2 10 2 11 2 12 2 13 2 14 2 15 2 16 2 17 2 18 2 19 2 20 2 21 2 22 2 23 2 24 2 25 3 4 3 5 3 6 3 7 3 8 3 9 3 10 3 11 3 12 3 13 3 14 3 15 3 16 3 17 3 18 3 19 3 20 3 21 3 22 3 23 3 24 3 25 4 5 4 6 4 7 4 8 4 9 4 10 4 11 4 12 4 13 4 14 4 15 4 16 4 17 4 18 4 19 4 20 4 21 4 22 4 23 4 24 4 25 5 6 5 7 5 8 5 9 5 10 5 11 5 12 5 13 5 14 5 15 5 16 5 17 5 18 5 19 5 20 5 21 5 22 5 23 5 24 5 25 6 7 6 8 6 9 6 10 6 11 6 12 6 13 6 14 6 15 6 16 6 17 6 18 6 19 6 20 6 21 6 22 6 23 6 24 6 25 7 8 7 9 7 10 7 11 7 12 7 13 7 14 7 15 7 16 7 17 7 18 7 19 7 20 7 21 7 22 7 23 7 24 7 25 8 \n",
"4 1 \n",
"1 \n1 \n1 \n1 \n7 1 \n",
"3 \n"
]
}
|
CORRECT
|
java
|
import java.util.*;
import java.io.*;
public class Solution{
static PrintWriter out=new PrintWriter(System.out);
public static void main (String[] args) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String[] input=br.readLine().trim().split(" ");
int numTestCases=Integer.parseInt(input[0]);
while(numTestCases-->0){
input=br.readLine().trim().split(" ");
int n=Integer.parseInt(input[0]);
long l=Long.parseLong(input[1]);
long r=Long.parseLong(input[2]);
printSequence(n,l,r);
}
out.flush();
out.close();
}
public static void printSequence(int n,long l,long r)
{
ArrayList<Integer> ans=new ArrayList<>();
long total=1;
for(int i=1;i<n && l<=r;i++){
long len=n-i;
if(total+2*(len)<=l){
total+=2*len;
continue;
}
for(int j=i+1;j<=n;j++){
if(total==l && l<=r)
{
ans.add(i);
l++;
}
total++;
if(total==l && l<=r){
ans.add(j);
l++;
}
total++;
}
}
if(l<=r){
ans.add(1);
}
for(int i=0;i<ans.size();i++){
out.print(ans.get(i)+" ");
}
out.println();
}
}
|
1334_D. Minimum Euler Cycle
|
You are given a complete directed graph K_n with n vertices: each pair of vertices u ≠ v in K_n have both directed edges (u, v) and (v, u); there are no self-loops.
You should find such a cycle in K_n that visits every directed edge exactly once (allowing for revisiting vertices).
We can write such cycle as a list of n(n - 1) + 1 vertices v_1, v_2, v_3, ..., v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1 — a visiting order, where each (v_i, v_{i + 1}) occurs exactly once.
Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists.
Since the answer can be too large print its [l, r] segment, in other words, v_l, v_{l + 1}, ..., v_r.
Input
The first line contains the single integer T (1 ≤ T ≤ 100) — the number of test cases.
Next T lines contain test cases — one per line. The first and only line of each test case contains three integers n, l and r (2 ≤ n ≤ 10^5, 1 ≤ l ≤ r ≤ n(n - 1) + 1, r - l + 1 ≤ 10^5) — the number of vertices in K_n, and segment of the cycle to print.
It's guaranteed that the total sum of n doesn't exceed 10^5 and the total sum of r - l + 1 doesn't exceed 10^5.
Output
For each test case print the segment v_l, v_{l + 1}, ..., v_r of the lexicographically smallest cycle that visits every edge exactly once.
Example
Input
3
2 1 3
3 3 6
99995 9998900031 9998900031
Output
1 2 1
1 3 2 3
1
Note
In the second test case, the lexicographically minimum cycle looks like: 1, 2, 1, 3, 2, 3, 1.
In the third test case, it's quite obvious that the cycle should start and end in vertex 1.
|
{
"input": [
"3\n2 1 3\n3 3 6\n99995 9998900031 9998900031\n"
],
"output": [
"1 2 1 \n1 3 2 3 \n1 \n"
]
}
|
{
"input": [
"1\n2 2 3\n",
"1\n4 13 13\n",
"1\n3 1 1\n",
"10\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n",
"1\n3 7 7\n",
"1\n25 30 295\n",
"1\n4 12 13\n",
"5\n3 7 7\n4 13 13\n5 21 21\n6 31 31\n7 42 43\n",
"1\n5 4 4\n"
],
"output": [
"2 1 \n",
"1 \n",
"1 \n",
"1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n",
"1 \n",
"16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 2 3 2 4 2 5 2 6 2 7 2 8 2 9 2 10 2 11 2 12 2 13 2 14 2 15 2 16 2 17 2 18 2 19 2 20 2 21 2 22 2 23 2 24 2 25 3 4 3 5 3 6 3 7 3 8 3 9 3 10 3 11 3 12 3 13 3 14 3 15 3 16 3 17 3 18 3 19 3 20 3 21 3 22 3 23 3 24 3 25 4 5 4 6 4 7 4 8 4 9 4 10 4 11 4 12 4 13 4 14 4 15 4 16 4 17 4 18 4 19 4 20 4 21 4 22 4 23 4 24 4 25 5 6 5 7 5 8 5 9 5 10 5 11 5 12 5 13 5 14 5 15 5 16 5 17 5 18 5 19 5 20 5 21 5 22 5 23 5 24 5 25 6 7 6 8 6 9 6 10 6 11 6 12 6 13 6 14 6 15 6 16 6 17 6 18 6 19 6 20 6 21 6 22 6 23 6 24 6 25 7 8 7 9 7 10 7 11 7 12 7 13 7 14 7 15 7 16 7 17 7 18 7 19 7 20 7 21 7 22 7 23 7 24 7 25 8 \n",
"4 1 \n",
"1 \n1 \n1 \n1 \n7 1 \n",
"3 \n"
]
}
|
CORRECT
|
python3
|
# -*- coding:utf-8 -*-
"""
created by shuangquan.huang at 2020/7/1
"""
import collections
import time
import os
import sys
import bisect
import heapq
from typing import List
def solve(n, l, r):
# 1, 2, 1, 3, ..., 1, n
# 2, 3, 2, 4, ..., 2, n
# ...
# n-1, n
# 1
lo, hi = 1, n
while lo <= hi:
k = (lo + hi) // 2
s = k * (2*n-1-k)
if s < l:
lo = k + 1
else:
hi = k - 1
k = lo
s = k * (2*n-1-k)
b = k
# [b, b+1, b, b+2, ..., b, n]
row = []
for i in range(b+1, n+1):
row.append(b)
row.append(i)
ans = row[l-s-1:]
d = r-l+1
if len(ans) >= d:
return ans[:d]
while len(ans) < d:
b += 1
row = []
for i in range(b + 1, n + 1):
row.append(b)
row.append(i)
if not row:
break
ans += row
ans.append(1)
# print(ans[:d])
return ans[:d]
if __name__ == '__main__':
T = int(input())
ans = []
for ti in range(T):
N, L, R = map(int, input().split())
ans.append(solve(N, L, R))
print('\n'.join([' '.join(map(str, v)) for v in ans]))
|
1334_D. Minimum Euler Cycle
|
You are given a complete directed graph K_n with n vertices: each pair of vertices u ≠ v in K_n have both directed edges (u, v) and (v, u); there are no self-loops.
You should find such a cycle in K_n that visits every directed edge exactly once (allowing for revisiting vertices).
We can write such cycle as a list of n(n - 1) + 1 vertices v_1, v_2, v_3, ..., v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1 — a visiting order, where each (v_i, v_{i + 1}) occurs exactly once.
Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists.
Since the answer can be too large print its [l, r] segment, in other words, v_l, v_{l + 1}, ..., v_r.
Input
The first line contains the single integer T (1 ≤ T ≤ 100) — the number of test cases.
Next T lines contain test cases — one per line. The first and only line of each test case contains three integers n, l and r (2 ≤ n ≤ 10^5, 1 ≤ l ≤ r ≤ n(n - 1) + 1, r - l + 1 ≤ 10^5) — the number of vertices in K_n, and segment of the cycle to print.
It's guaranteed that the total sum of n doesn't exceed 10^5 and the total sum of r - l + 1 doesn't exceed 10^5.
Output
For each test case print the segment v_l, v_{l + 1}, ..., v_r of the lexicographically smallest cycle that visits every edge exactly once.
Example
Input
3
2 1 3
3 3 6
99995 9998900031 9998900031
Output
1 2 1
1 3 2 3
1
Note
In the second test case, the lexicographically minimum cycle looks like: 1, 2, 1, 3, 2, 3, 1.
In the third test case, it's quite obvious that the cycle should start and end in vertex 1.
|
{
"input": [
"3\n2 1 3\n3 3 6\n99995 9998900031 9998900031\n"
],
"output": [
"1 2 1 \n1 3 2 3 \n1 \n"
]
}
|
{
"input": [
"1\n2 2 3\n",
"1\n4 13 13\n",
"1\n3 1 1\n",
"10\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n",
"1\n3 7 7\n",
"1\n25 30 295\n",
"1\n4 12 13\n",
"5\n3 7 7\n4 13 13\n5 21 21\n6 31 31\n7 42 43\n",
"1\n5 4 4\n"
],
"output": [
"2 1 \n",
"1 \n",
"1 \n",
"1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n",
"1 \n",
"16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 2 3 2 4 2 5 2 6 2 7 2 8 2 9 2 10 2 11 2 12 2 13 2 14 2 15 2 16 2 17 2 18 2 19 2 20 2 21 2 22 2 23 2 24 2 25 3 4 3 5 3 6 3 7 3 8 3 9 3 10 3 11 3 12 3 13 3 14 3 15 3 16 3 17 3 18 3 19 3 20 3 21 3 22 3 23 3 24 3 25 4 5 4 6 4 7 4 8 4 9 4 10 4 11 4 12 4 13 4 14 4 15 4 16 4 17 4 18 4 19 4 20 4 21 4 22 4 23 4 24 4 25 5 6 5 7 5 8 5 9 5 10 5 11 5 12 5 13 5 14 5 15 5 16 5 17 5 18 5 19 5 20 5 21 5 22 5 23 5 24 5 25 6 7 6 8 6 9 6 10 6 11 6 12 6 13 6 14 6 15 6 16 6 17 6 18 6 19 6 20 6 21 6 22 6 23 6 24 6 25 7 8 7 9 7 10 7 11 7 12 7 13 7 14 7 15 7 16 7 17 7 18 7 19 7 20 7 21 7 22 7 23 7 24 7 25 8 \n",
"4 1 \n",
"1 \n1 \n1 \n1 \n7 1 \n",
"3 \n"
]
}
|
CORRECT
|
java
|
import java.util.Arrays;
import java.util.Scanner;
public class D {
public static void main(String[] args) {
Scanner file = new Scanner(System.in);
int inputs = file.nextInt();
while(inputs-->0) {
int n = file.nextInt();
long l = file.nextLong();
long r = file.nextLong();
long[] changes = new long[n+1];
changes[1] = 2*(n-1);
for(int i = 2; i <= n; i++) {
changes[i] = changes[i-1] + Math.max(2*(n-i), 1);
}
// System.out.println(Arrays.toString(changes));
int level = 1;
while(l > changes[level])
level++;
long curr = (l-changes[level-1]+1)/2+level;
StringBuilder ans = new StringBuilder();
for(long index = l; index <= r; index++) {
if(index == changes[n])
ans.append(1);
else {
if(index > changes[level]) {
level++;
curr = level+1;
}
if(index % 2 == 0) {
ans.append(curr + " ");
curr++;
}
else {
ans.append(level + " ");
}
}
}
System.out.println(ans);
}
file.close();
}
}
|
1334_D. Minimum Euler Cycle
|
You are given a complete directed graph K_n with n vertices: each pair of vertices u ≠ v in K_n have both directed edges (u, v) and (v, u); there are no self-loops.
You should find such a cycle in K_n that visits every directed edge exactly once (allowing for revisiting vertices).
We can write such cycle as a list of n(n - 1) + 1 vertices v_1, v_2, v_3, ..., v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1 — a visiting order, where each (v_i, v_{i + 1}) occurs exactly once.
Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists.
Since the answer can be too large print its [l, r] segment, in other words, v_l, v_{l + 1}, ..., v_r.
Input
The first line contains the single integer T (1 ≤ T ≤ 100) — the number of test cases.
Next T lines contain test cases — one per line. The first and only line of each test case contains three integers n, l and r (2 ≤ n ≤ 10^5, 1 ≤ l ≤ r ≤ n(n - 1) + 1, r - l + 1 ≤ 10^5) — the number of vertices in K_n, and segment of the cycle to print.
It's guaranteed that the total sum of n doesn't exceed 10^5 and the total sum of r - l + 1 doesn't exceed 10^5.
Output
For each test case print the segment v_l, v_{l + 1}, ..., v_r of the lexicographically smallest cycle that visits every edge exactly once.
Example
Input
3
2 1 3
3 3 6
99995 9998900031 9998900031
Output
1 2 1
1 3 2 3
1
Note
In the second test case, the lexicographically minimum cycle looks like: 1, 2, 1, 3, 2, 3, 1.
In the third test case, it's quite obvious that the cycle should start and end in vertex 1.
|
{
"input": [
"3\n2 1 3\n3 3 6\n99995 9998900031 9998900031\n"
],
"output": [
"1 2 1 \n1 3 2 3 \n1 \n"
]
}
|
{
"input": [
"1\n2 2 3\n",
"1\n4 13 13\n",
"1\n3 1 1\n",
"10\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n",
"1\n3 7 7\n",
"1\n25 30 295\n",
"1\n4 12 13\n",
"5\n3 7 7\n4 13 13\n5 21 21\n6 31 31\n7 42 43\n",
"1\n5 4 4\n"
],
"output": [
"2 1 \n",
"1 \n",
"1 \n",
"1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n",
"1 \n",
"16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 2 3 2 4 2 5 2 6 2 7 2 8 2 9 2 10 2 11 2 12 2 13 2 14 2 15 2 16 2 17 2 18 2 19 2 20 2 21 2 22 2 23 2 24 2 25 3 4 3 5 3 6 3 7 3 8 3 9 3 10 3 11 3 12 3 13 3 14 3 15 3 16 3 17 3 18 3 19 3 20 3 21 3 22 3 23 3 24 3 25 4 5 4 6 4 7 4 8 4 9 4 10 4 11 4 12 4 13 4 14 4 15 4 16 4 17 4 18 4 19 4 20 4 21 4 22 4 23 4 24 4 25 5 6 5 7 5 8 5 9 5 10 5 11 5 12 5 13 5 14 5 15 5 16 5 17 5 18 5 19 5 20 5 21 5 22 5 23 5 24 5 25 6 7 6 8 6 9 6 10 6 11 6 12 6 13 6 14 6 15 6 16 6 17 6 18 6 19 6 20 6 21 6 22 6 23 6 24 6 25 7 8 7 9 7 10 7 11 7 12 7 13 7 14 7 15 7 16 7 17 7 18 7 19 7 20 7 21 7 22 7 23 7 24 7 25 8 \n",
"4 1 \n",
"1 \n1 \n1 \n1 \n7 1 \n",
"3 \n"
]
}
|
CORRECT
|
java
|
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Washoum
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
inputClass in = new inputClass(inputStream);
PrintWriter out = new PrintWriter(outputStream);
DMinimumEulerCycle solver = new DMinimumEulerCycle();
solver.solve(1, in, out);
out.close();
}
static class DMinimumEulerCycle {
public void solve(int testNumber, inputClass sc, PrintWriter out) {
int t = sc.nextInt();
while (t > 0) {
t--;
int n = sc.nextInt();
long l = sc.nextLong();
long r = sc.nextLong();
int min = 1;
long nagazt = 1;
while (nagazt < l) {
nagazt += 2 * (n - min);
min++;
}
min--;
nagazt -= 2 * (n - min);
loop:
for (; min <= n; min++) {
for (int i = min + 1; i <= n; i++) {
if (nagazt >= l) {
out.print(min + " ");
}
nagazt++;
if (nagazt > r) {
break loop;
}
if (nagazt >= l) {
out.print(i + " ");
}
nagazt++;
if (nagazt > r) {
break loop;
}
}
if (nagazt > r) {
break;
}
}
if (nagazt == r) {
out.println(1);
} else {
out.println();
}
}
}
}
static class inputClass {
BufferedReader br;
StringTokenizer st;
public inputClass(InputStream in) {
br = new BufferedReader(new InputStreamReader(in));
}
public String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
}
|
1334_D. Minimum Euler Cycle
|
You are given a complete directed graph K_n with n vertices: each pair of vertices u ≠ v in K_n have both directed edges (u, v) and (v, u); there are no self-loops.
You should find such a cycle in K_n that visits every directed edge exactly once (allowing for revisiting vertices).
We can write such cycle as a list of n(n - 1) + 1 vertices v_1, v_2, v_3, ..., v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1 — a visiting order, where each (v_i, v_{i + 1}) occurs exactly once.
Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists.
Since the answer can be too large print its [l, r] segment, in other words, v_l, v_{l + 1}, ..., v_r.
Input
The first line contains the single integer T (1 ≤ T ≤ 100) — the number of test cases.
Next T lines contain test cases — one per line. The first and only line of each test case contains three integers n, l and r (2 ≤ n ≤ 10^5, 1 ≤ l ≤ r ≤ n(n - 1) + 1, r - l + 1 ≤ 10^5) — the number of vertices in K_n, and segment of the cycle to print.
It's guaranteed that the total sum of n doesn't exceed 10^5 and the total sum of r - l + 1 doesn't exceed 10^5.
Output
For each test case print the segment v_l, v_{l + 1}, ..., v_r of the lexicographically smallest cycle that visits every edge exactly once.
Example
Input
3
2 1 3
3 3 6
99995 9998900031 9998900031
Output
1 2 1
1 3 2 3
1
Note
In the second test case, the lexicographically minimum cycle looks like: 1, 2, 1, 3, 2, 3, 1.
In the third test case, it's quite obvious that the cycle should start and end in vertex 1.
|
{
"input": [
"3\n2 1 3\n3 3 6\n99995 9998900031 9998900031\n"
],
"output": [
"1 2 1 \n1 3 2 3 \n1 \n"
]
}
|
{
"input": [
"1\n2 2 3\n",
"1\n4 13 13\n",
"1\n3 1 1\n",
"10\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n",
"1\n3 7 7\n",
"1\n25 30 295\n",
"1\n4 12 13\n",
"5\n3 7 7\n4 13 13\n5 21 21\n6 31 31\n7 42 43\n",
"1\n5 4 4\n"
],
"output": [
"2 1 \n",
"1 \n",
"1 \n",
"1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n",
"1 \n",
"16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 2 3 2 4 2 5 2 6 2 7 2 8 2 9 2 10 2 11 2 12 2 13 2 14 2 15 2 16 2 17 2 18 2 19 2 20 2 21 2 22 2 23 2 24 2 25 3 4 3 5 3 6 3 7 3 8 3 9 3 10 3 11 3 12 3 13 3 14 3 15 3 16 3 17 3 18 3 19 3 20 3 21 3 22 3 23 3 24 3 25 4 5 4 6 4 7 4 8 4 9 4 10 4 11 4 12 4 13 4 14 4 15 4 16 4 17 4 18 4 19 4 20 4 21 4 22 4 23 4 24 4 25 5 6 5 7 5 8 5 9 5 10 5 11 5 12 5 13 5 14 5 15 5 16 5 17 5 18 5 19 5 20 5 21 5 22 5 23 5 24 5 25 6 7 6 8 6 9 6 10 6 11 6 12 6 13 6 14 6 15 6 16 6 17 6 18 6 19 6 20 6 21 6 22 6 23 6 24 6 25 7 8 7 9 7 10 7 11 7 12 7 13 7 14 7 15 7 16 7 17 7 18 7 19 7 20 7 21 7 22 7 23 7 24 7 25 8 \n",
"4 1 \n",
"1 \n1 \n1 \n1 \n7 1 \n",
"3 \n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n, pstart, pend;
int t;
cin >> t;
for (int testcases = 1; testcases <= t; testcases++) {
cin >> n >> pstart >> pend;
bool completestart = true;
bool completeend = true;
bool flag = false;
bool paranoidcheck = false;
if (pstart == pend) {
paranoidcheck = true;
}
if (pend == n * (n - 1) + 1) {
if (pstart == pend) {
cout << 1 << endl;
continue;
} else {
flag = true;
pend -= 1;
}
}
if (pstart % 2 == 0) {
completestart = false;
} else {
pstart += 1;
}
pstart /= 2;
if (pend % 2 == 1) {
completeend = false;
pend += 1;
}
pend /= 2;
long long iter = pend - pstart;
if (paranoidcheck) {
iter -= 1;
}
long long a = 1;
long long b = 1;
while (pstart - (n - a) > 0) {
pstart -= (n - a);
a += 1;
}
b = a + pstart;
if (!completestart) {
cout << b << " ";
if (b < n) {
b += 1;
} else {
a += 1;
b = a + 1;
}
iter -= 1;
}
if (!completeend) {
iter -= 1;
}
for (int k = 0; k <= iter; k++) {
cout << a << " " << b << " ";
if (b < n) {
b += 1;
} else {
a += 1;
b = a + 1;
}
}
if (!completeend) {
cout << a << " ";
}
if (flag) {
cout << 1;
}
cout << endl;
}
return 0;
}
|
1334_D. Minimum Euler Cycle
|
You are given a complete directed graph K_n with n vertices: each pair of vertices u ≠ v in K_n have both directed edges (u, v) and (v, u); there are no self-loops.
You should find such a cycle in K_n that visits every directed edge exactly once (allowing for revisiting vertices).
We can write such cycle as a list of n(n - 1) + 1 vertices v_1, v_2, v_3, ..., v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1 — a visiting order, where each (v_i, v_{i + 1}) occurs exactly once.
Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists.
Since the answer can be too large print its [l, r] segment, in other words, v_l, v_{l + 1}, ..., v_r.
Input
The first line contains the single integer T (1 ≤ T ≤ 100) — the number of test cases.
Next T lines contain test cases — one per line. The first and only line of each test case contains three integers n, l and r (2 ≤ n ≤ 10^5, 1 ≤ l ≤ r ≤ n(n - 1) + 1, r - l + 1 ≤ 10^5) — the number of vertices in K_n, and segment of the cycle to print.
It's guaranteed that the total sum of n doesn't exceed 10^5 and the total sum of r - l + 1 doesn't exceed 10^5.
Output
For each test case print the segment v_l, v_{l + 1}, ..., v_r of the lexicographically smallest cycle that visits every edge exactly once.
Example
Input
3
2 1 3
3 3 6
99995 9998900031 9998900031
Output
1 2 1
1 3 2 3
1
Note
In the second test case, the lexicographically minimum cycle looks like: 1, 2, 1, 3, 2, 3, 1.
In the third test case, it's quite obvious that the cycle should start and end in vertex 1.
|
{
"input": [
"3\n2 1 3\n3 3 6\n99995 9998900031 9998900031\n"
],
"output": [
"1 2 1 \n1 3 2 3 \n1 \n"
]
}
|
{
"input": [
"1\n2 2 3\n",
"1\n4 13 13\n",
"1\n3 1 1\n",
"10\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n",
"1\n3 7 7\n",
"1\n25 30 295\n",
"1\n4 12 13\n",
"5\n3 7 7\n4 13 13\n5 21 21\n6 31 31\n7 42 43\n",
"1\n5 4 4\n"
],
"output": [
"2 1 \n",
"1 \n",
"1 \n",
"1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n",
"1 \n",
"16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 2 3 2 4 2 5 2 6 2 7 2 8 2 9 2 10 2 11 2 12 2 13 2 14 2 15 2 16 2 17 2 18 2 19 2 20 2 21 2 22 2 23 2 24 2 25 3 4 3 5 3 6 3 7 3 8 3 9 3 10 3 11 3 12 3 13 3 14 3 15 3 16 3 17 3 18 3 19 3 20 3 21 3 22 3 23 3 24 3 25 4 5 4 6 4 7 4 8 4 9 4 10 4 11 4 12 4 13 4 14 4 15 4 16 4 17 4 18 4 19 4 20 4 21 4 22 4 23 4 24 4 25 5 6 5 7 5 8 5 9 5 10 5 11 5 12 5 13 5 14 5 15 5 16 5 17 5 18 5 19 5 20 5 21 5 22 5 23 5 24 5 25 6 7 6 8 6 9 6 10 6 11 6 12 6 13 6 14 6 15 6 16 6 17 6 18 6 19 6 20 6 21 6 22 6 23 6 24 6 25 7 8 7 9 7 10 7 11 7 12 7 13 7 14 7 15 7 16 7 17 7 18 7 19 7 20 7 21 7 22 7 23 7 24 7 25 8 \n",
"4 1 \n",
"1 \n1 \n1 \n1 \n7 1 \n",
"3 \n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
const int N = 3 * 1e5 + 5;
const long long MOD = 1000000007;
const long long inf = 1e18;
using namespace std;
long long power(long long x, long long y, long long p) {
long long res = 1;
x = x % p;
if (x == 0) return 0;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
void solve() {
long long n, l, r;
cin >> n >> l >> r;
long long ctr = 0;
long long lt = l;
if (l == n * (n - 1) + 1) {
cout << 1 << "\n";
return;
}
while (lt > 0) {
ctr++;
lt -= 2 * (n - ctr);
}
lt += 2 * (n - ctr);
long long val = (r - l + 1);
while (val) {
val--;
if (ctr == n) {
cout << "1"
<< " "
<< "\n";
return;
}
if (lt % 2 == 1)
cout << ctr;
else
cout << ctr + lt / 2;
cout << " ";
if (lt < 2 * (n - ctr))
lt++;
else {
ctr++;
lt = 1;
}
}
cout << "\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
int t = 1;
cin >> t;
while (t--) {
solve();
}
}
|
1334_D. Minimum Euler Cycle
|
You are given a complete directed graph K_n with n vertices: each pair of vertices u ≠ v in K_n have both directed edges (u, v) and (v, u); there are no self-loops.
You should find such a cycle in K_n that visits every directed edge exactly once (allowing for revisiting vertices).
We can write such cycle as a list of n(n - 1) + 1 vertices v_1, v_2, v_3, ..., v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1 — a visiting order, where each (v_i, v_{i + 1}) occurs exactly once.
Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists.
Since the answer can be too large print its [l, r] segment, in other words, v_l, v_{l + 1}, ..., v_r.
Input
The first line contains the single integer T (1 ≤ T ≤ 100) — the number of test cases.
Next T lines contain test cases — one per line. The first and only line of each test case contains three integers n, l and r (2 ≤ n ≤ 10^5, 1 ≤ l ≤ r ≤ n(n - 1) + 1, r - l + 1 ≤ 10^5) — the number of vertices in K_n, and segment of the cycle to print.
It's guaranteed that the total sum of n doesn't exceed 10^5 and the total sum of r - l + 1 doesn't exceed 10^5.
Output
For each test case print the segment v_l, v_{l + 1}, ..., v_r of the lexicographically smallest cycle that visits every edge exactly once.
Example
Input
3
2 1 3
3 3 6
99995 9998900031 9998900031
Output
1 2 1
1 3 2 3
1
Note
In the second test case, the lexicographically minimum cycle looks like: 1, 2, 1, 3, 2, 3, 1.
In the third test case, it's quite obvious that the cycle should start and end in vertex 1.
|
{
"input": [
"3\n2 1 3\n3 3 6\n99995 9998900031 9998900031\n"
],
"output": [
"1 2 1 \n1 3 2 3 \n1 \n"
]
}
|
{
"input": [
"1\n2 2 3\n",
"1\n4 13 13\n",
"1\n3 1 1\n",
"10\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n",
"1\n3 7 7\n",
"1\n25 30 295\n",
"1\n4 12 13\n",
"5\n3 7 7\n4 13 13\n5 21 21\n6 31 31\n7 42 43\n",
"1\n5 4 4\n"
],
"output": [
"2 1 \n",
"1 \n",
"1 \n",
"1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n",
"1 \n",
"16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 2 3 2 4 2 5 2 6 2 7 2 8 2 9 2 10 2 11 2 12 2 13 2 14 2 15 2 16 2 17 2 18 2 19 2 20 2 21 2 22 2 23 2 24 2 25 3 4 3 5 3 6 3 7 3 8 3 9 3 10 3 11 3 12 3 13 3 14 3 15 3 16 3 17 3 18 3 19 3 20 3 21 3 22 3 23 3 24 3 25 4 5 4 6 4 7 4 8 4 9 4 10 4 11 4 12 4 13 4 14 4 15 4 16 4 17 4 18 4 19 4 20 4 21 4 22 4 23 4 24 4 25 5 6 5 7 5 8 5 9 5 10 5 11 5 12 5 13 5 14 5 15 5 16 5 17 5 18 5 19 5 20 5 21 5 22 5 23 5 24 5 25 6 7 6 8 6 9 6 10 6 11 6 12 6 13 6 14 6 15 6 16 6 17 6 18 6 19 6 20 6 21 6 22 6 23 6 24 6 25 7 8 7 9 7 10 7 11 7 12 7 13 7 14 7 15 7 16 7 17 7 18 7 19 7 20 7 21 7 22 7 23 7 24 7 25 8 \n",
"4 1 \n",
"1 \n1 \n1 \n1 \n7 1 \n",
"3 \n"
]
}
|
CORRECT
|
python3
|
import os
import sys
from io import BytesIO, IOBase
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")
t = int(input())
for _ in range(t):
n,a,b = map(int,input().split(' '))
loop = b+1-a
a-=1
a%=n*(n-1)
a+=1
sbb = 2*(n-1)
ctr = 1
ans = []
while a>sbb:
ctr+=1
sbb+=2*n-2*ctr
d = a-sbb+2*n-2*ctr
cur = ctr
d-=1
p1 = 1+cur
ti = 0
while d:
d-=1
ti += 1
if ti & 1:
cur = p1
if p1 == n:
ctr += 1
# if ctr == n-1:
p1 = ctr
else:
if ctr == n and p1 == n:
ctr = 1
p1 = 1
p1 += 1
cur = ctr
for j in range(loop):
ans.append(cur)
ti+=1
if ti & 1:
cur = p1
if p1 == n:
ctr += 1
# if ctr == n-1:
p1 = ctr
else:
if ctr == n and p1 == n:
ctr = 1
p1 = 1
p1+=1
cur = ctr
print(*ans)
|
1334_D. Minimum Euler Cycle
|
You are given a complete directed graph K_n with n vertices: each pair of vertices u ≠ v in K_n have both directed edges (u, v) and (v, u); there are no self-loops.
You should find such a cycle in K_n that visits every directed edge exactly once (allowing for revisiting vertices).
We can write such cycle as a list of n(n - 1) + 1 vertices v_1, v_2, v_3, ..., v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1 — a visiting order, where each (v_i, v_{i + 1}) occurs exactly once.
Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists.
Since the answer can be too large print its [l, r] segment, in other words, v_l, v_{l + 1}, ..., v_r.
Input
The first line contains the single integer T (1 ≤ T ≤ 100) — the number of test cases.
Next T lines contain test cases — one per line. The first and only line of each test case contains three integers n, l and r (2 ≤ n ≤ 10^5, 1 ≤ l ≤ r ≤ n(n - 1) + 1, r - l + 1 ≤ 10^5) — the number of vertices in K_n, and segment of the cycle to print.
It's guaranteed that the total sum of n doesn't exceed 10^5 and the total sum of r - l + 1 doesn't exceed 10^5.
Output
For each test case print the segment v_l, v_{l + 1}, ..., v_r of the lexicographically smallest cycle that visits every edge exactly once.
Example
Input
3
2 1 3
3 3 6
99995 9998900031 9998900031
Output
1 2 1
1 3 2 3
1
Note
In the second test case, the lexicographically minimum cycle looks like: 1, 2, 1, 3, 2, 3, 1.
In the third test case, it's quite obvious that the cycle should start and end in vertex 1.
|
{
"input": [
"3\n2 1 3\n3 3 6\n99995 9998900031 9998900031\n"
],
"output": [
"1 2 1 \n1 3 2 3 \n1 \n"
]
}
|
{
"input": [
"1\n2 2 3\n",
"1\n4 13 13\n",
"1\n3 1 1\n",
"10\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n",
"1\n3 7 7\n",
"1\n25 30 295\n",
"1\n4 12 13\n",
"5\n3 7 7\n4 13 13\n5 21 21\n6 31 31\n7 42 43\n",
"1\n5 4 4\n"
],
"output": [
"2 1 \n",
"1 \n",
"1 \n",
"1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n",
"1 \n",
"16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 2 3 2 4 2 5 2 6 2 7 2 8 2 9 2 10 2 11 2 12 2 13 2 14 2 15 2 16 2 17 2 18 2 19 2 20 2 21 2 22 2 23 2 24 2 25 3 4 3 5 3 6 3 7 3 8 3 9 3 10 3 11 3 12 3 13 3 14 3 15 3 16 3 17 3 18 3 19 3 20 3 21 3 22 3 23 3 24 3 25 4 5 4 6 4 7 4 8 4 9 4 10 4 11 4 12 4 13 4 14 4 15 4 16 4 17 4 18 4 19 4 20 4 21 4 22 4 23 4 24 4 25 5 6 5 7 5 8 5 9 5 10 5 11 5 12 5 13 5 14 5 15 5 16 5 17 5 18 5 19 5 20 5 21 5 22 5 23 5 24 5 25 6 7 6 8 6 9 6 10 6 11 6 12 6 13 6 14 6 15 6 16 6 17 6 18 6 19 6 20 6 21 6 22 6 23 6 24 6 25 7 8 7 9 7 10 7 11 7 12 7 13 7 14 7 15 7 16 7 17 7 18 7 19 7 20 7 21 7 22 7 23 7 24 7 25 8 \n",
"4 1 \n",
"1 \n1 \n1 \n1 \n7 1 \n",
"3 \n"
]
}
|
CORRECT
|
python2
|
'''input
3
2 1 3
3 3 6
99995 9998900031 9998900031
'''
import sys
import math
from collections import Counter
debug = 1
readln = sys.stdin.readline
#sys.setrecursionlimit(1000000)
def write(s):
sys.stdout.write(str(s))
def writeln(s):
sys.stdout.write(str(s))
sys.stdout.write('\n')
def readint():
return int(readln().strip())
def readints():
return map(int, readln().split())
def readstr():
return readln().strip()
def readstrs():
return readln().split()
def dbg(*args):
if debug: print(' '.join(map(str, args)))
def solve(n,l,r):
start = 0
ans = []
l, r = l-1, r-1
for i in xrange(1, n):
if l < start: break
next_start = start + (n-i) * 2
if l < next_start:
for cl in range(l, min(r+1, next_start)):
if cl % 2 == 0:
ans.append(i)
else:
ans.append((cl - start + 1) / 2 + i)
l = min(r, next_start)
start = next_start
if l == start:
ans.append(1)
return ans
t = readint()
for _ in xrange(t):
n, l, r = readints()
writeln(' '.join(map(str, solve(n,l,r))))
|
1334_D. Minimum Euler Cycle
|
You are given a complete directed graph K_n with n vertices: each pair of vertices u ≠ v in K_n have both directed edges (u, v) and (v, u); there are no self-loops.
You should find such a cycle in K_n that visits every directed edge exactly once (allowing for revisiting vertices).
We can write such cycle as a list of n(n - 1) + 1 vertices v_1, v_2, v_3, ..., v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1 — a visiting order, where each (v_i, v_{i + 1}) occurs exactly once.
Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists.
Since the answer can be too large print its [l, r] segment, in other words, v_l, v_{l + 1}, ..., v_r.
Input
The first line contains the single integer T (1 ≤ T ≤ 100) — the number of test cases.
Next T lines contain test cases — one per line. The first and only line of each test case contains three integers n, l and r (2 ≤ n ≤ 10^5, 1 ≤ l ≤ r ≤ n(n - 1) + 1, r - l + 1 ≤ 10^5) — the number of vertices in K_n, and segment of the cycle to print.
It's guaranteed that the total sum of n doesn't exceed 10^5 and the total sum of r - l + 1 doesn't exceed 10^5.
Output
For each test case print the segment v_l, v_{l + 1}, ..., v_r of the lexicographically smallest cycle that visits every edge exactly once.
Example
Input
3
2 1 3
3 3 6
99995 9998900031 9998900031
Output
1 2 1
1 3 2 3
1
Note
In the second test case, the lexicographically minimum cycle looks like: 1, 2, 1, 3, 2, 3, 1.
In the third test case, it's quite obvious that the cycle should start and end in vertex 1.
|
{
"input": [
"3\n2 1 3\n3 3 6\n99995 9998900031 9998900031\n"
],
"output": [
"1 2 1 \n1 3 2 3 \n1 \n"
]
}
|
{
"input": [
"1\n2 2 3\n",
"1\n4 13 13\n",
"1\n3 1 1\n",
"10\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n",
"1\n3 7 7\n",
"1\n25 30 295\n",
"1\n4 12 13\n",
"5\n3 7 7\n4 13 13\n5 21 21\n6 31 31\n7 42 43\n",
"1\n5 4 4\n"
],
"output": [
"2 1 \n",
"1 \n",
"1 \n",
"1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n",
"1 \n",
"16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 2 3 2 4 2 5 2 6 2 7 2 8 2 9 2 10 2 11 2 12 2 13 2 14 2 15 2 16 2 17 2 18 2 19 2 20 2 21 2 22 2 23 2 24 2 25 3 4 3 5 3 6 3 7 3 8 3 9 3 10 3 11 3 12 3 13 3 14 3 15 3 16 3 17 3 18 3 19 3 20 3 21 3 22 3 23 3 24 3 25 4 5 4 6 4 7 4 8 4 9 4 10 4 11 4 12 4 13 4 14 4 15 4 16 4 17 4 18 4 19 4 20 4 21 4 22 4 23 4 24 4 25 5 6 5 7 5 8 5 9 5 10 5 11 5 12 5 13 5 14 5 15 5 16 5 17 5 18 5 19 5 20 5 21 5 22 5 23 5 24 5 25 6 7 6 8 6 9 6 10 6 11 6 12 6 13 6 14 6 15 6 16 6 17 6 18 6 19 6 20 6 21 6 22 6 23 6 24 6 25 7 8 7 9 7 10 7 11 7 12 7 13 7 14 7 15 7 16 7 17 7 18 7 19 7 20 7 21 7 22 7 23 7 24 7 25 8 \n",
"4 1 \n",
"1 \n1 \n1 \n1 \n7 1 \n",
"3 \n"
]
}
|
CORRECT
|
java
|
import java.io.*;
import java.util.*;
/**
* @author Tran Anh Tai
* @link: https://codeforces.com/contest/1334/problem/D
* @Idea: the minimum lexicographical cycle will be in formed:
* 1. (1-2)-(1-3)-(1-4)....-(1-n); (2 * (n - 1))
* 2. (2-3)-(2-4)-(2-5)....-(2-n); (2 * (n - 2))
* 3. ...........................; 2 * (n - 1);
* n. 1
*/
public class MinimumEulerCycle {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Task solver = new Task();
solver.solve(in, out);
out.close();
}
// main solver
static class Task{
public void solve(InputReader in, PrintWriter out) {
int t = in.nextInt();
for (int test = 0; test < t; test++){
int n = in.nextInt();
long l = in.nextLong();
long r = in.nextLong();
int row = 0;
long total = 0;
for (row = 1; row <= n - 1; row++){
total += 2 * (n - row);
if (total >= l){
break;
}
}
if (row == n){
out.println(1); continue;
}
else{
int k = (int)(r - l + 1);
total -= 2 * (n - row);
l -= total;
int turn = 0;
int cur_vertice = row + 1;
int cur_row = row;
for (int i = 0; i < l - 1; i++){
if (turn == 1){
cur_vertice++;
}
turn = turn ^ 1;
}
for (int time = 0; time < k; time++){
if (cur_row == n){
out.print(1);
}
else if (turn == 0){
out.print(cur_row + " ");
}
else{
out.print(cur_vertice + " ");
cur_vertice++;
if (cur_vertice > n){
cur_row++;
cur_vertice = cur_row + 1;
}
}
turn = turn ^ 1;
}
out.println();
}
}
}
}
// fast input reader class;
static class InputReader {
BufferedReader br;
StringTokenizer st;
public InputReader(InputStream stream) {
br = new BufferedReader(new InputStreamReader(stream));
}
public String nextToken() {
while (st == null || !st.hasMoreTokens()) {
String line = null;
try {
line = br.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
if (line == null) {
return null;
}
st = new StringTokenizer(line);
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(nextToken());
}
public long nextLong(){
return Long.parseLong(nextToken());
}
}
}
|
1334_D. Minimum Euler Cycle
|
You are given a complete directed graph K_n with n vertices: each pair of vertices u ≠ v in K_n have both directed edges (u, v) and (v, u); there are no self-loops.
You should find such a cycle in K_n that visits every directed edge exactly once (allowing for revisiting vertices).
We can write such cycle as a list of n(n - 1) + 1 vertices v_1, v_2, v_3, ..., v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1 — a visiting order, where each (v_i, v_{i + 1}) occurs exactly once.
Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists.
Since the answer can be too large print its [l, r] segment, in other words, v_l, v_{l + 1}, ..., v_r.
Input
The first line contains the single integer T (1 ≤ T ≤ 100) — the number of test cases.
Next T lines contain test cases — one per line. The first and only line of each test case contains three integers n, l and r (2 ≤ n ≤ 10^5, 1 ≤ l ≤ r ≤ n(n - 1) + 1, r - l + 1 ≤ 10^5) — the number of vertices in K_n, and segment of the cycle to print.
It's guaranteed that the total sum of n doesn't exceed 10^5 and the total sum of r - l + 1 doesn't exceed 10^5.
Output
For each test case print the segment v_l, v_{l + 1}, ..., v_r of the lexicographically smallest cycle that visits every edge exactly once.
Example
Input
3
2 1 3
3 3 6
99995 9998900031 9998900031
Output
1 2 1
1 3 2 3
1
Note
In the second test case, the lexicographically minimum cycle looks like: 1, 2, 1, 3, 2, 3, 1.
In the third test case, it's quite obvious that the cycle should start and end in vertex 1.
|
{
"input": [
"3\n2 1 3\n3 3 6\n99995 9998900031 9998900031\n"
],
"output": [
"1 2 1 \n1 3 2 3 \n1 \n"
]
}
|
{
"input": [
"1\n2 2 3\n",
"1\n4 13 13\n",
"1\n3 1 1\n",
"10\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n",
"1\n3 7 7\n",
"1\n25 30 295\n",
"1\n4 12 13\n",
"5\n3 7 7\n4 13 13\n5 21 21\n6 31 31\n7 42 43\n",
"1\n5 4 4\n"
],
"output": [
"2 1 \n",
"1 \n",
"1 \n",
"1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n",
"1 \n",
"16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 2 3 2 4 2 5 2 6 2 7 2 8 2 9 2 10 2 11 2 12 2 13 2 14 2 15 2 16 2 17 2 18 2 19 2 20 2 21 2 22 2 23 2 24 2 25 3 4 3 5 3 6 3 7 3 8 3 9 3 10 3 11 3 12 3 13 3 14 3 15 3 16 3 17 3 18 3 19 3 20 3 21 3 22 3 23 3 24 3 25 4 5 4 6 4 7 4 8 4 9 4 10 4 11 4 12 4 13 4 14 4 15 4 16 4 17 4 18 4 19 4 20 4 21 4 22 4 23 4 24 4 25 5 6 5 7 5 8 5 9 5 10 5 11 5 12 5 13 5 14 5 15 5 16 5 17 5 18 5 19 5 20 5 21 5 22 5 23 5 24 5 25 6 7 6 8 6 9 6 10 6 11 6 12 6 13 6 14 6 15 6 16 6 17 6 18 6 19 6 20 6 21 6 22 6 23 6 24 6 25 7 8 7 9 7 10 7 11 7 12 7 13 7 14 7 15 7 16 7 17 7 18 7 19 7 20 7 21 7 22 7 23 7 24 7 25 8 \n",
"4 1 \n",
"1 \n1 \n1 \n1 \n7 1 \n",
"3 \n"
]
}
|
CORRECT
|
python3
|
t=int(input())
for _ in range(t):
n,l,r=map(int, input().split())
cycle_size = 2 * (n - 1)
cycle_start = 1
cycle_number = 1
# while True:
# if cycle_start + cycle_size >= l:
# break
# cycle_start += cycle_size
# cycle_number += 1
# cycle_size -= 2
# print [l,r]
while cycle_start + cycle_size <= l and cycle_number != n:
cycle_start += cycle_size
cycle_number += 1
cycle_size -= 2
# print('cycle_start, cycle_size', cycle_start, cycle_size)
ans = []
while l <= r:
# print('l, cycle_start, cycle_size', l, cycle_start, cycle_size)
if (l - cycle_start) & 1:
# print('cycle_size', cycle_size)
if not cycle_size:
ans.append(1)
else:
ans.append(cycle_number + (l - cycle_start + 1) // 2)
else:
# print('cycle_size', cycle_size)
if not cycle_size:
ans.append(1)
else:
ans.append(cycle_number)
# print('ans', ans)
# if (l - cycle_start) // 2
l += 1
if (l - cycle_start) >= cycle_size:
cycle_start += cycle_size
cycle_size -= 2
cycle_number += 1
print(' '.join(str(x) for x in ans))
|
1334_D. Minimum Euler Cycle
|
You are given a complete directed graph K_n with n vertices: each pair of vertices u ≠ v in K_n have both directed edges (u, v) and (v, u); there are no self-loops.
You should find such a cycle in K_n that visits every directed edge exactly once (allowing for revisiting vertices).
We can write such cycle as a list of n(n - 1) + 1 vertices v_1, v_2, v_3, ..., v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1 — a visiting order, where each (v_i, v_{i + 1}) occurs exactly once.
Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists.
Since the answer can be too large print its [l, r] segment, in other words, v_l, v_{l + 1}, ..., v_r.
Input
The first line contains the single integer T (1 ≤ T ≤ 100) — the number of test cases.
Next T lines contain test cases — one per line. The first and only line of each test case contains three integers n, l and r (2 ≤ n ≤ 10^5, 1 ≤ l ≤ r ≤ n(n - 1) + 1, r - l + 1 ≤ 10^5) — the number of vertices in K_n, and segment of the cycle to print.
It's guaranteed that the total sum of n doesn't exceed 10^5 and the total sum of r - l + 1 doesn't exceed 10^5.
Output
For each test case print the segment v_l, v_{l + 1}, ..., v_r of the lexicographically smallest cycle that visits every edge exactly once.
Example
Input
3
2 1 3
3 3 6
99995 9998900031 9998900031
Output
1 2 1
1 3 2 3
1
Note
In the second test case, the lexicographically minimum cycle looks like: 1, 2, 1, 3, 2, 3, 1.
In the third test case, it's quite obvious that the cycle should start and end in vertex 1.
|
{
"input": [
"3\n2 1 3\n3 3 6\n99995 9998900031 9998900031\n"
],
"output": [
"1 2 1 \n1 3 2 3 \n1 \n"
]
}
|
{
"input": [
"1\n2 2 3\n",
"1\n4 13 13\n",
"1\n3 1 1\n",
"10\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n",
"1\n3 7 7\n",
"1\n25 30 295\n",
"1\n4 12 13\n",
"5\n3 7 7\n4 13 13\n5 21 21\n6 31 31\n7 42 43\n",
"1\n5 4 4\n"
],
"output": [
"2 1 \n",
"1 \n",
"1 \n",
"1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n",
"1 \n",
"16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 2 3 2 4 2 5 2 6 2 7 2 8 2 9 2 10 2 11 2 12 2 13 2 14 2 15 2 16 2 17 2 18 2 19 2 20 2 21 2 22 2 23 2 24 2 25 3 4 3 5 3 6 3 7 3 8 3 9 3 10 3 11 3 12 3 13 3 14 3 15 3 16 3 17 3 18 3 19 3 20 3 21 3 22 3 23 3 24 3 25 4 5 4 6 4 7 4 8 4 9 4 10 4 11 4 12 4 13 4 14 4 15 4 16 4 17 4 18 4 19 4 20 4 21 4 22 4 23 4 24 4 25 5 6 5 7 5 8 5 9 5 10 5 11 5 12 5 13 5 14 5 15 5 16 5 17 5 18 5 19 5 20 5 21 5 22 5 23 5 24 5 25 6 7 6 8 6 9 6 10 6 11 6 12 6 13 6 14 6 15 6 16 6 17 6 18 6 19 6 20 6 21 6 22 6 23 6 24 6 25 7 8 7 9 7 10 7 11 7 12 7 13 7 14 7 15 7 16 7 17 7 18 7 19 7 20 7 21 7 22 7 23 7 24 7 25 8 \n",
"4 1 \n",
"1 \n1 \n1 \n1 \n7 1 \n",
"3 \n"
]
}
|
CORRECT
|
python3
|
for _ in range(int(input())):
numbers, LEFTs, RIGHTs = map(int, input().split())
KEYSs = 0
GREATS = 0
for i in range(1, numbers+1):
if KEYSs + 2 * (numbers-i) >= LEFTs:
GREATS = LEFTs-KEYSs-1
break
KEYSs += 2 * (numbers-i)
LISTs = []
while len(LISTs) < (RIGHTs-LEFTs+1) + GREATS:
for j in range(i+1, numbers+1):
LISTs.append(i)
LISTs.append(j)
i += 1
if i >= numbers:
LISTs.append(1)
break
print (' '.join(list(map(str, LISTs[GREATS:GREATS+RIGHTs-LEFTs+1]))))
|
1334_D. Minimum Euler Cycle
|
You are given a complete directed graph K_n with n vertices: each pair of vertices u ≠ v in K_n have both directed edges (u, v) and (v, u); there are no self-loops.
You should find such a cycle in K_n that visits every directed edge exactly once (allowing for revisiting vertices).
We can write such cycle as a list of n(n - 1) + 1 vertices v_1, v_2, v_3, ..., v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1 — a visiting order, where each (v_i, v_{i + 1}) occurs exactly once.
Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists.
Since the answer can be too large print its [l, r] segment, in other words, v_l, v_{l + 1}, ..., v_r.
Input
The first line contains the single integer T (1 ≤ T ≤ 100) — the number of test cases.
Next T lines contain test cases — one per line. The first and only line of each test case contains three integers n, l and r (2 ≤ n ≤ 10^5, 1 ≤ l ≤ r ≤ n(n - 1) + 1, r - l + 1 ≤ 10^5) — the number of vertices in K_n, and segment of the cycle to print.
It's guaranteed that the total sum of n doesn't exceed 10^5 and the total sum of r - l + 1 doesn't exceed 10^5.
Output
For each test case print the segment v_l, v_{l + 1}, ..., v_r of the lexicographically smallest cycle that visits every edge exactly once.
Example
Input
3
2 1 3
3 3 6
99995 9998900031 9998900031
Output
1 2 1
1 3 2 3
1
Note
In the second test case, the lexicographically minimum cycle looks like: 1, 2, 1, 3, 2, 3, 1.
In the third test case, it's quite obvious that the cycle should start and end in vertex 1.
|
{
"input": [
"3\n2 1 3\n3 3 6\n99995 9998900031 9998900031\n"
],
"output": [
"1 2 1 \n1 3 2 3 \n1 \n"
]
}
|
{
"input": [
"1\n2 2 3\n",
"1\n4 13 13\n",
"1\n3 1 1\n",
"10\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n",
"1\n3 7 7\n",
"1\n25 30 295\n",
"1\n4 12 13\n",
"5\n3 7 7\n4 13 13\n5 21 21\n6 31 31\n7 42 43\n",
"1\n5 4 4\n"
],
"output": [
"2 1 \n",
"1 \n",
"1 \n",
"1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n",
"1 \n",
"16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 2 3 2 4 2 5 2 6 2 7 2 8 2 9 2 10 2 11 2 12 2 13 2 14 2 15 2 16 2 17 2 18 2 19 2 20 2 21 2 22 2 23 2 24 2 25 3 4 3 5 3 6 3 7 3 8 3 9 3 10 3 11 3 12 3 13 3 14 3 15 3 16 3 17 3 18 3 19 3 20 3 21 3 22 3 23 3 24 3 25 4 5 4 6 4 7 4 8 4 9 4 10 4 11 4 12 4 13 4 14 4 15 4 16 4 17 4 18 4 19 4 20 4 21 4 22 4 23 4 24 4 25 5 6 5 7 5 8 5 9 5 10 5 11 5 12 5 13 5 14 5 15 5 16 5 17 5 18 5 19 5 20 5 21 5 22 5 23 5 24 5 25 6 7 6 8 6 9 6 10 6 11 6 12 6 13 6 14 6 15 6 16 6 17 6 18 6 19 6 20 6 21 6 22 6 23 6 24 6 25 7 8 7 9 7 10 7 11 7 12 7 13 7 14 7 15 7 16 7 17 7 18 7 19 7 20 7 21 7 22 7 23 7 24 7 25 8 \n",
"4 1 \n",
"1 \n1 \n1 \n1 \n7 1 \n",
"3 \n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
using namespace std;
long long binpow(long long base, long long exp, int mod) {
long long res = 1;
while (exp > 0) {
if (exp % 2 == 1) res = (res * base) % mod;
exp = exp >> 1;
base = (base * base) % mod;
}
return res;
}
long long mod(long long x) {
return ((x % 1000000007LL + 1000000007LL) % 1000000007LL);
}
long long add(long long a, long long b) { return mod(mod(a) + mod(b)); }
long long mul(long long a, long long b) { return mod(mod(a) * mod(b)); }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
long long int t;
cin >> t;
while (t--) {
long long int n, l, r;
cin >> n >> l >> r;
long long int check[n];
check[0] = 2 * (n - 1);
long long int temp = n - 1;
for (int i = 1; i < n; i++) {
temp--;
if (i == n - 1) {
check[i] = check[i - 1] + 1;
} else {
check[i] = check[i - 1] + 2 * temp;
}
}
long long int val, offset;
for (int i = 0; i < n; i++) {
if (check[i] >= l) {
val = i + 1;
if (i == 0) {
offset = val + l / 2 + l % 2;
} else {
offset = val + (l - check[i - 1]) / 2 + (l - check[i - 1]) % 2;
}
break;
}
}
long long int ans[r - l + 1];
long long int y = l;
if (val == n) {
cout << 1 << "\n";
} else {
for (; l <= r; l++) {
if (l % 2 == 1) {
cout << val << " ";
} else {
cout << offset << " ";
if (offset == n) {
val++;
offset = val + 1;
} else {
offset++;
}
if (val == n) {
break;
}
}
}
if (r == ((n) * (n - 1) + 1)) {
cout << 1;
}
cout << "\n";
}
}
return 0;
}
|
1334_D. Minimum Euler Cycle
|
You are given a complete directed graph K_n with n vertices: each pair of vertices u ≠ v in K_n have both directed edges (u, v) and (v, u); there are no self-loops.
You should find such a cycle in K_n that visits every directed edge exactly once (allowing for revisiting vertices).
We can write such cycle as a list of n(n - 1) + 1 vertices v_1, v_2, v_3, ..., v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1 — a visiting order, where each (v_i, v_{i + 1}) occurs exactly once.
Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists.
Since the answer can be too large print its [l, r] segment, in other words, v_l, v_{l + 1}, ..., v_r.
Input
The first line contains the single integer T (1 ≤ T ≤ 100) — the number of test cases.
Next T lines contain test cases — one per line. The first and only line of each test case contains three integers n, l and r (2 ≤ n ≤ 10^5, 1 ≤ l ≤ r ≤ n(n - 1) + 1, r - l + 1 ≤ 10^5) — the number of vertices in K_n, and segment of the cycle to print.
It's guaranteed that the total sum of n doesn't exceed 10^5 and the total sum of r - l + 1 doesn't exceed 10^5.
Output
For each test case print the segment v_l, v_{l + 1}, ..., v_r of the lexicographically smallest cycle that visits every edge exactly once.
Example
Input
3
2 1 3
3 3 6
99995 9998900031 9998900031
Output
1 2 1
1 3 2 3
1
Note
In the second test case, the lexicographically minimum cycle looks like: 1, 2, 1, 3, 2, 3, 1.
In the third test case, it's quite obvious that the cycle should start and end in vertex 1.
|
{
"input": [
"3\n2 1 3\n3 3 6\n99995 9998900031 9998900031\n"
],
"output": [
"1 2 1 \n1 3 2 3 \n1 \n"
]
}
|
{
"input": [
"1\n2 2 3\n",
"1\n4 13 13\n",
"1\n3 1 1\n",
"10\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n",
"1\n3 7 7\n",
"1\n25 30 295\n",
"1\n4 12 13\n",
"5\n3 7 7\n4 13 13\n5 21 21\n6 31 31\n7 42 43\n",
"1\n5 4 4\n"
],
"output": [
"2 1 \n",
"1 \n",
"1 \n",
"1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n",
"1 \n",
"16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 2 3 2 4 2 5 2 6 2 7 2 8 2 9 2 10 2 11 2 12 2 13 2 14 2 15 2 16 2 17 2 18 2 19 2 20 2 21 2 22 2 23 2 24 2 25 3 4 3 5 3 6 3 7 3 8 3 9 3 10 3 11 3 12 3 13 3 14 3 15 3 16 3 17 3 18 3 19 3 20 3 21 3 22 3 23 3 24 3 25 4 5 4 6 4 7 4 8 4 9 4 10 4 11 4 12 4 13 4 14 4 15 4 16 4 17 4 18 4 19 4 20 4 21 4 22 4 23 4 24 4 25 5 6 5 7 5 8 5 9 5 10 5 11 5 12 5 13 5 14 5 15 5 16 5 17 5 18 5 19 5 20 5 21 5 22 5 23 5 24 5 25 6 7 6 8 6 9 6 10 6 11 6 12 6 13 6 14 6 15 6 16 6 17 6 18 6 19 6 20 6 21 6 22 6 23 6 24 6 25 7 8 7 9 7 10 7 11 7 12 7 13 7 14 7 15 7 16 7 17 7 18 7 19 7 20 7 21 7 22 7 23 7 24 7 25 8 \n",
"4 1 \n",
"1 \n1 \n1 \n1 \n7 1 \n",
"3 \n"
]
}
|
CORRECT
|
python3
|
for nt in range(int(input())):
n,a,b=map(int,input().split())
if n==2:
l=[1,2,1]
print (*l[a-1:b])
continue
k=n
prev=0
for j in range(a,b+1):
i=j-prev
while k>1:
if i<=2*(k-1):
if i%2:
print (n-k+1,end=" ")
else:
print (i//2+(n-k+1),end=" ")
break
else:
i-=2*(k-1)
prev+=2*(k-1)
k-=1
if k==1:
print (1,end=" ")
print ()
|
1334_D. Minimum Euler Cycle
|
You are given a complete directed graph K_n with n vertices: each pair of vertices u ≠ v in K_n have both directed edges (u, v) and (v, u); there are no self-loops.
You should find such a cycle in K_n that visits every directed edge exactly once (allowing for revisiting vertices).
We can write such cycle as a list of n(n - 1) + 1 vertices v_1, v_2, v_3, ..., v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1 — a visiting order, where each (v_i, v_{i + 1}) occurs exactly once.
Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists.
Since the answer can be too large print its [l, r] segment, in other words, v_l, v_{l + 1}, ..., v_r.
Input
The first line contains the single integer T (1 ≤ T ≤ 100) — the number of test cases.
Next T lines contain test cases — one per line. The first and only line of each test case contains three integers n, l and r (2 ≤ n ≤ 10^5, 1 ≤ l ≤ r ≤ n(n - 1) + 1, r - l + 1 ≤ 10^5) — the number of vertices in K_n, and segment of the cycle to print.
It's guaranteed that the total sum of n doesn't exceed 10^5 and the total sum of r - l + 1 doesn't exceed 10^5.
Output
For each test case print the segment v_l, v_{l + 1}, ..., v_r of the lexicographically smallest cycle that visits every edge exactly once.
Example
Input
3
2 1 3
3 3 6
99995 9998900031 9998900031
Output
1 2 1
1 3 2 3
1
Note
In the second test case, the lexicographically minimum cycle looks like: 1, 2, 1, 3, 2, 3, 1.
In the third test case, it's quite obvious that the cycle should start and end in vertex 1.
|
{
"input": [
"3\n2 1 3\n3 3 6\n99995 9998900031 9998900031\n"
],
"output": [
"1 2 1 \n1 3 2 3 \n1 \n"
]
}
|
{
"input": [
"1\n2 2 3\n",
"1\n4 13 13\n",
"1\n3 1 1\n",
"10\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n",
"1\n3 7 7\n",
"1\n25 30 295\n",
"1\n4 12 13\n",
"5\n3 7 7\n4 13 13\n5 21 21\n6 31 31\n7 42 43\n",
"1\n5 4 4\n"
],
"output": [
"2 1 \n",
"1 \n",
"1 \n",
"1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n",
"1 \n",
"16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 2 3 2 4 2 5 2 6 2 7 2 8 2 9 2 10 2 11 2 12 2 13 2 14 2 15 2 16 2 17 2 18 2 19 2 20 2 21 2 22 2 23 2 24 2 25 3 4 3 5 3 6 3 7 3 8 3 9 3 10 3 11 3 12 3 13 3 14 3 15 3 16 3 17 3 18 3 19 3 20 3 21 3 22 3 23 3 24 3 25 4 5 4 6 4 7 4 8 4 9 4 10 4 11 4 12 4 13 4 14 4 15 4 16 4 17 4 18 4 19 4 20 4 21 4 22 4 23 4 24 4 25 5 6 5 7 5 8 5 9 5 10 5 11 5 12 5 13 5 14 5 15 5 16 5 17 5 18 5 19 5 20 5 21 5 22 5 23 5 24 5 25 6 7 6 8 6 9 6 10 6 11 6 12 6 13 6 14 6 15 6 16 6 17 6 18 6 19 6 20 6 21 6 22 6 23 6 24 6 25 7 8 7 9 7 10 7 11 7 12 7 13 7 14 7 15 7 16 7 17 7 18 7 19 7 20 7 21 7 22 7 23 7 24 7 25 8 \n",
"4 1 \n",
"1 \n1 \n1 \n1 \n7 1 \n",
"3 \n"
]
}
|
CORRECT
|
python3
|
import sys
input = sys.stdin.buffer.readline
from bisect import bisect_left
Q = int(input())
Query = [list(map(int, input().split())) for _ in range(Q)]
B = [0]
for i in range(1, 2*10**5):
B.append(i*(i+1)//2)
def solve(n, N):
M = N*(N-1)
n %= M
if n%2 == 0:
rem = (M - n)//2
ind = bisect_left(B, rem)
return N-ind
else:
rem = (M - (n-1))//2
ind = bisect_left(B, rem)
seq = rem - ind*(ind-1)//2
return N - seq + 1
for N, L, R in Query:
ans = []
for n in range(L-1, R):
ans.append(str(solve(n, N)))
print(" ".join(ans))
|
1334_D. Minimum Euler Cycle
|
You are given a complete directed graph K_n with n vertices: each pair of vertices u ≠ v in K_n have both directed edges (u, v) and (v, u); there are no self-loops.
You should find such a cycle in K_n that visits every directed edge exactly once (allowing for revisiting vertices).
We can write such cycle as a list of n(n - 1) + 1 vertices v_1, v_2, v_3, ..., v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1 — a visiting order, where each (v_i, v_{i + 1}) occurs exactly once.
Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists.
Since the answer can be too large print its [l, r] segment, in other words, v_l, v_{l + 1}, ..., v_r.
Input
The first line contains the single integer T (1 ≤ T ≤ 100) — the number of test cases.
Next T lines contain test cases — one per line. The first and only line of each test case contains three integers n, l and r (2 ≤ n ≤ 10^5, 1 ≤ l ≤ r ≤ n(n - 1) + 1, r - l + 1 ≤ 10^5) — the number of vertices in K_n, and segment of the cycle to print.
It's guaranteed that the total sum of n doesn't exceed 10^5 and the total sum of r - l + 1 doesn't exceed 10^5.
Output
For each test case print the segment v_l, v_{l + 1}, ..., v_r of the lexicographically smallest cycle that visits every edge exactly once.
Example
Input
3
2 1 3
3 3 6
99995 9998900031 9998900031
Output
1 2 1
1 3 2 3
1
Note
In the second test case, the lexicographically minimum cycle looks like: 1, 2, 1, 3, 2, 3, 1.
In the third test case, it's quite obvious that the cycle should start and end in vertex 1.
|
{
"input": [
"3\n2 1 3\n3 3 6\n99995 9998900031 9998900031\n"
],
"output": [
"1 2 1 \n1 3 2 3 \n1 \n"
]
}
|
{
"input": [
"1\n2 2 3\n",
"1\n4 13 13\n",
"1\n3 1 1\n",
"10\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n",
"1\n3 7 7\n",
"1\n25 30 295\n",
"1\n4 12 13\n",
"5\n3 7 7\n4 13 13\n5 21 21\n6 31 31\n7 42 43\n",
"1\n5 4 4\n"
],
"output": [
"2 1 \n",
"1 \n",
"1 \n",
"1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n",
"1 \n",
"16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 2 3 2 4 2 5 2 6 2 7 2 8 2 9 2 10 2 11 2 12 2 13 2 14 2 15 2 16 2 17 2 18 2 19 2 20 2 21 2 22 2 23 2 24 2 25 3 4 3 5 3 6 3 7 3 8 3 9 3 10 3 11 3 12 3 13 3 14 3 15 3 16 3 17 3 18 3 19 3 20 3 21 3 22 3 23 3 24 3 25 4 5 4 6 4 7 4 8 4 9 4 10 4 11 4 12 4 13 4 14 4 15 4 16 4 17 4 18 4 19 4 20 4 21 4 22 4 23 4 24 4 25 5 6 5 7 5 8 5 9 5 10 5 11 5 12 5 13 5 14 5 15 5 16 5 17 5 18 5 19 5 20 5 21 5 22 5 23 5 24 5 25 6 7 6 8 6 9 6 10 6 11 6 12 6 13 6 14 6 15 6 16 6 17 6 18 6 19 6 20 6 21 6 22 6 23 6 24 6 25 7 8 7 9 7 10 7 11 7 12 7 13 7 14 7 15 7 16 7 17 7 18 7 19 7 20 7 21 7 22 7 23 7 24 7 25 8 \n",
"4 1 \n",
"1 \n1 \n1 \n1 \n7 1 \n",
"3 \n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) {
long long n, l, r;
cin >> n >> l >> r;
vector<long long> pre(n + 1);
for (int i = 1; i <= n; i++) {
pre[i] = pre[i - 1] + 2 * (n - i);
}
pre[n]++;
for (long long i = l; i <= r; i++) {
int pos = lower_bound(pre.begin(), pre.end(), i) - pre.begin();
if (pos == n)
cout << "1"
<< " ";
else {
long long j = i - pre[pos - 1];
if (j % 2 == 1)
cout << pos << " ";
else
cout << (j / 2 + pos) << " ";
}
}
cout << endl;
}
return 0;
}
|
1334_D. Minimum Euler Cycle
|
You are given a complete directed graph K_n with n vertices: each pair of vertices u ≠ v in K_n have both directed edges (u, v) and (v, u); there are no self-loops.
You should find such a cycle in K_n that visits every directed edge exactly once (allowing for revisiting vertices).
We can write such cycle as a list of n(n - 1) + 1 vertices v_1, v_2, v_3, ..., v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1 — a visiting order, where each (v_i, v_{i + 1}) occurs exactly once.
Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists.
Since the answer can be too large print its [l, r] segment, in other words, v_l, v_{l + 1}, ..., v_r.
Input
The first line contains the single integer T (1 ≤ T ≤ 100) — the number of test cases.
Next T lines contain test cases — one per line. The first and only line of each test case contains three integers n, l and r (2 ≤ n ≤ 10^5, 1 ≤ l ≤ r ≤ n(n - 1) + 1, r - l + 1 ≤ 10^5) — the number of vertices in K_n, and segment of the cycle to print.
It's guaranteed that the total sum of n doesn't exceed 10^5 and the total sum of r - l + 1 doesn't exceed 10^5.
Output
For each test case print the segment v_l, v_{l + 1}, ..., v_r of the lexicographically smallest cycle that visits every edge exactly once.
Example
Input
3
2 1 3
3 3 6
99995 9998900031 9998900031
Output
1 2 1
1 3 2 3
1
Note
In the second test case, the lexicographically minimum cycle looks like: 1, 2, 1, 3, 2, 3, 1.
In the third test case, it's quite obvious that the cycle should start and end in vertex 1.
|
{
"input": [
"3\n2 1 3\n3 3 6\n99995 9998900031 9998900031\n"
],
"output": [
"1 2 1 \n1 3 2 3 \n1 \n"
]
}
|
{
"input": [
"1\n2 2 3\n",
"1\n4 13 13\n",
"1\n3 1 1\n",
"10\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n",
"1\n3 7 7\n",
"1\n25 30 295\n",
"1\n4 12 13\n",
"5\n3 7 7\n4 13 13\n5 21 21\n6 31 31\n7 42 43\n",
"1\n5 4 4\n"
],
"output": [
"2 1 \n",
"1 \n",
"1 \n",
"1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n",
"1 \n",
"16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 2 3 2 4 2 5 2 6 2 7 2 8 2 9 2 10 2 11 2 12 2 13 2 14 2 15 2 16 2 17 2 18 2 19 2 20 2 21 2 22 2 23 2 24 2 25 3 4 3 5 3 6 3 7 3 8 3 9 3 10 3 11 3 12 3 13 3 14 3 15 3 16 3 17 3 18 3 19 3 20 3 21 3 22 3 23 3 24 3 25 4 5 4 6 4 7 4 8 4 9 4 10 4 11 4 12 4 13 4 14 4 15 4 16 4 17 4 18 4 19 4 20 4 21 4 22 4 23 4 24 4 25 5 6 5 7 5 8 5 9 5 10 5 11 5 12 5 13 5 14 5 15 5 16 5 17 5 18 5 19 5 20 5 21 5 22 5 23 5 24 5 25 6 7 6 8 6 9 6 10 6 11 6 12 6 13 6 14 6 15 6 16 6 17 6 18 6 19 6 20 6 21 6 22 6 23 6 24 6 25 7 8 7 9 7 10 7 11 7 12 7 13 7 14 7 15 7 16 7 17 7 18 7 19 7 20 7 21 7 22 7 23 7 24 7 25 8 \n",
"4 1 \n",
"1 \n1 \n1 \n1 \n7 1 \n",
"3 \n"
]
}
|
CORRECT
|
java
|
import java.util.*;
import java.io.*;
public class Solution implements Runnable{
FastScanner sc;
PrintWriter pw;
final class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner() {
try {
br = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
e.printStackTrace();
}
}
public long nlo() {
return Long.parseLong(next());
}
public String next() {
if (st.hasMoreTokens()) return st.nextToken();
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
e.printStackTrace();
}
return st.nextToken();
}
public int ni() {
return Integer.parseInt(next());
}
public String nli() {
String line = "";
if (st.hasMoreTokens()) line = st.nextToken();
else try {
return br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
while (st.hasMoreTokens()) line += " " + st.nextToken();
return line;
}
public double nd() {
return Double.parseDouble(next());
}
}
public static void main(String[] args)
{
new Thread(null,new Solution(),"codeforces",1<<25).start();
}
public void run()
{
sc=new FastScanner();
pw=new PrintWriter(System.out);
solve();
pw.flush();
pw.close();
}
public long gcd(long a,long b)
{
return b==0L?a:gcd(b,a%b);
}
public long ppow(long a,long b,long mod)
{
if(b==0L)
return 1L;
long tmp=1;
while(b>1L)
{
if((b&1L)==1)
tmp*=a;
a*=a;
a%=mod;
tmp%=mod;
b>>=1;
}
return (tmp*a)%mod;
}
public int gcd(int x,int y)
{
return y==0?x:gcd(y,x%y);
}
//////////////////////////////////
///////////// LOGIC ///////////
////////////////////////////////
public void solve(){
int t=sc.ni();
while(t-->0)
{
long n=sc.nlo();
long l=sc.nlo();
long r=sc.nlo();
long tt=r-l+1L;
StringBuilder str=new StringBuilder();
for(long i=1;i<n;i++)
{
l-=(n-i)*2L;
if(l>0)
continue;
l+=(n-i)*2L;
if(l%2==0){
long j=l/2+i;
str.append(j+" ");
tt--;}
tt=dop(i,l/2+1,n,tt,str);
break;
}
if(tt>0)
str.append(1);
pw.println(str);
}
}
public long dop(long i,long p,long n,long t,StringBuilder str)
{
//pw.println(i+" "+p+" "+t);
for(;i<n;i++)
{
for(long j=i+p;j<=n;j++)
{
if(t>=2)
{str.append(i+" "+j+" ");t-=2L;}
else if(t==1)
{
str.append(i+" ");
t-=1;
}
if(t==0)
break;
}
p=1;
if(t==0)
break;
}
if(t==1)
{t--; str.append(1);}
return t;
}
}
|
1334_D. Minimum Euler Cycle
|
You are given a complete directed graph K_n with n vertices: each pair of vertices u ≠ v in K_n have both directed edges (u, v) and (v, u); there are no self-loops.
You should find such a cycle in K_n that visits every directed edge exactly once (allowing for revisiting vertices).
We can write such cycle as a list of n(n - 1) + 1 vertices v_1, v_2, v_3, ..., v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1 — a visiting order, where each (v_i, v_{i + 1}) occurs exactly once.
Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists.
Since the answer can be too large print its [l, r] segment, in other words, v_l, v_{l + 1}, ..., v_r.
Input
The first line contains the single integer T (1 ≤ T ≤ 100) — the number of test cases.
Next T lines contain test cases — one per line. The first and only line of each test case contains three integers n, l and r (2 ≤ n ≤ 10^5, 1 ≤ l ≤ r ≤ n(n - 1) + 1, r - l + 1 ≤ 10^5) — the number of vertices in K_n, and segment of the cycle to print.
It's guaranteed that the total sum of n doesn't exceed 10^5 and the total sum of r - l + 1 doesn't exceed 10^5.
Output
For each test case print the segment v_l, v_{l + 1}, ..., v_r of the lexicographically smallest cycle that visits every edge exactly once.
Example
Input
3
2 1 3
3 3 6
99995 9998900031 9998900031
Output
1 2 1
1 3 2 3
1
Note
In the second test case, the lexicographically minimum cycle looks like: 1, 2, 1, 3, 2, 3, 1.
In the third test case, it's quite obvious that the cycle should start and end in vertex 1.
|
{
"input": [
"3\n2 1 3\n3 3 6\n99995 9998900031 9998900031\n"
],
"output": [
"1 2 1 \n1 3 2 3 \n1 \n"
]
}
|
{
"input": [
"1\n2 2 3\n",
"1\n4 13 13\n",
"1\n3 1 1\n",
"10\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n",
"1\n3 7 7\n",
"1\n25 30 295\n",
"1\n4 12 13\n",
"5\n3 7 7\n4 13 13\n5 21 21\n6 31 31\n7 42 43\n",
"1\n5 4 4\n"
],
"output": [
"2 1 \n",
"1 \n",
"1 \n",
"1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n",
"1 \n",
"16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 2 3 2 4 2 5 2 6 2 7 2 8 2 9 2 10 2 11 2 12 2 13 2 14 2 15 2 16 2 17 2 18 2 19 2 20 2 21 2 22 2 23 2 24 2 25 3 4 3 5 3 6 3 7 3 8 3 9 3 10 3 11 3 12 3 13 3 14 3 15 3 16 3 17 3 18 3 19 3 20 3 21 3 22 3 23 3 24 3 25 4 5 4 6 4 7 4 8 4 9 4 10 4 11 4 12 4 13 4 14 4 15 4 16 4 17 4 18 4 19 4 20 4 21 4 22 4 23 4 24 4 25 5 6 5 7 5 8 5 9 5 10 5 11 5 12 5 13 5 14 5 15 5 16 5 17 5 18 5 19 5 20 5 21 5 22 5 23 5 24 5 25 6 7 6 8 6 9 6 10 6 11 6 12 6 13 6 14 6 15 6 16 6 17 6 18 6 19 6 20 6 21 6 22 6 23 6 24 6 25 7 8 7 9 7 10 7 11 7 12 7 13 7 14 7 15 7 16 7 17 7 18 7 19 7 20 7 21 7 22 7 23 7 24 7 25 8 \n",
"4 1 \n",
"1 \n1 \n1 \n1 \n7 1 \n",
"3 \n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
long long n, l, r;
cin >> n >> l >> r;
long long s = 0;
long long i;
for (i = 1; i <= n - 1; i++) {
s += 2 * (n - i);
if (l <= s) {
s = s - 2 * (n - i);
break;
}
}
long long j, x = i;
if (x == n) x = 1;
if ((l - s) % 2 == 1) {
j = i + (l + 1 - s) / 2;
int f = 0;
for (long long v = l; v <= r; v++) {
if (f == 0) {
cout << x << " ";
f = 1;
} else {
cout << j << " ";
j++;
f = 0;
if (j == n + 1) {
x++;
if (x == n) x = 1;
j = x + 1;
}
}
}
} else {
j = i + (l - s) / 2;
int f = 1;
for (long long v = l; v <= r; v++) {
if (f == 0) {
cout << x << " ";
f = 1;
} else {
cout << j << " ";
j++;
f = 0;
if (j == n + 1) {
x++;
if (x == n) x = 1;
j = x + 1;
}
}
}
}
cout << endl;
}
return 0;
}
|
1334_D. Minimum Euler Cycle
|
You are given a complete directed graph K_n with n vertices: each pair of vertices u ≠ v in K_n have both directed edges (u, v) and (v, u); there are no self-loops.
You should find such a cycle in K_n that visits every directed edge exactly once (allowing for revisiting vertices).
We can write such cycle as a list of n(n - 1) + 1 vertices v_1, v_2, v_3, ..., v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1 — a visiting order, where each (v_i, v_{i + 1}) occurs exactly once.
Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists.
Since the answer can be too large print its [l, r] segment, in other words, v_l, v_{l + 1}, ..., v_r.
Input
The first line contains the single integer T (1 ≤ T ≤ 100) — the number of test cases.
Next T lines contain test cases — one per line. The first and only line of each test case contains three integers n, l and r (2 ≤ n ≤ 10^5, 1 ≤ l ≤ r ≤ n(n - 1) + 1, r - l + 1 ≤ 10^5) — the number of vertices in K_n, and segment of the cycle to print.
It's guaranteed that the total sum of n doesn't exceed 10^5 and the total sum of r - l + 1 doesn't exceed 10^5.
Output
For each test case print the segment v_l, v_{l + 1}, ..., v_r of the lexicographically smallest cycle that visits every edge exactly once.
Example
Input
3
2 1 3
3 3 6
99995 9998900031 9998900031
Output
1 2 1
1 3 2 3
1
Note
In the second test case, the lexicographically minimum cycle looks like: 1, 2, 1, 3, 2, 3, 1.
In the third test case, it's quite obvious that the cycle should start and end in vertex 1.
|
{
"input": [
"3\n2 1 3\n3 3 6\n99995 9998900031 9998900031\n"
],
"output": [
"1 2 1 \n1 3 2 3 \n1 \n"
]
}
|
{
"input": [
"1\n2 2 3\n",
"1\n4 13 13\n",
"1\n3 1 1\n",
"10\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n",
"1\n3 7 7\n",
"1\n25 30 295\n",
"1\n4 12 13\n",
"5\n3 7 7\n4 13 13\n5 21 21\n6 31 31\n7 42 43\n",
"1\n5 4 4\n"
],
"output": [
"2 1 \n",
"1 \n",
"1 \n",
"1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n",
"1 \n",
"16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 2 3 2 4 2 5 2 6 2 7 2 8 2 9 2 10 2 11 2 12 2 13 2 14 2 15 2 16 2 17 2 18 2 19 2 20 2 21 2 22 2 23 2 24 2 25 3 4 3 5 3 6 3 7 3 8 3 9 3 10 3 11 3 12 3 13 3 14 3 15 3 16 3 17 3 18 3 19 3 20 3 21 3 22 3 23 3 24 3 25 4 5 4 6 4 7 4 8 4 9 4 10 4 11 4 12 4 13 4 14 4 15 4 16 4 17 4 18 4 19 4 20 4 21 4 22 4 23 4 24 4 25 5 6 5 7 5 8 5 9 5 10 5 11 5 12 5 13 5 14 5 15 5 16 5 17 5 18 5 19 5 20 5 21 5 22 5 23 5 24 5 25 6 7 6 8 6 9 6 10 6 11 6 12 6 13 6 14 6 15 6 16 6 17 6 18 6 19 6 20 6 21 6 22 6 23 6 24 6 25 7 8 7 9 7 10 7 11 7 12 7 13 7 14 7 15 7 16 7 17 7 18 7 19 7 20 7 21 7 22 7 23 7 24 7 25 8 \n",
"4 1 \n",
"1 \n1 \n1 \n1 \n7 1 \n",
"3 \n"
]
}
|
CORRECT
|
java
|
import java.util.Scanner;
public class ProblemD {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s = new Scanner(System.in);
int t = s.nextInt();
for(int a=0;a<t;a++) {
int n = s.nextInt();
long l = s.nextLong();
long r = s.nextLong();
long[] arr = new long[n];
long sum = 0;
long val = 2*(n-1);
for(int i=0;i<n-1;i++) {
sum += val;
val -= 2;
arr[i] = sum;
}
arr[n-1] = arr[n-2] + 1;
// for(int i=0;i<n;i++)
// System.out.println(arr[i]);
int index = upperBound(arr, l);
//System.out.println(index);
print(arr, l, r, index);
System.out.println();
}
}
public static void print(long[] arr, long l, long r, int index) {
int n = arr.length;
if(index == n-1) {
System.out.print(1+" ");
return;
}
long val1 = index + 1, val2 = 0;
long end = arr[index];
if(l%2 == 0) {
val2 = n - (end-l)/2;
}
else {
val2 = n - (end-l-1)/2;
}
for(long i=l;l <= Math.min(end, r);l++) {
if(l%2 == 1)
System.out.print(val1+" ");
else {
System.out.print(val2+" ");
val2++;
}
}
if(end < r)
print(arr, end + 1, r, index + 1);
}
public static int upperBound(long[] arr, long v) {
if(v > arr[arr.length-1])
return -1;
if(arr[0] > v)
return 0;
int low = 0, high = arr.length - 1;
while(low < high) {
if(low == high - 1) {
if(arr[low] >= v)
high = low;
else
low = high;
break;
}
int mid = (low + high)/2;
if(arr[mid] >= v)
high = mid;
else
low = mid + 1;
}
return low;
}
}
|
1334_D. Minimum Euler Cycle
|
You are given a complete directed graph K_n with n vertices: each pair of vertices u ≠ v in K_n have both directed edges (u, v) and (v, u); there are no self-loops.
You should find such a cycle in K_n that visits every directed edge exactly once (allowing for revisiting vertices).
We can write such cycle as a list of n(n - 1) + 1 vertices v_1, v_2, v_3, ..., v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1 — a visiting order, where each (v_i, v_{i + 1}) occurs exactly once.
Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists.
Since the answer can be too large print its [l, r] segment, in other words, v_l, v_{l + 1}, ..., v_r.
Input
The first line contains the single integer T (1 ≤ T ≤ 100) — the number of test cases.
Next T lines contain test cases — one per line. The first and only line of each test case contains three integers n, l and r (2 ≤ n ≤ 10^5, 1 ≤ l ≤ r ≤ n(n - 1) + 1, r - l + 1 ≤ 10^5) — the number of vertices in K_n, and segment of the cycle to print.
It's guaranteed that the total sum of n doesn't exceed 10^5 and the total sum of r - l + 1 doesn't exceed 10^5.
Output
For each test case print the segment v_l, v_{l + 1}, ..., v_r of the lexicographically smallest cycle that visits every edge exactly once.
Example
Input
3
2 1 3
3 3 6
99995 9998900031 9998900031
Output
1 2 1
1 3 2 3
1
Note
In the second test case, the lexicographically minimum cycle looks like: 1, 2, 1, 3, 2, 3, 1.
In the third test case, it's quite obvious that the cycle should start and end in vertex 1.
|
{
"input": [
"3\n2 1 3\n3 3 6\n99995 9998900031 9998900031\n"
],
"output": [
"1 2 1 \n1 3 2 3 \n1 \n"
]
}
|
{
"input": [
"1\n2 2 3\n",
"1\n4 13 13\n",
"1\n3 1 1\n",
"10\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n",
"1\n3 7 7\n",
"1\n25 30 295\n",
"1\n4 12 13\n",
"5\n3 7 7\n4 13 13\n5 21 21\n6 31 31\n7 42 43\n",
"1\n5 4 4\n"
],
"output": [
"2 1 \n",
"1 \n",
"1 \n",
"1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n",
"1 \n",
"16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 2 3 2 4 2 5 2 6 2 7 2 8 2 9 2 10 2 11 2 12 2 13 2 14 2 15 2 16 2 17 2 18 2 19 2 20 2 21 2 22 2 23 2 24 2 25 3 4 3 5 3 6 3 7 3 8 3 9 3 10 3 11 3 12 3 13 3 14 3 15 3 16 3 17 3 18 3 19 3 20 3 21 3 22 3 23 3 24 3 25 4 5 4 6 4 7 4 8 4 9 4 10 4 11 4 12 4 13 4 14 4 15 4 16 4 17 4 18 4 19 4 20 4 21 4 22 4 23 4 24 4 25 5 6 5 7 5 8 5 9 5 10 5 11 5 12 5 13 5 14 5 15 5 16 5 17 5 18 5 19 5 20 5 21 5 22 5 23 5 24 5 25 6 7 6 8 6 9 6 10 6 11 6 12 6 13 6 14 6 15 6 16 6 17 6 18 6 19 6 20 6 21 6 22 6 23 6 24 6 25 7 8 7 9 7 10 7 11 7 12 7 13 7 14 7 15 7 16 7 17 7 18 7 19 7 20 7 21 7 22 7 23 7 24 7 25 8 \n",
"4 1 \n",
"1 \n1 \n1 \n1 \n7 1 \n",
"3 \n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long n, l, r;
cin >> n >> l >> r;
if (l == n * (n - 1LL) + 1LL) {
cout << 1 << '\n';
return;
}
long long suma = 0LL;
long long trenutni = (long long)n - 1LL;
while (trenutni > 0) {
if (suma + 2 * trenutni >= l) break;
suma += 2 * trenutni;
--trenutni;
}
trenutni = (n - trenutni);
long long sledeci = trenutni;
for (long long i = suma + 1; i <= r; ++i) {
if (i == n * (n - 1) + 1) {
cout << 1 << ' ';
cout << '\n';
return;
}
if (i % 2 == 1) {
++sledeci;
}
if (i >= l) {
if (i % 2)
cout << trenutni << ' ';
else
cout << sledeci << ' ';
}
if (sledeci == n && i % 2 == 0) sledeci = ++trenutni;
}
cout << '\n';
}
int main() {
int t;
cin >> t;
while (t--) solve();
return 0;
}
|
1334_D. Minimum Euler Cycle
|
You are given a complete directed graph K_n with n vertices: each pair of vertices u ≠ v in K_n have both directed edges (u, v) and (v, u); there are no self-loops.
You should find such a cycle in K_n that visits every directed edge exactly once (allowing for revisiting vertices).
We can write such cycle as a list of n(n - 1) + 1 vertices v_1, v_2, v_3, ..., v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1 — a visiting order, where each (v_i, v_{i + 1}) occurs exactly once.
Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists.
Since the answer can be too large print its [l, r] segment, in other words, v_l, v_{l + 1}, ..., v_r.
Input
The first line contains the single integer T (1 ≤ T ≤ 100) — the number of test cases.
Next T lines contain test cases — one per line. The first and only line of each test case contains three integers n, l and r (2 ≤ n ≤ 10^5, 1 ≤ l ≤ r ≤ n(n - 1) + 1, r - l + 1 ≤ 10^5) — the number of vertices in K_n, and segment of the cycle to print.
It's guaranteed that the total sum of n doesn't exceed 10^5 and the total sum of r - l + 1 doesn't exceed 10^5.
Output
For each test case print the segment v_l, v_{l + 1}, ..., v_r of the lexicographically smallest cycle that visits every edge exactly once.
Example
Input
3
2 1 3
3 3 6
99995 9998900031 9998900031
Output
1 2 1
1 3 2 3
1
Note
In the second test case, the lexicographically minimum cycle looks like: 1, 2, 1, 3, 2, 3, 1.
In the third test case, it's quite obvious that the cycle should start and end in vertex 1.
|
{
"input": [
"3\n2 1 3\n3 3 6\n99995 9998900031 9998900031\n"
],
"output": [
"1 2 1 \n1 3 2 3 \n1 \n"
]
}
|
{
"input": [
"1\n2 2 3\n",
"1\n4 13 13\n",
"1\n3 1 1\n",
"10\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n",
"1\n3 7 7\n",
"1\n25 30 295\n",
"1\n4 12 13\n",
"5\n3 7 7\n4 13 13\n5 21 21\n6 31 31\n7 42 43\n",
"1\n5 4 4\n"
],
"output": [
"2 1 \n",
"1 \n",
"1 \n",
"1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n",
"1 \n",
"16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 2 3 2 4 2 5 2 6 2 7 2 8 2 9 2 10 2 11 2 12 2 13 2 14 2 15 2 16 2 17 2 18 2 19 2 20 2 21 2 22 2 23 2 24 2 25 3 4 3 5 3 6 3 7 3 8 3 9 3 10 3 11 3 12 3 13 3 14 3 15 3 16 3 17 3 18 3 19 3 20 3 21 3 22 3 23 3 24 3 25 4 5 4 6 4 7 4 8 4 9 4 10 4 11 4 12 4 13 4 14 4 15 4 16 4 17 4 18 4 19 4 20 4 21 4 22 4 23 4 24 4 25 5 6 5 7 5 8 5 9 5 10 5 11 5 12 5 13 5 14 5 15 5 16 5 17 5 18 5 19 5 20 5 21 5 22 5 23 5 24 5 25 6 7 6 8 6 9 6 10 6 11 6 12 6 13 6 14 6 15 6 16 6 17 6 18 6 19 6 20 6 21 6 22 6 23 6 24 6 25 7 8 7 9 7 10 7 11 7 12 7 13 7 14 7 15 7 16 7 17 7 18 7 19 7 20 7 21 7 22 7 23 7 24 7 25 8 \n",
"4 1 \n",
"1 \n1 \n1 \n1 \n7 1 \n",
"3 \n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007, N = 1e5 + 5, M = 1e5 + 5, INF = 0x3f3f3f3f;
long long powmod(long long a, long long b) {
long long res = 1;
a %= mod;
assert(b >= 0);
for (; b; b >>= 1) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
}
return res;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
int main(void) {
ios::sync_with_stdio(false);
cin.tie(0);
int T;
cin >> T;
while (T--) {
long long n, l, r;
cin >> n >> l >> r;
if (l == r && l == n * (n - 1) + 1) {
cout << 1 << endl;
continue;
}
int nowx = 1, nowy = 2;
long long sum = 0;
while (sum < l) {
sum += 2 * (n - nowx);
++nowx;
}
--nowx;
sum -= 2 * (n - nowx);
++sum;
vector<int> ans;
nowy = nowx + 1;
int di = 1;
int cnt = sum;
while (sum <= r) {
if (di)
ans.push_back(nowx);
else {
ans.push_back(nowy);
++nowy;
if (nowy > n) {
nowx++;
nowy = nowx + 1;
if (nowx > n) nowx = 1;
}
}
++sum;
di ^= 1;
}
if (r == n * (n - 1) + 1) ans[ans.size() - 1] = 1;
cout << ans[l - cnt];
for (long long i = l - cnt + 1; i <= r - cnt; ++i) cout << ' ' << ans[i];
cout << endl;
}
return 0;
}
|
1334_D. Minimum Euler Cycle
|
You are given a complete directed graph K_n with n vertices: each pair of vertices u ≠ v in K_n have both directed edges (u, v) and (v, u); there are no self-loops.
You should find such a cycle in K_n that visits every directed edge exactly once (allowing for revisiting vertices).
We can write such cycle as a list of n(n - 1) + 1 vertices v_1, v_2, v_3, ..., v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1 — a visiting order, where each (v_i, v_{i + 1}) occurs exactly once.
Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists.
Since the answer can be too large print its [l, r] segment, in other words, v_l, v_{l + 1}, ..., v_r.
Input
The first line contains the single integer T (1 ≤ T ≤ 100) — the number of test cases.
Next T lines contain test cases — one per line. The first and only line of each test case contains three integers n, l and r (2 ≤ n ≤ 10^5, 1 ≤ l ≤ r ≤ n(n - 1) + 1, r - l + 1 ≤ 10^5) — the number of vertices in K_n, and segment of the cycle to print.
It's guaranteed that the total sum of n doesn't exceed 10^5 and the total sum of r - l + 1 doesn't exceed 10^5.
Output
For each test case print the segment v_l, v_{l + 1}, ..., v_r of the lexicographically smallest cycle that visits every edge exactly once.
Example
Input
3
2 1 3
3 3 6
99995 9998900031 9998900031
Output
1 2 1
1 3 2 3
1
Note
In the second test case, the lexicographically minimum cycle looks like: 1, 2, 1, 3, 2, 3, 1.
In the third test case, it's quite obvious that the cycle should start and end in vertex 1.
|
{
"input": [
"3\n2 1 3\n3 3 6\n99995 9998900031 9998900031\n"
],
"output": [
"1 2 1 \n1 3 2 3 \n1 \n"
]
}
|
{
"input": [
"1\n2 2 3\n",
"1\n4 13 13\n",
"1\n3 1 1\n",
"10\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n",
"1\n3 7 7\n",
"1\n25 30 295\n",
"1\n4 12 13\n",
"5\n3 7 7\n4 13 13\n5 21 21\n6 31 31\n7 42 43\n",
"1\n5 4 4\n"
],
"output": [
"2 1 \n",
"1 \n",
"1 \n",
"1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n",
"1 \n",
"16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 2 3 2 4 2 5 2 6 2 7 2 8 2 9 2 10 2 11 2 12 2 13 2 14 2 15 2 16 2 17 2 18 2 19 2 20 2 21 2 22 2 23 2 24 2 25 3 4 3 5 3 6 3 7 3 8 3 9 3 10 3 11 3 12 3 13 3 14 3 15 3 16 3 17 3 18 3 19 3 20 3 21 3 22 3 23 3 24 3 25 4 5 4 6 4 7 4 8 4 9 4 10 4 11 4 12 4 13 4 14 4 15 4 16 4 17 4 18 4 19 4 20 4 21 4 22 4 23 4 24 4 25 5 6 5 7 5 8 5 9 5 10 5 11 5 12 5 13 5 14 5 15 5 16 5 17 5 18 5 19 5 20 5 21 5 22 5 23 5 24 5 25 6 7 6 8 6 9 6 10 6 11 6 12 6 13 6 14 6 15 6 16 6 17 6 18 6 19 6 20 6 21 6 22 6 23 6 24 6 25 7 8 7 9 7 10 7 11 7 12 7 13 7 14 7 15 7 16 7 17 7 18 7 19 7 20 7 21 7 22 7 23 7 24 7 25 8 \n",
"4 1 \n",
"1 \n1 \n1 \n1 \n7 1 \n",
"3 \n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int maxc = 1e5;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
long long k = 0;
long long n, l, r;
cin >> n >> l >> r;
for (long long i = 2 * (n - 1); i; k += i, i -= 2) {
for (long long j = max(l, k + 1); j <= min(r, k + i); j++) {
if (j % 2)
cout << n - i / 2 << " ";
else
cout << n - i / 2 + (j - k) / 2 << " ";
}
}
if (k + 1 == r) cout << 1;
cout << endl;
}
return 0;
}
|
1334_D. Minimum Euler Cycle
|
You are given a complete directed graph K_n with n vertices: each pair of vertices u ≠ v in K_n have both directed edges (u, v) and (v, u); there are no self-loops.
You should find such a cycle in K_n that visits every directed edge exactly once (allowing for revisiting vertices).
We can write such cycle as a list of n(n - 1) + 1 vertices v_1, v_2, v_3, ..., v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1 — a visiting order, where each (v_i, v_{i + 1}) occurs exactly once.
Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists.
Since the answer can be too large print its [l, r] segment, in other words, v_l, v_{l + 1}, ..., v_r.
Input
The first line contains the single integer T (1 ≤ T ≤ 100) — the number of test cases.
Next T lines contain test cases — one per line. The first and only line of each test case contains three integers n, l and r (2 ≤ n ≤ 10^5, 1 ≤ l ≤ r ≤ n(n - 1) + 1, r - l + 1 ≤ 10^5) — the number of vertices in K_n, and segment of the cycle to print.
It's guaranteed that the total sum of n doesn't exceed 10^5 and the total sum of r - l + 1 doesn't exceed 10^5.
Output
For each test case print the segment v_l, v_{l + 1}, ..., v_r of the lexicographically smallest cycle that visits every edge exactly once.
Example
Input
3
2 1 3
3 3 6
99995 9998900031 9998900031
Output
1 2 1
1 3 2 3
1
Note
In the second test case, the lexicographically minimum cycle looks like: 1, 2, 1, 3, 2, 3, 1.
In the third test case, it's quite obvious that the cycle should start and end in vertex 1.
|
{
"input": [
"3\n2 1 3\n3 3 6\n99995 9998900031 9998900031\n"
],
"output": [
"1 2 1 \n1 3 2 3 \n1 \n"
]
}
|
{
"input": [
"1\n2 2 3\n",
"1\n4 13 13\n",
"1\n3 1 1\n",
"10\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n",
"1\n3 7 7\n",
"1\n25 30 295\n",
"1\n4 12 13\n",
"5\n3 7 7\n4 13 13\n5 21 21\n6 31 31\n7 42 43\n",
"1\n5 4 4\n"
],
"output": [
"2 1 \n",
"1 \n",
"1 \n",
"1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n",
"1 \n",
"16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 2 3 2 4 2 5 2 6 2 7 2 8 2 9 2 10 2 11 2 12 2 13 2 14 2 15 2 16 2 17 2 18 2 19 2 20 2 21 2 22 2 23 2 24 2 25 3 4 3 5 3 6 3 7 3 8 3 9 3 10 3 11 3 12 3 13 3 14 3 15 3 16 3 17 3 18 3 19 3 20 3 21 3 22 3 23 3 24 3 25 4 5 4 6 4 7 4 8 4 9 4 10 4 11 4 12 4 13 4 14 4 15 4 16 4 17 4 18 4 19 4 20 4 21 4 22 4 23 4 24 4 25 5 6 5 7 5 8 5 9 5 10 5 11 5 12 5 13 5 14 5 15 5 16 5 17 5 18 5 19 5 20 5 21 5 22 5 23 5 24 5 25 6 7 6 8 6 9 6 10 6 11 6 12 6 13 6 14 6 15 6 16 6 17 6 18 6 19 6 20 6 21 6 22 6 23 6 24 6 25 7 8 7 9 7 10 7 11 7 12 7 13 7 14 7 15 7 16 7 17 7 18 7 19 7 20 7 21 7 22 7 23 7 24 7 25 8 \n",
"4 1 \n",
"1 \n1 \n1 \n1 \n7 1 \n",
"3 \n"
]
}
|
CORRECT
|
python3
|
import math
import sys
# input = sys.stdin.readline
T=int(input())
for _ in range(T):
n,l,r=map(int,input().split())
size=r-l+1
run=0
ans=[]
for i in range(1,n):
run+=2*(n-i)
# print(run)
if l<=run:
prev=run-2*(n-i)
gone=l-prev-1
size+=gone
# print(gone)
cur=0
now=i
nex=i+1
flag=True
while cur<size:
if flag:
if gone>0:
gone-=1
else:
ans.append(now)
flag=False
else:
if gone>0:
gone-=1
else:
ans.append(nex)
nex+=1
if nex>n:
now+=1
nex=now+1
flag=True
cur+=1
break
if len(ans)==0:
ans.append(1)
if r==n*(n-1)+1:
ans[-1]=1
print(*ans)
|
1334_D. Minimum Euler Cycle
|
You are given a complete directed graph K_n with n vertices: each pair of vertices u ≠ v in K_n have both directed edges (u, v) and (v, u); there are no self-loops.
You should find such a cycle in K_n that visits every directed edge exactly once (allowing for revisiting vertices).
We can write such cycle as a list of n(n - 1) + 1 vertices v_1, v_2, v_3, ..., v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1 — a visiting order, where each (v_i, v_{i + 1}) occurs exactly once.
Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists.
Since the answer can be too large print its [l, r] segment, in other words, v_l, v_{l + 1}, ..., v_r.
Input
The first line contains the single integer T (1 ≤ T ≤ 100) — the number of test cases.
Next T lines contain test cases — one per line. The first and only line of each test case contains three integers n, l and r (2 ≤ n ≤ 10^5, 1 ≤ l ≤ r ≤ n(n - 1) + 1, r - l + 1 ≤ 10^5) — the number of vertices in K_n, and segment of the cycle to print.
It's guaranteed that the total sum of n doesn't exceed 10^5 and the total sum of r - l + 1 doesn't exceed 10^5.
Output
For each test case print the segment v_l, v_{l + 1}, ..., v_r of the lexicographically smallest cycle that visits every edge exactly once.
Example
Input
3
2 1 3
3 3 6
99995 9998900031 9998900031
Output
1 2 1
1 3 2 3
1
Note
In the second test case, the lexicographically minimum cycle looks like: 1, 2, 1, 3, 2, 3, 1.
In the third test case, it's quite obvious that the cycle should start and end in vertex 1.
|
{
"input": [
"3\n2 1 3\n3 3 6\n99995 9998900031 9998900031\n"
],
"output": [
"1 2 1 \n1 3 2 3 \n1 \n"
]
}
|
{
"input": [
"1\n2 2 3\n",
"1\n4 13 13\n",
"1\n3 1 1\n",
"10\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n",
"1\n3 7 7\n",
"1\n25 30 295\n",
"1\n4 12 13\n",
"5\n3 7 7\n4 13 13\n5 21 21\n6 31 31\n7 42 43\n",
"1\n5 4 4\n"
],
"output": [
"2 1 \n",
"1 \n",
"1 \n",
"1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n",
"1 \n",
"16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 2 3 2 4 2 5 2 6 2 7 2 8 2 9 2 10 2 11 2 12 2 13 2 14 2 15 2 16 2 17 2 18 2 19 2 20 2 21 2 22 2 23 2 24 2 25 3 4 3 5 3 6 3 7 3 8 3 9 3 10 3 11 3 12 3 13 3 14 3 15 3 16 3 17 3 18 3 19 3 20 3 21 3 22 3 23 3 24 3 25 4 5 4 6 4 7 4 8 4 9 4 10 4 11 4 12 4 13 4 14 4 15 4 16 4 17 4 18 4 19 4 20 4 21 4 22 4 23 4 24 4 25 5 6 5 7 5 8 5 9 5 10 5 11 5 12 5 13 5 14 5 15 5 16 5 17 5 18 5 19 5 20 5 21 5 22 5 23 5 24 5 25 6 7 6 8 6 9 6 10 6 11 6 12 6 13 6 14 6 15 6 16 6 17 6 18 6 19 6 20 6 21 6 22 6 23 6 24 6 25 7 8 7 9 7 10 7 11 7 12 7 13 7 14 7 15 7 16 7 17 7 18 7 19 7 20 7 21 7 22 7 23 7 24 7 25 8 \n",
"4 1 \n",
"1 \n1 \n1 \n1 \n7 1 \n",
"3 \n"
]
}
|
CORRECT
|
python3
|
from sys import stdin, stdout
import math
def expandList(i,n):
if i!=n:
out = []
for j in range(n-i):
out.append(i)
out.append(i+j+1)
return out
else:
return [1]
T = int(stdin.readline().rstrip())
for iTest in range(T):
n,l,r = list(map(int,stdin.readline().rstrip().split(' ')))
listSizes = [2*i for i in range(n-1,-1,-1)]
listSizes[n-1] = 1
found = False
a = 0
i=1
while l>a+listSizes[i-1]:
a+=listSizes[i-1]
i+=1
listi = expandList(i,n)
ind = l-a-1
startList = listi[ind:]
while r>a+listSizes[i-1]:
a+=listSizes[i-1]
i+=1
startList+=expandList(i,n)
endList = expandList(i+1,n)
startList += endList
startList = startList[:r-l+1]
print(' '.join([str(x) for x in startList]))
|
1334_D. Minimum Euler Cycle
|
You are given a complete directed graph K_n with n vertices: each pair of vertices u ≠ v in K_n have both directed edges (u, v) and (v, u); there are no self-loops.
You should find such a cycle in K_n that visits every directed edge exactly once (allowing for revisiting vertices).
We can write such cycle as a list of n(n - 1) + 1 vertices v_1, v_2, v_3, ..., v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1 — a visiting order, where each (v_i, v_{i + 1}) occurs exactly once.
Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists.
Since the answer can be too large print its [l, r] segment, in other words, v_l, v_{l + 1}, ..., v_r.
Input
The first line contains the single integer T (1 ≤ T ≤ 100) — the number of test cases.
Next T lines contain test cases — one per line. The first and only line of each test case contains three integers n, l and r (2 ≤ n ≤ 10^5, 1 ≤ l ≤ r ≤ n(n - 1) + 1, r - l + 1 ≤ 10^5) — the number of vertices in K_n, and segment of the cycle to print.
It's guaranteed that the total sum of n doesn't exceed 10^5 and the total sum of r - l + 1 doesn't exceed 10^5.
Output
For each test case print the segment v_l, v_{l + 1}, ..., v_r of the lexicographically smallest cycle that visits every edge exactly once.
Example
Input
3
2 1 3
3 3 6
99995 9998900031 9998900031
Output
1 2 1
1 3 2 3
1
Note
In the second test case, the lexicographically minimum cycle looks like: 1, 2, 1, 3, 2, 3, 1.
In the third test case, it's quite obvious that the cycle should start and end in vertex 1.
|
{
"input": [
"3\n2 1 3\n3 3 6\n99995 9998900031 9998900031\n"
],
"output": [
"1 2 1 \n1 3 2 3 \n1 \n"
]
}
|
{
"input": [
"1\n2 2 3\n",
"1\n4 13 13\n",
"1\n3 1 1\n",
"10\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n",
"1\n3 7 7\n",
"1\n25 30 295\n",
"1\n4 12 13\n",
"5\n3 7 7\n4 13 13\n5 21 21\n6 31 31\n7 42 43\n",
"1\n5 4 4\n"
],
"output": [
"2 1 \n",
"1 \n",
"1 \n",
"1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n",
"1 \n",
"16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 2 3 2 4 2 5 2 6 2 7 2 8 2 9 2 10 2 11 2 12 2 13 2 14 2 15 2 16 2 17 2 18 2 19 2 20 2 21 2 22 2 23 2 24 2 25 3 4 3 5 3 6 3 7 3 8 3 9 3 10 3 11 3 12 3 13 3 14 3 15 3 16 3 17 3 18 3 19 3 20 3 21 3 22 3 23 3 24 3 25 4 5 4 6 4 7 4 8 4 9 4 10 4 11 4 12 4 13 4 14 4 15 4 16 4 17 4 18 4 19 4 20 4 21 4 22 4 23 4 24 4 25 5 6 5 7 5 8 5 9 5 10 5 11 5 12 5 13 5 14 5 15 5 16 5 17 5 18 5 19 5 20 5 21 5 22 5 23 5 24 5 25 6 7 6 8 6 9 6 10 6 11 6 12 6 13 6 14 6 15 6 16 6 17 6 18 6 19 6 20 6 21 6 22 6 23 6 24 6 25 7 8 7 9 7 10 7 11 7 12 7 13 7 14 7 15 7 16 7 17 7 18 7 19 7 20 7 21 7 22 7 23 7 24 7 25 8 \n",
"4 1 \n",
"1 \n1 \n1 \n1 \n7 1 \n",
"3 \n"
]
}
|
CORRECT
|
python3
|
# import sys
# _INPUT_LINES = sys.stdin.read().splitlines()
# input = iter(_INPUT_LINES).__next__
def go():
# n=int(input())
n,l,r = map(int, input().split())
# a = sorted(map(int, input().split()),reverse=True)
tot = n*(n-1)+1
add=[]
if r==tot:
add=['1']
r-=1
res=[]
if l<=r:
cur=0
nextg=n-1
while cur+2*nextg<l:
cur+=2*nextg
nextg-=1
g=n-nextg
shift=l-cur-1
pair,par = divmod(shift,2)
pair+=g+1
while l<=r:
if par==0:
res.append(g)
par+=1
else:
res.append(pair)
par=0
if pair<n:
pair+=1
else:
g+=1
pair=g+1
l+=1
res = res+add
return ' '.join(map(str,res))
# x,s = map(int,input().split())
t = int(input())
# t = 1
ans = []
for _ in range(t):
# print(go())
ans.append(str(go()))
#
print('\n'.join(ans))
|
1334_D. Minimum Euler Cycle
|
You are given a complete directed graph K_n with n vertices: each pair of vertices u ≠ v in K_n have both directed edges (u, v) and (v, u); there are no self-loops.
You should find such a cycle in K_n that visits every directed edge exactly once (allowing for revisiting vertices).
We can write such cycle as a list of n(n - 1) + 1 vertices v_1, v_2, v_3, ..., v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1 — a visiting order, where each (v_i, v_{i + 1}) occurs exactly once.
Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists.
Since the answer can be too large print its [l, r] segment, in other words, v_l, v_{l + 1}, ..., v_r.
Input
The first line contains the single integer T (1 ≤ T ≤ 100) — the number of test cases.
Next T lines contain test cases — one per line. The first and only line of each test case contains three integers n, l and r (2 ≤ n ≤ 10^5, 1 ≤ l ≤ r ≤ n(n - 1) + 1, r - l + 1 ≤ 10^5) — the number of vertices in K_n, and segment of the cycle to print.
It's guaranteed that the total sum of n doesn't exceed 10^5 and the total sum of r - l + 1 doesn't exceed 10^5.
Output
For each test case print the segment v_l, v_{l + 1}, ..., v_r of the lexicographically smallest cycle that visits every edge exactly once.
Example
Input
3
2 1 3
3 3 6
99995 9998900031 9998900031
Output
1 2 1
1 3 2 3
1
Note
In the second test case, the lexicographically minimum cycle looks like: 1, 2, 1, 3, 2, 3, 1.
In the third test case, it's quite obvious that the cycle should start and end in vertex 1.
|
{
"input": [
"3\n2 1 3\n3 3 6\n99995 9998900031 9998900031\n"
],
"output": [
"1 2 1 \n1 3 2 3 \n1 \n"
]
}
|
{
"input": [
"1\n2 2 3\n",
"1\n4 13 13\n",
"1\n3 1 1\n",
"10\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n",
"1\n3 7 7\n",
"1\n25 30 295\n",
"1\n4 12 13\n",
"5\n3 7 7\n4 13 13\n5 21 21\n6 31 31\n7 42 43\n",
"1\n5 4 4\n"
],
"output": [
"2 1 \n",
"1 \n",
"1 \n",
"1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n",
"1 \n",
"16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 2 3 2 4 2 5 2 6 2 7 2 8 2 9 2 10 2 11 2 12 2 13 2 14 2 15 2 16 2 17 2 18 2 19 2 20 2 21 2 22 2 23 2 24 2 25 3 4 3 5 3 6 3 7 3 8 3 9 3 10 3 11 3 12 3 13 3 14 3 15 3 16 3 17 3 18 3 19 3 20 3 21 3 22 3 23 3 24 3 25 4 5 4 6 4 7 4 8 4 9 4 10 4 11 4 12 4 13 4 14 4 15 4 16 4 17 4 18 4 19 4 20 4 21 4 22 4 23 4 24 4 25 5 6 5 7 5 8 5 9 5 10 5 11 5 12 5 13 5 14 5 15 5 16 5 17 5 18 5 19 5 20 5 21 5 22 5 23 5 24 5 25 6 7 6 8 6 9 6 10 6 11 6 12 6 13 6 14 6 15 6 16 6 17 6 18 6 19 6 20 6 21 6 22 6 23 6 24 6 25 7 8 7 9 7 10 7 11 7 12 7 13 7 14 7 15 7 16 7 17 7 18 7 19 7 20 7 21 7 22 7 23 7 24 7 25 8 \n",
"4 1 \n",
"1 \n1 \n1 \n1 \n7 1 \n",
"3 \n"
]
}
|
CORRECT
|
python3
|
import sys
def data(): return sys.stdin.buffer.readline().strip()
out=sys.stdout.write
def mdata(): return map(int, data().split())
for t in range(int(data())):
n,l,r=mdata()
a=l
for i in range(1,n+1):
if 2*(n-i)<=a:
a-=2*(n-i)
else:
break
cnt=l
ans=[]
if l%2==0:
if a==0:
ans.append(str(n))
else:
ans.append(str(i+a//2))
cnt+=1
k=i+a//2+1
for j in range(i,n):
while cnt<=r:
ans.append(str(j))
cnt+=1
if cnt>r:
break
ans.append(str(k))
k+=1
cnt+=1
if k==n+1:
k=j+2
break
if r==n*(n-1)+1:
ans.append(str(1))
out(' '.join(ans)+'\n')
|
1334_D. Minimum Euler Cycle
|
You are given a complete directed graph K_n with n vertices: each pair of vertices u ≠ v in K_n have both directed edges (u, v) and (v, u); there are no self-loops.
You should find such a cycle in K_n that visits every directed edge exactly once (allowing for revisiting vertices).
We can write such cycle as a list of n(n - 1) + 1 vertices v_1, v_2, v_3, ..., v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1 — a visiting order, where each (v_i, v_{i + 1}) occurs exactly once.
Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists.
Since the answer can be too large print its [l, r] segment, in other words, v_l, v_{l + 1}, ..., v_r.
Input
The first line contains the single integer T (1 ≤ T ≤ 100) — the number of test cases.
Next T lines contain test cases — one per line. The first and only line of each test case contains three integers n, l and r (2 ≤ n ≤ 10^5, 1 ≤ l ≤ r ≤ n(n - 1) + 1, r - l + 1 ≤ 10^5) — the number of vertices in K_n, and segment of the cycle to print.
It's guaranteed that the total sum of n doesn't exceed 10^5 and the total sum of r - l + 1 doesn't exceed 10^5.
Output
For each test case print the segment v_l, v_{l + 1}, ..., v_r of the lexicographically smallest cycle that visits every edge exactly once.
Example
Input
3
2 1 3
3 3 6
99995 9998900031 9998900031
Output
1 2 1
1 3 2 3
1
Note
In the second test case, the lexicographically minimum cycle looks like: 1, 2, 1, 3, 2, 3, 1.
In the third test case, it's quite obvious that the cycle should start and end in vertex 1.
|
{
"input": [
"3\n2 1 3\n3 3 6\n99995 9998900031 9998900031\n"
],
"output": [
"1 2 1 \n1 3 2 3 \n1 \n"
]
}
|
{
"input": [
"1\n2 2 3\n",
"1\n4 13 13\n",
"1\n3 1 1\n",
"10\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n",
"1\n3 7 7\n",
"1\n25 30 295\n",
"1\n4 12 13\n",
"5\n3 7 7\n4 13 13\n5 21 21\n6 31 31\n7 42 43\n",
"1\n5 4 4\n"
],
"output": [
"2 1 \n",
"1 \n",
"1 \n",
"1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n",
"1 \n",
"16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 2 3 2 4 2 5 2 6 2 7 2 8 2 9 2 10 2 11 2 12 2 13 2 14 2 15 2 16 2 17 2 18 2 19 2 20 2 21 2 22 2 23 2 24 2 25 3 4 3 5 3 6 3 7 3 8 3 9 3 10 3 11 3 12 3 13 3 14 3 15 3 16 3 17 3 18 3 19 3 20 3 21 3 22 3 23 3 24 3 25 4 5 4 6 4 7 4 8 4 9 4 10 4 11 4 12 4 13 4 14 4 15 4 16 4 17 4 18 4 19 4 20 4 21 4 22 4 23 4 24 4 25 5 6 5 7 5 8 5 9 5 10 5 11 5 12 5 13 5 14 5 15 5 16 5 17 5 18 5 19 5 20 5 21 5 22 5 23 5 24 5 25 6 7 6 8 6 9 6 10 6 11 6 12 6 13 6 14 6 15 6 16 6 17 6 18 6 19 6 20 6 21 6 22 6 23 6 24 6 25 7 8 7 9 7 10 7 11 7 12 7 13 7 14 7 15 7 16 7 17 7 18 7 19 7 20 7 21 7 22 7 23 7 24 7 25 8 \n",
"4 1 \n",
"1 \n1 \n1 \n1 \n7 1 \n",
"3 \n"
]
}
|
CORRECT
|
python3
|
T = int(input().strip())
for t in range(T):
n, l, r = map(int, input().strip().split())
if l == n*(n-1)+1:
print(1)
continue
k = int((2*n-1-((2*n-1)**2-4*l)**0.5)/2)
if l <= 2*k*n- k*(k+1): k -= 1
if l > 2*(k+1)*n - (k+1)*(k+2): k += 1
m = 2*k*n - k*(k+1)
s = []
k += 1
j = (l-m-1)//2 + k
while l<=r:
if l%2 == 1:
if k==n: s.append(1)
else: s.append(k)
else:
j += 1
s.append(j)
if j == n:
k += 1
j = k
l += 1
print(" ".join(map(str, s)))
|
1334_D. Minimum Euler Cycle
|
You are given a complete directed graph K_n with n vertices: each pair of vertices u ≠ v in K_n have both directed edges (u, v) and (v, u); there are no self-loops.
You should find such a cycle in K_n that visits every directed edge exactly once (allowing for revisiting vertices).
We can write such cycle as a list of n(n - 1) + 1 vertices v_1, v_2, v_3, ..., v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1 — a visiting order, where each (v_i, v_{i + 1}) occurs exactly once.
Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists.
Since the answer can be too large print its [l, r] segment, in other words, v_l, v_{l + 1}, ..., v_r.
Input
The first line contains the single integer T (1 ≤ T ≤ 100) — the number of test cases.
Next T lines contain test cases — one per line. The first and only line of each test case contains three integers n, l and r (2 ≤ n ≤ 10^5, 1 ≤ l ≤ r ≤ n(n - 1) + 1, r - l + 1 ≤ 10^5) — the number of vertices in K_n, and segment of the cycle to print.
It's guaranteed that the total sum of n doesn't exceed 10^5 and the total sum of r - l + 1 doesn't exceed 10^5.
Output
For each test case print the segment v_l, v_{l + 1}, ..., v_r of the lexicographically smallest cycle that visits every edge exactly once.
Example
Input
3
2 1 3
3 3 6
99995 9998900031 9998900031
Output
1 2 1
1 3 2 3
1
Note
In the second test case, the lexicographically minimum cycle looks like: 1, 2, 1, 3, 2, 3, 1.
In the third test case, it's quite obvious that the cycle should start and end in vertex 1.
|
{
"input": [
"3\n2 1 3\n3 3 6\n99995 9998900031 9998900031\n"
],
"output": [
"1 2 1 \n1 3 2 3 \n1 \n"
]
}
|
{
"input": [
"1\n2 2 3\n",
"1\n4 13 13\n",
"1\n3 1 1\n",
"10\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n",
"1\n3 7 7\n",
"1\n25 30 295\n",
"1\n4 12 13\n",
"5\n3 7 7\n4 13 13\n5 21 21\n6 31 31\n7 42 43\n",
"1\n5 4 4\n"
],
"output": [
"2 1 \n",
"1 \n",
"1 \n",
"1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n",
"1 \n",
"16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 2 3 2 4 2 5 2 6 2 7 2 8 2 9 2 10 2 11 2 12 2 13 2 14 2 15 2 16 2 17 2 18 2 19 2 20 2 21 2 22 2 23 2 24 2 25 3 4 3 5 3 6 3 7 3 8 3 9 3 10 3 11 3 12 3 13 3 14 3 15 3 16 3 17 3 18 3 19 3 20 3 21 3 22 3 23 3 24 3 25 4 5 4 6 4 7 4 8 4 9 4 10 4 11 4 12 4 13 4 14 4 15 4 16 4 17 4 18 4 19 4 20 4 21 4 22 4 23 4 24 4 25 5 6 5 7 5 8 5 9 5 10 5 11 5 12 5 13 5 14 5 15 5 16 5 17 5 18 5 19 5 20 5 21 5 22 5 23 5 24 5 25 6 7 6 8 6 9 6 10 6 11 6 12 6 13 6 14 6 15 6 16 6 17 6 18 6 19 6 20 6 21 6 22 6 23 6 24 6 25 7 8 7 9 7 10 7 11 7 12 7 13 7 14 7 15 7 16 7 17 7 18 7 19 7 20 7 21 7 22 7 23 7 24 7 25 8 \n",
"4 1 \n",
"1 \n1 \n1 \n1 \n7 1 \n",
"3 \n"
]
}
|
CORRECT
|
python3
|
from sys import *
t = int(stdin.readline())
import math
for _ in range(t):
n,l,r = list(map(int,stdin.readline().split(' ')))
if(l == n*n-n+1):
print('1')
continue
k = math.ceil(((2*n-1) - math.sqrt((2*n-1)**2 - 4*l))/2)
s = k*(2*n-1 -k)
sl = 2*(n-k)
lb = k
eb = k+1+math.ceil((l-s+sl)/2)-1
while(l <= r):
if(l == n*n-n+1):
break
if(l%2 == 0):
stdout.write(str(eb)+' ')
eb = eb+1
if(eb > n):
lb = lb+1
eb = lb+1
else:
stdout.write(str(lb)+' ')
l = l+1
if(r == n*n-n+1):
stdout.write('1')
stdout.write('\n')
|
1334_D. Minimum Euler Cycle
|
You are given a complete directed graph K_n with n vertices: each pair of vertices u ≠ v in K_n have both directed edges (u, v) and (v, u); there are no self-loops.
You should find such a cycle in K_n that visits every directed edge exactly once (allowing for revisiting vertices).
We can write such cycle as a list of n(n - 1) + 1 vertices v_1, v_2, v_3, ..., v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1 — a visiting order, where each (v_i, v_{i + 1}) occurs exactly once.
Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists.
Since the answer can be too large print its [l, r] segment, in other words, v_l, v_{l + 1}, ..., v_r.
Input
The first line contains the single integer T (1 ≤ T ≤ 100) — the number of test cases.
Next T lines contain test cases — one per line. The first and only line of each test case contains three integers n, l and r (2 ≤ n ≤ 10^5, 1 ≤ l ≤ r ≤ n(n - 1) + 1, r - l + 1 ≤ 10^5) — the number of vertices in K_n, and segment of the cycle to print.
It's guaranteed that the total sum of n doesn't exceed 10^5 and the total sum of r - l + 1 doesn't exceed 10^5.
Output
For each test case print the segment v_l, v_{l + 1}, ..., v_r of the lexicographically smallest cycle that visits every edge exactly once.
Example
Input
3
2 1 3
3 3 6
99995 9998900031 9998900031
Output
1 2 1
1 3 2 3
1
Note
In the second test case, the lexicographically minimum cycle looks like: 1, 2, 1, 3, 2, 3, 1.
In the third test case, it's quite obvious that the cycle should start and end in vertex 1.
|
{
"input": [
"3\n2 1 3\n3 3 6\n99995 9998900031 9998900031\n"
],
"output": [
"1 2 1 \n1 3 2 3 \n1 \n"
]
}
|
{
"input": [
"1\n2 2 3\n",
"1\n4 13 13\n",
"1\n3 1 1\n",
"10\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n",
"1\n3 7 7\n",
"1\n25 30 295\n",
"1\n4 12 13\n",
"5\n3 7 7\n4 13 13\n5 21 21\n6 31 31\n7 42 43\n",
"1\n5 4 4\n"
],
"output": [
"2 1 \n",
"1 \n",
"1 \n",
"1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n",
"1 \n",
"16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 2 3 2 4 2 5 2 6 2 7 2 8 2 9 2 10 2 11 2 12 2 13 2 14 2 15 2 16 2 17 2 18 2 19 2 20 2 21 2 22 2 23 2 24 2 25 3 4 3 5 3 6 3 7 3 8 3 9 3 10 3 11 3 12 3 13 3 14 3 15 3 16 3 17 3 18 3 19 3 20 3 21 3 22 3 23 3 24 3 25 4 5 4 6 4 7 4 8 4 9 4 10 4 11 4 12 4 13 4 14 4 15 4 16 4 17 4 18 4 19 4 20 4 21 4 22 4 23 4 24 4 25 5 6 5 7 5 8 5 9 5 10 5 11 5 12 5 13 5 14 5 15 5 16 5 17 5 18 5 19 5 20 5 21 5 22 5 23 5 24 5 25 6 7 6 8 6 9 6 10 6 11 6 12 6 13 6 14 6 15 6 16 6 17 6 18 6 19 6 20 6 21 6 22 6 23 6 24 6 25 7 8 7 9 7 10 7 11 7 12 7 13 7 14 7 15 7 16 7 17 7 18 7 19 7 20 7 21 7 22 7 23 7 24 7 25 8 \n",
"4 1 \n",
"1 \n1 \n1 \n1 \n7 1 \n",
"3 \n"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
struct R {
long long int nd, i;
};
int main() {
long long int n, l, r, st;
scanf("%*d");
while (~scanf("%lld %lld %lld", &n, &l, &r)) {
long long int p, i, j, nd;
vector<R> v;
v.push_back({1, 1});
for (p = 1, i = 2, j = n - 1; i < n; i++, j--) v.push_back({i, p += j + j});
long long int st = 0, en = v.size() - 1, md, pos = -1;
while (st <= en) {
md = (st + en) / 2;
if (v[md].i > l)
en = md - 1;
else {
pos = max(pos, md);
st = md + 1;
}
}
vector<R> pet;
for (nd = v[pos].nd, i = v[pos].i; i <= r && nd < n; nd++) {
for (j = nd + 1; j <= n && i <= r; j++)
pet.push_back({nd, i++}), pet.push_back({j, i++});
}
if (nd == n) pet.push_back({1, i++});
for (auto it : pet)
if (it.i >= l && it.i <= r) printf("%lld ", it.nd);
puts("");
}
return 0;
}
|
1334_D. Minimum Euler Cycle
|
You are given a complete directed graph K_n with n vertices: each pair of vertices u ≠ v in K_n have both directed edges (u, v) and (v, u); there are no self-loops.
You should find such a cycle in K_n that visits every directed edge exactly once (allowing for revisiting vertices).
We can write such cycle as a list of n(n - 1) + 1 vertices v_1, v_2, v_3, ..., v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1 — a visiting order, where each (v_i, v_{i + 1}) occurs exactly once.
Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists.
Since the answer can be too large print its [l, r] segment, in other words, v_l, v_{l + 1}, ..., v_r.
Input
The first line contains the single integer T (1 ≤ T ≤ 100) — the number of test cases.
Next T lines contain test cases — one per line. The first and only line of each test case contains three integers n, l and r (2 ≤ n ≤ 10^5, 1 ≤ l ≤ r ≤ n(n - 1) + 1, r - l + 1 ≤ 10^5) — the number of vertices in K_n, and segment of the cycle to print.
It's guaranteed that the total sum of n doesn't exceed 10^5 and the total sum of r - l + 1 doesn't exceed 10^5.
Output
For each test case print the segment v_l, v_{l + 1}, ..., v_r of the lexicographically smallest cycle that visits every edge exactly once.
Example
Input
3
2 1 3
3 3 6
99995 9998900031 9998900031
Output
1 2 1
1 3 2 3
1
Note
In the second test case, the lexicographically minimum cycle looks like: 1, 2, 1, 3, 2, 3, 1.
In the third test case, it's quite obvious that the cycle should start and end in vertex 1.
|
{
"input": [
"3\n2 1 3\n3 3 6\n99995 9998900031 9998900031\n"
],
"output": [
"1 2 1 \n1 3 2 3 \n1 \n"
]
}
|
{
"input": [
"1\n2 2 3\n",
"1\n4 13 13\n",
"1\n3 1 1\n",
"10\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n",
"1\n3 7 7\n",
"1\n25 30 295\n",
"1\n4 12 13\n",
"5\n3 7 7\n4 13 13\n5 21 21\n6 31 31\n7 42 43\n",
"1\n5 4 4\n"
],
"output": [
"2 1 \n",
"1 \n",
"1 \n",
"1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n",
"1 \n",
"16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 2 3 2 4 2 5 2 6 2 7 2 8 2 9 2 10 2 11 2 12 2 13 2 14 2 15 2 16 2 17 2 18 2 19 2 20 2 21 2 22 2 23 2 24 2 25 3 4 3 5 3 6 3 7 3 8 3 9 3 10 3 11 3 12 3 13 3 14 3 15 3 16 3 17 3 18 3 19 3 20 3 21 3 22 3 23 3 24 3 25 4 5 4 6 4 7 4 8 4 9 4 10 4 11 4 12 4 13 4 14 4 15 4 16 4 17 4 18 4 19 4 20 4 21 4 22 4 23 4 24 4 25 5 6 5 7 5 8 5 9 5 10 5 11 5 12 5 13 5 14 5 15 5 16 5 17 5 18 5 19 5 20 5 21 5 22 5 23 5 24 5 25 6 7 6 8 6 9 6 10 6 11 6 12 6 13 6 14 6 15 6 16 6 17 6 18 6 19 6 20 6 21 6 22 6 23 6 24 6 25 7 8 7 9 7 10 7 11 7 12 7 13 7 14 7 15 7 16 7 17 7 18 7 19 7 20 7 21 7 22 7 23 7 24 7 25 8 \n",
"4 1 \n",
"1 \n1 \n1 \n1 \n7 1 \n",
"3 \n"
]
}
|
CORRECT
|
java
|
//package CodeforcesJava;
import java.io.*;
import java.util.*;
public class Main {
public void solve(InputProvider in, PrintWriter out) throws IOException {
int testCount = in.nextInt();
for (int test = 0; test < testCount; test++) {
long pointCount = in.nextLong();
long firstIndex = in.nextLong();
long lastIndex = in.nextLong();
long firstGroup = 2 * (pointCount - 1);
boolean needLast = lastIndex == countElements(firstGroup, pointCount - 1) + 1;
if (firstIndex < pointCount * (pointCount - 1) + 1) {
if (needLast) {
lastIndex--;
}
long minGuess = 0;
long maxGuess = pointCount - 1;
while (maxGuess > minGuess + 1) {
long middleGuess = (maxGuess + minGuess) / 2;
long middleGuessCount = countElements(firstGroup, middleGuess);
if (firstIndex <= middleGuessCount) {
maxGuess = middleGuess;
} else {
minGuess = middleGuess;
}
}
while (countElements(firstGroup, minGuess + 1) < firstIndex) {
minGuess++;
}
long distance = firstIndex - countElements(firstGroup, minGuess);
long levelPoint = minGuess + 1;
long point;
long pairPoint;
if ((distance & 1) == 1) {
point = levelPoint;
pairPoint = (distance + 1) / 2 + levelPoint;
} else {
point = distance / 2 + levelPoint;
out.print(point + " ");
firstIndex++;
if (point == pointCount) {
point = levelPoint + 1;
pairPoint = point + 1;
} else {
pairPoint = point + 1;
point = levelPoint;
}
}
boolean printMain = true;
for (long i = firstIndex; i <= lastIndex; i++) {
if (printMain) {
out.print(point + " ");
printMain = false;
} else {
out.print(pairPoint + " ");
printMain = true;
if (pairPoint < pointCount) {
pairPoint++;
} else {
point++;
pairPoint = point + 1;
}
}
}
}
if (needLast) {
out.print("1 ");
}
out.print("\n");
}
}
private long countElements(long first, long count) {
return (2 * first - 2 * (count - 1)) * count / 2;
}
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();
}
}
}
|
1334_D. Minimum Euler Cycle
|
You are given a complete directed graph K_n with n vertices: each pair of vertices u ≠ v in K_n have both directed edges (u, v) and (v, u); there are no self-loops.
You should find such a cycle in K_n that visits every directed edge exactly once (allowing for revisiting vertices).
We can write such cycle as a list of n(n - 1) + 1 vertices v_1, v_2, v_3, ..., v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1 — a visiting order, where each (v_i, v_{i + 1}) occurs exactly once.
Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists.
Since the answer can be too large print its [l, r] segment, in other words, v_l, v_{l + 1}, ..., v_r.
Input
The first line contains the single integer T (1 ≤ T ≤ 100) — the number of test cases.
Next T lines contain test cases — one per line. The first and only line of each test case contains three integers n, l and r (2 ≤ n ≤ 10^5, 1 ≤ l ≤ r ≤ n(n - 1) + 1, r - l + 1 ≤ 10^5) — the number of vertices in K_n, and segment of the cycle to print.
It's guaranteed that the total sum of n doesn't exceed 10^5 and the total sum of r - l + 1 doesn't exceed 10^5.
Output
For each test case print the segment v_l, v_{l + 1}, ..., v_r of the lexicographically smallest cycle that visits every edge exactly once.
Example
Input
3
2 1 3
3 3 6
99995 9998900031 9998900031
Output
1 2 1
1 3 2 3
1
Note
In the second test case, the lexicographically minimum cycle looks like: 1, 2, 1, 3, 2, 3, 1.
In the third test case, it's quite obvious that the cycle should start and end in vertex 1.
|
{
"input": [
"3\n2 1 3\n3 3 6\n99995 9998900031 9998900031\n"
],
"output": [
"1 2 1 \n1 3 2 3 \n1 \n"
]
}
|
{
"input": [
"1\n2 2 3\n",
"1\n4 13 13\n",
"1\n3 1 1\n",
"10\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n2 1 3\n",
"1\n3 7 7\n",
"1\n25 30 295\n",
"1\n4 12 13\n",
"5\n3 7 7\n4 13 13\n5 21 21\n6 31 31\n7 42 43\n",
"1\n5 4 4\n"
],
"output": [
"2 1 \n",
"1 \n",
"1 \n",
"1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n1 2 1 \n",
"1 \n",
"16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 2 3 2 4 2 5 2 6 2 7 2 8 2 9 2 10 2 11 2 12 2 13 2 14 2 15 2 16 2 17 2 18 2 19 2 20 2 21 2 22 2 23 2 24 2 25 3 4 3 5 3 6 3 7 3 8 3 9 3 10 3 11 3 12 3 13 3 14 3 15 3 16 3 17 3 18 3 19 3 20 3 21 3 22 3 23 3 24 3 25 4 5 4 6 4 7 4 8 4 9 4 10 4 11 4 12 4 13 4 14 4 15 4 16 4 17 4 18 4 19 4 20 4 21 4 22 4 23 4 24 4 25 5 6 5 7 5 8 5 9 5 10 5 11 5 12 5 13 5 14 5 15 5 16 5 17 5 18 5 19 5 20 5 21 5 22 5 23 5 24 5 25 6 7 6 8 6 9 6 10 6 11 6 12 6 13 6 14 6 15 6 16 6 17 6 18 6 19 6 20 6 21 6 22 6 23 6 24 6 25 7 8 7 9 7 10 7 11 7 12 7 13 7 14 7 15 7 16 7 17 7 18 7 19 7 20 7 21 7 22 7 23 7 24 7 25 8 \n",
"4 1 \n",
"1 \n1 \n1 \n1 \n7 1 \n",
"3 \n"
]
}
|
CORRECT
|
python3
|
# |
# _` | __ \ _` | __| _ \ __ \ _` | _` |
# ( | | | ( | ( ( | | | ( | ( |
# \__,_| _| _| \__,_| \___| \___/ _| _| \__,_| \__,_|
import sys
import collections
def read_line():
return sys.stdin.readline()[:-1]
def read_int():
return int(sys.stdin.readline())
def read_int_line():
return [int(v) for v in sys.stdin.readline().split()]
t = read_int()
for i in range(t):
n,l,r = read_int_line()
u = r-l+1
lt = l
k = n
while lt>0 and k>0:
k-=1
lt -= 2*(k)
lt += 2*(k)
k = n-k
j= max(k,1)
s = 0
for i in range(1,j):
s += 2*(n-i)
lt = l
lt -= s
lt += 1
i = j + lt//2
ans = []
if l%2==0:
ans.append(i)
i+=1
while j<=n and len(ans)<=u:
if j!=k:
i = j+1
while i<=n and len(ans)<=u:
ans.append(j)
ans.append(i)
i+=1
j+=1
ans.append(1)
print(*ans[:u])
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.