Submission #1103578

# Submission time Handle Problem Language Result Execution time Memory
1103578 2024-10-21T10:17:29 Z duckindog Nile (IOI24_nile) C++17
100 / 100
190 ms 23624 KB
#include <bits/stdc++.h>
 
using namespace std;

#ifndef LOCAL
#include "nile.h"
#endif
 
const int N = 100'000 + 10;
struct DSU { 
  int id[N];
 
  DSU() { 
    memset(id, -1, sizeof id);
  }
 
  int root(int u) { return id[u] < 0 ? u : id[u] = root(id[u]); }
  void add(int u, int v) { 
    u = root(u); v = root(v);
    if (u == v) return;
    id[u] += id[v];
    id[v] = u;
  }
} fwd, bwd;
 
int n;
struct Artifact { 
  int w, a, b;
} p;
 
const long long MAX = 1'000'000'000'000'000;
struct IT { 
  long long f[N << 2];
 
  IT() { 
    memset(f, 14, sizeof f);
  }
 
  void update(int s, int l, int r, int p, int x) { 
    if (p < l || p > r) return;
    if (l == r) { 
      f[s] = x;
      return;
    }
    int mid = (l + r) >> 1;
    update(s << 1, l, mid, p, x); update(s << 1 | 1, mid + 1, r, p, x);
    f[s] = min(f[s << 1], f[s << 1 | 1]);
  }
  long long query(int s, int l, int r, int u, int v) { 
    if (v < l || u > r) return MAX;
    if (u <= l && r <= v) return f[s];
    int mid = (l + r) >> 1;
    return min(query(s << 1, l, mid, u, v), query(s << 1 | 1, mid + 1, r, u, v));
  }
} T[2], D[2];
 
using Tp = tuple<int, int, int>;
priority_queue<Tp, vector<Tp>, greater<>> s;
using pii = pair<int, int>;
priority_queue<pii, vector<pii>, greater<>> f[2];
 
vector<long long> calculate_costs(vector<int> W, vector<int> A,
                                  vector<int> B, vector<int> E) {
  int n = W.size();
  vector<Artifact> p(n);
  for (int i = 0; i < n; ++i) p[i] = {W[i], A[i], B[i]}; 
  
  int q = E.size();
  vector<pair<int, int>> queries(q);
  for (int i = 0; i < q; ++i) queries[i] = {E[i], i};
  
  vector<long long> answer(q);
  
  sort(queries.begin(), queries.end());
  sort(p.begin(), p.end(), [&](const auto& a, const auto& b) { return a.w > b.w; });
  vector<long long> d(n);
  for (int i = 0; i < n; ++i) { 
    if (i) d[i] = d[i - 1];
    d[i] += p[i].b;
  }
  
  auto cal = [&](int l, int r) { 
    if (r - l + 1 == 1) return 1ll * p[l].a;
    if (r - l + 1 == 2) return 1ll * p[l].b + p[r].b;
    
    long long value = d[r] - d[l] + p[l].b;
    if (!((r - l + 1) & 1)) return value;
    int type = l & 1;
    return value + min(T[type].query(1, 0, n - 1, l, r), 
                        D[type ^ 1].query(1, 0, n - 1, l + 1, r - 1));
  };
  
  for (int i = 1; i < n; ++i) s.push(make_tuple(p[i - 1].w - p[i].w, i - 1, i));
  for (int i = 1; i < n - 1; ++i) f[i & 1].push(make_pair(p[i - 1].w - p[i + 1].w, i));
  for (int i = 0; i < n; ++i) T[i & 1].update(1, 0, n - 1, i, p[i].a - p[i].b);
 
  long long total = 0;
  for (int i = 0; i < n; ++i) total += cal(i, i);
  for (int i = 0; i < q; ++i) {
    const auto& [d, idx] = queries[i];
 
    { //update
      while (s.size() && get<0>(s.top()) <= d) { 
        auto [cmp, r1, l2] = s.top(); s.pop();
        int l1 = bwd.root(r1), r2 = fwd.root(l2);
 
        total -= cal(l1, r1);     
        total -= cal(l2, r2);
        
        bwd.add(r1, l2); 
        fwd.add(l2, r1);
 
        int l = bwd.root(r1), r = fwd.root(l2);
        total += cal(l, r);     
      }
 
      for (int type = 0; type <= 1; ++type) { 
        while (f[type].size() && get<0>(f[type].top()) <= d) { 
          auto [cmp, pos] = f[type].top(); f[type].pop();
          int l = bwd.root(pos), r = fwd.root(pos);
          
          total -= cal(l, r);
          D[type].update(1, 0, n - 1, pos, p[pos].a - p[pos].b);
          total += cal(l, r);
        }
      }
    }
    answer[idx] = total;
  }
  return answer;
}

#ifdef LOCAL
int32_t main() {
  int N;
  assert(1 == scanf("%d", &N));
  std::vector<int> W(N), A(N), B(N);
  for (int i = 0; i < N; i++)
    assert(3 == scanf("%d%d%d", &W[i], &A[i], &B[i]));
  int Q;
  assert(1 == scanf("%d", &Q));
  std::vector<int> E(Q);
  for (int j = 0; j < Q; j++)
    assert(1 == scanf("%d", &E[j]));
  fclose(stdin);
 
  std::vector<long long> R = calculate_costs(W, A, B, E);
 
  int S = (int)R.size();
  for (int j = 0; j < S; j++)
    printf("%lld\n", R[j]);
  fclose(stdout);
 
  return 0;
}
#endif
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 14076 KB Output is correct
2 Correct 5 ms 13904 KB Output is correct
3 Correct 4 ms 13904 KB Output is correct
4 Correct 4 ms 13904 KB Output is correct
5 Correct 4 ms 13916 KB Output is correct
6 Correct 4 ms 13904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 115 ms 21948 KB Output is correct
2 Correct 108 ms 22036 KB Output is correct
3 Correct 103 ms 21952 KB Output is correct
4 Correct 83 ms 21952 KB Output is correct
5 Correct 83 ms 22192 KB Output is correct
6 Correct 80 ms 22020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 103 ms 21184 KB Output is correct
2 Correct 103 ms 21440 KB Output is correct
3 Correct 113 ms 20416 KB Output is correct
4 Correct 113 ms 20416 KB Output is correct
5 Correct 85 ms 20936 KB Output is correct
6 Correct 100 ms 20396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 14076 KB Output is correct
2 Correct 5 ms 13904 KB Output is correct
3 Correct 4 ms 13904 KB Output is correct
4 Correct 4 ms 13904 KB Output is correct
5 Correct 4 ms 13916 KB Output is correct
6 Correct 4 ms 13904 KB Output is correct
7 Correct 4 ms 13648 KB Output is correct
8 Correct 6 ms 13860 KB Output is correct
9 Correct 4 ms 13648 KB Output is correct
10 Correct 4 ms 13648 KB Output is correct
11 Correct 5 ms 13648 KB Output is correct
12 Correct 5 ms 13648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 14076 KB Output is correct
2 Correct 5 ms 13904 KB Output is correct
3 Correct 4 ms 13904 KB Output is correct
4 Correct 4 ms 13904 KB Output is correct
5 Correct 4 ms 13916 KB Output is correct
6 Correct 4 ms 13904 KB Output is correct
7 Correct 115 ms 21948 KB Output is correct
8 Correct 108 ms 22036 KB Output is correct
9 Correct 103 ms 21952 KB Output is correct
10 Correct 83 ms 21952 KB Output is correct
11 Correct 83 ms 22192 KB Output is correct
12 Correct 80 ms 22020 KB Output is correct
13 Correct 103 ms 21184 KB Output is correct
14 Correct 103 ms 21440 KB Output is correct
15 Correct 113 ms 20416 KB Output is correct
16 Correct 113 ms 20416 KB Output is correct
17 Correct 85 ms 20936 KB Output is correct
18 Correct 100 ms 20396 KB Output is correct
19 Correct 4 ms 13648 KB Output is correct
20 Correct 6 ms 13860 KB Output is correct
21 Correct 4 ms 13648 KB Output is correct
22 Correct 4 ms 13648 KB Output is correct
23 Correct 5 ms 13648 KB Output is correct
24 Correct 5 ms 13648 KB Output is correct
25 Correct 112 ms 21184 KB Output is correct
26 Correct 112 ms 21696 KB Output is correct
27 Correct 112 ms 20428 KB Output is correct
28 Correct 129 ms 20424 KB Output is correct
29 Correct 105 ms 20928 KB Output is correct
30 Correct 110 ms 20484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 103 ms 21184 KB Output is correct
2 Correct 103 ms 21440 KB Output is correct
3 Correct 113 ms 20416 KB Output is correct
4 Correct 113 ms 20416 KB Output is correct
5 Correct 85 ms 20936 KB Output is correct
6 Correct 100 ms 20396 KB Output is correct
7 Correct 100 ms 23492 KB Output is correct
8 Correct 111 ms 23624 KB Output is correct
9 Correct 130 ms 22720 KB Output is correct
10 Correct 130 ms 22720 KB Output is correct
11 Correct 128 ms 22720 KB Output is correct
12 Correct 126 ms 22724 KB Output is correct
13 Correct 136 ms 22728 KB Output is correct
14 Correct 106 ms 23232 KB Output is correct
15 Correct 128 ms 22716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13648 KB Output is correct
2 Correct 5 ms 14076 KB Output is correct
3 Correct 5 ms 13904 KB Output is correct
4 Correct 4 ms 13904 KB Output is correct
5 Correct 4 ms 13904 KB Output is correct
6 Correct 4 ms 13916 KB Output is correct
7 Correct 4 ms 13904 KB Output is correct
8 Correct 115 ms 21948 KB Output is correct
9 Correct 108 ms 22036 KB Output is correct
10 Correct 103 ms 21952 KB Output is correct
11 Correct 83 ms 21952 KB Output is correct
12 Correct 83 ms 22192 KB Output is correct
13 Correct 80 ms 22020 KB Output is correct
14 Correct 103 ms 21184 KB Output is correct
15 Correct 103 ms 21440 KB Output is correct
16 Correct 113 ms 20416 KB Output is correct
17 Correct 113 ms 20416 KB Output is correct
18 Correct 85 ms 20936 KB Output is correct
19 Correct 100 ms 20396 KB Output is correct
20 Correct 4 ms 13648 KB Output is correct
21 Correct 6 ms 13860 KB Output is correct
22 Correct 4 ms 13648 KB Output is correct
23 Correct 4 ms 13648 KB Output is correct
24 Correct 5 ms 13648 KB Output is correct
25 Correct 5 ms 13648 KB Output is correct
26 Correct 112 ms 21184 KB Output is correct
27 Correct 112 ms 21696 KB Output is correct
28 Correct 112 ms 20428 KB Output is correct
29 Correct 129 ms 20424 KB Output is correct
30 Correct 105 ms 20928 KB Output is correct
31 Correct 110 ms 20484 KB Output is correct
32 Correct 100 ms 23492 KB Output is correct
33 Correct 111 ms 23624 KB Output is correct
34 Correct 130 ms 22720 KB Output is correct
35 Correct 130 ms 22720 KB Output is correct
36 Correct 128 ms 22720 KB Output is correct
37 Correct 126 ms 22724 KB Output is correct
38 Correct 136 ms 22728 KB Output is correct
39 Correct 106 ms 23232 KB Output is correct
40 Correct 128 ms 22716 KB Output is correct
41 Correct 115 ms 23488 KB Output is correct
42 Correct 120 ms 23588 KB Output is correct
43 Correct 190 ms 22724 KB Output is correct
44 Correct 152 ms 22720 KB Output is correct
45 Correct 154 ms 22812 KB Output is correct
46 Correct 173 ms 22720 KB Output is correct
47 Correct 164 ms 22720 KB Output is correct
48 Correct 122 ms 23232 KB Output is correct
49 Correct 177 ms 22716 KB Output is correct