This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define pb push_back
#define all(x) x.begin(), x.end()
#define en cout << '\n'
const int N = 1e6+100, M = 1e5+10, K = 18;
const ll INF = (1ll<<60)-1;
struct Line{
ll x, y;
void re(){
cin >> x >> y;
}
bool operator<(const Line &other) const{
return x < other.x;
}
};
ll r(ll n){
if(n==0) return 1e18;
return n & -n;
}
ll f(ll x, ll y, ll cost, vector<array<ll, 3>> &g){
if(x==y){
g.pb({0, 0, cost});
return cost;
}
g.pb({x, y, cost});
if(r(x) < r(y)){
ll c;
if(y == 0)
c = x;
else
c = x - (x & (INF^(r(y)-1)));
return cost + f(x - c, y, c, g);
}
ll c;
if(x == 0)
c = y;
else
c = y - (y & (INF^(r(x)-1)));
return cost + f(x, y - c, c, g);
}
int n;
Line a[N], centro{0, 0};
map<int, deque<int>> co, ro;
map<pair<int, int>, int> is;
void dfs(int x, int y, int w){
// cout << x << ' ' << y << ' ' << w << '\n';
int s = 0, down = 0, right = 0;
for(int i = 0; i < co[y].size(); ++i){
if(co[y][i] <= x) co[y].pop_front(), --i;
else{
down++;
}
}
for(int i = 0; i < ro[x].size(); ++i){
if(ro[x][i] <= y) ro[x].pop_front(), --i;
else{
right++;
}
}
s = is[{x,y}];
// cout << right << ' ' << down << ' ' << s << "F"<< '\n';
// en;
if(right + s + w < down){
for(int i = 0; i < co[y].size(); ++i){
dfs(co[y][i], y, w + s + right);
return;
}
}
if(down + s + w < right){
for(int i = 0; i < ro[x].size(); ++i){
dfs(x, ro[x][i], w + s + down);
return;
}
}
centro.x = x;
centro.y = y;
}
void solve(){
cin >> n;
for(int i = 0; i < n; ++i){
a[i].re();
is[{a[i].x, a[i].y}]++;
}
ll ans = 0;
for(int i = 0; i < n; ++i){
vector<array<ll, 3>> X;
f(a[i].x, a[i].y, 0, X);
for(auto k: X){
co[k[1]].pb(k[0]);
ro[k[0]].pb(k[1]);
}
}
for(auto &v: ro){
sort(all(v.second));
}
for(auto &v: co){
sort(all(v.second));
}
int cur = 0, x = 0, y = 0;
dfs(0, 0, cur);
vector<array<ll, 3>> Y;
f(centro.x, centro.y, 0, Y);
ll s = centro.x + centro.y;
reverse(all(Y));
Y.pb(Y.back());
// en;
// cout << centro.x << ' ' << centro.y << '\n';
for(int i = 0; i < n; ++i){
vector<array<ll, 3>> X;
f(a[i].x, a[i].y, 0, X);
ll s = centro.x + centro.y + a[i].x + a[i].y;
reverse(all(X));
X.pb(X.back());
for(int i = 0; i < min(Y.size(), X.size()); ++i){
if(Y[i][0] == X[i][0] && Y[i][1] == X[i][1]){
s -= X[i][2] + Y[i][2];
}else{
s += abs(X[i][0] - Y[i][0]) + abs(Y[i][1] - X[i][1]);
break;
}
}
ans += s;
}
cout << ans;
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int tt = 1, aa;
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
// cin >> tt;
while(tt--){
solve();
// en;
}
cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n";
return 0;
}
Compilation message (stderr)
cvenk.cpp: In function 'void dfs(int, int, int)':
cvenk.cpp:57:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::deque<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | for(int i = 0; i < co[y].size(); ++i){
| ~~^~~~~~~~~~~~~~
cvenk.cpp:63:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::deque<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | for(int i = 0; i < ro[x].size(); ++i){
| ~~^~~~~~~~~~~~~~
cvenk.cpp:75:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::deque<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
75 | for(int i = 0; i < co[y].size(); ++i){
| ~~^~~~~~~~~~~~~~
cvenk.cpp:81:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::deque<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
81 | for(int i = 0; i < ro[x].size(); ++i){
| ~~^~~~~~~~~~~~~~
cvenk.cpp: In function 'void solve()':
cvenk.cpp:133:22: warning: comparison of integer expressions of different signedness: 'int' and 'const long unsigned int' [-Wsign-compare]
133 | for(int i = 0; i < min(Y.size(), X.size()); ++i){
| ~~^~~~~~~~~~~~~~~~~~~~~~~~~
cvenk.cpp:115:16: warning: unused variable 'x' [-Wunused-variable]
115 | int cur = 0, x = 0, y = 0;
| ^
cvenk.cpp:115:23: warning: unused variable 'y' [-Wunused-variable]
115 | int cur = 0, x = 0, y = 0;
| ^
cvenk.cpp:121:6: warning: unused variable 's' [-Wunused-variable]
121 | ll s = centro.x + centro.y;
| ^
cvenk.cpp: In function 'int main()':
cvenk.cpp:149:15: warning: unused variable 'aa' [-Wunused-variable]
149 | int tt = 1, aa;
| ^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |