답안 #395652

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
395652 2021-04-28T17:40:37 Z usachevd0 Road Construction (JOI21_road_construction) C++17
100 / 100
3025 ms 414432 KB
#pragma gcc optimize("Ofast")
#pragma gcc optimize("O3")
#pragma gcc optimize("fast-math")
#pragma gcc optimize("unroll-loops")
#pragma gcc optimize("no-stack-protector")
#pragma gcc target("avx,avx2,ffa,sse,sse2,sse4.2")
#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(a) (a).begin(), (a).end()
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
using ld = long double;
template<typename T1, typename T2> bool chkmin(T1 &x, T2 y) {
  return y < x ? (x = y, true) : false; 
}
template<typename T1, typename T2> bool chkmax(T1 &x, T2 y) {
  return y > x ? (x = y, true) : false;
}
void debug_out() {
  cerr << endl;
}
template<typename T1, typename... T2> void debug_out(T1 A, T2... B) {
  cerr << ' ' << A;
  debug_out(B...);
}
template<typename T> void mdebug_out(T* a, int n) {
  for (int i = 0; i < n; ++i) {
    cerr << a[i] << ' ';
  }
  cerr << endl;
}
#ifdef DEBUG
  #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
  #define mdebug(a, n) cerr << #a << ": ", mdebug_out(a, n)
#else
  #define debug(...) 1337
  #define mdebug(a, n) 1337
#endif
template<typename T> ostream& operator << (ostream& stream, const vector<T> &v) {
  for (auto& x : v) {
    stream << x << ' ';
  }
  return stream;
}
template<typename T1, typename T2> ostream& operator << (ostream& stream, const pair<T1, T2>& p) {
  return stream << p.first << ' ' << p.second;
}

const int INF32 = 2e9 + 1337 * 228;
const int maxN = 250010;
int n, K;
int X[maxN], Y[maxN];
pii ordX[maxN], ordY[maxN];
int posX[maxN], posY[maxN];

int sgt[maxN][2];

namespace psgt {
  const int maxV = 4e7 + 1337;
  int V = 1;
  int L[maxV], R[maxV];
  pii mn[maxV];

  void init() {
    V = 1;
    mn[0] = {INF32, -1};
  }
  int clone(int v) {
    L[V] = L[v];
    R[V] = R[v];
    mn[V] = mn[v];
    return V++;
  }
  int mdf(int v, int vl, int vr, int pos, int val) {
    int nv = clone(v);
    if (vl == vr) {
      mn[nv] = {val, vl};
      return nv;
    }
    int vm = (vl + vr) >> 1;
    if (pos <= vm) {
      L[nv] = mdf(L[nv], vl, vm, pos, val);
    } else {
      R[nv] = mdf(R[nv], vm + 1, vr, pos, val);
    }
    mn[nv] = min(mn[L[nv]], mn[R[nv]]);
    return nv;
  }
  pii rmq(int v, int vl, int vr, int l, int r) {
    if (l > r || vr < l || r < vl) return {INF32, -1};
    if (l <= vl && vr <= r) return mn[v];
    int vm = (vl + vr) >> 1;
    return min(rmq(L[v], vl, vm, l, r), rmq(R[v], vm + 1, vr, l, r));
  }
}

void sdebug(int v) {
  for (int i = 0; i < n; ++i) {
    cerr << psgt::rmq(v, 0, n - 1, i, i).fi << ' ';
  }
  cerr << endl;
}

int32_t main() {
#ifdef DEBUG
  freopen("in", "r", stdin);
#endif
  ios::sync_with_stdio(0);
  cin.tie(0);

  psgt::init();
  cin >> n >> K;
  for (int i = 0; i < n; ++i) {
    cin >> X[i] >> Y[i];
    ordX[i] = {X[i], i};
    ordY[i] = {Y[i], i};
  }
  sort(ordX, ordX + n);
  for (int i = 0; i < n; ++i) {
    posX[ordX[i].se] = i;
  }
  sort(ordY, ordY + n);
  for (int i = 0; i < n; ++i) {
    posY[ordY[i].se] = i;
  }
  //mdebug(ordX, n);
  //mdebug(ordY, n);
  
  multiset<pair<ll, pii>> pq;
  for (int t = 0; t < n; ++t) {
    int i = ordX[t].se;
    sgt[t + 1][0] = psgt::mdf(sgt[t][0], 0, n - 1, posY[i], -X[i] + Y[i]);
    sgt[t + 1][1] = psgt::mdf(sgt[t][1], 0, n - 1, posY[i], -X[i] - Y[i]);
    //debug(t);
    //sdebug(sgt[t + 1][0]);
    //sdebug(sgt[t + 1][1]);
    {
      auto res = psgt::rmq(sgt[t][0], 0, n - 1, posY[i] + 1, n - 1);
      if (res.fi < INF32) {
        sgt[t][0] = psgt::mdf(sgt[t][0], 0, n - 1, res.se, INF32);
        pq.insert(mp(res.fi + (ll)X[i] - (ll)Y[i], mp(i, 0)));
      }
    }
    {
      auto res = psgt::rmq(sgt[t][1], 0, n - 1, 0, posY[i] - 1);
      if (res.fi < INF32) {
        sgt[t][1] = psgt::mdf(sgt[t][1], 0, n - 1, res.se, INF32);
        pq.insert(mp(res.fi + (ll)X[i] + (ll)Y[i], mp(i, 1)));
      }
    }
  }
  for (int e = 0; e < K; ++e) {
    cout << pq.begin()->fi << '\n';
    int i = pq.begin()->se.fi;
    int d = pq.begin()->se.se;
    pq.erase(pq.begin());
    int& ver = sgt[posX[i]][d];
    if (d == 0) {
      auto res = psgt::rmq(ver, 0, n - 1, posY[i] + 1, n - 1);
      if (res.fi < INF32) {
        //sdebug(ver);
        //debug(res.se);
        ver = psgt::mdf(ver, 0, n - 1, res.se, INF32);
        //sdebug(ver);
        pq.insert(mp((ll)res.fi + (ll)X[i] - (ll)Y[i], mp(i, 0)));
      }
    } else {
      auto res = psgt::rmq(ver, 0, n - 1, 0, posY[i] - 1);
      if (res.fi < INF32) {
        //sdebug(ver);
        //debug(res);
        ver = psgt::mdf(ver, 0, n - 1, res.se, INF32);
        //sdebug(ver);
        pq.insert(mp((ll)res.fi + (ll)X[i] + (ll)Y[i], mp(i, 1)));
      }
    }
  }
  
    
  return 0;
}

Compilation message

road_construction.cpp:1: warning: ignoring #pragma gcc optimize [-Wunknown-pragmas]
    1 | #pragma gcc optimize("Ofast")
      | 
road_construction.cpp:2: warning: ignoring #pragma gcc optimize [-Wunknown-pragmas]
    2 | #pragma gcc optimize("O3")
      | 
road_construction.cpp:3: warning: ignoring #pragma gcc optimize [-Wunknown-pragmas]
    3 | #pragma gcc optimize("fast-math")
      | 
road_construction.cpp:4: warning: ignoring #pragma gcc optimize [-Wunknown-pragmas]
    4 | #pragma gcc optimize("unroll-loops")
      | 
road_construction.cpp:5: warning: ignoring #pragma gcc optimize [-Wunknown-pragmas]
    5 | #pragma gcc optimize("no-stack-protector")
      | 
road_construction.cpp:6: warning: ignoring #pragma gcc target [-Wunknown-pragmas]
    6 | #pragma gcc target("avx,avx2,ffa,sse,sse2,sse4.2")
      |
