Submission #1105027

# Submission time Handle Problem Language Result Execution time Memory
1105027 2024-10-25T07:56:29 Z flashmt Nile (IOI24_nile) C++17
100 / 100
268 ms 35900 KB
#include <bits/stdc++.h>
using namespace std;
const int oo = 1 << 30;

template<typename T>
struct SegmentTree
{
  int low, mid, high;
  T value;
  SegmentTree *l, *r;

  SegmentTree(int low, int high, vector<T> &initial): low(low), high(high)
  {
    mid = (low + high) / 2;
    if (low == high)
    {
      l = r = NULL;
      value = initial[low];
    }
    else
    {
      l = new SegmentTree(low, mid, initial);
      r = new SegmentTree(mid + 1, high, initial);
      value = min(l->value, r->value);
    }
  }

  void update(int x, T v)
  {
    if (low == high)
    {
      value = v;
      return;
    }
    if (x <= mid) l->update(x, v);
    else r->update(x, v);
    value = min(l->value, r->value);
  }

  T get(int x, int y)
  {
    if (low == x && high == y)
      return value;
    if (y <= mid) return l->get(x, y);
    else if (x > mid) return r->get(x, y);
    else return min(l->get(x, mid), r->get(mid + 1, y));
  }
};

vector<long long> calculate_costs(vector<int> w, vector<int> a, vector<int> b, vector<int> e)
{
  int n = size(w);
  long long curAns = accumulate(begin(a), end(a), 0LL);
  vector<pair<int, int>> c;
  for (int i = 0; i < n; i++)
    c.push_back({w[i], a[i] - b[i]});
  sort(begin(c), end(c));
  vector<pair<int, int>> diffs;
  for (int i = 0; i < n - 1; i++)
  {
    diffs.push_back({c[i + 1].first - c[i].first, i});
    if (i < n - 2)
      diffs.push_back({c[i + 2].first - c[i].first, i + n - 1});
  }
  sort(begin(diffs), end(diffs));
  set<int> active;
  for (int i = -1; i < n; i++)
    active.insert(i);

  vector<int> odd, even;
  for (int i = 0; i < n; i++)
    if (i % 2)
    {
      odd.push_back(c[i].second);
      even.push_back(oo);
    }
    else
    {
      odd.push_back(oo);
      even.push_back(c[i].second);
    }

  SegmentTree<int> oddTree(0, n - 1, odd);
  SegmentTree<int> evenTree(0, n - 1, even);

  map<int, long long> ansMap;
  ansMap[-1] = curAns;

  auto getScore = [&](int i)
  {
    auto u = active.lower_bound(i);
    int r = *u;
    u--;
    int l = *u + 1;
    int len = r - l + 1;
    if (len % 2 == 0)
      return 0;
    return l % 2 ? oddTree.get(l, r) : evenTree.get(l, r);
  };

  for (int i = 0; i < size(diffs);)
  {
    auto [curDiff, _] = diffs[i];
    int j = i;
    while (j < size(diffs))
    {
      auto [d, id] = diffs[j];
      if (d > curDiff)
        break;

      if (id < n - 1)
      {
        curAns -= getScore(id) + getScore(id + 1);
        active.erase(id);
        curAns += getScore(id);
      }
      else
      {
        id -= n - 1;
        curAns -= getScore(id);
        if (id % 2) oddTree.update(id + 1, c[id + 1].second);
        else evenTree.update(id + 1, c[id + 1].second);
        curAns += getScore(id);
      }

      j++;
    }

    i = j;
    ansMap[curDiff] = curAns;
  }

  vector<long long> ans;
  for (int d : e)
  {
    auto it = ansMap.upper_bound(d);
    it--;
    ans.push_back(it->second);
  }
  return ans;
}

Compilation message

nile.cpp: In function 'std::vector<long long int> calculate_costs(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
nile.cpp:101:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  101 |   for (int i = 0; i < size(diffs);)
      |                   ~~^~~~~~~~~~~~~
nile.cpp:105:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  105 |     while (j < size(diffs))
      |            ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 864 KB Output is correct
2 Correct 3 ms 1012 KB Output is correct
3 Correct 3 ms 848 KB Output is correct
4 Correct 2 ms 1012 KB Output is correct
5 Correct 4 ms 848 KB Output is correct
6 Correct 3 ms 848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 103 ms 32312 KB Output is correct
2 Correct 104 ms 32424 KB Output is correct
3 Correct 101 ms 32320 KB Output is correct
4 Correct 127 ms 32056 KB Output is correct
5 Correct 106 ms 32312 KB Output is correct
6 Correct 104 ms 32312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 120 ms 31028 KB Output is correct
2 Correct 138 ms 31376 KB Output is correct
3 Correct 234 ms 31440 KB Output is correct
4 Correct 232 ms 31288 KB Output is correct
5 Correct 154 ms 31188 KB Output is correct
6 Correct 242 ms 31292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 864 KB Output is correct
2 Correct 3 ms 1012 KB Output is correct
3 Correct 3 ms 848 KB Output is correct
4 Correct 2 ms 1012 KB Output is correct
5 Correct 4 ms 848 KB Output is correct
6 Correct 3 ms 848 KB Output is correct
7 Correct 3 ms 1104 KB Output is correct
8 Correct 3 ms 1104 KB Output is correct
9 Correct 5 ms 1104 KB Output is correct
10 Correct 5 ms 1104 KB Output is correct
11 Correct 3 ms 848 KB Output is correct
12 Correct 5 ms 1104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 864 KB Output is correct
2 Correct 3 ms 1012 KB Output is correct
3 Correct 3 ms 848 KB Output is correct
4 Correct 2 ms 1012 KB Output is correct
5 Correct 4 ms 848 KB Output is correct
6 Correct 3 ms 848 KB Output is correct
7 Correct 103 ms 32312 KB Output is correct
8 Correct 104 ms 32424 KB Output is correct
9 Correct 101 ms 32320 KB Output is correct
10 Correct 127 ms 32056 KB Output is correct
11 Correct 106 ms 32312 KB Output is correct
12 Correct 104 ms 32312 KB Output is correct
13 Correct 120 ms 31028 KB Output is correct
14 Correct 138 ms 31376 KB Output is correct
15 Correct 234 ms 31440 KB Output is correct
16 Correct 232 ms 31288 KB Output is correct
17 Correct 154 ms 31188 KB Output is correct
18 Correct 242 ms 31292 KB Output is correct
19 Correct 3 ms 1104 KB Output is correct
20 Correct 3 ms 1104 KB Output is correct
21 Correct 5 ms 1104 KB Output is correct
22 Correct 5 ms 1104 KB Output is correct
23 Correct 3 ms 848 KB Output is correct
24 Correct 5 ms 1104 KB Output is correct
25 Correct 132 ms 32568 KB Output is correct
26 Correct 152 ms 32808 KB Output is correct
27 Correct 233 ms 32820 KB Output is correct
28 Correct 241 ms 32552 KB Output is correct
29 Correct 148 ms 32316 KB Output is correct
30 Correct 238 ms 32568 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 120 ms 31028 KB Output is correct
2 Correct 138 ms 31376 KB Output is correct
3 Correct 234 ms 31440 KB Output is correct
4 Correct 232 ms 31288 KB Output is correct
5 Correct 154 ms 31188 KB Output is correct
6 Correct 242 ms 31292 KB Output is correct
7 Correct 157 ms 33844 KB Output is correct
8 Correct 190 ms 34104 KB Output is correct
9 Correct 260 ms 34368 KB Output is correct
10 Correct 265 ms 34356 KB Output is correct
11 Correct 268 ms 34364 KB Output is correct
12 Correct 241 ms 32724 KB Output is correct
13 Correct 236 ms 34360 KB Output is correct
14 Correct 181 ms 32312 KB Output is correct
15 Correct 246 ms 32572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 4 ms 864 KB Output is correct
3 Correct 3 ms 1012 KB Output is correct
4 Correct 3 ms 848 KB Output is correct
5 Correct 2 ms 1012 KB Output is correct
6 Correct 4 ms 848 KB Output is correct
7 Correct 3 ms 848 KB Output is correct
8 Correct 103 ms 32312 KB Output is correct
9 Correct 104 ms 32424 KB Output is correct
10 Correct 101 ms 32320 KB Output is correct
11 Correct 127 ms 32056 KB Output is correct
12 Correct 106 ms 32312 KB Output is correct
13 Correct 104 ms 32312 KB Output is correct
14 Correct 120 ms 31028 KB Output is correct
15 Correct 138 ms 31376 KB Output is correct
16 Correct 234 ms 31440 KB Output is correct
17 Correct 232 ms 31288 KB Output is correct
18 Correct 154 ms 31188 KB Output is correct
19 Correct 242 ms 31292 KB Output is correct
20 Correct 3 ms 1104 KB Output is correct
21 Correct 3 ms 1104 KB Output is correct
22 Correct 5 ms 1104 KB Output is correct
23 Correct 5 ms 1104 KB Output is correct
24 Correct 3 ms 848 KB Output is correct
25 Correct 5 ms 1104 KB Output is correct
26 Correct 132 ms 32568 KB Output is correct
27 Correct 152 ms 32808 KB Output is correct
28 Correct 233 ms 32820 KB Output is correct
29 Correct 241 ms 32552 KB Output is correct
30 Correct 148 ms 32316 KB Output is correct
31 Correct 238 ms 32568 KB Output is correct
32 Correct 157 ms 33844 KB Output is correct
33 Correct 190 ms 34104 KB Output is correct
34 Correct 260 ms 34368 KB Output is correct
35 Correct 265 ms 34356 KB Output is correct
36 Correct 268 ms 34364 KB Output is correct
37 Correct 241 ms 32724 KB Output is correct
38 Correct 236 ms 34360 KB Output is correct
39 Correct 181 ms 32312 KB Output is correct
40 Correct 246 ms 32572 KB Output is correct
41 Correct 190 ms 35128 KB Output is correct
42 Correct 193 ms 35320 KB Output is correct
43 Correct 261 ms 35892 KB Output is correct
44 Correct 257 ms 35900 KB Output is correct
45 Correct 246 ms 35896 KB Output is correct
46 Correct 255 ms 35892 KB Output is correct
47 Correct 265 ms 35892 KB Output is correct
48 Correct 183 ms 33848 KB Output is correct
49 Correct 264 ms 34084 KB Output is correct