Submission #846563

# Submission time Handle Problem Language Result Execution time Memory
846563 2023-09-07T22:02:02 Z mychecksedad ČVENK (COI15_cvenk) C++17
100 / 100
2738 ms 235548 KB
/* 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, vector<int>> co, ro;
pair<vector<int>, 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 = CO[mappedy].second; i < CO[mappedy].first.size(); ++i){
    if(CO[mappedy].first[i] <= x) CO[mappedy].second = i + 1;
    else if(CO[mappedy].first[i] > x + mxdown){
      break;
    }else down++;
  }
  for(int i = RO[mappedx].second; i < RO[mappedx].first.size(); ++i){
    if(RO[mappedx].first[i] <= y) RO[mappedx].second = i + 1;
    else if(RO[mappedx].first[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].first[CO[mappedy].second], y, w + s + right);
  }
  if(down + s + w < right){
    return dfs(x, RO[mappedx].first[RO[mappedx].second], 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].first.swap(co[al[i]]);
    RO[i+1].first.swap(ro[al[i]]);
    // co[al[i]].clear();
    // ro[al[i]].clear();
    sort(all(CO[i+1].first));
    sort(all(RO[i+1].first));
  }

  // 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:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   72 |   for(int i = CO[mappedy].second; i < CO[mappedy].first.size(); ++i){
      |                                   ~~^~~~~~~~~~~~~~~~~~~~~~~~~~
cvenk.cpp:78:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   78 |   for(int i = RO[mappedx].second; i < RO[mappedx].first.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:148:22: warning: comparison of integer expressions of different signedness: 'int' and 'const long unsigned int' [-Wsign-compare]
  148 |     for(int i = 0; i < min(X[n].size(), X[idx].size()); ++i){
      |                    ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cvenk.cpp:136:7: warning: unused variable 'cur' [-Wunused-variable]
  136 |   int cur = 0, x = 0, y = 0;
      |       ^~~
cvenk.cpp:136:16: warning: unused variable 'x' [-Wunused-variable]
  136 |   int cur = 0, x = 0, y = 0;
      |                ^
cvenk.cpp:136:23: warning: unused variable 'y' [-Wunused-variable]
  136 |   int cur = 0, x = 0, y = 0;
      |                       ^
cvenk.cpp: In function 'int main()':
cvenk.cpp:164:15: warning: unused variable 'aa' [-Wunused-variable]
  164 |   int tt = 1, aa;
      |               ^~
# Verdict Execution time Memory Grader output
1 Correct 20 ms 86616 KB Output is correct
2 Correct 18 ms 86620 KB Output is correct
3 Correct 19 ms 86740 KB Output is correct
4 Correct 17 ms 86620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 86872 KB Output is correct
2 Correct 18 ms 86620 KB Output is correct
3 Correct 17 ms 86620 KB Output is correct
4 Correct 18 ms 86620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 121 ms 112576 KB Output is correct
2 Correct 122 ms 112664 KB Output is correct
3 Correct 71 ms 105204 KB Output is correct
4 Correct 68 ms 104584 KB Output is correct
5 Correct 73 ms 104644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 927 ms 235024 KB Output is correct
2 Correct 931 ms 235548 KB Output is correct
3 Correct 2738 ms 133204 KB Output is correct
4 Correct 159 ms 127344 KB Output is correct
5 Correct 199 ms 126172 KB Output is correct