# 결과 실행 시간 메모리 Grader output
1 Correct 433 ms 46664 KB Output is correct
2 Correct 373 ms 46592 KB Output is correct
3 Correct 305 ms 44616 KB Output is correct
4 Correct 318 ms 44584 KB Output is correct
5 Correct 339 ms 45516 KB Output is correct
6 Correct 4 ms 1484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2939 ms 413444 KB Output is correct
2 Correct 3025 ms 413432 KB Output is correct
3 Correct 319 ms 44388 KB Output is correct
4 Correct 2271 ms 413272 KB Output is correct
5 Correct 2497 ms 413592 KB Output is correct
6 Correct 2469 ms 413588 KB Output is correct
7 Correct 2508 ms 412716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1784 ms 338076 KB Output is correct
2 Correct 1724 ms 338096 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1622 ms 338032 KB Output is correct
5 Correct 1507 ms 338092 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1784 ms 338076 KB Output is correct
2 Correct 1724 ms 338096 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1622 ms 338032 KB Output is correct
5 Correct 1507 ms 338092 KB Output is correct
6 Correct 1710 ms 338160 KB Output is correct
7 Correct 1717 ms 338028 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1686 ms 337976 KB Output is correct
11 Correct 1606 ms 338128 KB Output is correct
12 Correct 1545 ms 338056 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 433 ms 46664 KB Output is correct
2 Correct 373 ms 46592 KB Output is correct
3 Correct 305 ms 44616 KB Output is correct
4 Correct 318 ms 44584 KB Output is correct
5 Correct 339 ms 45516 KB Output is correct
6 Correct 4 ms 1484 KB Output is correct
7 Correct 1683 ms 198800 KB Output is correct
8 Correct 1671 ms 198752 KB Output is correct
9 Correct 300 ms 44644 KB Output is correct
10 Correct 996 ms 198136 KB Output is correct
11 Correct 1228 ms 198072 KB Output is correct
12 Correct 1035 ms 198432 KB Output is correct
13 Correct 1225 ms 197360 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 433 ms 46664 KB Output is correct
2 Correct 373 ms 46592 KB Output is correct
3 Correct 305 ms 44616 KB Output is correct
4 Correct 318 ms 44584 KB Output is correct
5 Correct 339 ms 45516 KB Output is correct
6 Correct 4 ms 1484 KB Output is correct
7 Correct 2939 ms 413444 KB Output is correct
8 Correct 3025 ms 413432 KB Output is correct
9 Correct 319 ms 44388 KB Output is correct
10 Correct 2271 ms 413272 KB Output is correct
11 Correct 2497 ms 413592 KB Output is correct
12 Correct 2469 ms 413588 KB Output is correct
13 Correct 2508 ms 412716 KB Output is correct
14 Correct 1784 ms 338076 KB Output is correct
15 Correct 1724 ms 338096 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1622 ms 338032 KB Output is correct
18 Correct 1507 ms 338092 KB Output is correct
19 Correct 1710 ms 338160 KB Output is correct
20 Correct 1717 ms 338028 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1686 ms 337976 KB Output is correct
24 Correct 1606 ms 338128 KB Output is correct
25 Correct 1545 ms 338056 KB Output is correct
26 Correct 1683 ms 198800 KB Output is correct
27 Correct 1671 ms 198752 KB Output is correct
28 Correct 300 ms 44644 KB Output is correct
29 Correct 996 ms 198136 KB Output is correct
30 Correct 1228 ms 198072 KB Output is correct
31 Correct 1035 ms 198432 KB Output is correct
32 Correct 1225 ms 197360 KB Output is correct
33 Correct 2957 ms 414372 KB Output is correct
34 Correct 2945 ms 414432 KB Output is correct
35 Correct 2155 ms 413604 KB Output is correct
36 Correct 2088 ms 413568 KB Output is correct
37 Correct 2101 ms 413820 KB Output is correct
38 Correct 2417 ms 412960 KB Output is correct