Submission #76663437


Source Code Expand

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

constexpr int mx_n = 2 * 100005;
constexpr int mx_t = 1000006;
int n, d;
int s[mx_n], t[mx_n];
vector<int> enter_at[mx_t], leave_at[mx_t];

void solve() {
#ifdef GEN_TEST
  n = 5;
  d = 2;
  for(int i = 0; i < n; ++i) {
    s[i] = rand() % 10 + 1;
    t[i] = rand() % (11 - s[i] + 1) + s[i];
  }
  cout << n << " " << d << endl;
  for(int i = 0; i < n; ++i) {
    cout << s[i] << " " << t[i] << endl;
  }
#else
  cin >> n >> d;
  for(int i = 0; i < n; ++i) {
    cin >> s[i] >> t[i];
  }
#endif

  for(int i = 0; i < n; ++i) {
    enter_at[s[i]].push_back(i);
    leave_at[t[i]].push_back(i);
  }
  ll ans = 0;
  multiset<int> valid; // has s[i] for s[i] <= t-d;
  multiset<int> future; // has s[i] for s[i] > t-d;

  for(int t = 0; t < mx_t; ++t) {
    for(int i: enter_at[t]) {
      future.insert(s[i]);
    }
    while(future.size() > 0 && *future.begin() <= t-d) {
      valid.insert(*future.begin());
      future.erase(future.begin());
    }
    int tot = valid.size();
    ans += 1ll * tot * (tot-1) / 2;
    for(int i: leave_at[t]) {
      if(s[i] <= t-d) {
        valid.erase(valid.find(s[i]));
      } else {
        future.erase(future.find(s[i]));
      }
    }

  }

  cout << ans << endl;

}

void brute() {
  ll ans = 0;
  for(int i = 0; i < n; ++i) {
    for(int j = i+1; j < n; ++j) {
      int from = max(s[i], s[j]);
      int to = min(t[i], t[j]);
      if(from <= to) {
        ans += max(0, (to-from)-d+1);
      }
    }
  }

  cout << "BRUTE: " << ans << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int t = 1;
  // cin >> t;
  while(t--) {
    solve();
    #ifndef ONLINE_JUDGE
      brute();
    #endif
  }

  return 0;
}

Submission Info

Submission Time
Task D - Accomplice
User Balajiganapathi
Language C++23 (Clang 21.1.0)
Score 400
Code Size 1843 Byte
Status AC
Exec Time 266 ms
Memory 67296 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 3
AC × 23
Set Name Test Cases
Sample sample-01.txt, sample-02.txt, sample-03.txt
All 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, sample-01.txt, sample-02.txt, sample-03.txt
Case Name Status Exec Time Memory
04.txt AC 26 ms 49804 KiB
05.txt AC 21 ms 50016 KiB
06.txt AC 171 ms 62260 KiB
07.txt AC 170 ms 62364 KiB
08.txt AC 148 ms 62376 KiB
09.txt AC 144 ms 62284 KiB
10.txt AC 115 ms 62276 KiB
11.txt AC 21 ms 49756 KiB
12.txt AC 21 ms 50144 KiB
13.txt AC 23 ms 50400 KiB
14.txt AC 95 ms 56072 KiB
15.txt AC 187 ms 61928 KiB
16.txt AC 254 ms 67200 KiB
17.txt AC 266 ms 67176 KiB
18.txt AC 257 ms 67296 KiB
19.txt AC 248 ms 67192 KiB
20.txt AC 191 ms 62944 KiB
21.txt AC 193 ms 62824 KiB
22.txt AC 193 ms 62808 KiB
23.txt AC 231 ms 65248 KiB
sample-01.txt AC 20 ms 49812 KiB
sample-02.txt AC 20 ms 49872 KiB
sample-03.txt AC 20 ms 49928 KiB