/* 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;
deque<int> CO[N], RO[N];
map<pair<int, int>, int> is;
vector<int> al;
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);
int mappedx = lower_bound(all(al), x) - al.begin() + 1;
int mappedy = lower_bound(all(al), y) - al.begin() + 1;
// cout << x << ' ' << y << ' ' << mappedx << ' ' << mappedy << '\n';
for(int i = 0; i < CO[mappedy].size(); ++i){
if(CO[mappedy][i] <= x) CO[mappedy].pop_front(), --i;
else if(CO[mappedy][i] > x + mxdown){
break;
}else down++;
}
for(int i = 0; i < RO[mappedx].size(); ++i){
if(RO[mappedx][i] <= y) RO[mappedx].pop_front(), --i;
else if(RO[mappedx][i] > y + mxright){
break;
}
else{
right++;
}
}
s = is[{x,y}];
// cout << right << ' ' << down << ' ' << s << "F"<< '\n';
// en;
if(right + s + w < down){
return dfs(CO[mappedy][0], y, w + s + right);
}
if(down + s + w < right){
return dfs(x, RO[mappedx][0], w + s + down);
}
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 f: X[i]) al.pb(f[0]), al.pb(f[1]);
}
sort(all(al));
al.erase(unique(all(al)), al.end());
for(int i = 0; i < al.size(); ++i){
CO[i + 1] = co[al[i]];
RO[i + 1] = ro[al[i]];
sort(all(CO[i + 1]));
sort(all(RO[i + 1]));
}
// for(auto &v: RO){
// sort(all(v.second));
// }
// for(int i = 0){
// 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: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 < CO[mappedy].size(); ++i){
| ~~^~~~~~~~~~~~~~~~~~~~
cvenk.cpp:78:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::deque<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
78 | for(int i = 0; i < RO[mappedx].size(); ++i){
| ~~^~~~~~~~~~~~~~~~~~~~
cvenk.cpp: In function 'void solve()':
cvenk.cpp:120:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
120 | for(int i = 0; i < al.size(); ++i){
| ~~^~~~~~~~~~~
cvenk.cpp:146:22: warning: comparison of integer expressions of different signedness: 'int' and 'const long unsigned int' [-Wsign-compare]
146 | for(int i = 0; i < min(X[n].size(), X[idx].size()); ++i){
| ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cvenk.cpp:134:7: warning: unused variable 'cur' [-Wunused-variable]
134 | int cur = 0, x = 0, y = 0;
| ^~~
cvenk.cpp:134:16: warning: unused variable 'x' [-Wunused-variable]
134 | int cur = 0, x = 0, y = 0;
| ^
cvenk.cpp:134:23: warning: unused variable 'y' [-Wunused-variable]
134 | int cur = 0, x = 0, y = 0;
| ^
cvenk.cpp: In function 'int main()':
cvenk.cpp:162:15: warning: unused variable 'aa' [-Wunused-variable]
162 | int tt = 1, aa;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
188 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
187 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
192 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
182 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |