답안 #846550

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
846550 2023-09-07T21:14:49 Z mychecksedad ČVENK (COI15_cvenk) C++17
60 / 100
3000 ms 306876 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, deque<int>> co, ro;
map<pair<int, int>, int> is;

ll r(ll n){
  if(n==0) return INF;
  return n & -n;
}
bool ok = 1;
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)));
    if(cost == 0) co[y].pb(x);
    ro[x-c].pb(y);
    return cost + f(x - c, y, c, g);
  }
  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, g); 
}

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}]++;
  }en;


  ll ans = 0;
  for(int i = 0; i < n; ++i){
    vector<array<ll, 3>> X;
    f(a[i].x, a[i].y, 0, X);
    ok = 0;
  }

  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);

  vector<array<ll, 3>> Y;
  f(centro.x, centro.y, 0, 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

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:143:22: warning: comparison of integer expressions of different signedness: 'int' and 'const long unsigned int' [-Wsign-compare]
  143 |     for(int i = 0; i < min(Y.size(), X.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:159:15: warning: unused variable 'aa' [-Wunused-variable]
  159 |   int tt = 1, aa;
      |               ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 856 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 81 ms 6224 KB Output is correct
2 Correct 79 ms 6140 KB Output is correct
3 Correct 77 ms 3744 KB Output is correct
4 Correct 50 ms 3664 KB Output is correct
5 Correct 45 ms 4344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1500 ms 306816 KB Output is correct
2 Correct 1535 ms 306876 KB Output is correct
3 Execution timed out 3045 ms 12360 KB Time limit exceeded
4 Halted 0 ms 0 KB -