/* 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;
}
};
int n;
Line a[N], centro{0, 0};
map<int, deque<int>> co, ro;
map<pair<int, int>, int> is;
ll r(ll n){
if(n==0) return INF;
return n & -n;
}
vector<array<ll, 3>> X[N];
ll f(ll x, ll y, ll cost, int idx){
if(x==y){
X[idx].pb({0, 0, cost});
return cost;
}
X[idx].pb({x, y, cost});
if(r(x) < r(y)){
ll c;
if(y == 0)
c = x;
else
c = x - (x & (INF^(r(y)-1)));
if(cost == 0) co[y].pb(x);
ro[x-c].pb(y);
return cost + f(x - c, y, c, idx);
}
ll c;
if(x == 0)
c = y;
else
c = y - (y & (INF^(r(x)-1)));
if(cost == 0) ro[x].pb(y);
co[y-c].pb(x);
return cost + f(x, y - c, c, idx);
}
void dfs(int x, int y, int w){
// cout << x << ' ' << y << ' ' << w << '\n';
int s = 0, down = 0, right = 0;
ll mxdown = r(y) - 1 - ((r(y)-1) & x), mxright = r(x) - 1 - ((r(x)-1) & y);
for(int i = 0; i < co[y].size(); ++i){
if(co[y][i] <= x) co[y].pop_front(), --i;
else if(co[y][i] > x + mxdown){
int fff = co[y].size() - i;
for(int j = 0; j < fff; ++j) co[y].pop_back();
break;
}else down++;
}
for(int i = 0; i < ro[x].size(); ++i){
if(ro[x][i] <= y) ro[x].pop_front(), --i;
else if(ro[x][i] > y + mxright){
int fff = ro[x].size() - i;
for(int j = 0; j < fff; ++j) ro[x].pop_back();
break;
}
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){
f(a[i].x, a[i].y, 0, i);
reverse(all(X[i]));
X[i].pb(X[i].back());
}
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, 0);
f(centro.x, centro.y, 0, n);
reverse(all(X[n]));
X[n].pb(X[n].back());
// en;
// cout << centro.x << ' ' << centro.y << '\n';
for(int idx = 0; idx < n; ++idx){
ll s = centro.x + centro.y + a[idx].x + a[idx].y;
for(int i = 0; i < min(X[n].size(), X[idx].size()); ++i){
if(X[n][i][0] == X[idx][i][0] && X[n][i][1] == X[idx][i][1]){
s -= X[idx][i][2] + X[n][i][2];
}else{
s += abs(X[idx][i][0] - X[n][i][0]) + abs(X[n][i][1] - X[idx][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
cvenk.cpp: In function 'void dfs(int, int, int)':
cvenk.cpp:64:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::deque<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | for(int i = 0; i < co[y].size(); ++i){
| ~~^~~~~~~~~~~~~~
cvenk.cpp:72:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::deque<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | for(int i = 0; i < ro[x].size(); ++i){
| ~~^~~~~~~~~~~~~~
cvenk.cpp:89:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::deque<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
89 | for(int i = 0; i < co[y].size(); ++i){
| ~~^~~~~~~~~~~~~~
cvenk.cpp:95:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::deque<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
95 | for(int i = 0; i < ro[x].size(); ++i){
| ~~^~~~~~~~~~~~~~
cvenk.cpp: In function 'void solve()':
cvenk.cpp:138:22: warning: comparison of integer expressions of different signedness: 'int' and 'const long unsigned int' [-Wsign-compare]
138 | for(int i = 0; i < min(X[n].size(), X[idx].size()); ++i){
| ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cvenk.cpp:126:7: warning: unused variable 'cur' [-Wunused-variable]
126 | int cur = 0, x = 0, y = 0;
| ^~~
cvenk.cpp:126:16: warning: unused variable 'x' [-Wunused-variable]
126 | int cur = 0, x = 0, y = 0;
| ^
cvenk.cpp:126:23: warning: unused variable 'y' [-Wunused-variable]
126 | int cur = 0, x = 0, y = 0;
| ^
cvenk.cpp: In function 'int main()':
cvenk.cpp:154:15: warning: unused variable 'aa' [-Wunused-variable]
154 | int tt = 1, aa;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
27228 KB |
Output is correct |
2 |
Correct |
5 ms |
27228 KB |
Output is correct |
3 |
Correct |
5 ms |
27228 KB |
Output is correct |
4 |
Correct |
5 ms |
27228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
27736 KB |
Output is correct |
2 |
Correct |
6 ms |
27228 KB |
Output is correct |
3 |
Correct |
6 ms |
27228 KB |
Output is correct |
4 |
Correct |
5 ms |
27228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
84 ms |
48980 KB |
Output is correct |
2 |
Correct |
83 ms |
48880 KB |
Output is correct |
3 |
Correct |
81 ms |
41040 KB |
Output is correct |
4 |
Correct |
57 ms |
40992 KB |
Output is correct |
5 |
Correct |
48 ms |
41812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
965 ms |
371504 KB |
Output is correct |
2 |
Correct |
1022 ms |
371540 KB |
Output is correct |
3 |
Execution timed out |
3049 ms |
51708 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |