/* 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){
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){
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:70:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::deque<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | for(int i = 0; i < ro[x].size(); ++i){
| ~~^~~~~~~~~~~~~~
cvenk.cpp:85:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::deque<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
85 | for(int i = 0; i < co[y].size(); ++i){
| ~~^~~~~~~~~~~~~~
cvenk.cpp:91:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::deque<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
91 | for(int i = 0; i < ro[x].size(); ++i){
| ~~^~~~~~~~~~~~~~
cvenk.cpp: In function 'void solve()':
cvenk.cpp:134:22: warning: comparison of integer expressions of different signedness: 'int' and 'const long unsigned int' [-Wsign-compare]
134 | for(int i = 0; i < min(X[n].size(), X[idx].size()); ++i){
| ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cvenk.cpp:122:7: warning: unused variable 'cur' [-Wunused-variable]
122 | int cur = 0, x = 0, y = 0;
| ^~~
cvenk.cpp:122:16: warning: unused variable 'x' [-Wunused-variable]
122 | int cur = 0, x = 0, y = 0;
| ^
cvenk.cpp:122:23: warning: unused variable 'y' [-Wunused-variable]
122 | int cur = 0, x = 0, y = 0;
| ^
cvenk.cpp: In function 'int main()':
cvenk.cpp:150:15: warning: unused variable 'aa' [-Wunused-variable]
150 | int tt = 1, aa;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
27380 KB |
Output is correct |
2 |
Correct |
5 ms |
27228 KB |
Output is correct |
3 |
Correct |
5 ms |
27224 KB |
Output is correct |
4 |
Correct |
5 ms |
27224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
27736 KB |
Output is correct |
2 |
Correct |
6 ms |
27224 KB |
Output is correct |
3 |
Correct |
5 ms |
27480 KB |
Output is correct |
4 |
Correct |
5 ms |
27224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
82 ms |
48976 KB |
Output is correct |
2 |
Correct |
82 ms |
48976 KB |
Output is correct |
3 |
Correct |
83 ms |
41080 KB |
Output is correct |
4 |
Correct |
56 ms |
41048 KB |
Output is correct |
5 |
Correct |
48 ms |
41808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
966 ms |
371436 KB |
Output is correct |
2 |
Correct |
1024 ms |
371448 KB |
Output is correct |
3 |
Execution timed out |
3031 ms |
51680 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |