1119D - Frets On Fire - CodeForces Solution


binary search sortings *1800

Please click on ads to support us..

C++ Code:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define int long long
#define IOS ios::sync_with_stdio(false), cin.tie(0)
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fLL;
const double eps = 1e-7;
const int N = 1e5 + 10;
const int M = 1e6 + 10;
int n, m, a[N], b[N], pre[N];
int lower(int a[], int l, int r, int g) {
  while (l < r) {
    int m = (l + r) >> 1;
    if (a[m] >= g) {
      r = m;
    } else {
      l = m + 1;
    }
  }
  return r;
}
signed main(void) {
  IOS;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  sort(a + 1, a + n + 1);
  for (int i = 1; i <= n; ++i) {
    b[i] = a[i + 1] - a[i];
  }
  b[n] = INF;
  sort(b + 1, b + n + 1);
  for (int i = 1; i <= n; ++i) {
    pre[i] = pre[i - 1] + b[i];
  }
  cin >> m;
  while (m--) {
    int u, v;
    cin >> u >> v;
    int t = lower(b, 1, n + 1, v - u + 1);
    int ans = pre[t - 1] + (n - t + 1) * (v - u + 1);
    cout << ans << ' ';
  }
  return 0;
}


Comments

Submit
0 Comments
More Questions

1609A - Divide and Multiply
149B - Martian Clock
205A - Little Elephant and Rozdil
1609B - William the Vigilant
978B - File Name
1426B - Symmetric Matrix
732B - Cormen --- The Best Friend Of a Man
1369A - FashionabLee
1474B - Different Divisors
1632B - Roof Construction
388A - Fox and Box Accumulation
451A - Game With Sticks
768A - Oath of the Night's Watch
156C - Cipher
545D - Queue
459B - Pashmak and Flowers
1538A - Stone Game
1454C - Sequence Transformation
165B - Burning Midnight Oil
17A - Noldbach problem
1350A - Orac and Factors
1373A - Donut Shops
26A - Almost Prime
1656E - Equal Tree Sums
1656B - Subtract Operation
1656A - Good Pairs
1367A - Short Substrings
87A - Trains
664A - Complicated GCD
1635D - Infinite Set