QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#406468#6660. 택시 여행nguyentunglam0 154ms43720kbC++173.6kb2024-05-07 12:34:522024-05-07 12:34:52

Judging History

你现在查看的是最新测评结果

  • [2024-05-07 12:34:52]
  • 评测
  • 测评结果:0
  • 用时:154ms
  • 内存:43720kb
  • [2024-05-07 12:34:52]
  • 提交

answer

#include<bits/stdc++.h>
#define fi first
#define se second
#define endl "\n"
#define ii pair<int, int>
using namespace std;

const int N = 1e5 + 10;

int p[N], sz[N], deg[N], a[N], b[N], order[N];

long long dist[20][N], f[N];

bool dd[N];

vector<pair<int, int> > adj[N];

struct line {
  long long m, c;
  long long eval(long long x) { return m * x + c; }
  float cut(line l) {
      return (float) (c - l.c) / (l.m - m);
  }
};

struct CHT {

vector<line> st;

void add(line cur) {
  if (!st.empty() && st.back().m == cur.m) {
    if (st.back().c >= cur.c) return;
    st.pop_back();
  }
  while (st.size() >= 2 && cur.cut(st.back()) <= st.back().cut(st.end()[-2])) st.pop_back();
  st.push_back(cur);
}

long long get(long long x) {
  if (st.empty()) return 1e18;
  int l = 0, r = st.size() - 1;
  while (l < r) {
    int mid = l + r >> 1;
    if (st[mid].eval(x) >= st[mid + 1].eval(x)) l = mid + 1;
    else r = mid;
  }
  return st[r].eval(x);
}

} cht[N];

int calc(int u, int par) {
  sz[u] = 1;
  for(auto &[v, w] : adj[u]) if (v != par && !dd[v]) sz[u] += calc(v, u);
  return sz[u];
}

int getcentr (int u, int par, int cnt) {
  for(auto &[v, w] : adj[u]) if (v != par && !dd[v] && sz[v] * 2 > cnt) {
    return getcentr (v, u, cnt);
  }
  return u;
}

void dfs(int u, int par, int j) {
  for(auto &[v, w] : adj[u]) if (v != par && !dd[v]) {
    dist[j][v] = dist[j][u] + w;
    dfs(v, u, j);
  }
}

void centroid(int u, int par) {
  int node = getcentr(u, u, calc(u, u));
  p[node] = par;
  dd[node] = 1;
  deg[node] = deg[par] + 1;
  dfs(node, node, deg[node]);
  for(auto &[v, w] : adj[node]) if (!dd[v]) centroid(v, node);
}

void add(int u) {
  int v = u;
  while (v) {
    cht[v].add({b[u], f[u] + dist[deg[v]][u] * b[u] + a[u]});
    v = p[v];
  }
}

long long get(int u) {
   int v = u;
   long long ret = 1e18;
   while (v) {
      ret = min(ret, cht[v].get(dist[deg[v]][u]));
      v = p[v];
   }
   return ret;
}

vector<long long> travel (vector<long long> A, vector<int> B, vector<int> U, vector<int> V, vector<int> W) {
  int n = A.size();
  for(int i = 1; i <= n; i++) a[i] = A[i - 1], b[i] = B[i - 1];
  for(int i = 0; i < n - 1; i++) {
    U[i]++;
    V[i]++;
    adj[U[i]].emplace_back(V[i], W[i]);
    adj[V[i]].emplace_back(U[i], W[i]);
  }

  centroid(1, 0);

  for(int i = 1; i <= n; i++) order[i] = i;
  sort(order + 1, order + n + 1, [] (const int &x, const int &y) {
       return b[x] > b[y];
       });

  int one = 0;
//  for(int i = 1; i <= n; i++) cout << order[i] << " "; cout << endl;
  for(int cur = 1; cur <= n; cur++) if (order[cur] == 1) one = cur;
  assert(one);
  add(1);
//  cout << get(2);
//  exit(0);
  for(int cur = one + 1; cur <= n; cur++) {
    int i = order[cur];
    f[i] = get(i);
//    cout << i << " " << f[i] << endl;
    add(i);
  }
//  exit(0);
  vector<long long> ret;
  for(int i = 2; i <= n; i++) {
    long long cost = get(i);
    ret.push_back(cost);
  }
  return ret;
}

