Submission #757822

# Submission time Handle Problem Language Result Execution time Memory
757822 2023-06-13T19:05:09 Z taher Autobahn (COI21_autobahn) C++17
100 / 100
96 ms 9380 KB
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k, x;
  cin >> n >> k >> x;
  vector<array<int, 3>> events;
  for (int i = 0; i < n; i++) {
    int l, t, r;
    cin >> l >> t >> r;
    events.push_back({l, +1, 0});
    int s = 0;
    int p = l + t;
    if (p < r + 1) {
      s = 1;
      events.push_back({p, +1, s});
    }
    events.push_back({r + 1, -1, s});
  }
  sort(events.begin(), events.end(), [&](array<int, 3> i, array<int, 3> j) {
    if (i[0] == j[0]) {
      return i[1] < j[1];
    }
    return i[0] < j[0];
  });
  vector<array<int, 2>> d;
  auto Update = [&](int p, int add) {
    if (add == 0) {
      return ;
    }
    if ((int) d.size() > 0 && d.back()[0] == p) {
      d[(int) d.size() - 1][1] += add;
    } else {
      d.push_back({p, add});
    }
    return ;
  };
  int openGood = 0;
  int openAll = 0;
  for (int i = 0; i < (int) events.size(); i++) {
    int s = events[i][2];
    int v = events[i][1];
    int pos = events[i][0];
    if (v == +1) {
      if (s) {
        openGood += 1;
        if (openAll >= k) {
          Update(pos, +1);
        }
      } else {
        openAll += 1;
        if (openAll == k) {
          Update(pos, +openGood);
        }
      }
    } else {
      if (s) {
        openGood -= 1;
        if (openAll >= k) {
          Update(pos, -1);
        }
      }
      openAll -= 1;
      if (openAll == k - 1) {
        Update(pos, -openGood);
      }
    }
  }

  const int inf = 1000000000;

  int len = (int) d.size();

  vector<long long> prefD(len);
  vector<long long> prefP(len);
  for (int i = 0; i < len; i++) {
    if (i > 0) {
      prefD[i] += prefD[i - 1];
      prefP[i] += prefP[i - 1];
    }
    prefD[i] += d[i][1];
    prefP[i] += 1LL * d[i][0] * d[i][1];
  }

  auto Calc = [&](int ptRight) {
    auto ptr0 = lower_bound(d.begin(), d.end(), array<int, 2> ({ptRight - x, inf})) - d.begin();
    auto ptr1 = lower_bound(d.begin(), d.end(), array<int, 2> ({ptRight, inf})) - d.begin();
    ptr1 -= 1;
    if (ptr1 < 0) return 0LL;
    long long ret = 1LL * (ptRight + 1) * (prefD[ptr1] - (ptr0 > 0 ? prefD[ptr0 - 1] : 0LL)) - (prefP[ptr1] - (ptr0 > 0 ? prefP[ptr0 - 1] : 0LL));
    if (ptr0 > 0) {
      assert(ptRight > x);
      ret += 1LL * prefD[ptr0 - 1] * x;
    }
    return ret;
  };

  long long res = 0;

  for (int i = 0; i < (int) d.size(); i++) {
    int pos = d[i][0];
    int c = d[i][1];
    if (c < 0) {
      res = max(res, Calc(pos - 1));
    }
  }

  cout << res << '\n';
  return 0; 
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 320 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 320 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 328 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 324 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 320 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 328 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 324 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 328 KB Output is correct
21 Correct 77 ms 9380 KB Output is correct
22 Correct 84 ms 9352 KB Output is correct
23 Correct 72 ms 9332 KB Output is correct
24 Correct 69 ms 9372 KB Output is correct
25 Correct 96 ms 9308 KB Output is correct
26 Correct 94 ms 9320 KB Output is correct
27 Correct 73 ms 9244 KB Output is correct
28 Correct 87 ms 9376 KB Output is correct
29 Correct 84 ms 9332 KB Output is correct