Submission #991099

# Submission time Handle Problem Language Result Execution time Memory
991099 2024-06-01T09:33:25 Z megatron10 Sails (IOI07_sails) C++17
100 / 100
41 ms 3924 KB
#include <bits/stdc++.h>
#define ull uint64_t
#define ll long long int
#define pb push_back
#define mp make_pair
#define pi pair<int, int>
#define vi vector<int>
#define ff first
#define ss second
#define mx5 100005
#define mx52 200005
#define mx6 1000005
#define mod 1000000007
#define smod 998244353
#define nfs                     \
  ios_base::sync_with_stdio(0); \
  cin.tie(0);                   \
  cout.tie(0);
using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V>
void __print(const pair<T, V> &x)
{
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T>
void __print(const T &x)
{
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v)
{
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)             \
  cerr << "[" << #x << "] = ["; \
  _print(x)
#else
#define debug(x...)
#endif

template <typename T>
bool MinPlace(T &a, const T &b)
{
  if (a > b)
  {
    a = b;
    return true;
  }
  return false;
}

template <typename T>
bool MaxPlace(T &a, const T &b)
{
  if (a < b)
  {
    a = b;
    return true;
  }
  return false;
}

template <typename S, typename T>
ostream &operator<<(ostream &out, const pair<S, T> &p)
{
  out << "{" << p.ff << ", " << p.ss << "}";
  return out;
}

template <typename T>
ostream &operator<<(ostream &out, const vector<T> &v)
{
  out << "[";
  for (int i = 0; i < (int)v.size(); i++)
  {
    out << v[i];
    if (i != (int)v.size() - 1)
      out << ", ";
  }
  out << "]";
  return out;
}

int main()
{
  nfs;
  int n;
  cin >> n;
  vector<pi> p(n);
  for (int i = 0; i < n; i++)
    cin >> p[i].ff >> p[i].ss;
  sort(p.begin(), p.end());
  set<int> margins{1};
  vi rc(mx5, 0);

  auto fix_margins = [&](auto &&...args)
  {
    auto fix = [&](int arg)
    {
      if (rc[arg] == 0)
        margins.erase(arg);
      else
        margins.insert(arg);
    };

    // Using fold expression to call print on each argument
    (fix(std::forward<decltype(args)>(args)), ...);
  };
  for (const auto &[h, k] : p)
  {
    int low = h - k + 1;
    auto itr = margins.lower_bound(low);
    if (itr == margins.end())
    {
      int m = *(--itr);
      rc[m]++;
      rc[m + k]--;
      fix_margins(m, m + k);
      continue;
    }
    int m = *itr, pm = *(--itr);
    if (m == low)
    {
      rc[low]++;
      rc[h + 1]--;
      fix_margins(low, h + 1);
    }
    else
    {
      int down = m - low;
      rc[pm]++;
      rc[pm + down]--;
      rc[m]++;
      rc[h + 1]--;
      fix_margins(pm, pm + down, m, h + 1);
    }
  }
  ll ans = 0;
  for (int i = 1; i < mx5; i++)
  {
    rc[i] += rc[i - 1];
    ans += (1ll * rc[i] * (rc[i] - 1)) / 2;
  }
  cout << ans << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 0 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 0 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 860 KB Output is correct
2 Correct 1 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 860 KB Output is correct
2 Correct 1 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 860 KB Output is correct
2 Correct 1 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 860 KB Output is correct
2 Correct 9 ms 1116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 1628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 1568 KB Output is correct
2 Correct 27 ms 2136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 3924 KB Output is correct
2 Correct 18 ms 1372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 2128 KB Output is correct
2 Correct 17 ms 1628 KB Output is correct