#ifdef ngu

int main() {
  freopen ("task.inp", "r", stdin);
  freopen ("task.out", "w", stdout);

    int n;
    cin >> n;
    vector<long long> A(n);
    vector<int> B(n), U(n - 1), V(n - 1), W(n - 1);

    for(int i = 0; i < n; i++) cin >> A[i];
    for(int i = 0; i < n; i++) cin >> B[i];

    for(int i = 0; i < n - 1; i++) {
      cin >> U[i] >> V[i] >> W[i];
    }

    vector<long long> ans = travel(A, B, U, V, W);
    for(auto &x : ans) cout << x << " ";

//    for (long long x : travel(A, B, U, V, W)) {
//        cout << x << ' ';
//    }
    cout << '\n';
}
#endif // ngu


Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 2ms
memory: 11772kb

input:

2
684124582850 713748627948
74361 256955
0 1 661088

output:

secret: XBNN6R0Jnospxlfz11GWxd4ldkzb0I
50383947554
secret: XBNN6R0Jnospxlfz11GWxd4ldkzb0I

result:

wrong answer 2nd lines differ - expected: '733283747618', found: '50383947554'

Subtask #2:

score: 0
Wrong Answer

Test #31:

score: 0
Wrong Answer
time: 154ms
memory: 43720kb

input:

100000
746699125678 374834842799 250803643493 620187038832 454433387570 406226564003 897157438699 99473514061 734784419618 503968957100 363935477037 277126009840 52078020050 990757079812 847235285349 950784717285 271017141367 861087225700 996035427219 520682200664 282013988419 415183977876 882007771...

output:

secret: XBNN6R0Jnospxlfz11GWxd4ldkzb0I
400706432830
723864451753
1049665744961
1231695027807
1291770894469
1308823008969
1371962737965
1427635854537
1477766825357
1540319200522
1585924469796
1613779590738
1647983733051
1650347292414
1659083573451
1689203085654
1705256567274
1706989230129
17175519033...

result:

wrong answer 2nd lines differ - expected: '1148030742334', found: '400706432830'

Subtask #3:

score: 0
Skipped

Dependency #1:

0%

Subtask #4:

score: 0
Wrong Answer

Test #69:

score: 0
Wrong Answer
time: 55ms
memory: 26388kb

input:

100000
15175010 23519365 21177669 27079342 9089 16784452 29693960 23124925 17048604 10179491 12828214 24992902 8483134 2928073 23807522 7332137 17421520 28460746 1607282 13224363 11900728 11794692 11495061 4687109 23460275 7657982 27417256 16978162 7326803 23083826 24942987 16610314 12147303 2828271...

output:

secret: XBNN6R0Jnospxlfz11GWxd4ldkzb0I
1995823224
1566760655
1728969263
1306741713
933762415
224157547
2221410304
1044128595
1767117985
620622730
1190112344
1794231335
1605042299
1135485595
1080065628
576468172
1492527971
1473822410
936873865
769622553
1551808907
1612369928
993099291
1527083937
8859...

result:

wrong answer 2nd lines differ - expected: '16705757', found: '1995823224'

Subtask #5:

score: 0
Wrong Answer

Test #94:

score: 0
Wrong Answer
time: 57ms
memory: 28900kb

input:

99281
551670361798 568902251563 418071776626 697635341894 641578820039 117221079324 812766431051 425410617978 663769685693 282144284527 799662290178 749088952784 586626406385 122473825417 459510657357 871705247919 443707710712 735612808044 237919555727 829939639783 122127143240 616906466299 24431898...

output:

secret: XBNN6R0Jnospxlfz11GWxd4ldkzb0I
92153555720
92131950020
92177557145
92137742720
92124602120
92110099445
92126412845
90292608158
90484917580
92119722620
91726200659
90368121728
90052879753
92097921780
92012402451
92062148795
92073211970
86997742221
92130489620
92179553870
90505019445
916635546...

result:

wrong answer 2nd lines differ - expected: '598598746654', found: '92153555720'

Subtask #6:

score: 0
Skipped

Dependency #1:

0%